mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 12:57:18 +00:00
00dacbd00d
* Remove custom block body root and block root methods * Add nil checks and fix tests * Fmt * Typo
53 lines
1.9 KiB
Go
53 lines
1.9 KiB
Go
package migration
|
|
|
|
import (
|
|
"github.com/golang/protobuf/proto"
|
|
"github.com/pkg/errors"
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1"
|
|
ethpb_alpha "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
)
|
|
|
|
// V1Alpha1BlockToV1BlockHeader converts a v1alpha1 SignedBeaconBlock proto to a v1 SignedBeaconBlockHeader proto.
|
|
func V1Alpha1BlockToV1BlockHeader(block *ethpb_alpha.SignedBeaconBlock) (*ethpb.SignedBeaconBlockHeader, error) {
|
|
bodyRoot, err := block.Block.Body.HashTreeRoot()
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed to get body root of block")
|
|
}
|
|
return ðpb.SignedBeaconBlockHeader{
|
|
Header: ðpb.BeaconBlockHeader{
|
|
Slot: block.Block.Slot,
|
|
ProposerIndex: block.Block.ProposerIndex,
|
|
ParentRoot: block.Block.ParentRoot,
|
|
StateRoot: block.Block.StateRoot,
|
|
BodyRoot: bodyRoot[:],
|
|
},
|
|
Signature: block.Signature,
|
|
}, nil
|
|
}
|
|
|
|
// V1Alpha1BlockToV1Block converts a v1alpha1 SignedBeaconBlock proto to a v1 proto.
|
|
func V1Alpha1ToV1Block(alphaBlk *ethpb_alpha.SignedBeaconBlock) (*ethpb.SignedBeaconBlock, error) {
|
|
marshaledBlk, err := alphaBlk.Marshal()
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not marshal block")
|
|
}
|
|
v1Block := ðpb.SignedBeaconBlock{}
|
|
if err := proto.Unmarshal(marshaledBlk, v1Block); err != nil {
|
|
return nil, errors.Wrap(err, "could not unmarshal block")
|
|
}
|
|
return v1Block, nil
|
|
}
|
|
|
|
// V1ToV1Alpha1Block converts a v1 SignedBeaconBlock proto to a v1alpha1 proto.
|
|
func V1ToV1Alpha1Block(alphaBlk *ethpb.SignedBeaconBlock) (*ethpb_alpha.SignedBeaconBlock, error) {
|
|
marshaledBlk, err := alphaBlk.Marshal()
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not marshal block")
|
|
}
|
|
v1alpha1Block := ðpb_alpha.SignedBeaconBlock{}
|
|
if err := proto.Unmarshal(marshaledBlk, v1alpha1Block); err != nil {
|
|
return nil, errors.Wrap(err, "could not unmarshal block")
|
|
}
|
|
return v1alpha1Block, nil
|
|
}
|