From c4c596a90fef3589bb917f396c2fffa612e27297 Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Thu, 29 Nov 2018 03:10:05 +0200 Subject: [PATCH] small fixes needed for the beacon node build --- eth_p2p.nim | 9 +++++++++ eth_p2p/p2p_tracing.nim | 28 +++++++++++++++------------- eth_p2p/rlpx.nim | 2 ++ 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/eth_p2p.nim b/eth_p2p.nim index f2c5223..919bb0d 100644 --- a/eth_p2p.nim +++ b/eth_p2p.nim @@ -149,3 +149,12 @@ proc randomPeer*(node: EthereumNode): Peer = for peer in node.peers: if i == peerIdx: return peer inc i + +proc randomPeerWith*(node: EthereumNode, Protocol: type): Peer = + mixin state + var candidates = newSeq[Peer]() + for p in node.peers(Protocol): + candidates.add(p) + if candidates.len > 0: + return candidates[random(candidates.len)] + diff --git a/eth_p2p/p2p_tracing.nim b/eth_p2p/p2p_tracing.nim index ee45f36..da12e4e 100644 --- a/eth_p2p/p2p_tracing.nim +++ b/eth_p2p/p2p_tracing.nim @@ -1,22 +1,24 @@ import - macros, - serialization, json_serialization/writer, - chronicles, chronicles_tail/configuration, private/types -export - # XXX: Nim visibility rules get in the way here. - # It would be nice if the users of this module don't have to - # import json_serializer, but this won't work at the moment, - # because the `encode` call inside `logMsgEvent` has its symbols - # mixed in from the module where `logMsgEvent` is called - # (instead of from this module, which will be more logical). - init, writeValue, getOutput - # TODO: File this as an issue - const tracingEnabled* = defined(p2pdump) when tracingEnabled: + import + macros, + serialization, json_serialization/writer, + chronicles, chronicles_tail/configuration + + export + # XXX: Nim visibility rules get in the way here. + # It would be nice if the users of this module don't have to + # import json_serializer, but this won't work at the moment, + # because the `encode` call inside `logMsgEvent` has its symbols + # mixed in from the module where `logMsgEvent` is called + # (instead of from this module, which will be more logical). + init, writeValue, getOutput + # TODO: File this as an issue + logStream p2pMessages[json[file(p2p_messages.json,truncate)]] p2pMessages.useTailPlugin "p2p_tracing_ctail_plugin.nim" diff --git a/eth_p2p/rlpx.nim b/eth_p2p/rlpx.nim index a50f2d1..539c380 100644 --- a/eth_p2p/rlpx.nim +++ b/eth_p2p/rlpx.nim @@ -548,6 +548,7 @@ proc getState(peer: Peer, proto: ProtocolInfo): RootRef = 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)) @@ -555,6 +556,7 @@ proc getNetworkState(node: EthereumNode, proto: ProtocolInfo): RootRef = node.protocolStates[proto.index] template protocolState*(node: EthereumNode, Protocol: type): untyped = + mixin NetworkState bind getNetworkState cast[Protocol.NetworkState](getNetworkState(node, Protocol.protocolInfo))