nim-eth-p2p/tests/tshh_connect_mocked.nim

50 lines
1.7 KiB
Nim
Raw Normal View History

2018-11-16 14:10:03 +00:00
#
# Ethereum P2P
# (c) Copyright 2018
# Status Research & Development GmbH
#
# Licensed under either of
# Apache License, version 2.0, (LICENSE-APACHEv2)
# MIT license (LICENSE-MIT)
import
options, unittest, asyncdispatch2, rlp, eth_keys,
2018-11-28 14:11:03 +00:00
eth_p2p, eth_p2p/mock_peers, eth_p2p/rlpx_protocols/[whisper_protocol]
2018-11-16 14:10:03 +00:00
proc localAddress(port: int): Address =
let port = Port(port)
result = Address(udpPort: port, tcpPort: port, ip: parseIpAddress("127.0.0.1"))
template asyncTest(name, body: untyped) =
test name:
proc scenario {.async.} = body
waitFor scenario()
2018-11-27 23:52:22 +00:00
asyncTest "network with 3 peers using the Whisper protocol":
2018-11-16 14:10:03 +00:00
const useCompression = defined(useSnappy)
let localKeys = newKeyPair()
let localAddress = localAddress(30303)
var localNode = newEthereumNode(localKeys, localAddress, 1, nil,
addAllCapabilities = false,
useCompression = useCompression)
2018-11-27 23:52:22 +00:00
localNode.addCapability Whisper
2018-11-16 14:10:03 +00:00
localNode.startListening()
var mock1 = newMockPeer do (m: MockConf):
2018-11-27 23:52:22 +00:00
m.addHandshake Whisper.status(protocolVersion: whisperVersion, powConverted: 0,
bloom: @[], isLightNode: false)
m.expect Whisper.messages
2018-11-16 14:10:03 +00:00
var mock2 = newMockPeer do (m: MockConf):
2018-11-27 23:52:22 +00:00
m.addHandshake Whisper.status(protocolVersion: whisperVersion,
powConverted: cast[uint](0.1),
bloom: @[], isLightNode: false)
m.expect Whisper.messages
2018-11-16 14:10:03 +00:00
var mock1Peer = await localNode.rlpxConnect(mock1)
var mock2Peer = await localNode.rlpxConnect(mock2)
check:
2018-11-27 23:52:22 +00:00
mock1Peer.state(Whisper).powRequirement == 0
mock2Peer.state(Whisper).powRequirement == 0.1