fixed closeness calculation
parent
57ec04eff5
commit
4a831a4a4d
|
@ -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))
|
||||||
|
|
|
@ -10,20 +10,31 @@ def identify_neighbors(
|
||||||
address: OnionAddressString,
|
address: OnionAddressString,
|
||||||
peers: Iterable[OnionAddressString],
|
peers: Iterable[OnionAddressString],
|
||||||
closest_n: int) -> 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 = extract_ed25519_from_onion_address(address)
|
||||||
address_int = int.from_bytes(address, "big")
|
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 = []
|
closeness_values = []
|
||||||
end_result = []
|
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.append((peer, _calc_closeness(peer)))
|
||||||
closeness_values.sort()
|
closeness_values.sort(key=lambda p: p[1])
|
||||||
for i, result in enumerate(closeness_values):
|
for i, result in enumerate(closeness_values):
|
||||||
|
end_result.append(result[0])
|
||||||
if i > closest_n:
|
if i > closest_n:
|
||||||
break
|
break
|
||||||
end_result.append(result[0])
|
|
||||||
return end_result
|
return end_result
|
||||||
|
|
|
@ -15,13 +15,6 @@ from streamfill import identify_neighbors, extract_ed25519_from_onion_address
|
||||||
|
|
||||||
class TestStreamfillNeighbors(unittest.TestCase):
|
class TestStreamfillNeighbors(unittest.TestCase):
|
||||||
def test_neighbor_closeness_consistent(self):
|
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'
|
main = '7uifxsgidchopmdwmtip6x4ydra6cpf2ov4ghj2lzx5uydyssduh5qid.onion'
|
||||||
others = ['bxxajpimlonmbxb5jzjre3go3dvfobqyayqwpksd6zpjz4s4mknstwyd.onion', '2zofaifd6s3flwbv5wl4vtgnesbprc4f2ptljl4a47dfkvrbmw3e5iqd.onion', '6umslj7jtzu27n4jgf3byn55ztz5mkoqocx32zwjya6rbnxqjpyysyyd.onion']
|
others = ['bxxajpimlonmbxb5jzjre3go3dvfobqyayqwpksd6zpjz4s4mknstwyd.onion', '2zofaifd6s3flwbv5wl4vtgnesbprc4f2ptljl4a47dfkvrbmw3e5iqd.onion', '6umslj7jtzu27n4jgf3byn55ztz5mkoqocx32zwjya6rbnxqjpyysyyd.onion']
|
||||||
main_num = int.from_bytes(extract_ed25519_from_onion_address(main), 'big')
|
main_num = int.from_bytes(extract_ed25519_from_onion_address(main), 'big')
|
||||||
|
@ -31,12 +24,13 @@ class TestStreamfillNeighbors(unittest.TestCase):
|
||||||
my_result = []
|
my_result = []
|
||||||
for i in others:
|
for i in others:
|
||||||
my_result.append((i, abs(main_num - int.from_bytes(extract_ed25519_from_onion_address(i), 'big'))))
|
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 = []
|
final = []
|
||||||
for i in my_result:
|
for i in my_result:
|
||||||
final.append(i[0])
|
final.append(i[0])
|
||||||
self.assertTrue(len(test_data) == 3)
|
self.assertListEqual(final, test_data)
|
||||||
self.assertListEqual(test_data, final)
|
|
||||||
|
|
||||||
def test_neighbor_closeness_random(self):
|
def test_neighbor_closeness_random(self):
|
||||||
onions = []
|
onions = []
|
||||||
|
@ -55,12 +49,12 @@ class TestStreamfillNeighbors(unittest.TestCase):
|
||||||
my_result = []
|
my_result = []
|
||||||
for i in onions:
|
for i in onions:
|
||||||
my_result.append((i, abs(main_num - int.from_bytes(extract_ed25519_from_onion_address(i), 'big'))))
|
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 = []
|
final = []
|
||||||
for i in my_result:
|
for i in my_result:
|
||||||
final.append(i[0])
|
final.append(i[0])
|
||||||
self.assertTrue(len(test_data) == 100)
|
self.assertListEqual(final, test_data)
|
||||||
self.assertListEqual(test_data, final)
|
|
||||||
|
|
||||||
|
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
Loading…
Reference in New Issue