nim-libp2p/tests/testpeerinfo.nim

66 lines
2.1 KiB
Nim
Raw Normal View History

{.used.}
import options
import chronos, stew/byteutils
import ../libp2p/crypto/crypto,
../libp2p/multicodec,
../libp2p/peerinfo,
../libp2p/peerid,
../libp2p/routing_record
import ./helpers
suite "PeerInfo":
test "Should init with private key":
let seckey = PrivateKey.random(ECDSA, rng[]).get()
var peerInfo = PeerInfo.new(seckey)
2021-12-16 10:05:20 +00:00
var peerId = PeerId.init(seckey).get()
check peerId == peerInfo.peerId
check seckey.getPublicKey().get() == peerInfo.publicKey
2022-10-20 10:22:28 +00:00
test "Signed peer record":
const
ExpectedDomain = $multiCodec("libp2p-peer-record")
ExpectedPayloadType = @[(byte) 0x03, (byte) 0x01]
2022-10-20 10:22:28 +00:00
let
seckey = PrivateKey.random(rng[]).tryGet()
peerId = PeerId.init(seckey).get()
multiAddresses = @[MultiAddress.init("/ip4/0.0.0.0/tcp/24").tryGet(), MultiAddress.init("/ip4/0.0.0.0/tcp/25").tryGet()]
peerInfo = PeerInfo.new(seckey, multiAddresses)
2022-10-20 10:22:28 +00:00
waitFor(peerInfo.update())
let
env = peerInfo.signedPeerRecord.envelope
rec = PeerRecord.decode(env.payload()).tryGet()
2022-10-20 10:22:28 +00:00
# Check envelope fields
check:
env.publicKey == peerInfo.publicKey
env.domain == ExpectedDomain
env.payloadType == ExpectedPayloadType
# Check payload (routing record)
check:
rec.peerId == peerId
rec.seqNo > 0
rec.addresses.len == 2
rec.addresses[0].address == multiAddresses[0]
rec.addresses[1].address == multiAddresses[1]
2022-10-20 10:22:28 +00:00
test "Public address mapping":
let
seckey = PrivateKey.random(ECDSA, rng[]).get()
multiAddresses = @[MultiAddress.init("/ip4/0.0.0.0/tcp/24").tryGet(), MultiAddress.init("/ip4/0.0.0.0/tcp/25").tryGet()]
multiAddresses2 = @[MultiAddress.init("/ip4/8.8.8.8/tcp/33").tryGet()]
proc addressMapper(input: seq[MultiAddress]): Future[seq[MultiAddress]] {.async.} =
check input == multiAddresses
await sleepAsync(0.seconds)
return multiAddresses2
var peerInfo = PeerInfo.new(seckey, multiAddresses, addressMappers = @[addressMapper])
waitFor peerInfo.update()
check peerInfo.addrs == multiAddresses2