Skip to content

Commit

Permalink
Capella coverage and json structs (#11854)
Browse files Browse the repository at this point in the history
* Capella coverage and json structs

* use SetupTestConfig
  • Loading branch information
potuz authored Jan 9, 2023
1 parent 4c9a0bf commit bdc3a6e
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 3 deletions.
24 changes: 23 additions & 1 deletion beacon-chain/rpc/apimiddleware/custom_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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))
}
Expand Down Expand Up @@ -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))
}
Expand Down
79 changes: 77 additions & 2 deletions beacon-chain/rpc/apimiddleware/custom_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions beacon-chain/rpc/apimiddleware/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions beacon-chain/rpc/prysm/v1alpha1/beacon/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,20 @@ func convertToBlockContainer(blk interfaces.SignedBeaconBlock, root [32]byte, is
}
ctr.Block = &ethpb.BeaconBlockContainer_BellatrixBlock{BellatrixBlock: rBlk}
}
case version.Capella:
if blk.IsBlinded() {
rBlk, err := blk.PbBlindedCapellaBlock()
if err != nil {
return nil, err
}
ctr.Block = &ethpb.BeaconBlockContainer_BlindedCapellaBlock{BlindedCapellaBlock: rBlk}
} else {
rBlk, err := blk.PbCapellaBlock()
if err != nil {
return nil, err
}
ctr.Block = &ethpb.BeaconBlockContainer_CapellaBlock{CapellaBlock: rBlk}
}
default:
return nil, errors.Errorf("block type is not recognized: %d", blk.Version())
}
Expand Down
73 changes: 73 additions & 0 deletions beacon-chain/rpc/prysm/v1alpha1/beacon/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,20 @@ func TestServer_ListBeaconBlocks_Genesis(t *testing.T) {
Block: &ethpb.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 := &ethpb.BeaconBlockContainer{
Block: &ethpb.BeaconBlockContainer_BlindedCapellaBlock{BlindedCapellaBlock: blindedProto}}
runListBlocksGenesis(t, wrapped, blkContainer)
})
}

func runListBlocksGenesis(t *testing.T, blk interfaces.SignedBeaconBlock, blkContainer *ethpb.BeaconBlockContainer) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 := &ethpb.BeaconBlockContainer{
Block: &ethpb.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,
Expand Down Expand Up @@ -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())
}

0 comments on commit bdc3a6e

Please sign in to comment.