From dda2bec8aa1bd86dd39f1fbd9698d2e7c7042de7 Mon Sep 17 00:00:00 2001 From: jangko Date: Sat, 13 Feb 2021 15:41:09 +0700 Subject: [PATCH] turn networkId into distinct uint --- eth/p2p.nim | 2 +- eth/p2p/peer_pool.nim | 2 +- eth/p2p/private/p2p_types.nim | 18 ++++++++++++++++-- eth/p2p/rlpx_protocols/eth_protocol.nim | 2 +- eth/p2p/rlpx_protocols/les_protocol.nim | 2 +- tests/p2p/p2p_test_helper.nim | 2 +- 6 files changed, 21 insertions(+), 7 deletions(-) diff --git a/eth/p2p.nim b/eth/p2p.nim index 17fad0a..3e2bc75 100644 --- a/eth/p2p.nim +++ b/eth/p2p.nim @@ -33,7 +33,7 @@ template addCapability*(node: var EthereumNode, Protocol: type) = proc newEthereumNode*(keys: KeyPair, address: Address, - networkId: uint, + networkId: NetworkId, chain: AbstractChainDB, clientId = "nim-eth-p2p/0.2.0", # TODO: read this value from nimble somehow addAllCapabilities = true, diff --git a/eth/p2p/peer_pool.nim b/eth/p2p/peer_pool.nim index 5dbaf9d..58b9226 100644 --- a/eth/p2p/peer_pool.nim +++ b/eth/p2p/peer_pool.nim @@ -11,7 +11,7 @@ const connectLoopSleep = chronos.milliseconds(2000) proc newPeerPool*(network: EthereumNode, - networkId: uint, keyPair: KeyPair, + networkId: NetworkId, keyPair: KeyPair, discovery: DiscoveryProtocol, clientId: string, listenPort = Port(30303), minPeers = 10): PeerPool = new result diff --git a/eth/p2p/private/p2p_types.nim b/eth/p2p/private/p2p_types.nim index b0193c1..aa23227 100644 --- a/eth/p2p/private/p2p_types.nim +++ b/eth/p2p/private/p2p_types.nim @@ -7,8 +7,10 @@ const useSnappy* = defined(useSnappy) type + NetworkId* = distinct uint + EthereumNode* = ref object - networkId*: uint + networkId*: NetworkId chain*: AbstractChainDB clientId*: string connectionState*: ConnectionState @@ -46,7 +48,7 @@ type # Private fields: network*: EthereumNode keyPair*: KeyPair - networkId*: uint + networkId*: NetworkId minPeers*: int clientId*: string discovery*: DiscoveryProtocol @@ -171,3 +173,15 @@ proc `$`*(peer: Peer): string = $peer.remote proc toENode*(v: EthereumNode): ENode = ENode(pubkey: v.keys.pubkey, address: v.address) + +proc append*(rlpWriter: var RlpWriter, id: NetworkId) {.inline.} = + rlpWriter.append(id.uint) + +proc read*(rlp: var Rlp, T: type NetworkId): T {.inline.} = + rlp.read(uint).NetworkId + +func `==`*(a, b: NetworkId): bool {.inline.} = + a.uint == b.uint + +func `$`*(x: NetworkId): string {.inline.} = + `$`(uint(x)) diff --git a/eth/p2p/rlpx_protocols/eth_protocol.nim b/eth/p2p/rlpx_protocols/eth_protocol.nim index 951f981..4ca8c62 100644 --- a/eth/p2p/rlpx_protocols/eth_protocol.nim +++ b/eth/p2p/rlpx_protocols/eth_protocol.nim @@ -64,7 +64,7 @@ p2pProtocol eth(version = protocolVersion, handshake: proc status(peer: Peer, protocolVersion: uint, - networkId: uint, + networkId: NetworkId, totalDifficulty: DifficultyInt, bestHash: KeccakHash, genesisHash: KeccakHash) diff --git a/eth/p2p/rlpx_protocols/les_protocol.nim b/eth/p2p/rlpx_protocols/les_protocol.nim index 5dbc777..18ea897 100644 --- a/eth/p2p/rlpx_protocols/les_protocol.nim +++ b/eth/p2p/rlpx_protocols/les_protocol.nim @@ -206,7 +206,7 @@ p2pProtocol les(version = lesVersion, let s = await peer.status(lesProperties, timeout = chronos.seconds(10)) - peerNetworkId = s.values.getRequiredValue(keyNetworkId, uint) + peerNetworkId = s.values.getRequiredValue(keyNetworkId, NetworkId) peerGenesisHash = s.values.getRequiredValue(keyGenesisHash, KeccakHash) peerLesVersion = s.values.getRequiredValue(keyProtocolVersion, uint) diff --git a/tests/p2p/p2p_test_helper.nim b/tests/p2p/p2p_test_helper.nim index e1e2d29..56c4816 100644 --- a/tests/p2p/p2p_test_helper.nim +++ b/tests/p2p/p2p_test_helper.nim @@ -20,7 +20,7 @@ proc setupTestNode*( capabilities: varargs[ProtocolInfo, `protocolInfo`]): EthereumNode {.gcsafe.} = # Don't create new RNG every time in production code! let keys1 = KeyPair.random(rng[]) - result = newEthereumNode(keys1, localAddress(nextPort), 1, nil, + result = newEthereumNode(keys1, localAddress(nextPort), NetworkId(1), nil, addAllCapabilities = false, rng = rng) nextPort.inc for capability in capabilities: