From 6f28d1186675f648e0587f44e730b9dc23291bab Mon Sep 17 00:00:00 2001 From: zah Date: Tue, 26 Jun 2018 17:09:50 +0300 Subject: [PATCH] Few steps towards having 64-bit block numbers (#57) Some of the other clients don't bother to store block numbers as 256-bit integers (64 bits or even 32 bit are enough). The changes here are based on helpers introduced in eth_common that try to abstract away all conversion steps in the code between VM words and block numbers in variables in fields. Unfortunately, this turn out to be more work than anticipated and I've postponed finishing the refactoring for now (Block numbers are still 256 bit). --- nimbus/constants.nim | 2 +- nimbus/utils/header.nim | 2 +- nimbus/vm/forks/f20150730_frontier/frontier_vm.nim | 4 ++-- nimbus/vm/interpreter/opcodes_impl/block_ops.nim | 6 +++--- nimbus/vm_state.nim | 13 ++++++++----- 5 files changed, 15 insertions(+), 12 deletions(-) diff --git a/nimbus/constants.nim b/nimbus/constants.nim index e63c4bef7..3d95c63c2 100644 --- a/nimbus/constants.nim +++ b/nimbus/constants.nim @@ -53,7 +53,7 @@ let GAS_MOD_EXP_QUADRATIC_DENOMINATOR* = 20.u256 - MAX_PREV_HEADER_DEPTH* = 256.u256 + MAX_PREV_HEADER_DEPTH* = 256.toBlockNumber FORK_ICEAGE_BLKNUM* = 200_000.u256 FORK_HOMESTED_BLKNUM* = 1_150_000.u256 diff --git a/nimbus/utils/header.nim b/nimbus/utils/header.nim index 8bdc8eaa1..2a8593240 100644 --- a/nimbus/utils/header.nim +++ b/nimbus/utils/header.nim @@ -84,7 +84,7 @@ proc generateHeaderFromParentHeader*( # TODO: validateGt(timestamp, parent.timestamp) result = BlockHeader( timestamp: max(getTime(), parent.timestamp + 1.milliseconds), # Note: Py-evm uses +1 second, not ms - blockNumber: (parent.blockNumber + 1.u256), + blockNumber: (parent.blockNumber + 1), # TODO: difficulty: parent.computeDifficulty(parent.timestamp), gasLimit: computeGasLimit(parent, gasLimitFloor = GENESIS_GAS_LIMIT), stateRoot: parent.stateRoot, diff --git a/nimbus/vm/forks/f20150730_frontier/frontier_vm.nim b/nimbus/vm/forks/f20150730_frontier/frontier_vm.nim index 14c39cb12..7ddb3a0a1 100644 --- a/nimbus/vm/forks/f20150730_frontier/frontier_vm.nim +++ b/nimbus/vm/forks/f20150730_frontier/frontier_vm.nim @@ -6,8 +6,8 @@ # at your option. This file may not be copied, modified, or distributed except according to those terms. import + stint, eth_common/eth_types, ../../../logging, ../../../constants, ../../../errors, - stint, ../../../block_types, ../../../vm/[base, stack], ../../../db/db_chain, ../../../utils/header, ./frontier_block, ./frontier_vm_state, ./frontier_validation @@ -22,7 +22,7 @@ method name*(vm: FrontierVM): string = method getBlockReward(vm: FrontierVM): UInt256 = BLOCK_REWARD -method getUncleReward(vm: FrontierVM, blockNumber: UInt256, uncle: Block): UInt256 = +method getUncleReward(vm: FrontierVM, blockNumber: BlockNumber, uncle: Block): UInt256 = BLOCK_REWARD * (UNCLE_DEPTH_PENALTY_FACTOR + uncle.blockNumber - blockNumber) div UNCLE_DEPTH_PENALTY_FACTOR diff --git a/nimbus/vm/interpreter/opcodes_impl/block_ops.nim b/nimbus/vm/interpreter/opcodes_impl/block_ops.nim index 1ed969942..1cab191bd 100644 --- a/nimbus/vm/interpreter/opcodes_impl/block_ops.nim +++ b/nimbus/vm/interpreter/opcodes_impl/block_ops.nim @@ -6,7 +6,7 @@ # at your option. This file may not be copied, modified, or distributed except according to those terms. import - times, ./impl_std_import + times, eth_common/eth_types, ./impl_std_import {.this: computation.} {.experimental.} @@ -15,7 +15,7 @@ using computation: var BaseComputation proc blockhash*(computation) = - let blockNumber = stack.popInt() + let blockNumber = vmWordToBlockNumber stack.popInt() let blockHash = vmState.getAncestorHash(blockNumber) stack.push(blockHash) @@ -28,7 +28,7 @@ proc timestamp*(computation) = stack.push(vmState.timestamp.toUnix.uint64.u256) proc number*(computation) = - stack.push(vmState.blockNumber) + stack.push(blockNumberToVmWord vmState.blockNumber) proc difficulty*(computation) = stack.push(vmState.difficulty) diff --git a/nimbus/vm_state.nim b/nimbus/vm_state.nim index 01d181129..25758a72b 100644 --- a/nimbus/vm_state.nim +++ b/nimbus/vm_state.nim @@ -66,12 +66,15 @@ method gasLimit*(vmState: BaseVMState): GasInt = vmState.blockHeader.gasLimit method getAncestorHash*(vmState: BaseVMState, blockNumber: BlockNumber): Hash256 = - var ancestorDepth = vmState.blockHeader.blockNumber - blockNumber - 1.u256 - if ancestorDepth >= constants.MAX_PREV_HEADER_DEPTH or - ancestorDepth < 0 or - ancestorDepth >= vmState.prevHeaders.len.u256: + var ancestorDepth = vmState.blockHeader.blockNumber - blockNumber - 1 + if ancestorDepth >= constants.MAX_PREV_HEADER_DEPTH or ancestorDepth < 0: return - var header = vmState.prevHeaders[ancestorDepth.toInt] + + let idx = ancestorDepth.toInt + if idx >= vmState.prevHeaders.len: + return + + var header = vmState.prevHeaders[idx] result = header.hash macro db*(vmState: untyped, readOnly: untyped, handler: untyped): untyped =