Skip to content

Commit 4ab961f

Browse files
abi87calberarezzmah
authored
chore(engine): Add previous proposer public key to engine API and validate (#2836)
Co-authored-by: Cal Bera <calbera@berachain.com> Co-authored-by: Rez <rez@berachain.com>
1 parent 5391087 commit 4ab961f

35 files changed

Lines changed: 521 additions & 200 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ go.work.sum
5151
# .nfs files are created when an open file is removed but is still being accessed
5252
.nfs*
5353

54+
# Version files created during code generation
55+
v*.*.*
56+
5457
###########
5558
# Windows #
5659
###########

beacon/blockchain/finalize_block.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
ctypes "github.com/berachain/beacon-kit/consensus-types/types"
2929
"github.com/berachain/beacon-kit/consensus/types"
3030
datypes "github.com/berachain/beacon-kit/da/types"
31+
"github.com/berachain/beacon-kit/primitives/crypto"
3132
"github.com/berachain/beacon-kit/primitives/math"
3233
"github.com/berachain/beacon-kit/primitives/transition"
3334
statedb "github.com/berachain/beacon-kit/state-transition/core/state"
@@ -46,11 +47,18 @@ func (s *Service) FinalizeBlock(
4647
return nil, fmt.Errorf("failed to decode block and blobs: %w", err)
4748
}
4849
blk := signedBlk.GetBeaconBlock()
50+
st := s.storageBackend.StateFromContext(ctx)
4951

5052
// Send an FCU to force the HEAD of the chain on the EL on startup.
5153
var finalizeErr error
5254
s.forceStartupSyncOnce.Do(func() {
53-
finalizeErr = s.forceSyncUponFinalize(ctx, blk)
55+
var parentProposerPubkey *crypto.BLSPubkey
56+
parentProposerPubkey, finalizeErr = st.ParentProposerPubkey(blk.GetTimestamp())
57+
if finalizeErr != nil {
58+
finalizeErr = fmt.Errorf("force sync upon finalize: failed retrieving parent proposer pubkey: %w", finalizeErr)
59+
} else {
60+
finalizeErr = s.forceSyncUponFinalize(ctx, blk, parentProposerPubkey)
61+
}
5462
})
5563
if finalizeErr != nil {
5664
return nil, finalizeErr
@@ -63,7 +71,6 @@ func (s *Service) FinalizeBlock(
6371

6472
// STEP 3: Finalize the block.
6573
consensusBlk := types.NewConsensusBlock(blk, req.GetProposerAddress(), req.GetTime())
66-
st := s.storageBackend.StateFromContext(ctx)
6774
valUpdates, err := s.finalizeBeaconBlock(ctx, st, consensusBlk)
6875
if err != nil {
6976
s.logger.Error("Failed to process verified beacon block",

beacon/blockchain/payload.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
engineerrors "github.com/berachain/beacon-kit/engine-primitives/errors"
3131
"github.com/berachain/beacon-kit/errors"
3232
"github.com/berachain/beacon-kit/payload/builder"
33+
"github.com/berachain/beacon-kit/primitives/crypto"
3334
"github.com/berachain/beacon-kit/primitives/math"
3435
statedb "github.com/berachain/beacon-kit/state-transition/core/state"
3536
)
@@ -79,10 +80,11 @@ func (s *Service) forceSyncUponProcess(
7980
func (s *Service) forceSyncUponFinalize(
8081
ctx context.Context,
8182
beaconBlock *ctypes.BeaconBlock,
83+
parentProposerPubkey *crypto.BLSPubkey,
8284
) error {
8385
// NewPayload call first to load payload into EL client.
8486
executionPayload := beaconBlock.GetBody().GetExecutionPayload()
85-
payloadReq, err := ctypes.BuildNewPayloadRequestFromFork(beaconBlock)
87+
payloadReq, err := ctypes.BuildNewPayloadRequestFromFork(beaconBlock, parentProposerPubkey)
8688
if err != nil {
8789
return err
8890
}
@@ -178,6 +180,11 @@ func (s *Service) preFetchBuildData(st *statedb.StateDB, currentTime math.U64) (
178180
return nil, err
179181
}
180182

183+
parentProposerPubkey, err := st.ParentProposerPubkey(nextPayloadTimestamp)
184+
if err != nil {
185+
return nil, fmt.Errorf("failed retrieving previous proposer public key: %w", err)
186+
}
187+
181188
return &builder.RequestPayloadData{
182189
Slot: blkSlot,
183190
Timestamp: nextPayloadTimestamp,
@@ -191,6 +198,8 @@ func (s *Service) preFetchBuildData(st *statedb.StateDB, currentTime math.U64) (
191198
// Assumuming consensus guarantees single slot finality, the parent
192199
// of the latest block we verified must be final already.
193200
FinalEth1BlockHash: lph.GetParentHash(),
201+
202+
ParentProposerPubkey: parentProposerPubkey,
194203
}, nil
195204
}
196205

beacon/blockchain/payload_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,13 +371,13 @@ func buildNextBlock(
371371
noExecReq = &ctypes.ExecutionRequests{}
372372
)
373373
if version.IsBefore(fv, version.Electra()) {
374-
ethBlk, _, err = ctypes.MakeEthBlock(payload, &parentBeaconBlockRoot)
374+
ethBlk, _, err = ctypes.MakeEthBlock(payload, parentBeaconBlockRoot, nil, nil)
375375
require.NoError(t, err)
376376
} else {
377377
encodedER, erErr := ctypes.GetExecutionRequestsList(noExecReq)
378378
require.NoError(t, erErr)
379379
require.NotNil(t, encodedER)
380-
ethBlk, _, err = ctypes.MakeEthBlockWithExecutionRequests(payload, &parentBeaconBlockRoot, encodedER)
380+
ethBlk, _, err = ctypes.MakeEthBlock(payload, parentBeaconBlockRoot, encodedER, nil)
381381
require.NoError(t, err)
382382
}
383383
payload.BlockHash = common.ExecutionHash(ethBlk.Hash())

beacon/validator/block_builder.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -273,14 +273,20 @@ func (s *Service) retrieveExecutionPayload(
273273
return nil, err
274274
}
275275

276+
parentProposerPubkey, err := st.ParentProposerPubkey(nextPayloadTimestamp)
277+
if err != nil {
278+
return nil, fmt.Errorf("failed retrieving previous proposer public key: %w", err)
279+
}
280+
276281
r := &builder.RequestPayloadData{
277-
Slot: slot,
278-
Timestamp: nextPayloadTimestamp,
279-
PayloadWithdrawals: payloadWithdrawals,
280-
PrevRandao: prevRandao,
281-
ParentBlockRoot: parentBlockRoot,
282-
HeadEth1BlockHash: lph.GetBlockHash(),
283-
FinalEth1BlockHash: lph.GetParentHash(),
282+
Slot: slot,
283+
Timestamp: nextPayloadTimestamp,
284+
PayloadWithdrawals: payloadWithdrawals,
285+
PrevRandao: prevRandao,
286+
ParentBlockRoot: parentBlockRoot,
287+
HeadEth1BlockHash: lph.GetBlockHash(),
288+
FinalEth1BlockHash: lph.GetParentHash(),
289+
ParentProposerPubkey: parentProposerPubkey,
284290
}
285291
return s.localPayloadBuilder.RequestPayloadSync(ctx, r)
286292
}

consensus-types/types/mocks/new_payload_request.mock.go

Lines changed: 55 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)