Skip to content

Commit

Permalink
fixes #558, use distinct uint for ChainId and NetworkId to prevent co…
Browse files Browse the repository at this point in the history
…nfusion
  • Loading branch information
jangko committed Feb 13, 2021
1 parent 2566ceb commit 76f88a9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
40 changes: 22 additions & 18 deletions nimbus/config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ type
staticNodes*: seq[ENode] ## List of static nodes to connect to
bindPort*: uint16 ## Main TCP bind port
discPort*: uint16 ## Discovery UDP bind port
metricsServer*: bool ## Enable metrics server
metricsServer*: bool ## Enable metrics server
metricsServerPort*: uint16 ## metrics HTTP server port
maxPeers*: int ## Maximum allowed number of peers
maxPendingPeers*: int ## Maximum allowed pending peers
networkId*: uint ## Network ID as integer
networkId*: NetworkId ## Network ID as integer
ident*: string ## Server ident name string
nodeKey*: PrivateKey ## Server private key
nat*: NatStrategy ## NAT strategy
Expand All @@ -126,8 +126,13 @@ type
keystore*: JsonNode
unlocked*: bool

# beware that although in some cases
# chainId have identical value to networkId
# they are separate entity
ChainId* = distinct uint

ChainConfig* = object
chainId*: uint
chainId*: ChainId
homesteadBlock*: BlockNumber
daoForkBlock*: BlockNumber
daoForkSupport*: bool
Expand Down Expand Up @@ -162,7 +167,7 @@ type
accounts*: Table[EthAddress, NimbusAccount]

CustomGenesisConfig = object
chainId*: uint
chainId*: ChainId
homesteadBlock*: BlockNumber
daoForkBlock*: BlockNumber
daoForkSupport*: bool
Expand Down Expand Up @@ -230,7 +235,7 @@ proc publicChainConfig*(id: PublicNetwork): ChainConfig =
result = case id
of MainNet:
ChainConfig(
chainId: MainNet.uint,
chainId: MainNet.ChainId,
homesteadBlock: 1_150_000.toBlockNumber, # 14/03/2016 20:49:53
daoForkBlock: 1_920_000.toBlockNumber,
daoForkSupport: true,
Expand All @@ -247,7 +252,7 @@ proc publicChainConfig*(id: PublicNetwork): ChainConfig =
)
of RopstenNet:
ChainConfig(
chainId: RopstenNet.uint,
chainId: RopstenNet.ChainId,
homesteadBlock: 0.toBlockNumber,
daoForkSupport: false,
eip150Block: 0.toBlockNumber,
Expand All @@ -263,7 +268,7 @@ proc publicChainConfig*(id: PublicNetwork): ChainConfig =
)
of RinkebyNet:
ChainConfig(
chainId: RinkebyNet.uint,
chainId: RinkebyNet.ChainId,
homesteadBlock: 1.toBlockNumber,
daoForkSupport: false,
eip150Block: 2.toBlockNumber,
Expand All @@ -279,7 +284,7 @@ proc publicChainConfig*(id: PublicNetwork): ChainConfig =
)
of GoerliNet:
ChainConfig(
chainId: GoerliNet.uint,
chainId: GoerliNet.ChainId,
homesteadBlock: 0.toBlockNumber,
daoForkSupport: false,
eip150Block: 0.toBlockNumber,
Expand All @@ -300,7 +305,7 @@ proc publicChainConfig*(id: PublicNetwork): ChainConfig =
doAssert(false, "No chain config for " & $id)
ChainConfig()

result.chainId = uint(id)
result.chainId = ChainId(id)

proc processCustomGenesisConfig(customGenesis: JsonNode): ConfigStatus =
## Parses Custom Genesis Block config options when customnetwork option provided
Expand Down Expand Up @@ -357,7 +362,7 @@ proc processCustomGenesisConfig(customGenesis: JsonNode): ConfigStatus =
let config = getConfiguration()
result = Success
var
chainId = 0.uint
chainId = 0.ChainId
homesteadBlock, daoForkblock, eip150Block, eip155Block, eip158Block, byzantiumBlock, constantinopleBlock = 0.toBlockNumber
petersburgBlock, istanbulBlock, muirGlacierBlock, berlinBlock = 0.toBlockNumber
eip150Hash, mixHash : MDigest[256]
Expand All @@ -374,8 +379,7 @@ proc processCustomGenesisConfig(customGenesis: JsonNode): ConfigStatus =
if customGenesis.hasKey("config"):
# Validate all fork blocks for custom genesis
let forkDetails = customGenesis["config"]
validateConfigValue(forkDetails, chainId, JInt, uint)
config.net.networkId = chainId
validateConfigValue(forkDetails, chainId, JInt, ChainId)
checkForFork(forkDetails, homesteadBlock, 0.toBlockNumber)
validateConfigValue(forkDetails, daoForkSupport, JBool, bool, checkError=false)
if daoForkSupport == true:
Expand Down Expand Up @@ -613,15 +617,15 @@ proc setBootnodes(onodes: var seq[ENode], nodeUris: openarray[string]) =
macro availableEnumValues(T: type enum): untyped =
let impl = getTypeImpl(T)[1].getTypeImpl()
result = newNimNode(nnkBracket)
for i in 1 ..< impl.len: result.add(newCall("uint", copyNimTree(impl[i])))
for i in 1 ..< impl.len: result.add(newCall("NetworkId", copyNimTree(impl[i])))

proc toPublicNetwork*(id: uint): PublicNetwork {.inline.} =
proc toPublicNetwork*(id: NetworkId): PublicNetwork {.inline.} =
if id in availableEnumValues(PublicNetwork):
result = PublicNetwork(id)

proc setNetwork(conf: var NetConfiguration, id: PublicNetwork) =
## Set network id and default network bootnodes
conf.networkId = uint(id)
conf.networkId = NetworkId(id)
case id
of MainNet:
conf.bootNodes.setBootnodes(MainnetBootnodes)
Expand All @@ -638,7 +642,7 @@ proc setNetwork(conf: var NetConfiguration, id: PublicNetwork) =
of CustomNet:
discard

proc setNetwork(conf: var NetConfiguration, id: uint) =
proc setNetwork(conf: var NetConfiguration, id: NetworkId) =
## Set network id and default network bootnodes
let pubNet = toPublicNetwork(id)
if pubNet == CustomNet:
Expand Down Expand Up @@ -692,7 +696,7 @@ proc processNetArguments(key, value: string): ConfigStatus =
var res = 0
result = processInteger(value, res)
if result == Success:
config.net.setNetwork(uint(result))
config.net.setNetwork(NetworkId(result))
elif skey == "nodiscover":
config.net.flags.incl(NoDiscover)
elif skey == "v5discover":
Expand Down Expand Up @@ -858,7 +862,7 @@ proc initConfiguration(): NimbusConfiguration =
result.rpc.binds = @[initTAddress("127.0.0.1:8545")]

## Network defaults
result.net.setNetwork(defaultNetwork)
result.net.setNetwork(defaultNetwork.NetworkId)
result.net.maxPeers = 25
result.net.maxPendingPeers = 0
result.net.bindPort = 30303'u16
Expand Down
4 changes: 2 additions & 2 deletions nimbus/rpc/rpc_utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ proc unsignedTx*(tx: TxSend, chain: BaseChainDB, defaultNonce: AccountNonce): Un

result.payload = hexToSeqByte(tx.data.string)

func rlpEncode(tx: UnsignedTx, chainId: uint): auto =
func rlpEncode(tx: UnsignedTx, chainId: ChainId): auto =
rlp.encode(Transaction(
accountNonce: tx.nonce,
gasPrice: tx.gasPrice,
Expand All @@ -144,7 +144,7 @@ proc signTransaction*(tx: UnsignedTx, chain: BaseChainDB, privateKey: PrivateKey

let sig = sign(privateKey, rlpTx).toRaw
let v = if eip155:
byte(sig[64].uint + chain.config.chainId * 2'u + 35'u)
byte(sig[64].uint + chain.config.chainId.uint * 2'u + 35'u)
else:
sig[64] + 27.byte

Expand Down
2 changes: 1 addition & 1 deletion nimbus/vm/computation.nim
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ template getChainId*(c: Computation): uint =
when evmc_enabled:
Uint256.fromEvmc(c.host.getTxContext().chain_id).truncate(uint)
else:
c.vmState.chaindb.config.chainId
c.vmState.chaindb.config.chainId.uint

template getOrigin*(c: Computation): EthAddress =
when evmc_enabled:
Expand Down
1 change: 1 addition & 0 deletions tests/test_rpc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import
nimcrypto, stew/byteutils, times,
json_rpc/[rpcserver, rpcclient], eth/common as eth_common,
eth/[rlp, keys], eth/trie/db, eth/p2p/rlpx_protocols/eth_protocol,
eth/p2p/private/p2p_types,
../nimbus/rpc/[common, p2p, hexstrings, rpc_types, rpc_utils],
../nimbus/[constants, vm_state, config, genesis, utils, transaction],
../nimbus/db/[accounts_cache, db_chain, storage_types, state_db],
Expand Down

0 comments on commit 76f88a9

Please sign in to comment.