2019-06-05 01:59:35 +00:00
|
|
|
var
|
|
|
|
gProtocols: seq[ProtocolInfo]
|
|
|
|
|
|
|
|
# The variables above are immutable RTTI information. We need to tell
|
|
|
|
# Nim to not consider them GcSafe violations:
|
|
|
|
template allProtocols*: auto = {.gcsafe.}: gProtocols
|
|
|
|
|
2019-05-19 18:05:02 +00:00
|
|
|
proc getState*(peer: Peer, proto: ProtocolInfo): RootRef =
|
2019-03-11 09:22:06 +00:00
|
|
|
peer.protocolStates[proto.index]
|
|
|
|
|
|
|
|
template state*(peer: Peer, Protocol: type): untyped =
|
|
|
|
## Returns the state object of a particular protocol for a
|
|
|
|
## particular connection.
|
|
|
|
mixin State
|
|
|
|
bind getState
|
|
|
|
cast[Protocol.State](getState(peer, Protocol.protocolInfo))
|
|
|
|
|
2019-05-19 18:05:02 +00:00
|
|
|
proc getNetworkState*(node: EthereumNode, proto: ProtocolInfo): RootRef =
|
2019-03-11 09:22:06 +00:00
|
|
|
node.protocolStates[proto.index]
|
|
|
|
|
|
|
|
template protocolState*(node: EthereumNode, Protocol: type): untyped =
|
|
|
|
mixin NetworkState
|
|
|
|
bind getNetworkState
|
|
|
|
cast[Protocol.NetworkState](getNetworkState(node, Protocol.protocolInfo))
|
|
|
|
|
|
|
|
template networkState*(connection: Peer, Protocol: type): untyped =
|
|
|
|
## Returns the network state object of a particular protocol for a
|
|
|
|
## particular connection.
|
|
|
|
protocolState(connection.network, Protocol)
|
|
|
|
|
|
|
|
proc initProtocolState*[T](state: T, x: Peer|EthereumNode) {.gcsafe.} = discard
|
|
|
|
|
2019-06-17 11:19:13 +00:00
|
|
|
proc initProtocolStates(peer: Peer, protocols: openarray[ProtocolInfo]) =
|
|
|
|
# Initialize all the active protocol states
|
|
|
|
newSeq(peer.protocolStates, allProtocols.len)
|
|
|
|
for protocol in protocols:
|
|
|
|
let peerStateInit = protocol.peerStateInitializer
|
|
|
|
if peerStateInit != nil:
|
|
|
|
peer.protocolStates[protocol.index] = peerStateInit(peer)
|
|
|
|
|