adding in error for generic check (#14801)

This commit is contained in:
james-prysm
2025-01-15 15:36:09 -06:00
committed by GitHub
parent e07341e1d5
commit 8f43f6cc84
3 changed files with 45 additions and 3 deletions

View File

@@ -0,0 +1,3 @@
### Fixed
- fix panic with type cast on pbgenericblock()

View File

@@ -124,8 +124,12 @@ func (b *SignedBeaconBlock) PbGenericBlock() (*eth.GenericSignedBeaconBlock, err
Block: &eth.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 &eth.GenericSignedBeaconBlock{
Block: &eth.GenericSignedBeaconBlock_Deneb{Deneb: pb.(*eth.SignedBeaconBlockContentsDeneb)},
Block: &eth.GenericSignedBeaconBlock_Deneb{Deneb: bc},
}, nil
case version.Electra:
if b.IsBlinded() {
@@ -133,8 +137,12 @@ func (b *SignedBeaconBlock) PbGenericBlock() (*eth.GenericSignedBeaconBlock, err
Block: &eth.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 &eth.GenericSignedBeaconBlock{
Block: &eth.GenericSignedBeaconBlock_Electra{Electra: pb.(*eth.SignedBeaconBlockContentsElectra)},
Block: &eth.GenericSignedBeaconBlock_Electra{Electra: bc},
}, nil
case version.Fulu:
if b.IsBlinded() {
@@ -142,8 +150,12 @@ func (b *SignedBeaconBlock) PbGenericBlock() (*eth.GenericSignedBeaconBlock, err
Block: &eth.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 &eth.GenericSignedBeaconBlock{
Block: &eth.GenericSignedBeaconBlock_Fulu{Fulu: pb.(*eth.SignedBeaconBlockContentsFulu)},
Block: &eth.GenericSignedBeaconBlock_Fulu{Fulu: bc},
}, nil
default:
return nil, errIncorrectBlockVersion

View File

@@ -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)
})
}