From a860cd6250286b9ceda7c71a29a6e4cbd3079380 Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Thu, 23 Dec 2021 16:58:54 +0200 Subject: [PATCH] Restore the build support of the -d:has_genesis_detection feature --- beacon_chain/eth1/eth1_monitor.nim | 54 +++++++++++++---------------- beacon_chain/nimbus_beacon_node.nim | 14 +++++--- beacon_chain/spec/beaconstate.nim | 2 +- 3 files changed, 34 insertions(+), 36 deletions(-) diff --git a/beacon_chain/eth1/eth1_monitor.nim b/beacon_chain/eth1/eth1_monitor.nim index 122ba5674..25482cb34 100644 --- a/beacon_chain/eth1/eth1_monitor.nim +++ b/beacon_chain/eth1/eth1_monitor.nim @@ -17,7 +17,7 @@ import eth/async_utils, stew/[objects, byteutils, shims/hashes], # Local modules: ../spec/[eth2_merkleization, forks, helpers], - ../spec/datatypes/[base, merge], + ../spec/datatypes/[base, phase0, merge], ../networking/network_metadata, ../consensus_object_pools/block_pools_types, ".."/[beacon_chain_db, beacon_node_status], @@ -60,7 +60,7 @@ type Eth1BlockTimestamp* = uint64 Eth1BlockHeader = web3Types.BlockHeader - Database* = object + GenesisStateRef = ref phase0.BeaconState Eth1Block* = ref object number*: Eth1BlockNumber @@ -110,7 +110,7 @@ type when hasGenesisDetection: genesisValidators: seq[ImmutableValidatorData] genesisValidatorKeyToIndex: Table[ValidatorPubKey, ValidatorIndex] - genesisState: NilableBeaconStateRef + genesisState: GenesisStateRef genesisStateFut: Future[void] Web3DataProvider* = object @@ -168,18 +168,17 @@ func depositCountU64(s: DepositContractState): uint64 = uint64.fromBytesBE s.deposit_count[24..31] +template cfg(m: Eth1Monitor): auto = + m.depositsChain.cfg + when hasGenesisDetection: - import spec/[beaconstate, signatures] + import ../spec/[beaconstate, signatures] template hasEnoughValidators(m: Eth1Monitor, blk: Eth1Block): bool = blk.activeValidatorsCount >= m.cfg.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT func chainHasEnoughValidators(m: Eth1Monitor): bool = - if m.depositsChain.blocks.len > 0: - m.hasEnoughValidators(m.depositsChain.blocks[^1]) - else: - m.knownStart.depositContractState.depositCountU64 >= - m.cfg.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT + m.depositsChain.blocks.len > 0 and m.hasEnoughValidators(m.depositsChain.blocks[^1]) func isAfterMinGenesisTime(m: Eth1Monitor, blk: Eth1Block): bool = doAssert blk.timestamp != 0 @@ -189,10 +188,10 @@ when hasGenesisDetection: func isGenesisCandidate(m: Eth1Monitor, blk: Eth1Block): bool = m.hasEnoughValidators(blk) and m.isAfterMinGenesisTime(blk) - func findGenesisBlockInRange(m: Eth1Monitor, startBlock, endBlock: Eth1Block): - Future[Eth1Block] {.async, gcsafe.} + proc findGenesisBlockInRange(m: Eth1Monitor, startBlock, endBlock: Eth1Block): + Future[Eth1Block] {.gcsafe.} - func signalGenesis(m: Eth1Monitor, genesisState: BeaconStateRef) = + proc signalGenesis(m: Eth1Monitor, genesisState: GenesisStateRef) = m.genesisState = genesisState if not m.genesisStateFut.isNil: @@ -200,10 +199,10 @@ when hasGenesisDetection: m.genesisStateFut = nil func allGenesisDepositsUpTo(m: Eth1Monitor, totalDeposits: uint64): seq[DepositData] = - for i in 0'u64 ..< totalDeposits: - result.add m.db.genesisDeposits.get(i) + for i in 0 ..< int64(totalDeposits): + result.add m.depositsChain.db.genesisDeposits.get(i) - func createGenesisState(m: Eth1Monitor, eth1Block: Eth1Block): BeaconStateRef = + proc createGenesisState(m: Eth1Monitor, eth1Block: Eth1Block): GenesisStateRef = notice "Generating genesis state", blockNum = eth1Block.number, blockHash = eth1Block.voteData.block_hash, @@ -222,7 +221,7 @@ when hasGenesisDetection: if eth1Block.activeValidatorsCount != 0: doAssert result.validators.lenu64 == eth1Block.activeValidatorsCount - func produceDerivedData(m: Eth1Monitor, deposit: DepositData) = + proc produceDerivedData(m: Eth1Monitor, deposit: DepositData) = let htr = hash_tree_root(deposit) if verify_deposit_signature(m.cfg, deposit): @@ -234,16 +233,13 @@ when hasGenesisDetection: withdrawal_credentials: deposit.withdrawal_credentials) m.genesisValidatorKeyToIndex[pubkey] = idx - func processGenesisDeposit*(m: Eth1Monitor, newDeposit: DepositData) = - m.db.genesisDeposits.add newDeposit + proc processGenesisDeposit*(m: Eth1Monitor, newDeposit: DepositData) = + m.depositsChain.db.genesisDeposits.add newDeposit m.produceDerivedData(newDeposit) template blocks*(m: Eth1Monitor): Deque[Eth1Block] = m.depositsChain.blocks -template cfg(m: Eth1Monitor): auto = - m.depositsChain.cfg - template finalizedDepositsMerkleizer(m: Eth1Monitor): auto = m.depositsChain.finalizedDepositsMerkleizer @@ -1014,7 +1010,7 @@ proc syncBlockRange(m: Eth1Monitor, eth1Block: blocksWithDeposits[^1].voteData.block_hash, depositContractState: merkleizer[].toDepositContractState) - m.db.putEth2FinalizedTo depositContractState + m.depositsChain.db.putEth2FinalizedTo depositContractState if m.genesisStateFut != nil and m.chainHasEnoughValidators: let lastIdx = m.depositsChain.blocks.len - 1 @@ -1255,17 +1251,16 @@ proc testWeb3Provider*(web3Url: Uri, when hasGenesisDetection: proc init*(T: type Eth1Monitor, - db: BeaconChainDB, cfg: RuntimeConfig, + db: BeaconChainDB, web3Urls: seq[string], - depositContractAddress: Eth1Address, depositContractDeployedAt: BlockHashOrNumber, eth1Network: Option[Eth1Network], forcePolling: bool): Future[Result[T, string]] {.async.} = doAssert web3Urls.len > 0 try: var urlIdx = 0 - let dataProviderRes = await Web3DataProvider.new(depositContractAddress, web3Urls[urlIdx]) + let dataProviderRes = await Web3DataProvider.new(cfg.DEPOSIT_CONTRACT_ADDRESS, web3Urls[urlIdx]) if dataProviderRes.isErr: return err(dataProviderRes.error) var dataProvider = dataProviderRes.get @@ -1292,7 +1287,7 @@ when hasGenesisDetection: # the Eth1Monitor will be restarted. inc urlIdx dataProvider = tryGet( - await Web3DataProvider.new(depositContractAddress, + await Web3DataProvider.new(cfg.DEPOSIT_CONTRACT_ADDRESS, web3Urls[urlIdx mod web3Urls.len])) blk.hash.asEth2Digest @@ -1300,10 +1295,9 @@ when hasGenesisDetection: eth1Block: knownStartBlockHash) var monitor = Eth1Monitor.init( - db, cfg, + db, web3Urls, - depositContractAddress, depositContractSnapshot, eth1Network, forcePolling) @@ -1362,7 +1356,7 @@ when hasGenesisDetection: return endBlock - proc waitGenesis*(m: Eth1Monitor): Future[BeaconStateRef] {.async.} = + proc waitGenesis*(m: Eth1Monitor): Future[GenesisStateRef] {.async.} = if m.genesisState.isNil: m.start() @@ -1377,4 +1371,4 @@ when hasGenesisDetection: return m.genesisState else: doAssert bnStatus == BeaconNodeStatus.Stopping - return new BeaconStateRef # cannot return nil... + return nil diff --git a/beacon_chain/nimbus_beacon_node.nim b/beacon_chain/nimbus_beacon_node.nim index f084eca4a..6a660af20 100644 --- a/beacon_chain/nimbus_beacon_node.nim +++ b/beacon_chain/nimbus_beacon_node.nim @@ -206,7 +206,7 @@ proc init(T: type BeaconNode, if genesisStateContents.len == 0 and checkpointState == nil: when hasGenesisDetection: - if genesisDepositsSnapshotContents != nil: + if genesisDepositsSnapshotContents.len > 0: fatal "A deposits snapshot cannot be provided without also providing a matching beacon state snapshot" quit 1 @@ -236,7 +236,11 @@ proc init(T: type BeaconNode, else: eth1Monitor = eth1MonitorRes.get - genesisState = waitFor eth1Monitor.waitGenesis() + let phase0Genesis = waitFor eth1Monitor.waitGenesis() + genesisState = newClone ForkedHashedBeaconState.init( + phase0.HashedBeaconState(data: phase0Genesis[], + root: hash_tree_root(phase0Genesis[]))) + if bnStatus == BeaconNodeStatus.Stopping: return nil @@ -244,9 +248,9 @@ proc init(T: type BeaconNode, tailBlock = get_initial_beacon_block(genesisState[]) notice "Eth2 genesis state detected", - genesisTime = genesisState.genesisTime, - eth1Block = genesisState.eth1_data.block_hash, - totalDeposits = genesisState.eth1_data.deposit_count + genesisTime = phase0Genesis.genesisTime, + eth1Block = phase0Genesis.eth1_data.block_hash, + totalDeposits = phase0Genesis.eth1_data.deposit_count else: fatal "No database and no genesis snapshot found: supply a genesis.ssz " & "with the network configuration, or compile the beacon node with " & diff --git a/beacon_chain/spec/beaconstate.nim b/beacon_chain/spec/beaconstate.nim index 0fe468dd8..1a41b6dd7 100644 --- a/beacon_chain/spec/beaconstate.nim +++ b/beacon_chain/spec/beaconstate.nim @@ -320,7 +320,7 @@ func get_initial_beacon_block*(state: merge.HashedBeaconState): # The genesis block is implicitly trusted let message = merge.TrustedBeaconBlock( slot: state.data.slot, - state_root: state.root,) + state_root: state.root) # parent_root, randao_reveal, eth1_data, signature, and body automatically # initialized to default values. merge.TrustedSignedBeaconBlock(