diff --git a/beacon_chain/consensus_object_pools/blockchain_dag.nim b/beacon_chain/consensus_object_pools/blockchain_dag.nim index 5ad19ad52..cdaf245a8 100644 --- a/beacon_chain/consensus_object_pools/blockchain_dag.nim +++ b/beacon_chain/consensus_object_pools/blockchain_dag.nim @@ -968,41 +968,42 @@ proc advanceSlots*( proc applyBlock( dag: ChainDAGRef, state: var ForkedHashedBeaconState, bid: BlockId, cache: var StateCache, info: var ForkedEpochInfo): Result[void, cstring] = - loadStateCache(dag, cache, bid, getStateField(state, slot).epoch) - case dag.cfg.consensusForkAtEpoch(bid.slot.epoch) + discard case dag.cfg.consensusForkAtEpoch(bid.slot.epoch) of ConsensusFork.Phase0: let data = getBlock(dag, bid, phase0.TrustedSignedBeaconBlock).valueOr: return err("Block load failed") - state_transition( + ? state_transition( dag.cfg, state, data, cache, info, dag.updateFlags + {slotProcessed}, noRollback) of ConsensusFork.Altair: let data = getBlock(dag, bid, altair.TrustedSignedBeaconBlock).valueOr: return err("Block load failed") - state_transition( + ? state_transition( dag.cfg, state, data, cache, info, dag.updateFlags + {slotProcessed}, noRollback) of ConsensusFork.Bellatrix: let data = getBlock(dag, bid, bellatrix.TrustedSignedBeaconBlock).valueOr: return err("Block load failed") - state_transition( + ? state_transition( dag.cfg, state, data, cache, info, dag.updateFlags + {slotProcessed}, noRollback) of ConsensusFork.Capella: let data = getBlock(dag, bid, capella.TrustedSignedBeaconBlock).valueOr: return err("Block load failed") - state_transition( + ? state_transition( dag.cfg, state, data, cache, info, dag.updateFlags + {slotProcessed}, noRollback) of ConsensusFork.Deneb: let data = getBlock(dag, bid, deneb.TrustedSignedBeaconBlock).valueOr: return err("Block load failed") - state_transition( + ? state_transition( dag.cfg, state, data, cache, info, dag.updateFlags + {slotProcessed}, noRollback) + ok() + proc init*(T: type ChainDAGRef, cfg: RuntimeConfig, db: BeaconChainDB, validatorMonitor: ref ValidatorMonitor, updateFlags: UpdateFlags, eraPath = ".", diff --git a/beacon_chain/spec/beaconstate.nim b/beacon_chain/spec/beaconstate.nim index 8ea33cfed..984b18c73 100644 --- a/beacon_chain/spec/beaconstate.nim +++ b/beacon_chain/spec/beaconstate.nim @@ -184,7 +184,7 @@ func get_proposer_reward(state: ForkyBeaconState, whistleblower_reward: Gwei): G proc slash_validator*( cfg: RuntimeConfig, state: var ForkyBeaconState, slashed_index: ValidatorIndex, cache: var StateCache): - Result[void, cstring] = + Result[Gwei, cstring] = ## Slash the validator with index ``index``. let epoch = get_current_epoch(state) ? initiate_validator_exit(cfg, state, slashed_index, cache) @@ -212,7 +212,7 @@ proc slash_validator*( # The rest doesn't make sense without there being any proposer index, so skip let proposer_index = get_beacon_proposer_index(state, cache).valueOr: debug "No beacon proposer index and probably no active validators" - return ok() + return ok(0.Gwei) # Apply proposer and whistleblower rewards let @@ -227,7 +227,7 @@ proc slash_validator*( increase_balance( state, whistleblower_index, whistleblower_reward - proposer_reward) - ok() + ok(proposer_reward) func genesis_time_from_eth1_timestamp( cfg: RuntimeConfig, eth1_timestamp: uint64): uint64 = @@ -729,7 +729,7 @@ func get_proposer_reward*(state: ForkyBeaconState, proc process_attestation*( state: var ForkyBeaconState, attestation: SomeAttestation, flags: UpdateFlags, base_reward_per_increment: Gwei, cache: var StateCache): - Result[void, cstring] = + Result[Gwei, cstring] = # In the spec, attestation validation is mixed with state mutation, so here # we've split it into two functions so that the validation logic can be # reused when looking for suitable blocks to include in attestations. @@ -761,23 +761,23 @@ proc process_attestation*( addPendingAttestation(state.current_epoch_attestations) else: addPendingAttestation(state.previous_epoch_attestations) - elif state is altair.BeaconState or state is bellatrix.BeaconState or - state is capella.BeaconState or state is deneb.BeaconState or - state is electra.BeaconState: - template updateParticipationFlags(epoch_participation: untyped) = + + const proposer_reward = 0.Gwei + else: + template updateParticipationFlags(epoch_participation: untyped): Gwei = let proposer_reward = get_proposer_reward( state, attestation, base_reward_per_increment, cache, epoch_participation) increase_balance(state, proposer_index, proposer_reward) + proposer_reward doAssert base_reward_per_increment > 0.Gwei - if attestation.data.target.epoch == get_current_epoch(state): - updateParticipationFlags(state.current_epoch_participation) - else: - updateParticipationFlags(state.previous_epoch_participation) - else: - static: doAssert false + let proposer_reward = + if attestation.data.target.epoch == get_current_epoch(state): + updateParticipationFlags(state.current_epoch_participation) + else: + updateParticipationFlags(state.previous_epoch_participation) - ok() + ok(proposer_reward) # https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.7/specs/altair/beacon-chain.md#get_next_sync_committee_indices func get_next_sync_committee_keys( diff --git a/beacon_chain/spec/state_transition.nim b/beacon_chain/spec/state_transition.nim index 67a1c0008..cbe929886 100644 --- a/beacon_chain/spec/state_transition.nim +++ b/beacon_chain/spec/state_transition.nim @@ -252,7 +252,7 @@ proc state_transition_block_aux( cfg: RuntimeConfig, state: var ForkyHashedBeaconState, signedBlock: SomeForkySignedBeaconBlock, - cache: var StateCache, flags: UpdateFlags): Result[void, cstring] = + cache: var StateCache, flags: UpdateFlags): Result[BlockRewards, cstring] = # Block updates - these happen when there's a new block being suggested # by the block proposer. Every actor in the network will update its state # according to the contents of this block - but first they will validate @@ -264,7 +264,8 @@ proc state_transition_block_aux( signature = shortLog(signedBlock.signature), blockRoot = shortLog(signedBlock.root) - ? process_block(cfg, state.data, signedBlock.message, flags, cache) + let blockRewards = + ? process_block(cfg, state.data, signedBlock.message, flags, cache) if skipStateRootValidation notin flags: ? verifyStateRoot(state.data, signedBlock.message) @@ -275,7 +276,7 @@ proc state_transition_block_aux( "see makeBeaconBlock for block production" state.root = signedBlock.message.state_root - ok() + ok(blockRewards) func noRollback*(state: var ForkedHashedBeaconState) = trace "Skipping rollback of broken state" @@ -285,7 +286,7 @@ proc state_transition_block*( state: var ForkedHashedBeaconState, signedBlock: SomeForkySignedBeaconBlock, cache: var StateCache, flags: UpdateFlags, - rollback: RollbackForkedHashedProc): Result[void, cstring] = + rollback: RollbackForkedHashedProc): Result[BlockRewards, cstring] = ## `rollback` is called if the transition fails and the given state has been ## partially changed. If a temporary state was given to `state_transition`, ## it is safe to use `noRollback` and leave it broken, else the state @@ -309,7 +310,7 @@ proc state_transition*( state: var ForkedHashedBeaconState, signedBlock: SomeForkySignedBeaconBlock, cache: var StateCache, info: var ForkedEpochInfo, flags: UpdateFlags, - rollback: RollbackForkedHashedProc): Result[void, cstring] = + rollback: RollbackForkedHashedProc): Result[BlockRewards, cstring] = ## Apply a block to the state, advancing the slot counter as necessary. The ## given state must be of a lower slot, or, in case the `slotProcessed` flag ## is set, can be the slot state of the same slot as the block (where the diff --git a/beacon_chain/spec/state_transition_block.nim b/beacon_chain/spec/state_transition_block.nim index e7bee8492..888ad61cf 100644 --- a/beacon_chain/spec/state_transition_block.nim +++ b/beacon_chain/spec/state_transition_block.nim @@ -186,11 +186,9 @@ proc check_proposer_slashing*( proc process_proposer_slashing*( cfg: RuntimeConfig, state: var ForkyBeaconState, proposer_slashing: SomeProposerSlashing, flags: UpdateFlags, - cache: var StateCache): - Result[void, cstring] = + cache: var StateCache): Result[Gwei, cstring] = let proposer_index = ? check_proposer_slashing(state, proposer_slashing, flags) - ? slash_validator(cfg, state, proposer_index, cache) - ok() + slash_validator(cfg, state, proposer_index, cache) # https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.6/specs/phase0/beacon-chain.md#is_slashable_attestation_data func is_slashable_attestation_data( @@ -253,14 +251,16 @@ proc process_attester_slashing*( attester_slashing: SomeAttesterSlashing, flags: UpdateFlags, cache: var StateCache - ): Result[void, cstring] = + ): Result[Gwei, cstring] = let slashed_attesters = ? check_attester_slashing(state, attester_slashing, flags) - for index in slashed_attesters: - ? slash_validator(cfg, state, index, cache) + var proposer_reward: Gwei - ok() + for index in slashed_attesters: + proposer_reward += ? slash_validator(cfg, state, index, cache) + + ok(proposer_reward) func findValidatorIndex*(state: ForkyBeaconState, pubkey: ValidatorPubKey): Opt[ValidatorIndex] = @@ -428,6 +428,14 @@ proc process_bls_to_execution_change*( ok() +type + # https://ethereum.github.io/beacon-APIs/?urls.primaryName=v2.5.0#/Rewards/getBlockRewards + BlockRewards* = object + attestations*: Gwei + sync_aggregate*: Gwei + proposer_slashings*: Gwei + attester_slashings*: Gwei + # https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.6/specs/phase0/beacon-chain.md#operations # https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.5/specs/capella/beacon-chain.md#modified-process_operations proc process_operations(cfg: RuntimeConfig, @@ -435,7 +443,7 @@ proc process_operations(cfg: RuntimeConfig, body: SomeForkyBeaconBlockBody, base_reward_per_increment: Gwei, flags: UpdateFlags, - cache: var StateCache): Result[void, cstring] = + cache: var StateCache): Result[BlockRewards, cstring] = # Verify that outstanding deposits are processed up to the maximum number of # deposits let @@ -446,12 +454,17 @@ proc process_operations(cfg: RuntimeConfig, body.deposits.lenu64 != req_deposits: return err("incorrect number of deposits") + var operations_rewards: BlockRewards + for op in body.proposer_slashings: - ? process_proposer_slashing(cfg, state, op, flags, cache) + operations_rewards.proposer_slashings += + ? process_proposer_slashing(cfg, state, op, flags, cache) for op in body.attester_slashings: - ? process_attester_slashing(cfg, state, op, flags, cache) + operations_rewards.attester_slashings += + ? process_attester_slashing(cfg, state, op, flags, cache) for op in body.attestations: - ? process_attestation(state, op, flags, base_reward_per_increment, cache) + operations_rewards.attestations += + ? process_attestation(state, op, flags, base_reward_per_increment, cache) for op in body.deposits: ? process_deposit(cfg, state, op, flags) for op in body.voluntary_exits: @@ -460,7 +473,7 @@ proc process_operations(cfg: RuntimeConfig, for op in body.bls_to_execution_changes: ? process_bls_to_execution_change(cfg, state, op) - ok() + ok(operations_rewards) # https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.7/specs/altair/beacon-chain.md#sync-aggregate-processing func get_participant_reward*(total_active_balance: Gwei): Gwei = @@ -484,9 +497,7 @@ proc process_sync_aggregate*( state: var (altair.BeaconState | bellatrix.BeaconState | capella.BeaconState | deneb.BeaconState | electra.BeaconState), sync_aggregate: SomeSyncAggregate, total_active_balance: Gwei, - flags: UpdateFlags, - cache: var StateCache): - Result[void, cstring] = + flags: UpdateFlags, cache: var StateCache): Result[Gwei, cstring] = if strictVerification in flags and state.slot > 1.Slot: template sync_committee_bits(): auto = sync_aggregate.sync_committee_bits let num_active_participants = countOnes(sync_committee_bits).uint64 @@ -541,7 +552,7 @@ proc process_sync_aggregate*( else: decrease_balance(state, participant_index, participant_reward) - ok() + ok(proposer_reward) # https://github.com/ethereum/consensus-specs/blob/v1.3.0/specs/bellatrix/beacon-chain.md#process_execution_payload proc process_execution_payload*( @@ -811,7 +822,7 @@ type SomePhase0Block = proc process_block*( cfg: RuntimeConfig, state: var phase0.BeaconState, blck: SomePhase0Block, flags: UpdateFlags, - cache: var StateCache): Result[void, cstring]= + cache: var StateCache): Result[BlockRewards, cstring]= ## When there's a new block, we need to verify that the block is sane and ## update the state accordingly - the state is left in an unknown state when ## block application fails (!) @@ -819,9 +830,8 @@ proc process_block*( ? process_block_header(state, blck, flags, cache) ? process_randao(state, blck.body, flags, cache) ? process_eth1_data(state, blck.body) - ? process_operations(cfg, state, blck.body, 0.Gwei, flags, cache) - ok() + ok(? process_operations(cfg, state, blck.body, 0.Gwei, flags, cache)) # https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.7/specs/altair/beacon-chain.md#block-processing # TODO workaround for https://github.com/nim-lang/Nim/issues/18095 @@ -831,7 +841,7 @@ type SomeAltairBlock = proc process_block*( cfg: RuntimeConfig, state: var altair.BeaconState, blck: SomeAltairBlock, flags: UpdateFlags, - cache: var StateCache): Result[void, cstring]= + cache: var StateCache): Result[BlockRewards, cstring]= ## When there's a new block, we need to verify that the block is sane and ## update the state accordingly - the state is left in an unknown state when ## block application fails (!) @@ -844,14 +854,13 @@ proc process_block*( total_active_balance = get_total_active_balance(state, cache) base_reward_per_increment = get_base_reward_per_increment(total_active_balance) - - ? process_operations( + var operations_rewards = ? process_operations( cfg, state, blck.body, base_reward_per_increment, flags, cache) - ? process_sync_aggregate( + operations_rewards.sync_aggregate = ? process_sync_aggregate( state, blck.body.sync_aggregate, total_active_balance, flags, cache) # [New in Altair] - ok() + ok(operations_rewards) # https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.7/specs/bellatrix/beacon-chain.md#block-processing # TODO workaround for https://github.com/nim-lang/Nim/issues/18095 @@ -860,7 +869,7 @@ type SomeBellatrixBlock = proc process_block*( cfg: RuntimeConfig, state: var bellatrix.BeaconState, blck: SomeBellatrixBlock, - flags: UpdateFlags, cache: var StateCache): Result[void, cstring]= + flags: UpdateFlags, cache: var StateCache): Result[BlockRewards, cstring]= ## When there's a new block, we need to verify that the block is sane and ## update the state accordingly - the state is left in an unknown state when ## block application fails (!) @@ -877,12 +886,12 @@ proc process_block*( total_active_balance = get_total_active_balance(state, cache) base_reward_per_increment = get_base_reward_per_increment(total_active_balance) - ? process_operations( + var operations_rewards = ? process_operations( cfg, state, blck.body, base_reward_per_increment, flags, cache) - ? process_sync_aggregate( + operations_rewards.sync_aggregate = ? process_sync_aggregate( state, blck.body.sync_aggregate, total_active_balance, flags, cache) - ok() + ok(operations_rewards) # https://github.com/ethereum/consensus-specs/blob/v1.3.0/specs/capella/beacon-chain.md#block-processing # TODO workaround for https://github.com/nim-lang/Nim/issues/18095 @@ -891,7 +900,7 @@ type SomeCapellaBlock = proc process_block*( cfg: RuntimeConfig, state: var capella.BeaconState, blck: SomeCapellaBlock, - flags: UpdateFlags, cache: var StateCache): Result[void, cstring]= + flags: UpdateFlags, cache: var StateCache): Result[BlockRewards, cstring] = ## When there's a new block, we need to verify that the block is sane and ## update the state accordingly - the state is left in an unknown state when ## block application fails (!) @@ -913,13 +922,13 @@ proc process_block*( total_active_balance = get_total_active_balance(state, cache) base_reward_per_increment = get_base_reward_per_increment(total_active_balance) - ? process_operations( + var operations_rewards = ? process_operations( cfg, state, blck.body, base_reward_per_increment, flags, cache) # [Modified in Capella] - ? process_sync_aggregate( + operations_rewards.sync_aggregate = ? process_sync_aggregate( state, blck.body.sync_aggregate, total_active_balance, flags, cache) - ok() + ok(operations_rewards) # https://github.com/ethereum/consensus-specs/blob/v1.3.0/specs/deneb/beacon-chain.md#block-processing # TODO workaround for https://github.com/nim-lang/Nim/issues/18095 @@ -928,7 +937,7 @@ type SomeDenebBlock = proc process_block*( cfg: RuntimeConfig, state: var deneb.BeaconState, blck: SomeDenebBlock, - flags: UpdateFlags, cache: var StateCache): Result[void, cstring]= + flags: UpdateFlags, cache: var StateCache): Result[BlockRewards, cstring] = ## When there's a new block, we need to verify that the block is sane and ## update the state accordingly - the state is left in an unknown state when ## block application fails (!) @@ -949,12 +958,12 @@ proc process_block*( total_active_balance = get_total_active_balance(state, cache) base_reward_per_increment = get_base_reward_per_increment(total_active_balance) - ? process_operations( + var operations_rewards = ? process_operations( cfg, state, blck.body, base_reward_per_increment, flags, cache) - ? process_sync_aggregate( + operations_rewards.sync_aggregate = ? process_sync_aggregate( state, blck.body.sync_aggregate, total_active_balance, flags, cache) - ok() + ok(operations_rewards) # https://github.com/ethereum/consensus-specs/blob/v1.3.0/specs/deneb/beacon-chain.md#block-processing # TODO workaround for https://github.com/nim-lang/Nim/issues/18095 @@ -963,7 +972,7 @@ type SomeElectraBlock = proc process_block*( cfg: RuntimeConfig, state: var electra.BeaconState, blck: SomeElectraBlock, - flags: UpdateFlags, cache: var StateCache): Result[void, cstring]= + flags: UpdateFlags, cache: var StateCache): Result[BlockRewards, cstring] = ## When there's a new block, we need to verify that the block is sane and ## update the state accordingly - the state is left in an unknown state when ## block application fails (!) @@ -984,9 +993,9 @@ proc process_block*( total_active_balance = get_total_active_balance(state, cache) base_reward_per_increment = get_base_reward_per_increment(total_active_balance) - ? process_operations( - cfg, state, blck.body, base_reward_per_increment, flags, cache) + operations_rewards = ? process_operations( + cfg, state, blck.body, base_reward_per_increment, flags, cache) ? process_sync_aggregate( state, blck.body.sync_aggregate, total_active_balance, flags, cache) - ok() + ok(operations_rewards) diff --git a/tests/consensus_spec/altair/test_fixture_operations.nim b/tests/consensus_spec/altair/test_fixture_operations.nim index c1322d59d..54f0ad8f5 100644 --- a/tests/consensus_spec/altair/test_fixture_operations.nim +++ b/tests/consensus_spec/altair/test_fixture_operations.nim @@ -9,8 +9,6 @@ {.used.} import - # Standard library - std/[sequtils, sets], # Utilities chronicles, unittest2, @@ -23,6 +21,8 @@ import ../fixtures_utils, ../os_ops, ../../helpers/debug_state +from std/sequtils import mapIt, toSeq + const OpDir = SszTestsDir/const_preset/"altair"/"operations" OpAttestationsDir = OpDir/"attestation" @@ -72,14 +72,17 @@ suite baseDescription & "Attestation " & preset(): proc applyAttestation( preState: var altair.BeaconState, attestation: Attestation): Result[void, cstring] = - var cache = StateCache() + var cache: StateCache let total_active_balance = get_total_active_balance(preState, cache) base_reward_per_increment = get_base_reward_per_increment(total_active_balance) - process_attestation( + # This returns the proposer reward for including the attestation, which + # isn't tested here. + discard ? process_attestation( preState, attestation, {}, base_reward_per_increment, cache) + ok() for path in walkTests(OpAttestationsDir): runTest[Attestation, typeof applyAttestation]( @@ -90,9 +93,10 @@ suite baseDescription & "Attester Slashing " & preset(): proc applyAttesterSlashing( preState: var altair.BeaconState, attesterSlashing: AttesterSlashing): Result[void, cstring] = - var cache = StateCache() - process_attester_slashing( - defaultRuntimeConfig, preState, attesterSlashing, {}, cache) + var cache: StateCache + doAssert (? process_attester_slashing( + defaultRuntimeConfig, preState, attesterSlashing, {}, cache)) > 0 + ok() for path in walkTests(OpAttSlashingDir): runTest[AttesterSlashing, typeof applyAttesterSlashing]( @@ -103,7 +107,7 @@ suite baseDescription & "Block Header " & preset(): func applyBlockHeader( preState: var altair.BeaconState, blck: altair.BeaconBlock): Result[void, cstring] = - var cache = StateCache() + var cache: StateCache process_block_header(preState, blck, {}, cache) for path in walkTests(OpBlockHeaderDir): @@ -124,9 +128,10 @@ suite baseDescription & "Proposer Slashing " & preset(): proc applyProposerSlashing( preState: var altair.BeaconState, proposerSlashing: ProposerSlashing): Result[void, cstring] = - var cache = StateCache() - process_proposer_slashing( - defaultRuntimeConfig, preState, proposerSlashing, {}, cache) + var cache: StateCache + doAssert (? process_proposer_slashing( + defaultRuntimeConfig, preState, proposerSlashing, {}, cache)) > 0 + ok() for path in walkTests(OpProposerSlashingDir): runTest[ProposerSlashing, typeof applyProposerSlashing]( @@ -137,10 +142,11 @@ suite baseDescription & "Sync Aggregate " & preset(): proc applySyncAggregate( preState: var altair.BeaconState, syncAggregate: SyncAggregate): Result[void, cstring] = - var cache = StateCache() - process_sync_aggregate( + var cache: StateCache + doAssert (? process_sync_aggregate( preState, syncAggregate, get_total_active_balance(preState, cache), - {}, cache) + {}, cache)) > 0 + ok() for path in walkTests(OpSyncAggregateDir): runTest[SyncAggregate, typeof applySyncAggregate]( @@ -151,7 +157,7 @@ suite baseDescription & "Voluntary Exit " & preset(): proc applyVoluntaryExit( preState: var altair.BeaconState, voluntaryExit: SignedVoluntaryExit): Result[void, cstring] = - var cache = StateCache() + var cache: StateCache process_voluntary_exit( defaultRuntimeConfig, preState, voluntaryExit, {}, cache) diff --git a/tests/consensus_spec/bellatrix/test_fixture_operations.nim b/tests/consensus_spec/bellatrix/test_fixture_operations.nim index acaefb9c3..9fa2e7f67 100644 --- a/tests/consensus_spec/bellatrix/test_fixture_operations.nim +++ b/tests/consensus_spec/bellatrix/test_fixture_operations.nim @@ -77,14 +77,17 @@ suite baseDescription & "Attestation " & preset(): proc applyAttestation( preState: var bellatrix.BeaconState, attestation: Attestation): Result[void, cstring] = - var cache = StateCache() + var cache: StateCache let total_active_balance = get_total_active_balance(preState, cache) base_reward_per_increment = get_base_reward_per_increment(total_active_balance) - process_attestation( + # This returns the proposer reward for including the attestation, which + # isn't tested here. + discard ? process_attestation( preState, attestation, {}, base_reward_per_increment, cache) + ok() for path in walkTests(OpAttestationsDir): runTest[Attestation, typeof applyAttestation]( @@ -95,9 +98,10 @@ suite baseDescription & "Attester Slashing " & preset(): proc applyAttesterSlashing( preState: var bellatrix.BeaconState, attesterSlashing: AttesterSlashing): Result[void, cstring] = - var cache = StateCache() - process_attester_slashing( - defaultRuntimeConfig, preState, attesterSlashing, {}, cache) + var cache: StateCache + doAssert (? process_attester_slashing( + defaultRuntimeConfig, preState, attesterSlashing, {}, cache)) > 0 + ok() for path in walkTests(OpAttSlashingDir): runTest[AttesterSlashing, typeof applyAttesterSlashing]( @@ -108,7 +112,7 @@ suite baseDescription & "Block Header " & preset(): func applyBlockHeader( preState: var bellatrix.BeaconState, blck: bellatrix.BeaconBlock): Result[void, cstring] = - var cache = StateCache() + var cache: StateCache process_block_header(preState, blck, {}, cache) for path in walkTests(OpBlockHeaderDir): @@ -148,9 +152,10 @@ suite baseDescription & "Proposer Slashing " & preset(): proc applyProposerSlashing( preState: var bellatrix.BeaconState, proposerSlashing: ProposerSlashing): Result[void, cstring] = - var cache = StateCache() - process_proposer_slashing( - defaultRuntimeConfig, preState, proposerSlashing, {}, cache) + var cache: StateCache + doAssert (? process_proposer_slashing( + defaultRuntimeConfig, preState, proposerSlashing, {}, cache)) > 0 + ok() for path in walkTests(OpProposerSlashingDir): runTest[ProposerSlashing, typeof applyProposerSlashing]( @@ -161,10 +166,11 @@ suite baseDescription & "Sync Aggregate " & preset(): proc applySyncAggregate( preState: var bellatrix.BeaconState, syncAggregate: SyncAggregate): Result[void, cstring] = - var cache = StateCache() - process_sync_aggregate( + var cache: StateCache + doAssert (? process_sync_aggregate( preState, syncAggregate, get_total_active_balance(preState, cache), - {}, cache) + {}, cache)) > 0 + ok() for path in walkTests(OpSyncAggregateDir): runTest[SyncAggregate, typeof applySyncAggregate]( @@ -175,7 +181,7 @@ suite baseDescription & "Voluntary Exit " & preset(): proc applyVoluntaryExit( preState: var bellatrix.BeaconState, voluntaryExit: SignedVoluntaryExit): Result[void, cstring] = - var cache = StateCache() + var cache: StateCache process_voluntary_exit( defaultRuntimeConfig, preState, voluntaryExit, {}, cache) diff --git a/tests/consensus_spec/capella/test_fixture_operations.nim b/tests/consensus_spec/capella/test_fixture_operations.nim index 9428c4f05..28e038907 100644 --- a/tests/consensus_spec/capella/test_fixture_operations.nim +++ b/tests/consensus_spec/capella/test_fixture_operations.nim @@ -81,14 +81,17 @@ suite baseDescription & "Attestation " & preset(): proc applyAttestation( preState: var capella.BeaconState, attestation: Attestation): Result[void, cstring] = - var cache = StateCache() + var cache: StateCache let total_active_balance = get_total_active_balance(preState, cache) base_reward_per_increment = get_base_reward_per_increment(total_active_balance) - process_attestation( + # This returns the proposer reward for including the attestation, which + # isn't tested here. + discard ? process_attestation( preState, attestation, {}, base_reward_per_increment, cache) + ok() for path in walkTests(OpAttestationsDir): runTest[Attestation, typeof applyAttestation]( @@ -99,9 +102,10 @@ suite baseDescription & "Attester Slashing " & preset(): proc applyAttesterSlashing( preState: var capella.BeaconState, attesterSlashing: AttesterSlashing): Result[void, cstring] = - var cache = StateCache() - process_attester_slashing( - defaultRuntimeConfig, preState, attesterSlashing, {}, cache) + var cache: StateCache + doAssert (? process_attester_slashing( + defaultRuntimeConfig, preState, attesterSlashing, {}, cache)) > 0 + ok() for path in walkTests(OpAttSlashingDir): runTest[AttesterSlashing, typeof applyAttesterSlashing]( @@ -112,7 +116,7 @@ suite baseDescription & "Block Header " & preset(): func applyBlockHeader( preState: var capella.BeaconState, blck: capella.BeaconBlock): Result[void, cstring] = - var cache = StateCache() + var cache: StateCache process_block_header(preState, blck, {}, cache) for path in walkTests(OpBlockHeaderDir): @@ -165,9 +169,10 @@ suite baseDescription & "Proposer Slashing " & preset(): proc applyProposerSlashing( preState: var capella.BeaconState, proposerSlashing: ProposerSlashing): Result[void, cstring] = - var cache = StateCache() - process_proposer_slashing( - defaultRuntimeConfig, preState, proposerSlashing, {}, cache) + var cache: StateCache + doAssert (? process_proposer_slashing( + defaultRuntimeConfig, preState, proposerSlashing, {}, cache)) > 0 + ok() for path in walkTests(OpProposerSlashingDir): runTest[ProposerSlashing, typeof applyProposerSlashing]( @@ -178,10 +183,11 @@ suite baseDescription & "Sync Aggregate " & preset(): proc applySyncAggregate( preState: var capella.BeaconState, syncAggregate: SyncAggregate): Result[void, cstring] = - var cache = StateCache() - process_sync_aggregate( + var cache: StateCache + doAssert (? process_sync_aggregate( preState, syncAggregate, get_total_active_balance(preState, cache), - {}, cache) + {}, cache)) > 0 + ok() for path in walkTests(OpSyncAggregateDir): runTest[SyncAggregate, typeof applySyncAggregate]( @@ -192,7 +198,7 @@ suite baseDescription & "Voluntary Exit " & preset(): proc applyVoluntaryExit( preState: var capella.BeaconState, voluntaryExit: SignedVoluntaryExit): Result[void, cstring] = - var cache = StateCache() + var cache: StateCache process_voluntary_exit( defaultRuntimeConfig, preState, voluntaryExit, {}, cache) diff --git a/tests/consensus_spec/deneb/test_fixture_operations.nim b/tests/consensus_spec/deneb/test_fixture_operations.nim index 3bcaa41c7..7c7e088c9 100644 --- a/tests/consensus_spec/deneb/test_fixture_operations.nim +++ b/tests/consensus_spec/deneb/test_fixture_operations.nim @@ -81,14 +81,17 @@ suite baseDescription & "Attestation " & preset(): proc applyAttestation( preState: var deneb.BeaconState, attestation: Attestation): Result[void, cstring] = - var cache = StateCache() + var cache: StateCache let total_active_balance = get_total_active_balance(preState, cache) base_reward_per_increment = get_base_reward_per_increment(total_active_balance) - process_attestation( + # This returns the proposer reward for including the attestation, which + # isn't tested here. + discard ? process_attestation( preState, attestation, {}, base_reward_per_increment, cache) + ok() for path in walkTests(OpAttestationsDir): runTest[Attestation, typeof applyAttestation]( @@ -99,9 +102,10 @@ suite baseDescription & "Attester Slashing " & preset(): proc applyAttesterSlashing( preState: var deneb.BeaconState, attesterSlashing: AttesterSlashing): Result[void, cstring] = - var cache = StateCache() - process_attester_slashing( - defaultRuntimeConfig, preState, attesterSlashing, {}, cache) + var cache: StateCache + doAssert (? process_attester_slashing( + defaultRuntimeConfig, preState, attesterSlashing, {}, cache)) > 0 + ok() for path in walkTests(OpAttSlashingDir): runTest[AttesterSlashing, typeof applyAttesterSlashing]( @@ -112,7 +116,7 @@ suite baseDescription & "Block Header " & preset(): func applyBlockHeader( preState: var deneb.BeaconState, blck: deneb.BeaconBlock): Result[void, cstring] = - var cache = StateCache() + var cache: StateCache process_block_header(preState, blck, {}, cache) for path in walkTests(OpBlockHeaderDir): @@ -167,9 +171,10 @@ suite baseDescription & "Proposer Slashing " & preset(): proc applyProposerSlashing( preState: var deneb.BeaconState, proposerSlashing: ProposerSlashing): Result[void, cstring] = - var cache = StateCache() - process_proposer_slashing( - defaultRuntimeConfig, preState, proposerSlashing, {}, cache) + var cache: StateCache + doAssert (? process_proposer_slashing( + defaultRuntimeConfig, preState, proposerSlashing, {}, cache)) > 0 + ok() for path in walkTests(OpProposerSlashingDir): runTest[ProposerSlashing, typeof applyProposerSlashing]( @@ -180,10 +185,11 @@ suite baseDescription & "Sync Aggregate " & preset(): proc applySyncAggregate( preState: var deneb.BeaconState, syncAggregate: SyncAggregate): Result[void, cstring] = - var cache = StateCache() - process_sync_aggregate( + var cache: StateCache + doAssert (? process_sync_aggregate( preState, syncAggregate, get_total_active_balance(preState, cache), - {}, cache) + {}, cache)) > 0 + ok() for path in walkTests(OpSyncAggregateDir): runTest[SyncAggregate, typeof applySyncAggregate]( @@ -194,7 +200,7 @@ suite baseDescription & "Voluntary Exit " & preset(): proc applyVoluntaryExit( preState: var deneb.BeaconState, voluntaryExit: SignedVoluntaryExit): Result[void, cstring] = - var cache = StateCache() + var cache: StateCache process_voluntary_exit( defaultRuntimeConfig, preState, voluntaryExit, {}, cache) diff --git a/tests/consensus_spec/phase0/test_fixture_operations.nim b/tests/consensus_spec/phase0/test_fixture_operations.nim index d6fe1dc38..88683b35e 100644 --- a/tests/consensus_spec/phase0/test_fixture_operations.nim +++ b/tests/consensus_spec/phase0/test_fixture_operations.nim @@ -9,8 +9,6 @@ {.used.} import - # Standard library - std/[sequtils, sets], # Utilities chronicles, unittest2, @@ -23,6 +21,8 @@ import ../fixtures_utils, ../os_ops, ../../helpers/debug_state +from std/sequtils import mapIt, toSeq + const OpDir = SszTestsDir/const_preset/"phase0"/"operations" OpAttestationsDir = OpDir/"attestation" @@ -70,8 +70,10 @@ suite baseDescription & "Attestation " & preset(): proc applyAttestation( preState: var phase0.BeaconState, attestation: Attestation): Result[void, cstring] = - var cache = StateCache() - process_attestation(preState, attestation, {}, 0.Gwei, cache) + var cache: StateCache + doAssert (? process_attestation( + preState, attestation, {}, 0.Gwei, cache)) == 0 + ok() for path in walkTests(OpAttestationsDir): runTest[Attestation, typeof applyAttestation]( @@ -82,9 +84,10 @@ suite baseDescription & "Attester Slashing " & preset(): proc applyAttesterSlashing( preState: var phase0.BeaconState, attesterSlashing: AttesterSlashing): Result[void, cstring] = - var cache = StateCache() - process_attester_slashing( - defaultRuntimeConfig, preState, attesterSlashing, {}, cache) + var cache: StateCache + doAssert (? process_attester_slashing( + defaultRuntimeConfig, preState, attesterSlashing, {}, cache)) > 0 + ok() for path in walkTests(OpAttSlashingDir): runTest[AttesterSlashing, typeof applyAttesterSlashing]( @@ -95,7 +98,7 @@ suite baseDescription & "Block Header " & preset(): func applyBlockHeader( preState: var phase0.BeaconState, blck: phase0.BeaconBlock): Result[void, cstring] = - var cache = StateCache() + var cache: StateCache process_block_header(preState, blck, {}, cache) for path in walkTests(OpBlockHeaderDir): @@ -117,9 +120,10 @@ suite baseDescription & "Proposer Slashing " & preset(): proc applyProposerSlashing( preState: var phase0.BeaconState, proposerSlashing: ProposerSlashing): Result[void, cstring] = - var cache = StateCache() - process_proposer_slashing( - defaultRuntimeConfig, preState, proposerSlashing, {}, cache) + var cache: StateCache + doAssert (? process_proposer_slashing( + defaultRuntimeConfig, preState, proposerSlashing, {}, cache)) > 0 + ok() for path in walkTests(OpProposerSlashingDir): runTest[ProposerSlashing, typeof applyProposerSlashing]( @@ -130,7 +134,7 @@ suite baseDescription & "Voluntary Exit " & preset(): proc applyVoluntaryExit( preState: var phase0.BeaconState, voluntaryExit: SignedVoluntaryExit): Result[void, cstring] = - var cache = StateCache() + var cache: StateCache process_voluntary_exit( defaultRuntimeConfig, preState, voluntaryExit, {}, cache) diff --git a/tests/consensus_spec/test_fixture_sanity_blocks.nim b/tests/consensus_spec/test_fixture_sanity_blocks.nim index 56a101b56..193b6075c 100644 --- a/tests/consensus_spec/test_fixture_sanity_blocks.nim +++ b/tests/consensus_spec/test_fixture_sanity_blocks.nim @@ -47,7 +47,9 @@ proc runTest( SSZ, consensusFork.SignedBeaconBlock) if hasPostState: - state_transition( + # The return value is the block rewards, which aren't tested here; + # the .expect() already handles the the validaty check. + discard state_transition( defaultRuntimeConfig, fhPreState[], blck, cache, info, flags = {}, noRollback).expect("should apply block") else: diff --git a/tests/consensus_spec/test_fixture_transition.nim b/tests/consensus_spec/test_fixture_transition.nim index 53cbb6652..74230bed7 100644 --- a/tests/consensus_spec/test_fixture_transition.nim +++ b/tests/consensus_spec/test_fixture_transition.nim @@ -60,7 +60,9 @@ proc runTest( cfg, fhPreState[], blck, cache, info, flags = {skipStateRootValidation}, noRollback) - res.expect("no failure when applying block " & $i) + # The return value is the block rewards, which aren't tested here; + # the .expect() already handles the the validaty check. + discard res.expect("no failure when applying block " & $i) else: let blck = parseTest( @@ -69,7 +71,9 @@ proc runTest( cfg, fhPreState[], blck, cache, info, flags = {skipStateRootValidation}, noRollback) - res.expect("no failure when applying block " & $i) + # The return value is the block rewards, which aren't tested here; + # the .expect() already handles the the validaty check. + discard res.expect("no failure when applying block " & $i) let postState = newClone( parseTest(testPath/"post.ssz_snappy", SSZ, PostBeaconState))