prysm-pulse/shared/blockutil/block_utils.go
Raul Jordan 5aac06f04e
Move EthereumAPIs Into Prysm (#8968)
* begin move

* use same import path

* imports

* regen protos

* regen

* no rename

* generate ssz

* gaz

* fmt

* edit build file

* imports

* modify

* remove generated files

* remove protos

* edit imports in prysm

* beacon chain all builds

* edit script

* add generated pbs

* add replace rules

* license for ethereumapis protos

* change visibility

* fmt

* update build files to gaz ignore

* use proper form

* edit imports

* wrap block

* revert scripts

* revert go mod
2021-06-02 18:49:52 -05:00

48 lines
1.4 KiB
Go

package blockutil
import (
"github.com/pkg/errors"
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
)
// SignedBeaconBlockHeaderFromBlock function to retrieve signed block header from block.
func SignedBeaconBlockHeaderFromBlock(block *ethpb.SignedBeaconBlock) (*ethpb.SignedBeaconBlockHeader, error) {
if block.Block == nil || block.Block.Body == nil {
return nil, errors.New("nil block")
}
bodyRoot, err := block.Block.Body.HashTreeRoot()
if err != nil {
return nil, errors.Wrap(err, "failed to get body root of block")
}
return &ethpb.SignedBeaconBlockHeader{
Header: &ethpb.BeaconBlockHeader{
Slot: block.Block.Slot,
ProposerIndex: block.Block.ProposerIndex,
ParentRoot: block.Block.ParentRoot,
StateRoot: block.Block.StateRoot,
BodyRoot: bodyRoot[:],
},
Signature: block.Signature,
}, nil
}
// BeaconBlockHeaderFromBlock function to retrieve block header from block.
func BeaconBlockHeaderFromBlock(block *ethpb.BeaconBlock) (*ethpb.BeaconBlockHeader, error) {
if block.Body == nil {
return nil, errors.New("nil block body")
}
bodyRoot, err := block.Body.HashTreeRoot()
if err != nil {
return nil, errors.Wrap(err, "failed to get body root of block")
}
return &ethpb.BeaconBlockHeader{
Slot: block.Slot,
ProposerIndex: block.ProposerIndex,
ParentRoot: block.ParentRoot,
StateRoot: block.StateRoot,
BodyRoot: bodyRoot[:],
}, nil
}