mirror of
https://github.com/status-im/nimbus-eth2.git
synced 2025-02-12 22:46:59 +00:00
Adopt new vision to block rewards.
This commit is contained in:
parent
cab9618f53
commit
39292de2bb
@ -48,7 +48,7 @@ import
|
||||
beaconstate, eth2_merkleization, forks, helpers, signatures,
|
||||
state_transition_block, state_transition_epoch, validator]
|
||||
|
||||
export results, extras
|
||||
export results, extras, state_transition_block
|
||||
|
||||
logScope:
|
||||
topics = "state_transition"
|
||||
@ -382,7 +382,7 @@ func partialBeaconBlock*(
|
||||
|
||||
res
|
||||
|
||||
proc makeBeaconBlock*(
|
||||
proc makeBeaconBlockWithRewards*(
|
||||
cfg: RuntimeConfig,
|
||||
state: var ForkedHashedBeaconState,
|
||||
proposer_index: ValidatorIndex,
|
||||
@ -403,13 +403,15 @@ proc makeBeaconBlock*(
|
||||
transactions_root: Opt[Eth2Digest],
|
||||
execution_payload_root: Opt[Eth2Digest],
|
||||
kzg_commitments: Opt[KzgCommitments]):
|
||||
Result[ForkedBeaconBlock, cstring] =
|
||||
Result[tuple[blck: ForkedBeaconBlock, rewards: BlockRewards], cstring] =
|
||||
## Create a block for the given state. The latest block applied to it will
|
||||
## be used for the parent_root value, and the slot will be take from
|
||||
## state.slot meaning process_slots must be called up to the slot for which
|
||||
## the block is to be created.
|
||||
|
||||
template makeBeaconBlock(kind: untyped): Result[ForkedBeaconBlock, cstring] =
|
||||
template makeBeaconBlock(
|
||||
kind: untyped
|
||||
): Result[tuple[blck: ForkedBeaconBlock, rewards: BlockRewards], cstring] =
|
||||
# To create a block, we'll first apply a partial block to the state, skipping
|
||||
# some validations.
|
||||
|
||||
@ -458,11 +460,10 @@ proc makeBeaconBlock*(
|
||||
else:
|
||||
static: raiseAssert "Unreachable"
|
||||
|
||||
|
||||
state.`kind Data`.root = hash_tree_root(state.`kind Data`.data)
|
||||
blck.`kind Data`.state_root = state.`kind Data`.root
|
||||
|
||||
ok(blck)
|
||||
ok((blck: blck, rewards: res.get))
|
||||
|
||||
const payloadFork = typeof(executionPayload).kind
|
||||
when payloadFork == ConsensusFork.Bellatrix:
|
||||
@ -482,6 +483,28 @@ proc makeBeaconBlock*(
|
||||
else:
|
||||
{.error: "Unsupported fork".}
|
||||
|
||||
proc makeBeaconBlock*(
|
||||
cfg: RuntimeConfig, state: var ForkedHashedBeaconState,
|
||||
proposer_index: ValidatorIndex, randao_reveal: ValidatorSig,
|
||||
eth1_data: Eth1Data, graffiti: GraffitiBytes,
|
||||
attestations: seq[Attestation], deposits: seq[Deposit],
|
||||
validator_changes: BeaconBlockValidatorChanges,
|
||||
sync_aggregate: SyncAggregate,
|
||||
executionPayload: ForkyExecutionPayloadForSigning,
|
||||
rollback: RollbackForkedHashedProc, cache: var StateCache,
|
||||
verificationFlags: UpdateFlags,
|
||||
transactions_root: Opt[Eth2Digest],
|
||||
execution_payload_root: Opt[Eth2Digest],
|
||||
kzg_commitments: Opt[KzgCommitments]):
|
||||
Result[ForkedBeaconBlock, cstring] =
|
||||
let blockAndRewards =
|
||||
? makeBeaconBlockWithRewards(
|
||||
cfg, state, proposer_index, randao_reveal, eth1_data, graffiti,
|
||||
attestations, deposits, validator_changes, sync_aggregate,
|
||||
executionPayload, rollback, cache, verificationFlags, transactions_root,
|
||||
execution_payload_root, kzg_commitments)
|
||||
ok(blockAndRewards.blck)
|
||||
|
||||
proc makeBeaconBlock*(
|
||||
cfg: RuntimeConfig, state: var ForkedHashedBeaconState,
|
||||
proposer_index: ValidatorIndex, randao_reveal: ValidatorSig,
|
||||
|
@ -107,6 +107,10 @@ proc getValidator*(validators: auto,
|
||||
Opt.some ValidatorAndIndex(index: ValidatorIndex(idx),
|
||||
validator: validators[idx])
|
||||
|
||||
func sum*(r: BlockRewards): Gwei =
|
||||
Gwei(uint64(r.attestations) + uint64(r.sync_aggregate) +
|
||||
uint64(r.proposer_slashings) + uint64(r.attester_slashings))
|
||||
|
||||
proc addValidatorsFromWeb3Signer(
|
||||
node: BeaconNode, web3signerUrl: Web3SignerUrl, epoch: Epoch)
|
||||
{.async: (raises: [CancelledError]).} =
|
||||
@ -497,7 +501,7 @@ proc makeBeaconBlockForHeadAndSlot*(
|
||||
slot, validator_index
|
||||
return err("Unable to get execution payload")
|
||||
|
||||
let blck = makeBeaconBlock(
|
||||
let res = makeBeaconBlockWithRewards(
|
||||
node.dag.cfg,
|
||||
state[],
|
||||
validator_index,
|
||||
@ -527,17 +531,15 @@ proc makeBeaconBlockForHeadAndSlot*(
|
||||
when payload is deneb.ExecutionPayloadForSigning:
|
||||
blobsBundleOpt = Opt.some(payload.blobsBundle)
|
||||
|
||||
let reward = collectBlockRewards(state[], blck.get())
|
||||
|
||||
return if blck.isOk:
|
||||
return if res.isOk:
|
||||
ok(EngineBid(
|
||||
blck: blck.get(),
|
||||
blck: res.get().blck,
|
||||
executionPayloadValue: payload.blockValue,
|
||||
consensusBlockValue: reward.get,
|
||||
consensusBlockValue: res.get().rewards.sum.u256,
|
||||
blobsBundleOpt: blobsBundleOpt
|
||||
))
|
||||
else:
|
||||
err(blck.error)
|
||||
err(res.error)
|
||||
|
||||
proc makeBeaconBlockForHeadAndSlot*(
|
||||
PayloadType: type ForkyExecutionPayloadForSigning, node: BeaconNode, randao_reveal: ValidatorSig,
|
||||
@ -1990,10 +1992,6 @@ proc registerDuties*(node: BeaconNode, wallSlot: Slot) {.async: (raises: [Cancel
|
||||
node.consensusManager[].actionTracker.registerDuty(
|
||||
slot, subnet_id, validator_index, isAggregator)
|
||||
|
||||
proc getConsensusBlockValue(node: BeaconNode,
|
||||
blck: RewardingBlock): Opt[UInt256] =
|
||||
collectBlockRewards(node.dag.headState, blck)
|
||||
|
||||
proc makeMaybeBlindedBeaconBlockForHeadAndSlotImpl[ResultType](
|
||||
node: BeaconNode, consensusFork: static ConsensusFork,
|
||||
randao_reveal: ValidatorSig, graffiti: GraffitiBytes,
|
||||
|
@ -1,135 +0,0 @@
|
||||
# beacon_chain
|
||||
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
||||
# Licensed and distributed under either of
|
||||
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
||||
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
||||
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
||||
|
||||
{.used.}
|
||||
|
||||
import
|
||||
chronicles,
|
||||
../../beacon_chain/spec/forks,
|
||||
../../beacon_chain/spec/state_transition,
|
||||
../../beacon_chain/validators/rewards,
|
||||
./consensus_spec/os_ops,
|
||||
./testutil
|
||||
|
||||
from std/sequtils import toSeq
|
||||
from std/strutils import toLowerAscii
|
||||
from ../../beacon_chain/spec/presets import
|
||||
const_preset, defaultRuntimeConfig
|
||||
from ./consensus_spec/fixtures_utils import
|
||||
SSZ, SszTestsDir, hash_tree_root, parseTest, readSszBytes, toSszType
|
||||
|
||||
proc runTest(consensusFork: static ConsensusFork,
|
||||
testDir: static[string], suiteName, path: string) =
|
||||
test "Block rewards test -" & preset():
|
||||
echo ""
|
||||
echo "path = ", path
|
||||
|
||||
when consensusFork == ConsensusFork.Phase0:
|
||||
skip()
|
||||
else:
|
||||
let
|
||||
testPath = testDir / path
|
||||
preState = newClone(parseTest(testPath / "pre.ssz_snappy",
|
||||
SSZ, consensusFork.BeaconState))
|
||||
postState = testPath / "post.ssz_snappy"
|
||||
blockPath = testPath / "blocks_0.ssz_snappy"
|
||||
|
||||
if not(fileExists(blockPath)) or not(fileExists(postState)):
|
||||
discard
|
||||
else:
|
||||
var
|
||||
fhPreState = ForkedHashedBeaconState.new(preState[])
|
||||
cache = StateCache()
|
||||
info = ForkedEpochInfo()
|
||||
|
||||
let
|
||||
blck = parseTest(testPath/"blocks_0.ssz_snappy",
|
||||
SSZ, consensusFork.SignedBeaconBlock)
|
||||
forkedBlock = ForkedBeaconBlock.init(blck.message)
|
||||
consensusBlockValue = collectBlockRewards(fhPreState[], forkedBlock)
|
||||
|
||||
let (proposerIndex, preStateBalance, blockValue, stateSlot, blckSlot) =
|
||||
withStateAndBlck(fhPreState[], forkedBlock):
|
||||
(forkyBlck.proposer_index,
|
||||
forkyState.data.balances.item(forkyBlck.proposer_index),
|
||||
consensusBlockValue.get(),
|
||||
forkyState.data.slot,
|
||||
forkyBlck.slot)
|
||||
|
||||
info "Perform state_transition with block",
|
||||
blck = shortLog(forkedBlock),
|
||||
block_consensus_value = blockValue,
|
||||
proposer_index = proposerIndex,
|
||||
validator_balance = preStateBalance,
|
||||
state_slot = stateSlot,
|
||||
expected_balance = preStateBalance + blockValue
|
||||
|
||||
block:
|
||||
let res =
|
||||
process_slots(defaultRuntimeConfig, fhPreState[],
|
||||
blckSlot, cache, info, flags = {})
|
||||
if res.isErr():
|
||||
# Ignore failed states.
|
||||
info "State advance failed", reason = $res.error
|
||||
return
|
||||
|
||||
let advanceBalance =
|
||||
withState(fhPreState[]):
|
||||
forkyState.data.balances.item(proposerIndex)
|
||||
|
||||
block:
|
||||
let res =
|
||||
state_transition_block(defaultRuntimeConfig, fhPreState[], blck,
|
||||
cache, flags = {}, noRollback)
|
||||
if res.isErr():
|
||||
# Ignore failed states.
|
||||
info "State transition failed", reason = $res.error
|
||||
return
|
||||
|
||||
let balance =
|
||||
withState(fhPreState[]):
|
||||
forkyState.data.balances.item(proposerIndex)
|
||||
|
||||
info "State transition succesfull",
|
||||
actual_validator_balance = balance,
|
||||
snapshot0_balance = preStateBalance,
|
||||
snapshot1_balance = advanceBalance,
|
||||
calculated_value0 = preStateBalance + blockValue,
|
||||
calculated_value1 = advanceBalance + blockValue
|
||||
|
||||
template runForkBlockTests(consensusFork: static ConsensusFork) =
|
||||
const
|
||||
forkHumanName = $consensusFork
|
||||
forkDirName = forkHumanName.toLowerAscii()
|
||||
FinalityDir =
|
||||
SszTestsDir/const_preset/forkDirName/"finality"/"finality"/"pyspec_tests"
|
||||
RandomDir =
|
||||
SszTestsDir/const_preset/forkDirName/"random"/"random"/"pyspec_tests"
|
||||
SanityBlocksDir =
|
||||
SszTestsDir/const_preset/forkDirName/"sanity"/"blocks"/"pyspec_tests"
|
||||
|
||||
suite "Consensus block value calculation - " & forkHumanName &
|
||||
" - Sanity -" & preset():
|
||||
for kind, path in walkDir(SanityBlocksDir, relative = true,
|
||||
checkDir = true):
|
||||
consensusFork.runTest(SanityBlocksDir, suiteName, path)
|
||||
|
||||
suite "Consensus block value calculation - " & forkHumanName &
|
||||
" - Finality -" & preset():
|
||||
for kind, path in walkDir(FinalityDir, relative = true,
|
||||
checkDir = true):
|
||||
consensusFork.runTest(FinalityDir, suiteName, path)
|
||||
|
||||
suite "Consensus block value calculation - " & forkHumanName &
|
||||
" - Random -" & preset():
|
||||
for kind, path in walkDir(RandomDir, relative = true,
|
||||
checkDir = true):
|
||||
consensusFork.runTest(RandomDir, suiteName, path)
|
||||
|
||||
|
||||
withAll(ConsensusFork):
|
||||
runForkBlockTests(consensusFork)
|
Loading…
x
Reference in New Issue
Block a user