From 57974ce61be35132da0e0108ee1ed1c9bdf68e52 Mon Sep 17 00:00:00 2001 From: tersec Date: Fri, 17 Dec 2021 12:23:32 +0000 Subject: [PATCH] forkchoiceUpdate support (#3199) --- beacon_chain/eth1/eth1_monitor.nim | 15 +++++++++++++++ beacon_chain/validators/validator_duties.nim | 19 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/beacon_chain/eth1/eth1_monitor.nim b/beacon_chain/eth1/eth1_monitor.nim index 10e97f2f95..122ba56747 100644 --- a/beacon_chain/eth1/eth1_monitor.nim +++ b/beacon_chain/eth1/eth1_monitor.nim @@ -421,6 +421,21 @@ proc executePayload*(p: Web3DataProviderRef, payload: engine_api.ExecutionPayloadV1): Future[ExecutePayloadResponse] = p.web3.provider.engine_executePayloadV1(payload) +proc forkchoiceUpdated*(p: Web3DataProviderRef, + headBlock, finalizedBlock: Eth2Digest): + Future[engine_api.ForkchoiceUpdatedResponse] = + p.web3.provider.engine_forkchoiceUpdatedV1( + ForkchoiceStateV1( + headBlockHash: headBlock.asBlockHash, + + # https://hackmd.io/@n0ble/kintsugi-spec#Engine-API + # "CL client software MUST use headBlockHash value as a stub for the + # safeBlockHash parameter" + safeBlockHash: headBlock.asBlockHash, + + finalizedBlockHash: finalizedBlock.asBlockHash), + none(engine_api.PayloadAttributesV1)) + proc forkchoiceUpdated*(p: Web3DataProviderRef, headBlock, finalizedBlock: Eth2Digest, timestamp: uint64, diff --git a/beacon_chain/validators/validator_duties.nim b/beacon_chain/validators/validator_duties.nim index e750d57375..2bda7bc17f 100644 --- a/beacon_chain/validators/validator_duties.nim +++ b/beacon_chain/validators/validator_duties.nim @@ -18,6 +18,7 @@ import json_serialization/std/[options, sets, net], serialization/errors, eth/db/kvstore, eth/keys, eth/p2p/discoveryv5/[protocol, enr], + web3/ethtypes, # Local modules ../spec/datatypes/[phase0, altair, merge], @@ -416,6 +417,24 @@ proc getBlockProposalEth1Data*(node: BeaconNode, state, finalizedEpochRef.eth1_data, finalizedEpochRef.eth1_deposit_index) +proc forkchoice_updated(state: merge.BeaconState, + head_block_hash: Eth2Digest, + finalized_block_hash: Eth2Digest, + fee_recipient: ethtypes.Address, + execution_engine: Web3DataProviderRef): + Future[Option[merge.PayloadId]] {.async.} = + let + timestamp = compute_timestamp_at_slot(state, state.slot) + random = get_randao_mix(state, get_current_epoch(state)) + payloadId = + (await execution_engine.forkchoiceUpdated( + head_block_hash, finalized_block_hash, timestamp, random.data, + fee_recipient)).payloadId + return if payloadId.isSome: + some(merge.PayloadId(payloadId.get)) + else: + none(merge.PayloadId) + proc makeBeaconBlockForHeadAndSlot*(node: BeaconNode, randao_reveal: ValidatorSig, validator_index: ValidatorIndex,