https://github.com/cortze/py-dht
Python module that can be used to simulate how a Kademlia based DHT could be integrated into Ethereum to help with the data-sharding schema
Science Score: 26.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
○Academic publication links
-
○Committers with academic emails
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (10.6%) to scientific vocabulary
Keywords from Contributors
Repository
Python module that can be used to simulate how a Kademlia based DHT could be integrated into Ethereum to help with the data-sharding schema
Basic Info
Statistics
- Stars: 7
- Watchers: 3
- Forks: 1
- Open Issues: 0
- Releases: 2
Metadata Files
README.md
py-dht · 
small and straightforward representation of how a kademlia-based dht could be integrated into ethereum, particularly at das-research.
What can this dht do?
the current work on this simulation of a kademlia-based dht network offers:
DHTNewtorkobject that can: spawndhtclients, serve as main source to initialize therouting tableof thedhtclients, resolveconnectionsbetweendhtclients, handle and keep track of the interactions betweendhtclients. the parameters to configure the adhtnetworkare:networkid: in case we want to simulate different network at the same timeerrorrate: to define the number connections that will fall into an error (we can understand it as the likelines o f an error in %)delayrage: range between the slowest possible delay and the biggest one. a random delay will be selected every time a connection is stablished between 2 nodes (if no error is raised)
the network offers the following functions:
- parallel_clilist_initializer
- init_with_random_peers initializes a network using a "blazingly fast" method, which can be optimized even more if
a number of threads/processes is defined
- add_new_node adds a new node to the local Network
- connect_to_node returns the Connection obj between node A and B
- bootstrap_node return the "best" nodes/dhtclis to compose the routing table for the given node
- summary return the summary of the current status of the network (number of nodes, successful connections, failed
ones, etc), will evolve over time
Connectioninterface that limits how twodhtclientsinteract with each other (like if it was a closed api/protocol). it offers the possibility to clienta(client starting the connection) to ask clientb(remote client), applying if specified the delay at the moment of stablishing the connection and per each interaction:get_closest_nodes_to(hash)will return the k closest nodes to the givenhashthat clientbhas in it's routing table. note: it will also return the value if it's stored locally :)store_segment(segment)will add thehashand the segment as a key-value pair inb's local storageretrieve_segment(hash)will askbto return the segment of the givenhashif it has it
DHTClientas representation of a node in the simulated dht network. thedhtclientcan be created using the following parameters:nodeid: id of the node that hosts thedhtclientnetwork: referece to the network obj that where thedhtclientparticipates inkbucketsize: k value, number of nodes per kbucketa: number of concurrent node connections the client does while looking for a given keyb: target of nodes (number of nodes) returned when asking for ahashsteptostop: number of iterations without finding anyone closer to stop thelookupoperation
the client serves a list of endpoints such as:
- bootstrap uses the network reference to find the right peers for the routing table
- lookup_for_hash will try to look the value of the hash in the network, and the closest nodes to it
- get_closest_nodes_to will return the closest nodes to a hash from the local routing table
- provide_block_segment will lookup for the closest nodes in the network, and store the segment on them
- store_segment will store locally a segment value using its hash as key
- retrieve_segment will return the value of a hash if its locally, exception raised otherwise
RoutingTableandKBucketclasses to store locally the local representation of the network for a given nodeHashandBitArrayclasses to represent anodeid/blocksegment/generalobject
Dependencies
The source code runs mostly on plain Python libraries. However, to speed up the performance, the plain arrays and dicts
were updated to classes from collections. Thus, I recomend to have an specific virtual environment to use the module.
To install the dependencies, do: ```shell
python -m venv venv
or
python -m virtualenv venv
source venv/bin/activate
(venv)$ pip install -r requirements.txt ```
Tests
The repo has a list of tests to ensure that no functionality is broken whenever a new feature is added. All the tests are
triggered whenever GitHub records a Push or a PullRequest. However, there are locally runable using the ./launch_test.sh
script
```shell
the script will try to activate any venv located at the root of the directory
py-dht$ bash launch_tests.sh ```
Benchmarks
Running benchmarks is a bit trickier. First install the py-dht module as editable in the venv
shell
py-dht$ pip install -e ./
after that, feel free to change the parameters in the benchmarks/launch_benchmarks.sh and run it like if it was a test:
```shell
the script will try to activate any venv located at the root of the directory
py-dht$ cd benchmarks py-dht/benchmarks$ bash launch_benchmarks.sh ```
Numbers and recomendations
From the experience of running tests and benchmarks on the repo, I can say that the optimizations on #8 and #9 were more than necesary.
Recomendations:
- to simulate a network -> use the network.init_with_random_peers() function setting the processes parameters
the initialization of the network is by far the process that takes the longer, as it has to compute the best routing tables
for each of the spawned nodes. So, please benefit from the concurrency to reduce the duration
(check this test as example)
- At the moment using 20 cores of a ryzen 5900x I'm able to initialize a network of 10k dhtclients with k=20 in 28 secs
Latest numbers of the network becnhmark
```shell
03.08.2023
py-dht$ python benchmarks/network.py -t opt-conc -o ./csvs -i 1 -k 20 -n 10000 --threads 20 -- benchmark: opt-concnetworkninitialization -- rounds : 1 failed rounds : 0 prep time (s) : 1.5974044799804688e-05 avg (s) : 0.09420228004455566 median (s) : 0.09420228004455566 p90duration (s): 0.09420228004455566 -- benchmark: opt-concnetworkbootstrapnode -- rounds : 1 failed rounds : 0 prep time (s) : 0.11602234840393066 avg (s) : 0.6563236713409424 median (s) : 0.6563236713409424 p90duration (s): 0.6563236713409424 -- benchmark: opt-concnetworkfastbootstrapnetwork -- rounds : 1 failed rounds : 0 prep time (s) : 8.344650268554688e-06 avg (s) : 174.6814157962799 median (s) : 174.6814157962799 p90duration (s): 174.6814157962799 -- benchmark: opt-concnetworkfastthreadedbootstrapnetwork -- rounds : 1 failed rounds : 0 prep time (s) : 0.0008330345153808594 avg (s) : 28.239949226379395 median (s) : 28.239949226379395 p90_duration (s): 28.239949226379395 ``` At the moment (after applying the code optimizations in #8 and the conncurrency #9) the numbers look like this:
| Number of nodes | Default implementation | 1st Optimization (monothread) | 2nd Optimization (8 processes) | |-----------------|------------------------|-------------------------------|--------------------------------| | 500 | 10 | 2.096 | 0.489 | | 1000 | 39 | 5.39 | 1.02 | | 1200 | 56 | 7.22 | 1.48 | | 2000 | 157.36 | 14.49 | 2.83 | | 5000 | 995.73 | 60.01 | 11.15 | | 10000 | (beyond 2 hours) | 199.97 | 39.15 | NOTE: all the measurements displayed in the table are expresed in seconds

Maintainer
Mikel Cortes-Goicoechea - @cortze
Contributing
feel free to dive in! change proposals, issues, and prs will be more than welcome.
Support
- the work has been supported by codex
- feel free to support this project through buy me a coffee; help me getting the ship of caffeine that I need 😊.
License
mit, see license file
Owner
- Name: Mikel Cortes
- Login: cortze
- Kind: user
- Location: Barcelona
- Company: Barcelona Supercomputing Center
- Repositories: 8
- Profile: https://github.com/cortze
Research Engineer and Ph.D. student on Distributed P2P Networks. LIbp2p, IPFS, GossipSub, Eth CL, and more
GitHub Events
Total
- Watch event: 2
Last Year
- Watch event: 2
Committers
Last synced: almost 2 years ago
Top Committers
| Name | Commits | |
|---|---|---|
| cortze | c****e@p****m | 90 |
| Mikel Cortes | 4****e | 4 |
| cortze | m****3@g****m | 2 |
Issues and Pull Requests
Last synced: over 1 year ago
All Time
- Total issues: 4
- Total pull requests: 26
- Average time to close issues: 5 days
- Average time to close pull requests: about 14 hours
- Total issue authors: 1
- Total pull request authors: 1
- Average comments per issue: 1.25
- Average comments per pull request: 0.0
- Merged pull requests: 26
- Bot issues: 0
- Bot pull requests: 0
Past Year
- Issues: 0
- Pull requests: 0
- Average time to close issues: N/A
- Average time to close pull requests: N/A
- Issue authors: 0
- Pull request authors: 0
- Average comments per issue: 0
- Average comments per pull request: 0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0
Top Authors
Issue Authors
- cortze (4)
Pull Request Authors
- cortze (24)
Top Labels
Issue Labels
Pull Request Labels
Dependencies
- actions/checkout v3 composite
- actions/setup-python v3 composite
- bitarray *
- bitarray ==2.8.0