diff --git a/changelog/james-prysm_handle-pbgeneric-error.md b/changelog/james-prysm_handle-pbgeneric-error.md new file mode 100644 index 0000000000..a5ee15ed31 --- /dev/null +++ b/changelog/james-prysm_handle-pbgeneric-error.md @@ -0,0 +1,3 @@ +### Fixed + +- fix panic with type cast on pbgenericblock() diff --git a/consensus-types/blocks/getters.go b/consensus-types/blocks/getters.go index 99f9dbf5cd..cb99e52ec1 100644 --- a/consensus-types/blocks/getters.go +++ b/consensus-types/blocks/getters.go @@ -124,8 +124,12 @@ func (b *SignedBeaconBlock) PbGenericBlock() (*eth.GenericSignedBeaconBlock, err Block: ð.GenericSignedBeaconBlock_BlindedDeneb{BlindedDeneb: pb.(*eth.SignedBlindedBeaconBlockDeneb)}, }, nil } + bc, ok := pb.(*eth.SignedBeaconBlockContentsDeneb) + if !ok { + return nil, fmt.Errorf("PbGenericBlock() only supports block content type but got %T", pb) + } return ð.GenericSignedBeaconBlock{ - Block: ð.GenericSignedBeaconBlock_Deneb{Deneb: pb.(*eth.SignedBeaconBlockContentsDeneb)}, + Block: ð.GenericSignedBeaconBlock_Deneb{Deneb: bc}, }, nil case version.Electra: if b.IsBlinded() { @@ -133,8 +137,12 @@ func (b *SignedBeaconBlock) PbGenericBlock() (*eth.GenericSignedBeaconBlock, err Block: ð.GenericSignedBeaconBlock_BlindedElectra{BlindedElectra: pb.(*eth.SignedBlindedBeaconBlockElectra)}, }, nil } + bc, ok := pb.(*eth.SignedBeaconBlockContentsElectra) + if !ok { + return nil, fmt.Errorf("PbGenericBlock() only supports block content type but got %T", pb) + } return ð.GenericSignedBeaconBlock{ - Block: ð.GenericSignedBeaconBlock_Electra{Electra: pb.(*eth.SignedBeaconBlockContentsElectra)}, + Block: ð.GenericSignedBeaconBlock_Electra{Electra: bc}, }, nil case version.Fulu: if b.IsBlinded() { @@ -142,8 +150,12 @@ func (b *SignedBeaconBlock) PbGenericBlock() (*eth.GenericSignedBeaconBlock, err Block: ð.GenericSignedBeaconBlock_BlindedFulu{BlindedFulu: pb.(*eth.SignedBlindedBeaconBlockFulu)}, }, nil } + bc, ok := pb.(*eth.SignedBeaconBlockContentsFulu) + if !ok { + return nil, fmt.Errorf("PbGenericBlock() only supports block content type but got %T", pb) + } return ð.GenericSignedBeaconBlock{ - Block: ð.GenericSignedBeaconBlock_Fulu{Fulu: pb.(*eth.SignedBeaconBlockContentsFulu)}, + Block: ð.GenericSignedBeaconBlock_Fulu{Fulu: bc}, }, nil default: return nil, errIncorrectBlockVersion diff --git a/testing/util/block_test.go b/testing/util/block_test.go index c95d3d0a92..77e2872af9 100644 --- a/testing/util/block_test.go +++ b/testing/util/block_test.go @@ -322,3 +322,30 @@ func TestGenerateVoluntaryExits(t *testing.T) { require.NoError(t, err) require.NoError(t, coreBlock.VerifyExitAndSignature(val, beaconState, exit)) } + +func Test_PostDenebPbGenericBlock_ErrorsForPlainBlock(t *testing.T) { + t.Run("Deneb block returns type error", func(t *testing.T) { + eb := NewBeaconBlockDeneb() + b, err := blocks.NewSignedBeaconBlock(eb) + require.NoError(t, err) + + _, err = b.PbGenericBlock() + require.ErrorContains(t, "PbGenericBlock() only supports block content type but got", err) + }) + t.Run("Electra block returns type error", func(t *testing.T) { + eb := NewBeaconBlockElectra() + b, err := blocks.NewSignedBeaconBlock(eb) + require.NoError(t, err) + + _, err = b.PbGenericBlock() + require.ErrorContains(t, "PbGenericBlock() only supports block content type but got", err) + }) + t.Run("Fulu block returns type error", func(t *testing.T) { + eb := NewBeaconBlockFulu() + b, err := blocks.NewSignedBeaconBlock(eb) + require.NoError(t, err) + + _, err = b.PbGenericBlock() + require.ErrorContains(t, "PbGenericBlock() only supports block content type but got", err) + }) +}