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).
This commit is contained in:
zah 2018-06-26 17:09:50 +03:00 committed by Mamy Ratsimbazafy
parent 3060452d96
commit 6f28d11866
5 changed files with 15 additions and 12 deletions

View File

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

View File

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

View File

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

View File

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

View File

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