Use ssz vectors for the hashes and add enrs in the tests (#763)

This commit is contained in:
Kim De Mey 2021-07-20 14:04:57 +02:00 committed by GitHub
parent e37bafd47e
commit 66532e8ad5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 72 additions and 14 deletions

View File

@ -25,11 +25,11 @@ type
NetworkId* = uint16
NodeHash* = List[byte, 32] # MDigest[32 * 8] - keccak256
NodeHash* = MDigest[32 * 8] # keccak256
CodeHash* = List[byte, 32] # MDigest[32 * 8] - keccak256
CodeHash* = MDigest[32 * 8] # keccak256
Address* = List[byte, 20]
Address* = array[20, byte]
ContentKey* = object
networkId*: NetworkId
@ -79,7 +79,7 @@ type
proc getContent*(storage: ContentStorage, key: ContentKey): Option[seq[byte]] =
if storage.trie.db == nil: # TODO: for now...
return none(seq[byte])
let val = storage.trie.db.get(key.nodeHash.asSeq())
let val = storage.trie.db.get(key.nodeHash.data)
if val.len > 0:
some(val)
else:

View File

@ -48,6 +48,9 @@ type
FindContentMessage* = object
contentKey*: ContentKey
# TODO: According to the specification, this is actually a ByteList from the
# serialized ContentKey container, which will result in a different
# serialization
FoundContentMessage* = object
enrs*: List[ByteList, 32]

View File

@ -200,10 +200,11 @@ proc run(config: DiscoveryConf) =
key
# For now just random content node hash
# For now just zeroes content node hash
var nodeHash: NodeHash
let contentKey = ContentKey(networkId: 0'u16,
contentType: messages.ContentType.Account,
nodeHash: List[byte, 32](@(UInt256.random(rng[]).toBytes())))
nodeHash: nodeHash)
let foundContent = waitFor portal.findContent(config.findContentTarget,
contentKey)

View File

@ -54,11 +54,14 @@ procSuite "Content Network":
keys.add(k)
for key in keys:
var nodeHash: NodeHash
copyMem(nodeHash.data.addr, unsafeAddr key[0], sizeof(nodeHash.data))
let
contentKey = ContentKey(
networkId: 0'u16,
contentType: content.ContentType.Account,
nodeHash: List[byte, 32](key))
nodeHash: nodeHash)
let foundContent = await proto2.findContent(proto1.baseProtocol.localNode,
contentKey)

View File

@ -102,9 +102,11 @@ procSuite "Portal Tests":
check (await node1.ping(node2.localNode)).isOk()
check (await node2.ping(node1.localNode)).isOk()
var nodeHash: NodeHash
let contentKey = ContentKey(networkId: 0'u16,
contentType: ContentType.Account,
nodeHash: List[byte, 32](@(UInt256.random(rng[]).toBytes())))
nodeHash: nodeHash)
# content does not exist so this should provide us with the closest nodes
# to the content, which is the only node in the routing table.

View File

@ -9,7 +9,7 @@
import
std/unittest,
stint, stew/[byteutils, results],
stint, stew/[byteutils, results], eth/p2p/discoveryv5/enr,
../network/messages
suite "Portal Protocol Message Encodings":
@ -65,7 +65,7 @@ suite "Portal Protocol Message Encodings":
message.kind == findnode
message.findnode.distances == distances
test "Nodes Response (empty)":
test "Nodes Response - empty":
let
total = 0x1'u8
n = NodesMessage(total: total)
@ -82,8 +82,32 @@ suite "Portal Protocol Message Encodings":
message.nodes.total == total
message.nodes.enrs.len() == 0
test "Nodes Response - enr":
var e1, e2: Record
check:
e1.fromURI("enr:-HW4QBzimRxkmT18hMKaAL3IcZF1UcfTMPyi3Q1pxwZZbcZVRI8DC5infUAB_UauARLOJtYTxaagKoGmIjzQxO2qUygBgmlkgnY0iXNlY3AyNTZrMaEDymNMrg1JrLQB2KTGtv6MVbcNEVv0AHacwUAPMljNMTg")
e2.fromURI("enr:-HW4QNfxw543Ypf4HXKXdYxkyzfcxcO-6p9X986WldfVpnVTQX1xlTnWrktEWUbeTZnmgOuAY_KUhbVV1Ft98WoYUBMBgmlkgnY0iXNlY3AyNTZrMaEDDiy3QkHAxPyOgWbxp5oF1bDdlYE6dLCUUp8xfVw50jU")
let
total = 0x1'u8
n = NodesMessage(total: total, enrs: List[ByteList, 32](@[ByteList(e1.raw), ByteList(e2.raw)]))
let encoded = encodeMessage(n)
check encoded.toHex == "040105000000080000007f000000f875b8401ce2991c64993d7c84c29a00bdc871917551c7d330fca2dd0d69c706596dc655448f030b98a77d4001fd46ae0112ce26d613c5a6a02a81a6223cd0c4edaa53280182696482763489736563703235366b31a103ca634cae0d49acb401d8a4c6b6fe8c55b70d115bf400769cc1400f3258cd3138f875b840d7f1c39e376297f81d7297758c64cb37dcc5c3beea9f57f7ce9695d7d5a67553417d719539d6ae4b445946de4d99e680eb8063f29485b555d45b7df16a1850130182696482763489736563703235366b31a1030e2cb74241c0c4fc8e8166f1a79a05d5b0dd95813a74b094529f317d5c39d235"
let decoded = decodeMessage(encoded)
check decoded.isOk()
let message = decoded.get()
check:
message.kind == nodes
message.nodes.total == total
message.nodes.enrs.len() == 2
message.nodes.enrs[0] == ByteList(e1.raw)
message.nodes.enrs[1] == ByteList(e2.raw)
test "FindContent Request":
var nodeHash: List[byte, 32]
var nodeHash: NodeHash # zeroes hash
let
contentKey = ContentKey(
networkId: 0'u16,
@ -92,7 +116,7 @@ suite "Portal Protocol Message Encodings":
fn = FindContentMessage(contentKey: contentKey)
let encoded = encodeMessage(fn)
check encoded.toHex == "050400000000000107000000"
check encoded.toHex == "050000010000000000000000000000000000000000000000000000000000000000000000"
let decoded = decodeMessage(encoded)
check decoded.isOk()
@ -102,7 +126,7 @@ suite "Portal Protocol Message Encodings":
message.kind == findcontent
message.findcontent.contentKey == contentKey
test "FoundContent Response (empty enrs)":
test "FoundContent Response - payload":
let
enrs = List[ByteList, 32](@[])
payload = ByteList(@[byte 0x01, 0x02, 0x03])
@ -120,6 +144,31 @@ suite "Portal Protocol Message Encodings":
message.foundcontent.enrs.len() == 0
message.foundcontent.payload == payload
test "FoundContent Response - enrs":
var e1, e2: Record
check:
e1.fromURI("enr:-HW4QBzimRxkmT18hMKaAL3IcZF1UcfTMPyi3Q1pxwZZbcZVRI8DC5infUAB_UauARLOJtYTxaagKoGmIjzQxO2qUygBgmlkgnY0iXNlY3AyNTZrMaEDymNMrg1JrLQB2KTGtv6MVbcNEVv0AHacwUAPMljNMTg")
e2.fromURI("enr:-HW4QNfxw543Ypf4HXKXdYxkyzfcxcO-6p9X986WldfVpnVTQX1xlTnWrktEWUbeTZnmgOuAY_KUhbVV1Ft98WoYUBMBgmlkgnY0iXNlY3AyNTZrMaEDDiy3QkHAxPyOgWbxp5oF1bDdlYE6dLCUUp8xfVw50jU")
let
enrs = List[ByteList, 32](@[ByteList(e1.raw), ByteList(e2.raw)])
payload = ByteList(@[])
n = FoundContentMessage(enrs: enrs, payload: payload)
let encoded = encodeMessage(n)
check encoded.toHex == "0608000000fe000000080000007f000000f875b8401ce2991c64993d7c84c29a00bdc871917551c7d330fca2dd0d69c706596dc655448f030b98a77d4001fd46ae0112ce26d613c5a6a02a81a6223cd0c4edaa53280182696482763489736563703235366b31a103ca634cae0d49acb401d8a4c6b6fe8c55b70d115bf400769cc1400f3258cd3138f875b840d7f1c39e376297f81d7297758c64cb37dcc5c3beea9f57f7ce9695d7d5a67553417d719539d6ae4b445946de4d99e680eb8063f29485b555d45b7df16a1850130182696482763489736563703235366b31a1030e2cb74241c0c4fc8e8166f1a79a05d5b0dd95813a74b094529f317d5c39d235"
let decoded = decodeMessage(encoded)
check decoded.isOk()
let message = decoded.get()
check:
message.kind == foundcontent
message.foundcontent.enrs.len() == 2
message.foundcontent.enrs[0] == ByteList(e1.raw)
message.foundcontent.enrs[1] == ByteList(e2.raw)
message.foundcontent.payload == payload
test "Advertise Request":
let
contentKeys = List[ByteList, 32](List(@[ByteList(@[byte 0x01, 0x02, 0x03])]))

2
vendor/nim-eth vendored

@ -1 +1 @@
Subproject commit 79911ed5d8c951d5dcab9f2b20e1db8d2f59cdd1
Subproject commit 2557fd35c62d77c1e92478c1b3dbbf262a7addb7