diff --git a/beacon_chain/el/el_manager.nim b/beacon_chain/el/el_manager.nim index 680a9fa59..b49542930 100644 --- a/beacon_chain/el/el_manager.nim +++ b/beacon_chain/el/el_manager.nim @@ -98,8 +98,6 @@ type Eth1BlockTimestamp* = uint64 Eth1BlockHeader = engine_api.BlockHeader - GenesisStateRef = ref phase0.BeaconState - Eth1Block* = ref object hash*: Eth2Digest number*: Eth1BlockNumber diff --git a/beacon_chain/fork_choice/fork_choice.nim b/beacon_chain/fork_choice/fork_choice.nim index bb92f584e..5a05bf410 100644 --- a/beacon_chain/fork_choice/fork_choice.nim +++ b/beacon_chain/fork_choice/fork_choice.nim @@ -174,10 +174,11 @@ func process_attestation( validator_index = validator_index, new_vote = shortLog(vote) -func process_attestation_queue(self: var ForkChoice, slot: Slot) = +proc process_attestation_queue(self: var ForkChoice, slot: Slot) = # Spec: # Attestations can only affect the fork choice of subsequent slots. # Delay consideration in the fork choice until their slot is in the past. + let startTick = Moment.now() self.queuedAttestations.keepItIf: if it.slot < slot: for validator_index in it.attesting_indices: @@ -186,6 +187,8 @@ func process_attestation_queue(self: var ForkChoice, slot: Slot) = false else: true + let endTick = Moment.now() + debug "Processed attestation queue", processDur = endTick - startTick func contains*(self: ForkChoiceBackend, block_root: Eth2Digest): bool = ## Returns `true` if a block is known to the fork choice diff --git a/beacon_chain/networking/network_metadata.nim b/beacon_chain/networking/network_metadata.nim index 3c2f116ab..a7eea86e3 100644 --- a/beacon_chain/networking/network_metadata.nim +++ b/beacon_chain/networking/network_metadata.nim @@ -281,6 +281,7 @@ elif const_preset == "mainnet": when incbinEnabled: # Nim is very inefficent at loading large constants from binary files so we # use this trick instead which saves significant amounts of compile time + {.push hint[GlobalVar]:off.} let mainnetGenesis* {.importc: "eth2_mainnet_genesis".}: ptr UncheckedArray[byte] mainnetGenesisSize* {.importc: "eth2_mainnet_genesis_size".}: int @@ -290,6 +291,7 @@ elif const_preset == "mainnet": sepoliaGenesis* {.importc: "eth2_sepolia_genesis".}: ptr UncheckedArray[byte] sepoliaGenesisSize* {.importc: "eth2_sepolia_genesis_size".}: int + {.pop.} # let `.incbin` in assembly file find the binary file through search path {.passc: "-I" & vendorDir.} @@ -465,7 +467,8 @@ when const_preset in ["mainnet", "gnosis"]: raiseAssert "The baked network metadata should use one of the name above" func bakedGenesisValidatorsRoot*(metadata: Eth2NetworkMetadata): Opt[Eth2Digest] = - if metadata.genesis.kind == BakedIn: + case metadata.genesis.kind + of BakedIn: try: let header = SSZ.decode( toOpenArray(metadata.genesis.bakedBytes, 0, sizeof(BeaconStateHeader) - 1), diff --git a/beacon_chain/spec/forks.nim b/beacon_chain/spec/forks.nim index 66b909188..ded94418c 100644 --- a/beacon_chain/spec/forks.nim +++ b/beacon_chain/spec/forks.nim @@ -357,6 +357,34 @@ template BeaconBlockType*(fork: static ConsensusFork): auto = template BeaconBlockBodyType*(fork: static ConsensusFork): auto = getSymbolFromForkModule(fork, "BeaconBlockBody") +template BeaconState*(kind: static ConsensusFork): auto = + when kind == ConsensusFork.Deneb: + typedesc[deneb.BeaconState] + elif kind == ConsensusFork.Capella: + typedesc[capella.BeaconState] + elif kind == ConsensusFork.Bellatrix: + typedesc[bellatrix.BeaconState] + elif kind == ConsensusFork.Altair: + typedesc[altair.BeaconState] + elif kind == ConsensusFork.Phase0: + typedesc[phase0.BeaconState] + else: + static: raiseAssert "Unreachable" + +template BeaconBlock*(kind: static ConsensusFork): auto = + when kind == ConsensusFork.Deneb: + typedesc[deneb.BeaconBlock] + elif kind == ConsensusFork.Capella: + typedesc[capella.BeaconBlock] + elif kind == ConsensusFork.Bellatrix: + typedesc[bellatrix.BeaconBlock] + elif kind == ConsensusFork.Altair: + typedesc[altair.BeaconBlock] + elif kind == ConsensusFork.Phase0: + typedesc[phase0.BeaconBlock] + else: + static: raiseAssert "Unreachable" + template SignedBeaconBlock*(kind: static ConsensusFork): auto = when kind == ConsensusFork.Deneb: typedesc[deneb.SignedBeaconBlock] @@ -371,6 +399,20 @@ template SignedBeaconBlock*(kind: static ConsensusFork): auto = else: static: raiseAssert "Unreachable" +template TrustedSignedBeaconBlock*(kind: static ConsensusFork): auto = + when kind == ConsensusFork.Deneb: + typedesc[deneb.TrustedSignedBeaconBlock] + elif kind == ConsensusFork.Capella: + typedesc[capella.TrustedSignedBeaconBlock] + elif kind == ConsensusFork.Bellatrix: + typedesc[bellatrix.TrustedSignedBeaconBlock] + elif kind == ConsensusFork.Altair: + typedesc[altair.TrustedSignedBeaconBlock] + elif kind == ConsensusFork.Phase0: + typedesc[phase0.TrustedSignedBeaconBlock] + else: + static: raiseAssert "Unreachable" + template ExecutionPayloadForSigning*(kind: static ConsensusFork): auto = when kind == ConsensusFork.Deneb: typedesc[deneb.ExecutionPayloadForSigning] diff --git a/beacon_chain/validators/slashing_protection_common.nim b/beacon_chain/validators/slashing_protection_common.nim index 8d9dc4147..6be04dada 100644 --- a/beacon_chain/validators/slashing_protection_common.nim +++ b/beacon_chain/validators/slashing_protection_common.nim @@ -141,6 +141,7 @@ type of BadProposalKind.DatabaseError: message*: string +{.push warning[ProveField]:off.} func `==`*(a, b: BadVote): bool = ## Comparison operator. ## Used implictily by Result when comparing the @@ -167,6 +168,7 @@ func `==`*(a, b: BadVote): bool = (a.candidateTarget == b.candidateTarget) of BadVoteKind.DatabaseError: true +{.pop.} template `==`*(a, b: PubKey0x): bool = PubKeyBytes(a) == PubKeyBytes(b) @@ -177,6 +179,7 @@ template `<`*(a, b: PubKey0x): bool = template cmp*(a, b: PubKey0x): bool = cmp(PubKeyBytes(a), PubKeyBytes(b)) +{.push warning[ProveField]:off.} func `==`*(a, b: BadProposal): bool = ## Comparison operator. ## Used implictily by Result when comparing the @@ -192,6 +195,7 @@ func `==`*(a, b: BadProposal): bool = a.candidateSlot == b.candidateSlot else: # Unreachable false +{.pop.} # Serialization # -------------------------------------------- diff --git a/beacon_chain/validators/validator_pool.nim b/beacon_chain/validators/validator_pool.nim index 9616ed74c..c51dfb62a 100644 --- a/beacon_chain/validators/validator_pool.nim +++ b/beacon_chain/validators/validator_pool.nim @@ -314,7 +314,7 @@ proc doppelgangerActivity*(validator: AttachedValidator, epoch: Epoch) = validator = shortLog(validator), activity, epoch return - if activity - epoch > 1: + if epoch - activity > 1: # We missed work in some epoch debug "Doppelganger stale activity", validator = shortLog(validator), activity, epoch diff --git a/tests/consensus_spec/test_fixture_fork_choice.nim b/tests/consensus_spec/test_fixture_fork_choice.nim index d2bb90324..c83165279 100644 --- a/tests/consensus_spec/test_fixture_fork_choice.nim +++ b/tests/consensus_spec/test_fixture_fork_choice.nim @@ -13,9 +13,6 @@ import taskpools, # Internals ../../beacon_chain/spec/[helpers, forks, state_transition_block], - ../../beacon_chain/spec/datatypes/[ - base, - phase0, altair, bellatrix], ../../beacon_chain/fork_choice/[fork_choice, fork_choice_types], ../../beacon_chain/[beacon_chain_db, beacon_clock], ../../beacon_chain/consensus_object_pools/[ @@ -70,12 +67,6 @@ type of opChecks: checks: JsonNode -from ../../beacon_chain/spec/datatypes/capella import - BeaconBlock, BeaconState, SignedBeaconBlock - -from ../../beacon_chain/spec/datatypes/deneb import - KzgBlob, KzgProof, BeaconBlock, BeaconState, SignedBeaconBlock - proc initialLoad( path: string, db: BeaconChainDB, StateType, BlockType: typedesc @@ -89,37 +80,10 @@ proc initialLoad( path/"anchor_block.ssz_snappy", SSZ, BlockType) - when BlockType is deneb.BeaconBlock: - let signedBlock = ForkedSignedBeaconBlock.init(deneb.SignedBeaconBlock( + signedBlock = ForkedSignedBeaconBlock.init(BlockType.kind.SignedBeaconBlock( message: blck, # signature: - unused as it's trusted - root: hash_tree_root(blck) - )) - elif BlockType is capella.BeaconBlock: - let signedBlock = ForkedSignedBeaconBlock.init(capella.SignedBeaconBlock( - message: blck, - # signature: - unused as it's trusted - root: hash_tree_root(blck) - )) - elif BlockType is bellatrix.BeaconBlock: - let signedBlock = ForkedSignedBeaconBlock.init(bellatrix.SignedBeaconBlock( - message: blck, - # signature: - unused as it's trusted - root: hash_tree_root(blck) - )) - elif BlockType is altair.BeaconBlock: - let signedBlock = ForkedSignedBeaconBlock.init(altair.SignedBeaconBlock( - message: blck, - # signature: - unused as it's trusted - root: hash_tree_root(blck) - )) - elif BlockType is phase0.BeaconBlock: - let signedBlock = ForkedSignedBeaconBlock.init(phase0.SignedBeaconBlock( - message: blck, - # signature: - unused as it's trusted - root: hash_tree_root(blck) - )) - else: {.error: "Unknown block fork: " & name(BlockType).} + root: hash_tree_root(blck))) ChainDAGRef.preInit(db, forkedState[]) @@ -155,54 +119,27 @@ proc loadOps(path: string, fork: ConsensusFork): seq[Operation] = elif step.hasKey"block": let filename = step["block"].getStr() doAssert step.hasKey"blobs" == step.hasKey"proofs" - case fork - of ConsensusFork.Phase0: - let blck = parseTest( - path/filename & ".ssz_snappy", - SSZ, phase0.SignedBeaconBlock - ) - doAssert not step.hasKey"blobs" - result.add Operation(kind: opOnBlock, - blck: ForkedSignedBeaconBlock.init(blck)) - of ConsensusFork.Altair: - let blck = parseTest( - path/filename & ".ssz_snappy", - SSZ, altair.SignedBeaconBlock - ) - doAssert not step.hasKey"blobs" - result.add Operation(kind: opOnBlock, - blck: ForkedSignedBeaconBlock.init(blck)) - of ConsensusFork.Bellatrix: - let blck = parseTest( - path/filename & ".ssz_snappy", - SSZ, bellatrix.SignedBeaconBlock - ) - doAssert not step.hasKey"blobs" - result.add Operation(kind: opOnBlock, - blck: ForkedSignedBeaconBlock.init(blck)) - of ConsensusFork.Capella: - let blck = parseTest( - path/filename & ".ssz_snappy", - SSZ, capella.SignedBeaconBlock - ) - doAssert not step.hasKey"blobs" - result.add Operation(kind: opOnBlock, - blck: ForkedSignedBeaconBlock.init(blck)) - of ConsensusFork.Deneb: + withConsensusFork(fork): let blck = parseTest( path/filename & ".ssz_snappy", - SSZ, deneb.SignedBeaconBlock) + SSZ, consensusFork.SignedBeaconBlock) + blobData = - if step.hasKey"blobs": - numExtraFields += 2 - Opt.some BlobData( - blobs: distinctBase(parseTest( - path/(step["blobs"].getStr()) & ".ssz_snappy", - SSZ, List[KzgBlob, Limit MAX_BLOBS_PER_BLOCK])), - proofs: step["proofs"].mapIt(KzgProof.fromHex(it.getStr()))) + when consensusFork >= ConsensusFork.Deneb: + if step.hasKey"blobs": + numExtraFields += 2 + Opt.some BlobData( + blobs: distinctBase(parseTest( + path/(step["blobs"].getStr()) & ".ssz_snappy", + SSZ, List[KzgBlob, Limit MAX_BLOBS_PER_BLOCK])), + proofs: step["proofs"].mapIt(KzgProof.fromHex(it.getStr()))) + else: + Opt.none(BlobData) else: + doAssert not step.hasKey"blobs" Opt.none(BlobData) + result.add Operation(kind: opOnBlock, blck: ForkedSignedBeaconBlock.init(blck), blobData: blobData) @@ -264,19 +201,7 @@ proc stepOnBlock( ) # 3. Add block to DAG - when signedBlock is phase0.SignedBeaconBlock: - type TrustedBlock = phase0.TrustedSignedBeaconBlock - elif signedBlock is altair.SignedBeaconBlock: - type TrustedBlock = altair.TrustedSignedBeaconBlock - elif signedBlock is bellatrix.SignedBeaconBlock: - type TrustedBlock = bellatrix.TrustedSignedBeaconBlock - elif signedBlock is capella.SignedBeaconBlock: - type TrustedBlock = capella.TrustedSignedBeaconBlock - elif signedBlock is deneb.SignedBeaconBlock: - type TrustedBlock = deneb.TrustedSignedBeaconBlock - else: - doAssert false, "Unknown TrustedSignedBeaconBlock fork" - + const consensusFork = typeof(signedBlock).kind # In normal Nimbus flow, for this (effectively) newPayload-based INVALID, it # is checked even before entering the DAG, by the block processor. Currently @@ -284,9 +209,7 @@ proc stepOnBlock( # this wouldn't be part of this check, presumably, their FC test vector step # would also have `true` validity because it'd not be known they weren't, so # adding this mock of the block processor is realistic and sufficient. - when not ( - signedBlock is phase0.SignedBeaconBlock or - signedBlock is altair.SignedBeaconBlock): + when consensusFork >= ConsensusFork.Bellatrix: let executionPayloadHash = signedBlock.message.body.execution_payload.block_hash if executionPayloadHash in invalidatedRoots: @@ -303,7 +226,7 @@ proc stepOnBlock( return err VerifierError.Invalid let blockAdded = dag.addHeadBlock(verifier, signedBlock) do ( - blckRef: BlockRef, signedBlock: TrustedBlock, + blckRef: BlockRef, signedBlock: consensusFork.TrustedSignedBeaconBlock, epochRef: EpochRef, unrealized: FinalityCheckpoints): # 4. Update fork choice if valid @@ -370,20 +293,11 @@ proc doRunTest(path: string, fork: ConsensusFork) = defer: db.close() - let stores = - case fork - of ConsensusFork.Deneb: - initialLoad(path, db, deneb.BeaconState, deneb.BeaconBlock) - of ConsensusFork.Capella: - initialLoad(path, db, capella.BeaconState, capella.BeaconBlock) - of ConsensusFork.Bellatrix: - initialLoad(path, db, bellatrix.BeaconState, bellatrix.BeaconBlock) - of ConsensusFork.Altair: - initialLoad(path, db, altair.BeaconState, altair.BeaconBlock) - of ConsensusFork.Phase0: - initialLoad(path, db, phase0.BeaconState, phase0.BeaconBlock) - let + stores = withConsensusFork(fork): + initialLoad( + path, db, consensusFork.BeaconState, consensusFork.BeaconBlock) + rng = HmacDrbgContext.new() taskpool = Taskpool.new() var verifier = BatchVerifier.init(rng, taskpool) diff --git a/vendor/eth2-networks b/vendor/eth2-networks index e930d81f7..063f826a0 160000 --- a/vendor/eth2-networks +++ b/vendor/eth2-networks @@ -1 +1 @@ -Subproject commit e930d81f7c9db816c88d1a9336be8cef858f7f4d +Subproject commit 063f826a03676c33c95a66306916f18b690d35eb diff --git a/vendor/nim-chronicles b/vendor/nim-chronicles index 71556f84a..fb4fce77a 160000 --- a/vendor/nim-chronicles +++ b/vendor/nim-chronicles @@ -1 +1 @@ -Subproject commit 71556f84af16cd275a8ae4c2390060481c6fedf7 +Subproject commit fb4fce77ace0b5558df1be21436b331c606de9a2