From 4a831a4a4d0042db349445b1218eceb51836119a Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Mon, 26 Oct 2020 06:11:24 +0000 Subject: [PATCH] fixed closeness calculation --- scripts/closeness-calculator.py | 21 +++++++++++++++++++++ src/streamfill/neighbors.py | 25 ++++++++++++++++++------- tests/test_streamfill_neighbors.py | 20 +++++++------------- 3 files changed, 46 insertions(+), 20 deletions(-) create mode 100644 scripts/closeness-calculator.py diff --git a/scripts/closeness-calculator.py b/scripts/closeness-calculator.py new file mode 100644 index 00000000..cd162fbd --- /dev/null +++ b/scripts/closeness-calculator.py @@ -0,0 +1,21 @@ +import sys +import os +import subprocess +import base64 +if not os.path.exists('onionr.sh'): + os.chdir('../') +sys.path.append("src/") +from streamfill import identify_neighbors + +onions = [] +p = subprocess.Popen(["scripts/generate-onions.py", '5'], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) +for line in iter(p.stdout.readline, b''): + line = line.decode() + onions.append(line.strip()) + + +for onion in onions: + print(onion, identify_neighbors(onion, onions, 3)) + diff --git a/src/streamfill/neighbors.py b/src/streamfill/neighbors.py index 39285cab..10263872 100644 --- a/src/streamfill/neighbors.py +++ b/src/streamfill/neighbors.py @@ -10,20 +10,31 @@ def identify_neighbors( address: OnionAddressString, peers: Iterable[OnionAddressString], closest_n: int) -> OnionAddressString: + """Identify node addresses that are closest + in value to a given node address""" + peers_to_test = list(peers) + + try: + peers_to_test.remove(address) + except ValueError: + pass address = extract_ed25519_from_onion_address(address) address_int = int.from_bytes(address, "big") - - def _calc_closeness(y): - return abs(address_int - int.from_bytes(extract_ed25519_from_onion_address(y), "big")) - closeness_values = [] end_result = [] - for peer in peers: + + def _calc_closeness(y): + ret = abs( + address_int - + int.from_bytes(extract_ed25519_from_onion_address(y), "big")) + return ret + + for peer in peers_to_test: closeness_values.append((peer, _calc_closeness(peer))) - closeness_values.sort() + closeness_values.sort(key=lambda p: p[1]) for i, result in enumerate(closeness_values): + end_result.append(result[0]) if i > closest_n: break - end_result.append(result[0]) return end_result diff --git a/tests/test_streamfill_neighbors.py b/tests/test_streamfill_neighbors.py index 5f33abb5..8471fa53 100644 --- a/tests/test_streamfill_neighbors.py +++ b/tests/test_streamfill_neighbors.py @@ -15,13 +15,6 @@ from streamfill import identify_neighbors, extract_ed25519_from_onion_address class TestStreamfillNeighbors(unittest.TestCase): def test_neighbor_closeness_consistent(self): - onions = [] - # p = subprocess.Popen(["scripts/generate-onions.py", '5'], - # stdout=subprocess.PIPE, - # stderr=subprocess.PIPE) - # for line in iter(p.stdout.readline, b''): - # line = line.decode() - # onions.append(line.strip()) main = '7uifxsgidchopmdwmtip6x4ydra6cpf2ov4ghj2lzx5uydyssduh5qid.onion' others = ['bxxajpimlonmbxb5jzjre3go3dvfobqyayqwpksd6zpjz4s4mknstwyd.onion', '2zofaifd6s3flwbv5wl4vtgnesbprc4f2ptljl4a47dfkvrbmw3e5iqd.onion', '6umslj7jtzu27n4jgf3byn55ztz5mkoqocx32zwjya6rbnxqjpyysyyd.onion'] main_num = int.from_bytes(extract_ed25519_from_onion_address(main), 'big') @@ -31,12 +24,13 @@ class TestStreamfillNeighbors(unittest.TestCase): my_result = [] for i in others: my_result.append((i, abs(main_num - int.from_bytes(extract_ed25519_from_onion_address(i), 'big')))) - my_result.sort() + my_result.sort(key=lambda p: p[1]) + final = [] for i in my_result: final.append(i[0]) - self.assertTrue(len(test_data) == 3) - self.assertListEqual(test_data, final) + self.assertListEqual(final, test_data) + def test_neighbor_closeness_random(self): onions = [] @@ -55,12 +49,12 @@ class TestStreamfillNeighbors(unittest.TestCase): my_result = [] for i in onions: my_result.append((i, abs(main_num - int.from_bytes(extract_ed25519_from_onion_address(i), 'big')))) - my_result.sort() + my_result.sort(key=lambda p: p[1]) + final = [] for i in my_result: final.append(i[0]) - self.assertTrue(len(test_data) == 100) - self.assertListEqual(test_data, final) + self.assertListEqual(final, test_data) unittest.main()