tests: generate PeerRecords from NodeId

this is just for the testing
This commit is contained in:
Csaba Kiraly 2022-02-16 10:17:00 +01:00
parent 54fde4e09e
commit 77d93d71bd
1 changed files with 18 additions and 1 deletions

View File

@ -17,7 +17,9 @@ import
chronicles, chronicles,
../../eth/p2p/discoveryv5/protocol as discv5_protocol, ../../eth/p2p/discoveryv5/protocol as discv5_protocol,
./discv5_test_helper, ./discv5_test_helper,
libp2p/routing_record libp2p/routing_record,
libp2p/multihash,
libp2p/multicodec
proc initProvidersNode( proc initProvidersNode(
rng: ref BrHmacDrbgContext, rng: ref BrHmacDrbgContext,
@ -29,6 +31,21 @@ proc initProvidersNode(
let d = initDiscoveryNode(rng, privKey, address, bootstrapRecords) let d = initDiscoveryNode(rng, privKey, address, bootstrapRecords)
newProvidersProtocol(d) newProvidersProtocol(d)
proc toPeerRecord(p: ProvidersProtocol) : PeerRecord =
## hadle conversion between the two worlds
#NodeId is a keccak-256 hash created by keccak256.digest and stored as UInt256
let discNodeId = p.discovery.localNode.id
## get it back to MDigest form
var digest: MDigest[256]
digest.data = discNodeId.toBytesBE
## get into a MultiHash
var mh = MultiHash.init(multiCodec("keccak-256"), digest).orError(HashError).get()
result = PeerRecord.init(
peerId = PeerId.init(mh.data.buffer).get,
seqNo = 0,
addresses = @[])
proc bootstrapNodes(nodecount: int, bootnodes: openArray[Record], rng = keys.newRng()) : seq[ProvidersProtocol] = proc bootstrapNodes(nodecount: int, bootnodes: openArray[Record], rng = keys.newRng()) : seq[ProvidersProtocol] =
for i in 0..<nodecount: for i in 0..<nodecount: