2019-06-18 15:28:55 +00:00
|
|
|
import
|
2021-09-07 14:00:01 +00:00
|
|
|
std/[times, os, strformat, strutils],
|
|
|
|
chronos, stew/byteutils, stint, chronicles, nimcrypto,
|
|
|
|
../../../eth/p2p/[discovery, kademlia], ../../../eth/[keys, rlp],
|
|
|
|
../../p2p/p2p_test_helper,
|
|
|
|
../fuzzing_helpers
|
2019-06-18 15:28:55 +00:00
|
|
|
|
|
|
|
template sourceDir: string = currentSourcePath.rsplit(DirSep, 1)[0]
|
|
|
|
const inputsDir = &"{sourceDir}{DirSep}generated-input{DirSep}"
|
|
|
|
|
|
|
|
const EXPIRATION = 3600 * 24 * 365 * 10
|
|
|
|
proc expiration(): uint32 = uint32(epochTime() + EXPIRATION)
|
|
|
|
|
|
|
|
proc generate() =
|
|
|
|
## Generate some valid inputs where one can start fuzzing with
|
|
|
|
let
|
|
|
|
fromAddr = localAddress(30303)
|
|
|
|
toAddr = localAddress(30304)
|
2020-04-04 16:44:01 +00:00
|
|
|
peerKey = PrivateKey.fromHex("a2b50376a79b1a8c8a3296485572bdfbf54708bb46d3c25d73d2723aaaf6a617")[]
|
2019-06-18 15:28:55 +00:00
|
|
|
|
|
|
|
# valid data for a Ping packet
|
|
|
|
block:
|
2024-09-30 16:32:36 +00:00
|
|
|
let payload = rlp.encode((uint64 4, fromAddr, toAddr, expiration()))
|
2020-04-20 18:14:39 +00:00
|
|
|
let encodedData = @[1.byte] & payload
|
2019-06-18 15:28:55 +00:00
|
|
|
debug "Ping", data=byteutils.toHex(encodedData)
|
|
|
|
|
|
|
|
encodedData.toFile(inputsDir & "ping")
|
|
|
|
|
|
|
|
# valid data for a Pong packet
|
|
|
|
block:
|
2021-09-07 14:00:01 +00:00
|
|
|
let token = keccak256.digest(@[byte 0])
|
2020-04-20 18:14:39 +00:00
|
|
|
let payload = rlp.encode((toAddr, token , expiration()))
|
|
|
|
let encodedData = @[2.byte] & payload
|
2019-06-18 15:28:55 +00:00
|
|
|
debug "Pong", data=byteutils.toHex(encodedData)
|
|
|
|
|
|
|
|
encodedData.toFile(inputsDir & "pong")
|
|
|
|
|
|
|
|
# valid data for a FindNode packet
|
|
|
|
block:
|
|
|
|
var data: array[64, byte]
|
2021-09-07 14:00:01 +00:00
|
|
|
data[32 .. ^1] = peerKey.toPublicKey().toNodeId().toByteArrayBE()
|
2020-04-20 18:14:39 +00:00
|
|
|
let payload = rlp.encode((data, expiration()))
|
2021-09-07 14:00:01 +00:00
|
|
|
let encodedData = @[3.byte] & @payload
|
2019-06-18 15:28:55 +00:00
|
|
|
debug "FindNode", data=byteutils.toHex(encodedData)
|
|
|
|
|
|
|
|
encodedData.toFile(inputsDir & "findnode")
|
|
|
|
|
|
|
|
# valid data for a Neighbours packet
|
|
|
|
block:
|
|
|
|
let
|
|
|
|
n1Addr = localAddress(30305)
|
|
|
|
n2Addr = localAddress(30306)
|
2020-04-04 16:44:01 +00:00
|
|
|
n1Key = PrivateKey.fromHex(
|
|
|
|
"a2b50376a79b1a8c8a3296485572bdfbf54708bb46d3c25d73d2723aaaf6a618")[]
|
|
|
|
n2Key = PrivateKey.fromHex(
|
|
|
|
"a2b50376a79b1a8c8a3296485572bdfbf54708bb46d3c25d73d2723aaaf6a619")[]
|
2019-06-18 15:28:55 +00:00
|
|
|
|
|
|
|
type Neighbour = tuple[ip: IpAddress, udpPort, tcpPort: Port, pk: PublicKey]
|
|
|
|
var nodes = newSeqOfCap[Neighbour](2)
|
|
|
|
|
2021-09-07 14:00:01 +00:00
|
|
|
nodes.add((n1Addr.ip, n1Addr.udpPort, n1Addr.tcpPort, n1Key.toPublicKey()))
|
|
|
|
nodes.add((n2Addr.ip, n2Addr.udpPort, n2Addr.tcpPort, n2Key.toPublicKey()))
|
2019-06-18 15:28:55 +00:00
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
let payload = rlp.encode((nodes, expiration()))
|
2021-09-07 14:00:01 +00:00
|
|
|
let encodedData = @[4.byte] & @payload
|
2019-06-18 15:28:55 +00:00
|
|
|
debug "Neighbours", data=byteutils.toHex(encodedData)
|
|
|
|
|
|
|
|
encodedData.toFile(inputsDir & "neighbours")
|
|
|
|
|
|
|
|
discard existsOrCreateDir(inputsDir)
|
|
|
|
generate()
|