From fef47331c37ee8abb8608037222658737ff498a6 Mon Sep 17 00:00:00 2001 From: Kim De Mey Date: Thu, 20 Oct 2022 08:49:20 +0200 Subject: [PATCH] Networkid common (#547) * Move NetworkId type to common eth_types NetworkId is after all a common type and this way it avoid an application that requires it to also import all devp2p related network types. RLP related calls are moved to eth_types_rlp, this means that p2p protocols that send NetworkId over the wire need to import this too. Or just common in general. * Remove # in front of multiline comment end bracket These seem to be interpreted wrongly by the GitHub code browser. --- eth/common/eth_types.nim | 8 ++++++++ eth/common/eth_types_rlp.nim | 5 +++++ eth/p2p.nim | 8 ++++---- eth/p2p/private/p2p_types.nim | 21 ++++----------------- eth/p2p/rlpx.nim | 4 ++-- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/eth/common/eth_types.nim b/eth/common/eth_types.nim index 8e8c5b3..f081ed9 100644 --- a/eth/common/eth_types.nim +++ b/eth/common/eth_types.nim @@ -51,6 +51,8 @@ type # they are separate entity ChainId* = distinct uint64 + NetworkId* = distinct uint + Account* = object nonce*: AccountNonce balance*: UInt256 @@ -284,3 +286,9 @@ template hasData*(r: EthResourceRefs): bool = r != nil template deref*(b: Blob): auto = b template deref*(o: Option): auto = o.get template deref*(r: EthResourceRefs): auto = r[] + +func `==`*(a, b: NetworkId): bool = + a.uint == b.uint + +func `$`*(x: NetworkId): string = + `$`(uint(x)) diff --git a/eth/common/eth_types_rlp.nim b/eth/common/eth_types_rlp.nim index cf0b119..10a5df4 100644 --- a/eth/common/eth_types_rlp.nim +++ b/eth/common/eth_types_rlp.nim @@ -336,3 +336,8 @@ proc rlpHash*[T](v: T): Hash256 = func blockHash*(h: BlockHeader): KeccakHash {.inline.} = rlpHash(h) +proc append*(rlpWriter: var RlpWriter, id: NetworkId) = + rlpWriter.append(id.uint) + +proc read*(rlp: var Rlp, T: type NetworkId): T = + rlp.read(uint).NetworkId diff --git a/eth/p2p.nim b/eth/p2p.nim index 0e30a96..8d1ced7 100644 --- a/eth/p2p.nim +++ b/eth/p2p.nim @@ -1,5 +1,5 @@ # nim-eth -# Copyright (c) 2018-2021 Status Research & Development GmbH +# Copyright (c) 2018-2022 Status Research & Development GmbH # Licensed and distributed under either of # * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT). # * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0). @@ -46,7 +46,7 @@ template addCapability*(node: var EthereumNode, name(ParamType), name(Protocol.NetworkState)] {. error: errMsg .} - addCapability(node, Protocol.protocolInfo, + addCapability(node, Protocol.protocolInfo, cast[RootRef](networkState)) proc replaceNetworkState*(node: var EthereumNode, @@ -66,14 +66,14 @@ template replaceNetworkState*(node: var EthereumNode, name(ParamType), name(Protocol.NetworkState)] {. error: errMsg .} - replaceNetworkState(node, Protocol.protocolInfo, + replaceNetworkState(node, Protocol.protocolInfo, cast[RootRef](networkState)) proc newEthereumNode*( keys: KeyPair, address: Address, networkId: NetworkId, - clientId = "nim-eth-p2p/0.2.0", # TODO: read this value from nimble somehow + clientId = "nim-eth-p2p", addAllCapabilities = true, useCompression: bool = false, minPeers = 10, diff --git a/eth/p2p/private/p2p_types.nim b/eth/p2p/private/p2p_types.nim index 56e31c5..1477d6e 100644 --- a/eth/p2p/private/p2p_types.nim +++ b/eth/p2p/private/p2p_types.nim @@ -1,6 +1,5 @@ - # nim-eth -# Copyright (c) 2018-2021 Status Research & Development GmbH +# Copyright (c) 2018-2022 Status Research & Development GmbH # Licensed and distributed under either of # * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT). # * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0). @@ -10,15 +9,15 @@ import std/[deques, tables], chronos, stew/results, - ".."/../[rlp, keys], + ".."/../[rlp, keys], ../../common/eth_types, ".."/[enode, kademlia, discovery, rlpxcrypt] +export eth_types.NetworkId + const useSnappy* = defined(useSnappy) type - NetworkId* = distinct uint - EthereumNode* = ref object networkId*: NetworkId clientId*: string @@ -188,15 +187,3 @@ 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.nim b/eth/p2p/rlpx.nim index fc3b916..676ef5d 100644 --- a/eth/p2p/rlpx.nim +++ b/eth/p2p/rlpx.nim @@ -525,7 +525,7 @@ proc getRlpxHeaderData(header: RlpxHeader): (int,int,int) = capabilityId = result[0], contextId = result[1], datagramSize = datagramSize() - #]# + ]# except: error "RLPx message header-data options, parse error", capabilityId = result[0], @@ -659,7 +659,7 @@ proc recvMsg*(peer: Peer): Future[tuple[msgId: int, msgData: Rlp]] {.async.} = trace "RLPx next chunked datagram fragment", peer, msgId = result[0], ctxId, msgSize, moreData, totalMsgSize, dcrBytesCount, payloadSoFar = decryptedBytes.len - #]# + ]# # End While