2021-02-08 21:16:23 +00:00
|
|
|
import
|
2021-06-08 16:55:24 +00:00
|
|
|
unittest2,
|
|
|
|
std/[tables, sequtils, sets],
|
2021-02-08 21:16:23 +00:00
|
|
|
../libp2p/crypto/crypto,
|
|
|
|
../libp2p/multiaddress,
|
|
|
|
../libp2p/peerid,
|
|
|
|
../libp2p/peerstore,
|
|
|
|
./helpers
|
|
|
|
|
|
|
|
suite "PeerStore":
|
|
|
|
# Testvars
|
|
|
|
let
|
|
|
|
# Peer 1
|
|
|
|
keyPair1 = KeyPair.random(ECDSA, rng[]).get()
|
2021-12-16 10:05:20 +00:00
|
|
|
peerId1 = PeerId.init(keyPair1.seckey).get()
|
2021-02-08 21:16:23 +00:00
|
|
|
multiaddrStr1 = "/ip4/127.0.0.1/udp/1234/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC"
|
|
|
|
multiaddr1 = MultiAddress.init(multiaddrStr1).get()
|
|
|
|
testcodec1 = "/nim/libp2p/test/0.0.1-beta1"
|
|
|
|
# Peer 2
|
|
|
|
keyPair2 = KeyPair.random(ECDSA, rng[]).get()
|
2021-12-16 10:05:20 +00:00
|
|
|
peerId2 = PeerId.init(keyPair2.seckey).get()
|
2021-02-08 21:16:23 +00:00
|
|
|
multiaddrStr2 = "/ip4/0.0.0.0/tcp/1234/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC"
|
|
|
|
multiaddr2 = MultiAddress.init(multiaddrStr2).get()
|
|
|
|
testcodec2 = "/nim/libp2p/test/0.0.2-beta1"
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2021-02-08 21:16:23 +00:00
|
|
|
test "PeerStore API":
|
|
|
|
# Set up peer store
|
|
|
|
var
|
|
|
|
peerStore = PeerStore.new()
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2022-05-25 10:12:57 +00:00
|
|
|
peerStore[AddressBook][peerId1] = @[multiaddr1]
|
|
|
|
peerStore[AddressBook][peerId2] = @[multiaddr2]
|
|
|
|
peerStore[ProtoBook][peerId1] = @[testcodec1]
|
|
|
|
peerStore[ProtoBook][peerId2] = @[testcodec2]
|
|
|
|
peerStore[KeyBook][peerId1] = keyPair1.pubkey
|
|
|
|
peerStore[KeyBook][peerId2] = keyPair2.pubkey
|
|
|
|
|
|
|
|
# Test PeerStore::del
|
|
|
|
# Delete existing peerId
|
|
|
|
peerStore.del(peerId1)
|
|
|
|
check peerId1 notin peerStore[AddressBook]
|
|
|
|
# Now try and del it again
|
|
|
|
peerStore.del(peerId1)
|
2021-02-08 21:16:23 +00:00
|
|
|
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2021-02-08 21:16:23 +00:00
|
|
|
test "PeerStore listeners":
|
|
|
|
# Set up peer store with listener
|
|
|
|
var
|
|
|
|
peerStore = PeerStore.new()
|
|
|
|
addrChanged = false
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2022-05-25 10:12:57 +00:00
|
|
|
proc addrChange(peerId: PeerId) {.gcsafe.} =
|
2021-02-08 21:16:23 +00:00
|
|
|
addrChanged = true
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2022-05-25 10:12:57 +00:00
|
|
|
peerStore[AddressBook].addHandler(addrChange)
|
2021-02-08 21:16:23 +00:00
|
|
|
|
|
|
|
# Test listener triggered on adding multiaddr
|
2022-05-25 10:12:57 +00:00
|
|
|
peerStore[AddressBook][peerId1] = @[multiaddr1]
|
|
|
|
check: addrChanged == true
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2021-02-08 21:16:23 +00:00
|
|
|
addrChanged = false
|
|
|
|
check:
|
2022-05-25 10:12:57 +00:00
|
|
|
peerStore[AddressBook].del(peerId1) == true
|
2021-02-08 21:16:23 +00:00
|
|
|
addrChanged == true
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2022-05-25 10:12:57 +00:00
|
|
|
test "PeerBook API":
|
2021-02-08 21:16:23 +00:00
|
|
|
# Set up address book
|
2022-05-25 10:12:57 +00:00
|
|
|
var addressBook = PeerStore.new()[AddressBook]
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2021-02-08 21:16:23 +00:00
|
|
|
# Test AddressBook::add
|
2022-05-25 10:12:57 +00:00
|
|
|
addressBook[peerId1] = @[multiaddr1]
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2021-02-08 21:16:23 +00:00
|
|
|
check:
|
|
|
|
toSeq(keys(addressBook.book))[0] == peerId1
|
2022-05-25 10:12:57 +00:00
|
|
|
toSeq(values(addressBook.book))[0] == @[multiaddr1]
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2021-02-08 21:16:23 +00:00
|
|
|
# Test AddressBook::get
|
|
|
|
check:
|
2022-05-25 10:12:57 +00:00
|
|
|
addressBook[peerId1] == @[multiaddr1]
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2022-05-25 10:12:57 +00:00
|
|
|
# Test AddressBook::del
|
2021-02-08 21:16:23 +00:00
|
|
|
check:
|
2022-05-25 10:12:57 +00:00
|
|
|
# Try to del peerId that doesn't exist
|
|
|
|
addressBook.del(peerId2) == false
|
2021-02-08 21:16:23 +00:00
|
|
|
|
|
|
|
# Delete existing peerId
|
|
|
|
addressBook.book.len == 1 # sanity
|
2022-05-25 10:12:57 +00:00
|
|
|
addressBook.del(peerId1) == true
|
2021-02-08 21:16:23 +00:00
|
|
|
addressBook.book.len == 0
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2021-02-08 21:16:23 +00:00
|
|
|
# Test AddressBook::set
|
|
|
|
# Set peerId2 with multiple multiaddrs
|
2022-05-25 10:12:57 +00:00
|
|
|
addressBook[peerId2] = @[multiaddr1, multiaddr2]
|
2021-02-08 21:16:23 +00:00
|
|
|
check:
|
|
|
|
toSeq(keys(addressBook.book))[0] == peerId2
|
2022-05-25 10:12:57 +00:00
|
|
|
toSeq(values(addressBook.book))[0] == @[multiaddr1, multiaddr2]
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2022-05-25 10:12:57 +00:00
|
|
|
test "Pruner - no capacity":
|
|
|
|
let peerStore = PeerStore.new(capacity = 0)
|
|
|
|
peerStore[AgentBook][peerId1] = "gds"
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2022-05-25 10:12:57 +00:00
|
|
|
peerStore.cleanup(peerId1)
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2022-05-25 10:12:57 +00:00
|
|
|
check peerId1 notin peerStore[AgentBook]
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2022-05-25 10:12:57 +00:00
|
|
|
test "Pruner - FIFO":
|
|
|
|
let peerStore = PeerStore.new(capacity = 1)
|
|
|
|
peerStore[AgentBook][peerId1] = "gds"
|
|
|
|
peerStore[AgentBook][peerId2] = "gds"
|
|
|
|
peerStore.cleanup(peerId2)
|
|
|
|
peerStore.cleanup(peerId1)
|
2021-02-08 21:16:23 +00:00
|
|
|
check:
|
2022-05-25 10:12:57 +00:00
|
|
|
peerId1 in peerStore[AgentBook]
|
|
|
|
peerId2 notin peerStore[AgentBook]
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2022-05-25 10:12:57 +00:00
|
|
|
test "Pruner - regular capacity":
|
|
|
|
var peerStore = PeerStore.new(capacity = 20)
|
2021-02-08 21:16:23 +00:00
|
|
|
|
2022-05-25 10:12:57 +00:00
|
|
|
for i in 0..<30:
|
|
|
|
let randomPeerId = PeerId.init(KeyPair.random(ECDSA, rng[]).get().pubkey).get()
|
|
|
|
peerStore[AgentBook][randomPeerId] = "gds"
|
|
|
|
peerStore.cleanup(randomPeerId)
|
2021-02-08 21:16:23 +00:00
|
|
|
|
2022-05-25 10:12:57 +00:00
|
|
|
check peerStore[AgentBook].len == 20
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2022-05-25 10:12:57 +00:00
|
|
|
test "Pruner - infinite capacity":
|
|
|
|
var peerStore = PeerStore.new(capacity = -1)
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2022-05-25 10:12:57 +00:00
|
|
|
for i in 0..<30:
|
|
|
|
let randomPeerId = PeerId.init(KeyPair.random(ECDSA, rng[]).get().pubkey).get()
|
|
|
|
peerStore[AgentBook][randomPeerId] = "gds"
|
|
|
|
peerStore.cleanup(randomPeerId)
|
2021-02-08 21:16:23 +00:00
|
|
|
|
2022-05-25 10:12:57 +00:00
|
|
|
check peerStore[AgentBook].len == 30
|