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.
This commit is contained in:
Kim De Mey 2022-10-20 08:49:20 +02:00 committed by GitHub
parent cc52ef35e4
commit fef47331c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 23 deletions

View File

@ -51,6 +51,8 @@ type
# they are separate entity # they are separate entity
ChainId* = distinct uint64 ChainId* = distinct uint64
NetworkId* = distinct uint
Account* = object Account* = object
nonce*: AccountNonce nonce*: AccountNonce
balance*: UInt256 balance*: UInt256
@ -284,3 +286,9 @@ template hasData*(r: EthResourceRefs): bool = r != nil
template deref*(b: Blob): auto = b template deref*(b: Blob): auto = b
template deref*(o: Option): auto = o.get template deref*(o: Option): auto = o.get
template deref*(r: EthResourceRefs): auto = r[] template deref*(r: EthResourceRefs): auto = r[]
func `==`*(a, b: NetworkId): bool =
a.uint == b.uint
func `$`*(x: NetworkId): string =
`$`(uint(x))

View File

@ -336,3 +336,8 @@ proc rlpHash*[T](v: T): Hash256 =
func blockHash*(h: BlockHeader): KeccakHash {.inline.} = rlpHash(h) 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

View File

@ -1,5 +1,5 @@
# nim-eth # nim-eth
# Copyright (c) 2018-2021 Status Research & Development GmbH # Copyright (c) 2018-2022 Status Research & Development GmbH
# Licensed and distributed under either of # Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT). # * 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). # * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
@ -73,7 +73,7 @@ proc newEthereumNode*(
keys: KeyPair, keys: KeyPair,
address: Address, address: Address,
networkId: NetworkId, networkId: NetworkId,
clientId = "nim-eth-p2p/0.2.0", # TODO: read this value from nimble somehow clientId = "nim-eth-p2p",
addAllCapabilities = true, addAllCapabilities = true,
useCompression: bool = false, useCompression: bool = false,
minPeers = 10, minPeers = 10,

View File

@ -1,6 +1,5 @@
# nim-eth # nim-eth
# Copyright (c) 2018-2021 Status Research & Development GmbH # Copyright (c) 2018-2022 Status Research & Development GmbH
# Licensed and distributed under either of # Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT). # * 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). # * 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], std/[deques, tables],
chronos, chronos,
stew/results, stew/results,
".."/../[rlp, keys], ".."/../[rlp, keys], ../../common/eth_types,
".."/[enode, kademlia, discovery, rlpxcrypt] ".."/[enode, kademlia, discovery, rlpxcrypt]
export eth_types.NetworkId
const const
useSnappy* = defined(useSnappy) useSnappy* = defined(useSnappy)
type type
NetworkId* = distinct uint
EthereumNode* = ref object EthereumNode* = ref object
networkId*: NetworkId networkId*: NetworkId
clientId*: string clientId*: string
@ -188,15 +187,3 @@ proc `$`*(peer: Peer): string = $peer.remote
proc toENode*(v: EthereumNode): ENode = proc toENode*(v: EthereumNode): ENode =
ENode(pubkey: v.keys.pubkey, address: v.address) 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))

View File

@ -525,7 +525,7 @@ proc getRlpxHeaderData(header: RlpxHeader): (int,int,int) =
capabilityId = result[0], capabilityId = result[0],
contextId = result[1], contextId = result[1],
datagramSize = datagramSize() datagramSize = datagramSize()
#]# ]#
except: except:
error "RLPx message header-data options, parse error", error "RLPx message header-data options, parse error",
capabilityId = result[0], capabilityId = result[0],
@ -659,7 +659,7 @@ proc recvMsg*(peer: Peer): Future[tuple[msgId: int, msgData: Rlp]] {.async.} =
trace "RLPx next chunked datagram fragment", trace "RLPx next chunked datagram fragment",
peer, msgId = result[0], ctxId, msgSize, moreData, totalMsgSize, peer, msgId = result[0], ctxId, msgSize, moreData, totalMsgSize,
dcrBytesCount, payloadSoFar = decryptedBytes.len dcrBytesCount, payloadSoFar = decryptedBytes.len
#]# ]#
# End While # End While