finished closeness measurement
This commit is contained in:
		
							parent
							
								
									c4be08bd23
								
							
						
					
					
						commit
						da11c74f6c
					
				
					 6 changed files with 92 additions and 11 deletions
				
			
		|  | @ -8,6 +8,8 @@ LoopBackIP = NewType('LoopBackIP', str) | |||
| 
 | ||||
| DeterministicKeyPassphrase = NewType('DeterministicKeyPassphrase', str) | ||||
| 
 | ||||
| Ed25519PublicKeyBytes = NewType('Ed25519PublicKeyBytes', bytes) | ||||
| 
 | ||||
| BlockHash = NewType('BlockHash', str) | ||||
| 
 | ||||
| OnboardingConfig = NewType('OnboardingConfig', str) | ||||
|  |  | |||
|  | @ -1 +1,2 @@ | |||
| from .extracted25519 import extract_ed25519_from_onion_address | ||||
| from .extracted25519 import extract_ed25519_from_onion_address | ||||
| from .neighbors import identify_neighbors | ||||
|  | @ -8,7 +8,7 @@ if TYPE_CHECKING: | |||
| 
 | ||||
| 
 | ||||
| def extract_ed25519_from_onion_address( | ||||
|         address: OnionAddressString) -> Ed25519PublicKeyBytes: | ||||
|         address: 'OnionAddressString') -> 'Ed25519PublicKeyBytes': | ||||
|     address = str_to_bytes(address).replace(b'.onion', b'').upper() | ||||
|     ed25519 = b32decode(address)[:-3] | ||||
|     return ed25519 | ||||
|  | @ -1,5 +1,8 @@ | |||
| from onionrtypes import OnionAddressString | ||||
| from typing import Iterable | ||||
| 
 | ||||
| from collections import OrderedDict | ||||
| 
 | ||||
| from .extracted25519 import extract_ed25519_from_onion_address | ||||
| 
 | ||||
| 
 | ||||
|  | @ -12,11 +15,15 @@ def identify_neighbors( | |||
|     address_int = int.from_bytes(address, "big") | ||||
| 
 | ||||
|     def _calc_closeness(y): | ||||
|         return abs(address_int - int.from_bytes(y, "big")) | ||||
| 
 | ||||
| 
 | ||||
|     peer_ed_keys = list(map(extract_ed25519_from_onion_address, peers)) | ||||
|     differences = list(map(_calc_closeness, peer_ed_keys)) | ||||
| 
 | ||||
|     return sorted(differences)[:closest_n] | ||||
|         return abs(address_int - int.from_bytes(extract_ed25519_from_onion_address(y), "big")) | ||||
| 
 | ||||
|     closeness_values = [] | ||||
|     end_result = [] | ||||
|     for peer in peers: | ||||
|         closeness_values.append((peer, _calc_closeness(peer))) | ||||
|     closeness_values.sort() | ||||
|     for i, result in enumerate(closeness_values): | ||||
|         if i > closest_n: | ||||
|             break | ||||
|         end_result.append(result[0]) | ||||
|     return end_result | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue