turn networkId into distinct uint

This commit is contained in:
jangko 2021-02-13 15:41:09 +07:00 committed by andri lim
parent e8c9691b35
commit dda2bec8aa
6 changed files with 21 additions and 7 deletions

View File

@ -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,

View File

@ -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

View File

@ -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))

View File

@ -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)

View File

@ -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)

View File

@ -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: