From bdc3a6ef11d33f9a91393870ff37d8d165f266e6 Mon Sep 17 00:00:00 2001 From: Potuz Date: Mon, 9 Jan 2023 11:47:40 -0300 Subject: [PATCH] Capella coverage and json structs (#11854) * Capella coverage and json structs * use SetupTestConfig --- .../rpc/apimiddleware/custom_hooks.go | 24 +++++- .../rpc/apimiddleware/custom_hooks_test.go | 79 ++++++++++++++++++- beacon-chain/rpc/apimiddleware/structs.go | 2 + .../rpc/prysm/v1alpha1/beacon/blocks.go | 14 ++++ .../rpc/prysm/v1alpha1/beacon/blocks_test.go | 73 +++++++++++++++++ 5 files changed, 189 insertions(+), 3 deletions(-) diff --git a/beacon-chain/rpc/apimiddleware/custom_hooks.go b/beacon-chain/rpc/apimiddleware/custom_hooks.go index 00924ca3d913..383abbac4520 100644 --- a/beacon-chain/rpc/apimiddleware/custom_hooks.go +++ b/beacon-chain/rpc/apimiddleware/custom_hooks.go @@ -301,8 +301,10 @@ func setInitialPublishBlockPostRequest(endpoint *apimiddleware.Endpoint, endpoint.PostRequest = &SignedBeaconBlockContainerJson{} } else if currentEpoch < params.BeaconConfig().BellatrixForkEpoch { endpoint.PostRequest = &SignedBeaconBlockAltairContainerJson{} - } else { + } else if currentEpoch < params.BeaconConfig().CapellaForkEpoch { endpoint.PostRequest = &SignedBeaconBlockBellatrixContainerJson{} + } else { + endpoint.PostRequest = &SignedBeaconBlockCapellaContainerJson{} } req.Body = io.NopCloser(bytes.NewBuffer(buf)) return true, nil @@ -707,11 +709,21 @@ type bellatrixProduceBlockResponseJson struct { Data *BeaconBlockBellatrixJson `json:"data"` } +type capellaProduceBlockResponseJson struct { + Version string `json:"version" enum:"true"` + Data *BeaconBlockCapellaJson `json:"data"` +} + type bellatrixProduceBlindedBlockResponseJson struct { Version string `json:"version" enum:"true"` Data *BlindedBeaconBlockBellatrixJson `json:"data"` } +type capellaProduceBlindedBlockResponseJson struct { + Version string `json:"version" enum:"true"` + Data *BlindedBeaconBlockCapellaJson `json:"data"` +} + func serializeProducedV2Block(response interface{}) (apimiddleware.RunDefault, []byte, apimiddleware.ErrorJson) { respContainer, ok := response.(*ProduceBlockResponseV2Json) if !ok { @@ -735,6 +747,11 @@ func serializeProducedV2Block(response interface{}) (apimiddleware.RunDefault, [ Version: respContainer.Version, Data: respContainer.Data.BellatrixBlock, } + case strings.EqualFold(respContainer.Version, strings.ToLower(ethpbv2.Version_CAPELLA.String())): + actualRespContainer = &capellaProduceBlockResponseJson{ + Version: respContainer.Version, + Data: respContainer.Data.CapellaBlock, + } default: return false, nil, apimiddleware.InternalServerError(fmt.Errorf("unsupported block version '%s'", respContainer.Version)) } @@ -769,6 +786,11 @@ func serializeProducedBlindedBlock(response interface{}) (apimiddleware.RunDefau Version: respContainer.Version, Data: respContainer.Data.BellatrixBlock, } + case strings.EqualFold(respContainer.Version, strings.ToLower(ethpbv2.Version_CAPELLA.String())): + actualRespContainer = &capellaProduceBlindedBlockResponseJson{ + Version: respContainer.Version, + Data: respContainer.Data.CapellaBlock, + } default: return false, nil, apimiddleware.InternalServerError(fmt.Errorf("unsupported block version '%s'", respContainer.Version)) } diff --git a/beacon-chain/rpc/apimiddleware/custom_hooks_test.go b/beacon-chain/rpc/apimiddleware/custom_hooks_test.go index fafb0c1fb2aa..f9db80bb6e74 100644 --- a/beacon-chain/rpc/apimiddleware/custom_hooks_test.go +++ b/beacon-chain/rpc/apimiddleware/custom_hooks_test.go @@ -457,6 +457,26 @@ func TestSetInitialPublishBlockPostRequest(t *testing.T) { assert.Equal(t, apimiddleware.RunDefault(true), runDefault) assert.Equal(t, reflect.TypeOf(SignedBeaconBlockBellatrixContainerJson{}).Name(), reflect.Indirect(reflect.ValueOf(endpoint.PostRequest)).Type().Name()) }) + t.Run("Capella", func(t *testing.T) { + params.SetupTestConfigCleanup(t) + cfg := params.BeaconConfig() + cfg.CapellaForkEpoch = cfg.BellatrixForkEpoch.Add(2) + params.OverrideBeaconConfig(cfg) + + slot, err := slots.EpochStart(params.BeaconConfig().CapellaForkEpoch) + require.NoError(t, err) + s.Message = struct{ Slot string }{Slot: strconv.FormatUint(uint64(slot), 10)} + j, err := json.Marshal(s) + require.NoError(t, err) + var body bytes.Buffer + _, err = body.Write(j) + require.NoError(t, err) + request := httptest.NewRequest("POST", "http://foo.example", &body) + runDefault, errJson := setInitialPublishBlockPostRequest(endpoint, nil, request) + require.Equal(t, true, errJson == nil) + assert.Equal(t, apimiddleware.RunDefault(true), runDefault) + assert.Equal(t, reflect.TypeOf(SignedBeaconBlockCapellaContainerJson{}).Name(), reflect.Indirect(reflect.ValueOf(endpoint.PostRequest)).Type().Name()) + }) } func TestPreparePublishedBlock(t *testing.T) { @@ -1100,7 +1120,35 @@ func TestSerializeProducedV2Block(t *testing.T) { require.Equal(t, nil, errJson) require.Equal(t, apimiddleware.RunDefault(false), runDefault) require.NotNil(t, j) - resp := &bellatrixProduceBlockResponseJson{} + resp := &capellaProduceBlockResponseJson{} + require.NoError(t, json.Unmarshal(j, resp)) + require.NotNil(t, resp.Data) + require.NotNil(t, resp.Data) + beaconBlock := resp.Data + assert.Equal(t, "1", beaconBlock.Slot) + assert.Equal(t, "1", beaconBlock.ProposerIndex) + assert.Equal(t, "root", beaconBlock.ParentRoot) + assert.Equal(t, "root", beaconBlock.StateRoot) + require.NotNil(t, beaconBlock.Body) + }) + t.Run("Capella", func(t *testing.T) { + response := &ProduceBlockResponseV2Json{ + Version: ethpbv2.Version_CAPELLA.String(), + Data: &BeaconBlockContainerV2Json{ + CapellaBlock: &BeaconBlockCapellaJson{ + Slot: "1", + ProposerIndex: "1", + ParentRoot: "root", + StateRoot: "root", + Body: &BeaconBlockBodyCapellaJson{}, + }, + }, + } + runDefault, j, errJson := serializeProducedV2Block(response) + require.Equal(t, nil, errJson) + require.Equal(t, apimiddleware.RunDefault(false), runDefault) + require.NotNil(t, j) + resp := &capellaProduceBlockResponseJson{} require.NoError(t, json.Unmarshal(j, resp)) require.NotNil(t, resp.Data) require.NotNil(t, resp.Data) @@ -1111,7 +1159,6 @@ func TestSerializeProducedV2Block(t *testing.T) { assert.Equal(t, "root", beaconBlock.StateRoot) require.NotNil(t, beaconBlock.Body) }) - t.Run("incorrect response type", func(t *testing.T) { response := &types.Empty{} runDefault, j, errJson := serializeProducedV2Block(response) @@ -1222,6 +1269,34 @@ func TestSerializeProduceBlindedBlock(t *testing.T) { require.NotNil(t, beaconBlock.Body) }) + t.Run("Capella", func(t *testing.T) { + response := &ProduceBlindedBlockResponseJson{ + Version: ethpbv2.Version_CAPELLA.String(), + Data: &BlindedBeaconBlockContainerJson{ + CapellaBlock: &BlindedBeaconBlockCapellaJson{ + Slot: "1", + ProposerIndex: "1", + ParentRoot: "root", + StateRoot: "root", + Body: &BlindedBeaconBlockBodyCapellaJson{}, + }, + }, + } + runDefault, j, errJson := serializeProducedBlindedBlock(response) + require.Equal(t, nil, errJson) + require.Equal(t, apimiddleware.RunDefault(false), runDefault) + require.NotNil(t, j) + resp := &capellaProduceBlindedBlockResponseJson{} + require.NoError(t, json.Unmarshal(j, resp)) + require.NotNil(t, resp.Data) + beaconBlock := resp.Data + assert.Equal(t, "1", beaconBlock.Slot) + assert.Equal(t, "1", beaconBlock.ProposerIndex) + assert.Equal(t, "root", beaconBlock.ParentRoot) + assert.Equal(t, "root", beaconBlock.StateRoot) + require.NotNil(t, beaconBlock.Body) + }) + t.Run("incorrect response type", func(t *testing.T) { response := &types.Empty{} runDefault, j, errJson := serializeProducedV2Block(response) diff --git a/beacon-chain/rpc/apimiddleware/structs.go b/beacon-chain/rpc/apimiddleware/structs.go index dc8a083522c7..83486940aa7b 100644 --- a/beacon-chain/rpc/apimiddleware/structs.go +++ b/beacon-chain/rpc/apimiddleware/structs.go @@ -400,12 +400,14 @@ type BeaconBlockContainerV2Json struct { Phase0Block *BeaconBlockJson `json:"phase0_block"` AltairBlock *BeaconBlockAltairJson `json:"altair_block"` BellatrixBlock *BeaconBlockBellatrixJson `json:"bellatrix_block"` + CapellaBlock *BeaconBlockCapellaJson `json:"capella_block"` } type BlindedBeaconBlockContainerJson struct { Phase0Block *BeaconBlockJson `json:"phase0_block"` AltairBlock *BeaconBlockAltairJson `json:"altair_block"` BellatrixBlock *BlindedBeaconBlockBellatrixJson `json:"bellatrix_block"` + CapellaBlock *BlindedBeaconBlockCapellaJson `json:"capella_block"` } type SignedBeaconBlockAltairContainerJson struct { diff --git a/beacon-chain/rpc/prysm/v1alpha1/beacon/blocks.go b/beacon-chain/rpc/prysm/v1alpha1/beacon/blocks.go index 7689f95b0110..2bad2199c84e 100644 --- a/beacon-chain/rpc/prysm/v1alpha1/beacon/blocks.go +++ b/beacon-chain/rpc/prysm/v1alpha1/beacon/blocks.go @@ -122,6 +122,20 @@ func convertToBlockContainer(blk interfaces.SignedBeaconBlock, root [32]byte, is } ctr.Block = ðpb.BeaconBlockContainer_BellatrixBlock{BellatrixBlock: rBlk} } + case version.Capella: + if blk.IsBlinded() { + rBlk, err := blk.PbBlindedCapellaBlock() + if err != nil { + return nil, err + } + ctr.Block = ðpb.BeaconBlockContainer_BlindedCapellaBlock{BlindedCapellaBlock: rBlk} + } else { + rBlk, err := blk.PbCapellaBlock() + if err != nil { + return nil, err + } + ctr.Block = ðpb.BeaconBlockContainer_CapellaBlock{CapellaBlock: rBlk} + } default: return nil, errors.Errorf("block type is not recognized: %d", blk.Version()) } diff --git a/beacon-chain/rpc/prysm/v1alpha1/beacon/blocks_test.go b/beacon-chain/rpc/prysm/v1alpha1/beacon/blocks_test.go index 5039bc72df56..be29de70bc4c 100644 --- a/beacon-chain/rpc/prysm/v1alpha1/beacon/blocks_test.go +++ b/beacon-chain/rpc/prysm/v1alpha1/beacon/blocks_test.go @@ -541,6 +541,20 @@ func TestServer_ListBeaconBlocks_Genesis(t *testing.T) { Block: ðpb.BeaconBlockContainer_BlindedBellatrixBlock{BlindedBellatrixBlock: blindedProto}} runListBlocksGenesis(t, wrapped, blkContainer) }) + t.Run("capella block", func(t *testing.T) { + parentRoot := [32]byte{'a'} + blk := util.NewBeaconBlockCapella() + blk.Block.ParentRoot = parentRoot[:] + wrapped, err := blocks.NewSignedBeaconBlock(blk) + assert.NoError(t, err) + blinded, err := wrapped.ToBlinded() + assert.NoError(t, err) + blindedProto, err := blinded.PbBlindedCapellaBlock() + assert.NoError(t, err) + blkContainer := ðpb.BeaconBlockContainer{ + Block: ðpb.BeaconBlockContainer_BlindedCapellaBlock{BlindedCapellaBlock: blindedProto}} + runListBlocksGenesis(t, wrapped, blkContainer) + }) } func runListBlocksGenesis(t *testing.T, blk interfaces.SignedBeaconBlock, blkContainer *ethpb.BeaconBlockContainer) { @@ -632,6 +646,21 @@ func TestServer_ListBeaconBlocks_Genesis_MultiBlocks(t *testing.T) { assert.NoError(t, err) runListBeaconBlocksGenesisMultiBlocks(t, gBlock, blockCreator) }) + t.Run("capella block", func(t *testing.T) { + parentRoot := [32]byte{1, 2, 3} + blk := util.NewBeaconBlockCapella() + blk.Block.ParentRoot = parentRoot[:] + blockCreator := func(i types.Slot) interfaces.SignedBeaconBlock { + b := util.NewBeaconBlockCapella() + b.Block.Slot = i + wrappedB, err := blocks.NewSignedBeaconBlock(b) + assert.NoError(t, err) + return wrappedB + } + gBlock, err := blocks.NewSignedBeaconBlock(blk) + assert.NoError(t, err) + runListBeaconBlocksGenesisMultiBlocks(t, gBlock, blockCreator) + }) } func runListBeaconBlocksGenesisMultiBlocks(t *testing.T, genBlock interfaces.SignedBeaconBlock, @@ -739,6 +768,30 @@ func TestServer_ListBeaconBlocks_Pagination(t *testing.T) { assert.NoError(t, err) runListBeaconBlocksPagination(t, orphanedB, blockCreator, containerCreator) }) + t.Run("capella block", func(t *testing.T) { + blk := util.NewBeaconBlockCapella() + blk.Block.Slot = 300 + blockCreator := func(i types.Slot) interfaces.SignedBeaconBlock { + b := util.NewBeaconBlockCapella() + b.Block.Slot = i + wrappedB, err := blocks.NewSignedBeaconBlock(b) + assert.NoError(t, err) + return wrappedB + } + containerCreator := func(i types.Slot, root []byte, canonical bool) *ethpb.BeaconBlockContainer { + b := util.NewBeaconBlockCapella() + b.Block.Slot = i + ctr := ðpb.BeaconBlockContainer{ + Block: ðpb.BeaconBlockContainer_CapellaBlock{ + CapellaBlock: util.HydrateSignedBeaconBlockCapella(b)}, + BlockRoot: root, + Canonical: canonical} + return ctr + } + orphanedB, err := blocks.NewSignedBeaconBlock(blk) + assert.NoError(t, err) + runListBeaconBlocksPagination(t, orphanedB, blockCreator, containerCreator) + }) } func runListBeaconBlocksPagination(t *testing.T, orphanedBlk interfaces.SignedBeaconBlock, @@ -853,3 +906,23 @@ func runListBeaconBlocksPagination(t *testing.T, orphanedBlk interfaces.SignedBe }) } } + +func TestServer_ConvertToBlockContainer(t *testing.T) { + b := util.NewBeaconBlockCapella() + root, err := b.HashTreeRoot() + require.NoError(t, err) + wrapped, err := blocks.NewSignedBeaconBlock(b) + assert.NoError(t, err) + container, err := convertToBlockContainer(wrapped, root, true) + require.NoError(t, err) + require.NotNil(t, container.GetCapellaBlock()) + + bb := util.NewBlindedBeaconBlockCapella() + root, err = b.HashTreeRoot() + require.NoError(t, err) + wrapped, err = blocks.NewSignedBeaconBlock(bb) + assert.NoError(t, err) + container, err = convertToBlockContainer(wrapped, root, true) + require.NoError(t, err) + require.NotNil(t, container.GetBlindedCapellaBlock()) +}