2019-04-05 08:13:22 +00:00
|
|
|
import
|
2019-10-22 12:08:25 +00:00
|
|
|
unittest, chronos, nimcrypto, strutils,
|
|
|
|
eth/[keys, p2p], eth/p2p/[discovery, enode]
|
2019-04-05 08:13:22 +00:00
|
|
|
|
|
|
|
var nextPort = 30303
|
|
|
|
|
2019-06-17 20:12:50 +00:00
|
|
|
proc localAddress*(port: int): Address =
|
2019-04-05 08:13:22 +00:00
|
|
|
let port = Port(port)
|
2020-02-17 23:07:23 +00:00
|
|
|
result = Address(udpPort: port, tcpPort: port,
|
|
|
|
ip: parseIpAddress("127.0.0.1"))
|
2019-04-05 08:13:22 +00:00
|
|
|
|
2019-06-17 20:12:50 +00:00
|
|
|
proc startDiscoveryNode*(privKey: PrivateKey, address: Address,
|
2019-04-05 08:13:22 +00:00
|
|
|
bootnodes: seq[ENode]): Future[DiscoveryProtocol] {.async.} =
|
|
|
|
result = newDiscoveryProtocol(privKey, address, bootnodes)
|
|
|
|
result.open()
|
|
|
|
await result.bootstrap()
|
|
|
|
|
|
|
|
proc setupBootNode*(): Future[ENode] {.async.} =
|
|
|
|
let
|
2020-04-04 16:44:01 +00:00
|
|
|
bootNodeKey = KeyPair.random()[]
|
2019-04-05 08:13:22 +00:00
|
|
|
bootNodeAddr = localAddress(30301)
|
2020-04-04 16:44:01 +00:00
|
|
|
bootNode = await startDiscoveryNode(bootNodeKey.seckey, bootNodeAddr, @[])
|
2020-04-06 16:24:15 +00:00
|
|
|
result = ENode(pubkey: bootNodeKey.pubkey, address: bootNodeAddr)
|
2019-04-05 08:13:22 +00:00
|
|
|
|
|
|
|
proc setupTestNode*(capabilities: varargs[ProtocolInfo, `protocolInfo`]): EthereumNode =
|
2020-04-04 16:44:01 +00:00
|
|
|
let keys1 = KeyPair.random()[]
|
2019-04-05 08:13:22 +00:00
|
|
|
result = newEthereumNode(keys1, localAddress(nextPort), 1, nil,
|
|
|
|
addAllCapabilities = false)
|
|
|
|
nextPort.inc
|
|
|
|
for capability in capabilities:
|
|
|
|
result.addCapability capability
|
|
|
|
|
|
|
|
template asyncTest*(name, body: untyped) =
|
|
|
|
test name:
|
|
|
|
proc scenario {.async.} = body
|
|
|
|
waitFor scenario()
|
2019-06-18 15:28:55 +00:00
|
|
|
|
2020-03-23 21:09:16 +00:00
|
|
|
template procSuite*(name, body: untyped) =
|
|
|
|
proc suitePayload =
|
|
|
|
suite name:
|
|
|
|
body
|
|
|
|
|
|
|
|
suitePayload()
|
|
|
|
|
2019-10-01 10:00:20 +00:00
|
|
|
proc packData*(payload: openArray[byte], pk: PrivateKey): seq[byte] =
|
2019-06-18 15:28:55 +00:00
|
|
|
let
|
2019-10-01 10:00:20 +00:00
|
|
|
payloadSeq = @payload
|
2020-04-04 16:44:01 +00:00
|
|
|
signature = @(pk.sign(payload).tryGet().toRaw())
|
2019-10-01 10:00:20 +00:00
|
|
|
msgHash = keccak256.digest(signature & payloadSeq)
|
|
|
|
result = @(msgHash.data) & signature & payloadSeq
|
2019-10-22 12:08:25 +00:00
|
|
|
|
|
|
|
template sourceDir*: string = currentSourcePath.rsplit(DirSep, 1)[0]
|
|
|
|
|
|
|
|
proc recvMsgMock*(msg: openArray[byte]): tuple[msgId: int, msgData: Rlp] =
|
2020-04-20 18:14:39 +00:00
|
|
|
var rlp = rlpFromBytes(msg)
|
2019-10-22 12:08:25 +00:00
|
|
|
|
2019-10-22 15:00:19 +00:00
|
|
|
let msgId = rlp.read(int32)
|
2020-02-17 23:07:23 +00:00
|
|
|
return (msgId.int, rlp)
|