From ba4fa9b12a75bf417dc2afe3635c3c9db5f437f4 Mon Sep 17 00:00:00 2001 From: kewei Date: Wed, 27 Nov 2024 16:28:02 +0800 Subject: [PATCH] update --- cl/spectest/consensus_tests/operations.go | 32 +++++++++++++++++++++++ cl/transition/impl/eth2/operations.go | 22 +++++++++++++++- cl/transition/machine/block.go | 5 +--- cl/transition/machine/machine.go | 2 +- 4 files changed, 55 insertions(+), 6 deletions(-) diff --git a/cl/spectest/consensus_tests/operations.go b/cl/spectest/consensus_tests/operations.go index 69f7b46d8d4..df059698053 100644 --- a/cl/spectest/consensus_tests/operations.go +++ b/cl/spectest/consensus_tests/operations.go @@ -466,3 +466,35 @@ func operationWithdrawalRequstHandler(t *testing.T, root fs.FS, c spectest.TestC assert.EqualValues(t, haveRoot, expectedRoot) return nil } + +func operationExecutionPayloadHandler(t *testing.T, root fs.FS, c spectest.TestCase) error { + preState, err := spectest.ReadBeaconState(root, c.Version(), "pre.ssz_snappy") + require.NoError(t, err) + postState, err := spectest.ReadBeaconState(root, c.Version(), "post.ssz_snappy") + expectedError := os.IsNotExist(err) + if err != nil && !expectedError { + return err + } + body := cltypes.NewBeaconBody(&clparams.MainnetBeaconConfig, c.Version()) + if err := spectest.ReadSszOld(root, body, c.Version(), "body.ssz_snappy"); err != nil { + return err + } + if err := c.Machine.ProcessExecutionPayload(preState, body); err != nil { + //if err := machine.ProcessBlock(c.Machine, preState, body); err != nil { + if expectedError { + return nil + } + return err + } + if expectedError { + return errors.New("expected error") + } + haveRoot, err := preState.HashSSZ() + require.NoError(t, err) + + expectedRoot, err := postState.HashSSZ() + require.NoError(t, err) + + assert.EqualValues(t, haveRoot, expectedRoot) + return nil +} diff --git a/cl/transition/impl/eth2/operations.go b/cl/transition/impl/eth2/operations.go index 0005865bebd..09a89afbe50 100644 --- a/cl/transition/impl/eth2/operations.go +++ b/cl/transition/impl/eth2/operations.go @@ -357,13 +357,24 @@ func (I *impl) ProcessWithdrawals( } // ProcessExecutionPayload sets the latest payload header accordinly. -func (I *impl) ProcessExecutionPayload(s abstract.BeaconState, parentHash, prevRandao common.Hash, time uint64, payloadHeader *cltypes.Eth1Header) error { +func (I *impl) ProcessExecutionPayload(s abstract.BeaconState, body cltypes.GenericBeaconBody) error { + payloadHeader, err := body.GetPayloadHeader() + if err != nil { + return err + } + parentHash := payloadHeader.ParentHash + prevRandao := payloadHeader.PrevRandao + time := payloadHeader.Time if state.IsMergeTransitionComplete(s) { + // Verify consistency of the parent hash with respect to the previous execution payload header + // assert payload.parent_hash == state.latest_execution_payload_header.block_hash if parentHash != s.LatestExecutionPayloadHeader().BlockHash { return errors.New("ProcessExecutionPayload: invalid eth1 chain. mismatching parent") } } if prevRandao != s.GetRandaoMixes(state.Epoch(s)) { + // Verify prev_randao + // assert payload.prev_randao == get_randao_mix(state, get_current_epoch(state)) return fmt.Errorf( "ProcessExecutionPayload: randao mix mismatches with mix digest, expected %x, got %x", s.GetRandaoMixes(state.Epoch(s)), @@ -371,8 +382,17 @@ func (I *impl) ProcessExecutionPayload(s abstract.BeaconState, parentHash, prevR ) } if time != state.ComputeTimestampAtSlot(s, s.Slot()) { + // Verify timestamp + // assert payload.timestamp == compute_timestamp_at_slot(state, state.slot) return errors.New("ProcessExecutionPayload: invalid Eth1 timestamp") } + + // Verify commitments are under limit + // assert len(body.blob_kzg_commitments) <= MAX_BLOBS_PER_BLOCK + if body.GetBlobKzgCommitments().Len() > int(s.BeaconConfig().MaxBlobsPerBlock) { + return errors.New("ProcessExecutionPayload: too many blob commitments") + } + s.SetLatestExecutionPayloadHeader(payloadHeader) return nil } diff --git a/cl/transition/machine/block.go b/cl/transition/machine/block.go index 4f84cc0270d..c1854b0699b 100644 --- a/cl/transition/machine/block.go +++ b/cl/transition/machine/block.go @@ -68,10 +68,7 @@ func ProcessBlock(impl BlockProcessor, s abstract.BeaconState, block cltypes.Gen return fmt.Errorf("processBlock: failed to process withdrawals: %v", err) } } - parentHash := payloadHeader.ParentHash - prevRandao := payloadHeader.PrevRandao - time := payloadHeader.Time - if err := impl.ProcessExecutionPayload(s, parentHash, prevRandao, time, payloadHeader); err != nil { + if err := impl.ProcessExecutionPayload(s, body); err != nil { return fmt.Errorf("processBlock: failed to process execution payload: %v", err) } } diff --git a/cl/transition/machine/machine.go b/cl/transition/machine/machine.go index 74027a42f2a..a7d7d573936 100644 --- a/cl/transition/machine/machine.go +++ b/cl/transition/machine/machine.go @@ -47,7 +47,7 @@ type SlotProcessor interface { type BlockHeaderProcessor interface { ProcessBlockHeader(s abstract.BeaconState, slot, proposerIndex uint64, parentRoot common.Hash, bodyRoot [32]byte) error ProcessWithdrawals(s abstract.BeaconState, withdrawals *solid.ListSSZ[*cltypes.Withdrawal]) error - ProcessExecutionPayload(s abstract.BeaconState, parentHash, prevRandao common.Hash, time uint64, payloadHeader *cltypes.Eth1Header) error + ProcessExecutionPayload(s abstract.BeaconState, body cltypes.GenericBeaconBody) error ProcessRandao(s abstract.BeaconState, randao [96]byte, proposerIndex uint64) error ProcessEth1Data(state abstract.BeaconState, eth1Data *cltypes.Eth1Data) error ProcessSyncAggregate(s abstract.BeaconState, sync *cltypes.SyncAggregate) error