From 725b120bc372c3e3b0c4431ad018d6556ee66753 Mon Sep 17 00:00:00 2001 From: Eugene Kabanov Date: Tue, 28 Jun 2022 13:21:16 +0300 Subject: [PATCH] Fix proper timing check for bellatrix epoch. (#3807) --- .../block_pools_types.nim | 29 ++++---- .../consensus_object_pools/blockchain_dag.nim | 15 +--- beacon_chain/nimbus_beacon_node.nim | 71 +++++++++++++------ beacon_chain/rpc/rest_node_api.nim | 2 +- beacon_chain/rpc/rest_utils.nim | 4 +- beacon_chain/rpc/rest_validator_api.nim | 10 ++- 6 files changed, 79 insertions(+), 52 deletions(-) diff --git a/beacon_chain/consensus_object_pools/block_pools_types.nim b/beacon_chain/consensus_object_pools/block_pools_types.nim index 7f271f3a3..f83b69eb2 100644 --- a/beacon_chain/consensus_object_pools/block_pools_types.nim +++ b/beacon_chain/consensus_object_pools/block_pools_types.nim @@ -335,6 +335,15 @@ func shortLog*(v: EpochKey): string = template setFinalizationCb*(dag: ChainDAGRef, cb: OnFinalizedCallback) = dag.onFinHappened = cb +template setBlockCb*(dag: ChainDAGRef, cb: OnBlockCallback) = + dag.onBlockAdded = cb + +template setHeadCb*(dag: ChainDAGRef, cb: OnHeadCallback) = + dag.onHeadChanged = cb + +template setReorgCb*(dag: ChainDAGRef, cb: OnReorgCallback) = + dag.onReorgHappened = cb + func shortLog*(v: EpochRef): string = # epoch:root when logging epoch, root:slot when logging slot! if v.isNil(): @@ -363,40 +372,36 @@ func blockRef*(key: KeyedBlockRef): BlockRef = func init*(t: typedesc[HeadChangeInfoObject], slot: Slot, blockRoot: Eth2Digest, stateRoot: Eth2Digest, epochTransition: bool, - previousDutyDepRoot: Eth2Digest, currentDutyDepRoot: Eth2Digest, - optimistic: Option[bool]): HeadChangeInfoObject = + previousDutyDepRoot: Eth2Digest, + currentDutyDepRoot: Eth2Digest): HeadChangeInfoObject = HeadChangeInfoObject( slot: slot, block_root: blockRoot, state_root: stateRoot, epoch_transition: epochTransition, previous_duty_dependent_root: previousDutyDepRoot, - current_duty_dependent_root: currentDutyDepRoot, - optimistic: optimistic + current_duty_dependent_root: currentDutyDepRoot ) func init*(t: typedesc[ReorgInfoObject], slot: Slot, depth: uint64, oldHeadBlockRoot: Eth2Digest, newHeadBlockRoot: Eth2Digest, - oldHeadStateRoot: Eth2Digest, newHeadStateRoot: Eth2Digest, - optimistic: Option[bool]): ReorgInfoObject = + oldHeadStateRoot: Eth2Digest, + newHeadStateRoot: Eth2Digest): ReorgInfoObject = ReorgInfoObject( slot: slot, depth: depth, old_head_block: oldHeadBlockRoot, new_head_block: newHeadBlockRoot, old_head_state: oldHeadStateRoot, - new_head_state: newHeadStateRoot, - optimistic: optimistic + new_head_state: newHeadStateRoot ) func init*(t: typedesc[FinalizationInfoObject], blockRoot: Eth2Digest, - stateRoot: Eth2Digest, epoch: Epoch, - optimistic: Option[bool]): FinalizationInfoObject = + stateRoot: Eth2Digest, epoch: Epoch): FinalizationInfoObject = FinalizationInfoObject( block_root: blockRoot, state_root: stateRoot, - epoch: epoch, - optimistic: optimistic + epoch: epoch ) func init*(t: typedesc[EventBeaconBlockObject], diff --git a/beacon_chain/consensus_object_pools/blockchain_dag.nim b/beacon_chain/consensus_object_pools/blockchain_dag.nim index 2caa301be..7bc4b46f4 100644 --- a/beacon_chain/consensus_object_pools/blockchain_dag.nim +++ b/beacon_chain/consensus_object_pools/blockchain_dag.nim @@ -1624,13 +1624,10 @@ proc updateHead*( if not(isNil(dag.onReorgHappened)): let # TODO (cheatfate): Proper implementation required - optimistic = - if dag.getHeadStateMergeComplete(): some(false) else: none[bool]() data = ReorgInfoObject.init(dag.head.slot, uint64(ancestorDepth), lastHead.root, newHead.root, lastHeadStateRoot, - getStateRoot(dag.headState), - optimistic) + getStateRoot(dag.headState)) dag.onReorgHappened(data) # A reasonable criterion for "reorganizations of the chain" @@ -1652,12 +1649,10 @@ proc updateHead*( prevDepRoot = withState(dag.headState): state.attester_dependent_root epochTransition = (finalizedHead != dag.finalizedHead) # TODO (cheatfate): Proper implementation required - optimistic = - if dag.getHeadStateMergeComplete(): some(false) else: none[bool]() data = HeadChangeInfoObject.init(dag.head.slot, dag.head.root, getStateRoot(dag.headState), epochTransition, depRoot, - prevDepRoot, optimistic) + prevDepRoot) dag.onHeadChanged(data) withState(dag.headState): @@ -1717,12 +1712,8 @@ proc updateHead*( else: Eth2Digest() # The thing that finalized was >8192 blocks old? # TODO (cheatfate): Proper implementation required - let optimistic = - if dag.getHeadStateMergeComplete(): some(false) else: none[bool]() - let data = FinalizationInfoObject.init( - dag.finalizedHead.blck.root, stateRoot, dag.finalizedHead.slot.epoch, - optimistic) + dag.finalizedHead.blck.root, stateRoot, dag.finalizedHead.slot.epoch) dag.onFinHappened(dag, data) proc isInitialized*(T: type ChainDAGRef, db: BeaconChainDB): Result[void, cstring] = diff --git a/beacon_chain/nimbus_beacon_node.nim b/beacon_chain/nimbus_beacon_node.nim index 1eac467b1..47b9959b4 100644 --- a/beacon_chain/nimbus_beacon_node.nim +++ b/beacon_chain/nimbus_beacon_node.nim @@ -152,21 +152,8 @@ proc loadChainDag( eventBus: EventBus, validatorMonitor: ref ValidatorMonitor, networkGenesisValidatorsRoot: Option[Eth2Digest]): ChainDAGRef = - var dag: ChainDAGRef info "Loading block DAG from database", path = config.databaseDir - proc onBlockAdded(data: ForkedTrustedSignedBeaconBlock) = - # TODO (cheatfate): Proper implementation required - let optimistic = - if isNil(dag): - none[bool]() - else: - if dag.getHeadStateMergeComplete(): some(false) else: none[bool]() - eventBus.blocksQueue.emit(EventBeaconBlockObject.init(data, optimistic)) - proc onHeadChanged(data: HeadChangeInfoObject) = - eventBus.headQueue.emit(data) - proc onChainReorg(data: ReorgInfoObject) = - eventBus.reorgQueue.emit(data) proc onLightClientFinalityUpdate(data: altair.LightClientFinalityUpdate) = eventBus.finUpdateQueue.emit(data) proc onLightClientOptimisticUpdate(data: altair.LightClientOptimisticUpdate) = @@ -183,16 +170,14 @@ proc loadChainDag( if config.lightClientDataServe.get: onLightClientOptimisticUpdate else: nil - dag = ChainDAGRef.init( - cfg, db, validatorMonitor, chainDagFlags, config.eraDir, - onBlockAdded, onHeadChanged, onChainReorg, - onLCFinalityUpdateCb = onLightClientFinalityUpdateCb, - onLCOptimisticUpdateCb = onLightClientOptimisticUpdateCb, - lightClientDataServe = config.lightClientDataServe.get, - lightClientDataImportMode = config.lightClientDataImportMode.get, - vanityLogs = getPandas(detectTTY(config.logStdout))) + dag = ChainDAGRef.init( + cfg, db, validatorMonitor, chainDagFlags, config.eraDir, + onLCFinalityUpdateCb = onLightClientFinalityUpdateCb, + onLCOptimisticUpdateCb = onLightClientOptimisticUpdateCb, + lightClientDataServe = config.lightClientDataServe.get, + lightClientDataImportMode = config.lightClientDataImportMode.get, + vanityLogs = getPandas(detectTTY(config.logStdout))) - let databaseGenesisValidatorsRoot = getStateField(dag.headState, genesis_validators_root) @@ -235,6 +220,35 @@ proc initFullNode( node.eventBus.contribQueue.emit(data) proc onVoluntaryExitAdded(data: SignedVoluntaryExit) = node.eventBus.exitQueue.emit(data) + proc onBlockAdded(data: ForkedTrustedSignedBeaconBlock) = + # TODO (cheatfate): Proper implementation required + let optimistic = + if node.currentSlot().epoch() >= dag.cfg.BELLATRIX_FORK_EPOCH: + some(false) + else: + none[bool]() + node.eventBus.blocksQueue.emit( + EventBeaconBlockObject.init(data, optimistic)) + proc onHeadChanged(data: HeadChangeInfoObject) = + # TODO (cheatfate): Proper implementation required + let eventData = + if node.currentSlot().epoch() >= dag.cfg.BELLATRIX_FORK_EPOCH: + var res = data + res.optimistic = some(false) + res + else: + data + node.eventBus.headQueue.emit(eventData) + proc onChainReorg(data: ReorgInfoObject) = + # TODO (cheatfate): Proper implementation required + let eventData = + if node.currentSlot().epoch() >= dag.cfg.BELLATRIX_FORK_EPOCH: + var res = data + res.optimistic = some(false) + res + else: + data + node.eventBus.reorgQueue.emit(eventData) proc makeOnFinalizationCb( # This `nimcall` functions helps for keeping track of what # needs to be captured by the onFinalization closure. @@ -248,7 +262,15 @@ proc initFullNode( finalizedEpochRef.eth1_data, finalizedEpochRef.eth1_deposit_index) node.updateLightClientFromDag() - eventBus.finalQueue.emit(data) + # TODO (cheatfate): Proper implementation required + let eventData = + if node.currentSlot().epoch() >= dag.cfg.BELLATRIX_FORK_EPOCH: + var res = data + res.optimistic = some(false) + res + else: + data + eventBus.finalQueue.emit(eventData) func getLocalHeadSlot(): Slot = dag.head.slot @@ -305,6 +327,9 @@ proc initFullNode( getFrontfillSlot, dag.backfill.slot, blockVerifier, maxHeadAge = 0) dag.setFinalizationCb makeOnFinalizationCb(node.eventBus, node.eth1Monitor) + dag.setBlockCb(onBlockAdded) + dag.setHeadCb(onHeadChanged) + dag.setReorgCb(onChainReorg) node.dag = dag node.quarantine = quarantine diff --git a/beacon_chain/rpc/rest_node_api.nim b/beacon_chain/rpc/rest_node_api.nim index c6ea0a2da..2e76882ea 100644 --- a/beacon_chain/rpc/rest_node_api.nim +++ b/beacon_chain/rpc/rest_node_api.nim @@ -263,7 +263,7 @@ proc installNodeApiHandlers*(router: var RestRouter, node: BeaconNode) = else: node.syncManager.inProgress isOptimistic = - if node.dag.getHeadStateMergeComplete(): + if node.currentSlot().epoch() >= node.dag.cfg.BELLATRIX_FORK_EPOCH: # TODO (cheatfate): Proper implementation required some(false) else: diff --git a/beacon_chain/rpc/rest_utils.nim b/beacon_chain/rpc/rest_utils.nim index 3cf616510..cac09c310 100644 --- a/beacon_chain/rpc/rest_utils.nim +++ b/beacon_chain/rpc/rest_utils.nim @@ -274,7 +274,7 @@ proc getRouter*(allowedOrigin: Option[string]): RestRouter = proc getStateOptimistic*(node: BeaconNode, state: ForkedHashedBeaconState): Option[bool] = - if node.dag.getHeadStateMergeComplete(): + if node.currentSlot().epoch() >= node.dag.cfg.BELLATRIX_FORK_EPOCH: case state.kind of BeaconStateFork.Phase0, BeaconStateFork.Altair: some[bool](false) @@ -287,7 +287,7 @@ proc getStateOptimistic*(node: BeaconNode, proc getBlockOptimistic*(node: BeaconNode, blck: ForkedTrustedSignedBeaconBlock | ForkedSignedBeaconBlock): Option[bool] = - if node.dag.getHeadStateMergeComplete(): + if node.currentSlot().epoch() >= node.dag.cfg.BELLATRIX_FORK_EPOCH: case blck.kind of BeaconBlockFork.Phase0, BeaconBlockFork.Altair: some[bool](false) diff --git a/beacon_chain/rpc/rest_validator_api.nim b/beacon_chain/rpc/rest_validator_api.nim index f089a8b0c..b0030ee29 100644 --- a/beacon_chain/rpc/rest_validator_api.nim +++ b/beacon_chain/rpc/rest_validator_api.nim @@ -99,7 +99,10 @@ proc installValidatorApiHandlers*(router: var RestRouter, node: BeaconNode) = # TODO (cheatfate): Proper implementation required let optimistic = - if node.dag.getHeadStateMergeComplete(): some(false) else: none[bool]() + if node.currentSlot().epoch() >= node.dag.cfg.BELLATRIX_FORK_EPOCH: + some(false) + else: + none[bool]() return RestApiResponse.jsonResponseWRoot( duties, epochRef.attester_dependent_root, optimistic) @@ -150,7 +153,10 @@ proc installValidatorApiHandlers*(router: var RestRouter, node: BeaconNode) = # TODO (cheatfate): Proper implementation required let optimistic = - if node.dag.getHeadStateMergeComplete(): some(false) else: none[bool]() + if node.currentSlot().epoch() >= node.dag.cfg.BELLATRIX_FORK_EPOCH: + some(false) + else: + none[bool]() return RestApiResponse.jsonResponseWRoot( duties, epochRef.proposer_dependent_root, optimistic)