From e90c5440e8b0f6fc2d3c1f269524449a2544937e Mon Sep 17 00:00:00 2001 From: protolambda Date: Thu, 6 Aug 2020 11:08:54 +0200 Subject: [PATCH] make eth1 distance runtime configurable --- beacon_chain/mainchain_monitor.nim | 22 +++++++++---------- beacon_chain/spec/presets.nim | 11 +++++++--- beacon_chain/spec/presets/v0_12_2/mainnet.nim | 2 -- beacon_chain/spec/presets/v0_12_2/minimal.nim | 3 --- tests/simulation/start.sh | 6 ++--- 5 files changed, 22 insertions(+), 22 deletions(-) diff --git a/beacon_chain/mainchain_monitor.nim b/beacon_chain/mainchain_monitor.nim index 499972652..d56804346 100644 --- a/beacon_chain/mainchain_monitor.nim +++ b/beacon_chain/mainchain_monitor.nim @@ -94,11 +94,10 @@ type const reorgDepthLimit = 1000 web3Timeouts = 5.seconds - followDistanceInSeconds = uint64(SECONDS_PER_ETH1_BLOCK * ETH1_FOLLOW_DISTANCE) # TODO: Add preset validation # MIN_GENESIS_ACTIVE_VALIDATOR_COUNT should be larger than SLOTS_PER_EPOCH -# doAssert SECONDS_PER_ETH1_BLOCK * ETH1_FOLLOW_DISTANCE < GENESIS_DELAY, +# doAssert SECONDS_PER_ETH1_BLOCK * preset.ETH1_FOLLOW_DISTANCE < GENESIS_DELAY, # "Invalid configuration: GENESIS_DELAY is set too low" # TODO Nim's analysis on the lock level of the methods in this @@ -116,9 +115,9 @@ func voting_period_start_time*(state: BeaconState): uint64 = compute_time_at_slot(state, eth1_voting_period_start_slot) # https://github.com/ethereum/eth2.0-specs/blob/v0.12.2/specs/phase0/validator.md#get_eth1_data -func is_candidate_block(blk: Eth1Block, period_start: uint64): bool = - (blk.timestamp + SECONDS_PER_ETH1_BLOCK.uint64 * ETH1_FOLLOW_DISTANCE.uint64 <= period_start) and - (blk.timestamp + SECONDS_PER_ETH1_BLOCK.uint64 * ETH1_FOLLOW_DISTANCE.uint64 * 2 >= period_start) +func is_candidate_block(preset: RuntimePreset, blk: Eth1Block, period_start: uint64): bool = + (blk.timestamp + SECONDS_PER_ETH1_BLOCK.uint64 * preset.ETH1_FOLLOW_DISTANCE <= period_start) and + (blk.timestamp + SECONDS_PER_ETH1_BLOCK.uint64 * preset.ETH1_FOLLOW_DISTANCE * 2 >= period_start) func asEth2Digest*(x: BlockHash): Eth2Digest = Eth2Digest(data: array[32, byte](x)) @@ -164,10 +163,10 @@ proc findParent*(eth1Chain: Eth1Chain, blk: BlockObject): Eth1Block = parentHash = blk.parentHash.toHex, parentNumber = result.number result = nil -func latestCandidateBlock(eth1Chain: Eth1Chain, periodStart: uint64): Eth1Block = +func latestCandidateBlock(eth1Chain: Eth1Chain, preset: RuntimePreset, periodStart: uint64): Eth1Block = for i in countdown(eth1Chain.blocks.len - 1, 0): let blk = eth1Chain.blocks[i] - if is_candidate_block(blk, periodStart): + if is_candidate_block(preset, blk, periodStart): return blk func popBlock(eth1Chain: var Eth1Chain) = @@ -421,7 +420,7 @@ method onBlockHeaders*(p: Web3DataProviderRef, options, blockHeaderHandler, errorHandler) # https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/validator.md#get_eth1_data -func getBlockProposalData*(eth1Chain: Eth1Chain, +func getBlockProposalData*(preset: RuntimePreset, eth1Chain: Eth1Chain, state: BeaconState): (Eth1Data, seq[Deposit]) = template voteForNoChange() = return (state.eth1_data, newSeq[Deposit]()) @@ -440,21 +439,21 @@ func getBlockProposalData*(eth1Chain: Eth1Chain, var otherVotesCountTable = initCountTable[Eth1Block]() for vote in state.eth1_data_votes: let eth1Block = eth1Chain.findBlock(vote) - if eth1Block != nil and is_candidate_block(eth1Block, periodStart): + if eth1Block != nil and is_candidate_block(preset, eth1Block, periodStart): otherVotesCountTable.inc eth1Block var ourVote: Eth1Block if otherVotesCountTable.len > 0: ourVote = otherVotesCountTable.largest.key else: - ourVote = eth1Chain.latestCandidateBlock(periodStart) + ourVote = eth1Chain.latestCandidateBlock(preset, periodStart) if ourVote == nil: voteForNoChange() (ourVote.voteData, eth1Chain.getDepositsInRange(prevBlock.number, ourVote.number)) template getBlockProposalData*(m: MainchainMonitor, state: BeaconState): untyped = - getBlockProposalData(m.eth1Chain, state) + getBlockProposalData(m.preset, m.eth1Chain, state) proc init*(T: type MainchainMonitor, preset: RuntimePreset, @@ -470,6 +469,7 @@ proc init*(T: type MainchainMonitor, proc isCandidateForGenesis(preset: RuntimePreset, timeNow: float, blk: Eth1Block): bool = + let followDistanceInSeconds = uint64(SECONDS_PER_ETH1_BLOCK) * preset.ETH1_FOLLOW_DISTANCE if float(blk.timestamp + followDistanceInSeconds) > timeNow: return false diff --git a/beacon_chain/spec/presets.nim b/beacon_chain/spec/presets.nim index 0fc890691..999707da3 100644 --- a/beacon_chain/spec/presets.nim +++ b/beacon_chain/spec/presets.nim @@ -18,6 +18,7 @@ type GENESIS_DELAY*: uint64 MIN_GENESIS_ACTIVE_VALIDATOR_COUNT*: uint64 MIN_GENESIS_TIME*: uint64 + ETH1_FOLLOW_DISTANCE*: uint64 PresetFile* = object values*: Table[PresetValue, TaintedString] @@ -33,6 +34,7 @@ const MIN_GENESIS_TIME, GENESIS_FORK_VERSION, GENESIS_DELAY, + ETH1_FOLLOW_DISTANCE, } # These constants cannot really be overriden in a preset. @@ -124,13 +126,15 @@ const MIN_GENESIS_ACTIVE_VALIDATOR_COUNT: 16384, MIN_GENESIS_TIME: 1578009600, GENESIS_FORK_VERSION: Version [byte 0, 0, 0, 0], - GENESIS_DELAY: 172800) + GENESIS_DELAY: 172800, + ETH1_FOLLOW_DISTANCE: 1024) minimalRuntimePreset* = RuntimePreset( MIN_GENESIS_ACTIVE_VALIDATOR_COUNT: 64, MIN_GENESIS_TIME: 1578009600, GENESIS_FORK_VERSION: Version [byte 0, 0, 0, 1], - GENESIS_DELAY: 300) + GENESIS_DELAY: 300, + ETH1_FOLLOW_DISTANCE: 16) when const_preset == "mainnet": template defaultRuntimePreset*: auto = mainnetRuntimePreset @@ -170,5 +174,6 @@ else: MIN_GENESIS_ACTIVE_VALIDATOR_COUNT: MIN_GENESIS_ACTIVE_VALIDATOR_COUNT, MIN_GENESIS_TIME: MIN_GENESIS_TIME, GENESIS_FORK_VERSION: GENESIS_FORK_VERSION, - GENESIS_DELAY: GENESIS_DELAY) + GENESIS_DELAY: GENESIS_DELAY, + ETH1_FOLLOW_DISTANCE: ETH1_FOLLOW_DISTANCE) diff --git a/beacon_chain/spec/presets/v0_12_2/mainnet.nim b/beacon_chain/spec/presets/v0_12_2/mainnet.nim index aff2b6aa8..a31f3c322 100644 --- a/beacon_chain/spec/presets/v0_12_2/mainnet.nim +++ b/beacon_chain/spec/presets/v0_12_2/mainnet.nim @@ -151,8 +151,6 @@ const # Validators # --------------------------------------------------------------- - # https://github.com/ethereum/eth2.0-specs/blob/v0.12.2/configs/mainnet/phase0.yaml#L38 - ETH1_FOLLOW_DISTANCE* {.intdefine.}: uint64 = 1024 # blocks ~ 4 hours TARGET_AGGREGATORS_PER_COMMITTEE*: uint64 = 16 # validators RANDOM_SUBNETS_PER_VALIDATOR*: uint64 = 1 # subnet EPOCHS_PER_RANDOM_SUBNET_SUBSCRIPTION*: uint64 = 256 # epochs ~ 27 hours diff --git a/beacon_chain/spec/presets/v0_12_2/minimal.nim b/beacon_chain/spec/presets/v0_12_2/minimal.nim index 75e450f0d..780112c1d 100644 --- a/beacon_chain/spec/presets/v0_12_2/minimal.nim +++ b/beacon_chain/spec/presets/v0_12_2/minimal.nim @@ -136,9 +136,6 @@ const # --------------------------------------------------------------- # https://github.com/ethereum/eth2.0-specs/blob/v0.12.2/configs/minimal/phase0.yaml#L38 - # Changed - ETH1_FOLLOW_DISTANCE* {.intdefine.}: uint64 = 16 # blocks - # Unchanged TARGET_AGGREGATORS_PER_COMMITTEE*: uint64 = 16 # validators RANDOM_SUBNETS_PER_VALIDATOR*: uint64 = 1 # subnet diff --git a/tests/simulation/start.sh b/tests/simulation/start.sh index 48b0cd1e3..8aa3374f1 100755 --- a/tests/simulation/start.sh +++ b/tests/simulation/start.sh @@ -54,8 +54,7 @@ GANACHE_BLOCK_TIME=5 # Run with "SLOTS_PER_EPOCH=8 ./start.sh" to change these DEFS="" -DEFS+="-d:SECONDS_PER_ETH1_BLOCK=$GANACHE_BLOCK_TIME \ - -d:ETH1_FOLLOW_DISTANCE=1 " +DEFS+="-d:SECONDS_PER_ETH1_BLOCK=$GANACHE_BLOCK_TIME" DEFS+="-d:MAX_COMMITTEES_PER_SLOT=${MAX_COMMITTEES_PER_SLOT:-1} " # Spec default: 64 DEFS+="-d:SLOTS_PER_EPOCH=${SLOTS_PER_EPOCH:-6} " # Spec default: 32 DEFS+="-d:SECONDS_PER_SLOT=${SECONDS_PER_SLOT:-6} " # Spec default: 12 @@ -201,7 +200,8 @@ tee "$NETWORK_METADATA_FILE" <