From d4fa490dec9e8e08086d5a1f19cc349bf14e9b94 Mon Sep 17 00:00:00 2001 From: terencechain Date: Tue, 3 May 2022 09:55:59 -0700 Subject: [PATCH] Handle blind block for DB (#10580) * Handle blind block for DB * Update blinded_beacon_block_bellatrix_test.go * Update blocks_test.go Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com> --- beacon-chain/db/kv/blocks.go | 7 +++++++ beacon-chain/db/kv/blocks_test.go | 11 +++++++++++ beacon-chain/db/kv/key.go | 7 +++++++ beacon-chain/db/kv/schema.go | 5 +++-- beacon-chain/rpc/eth/beacon/blocks_test.go | 2 +- .../wrapper/blinded_beacon_block_bellatrix.go | 4 ++-- .../wrapper/blinded_beacon_block_bellatrix_test.go | 4 ++-- runtime/version/fork.go | 3 +++ testing/util/merge.go | 7 ++++++- 9 files changed, 42 insertions(+), 8 deletions(-) diff --git a/beacon-chain/db/kv/blocks.go b/beacon-chain/db/kv/blocks.go index 69974de560..590d7a19dc 100644 --- a/beacon-chain/db/kv/blocks.go +++ b/beacon-chain/db/kv/blocks.go @@ -705,6 +705,11 @@ func unmarshalBlock(_ context.Context, enc []byte) (interfaces.SignedBeaconBlock if err := rawBlock.UnmarshalSSZ(enc[len(bellatrixKey):]); err != nil { return nil, err } + case hasBellatrixBlindKey(enc): + rawBlock = ðpb.SignedBlindedBeaconBlockBellatrix{} + if err := rawBlock.UnmarshalSSZ(enc[len(bellatrixBlindKey):]); err != nil { + return nil, err + } default: // Marshal block bytes to phase 0 beacon block. rawBlock = ðpb.SignedBeaconBlock{} @@ -722,6 +727,8 @@ func marshalBlock(_ context.Context, blk interfaces.SignedBeaconBlock) ([]byte, return nil, err } switch blk.Version() { + case version.BellatrixBlind: + return snappy.Encode(nil, append(bellatrixBlindKey, obj...)), nil case version.Bellatrix: return snappy.Encode(nil, append(bellatrixKey, obj...)), nil case version.Altair: diff --git a/beacon-chain/db/kv/blocks_test.go b/beacon-chain/db/kv/blocks_test.go index 8d43b24336..82b56ab5dc 100644 --- a/beacon-chain/db/kv/blocks_test.go +++ b/beacon-chain/db/kv/blocks_test.go @@ -56,6 +56,17 @@ var blockTests = []struct { return wrapper.WrappedSignedBeaconBlock(b) }, }, + { + name: "bellatrix blind", + newBlock: func(slot types.Slot, root []byte) (interfaces.SignedBeaconBlock, error) { + b := util.NewBlindedBeaconBlockBellatrix() + b.Block.Slot = slot + if root != nil { + b.Block.ParentRoot = root + } + return wrapper.WrappedSignedBeaconBlock(b) + }, + }, } func TestStore_SaveBackfillBlockRoot(t *testing.T) { diff --git a/beacon-chain/db/kv/key.go b/beacon-chain/db/kv/key.go index 413a8cf24d..43895058ae 100644 --- a/beacon-chain/db/kv/key.go +++ b/beacon-chain/db/kv/key.go @@ -16,3 +16,10 @@ func hasBellatrixKey(enc []byte) bool { } return bytes.Equal(enc[:len(bellatrixKey)], bellatrixKey) } + +func hasBellatrixBlindKey(enc []byte) bool { + if len(bellatrixBlindKey) >= len(enc) { + return false + } + return bytes.Equal(enc[:len(bellatrixBlindKey)], bellatrixBlindKey) +} diff --git a/beacon-chain/db/kv/schema.go b/beacon-chain/db/kv/schema.go index 187d82d9b0..f1b813ed23 100644 --- a/beacon-chain/db/kv/schema.go +++ b/beacon-chain/db/kv/schema.go @@ -48,8 +48,9 @@ var ( // Below keys are used to identify objects are to be fork compatible. // Objects that are only compatible with specific forks should be prefixed with such keys. - altairKey = []byte("altair") - bellatrixKey = []byte("merge") + altairKey = []byte("altair") + bellatrixKey = []byte("merge") + bellatrixBlindKey = []byte("blind-bellatrix") // block root included in the beacon state used by weak subjectivity initial sync originCheckpointBlockRootKey = []byte("origin-checkpoint-block-root") // block root tracking the progress of backfill, or pointing at genesis if backfill has not been initiated diff --git a/beacon-chain/rpc/eth/beacon/blocks_test.go b/beacon-chain/rpc/eth/beacon/blocks_test.go index 4b3adb8781..e565f462b6 100644 --- a/beacon-chain/rpc/eth/beacon/blocks_test.go +++ b/beacon-chain/rpc/eth/beacon/blocks_test.go @@ -674,7 +674,7 @@ func TestSubmitBlindedBlock(t *testing.T) { blk.Block.Slot = 5 blk.Block.ParentRoot = bsRoot[:] blk.Block.Body.ExecutionPayload.Transactions = transactions - blindedBlk := util.NewBlindedBeaconBlockBellatrix() + blindedBlk := util.NewBlindedBeaconBlockBellatrixV2() blindedBlk.Message.Slot = 5 blindedBlk.Message.ParentRoot = bsRoot[:] blindedBlk.Message.Body.ExecutionPayloadHeader.TransactionsRoot = transactionsRoot[:] diff --git a/consensus-types/wrapper/blinded_beacon_block_bellatrix.go b/consensus-types/wrapper/blinded_beacon_block_bellatrix.go index d38ba20ce7..4949a7caf4 100644 --- a/consensus-types/wrapper/blinded_beacon_block_bellatrix.go +++ b/consensus-types/wrapper/blinded_beacon_block_bellatrix.go @@ -110,7 +110,7 @@ func (signedBlindedBeaconBlockBellatrix) PbAltairBlock() (*eth.SignedBeaconBlock // Version of the underlying protobuf object. func (signedBlindedBeaconBlockBellatrix) Version() int { - return version.Bellatrix + return version.BellatrixBlind } // Header converts the underlying protobuf object from blinded block to header format. @@ -225,7 +225,7 @@ func (w blindedBeaconBlockBellatrix) Proto() proto.Message { // Version of the underlying protobuf object. func (blindedBeaconBlockBellatrix) Version() int { - return version.Bellatrix + return version.BellatrixBlind } // AsSignRequestObject returns the underlying sign request object. diff --git a/consensus-types/wrapper/blinded_beacon_block_bellatrix_test.go b/consensus-types/wrapper/blinded_beacon_block_bellatrix_test.go index 78d17256fd..9cfae22243 100644 --- a/consensus-types/wrapper/blinded_beacon_block_bellatrix_test.go +++ b/consensus-types/wrapper/blinded_beacon_block_bellatrix_test.go @@ -149,7 +149,7 @@ func TestBellatrixSignedBlindedBeaconBlock_Version(t *testing.T) { wsb, err := wrapper.WrappedSignedBeaconBlock(ðpb.SignedBlindedBeaconBlockBellatrix{Block: ðpb.BlindedBeaconBlockBellatrix{}}) require.NoError(t, err) - assert.Equal(t, version.Bellatrix, wsb.Version()) + assert.Equal(t, version.BellatrixBlind, wsb.Version()) } func TestBellatrixBlindedBeaconBlock_Slot(t *testing.T) { @@ -243,7 +243,7 @@ func TestBellatrixBlindedBeaconBlock_Version(t *testing.T) { wb, err := wrapper.WrappedBeaconBlock(ðpb.BlindedBeaconBlockBellatrix{}) require.NoError(t, err) - assert.Equal(t, version.Bellatrix, wb.Version()) + assert.Equal(t, version.BellatrixBlind, wb.Version()) } func TestBellatrixBlindedBeaconBlockBody_RandaoReveal(t *testing.T) { diff --git a/runtime/version/fork.go b/runtime/version/fork.go index 2ce11ff5fa..1d9f81e267 100644 --- a/runtime/version/fork.go +++ b/runtime/version/fork.go @@ -4,6 +4,7 @@ const ( Phase0 = iota Altair Bellatrix + BellatrixBlind ) func String(version int) string { @@ -14,6 +15,8 @@ func String(version int) string { return "altair" case Bellatrix: return "bellatrix" + case BellatrixBlind: + return "bellatrix-blind" default: return "unknown version" } diff --git a/testing/util/merge.go b/testing/util/merge.go index 49f8962a61..6af3174d17 100644 --- a/testing/util/merge.go +++ b/testing/util/merge.go @@ -11,6 +11,11 @@ func NewBeaconBlockBellatrix() *ethpb.SignedBeaconBlockBellatrix { } // NewBlindedBeaconBlockBellatrix creates a blinded beacon block with minimum marshalable fields. -func NewBlindedBeaconBlockBellatrix() *v2.SignedBlindedBeaconBlockBellatrix { +func NewBlindedBeaconBlockBellatrix() *ethpb.SignedBlindedBeaconBlockBellatrix { + return HydrateSignedBlindedBeaconBlockBellatrix(ðpb.SignedBlindedBeaconBlockBellatrix{}) +} + +// NewBlindedBeaconBlockBellatrixV2 creates a blinded beacon block with minimum marshalable fields. +func NewBlindedBeaconBlockBellatrixV2() *v2.SignedBlindedBeaconBlockBellatrix { return HydrateV2SignedBlindedBeaconBlockBellatrix(&v2.SignedBlindedBeaconBlockBellatrix{}) }