Add absolutely minimal implementation of a DHT routing table

This commit is contained in:
Mark Spanbroek 2021-01-20 12:54:19 +01:00 committed by markspanbroek
parent 7b15a6ae83
commit b3f5599925
3 changed files with 29 additions and 0 deletions

11
ipfs/dht/routing.nim Normal file
View File

@ -0,0 +1,11 @@
import pkg/libp2p
type
RoutingTable* = object
peers: seq[PeerInfo]
proc add*(table: var RoutingTable, peer: PeerInfo) =
table.peers.add(peer)
proc closest*(table: RoutingTable, id: Cid): seq[PeerInfo] =
table.peers

View File

@ -0,0 +1,17 @@
import std/unittest
import pkg/libp2p
import pkg/ipfs/ipfsobject
import pkg/ipfs/dht/routing
suite "DHT routing table":
test "finds peers closest to some content":
let peer1 = PeerInfo(peer: PeerId(data: @[1'u8]))
let peer2 = PeerInfo(peer: PeerId(data: @[2'u8]))
let contentId = IpfsObject(data: @[]).cid
var table = RoutingTable()
table.add(peer1)
table.add(peer2)
check table.closest(contentId) == @[peer1, peer2]

View File

@ -1,6 +1,7 @@
import ./ipfs/testObject
import ./ipfs/testChunking
import ./ipfs/testRepo
import ./ipfs/testDhtRouting
import ./ipfs/testIpfs
{.warning[UnusedImport]: off.}