2024-02-14 08:59:13 +00:00
|
|
|
# nim-eth
|
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
2024-01-22 09:47:46 +00:00
|
|
|
let protocolManager = ProtocolManager()
|
2019-06-05 01:59:35 +00:00
|
|
|
|
|
|
|
# The variables above are immutable RTTI information. We need to tell
|
|
|
|
# Nim to not consider them GcSafe violations:
|
2024-01-22 09:47:46 +00:00
|
|
|
proc registerProtocol*(proto: ProtocolInfo) {.gcsafe.} =
|
|
|
|
{.gcsafe.}:
|
|
|
|
proto.index = protocolManager.protocols.len
|
|
|
|
if proto.name == "p2p":
|
|
|
|
doAssert(proto.index == 0)
|
|
|
|
protocolManager.protocols.add proto
|
|
|
|
|
|
|
|
proc protocolCount*(): int {.gcsafe.} =
|
|
|
|
{.gcsafe.}:
|
|
|
|
protocolManager.protocols.len
|
|
|
|
|
|
|
|
proc getProtocol*(index: int): ProtocolInfo {.gcsafe.} =
|
|
|
|
{.gcsafe.}:
|
|
|
|
protocolManager.protocols[index]
|
|
|
|
|
|
|
|
iterator protocols*(): ProtocolInfo {.gcsafe.} =
|
|
|
|
{.gcsafe.}:
|
|
|
|
for x in protocolManager.protocols:
|
|
|
|
yield x
|
|
|
|
|
|
|
|
template getProtocol*(Protocol: type): ProtocolInfo =
|
|
|
|
getProtocol(Protocol.index)
|
|
|
|
|
|
|
|
template devp2pInfo*(): ProtocolInfo =
|
|
|
|
getProtocol(0)
|
2019-06-05 01:59:35 +00:00
|
|
|
|
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)
|
|
|
|
|
2021-05-06 15:20:54 +00:00
|
|
|
proc initProtocolState*[T](state: T, x: Peer|EthereumNode)
|
2023-05-10 13:50:04 +00:00
|
|
|
{.gcsafe, raises: [].} =
|
2021-05-06 15:20:54 +00:00
|
|
|
discard
|
2019-03-11 09:22:06 +00:00
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
proc initProtocolStates(peer: Peer, protocols: openArray[ProtocolInfo])
|
2023-05-10 13:50:04 +00:00
|
|
|
{.raises: [].} =
|
2019-06-17 11:19:13 +00:00
|
|
|
# Initialize all the active protocol states
|
2024-01-22 09:47:46 +00:00
|
|
|
newSeq(peer.protocolStates, protocolCount())
|
2019-06-17 11:19:13 +00:00
|
|
|
for protocol in protocols:
|
|
|
|
let peerStateInit = protocol.peerStateInitializer
|
|
|
|
if peerStateInit != nil:
|
|
|
|
peer.protocolStates[protocol.index] = peerStateInit(peer)
|
2022-11-09 17:57:04 +00:00
|
|
|
|
2024-02-14 08:59:13 +00:00
|
|
|
{.pop.}
|
|
|
|
|