Get distance of trust with bfs
This commit is contained in:
parent
86615305d7
commit
c0d3b367dc
@ -3,6 +3,7 @@ from threading import Thread
|
||||
from queue import Queue
|
||||
from queue import Empty
|
||||
|
||||
from onionrplugins.onionrevents import event
|
||||
import blockdb
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@ -32,9 +33,8 @@ def store_blocks(dandelion_phase: 'DandelionPhase'):
|
||||
while not dandelion_phase.is_stem_phase() \
|
||||
and dandelion_phase.remaining_time() > 1:
|
||||
try:
|
||||
blockdb.add_block_to_db(
|
||||
new_queue.get(timeout=dandelion_phase.remaining_time() + 1)
|
||||
)
|
||||
bl = new_queue.get(timeout=dandelion_phase.remaining_time() + 1)
|
||||
blockdb.add_block_to_db(bl)
|
||||
event('gotblock', data=bl, threaded=True)
|
||||
except Empty:
|
||||
pass
|
||||
|
||||
|
@ -0,0 +1,40 @@
|
||||
import os, uuid
|
||||
from random import randint
|
||||
from sqlite3 import Time
|
||||
import socket
|
||||
from queue import Queue
|
||||
from time import sleep
|
||||
import secrets
|
||||
|
||||
|
||||
TEST_DIR = 'testdata/%s-%s' % (str(uuid.uuid4())[:6], os.path.basename(__file__)) + '/'
|
||||
print("Test directory:", TEST_DIR)
|
||||
os.environ["ONIONR_HOME"] = TEST_DIR
|
||||
|
||||
import unittest
|
||||
import sys
|
||||
sys.path.append(".")
|
||||
sys.path.append('static-data/default-plugins/wot/')
|
||||
sys.path.append("src/")
|
||||
from wot import identity
|
||||
|
||||
|
||||
def generate_graph(iden: identity.Identity, depth, max_neighbors):
|
||||
c = 0
|
||||
if depth == 0:
|
||||
return
|
||||
for i in range(randint(0, max_neighbors)):
|
||||
i = identity.Identity(secrets.token_hex(16))
|
||||
iden.trusted.add(i)
|
||||
generate_graph(i, depth - 1, max_neighbors)
|
||||
|
||||
|
||||
class IdentityDistanceTest(unittest.TestCase):
|
||||
def test_distance(self):
|
||||
iden = identity.Identity(secrets.token_hex(16))
|
||||
generate_graph(iden, 10, 5)
|
||||
iden2 = list(list(iden.trusted)[0].trusted)[0]
|
||||
|
||||
self.assertEqual(identity.get_distance(iden, iden2), 2)
|
||||
|
||||
unittest.main()
|
@ -0,0 +1,2 @@
|
||||
# The web of trust is a graph of identities where each edge is a signature
|
||||
# of a byte representing a trust level and an identity's public key
|
42
static-data/default-plugins/wot/wot/identity.py
Normal file
42
static-data/default-plugins/wot/wot/identity.py
Normal file
@ -0,0 +1,42 @@
|
||||
from collections import deque
|
||||
from typing import Set, Union, List
|
||||
|
||||
|
||||
identities: List['Identity'] = []
|
||||
|
||||
|
||||
class Identity:
|
||||
def __init__(self, key: Union['Ed25519PublicKey', 'Ed25519PrivateKey']):
|
||||
self.trusted: Set[Identity] = set()
|
||||
self.key = key
|
||||
|
||||
def __eq__(self, other):
|
||||
return self.key == other
|
||||
|
||||
def __str__(self):
|
||||
return self.key
|
||||
|
||||
def __hash__(self):
|
||||
return hash(self.key)
|
||||
|
||||
|
||||
def get_distance(identity: Identity, identity2: Identity):
|
||||
distance = 0
|
||||
visited = set()
|
||||
stack = deque([identity])
|
||||
|
||||
while stack:
|
||||
current_iden = stack.popleft()
|
||||
|
||||
if current_iden == identity2:
|
||||
return distance
|
||||
distance += 1
|
||||
|
||||
if identity2 in current_iden.trusted:
|
||||
return distance
|
||||
|
||||
for trusted in current_iden.trusted:
|
||||
if trusted not in visited:
|
||||
visited.add(trusted)
|
||||
stack.append(trusted)
|
||||
raise ValueError
|
@ -8,19 +8,11 @@
|
||||
"general": {
|
||||
"allow_public_api_dns_rebinding": false,
|
||||
"announce_node": true,
|
||||
"bind_address": "",
|
||||
"dev_mode": false,
|
||||
"display_header": true,
|
||||
"ephemeral_tunnels": false,
|
||||
"insert_deniable_blocks": true,
|
||||
"max_block_age": 2678400,
|
||||
"minimum_block_pow": 5,
|
||||
"minimum_send_pow": 5,
|
||||
"public_key": "",
|
||||
"random_bind_ip": true,
|
||||
"security_level": 0,
|
||||
"show_notifications": true,
|
||||
"store_plaintext_blocks": true,
|
||||
"use_bootstrap_list": true
|
||||
},
|
||||
"log": {
|
||||
@ -34,50 +26,14 @@
|
||||
},
|
||||
"verbosity": "default"
|
||||
},
|
||||
"onboarding": {
|
||||
"done": false
|
||||
},
|
||||
"peers": {
|
||||
"max_connect": 1000,
|
||||
"max_stored_peers": 10000000,
|
||||
"minimum_score": -100
|
||||
},
|
||||
"plugins": {
|
||||
"disabled": ["unixtransport"],
|
||||
"enabled": []
|
||||
},
|
||||
"statistics": {
|
||||
"i_dont_want_privacy": false,
|
||||
"server": ""
|
||||
},
|
||||
"security": {
|
||||
"dandelion": {
|
||||
"strict": true,
|
||||
"enabled": true
|
||||
}
|
||||
},
|
||||
"timers": {
|
||||
"getBlocks": 10,
|
||||
"lookupBlocks": 25
|
||||
},
|
||||
"tor": {
|
||||
"bridge_fingerprint": "",
|
||||
"bridge_ip": "",
|
||||
"existing_control_password": "",
|
||||
"existing_control_port": 0,
|
||||
"existing_socks_port": 0,
|
||||
"use_bridge": false,
|
||||
"use_existing_tor": false
|
||||
},
|
||||
"transports": {
|
||||
"lan": true,
|
||||
"sneakernet": true,
|
||||
"tor": true
|
||||
},
|
||||
"ui": {
|
||||
"animated_background": true,
|
||||
"public_remote_enabled": false,
|
||||
"public_remote_hosts": [],
|
||||
"theme": "dark"
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user