2019-04-02 15:48:20 +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)
|
|
|
|
|
2021-05-06 15:20:54 +00:00
|
|
|
{.used.}
|
|
|
|
|
2019-04-02 15:48:20 +00:00
|
|
|
import
|
2021-04-06 11:33:24 +00:00
|
|
|
std/tables,
|
|
|
|
chronos, testutils/unittests,
|
|
|
|
../../eth/p2p,
|
2022-11-10 08:01:58 +00:00
|
|
|
../stubloglevel,
|
2019-04-05 08:13:22 +00:00
|
|
|
./p2p_test_helper
|
2019-04-02 15:48:20 +00:00
|
|
|
|
|
|
|
type
|
2022-11-09 17:57:04 +00:00
|
|
|
network = ref object
|
2019-04-02 15:48:20 +00:00
|
|
|
count*: int
|
|
|
|
|
|
|
|
p2pProtocol abc(version = 1,
|
2019-08-05 13:31:51 +00:00
|
|
|
rlpxName = "abc",
|
2019-04-02 15:48:20 +00:00
|
|
|
networkState = network):
|
|
|
|
|
|
|
|
onPeerConnected do (peer: Peer):
|
|
|
|
peer.networkState.count += 1
|
|
|
|
|
|
|
|
onPeerDisconnected do (peer: Peer, reason: DisconnectionReason) {.gcsafe.}:
|
|
|
|
peer.networkState.count -= 1
|
|
|
|
if true:
|
2019-04-05 08:13:22 +00:00
|
|
|
raise newException(CatchableError, "Fake abc exception")
|
2019-04-02 15:48:20 +00:00
|
|
|
|
|
|
|
p2pProtocol xyz(version = 1,
|
2019-08-05 13:31:51 +00:00
|
|
|
rlpxName = "xyz",
|
2022-11-09 17:57:04 +00:00
|
|
|
networkState = network):
|
2019-04-02 15:48:20 +00:00
|
|
|
|
|
|
|
onPeerConnected do (peer: Peer):
|
|
|
|
peer.networkState.count += 1
|
|
|
|
|
|
|
|
onPeerDisconnected do (peer: Peer, reason: DisconnectionReason) {.gcsafe.}:
|
|
|
|
peer.networkState.count -= 1
|
|
|
|
if true:
|
2019-04-05 08:13:22 +00:00
|
|
|
raise newException(CatchableError, "Fake xyz exception")
|
2019-04-02 15:48:20 +00:00
|
|
|
|
2019-04-11 13:08:32 +00:00
|
|
|
p2pProtocol hah(version = 1,
|
2019-08-05 13:31:51 +00:00
|
|
|
rlpxName = "hah",
|
2019-04-11 13:08:32 +00:00
|
|
|
networkState = network):
|
|
|
|
|
|
|
|
onPeerConnected do (peer: Peer):
|
|
|
|
if true:
|
|
|
|
raise newException(UselessPeerError, "Fake hah exception")
|
|
|
|
peer.networkState.count += 1
|
|
|
|
|
|
|
|
onPeerDisconnected do (peer: Peer, reason: DisconnectionReason) {.gcsafe.}:
|
|
|
|
peer.networkState.count -= 1
|
|
|
|
|
2020-07-07 08:56:26 +00:00
|
|
|
|
2019-04-05 08:13:22 +00:00
|
|
|
suite "Testing protocol handlers":
|
2019-04-11 13:08:32 +00:00
|
|
|
asyncTest "Failing disconnection handler":
|
2020-07-07 08:56:26 +00:00
|
|
|
let rng = newRng()
|
|
|
|
|
|
|
|
var node1 = setupTestNode(rng, abc, xyz)
|
|
|
|
var node2 = setupTestNode(rng, abc, xyz)
|
2019-06-21 10:06:37 +00:00
|
|
|
|
|
|
|
node2.startListening()
|
2023-03-16 15:45:12 +00:00
|
|
|
let res = await node1.rlpxConnect(newNode(node2.toENode()))
|
2019-04-02 15:48:20 +00:00
|
|
|
check:
|
2023-03-16 15:45:12 +00:00
|
|
|
res.isOk()
|
|
|
|
|
|
|
|
let peer = res.get()
|
2019-04-02 15:48:20 +00:00
|
|
|
|
2019-06-21 10:06:37 +00:00
|
|
|
await peer.disconnect(SubprotocolReason, true)
|
2019-04-02 15:48:20 +00:00
|
|
|
check:
|
|
|
|
# we want to check that even though the exceptions in the disconnect
|
|
|
|
# handlers, each handler still ran
|
|
|
|
node1.protocolState(abc).count == 0
|
|
|
|
node1.protocolState(xyz).count == 0
|
2019-04-11 13:08:32 +00:00
|
|
|
|
|
|
|
asyncTest "Failing connection handler":
|
2020-07-07 08:56:26 +00:00
|
|
|
let rng = newRng()
|
|
|
|
|
|
|
|
var node1 = setupTestNode(rng, hah)
|
|
|
|
var node2 = setupTestNode(rng, hah)
|
2019-04-11 13:08:32 +00:00
|
|
|
node2.startListening()
|
2023-03-16 15:45:12 +00:00
|
|
|
let res = await node1.rlpxConnect(newNode(node2.toENode()))
|
2019-04-11 13:08:32 +00:00
|
|
|
check:
|
2023-03-16 15:45:12 +00:00
|
|
|
res.isErr()
|
2019-04-11 13:08:32 +00:00
|
|
|
# To check if the disconnection handler did not run
|
|
|
|
node1.protocolState(hah).count == 0
|
2022-10-14 10:16:25 +00:00
|
|
|
|
|
|
|
test "Override network state":
|
|
|
|
let rng = newRng()
|
|
|
|
var node = setupTestNode(rng, hah)
|
2022-10-15 01:49:11 +00:00
|
|
|
node.addCapability(hah, network(count: 3))
|
|
|
|
check node.protocolState(hah).count == 3
|
|
|
|
node.replaceNetworkState(hah, network(count: 7))
|
|
|
|
check node.protocolState(hah).count == 7
|