From d35006b0a3f6d61164f42fe8fb5af1373c9298d3 Mon Sep 17 00:00:00 2001 From: terencechain Date: Wed, 10 Aug 2022 11:31:37 -0700 Subject: [PATCH] Validate builder header (#11195) Co-authored-by: Raul Jordan --- .../prysm/v1alpha1/validator/proposer_bellatrix.go | 14 ++++++++++++++ .../v1alpha1/validator/proposer_bellatrix_test.go | 5 ++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/beacon-chain/rpc/prysm/v1alpha1/validator/proposer_bellatrix.go b/beacon-chain/rpc/prysm/v1alpha1/validator/proposer_bellatrix.go index eacef1e00..a26447d15 100644 --- a/beacon-chain/rpc/prysm/v1alpha1/validator/proposer_bellatrix.go +++ b/beacon-chain/rpc/prysm/v1alpha1/validator/proposer_bellatrix.go @@ -1,6 +1,7 @@ package validator import ( + "bytes" "context" "fmt" "time" @@ -21,6 +22,7 @@ import ( enginev1 "github.com/prysmaticlabs/prysm/proto/engine/v1" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" "github.com/prysmaticlabs/prysm/runtime/version" + "github.com/prysmaticlabs/prysm/time/slots" "github.com/sirupsen/logrus" ) @@ -118,6 +120,18 @@ func (vs *Server) getPayloadHeaderFromBuilder(ctx context.Context, slot types.Sl if err != nil { return nil, err } + if !bytes.Equal(bid.Message.Header.ParentHash, h.BlockHash()) { + return nil, fmt.Errorf("incorrect parent hash %#x != %#x", bid.Message.Header.ParentHash, h.BlockHash()) + } + + t, err := slots.ToTime(uint64(vs.TimeFetcher.GenesisTime().Unix()), slot) + if err != nil { + return nil, err + } + if bid.Message.Header.Timestamp != uint64(t.Unix()) { + return nil, fmt.Errorf("incorrect timestamp %d != %d", bid.Message.Header.Timestamp, uint64(t.Unix())) + } + if err := vs.validateBuilderSignature(bid); err != nil { return nil, errors.Wrap(err, "could not validate builder signature") } diff --git a/beacon-chain/rpc/prysm/v1alpha1/validator/proposer_bellatrix_test.go b/beacon-chain/rpc/prysm/v1alpha1/validator/proposer_bellatrix_test.go index a7f576e25..97564149e 100644 --- a/beacon-chain/rpc/prysm/v1alpha1/validator/proposer_bellatrix_test.go +++ b/beacon-chain/rpc/prysm/v1alpha1/validator/proposer_bellatrix_test.go @@ -353,6 +353,7 @@ func TestServer_getAndBuildHeaderBlock(t *testing.T) { altairBlk, err := util.GenerateFullBlockAltair(copiedState, keys, util.DefaultBlockGenConfig(), 2) require.NoError(t, err) + ts := uint64(time.Now().Unix()) + uint64(altairBlk.Block.Slot)*params.BeaconConfig().SecondsPerSlot h := &v1.ExecutionPayloadHeader{ BlockNumber: 123, GasLimit: 456, @@ -366,6 +367,7 @@ func TestServer_getAndBuildHeaderBlock(t *testing.T) { BaseFeePerGas: make([]byte, fieldparams.RootLength), BlockHash: make([]byte, fieldparams.RootLength), TransactionsRoot: make([]byte, fieldparams.RootLength), + Timestamp: ts, } vs.StateGen = stategen.New(vs.BeaconDB) @@ -388,6 +390,7 @@ func TestServer_getAndBuildHeaderBlock(t *testing.T) { BaseFeePerGas: make([]byte, fieldparams.RootLength), BlockHash: make([]byte, fieldparams.RootLength), TransactionsRoot: make([]byte, fieldparams.RootLength), + Timestamp: ts, }, Pubkey: sk.PublicKey().Marshal(), Value: bytesutil.PadTo([]byte{1, 2, 3}, 32), @@ -402,7 +405,7 @@ func TestServer_getAndBuildHeaderBlock(t *testing.T) { Signature: sk.Sign(sr[:]).Marshal(), } vs.BlockBuilder = &builderTest.MockBuilderService{HasConfigured: true, Bid: sBid} - + vs.TimeFetcher = &blockchainTest.ChainService{Genesis: time.Now()} ready, builtBlk, err := vs.getAndBuildBlindBlock(ctx, altairBlk.Block) require.NoError(t, err) require.Equal(t, true, ready)