mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 15:37:56 -05:00
allow calling Proto on nil
This commit is contained in:
@@ -10,7 +10,7 @@ import (
|
||||
// Proto returns the underlying protobuf signed beacon block.
|
||||
func (b *SignedBeaconBlock) Proto() (proto.Message, error) {
|
||||
if b == nil {
|
||||
return nil, errNilBlock
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
blockMessage, err := b.block.Proto()
|
||||
@@ -20,36 +20,52 @@ func (b *SignedBeaconBlock) Proto() (proto.Message, error) {
|
||||
|
||||
switch b.version {
|
||||
case version.Phase0:
|
||||
block, ok := blockMessage.(*eth.BeaconBlock)
|
||||
if !ok {
|
||||
return nil, errors.Wrap(err, incorrectBlockVersion)
|
||||
var block *eth.BeaconBlock
|
||||
if blockMessage != nil {
|
||||
var ok bool
|
||||
block, ok = blockMessage.(*eth.BeaconBlock)
|
||||
if !ok {
|
||||
return nil, errors.Wrap(err, incorrectBlockVersion)
|
||||
}
|
||||
}
|
||||
return ð.SignedBeaconBlock{
|
||||
Block: block,
|
||||
Signature: b.signature,
|
||||
}, nil
|
||||
case version.Altair:
|
||||
block, ok := blockMessage.(*eth.BeaconBlockAltair)
|
||||
if !ok {
|
||||
return nil, errors.Wrap(err, incorrectBlockVersion)
|
||||
var block *eth.BeaconBlockAltair
|
||||
if blockMessage != nil {
|
||||
var ok bool
|
||||
block, ok = blockMessage.(*eth.BeaconBlockAltair)
|
||||
if !ok {
|
||||
return nil, errors.Wrap(err, incorrectBlockVersion)
|
||||
}
|
||||
}
|
||||
return ð.SignedBeaconBlockAltair{
|
||||
Block: block,
|
||||
Signature: b.signature,
|
||||
}, nil
|
||||
case version.Bellatrix:
|
||||
block, ok := blockMessage.(*eth.BeaconBlockBellatrix)
|
||||
if !ok {
|
||||
return nil, errors.Wrap(err, incorrectBlockVersion)
|
||||
var block *eth.BeaconBlockBellatrix
|
||||
if blockMessage != nil {
|
||||
var ok bool
|
||||
block, ok = blockMessage.(*eth.BeaconBlockBellatrix)
|
||||
if !ok {
|
||||
return nil, errors.Wrap(err, incorrectBlockVersion)
|
||||
}
|
||||
}
|
||||
return ð.SignedBeaconBlockBellatrix{
|
||||
Block: block,
|
||||
Signature: b.signature,
|
||||
}, nil
|
||||
case version.BellatrixBlind:
|
||||
block, ok := blockMessage.(*eth.BlindedBeaconBlockBellatrix)
|
||||
if !ok {
|
||||
return nil, errors.Wrap(err, incorrectBlockVersion)
|
||||
var block *eth.BlindedBeaconBlockBellatrix
|
||||
if blockMessage != nil {
|
||||
var ok bool
|
||||
block, ok = blockMessage.(*eth.BlindedBeaconBlockBellatrix)
|
||||
if !ok {
|
||||
return nil, errors.Wrap(err, incorrectBlockVersion)
|
||||
}
|
||||
}
|
||||
return ð.SignedBlindedBeaconBlockBellatrix{
|
||||
Block: block,
|
||||
@@ -63,7 +79,7 @@ func (b *SignedBeaconBlock) Proto() (proto.Message, error) {
|
||||
// Proto returns the underlying protobuf beacon block.
|
||||
func (b *BeaconBlock) Proto() (proto.Message, error) {
|
||||
if b == nil {
|
||||
return nil, errNilBlock
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
bodyMessage, err := b.body.Proto()
|
||||
@@ -73,9 +89,13 @@ func (b *BeaconBlock) Proto() (proto.Message, error) {
|
||||
|
||||
switch b.version {
|
||||
case version.Phase0:
|
||||
body, ok := bodyMessage.(*eth.BeaconBlockBody)
|
||||
if !ok {
|
||||
return nil, errors.Wrap(err, incorrectBodyVersion)
|
||||
var body *eth.BeaconBlockBody
|
||||
if bodyMessage != nil {
|
||||
var ok bool
|
||||
body, ok = bodyMessage.(*eth.BeaconBlockBody)
|
||||
if !ok {
|
||||
return nil, errors.Wrap(err, incorrectBodyVersion)
|
||||
}
|
||||
}
|
||||
return ð.BeaconBlock{
|
||||
Slot: b.slot,
|
||||
@@ -85,9 +105,13 @@ func (b *BeaconBlock) Proto() (proto.Message, error) {
|
||||
Body: body,
|
||||
}, nil
|
||||
case version.Altair:
|
||||
body, ok := bodyMessage.(*eth.BeaconBlockBodyAltair)
|
||||
if !ok {
|
||||
return nil, errors.Wrap(err, incorrectBodyVersion)
|
||||
var body *eth.BeaconBlockBodyAltair
|
||||
if bodyMessage != nil {
|
||||
var ok bool
|
||||
body, ok = bodyMessage.(*eth.BeaconBlockBodyAltair)
|
||||
if !ok {
|
||||
return nil, errors.Wrap(err, incorrectBodyVersion)
|
||||
}
|
||||
}
|
||||
return ð.BeaconBlockAltair{
|
||||
Slot: b.slot,
|
||||
@@ -97,9 +121,13 @@ func (b *BeaconBlock) Proto() (proto.Message, error) {
|
||||
Body: body,
|
||||
}, nil
|
||||
case version.Bellatrix:
|
||||
body, ok := bodyMessage.(*eth.BeaconBlockBodyBellatrix)
|
||||
if !ok {
|
||||
return nil, errors.Wrap(err, incorrectBodyVersion)
|
||||
var body *eth.BeaconBlockBodyBellatrix
|
||||
if bodyMessage != nil {
|
||||
var ok bool
|
||||
body, ok = bodyMessage.(*eth.BeaconBlockBodyBellatrix)
|
||||
if !ok {
|
||||
return nil, errors.Wrap(err, incorrectBodyVersion)
|
||||
}
|
||||
}
|
||||
return ð.BeaconBlockBellatrix{
|
||||
Slot: b.slot,
|
||||
@@ -109,9 +137,13 @@ func (b *BeaconBlock) Proto() (proto.Message, error) {
|
||||
Body: body,
|
||||
}, nil
|
||||
case version.BellatrixBlind:
|
||||
body, ok := bodyMessage.(*eth.BlindedBeaconBlockBodyBellatrix)
|
||||
if !ok {
|
||||
return nil, errors.Wrap(err, incorrectBodyVersion)
|
||||
var body *eth.BlindedBeaconBlockBodyBellatrix
|
||||
if bodyMessage != nil {
|
||||
var ok bool
|
||||
body, ok = bodyMessage.(*eth.BlindedBeaconBlockBodyBellatrix)
|
||||
if !ok {
|
||||
return nil, errors.Wrap(err, incorrectBodyVersion)
|
||||
}
|
||||
}
|
||||
return ð.BlindedBeaconBlockBellatrix{
|
||||
Slot: b.slot,
|
||||
@@ -128,7 +160,7 @@ func (b *BeaconBlock) Proto() (proto.Message, error) {
|
||||
// Proto returns the underlying protobuf beacon block body.
|
||||
func (b *BeaconBlockBody) Proto() (proto.Message, error) {
|
||||
if b == nil {
|
||||
return nil, errNilBody
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
switch b.version {
|
||||
|
||||
@@ -26,7 +26,6 @@ var (
|
||||
// ErrUnsupportedGetter is returned when a getter access is not supported for a specific beacon block version.
|
||||
ErrUnsupportedGetter = errors.New("unsupported getter")
|
||||
errNilBlock = errors.New("received nil beacon block")
|
||||
errNilBody = errors.New("received nil beacon block body")
|
||||
errIncorrectBlockVersion = errors.New(incorrectBlockVersion)
|
||||
errIncorrectBodyVersion = errors.New(incorrectBodyVersion)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user