mirror of https://github.com/status-im/nim-eth.git
allow EhereumNode network state override
This commit is contained in:
parent
ae616935c3
commit
8115885b6e
46
eth/p2p.nim
46
eth/p2p.nim
|
@ -8,7 +8,7 @@
|
||||||
{.push raises: [Defect].}
|
{.push raises: [Defect].}
|
||||||
|
|
||||||
import
|
import
|
||||||
std/[tables, algorithm, random],
|
std/[tables, algorithm, random, typetraits, strutils],
|
||||||
chronos, chronos/timer, chronicles,
|
chronos, chronos/timer, chronicles,
|
||||||
./keys, ./p2p/private/p2p_types,
|
./keys, ./p2p/private/p2p_types,
|
||||||
./p2p/[kademlia, discovery, enode, peer_pool, rlpx]
|
./p2p/[kademlia, discovery, enode, peer_pool, rlpx]
|
||||||
|
@ -16,19 +16,59 @@ import
|
||||||
export
|
export
|
||||||
p2p_types, rlpx, enode, kademlia
|
p2p_types, rlpx, enode, kademlia
|
||||||
|
|
||||||
proc addCapability*(node: var EthereumNode, p: ProtocolInfo) =
|
proc addCapability*(node: var EthereumNode,
|
||||||
|
p: ProtocolInfo,
|
||||||
|
networkState: RootRef = nil) =
|
||||||
doAssert node.connectionState == ConnectionState.None
|
doAssert node.connectionState == ConnectionState.None
|
||||||
|
|
||||||
let pos = lowerBound(node.protocols, p, rlpx.cmp)
|
let pos = lowerBound(node.protocols, p, rlpx.cmp)
|
||||||
node.protocols.insert(p, pos)
|
node.protocols.insert(p, pos)
|
||||||
node.capabilities.insert(p.asCapability, pos)
|
node.capabilities.insert(p.asCapability, pos)
|
||||||
|
|
||||||
if p.networkStateInitializer != nil:
|
if p.networkStateInitializer != nil and networkState.isNil:
|
||||||
node.protocolStates[p.index] = p.networkStateInitializer(node)
|
node.protocolStates[p.index] = p.networkStateInitializer(node)
|
||||||
|
|
||||||
|
if networkState.isNil.not:
|
||||||
|
node.protocolStates[p.index] = networkState
|
||||||
|
|
||||||
template addCapability*(node: var EthereumNode, Protocol: type) =
|
template addCapability*(node: var EthereumNode, Protocol: type) =
|
||||||
addCapability(node, Protocol.protocolInfo)
|
addCapability(node, Protocol.protocolInfo)
|
||||||
|
|
||||||
|
template addCapability*(node: var EthereumNode,
|
||||||
|
Protocol: type,
|
||||||
|
networkState: untyped) =
|
||||||
|
mixin NetworkState
|
||||||
|
type
|
||||||
|
ParamType = type(networkState)
|
||||||
|
|
||||||
|
when ParamType isnot Protocol.NetworkState:
|
||||||
|
const errMsg = "`$1` is not compatible with `$2`" % [
|
||||||
|
name(ParamType), name(Protocol.NetworkState)]
|
||||||
|
{. error: errMsg .}
|
||||||
|
|
||||||
|
addCapability(node, Protocol.protocolInfo,
|
||||||
|
cast[RootRef](networkState))
|
||||||
|
|
||||||
|
proc replaceNetworkState*(node: var EthereumNode,
|
||||||
|
p: ProtocolInfo,
|
||||||
|
networkState: RootRef) =
|
||||||
|
node.protocolStates[p.index] = networkState
|
||||||
|
|
||||||
|
template replaceNetworkState*(node: var EthereumNode,
|
||||||
|
Protocol: type,
|
||||||
|
networkState: untyped) =
|
||||||
|
mixin NetworkState
|
||||||
|
type
|
||||||
|
ParamType = type(networkState)
|
||||||
|
|
||||||
|
when ParamType isnot Protocol.NetworkState:
|
||||||
|
const errMsg = "`$1` is not compatible with `$2`" % [
|
||||||
|
name(ParamType), name(Protocol.NetworkState)]
|
||||||
|
{. error: errMsg .}
|
||||||
|
|
||||||
|
replaceNetworkState(node, Protocol.protocolInfo,
|
||||||
|
cast[RootRef](networkState))
|
||||||
|
|
||||||
proc newEthereumNode*(
|
proc newEthereumNode*(
|
||||||
keys: KeyPair,
|
keys: KeyPair,
|
||||||
address: Address,
|
address: Address,
|
||||||
|
|
|
@ -86,3 +86,9 @@ suite "Testing protocol handlers":
|
||||||
peer.isNil == true
|
peer.isNil == true
|
||||||
# To check if the disconnection handler did not run
|
# To check if the disconnection handler did not run
|
||||||
node1.protocolState(hah).count == 0
|
node1.protocolState(hah).count == 0
|
||||||
|
|
||||||
|
test "Override network state":
|
||||||
|
let rng = newRng()
|
||||||
|
var node = setupTestNode(rng, hah)
|
||||||
|
node.addCapability(hah, network())
|
||||||
|
node.replaceNetworkState(hah, network())
|
||||||
|
|
Loading…
Reference in New Issue