mirror of https://github.com/status-im/nim-eth.git
Added packet encoding tests
This commit is contained in:
parent
129710d3e4
commit
3527d47cb5
|
@ -246,7 +246,8 @@ proc decodeEncrypted*(c: var Codec, fromId: NodeID, fromAddr: Address, input: se
|
|||
result = decodePacketBody(body[0], body.toOpenArray(1, body.high), packet)
|
||||
|
||||
proc newRequestId*(): RequestId =
|
||||
randomBytes(result)
|
||||
if randomBytes(addr result, sizeof(result)) != sizeof(result):
|
||||
raise newException(Exception, "Could not randomize bytes") # TODO:
|
||||
|
||||
proc numFields(T: typedesc): int =
|
||||
for k, v in fieldPairs(default(T)): inc result
|
||||
|
@ -265,9 +266,3 @@ proc encodePacket*[T: SomePacket](p: T, reqId: RequestId): seq[byte] =
|
|||
|
||||
proc encodePacket*[T: SomePacket](p: T): seq[byte] =
|
||||
encodePacket(p, newRequestId())
|
||||
|
||||
proc makePingPacket*(enrSeq: uint64): seq[byte] =
|
||||
encodePacket(PingPacket(enrSeq: enrSeq))
|
||||
|
||||
proc makeFindnodePacket*(distance: uint32): seq[byte] =
|
||||
encodePacket(FindNodePacket(distance: distance))
|
||||
|
|
|
@ -28,7 +28,7 @@ type
|
|||
regconfirmation = 0x07
|
||||
topicquery = 0x08
|
||||
|
||||
RequestId* = array[8, byte]
|
||||
RequestId* = uint64
|
||||
|
||||
PingPacket* = object
|
||||
enrSeq*: uint64
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
import unittest
|
||||
import eth/p2p/discoveryv5/[types, encoding, enr]
|
||||
import eth/rlp, stew/byteutils
|
||||
|
||||
# According to test vectors:
|
||||
# https://github.com/ethereum/devp2p/blob/master/discv5/discv5-wire-test-vectors.md
|
||||
|
||||
suite "Discovery v5 Protocol Message Encodings":
|
||||
test "Ping Request":
|
||||
var p: PingPacket
|
||||
p.enrSeq = 1
|
||||
var reqId: RequestId = 1
|
||||
check encodePacket(p, reqId).toHex == "01c20101"
|
||||
|
||||
test "Pong Response":
|
||||
var p: PongPacket
|
||||
p.enrSeq = 1
|
||||
p.port = 5000
|
||||
p.ip = @[127.byte, 0, 0, 1]
|
||||
var reqId: RequestId = 1
|
||||
check encodePacket(p, reqId).toHex == "02ca0101847f000001821388"
|
||||
|
||||
test "FindNode Request":
|
||||
var p: FindNodePacket
|
||||
p.distance = 0x0100
|
||||
var reqId: RequestId = 1
|
||||
check encodePacket(p, reqId).toHex == "03c401820100"
|
||||
|
||||
test "Nodes Response (empty)":
|
||||
var p: NodesPacket
|
||||
p.total = 0x1
|
||||
var reqId: RequestId = 1
|
||||
check encodePacket(p, reqId).toHex == "04c30101c0"
|
||||
|
||||
test "Nodes Response (multiple)":
|
||||
var p: NodesPacket
|
||||
p.total = 0x1
|
||||
var e1, e2: Record
|
||||
check e1.fromURI("enr:-HW4QBzimRxkmT18hMKaAL3IcZF1UcfTMPyi3Q1pxwZZbcZVRI8DC5infUAB_UauARLOJtYTxaagKoGmIjzQxO2qUygBgmlkgnY0iXNlY3AyNTZrMaEDymNMrg1JrLQB2KTGtv6MVbcNEVv0AHacwUAPMljNMTg")
|
||||
check e2.fromURI("enr:-HW4QNfxw543Ypf4HXKXdYxkyzfcxcO-6p9X986WldfVpnVTQX1xlTnWrktEWUbeTZnmgOuAY_KUhbVV1Ft98WoYUBMBgmlkgnY0iXNlY3AyNTZrMaEDDiy3QkHAxPyOgWbxp5oF1bDdlYE6dLCUUp8xfVw50jU")
|
||||
|
||||
p.enrs = @[e1, e2]
|
||||
var reqId: RequestId = 1
|
||||
check encodePacket(p, reqId).toHex == "04f8f20101f8eef875b8401ce2991c64993d7c84c29a00bdc871917551c7d330fca2dd0d69c706596dc655448f030b98a77d4001fd46ae0112ce26d613c5a6a02a81a6223cd0c4edaa53280182696482763489736563703235366b31a103ca634cae0d49acb401d8a4c6b6fe8c55b70d115bf400769cc1400f3258cd3138f875b840d7f1c39e376297f81d7297758c64cb37dcc5c3beea9f57f7ce9695d7d5a67553417d719539d6ae4b445946de4d99e680eb8063f29485b555d45b7df16a1850130182696482763489736563703235366b31a1030e2cb74241c0c4fc8e8166f1a79a05d5b0dd95813a74b094529f317d5c39d235"
|
Loading…
Reference in New Issue