Use proper state to calculate consensus block value.

This commit is contained in:
cheatfate 2024-02-16 22:54:57 +02:00
parent da45307422
commit 00e383070d
No known key found for this signature in database
GPG Key ID: 46ADD633A7201F95
2 changed files with 55 additions and 38 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

@ -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]
@ -523,8 +526,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)
let reward = collectBlockRewards(state[], blck.get())
return if blck.isOk: return if blck.isOk:
ok((blck.get, payload.blockValue, blobsBundleOpt)) ok(EngineBid(
blck: blck.get(),
executionPayloadValue: payload.blockValue,
consensusBlockValue: reward.get,
blobsBundleOpt: blobsBundleOpt
))
else: else:
err(blck.error) err(blck.error)
@ -578,11 +589,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 +702,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 +787,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 +813,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 +822,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 +903,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 +1035,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 +1057,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 +1073,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
@ -1997,8 +2019,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")
@ -2006,19 +2028,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: blck:
consensusFork.MaybeBlindedBeaconBlock( consensusFork.MaybeBlindedBeaconBlock(
isBlinded: true, isBlinded: true,
blindedData: blindedBid.blindedBlckPart.message), blindedData: blindedBid.blindedBlckPart.message),
executionValue: executionValue: Opt.some(blindedBid.executionPayloadValue),
Opt.some(payloadValue), consensusValue: Opt.some(blindedBid.consensusBlockValue)))
consensusValue:
node.getConsensusBlockValue(blindedBid.blindedBlckPart.message)))
collectedBids.engineBid.value() collectedBids.engineBid.value()
@ -2034,15 +2051,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: node.getConsensusBlockValue(engineBid.blck))) 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: node.getConsensusBlockValue(engineBid.blck))) consensusValue: Opt.some(engineBid.consensusBlockValue)))
proc makeMaybeBlindedBeaconBlockForHeadAndSlot*( proc makeMaybeBlindedBeaconBlockForHeadAndSlot*(
node: BeaconNode, consensusFork: static ConsensusFork, node: BeaconNode, consensusFork: static ConsensusFork,