Consensus block value calculation for produceBlockV3 API call. (#5873)

* allow specifying get_proposer_reward block root at state.slot

* Add consensus_block_value calculation.

* Address review comments.

* Post-rebase adjustments.

* Use proper state to calculate consensus block value.

* Revert "allow specifying get_proposer_reward block root at state.slot"

This reverts commit 9fef9a8199f63056060527ac2531acc3b0ed8dcb.

* Fix post-revert problems.
Return back to Gwei.

* Adding test which is not working.

* Do not use test suite if it does not have post-state.

* Add debug logging.

* Increase logging to track sources of balance changes.

* Fix sync committee rewards/penalties calculation.

* Revert "Increase logging to track sources of balance changes."

This reverts commit 32feb20f2fdb66521401710866cd59ecc9951ef8.

* Adopt new vision to block rewards.

* Add block produce logging to VC.

* Remove rewards.nim.

* Eliminate toWei changes.

* Improve UInt256 shortLog.

* Fix conversion procedure.

* Address review comments.

* Fix test.

* Revert "Fix test."

This reverts commit 4948b2c1ec.

---------

Co-authored-by: tersec <tersec@users.noreply.github.com>
Co-authored-by: Etan Kissling <etan@status.im>
This commit is contained in:
Eugene Kabanov 2024-03-11 16:18:50 +02:00 committed by GitHub
parent 2d519ac715
commit f088e5f57b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 125 additions and 48 deletions

View File

@ -672,8 +672,8 @@ proc installValidatorApiHandlers*(router: var RestRouter, node: BeaconNode) =
message = (await PayloadType.makeBeaconBlockForHeadAndSlot( message = (await PayloadType.makeBeaconBlockForHeadAndSlot(
node, qrandao, proposer, qgraffiti, qhead, qslot)).valueOr: node, qrandao, proposer, qgraffiti, qhead, qslot)).valueOr:
return RestApiResponse.jsonError(Http500, error) return RestApiResponse.jsonError(Http500, error)
executionValue = Opt.some(UInt256(message.blockValue)) executionValue = Opt.some(UInt256(message.executionPayloadValue))
consensusValue = Opt.none(UInt256) consensusValue = Opt.some(UInt256(message.consensusBlockValue))
headers = consensusFork.getMaybeBlindedHeaders( headers = consensusFork.getMaybeBlindedHeaders(
isBlinded = false, executionValue, consensusValue) isBlinded = false, executionValue, consensusValue)

View File

@ -48,7 +48,7 @@ import
beaconstate, eth2_merkleization, forks, helpers, signatures, beaconstate, eth2_merkleization, forks, helpers, signatures,
state_transition_block, state_transition_epoch, validator] state_transition_block, state_transition_epoch, validator]
export results, extras export results, extras, state_transition_block
logScope: logScope:
topics = "state_transition" topics = "state_transition"
@ -382,7 +382,7 @@ func partialBeaconBlock*(
res res
proc makeBeaconBlock*( proc makeBeaconBlockWithRewards*(
cfg: RuntimeConfig, cfg: RuntimeConfig,
state: var ForkedHashedBeaconState, state: var ForkedHashedBeaconState,
proposer_index: ValidatorIndex, proposer_index: ValidatorIndex,
@ -403,13 +403,15 @@ proc makeBeaconBlock*(
transactions_root: Opt[Eth2Digest], transactions_root: Opt[Eth2Digest],
execution_payload_root: Opt[Eth2Digest], execution_payload_root: Opt[Eth2Digest],
kzg_commitments: Opt[KzgCommitments]): 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 ## 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 ## 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 ## state.slot meaning process_slots must be called up to the slot for which
## the block is to be created. ## 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 # To create a block, we'll first apply a partial block to the state, skipping
# some validations. # some validations.
@ -458,11 +460,10 @@ proc makeBeaconBlock*(
else: else:
static: raiseAssert "Unreachable" static: raiseAssert "Unreachable"
state.`kind Data`.root = hash_tree_root(state.`kind Data`.data) state.`kind Data`.root = hash_tree_root(state.`kind Data`.data)
blck.`kind Data`.state_root = state.`kind Data`.root blck.`kind Data`.state_root = state.`kind Data`.root
ok(blck) ok((blck: blck, rewards: res.get))
const payloadFork = typeof(executionPayload).kind const payloadFork = typeof(executionPayload).kind
when payloadFork == ConsensusFork.Bellatrix: when payloadFork == ConsensusFork.Bellatrix:
@ -482,6 +483,28 @@ proc makeBeaconBlock*(
else: else:
{.error: "Unsupported fork".} {.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*( proc makeBeaconBlock*(
cfg: RuntimeConfig, state: var ForkedHashedBeaconState, cfg: RuntimeConfig, state: var ForkedHashedBeaconState,
proposer_index: ValidatorIndex, randao_reveal: ValidatorSig, proposer_index: ValidatorIndex, randao_reveal: ValidatorSig,

View File

@ -33,6 +33,19 @@ type
blockRoot*: Eth2Digest blockRoot*: Eth2Digest
data*: ForkedBlindedBeaconBlock data*: ForkedBlindedBeaconBlock
func shortLog(v: Opt[UInt256]): auto =
if v.isNone(): "<not available>" else: toString(v.get, 10)
func shortLog(v: ForkedMaybeBlindedBeaconBlock): auto =
withForkyMaybeBlindedBlck(v):
when consensusFork < ConsensusFork.Deneb:
shortLog(forkyMaybeBlindedBlck)
else:
when isBlinded:
shortLog(forkyMaybeBlindedBlck)
else:
shortLog(forkyMaybeBlindedBlck.`block`)
proc proposeBlock(vc: ValidatorClientRef, slot: Slot, proc proposeBlock(vc: ValidatorClientRef, slot: Slot,
proposerKey: ValidatorPubKey) {.async.} proposerKey: ValidatorPubKey) {.async.}
@ -237,6 +250,15 @@ proc publishBlockV3(vc: ValidatorClientRef, currentSlot, slot: Slot,
when isBlinded: when isBlinded:
let let
blockRoot = hash_tree_root(forkyMaybeBlindedBlck) blockRoot = hash_tree_root(forkyMaybeBlindedBlck)
debug "Block produced",
block_type = "blinded",
block_root = shortLog(blockRoot),
blck = shortLog(maybeBlock),
execution_value = shortLog(maybeBlock.executionValue),
consensus_value = shortLog(maybeBlock.consensusValue)
let
signingRoot = signingRoot =
compute_block_signing_root(fork, genesisRoot, slot, blockRoot) compute_block_signing_root(fork, genesisRoot, slot, blockRoot)
notSlashable = vc.attachedValidators[] notSlashable = vc.attachedValidators[]
@ -308,6 +330,15 @@ proc publishBlockV3(vc: ValidatorClientRef, currentSlot, slot: Slot,
else: else:
forkyMaybeBlindedBlck.`block` forkyMaybeBlindedBlck.`block`
) )
debug "Block produced",
block_type = "non-blinded",
block_root = shortLog(blockRoot),
blck = shortLog(maybeBlock),
execution_value = shortLog(maybeBlock.executionValue),
consensus_value = shortLog(maybeBlock.consensusValue)
let
signingRoot = signingRoot =
compute_block_signing_root(fork, genesisRoot, slot, blockRoot) compute_block_signing_root(fork, genesisRoot, slot, blockRoot)
notSlashable = vc.attachedValidators[] notSlashable = vc.attachedValidators[]

View File

@ -76,13 +76,16 @@ declarePublicGauge(attached_validator_balance_total,
logScope: topics = "beacval" logScope: topics = "beacval"
type type
EngineBid = tuple[ EngineBid* = object
blck: ForkedBeaconBlock, blck*: ForkedBeaconBlock
blockValue: Wei, executionPayloadValue*: Wei
blobsBundleOpt: Opt[BlobsBundle]] consensusBlockValue*: UInt256
blobsBundleOpt*: Opt[BlobsBundle]
BuilderBid[SBBB] = tuple[ BuilderBid[SBBB] = object
blindedBlckPart: SBBB, blockValue: UInt256] blindedBlckPart*: SBBB
executionPayloadValue*: UInt256
consensusBlockValue*: UInt256
ForkedBlockResult = ForkedBlockResult =
Result[EngineBid, string] Result[EngineBid, string]
@ -104,6 +107,11 @@ proc getValidator*(validators: auto,
Opt.some ValidatorAndIndex(index: ValidatorIndex(idx), Opt.some ValidatorAndIndex(index: ValidatorIndex(idx),
validator: validators[idx]) validator: validators[idx])
func blockConsensusValue(r: BlockRewards): UInt256 {.noinit.} =
# Returns value of `block-consensus-value` in Wei units.
u256(r.attestations + r.sync_aggregate +
r.proposer_slashings + r.attester_slashings) * u256(1000000000)
proc addValidatorsFromWeb3Signer( proc addValidatorsFromWeb3Signer(
node: BeaconNode, web3signerUrl: Web3SignerUrl, epoch: Epoch) node: BeaconNode, web3signerUrl: Web3SignerUrl, epoch: Epoch)
{.async: (raises: [CancelledError]).} = {.async: (raises: [CancelledError]).} =
@ -494,7 +502,7 @@ proc makeBeaconBlockForHeadAndSlot*(
slot, validator_index slot, validator_index
return err("Unable to get execution payload") return err("Unable to get execution payload")
let blck = makeBeaconBlock( let res = makeBeaconBlockWithRewards(
node.dag.cfg, node.dag.cfg,
state[], state[],
validator_index, validator_index,
@ -523,10 +531,16 @@ proc makeBeaconBlockForHeadAndSlot*(
var blobsBundleOpt = Opt.none(BlobsBundle) var blobsBundleOpt = Opt.none(BlobsBundle)
when payload is deneb.ExecutionPayloadForSigning: when payload is deneb.ExecutionPayloadForSigning:
blobsBundleOpt = Opt.some(payload.blobsBundle) blobsBundleOpt = Opt.some(payload.blobsBundle)
return if blck.isOk:
ok((blck.get, payload.blockValue, blobsBundleOpt)) if res.isOk:
ok(EngineBid(
blck: res.get().blck,
executionPayloadValue: payload.blockValue,
consensusBlockValue: res.get().rewards.blockConsensusValue(),
blobsBundleOpt: blobsBundleOpt
))
else: else:
err(blck.error) err(res.error)
proc makeBeaconBlockForHeadAndSlot*( proc makeBeaconBlockForHeadAndSlot*(
PayloadType: type ForkyExecutionPayloadForSigning, node: BeaconNode, randao_reveal: ValidatorSig, PayloadType: type ForkyExecutionPayloadForSigning, node: BeaconNode, randao_reveal: ValidatorSig,
@ -578,11 +592,11 @@ proc getBlindedExecutionPayload[
when EPH is deneb_mev.BlindedExecutionPayloadAndBlobsBundle: when EPH is deneb_mev.BlindedExecutionPayloadAndBlobsBundle:
template builderBid: untyped = blindedHeader.data.message template builderBid: untyped = blindedHeader.data.message
return ok(( return ok(BuilderBid[EPH](
blindedBlckPart: EPH( blindedBlckPart: EPH(
execution_payload_header: builderBid.header, execution_payload_header: builderBid.header,
blob_kzg_commitments: builderBid.blob_kzg_commitments), blob_kzg_commitments: builderBid.blob_kzg_commitments),
blockValue: builderBid.value)) executionPayloadValue: builderBid.value))
else: else:
static: doAssert false static: doAssert false
@ -691,7 +705,7 @@ proc getBlindedBlockParts[
node: BeaconNode, payloadBuilderClient: RestClientRef, head: BlockRef, node: BeaconNode, payloadBuilderClient: RestClientRef, head: BlockRef,
pubkey: ValidatorPubKey, slot: Slot, randao: ValidatorSig, pubkey: ValidatorPubKey, slot: Slot, randao: ValidatorSig,
validator_index: ValidatorIndex, graffiti: GraffitiBytes): validator_index: ValidatorIndex, graffiti: GraffitiBytes):
Future[Result[(EPH, UInt256, ForkedBeaconBlock), string]] Future[Result[(EPH, UInt256, UInt256, ForkedBeaconBlock), string]]
{.async: (raises: [CancelledError]).} = {.async: (raises: [CancelledError]).} =
let let
executionBlockHash = node.dag.loadExecutionBlockHash(head).valueOr: executionBlockHash = node.dag.loadExecutionBlockHash(head).valueOr:
@ -776,7 +790,8 @@ proc getBlindedBlockParts[
return ok( return ok(
(executionPayloadHeader.get.blindedBlckPart, (executionPayloadHeader.get.blindedBlckPart,
executionPayloadHeader.get.blockValue, executionPayloadHeader.get.executionPayloadValue,
forkedBlck.consensusBlockValue,
forkedBlck.blck)) forkedBlck.blck))
proc getBuilderBid[SBBB: deneb_mev.SignedBlindedBeaconBlock]( proc getBuilderBid[SBBB: deneb_mev.SignedBlindedBeaconBlock](
@ -801,7 +816,8 @@ proc getBuilderBid[SBBB: deneb_mev.SignedBlindedBeaconBlock](
# These, together, get combined into the blinded block for signing and # These, together, get combined into the blinded block for signing and
# proposal through the relay network. # proposal through the relay network.
let (executionPayloadHeader, bidValue, forkedBlck) = blindedBlockParts.get let (executionPayloadHeader, bidValue, consensusValue, forkedBlck) =
blindedBlockParts.get
let unsignedBlindedBlock = getUnsignedBlindedBeaconBlock[SBBB]( let unsignedBlindedBlock = getUnsignedBlindedBeaconBlock[SBBB](
node, slot, validator_index, forkedBlck, executionPayloadHeader) node, slot, validator_index, forkedBlck, executionPayloadHeader)
@ -809,7 +825,11 @@ proc getBuilderBid[SBBB: deneb_mev.SignedBlindedBeaconBlock](
if unsignedBlindedBlock.isErr: if unsignedBlindedBlock.isErr:
return err unsignedBlindedBlock.error() return err unsignedBlindedBlock.error()
return ok (unsignedBlindedBlock.get, bidValue) ok(BuilderBid[SBBB](
blindedBlckPart: unsignedBlindedBlock.get,
executionPayloadValue: bidValue,
consensusBlockValue: consensusValue
))
proc proposeBlockMEV( proc proposeBlockMEV(
node: BeaconNode, payloadBuilderClient: RestClientRef, node: BeaconNode, payloadBuilderClient: RestClientRef,
@ -886,15 +906,20 @@ proc makeBlindedBeaconBlockForHeadAndSlot*[BBB: ForkyBlindedBeaconBlock](
# Don't try EL fallback -- VC specifically requested a blinded block # Don't try EL fallback -- VC specifically requested a blinded block
return err("Unable to create blinded block") return err("Unable to create blinded block")
let (executionPayloadHeader, bidValue, forkedBlck) = blindedBlockParts.get let (executionPayloadHeader, bidValue, consensusValue, forkedBlck) =
blindedBlockParts.get
withBlck(forkedBlck): withBlck(forkedBlck):
when consensusFork >= ConsensusFork.Capella: when consensusFork >= ConsensusFork.Capella:
when ((consensusFork == ConsensusFork.Deneb and when ((consensusFork == ConsensusFork.Deneb and
EPH is deneb_mev.BlindedExecutionPayloadAndBlobsBundle) or EPH is deneb_mev.BlindedExecutionPayloadAndBlobsBundle) or
(consensusFork == ConsensusFork.Capella and (consensusFork == ConsensusFork.Capella and
EPH is capella.ExecutionPayloadHeader)): EPH is capella.ExecutionPayloadHeader)):
return ok (constructPlainBlindedBlock[BBB]( return ok(
forkyBlck, executionPayloadHeader), bidValue) BuilderBid[BBB](
blindedBlckPart:
constructPlainBlindedBlock[BBB](forkyBlck, executionPayloadHeader),
executionPayloadValue: bidValue,
consensusBlockValue: consensusValue))
else: else:
return err("makeBlindedBeaconBlockForHeadAndSlot: mismatched block/payload types") return err("makeBlindedBeaconBlockForHeadAndSlot: mismatched block/payload types")
else: else:
@ -1013,8 +1038,8 @@ proc proposeBlockAux(
if collectedBids.builderBid.isSome(): if collectedBids.builderBid.isSome():
collectedBids.engineBid.isNone() or builderBetterBid( collectedBids.engineBid.isNone() or builderBetterBid(
localBlockValueBoost, localBlockValueBoost,
collectedBids.builderBid.value().blockValue, collectedBids.builderBid.value().executionPayloadValue,
collectedBids.engineBid.value().blockValue) collectedBids.engineBid.value().executionPayloadValue)
else: else:
if not collectedBids.engineBid.isSome(): if not collectedBids.engineBid.isSome():
return head # errors logged in router return head # errors logged in router
@ -1035,14 +1060,14 @@ proc proposeBlockAux(
localBlockValueBoost, localBlockValueBoost,
useBuilderBlock, useBuilderBlock,
builderBlockValue = builderBlockValue =
toString(collectedBids.builderBid.value().blockValue, 10), toString(collectedBids.builderBid.value().executionPayloadValue, 10),
engineBlockValue = engineBlockValue =
toString(collectedBids.engineBid.value().blockValue, 10) toString(collectedBids.engineBid.value().executionPayloadValue, 10)
elif payloadBuilderClient.isNil: elif payloadBuilderClient.isNil:
discard # builder API not configured for this block discard # builder API not configured for this block
else: else:
info "Did not receive expected builder bid; using engine block", info "Did not receive expected builder bid; using engine block",
engineBlockValue = collectedBids.engineBid.value().blockValue engineBlockValue = collectedBids.engineBid.value().executionPayloadValue
else: else:
# Similar three cases: builder bid expected and absent, builder bid # Similar three cases: builder bid expected and absent, builder bid
# expected and present, and builder bid not expected. However, only # expected and present, and builder bid not expected. However, only
@ -1051,7 +1076,7 @@ proc proposeBlockAux(
if collectedBids.builderBid.isSome: if collectedBids.builderBid.isSome:
info "Did not receive expected engine bid; using builder block", info "Did not receive expected engine bid; using builder block",
builderBlockValue = builderBlockValue =
collectedBids.builderBid.value().blockValue collectedBids.builderBid.value().executionPayloadValue
if useBuilderBlock: if useBuilderBlock:
let let
@ -1993,8 +2018,8 @@ proc makeMaybeBlindedBeaconBlockForHeadAndSlotImpl[ResultType](
if collectedBids.builderBid.isSome(): if collectedBids.builderBid.isSome():
collectedBids.engineBid.isNone() or builderBetterBid( collectedBids.engineBid.isNone() or builderBetterBid(
localBlockValueBoost, localBlockValueBoost,
collectedBids.builderBid.value().blockValue, collectedBids.builderBid.value().executionPayloadValue,
collectedBids.engineBid.value().blockValue) collectedBids.engineBid.value().executionPayloadValue)
else: else:
if not(collectedBids.engineBid.isSome): if not(collectedBids.engineBid.isSome):
return ResultType.err("Engine bid is not available") return ResultType.err("Engine bid is not available")
@ -2002,16 +2027,14 @@ proc makeMaybeBlindedBeaconBlockForHeadAndSlotImpl[ResultType](
engineBid = block: engineBid = block:
if useBuilderBlock: if useBuilderBlock:
let let blindedBid = collectedBids.builderBid.value()
blindedBid = collectedBids.builderBid.value()
payloadValue = blindedBid.blockValue
return ResultType.ok(( return ResultType.ok((
blck: consensusFork.MaybeBlindedBeaconBlock( blck:
consensusFork.MaybeBlindedBeaconBlock(
isBlinded: true, isBlinded: true,
blindedData: blindedBid.blindedBlckPart.message), blindedData: blindedBid.blindedBlckPart.message),
executionValue: Opt.some(payloadValue), executionValue: Opt.some(blindedBid.executionPayloadValue),
consensusValue: Opt.none(UInt256))) consensusValue: Opt.some(blindedBid.consensusBlockValue)))
collectedBids.engineBid.value() collectedBids.engineBid.value()
@ -2027,15 +2050,15 @@ proc makeMaybeBlindedBeaconBlockForHeadAndSlotImpl[ResultType](
`block`: forkyBlck, `block`: forkyBlck,
kzg_proofs: blobsBundle.proofs, kzg_proofs: blobsBundle.proofs,
blobs: blobsBundle.blobs)), blobs: blobsBundle.blobs)),
executionValue: Opt.some(engineBid.blockValue), executionValue: Opt.some(engineBid.executionPayloadValue),
consensusValue: Opt.none(UInt256))) consensusValue: Opt.some(engineBid.consensusBlockValue)))
else: else:
ResultType.ok(( ResultType.ok((
blck: consensusFork.MaybeBlindedBeaconBlock( blck: consensusFork.MaybeBlindedBeaconBlock(
isBlinded: false, isBlinded: false,
data: forkyBlck), data: forkyBlck),
executionValue: Opt.some(engineBid.blockValue), executionValue: Opt.some(engineBid.executionPayloadValue),
consensusValue: Opt.none(UInt256))) consensusValue: Opt.some(engineBid.consensusBlockValue)))
proc makeMaybeBlindedBeaconBlockForHeadAndSlot*( proc makeMaybeBlindedBeaconBlockForHeadAndSlot*(
node: BeaconNode, consensusFork: static ConsensusFork, node: BeaconNode, consensusFork: static ConsensusFork,