From b3f5599925d4a5198d47f6330e8fb2f121a55242 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Wed, 20 Jan 2021 12:54:19 +0100 Subject: [PATCH] Add absolutely minimal implementation of a DHT routing table --- ipfs/dht/routing.nim | 11 +++++++++++ tests/ipfs/testDhtRouting.nim | 17 +++++++++++++++++ tests/testAll.nim | 1 + 3 files changed, 29 insertions(+) create mode 100644 ipfs/dht/routing.nim create mode 100644 tests/ipfs/testDhtRouting.nim diff --git a/ipfs/dht/routing.nim b/ipfs/dht/routing.nim new file mode 100644 index 00000000..59d7ca54 --- /dev/null +++ b/ipfs/dht/routing.nim @@ -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 diff --git a/tests/ipfs/testDhtRouting.nim b/tests/ipfs/testDhtRouting.nim new file mode 100644 index 00000000..82a3abfa --- /dev/null +++ b/tests/ipfs/testDhtRouting.nim @@ -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] diff --git a/tests/testAll.nim b/tests/testAll.nim index 6059f90f..d1bae747 100644 --- a/tests/testAll.nim +++ b/tests/testAll.nim @@ -1,6 +1,7 @@ import ./ipfs/testObject import ./ipfs/testChunking import ./ipfs/testRepo +import ./ipfs/testDhtRouting import ./ipfs/testIpfs {.warning[UnusedImport]: off.}