lbry-sdk/lbrynet/dht/constants.py

41 lines
1,003 B
Python
Raw Normal View History

2019-01-22 18:49:43 +01:00
import hashlib
import os
2015-08-20 17:27:15 +02:00
2019-01-22 18:49:43 +01:00
hash_class = hashlib.sha384
hash_length = hash_class().digest_size
hash_bits = hash_length * 8
alpha = 5
2015-08-20 17:27:15 +02:00
k = 8
2019-01-22 18:49:43 +01:00
replacement_cache_size = 8
rpc_timeout = 5.0
rpc_attempts = 5
rpc_attempts_pruning_window = 600
iterative_lookup_delay = rpc_timeout / 2.0 # TODO: use config val / 2 if rpc timeout is provided
2019-01-22 18:49:43 +01:00
refresh_interval = 3600 # 1 hour
replicate_interval = refresh_interval
data_expiration = 86400 # 24 hours
token_secret_refresh_interval = 300 # 5 minutes
check_refresh_interval = refresh_interval / 5
max_datagram_size = 8192 # 8 KB
rpc_id_length = 20
protocol_version = 1
bottom_out_limit = 3
msg_size_limit = max_datagram_size - 26
2015-08-20 17:27:15 +02:00
2019-01-22 18:49:43 +01:00
def digest(data: bytes) -> bytes:
h = hash_class()
h.update(data)
return h.digest()
2015-08-20 17:27:15 +02:00
2019-01-22 18:49:43 +01:00
def generate_id(num=None) -> bytes:
if num is not None:
return digest(str(num).encode())
else:
return digest(os.urandom(32))
2017-10-25 02:05:30 +02:00
2019-01-22 18:49:43 +01:00
def generate_rpc_id(num=None) -> bytes:
return generate_id(num)[:rpc_id_length]