2020-05-20 15:23:22 +00:00
|
|
|
package blockutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/pkg/errors"
|
2021-07-15 16:03:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/proto/interfaces"
|
2021-07-21 21:34:07 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2020-05-20 15:23:22 +00:00
|
|
|
)
|
|
|
|
|
2020-06-23 16:46:48 +00:00
|
|
|
// SignedBeaconBlockHeaderFromBlock function to retrieve signed block header from block.
|
2020-05-20 15:23:22 +00:00
|
|
|
func SignedBeaconBlockHeaderFromBlock(block *ethpb.SignedBeaconBlock) (*ethpb.SignedBeaconBlockHeader, error) {
|
2020-12-08 22:27:58 +00:00
|
|
|
if block.Block == nil || block.Block.Body == nil {
|
|
|
|
return nil, errors.New("nil block")
|
|
|
|
}
|
2020-12-09 18:11:42 +00:00
|
|
|
|
|
|
|
bodyRoot, err := block.Block.Body.HashTreeRoot()
|
2020-05-20 15:23:22 +00:00
|
|
|
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
|
|
|
|
}
|
2020-06-23 16:46:48 +00:00
|
|
|
|
2021-07-15 16:03:46 +00:00
|
|
|
// SignedBeaconBlockHeaderFromBlockInterface function to retrieve signed block header from block.
|
|
|
|
func SignedBeaconBlockHeaderFromBlockInterface(sb interfaces.SignedBeaconBlock) (*ethpb.SignedBeaconBlockHeader, error) {
|
|
|
|
b := sb.Block()
|
|
|
|
if b.IsNil() || b.Body().IsNil() {
|
|
|
|
return nil, errors.New("nil block")
|
|
|
|
}
|
|
|
|
|
|
|
|
h, err := BeaconBlockHeaderFromBlockInterface(b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to get block header of block")
|
|
|
|
}
|
|
|
|
return ðpb.SignedBeaconBlockHeader{
|
|
|
|
Header: h,
|
|
|
|
Signature: sb.Signature(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-06-23 16:46:48 +00:00
|
|
|
// BeaconBlockHeaderFromBlock function to retrieve block header from block.
|
|
|
|
func BeaconBlockHeaderFromBlock(block *ethpb.BeaconBlock) (*ethpb.BeaconBlockHeader, error) {
|
2020-12-08 22:27:58 +00:00
|
|
|
if block.Body == nil {
|
|
|
|
return nil, errors.New("nil block body")
|
|
|
|
}
|
2020-12-09 18:11:42 +00:00
|
|
|
|
|
|
|
bodyRoot, err := block.Body.HashTreeRoot()
|
2020-06-23 16:46:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to get body root of block")
|
|
|
|
}
|
|
|
|
return ðpb.BeaconBlockHeader{
|
|
|
|
Slot: block.Slot,
|
|
|
|
ProposerIndex: block.ProposerIndex,
|
|
|
|
ParentRoot: block.ParentRoot,
|
|
|
|
StateRoot: block.StateRoot,
|
|
|
|
BodyRoot: bodyRoot[:],
|
|
|
|
}, nil
|
|
|
|
}
|
2021-07-15 16:03:46 +00:00
|
|
|
|
|
|
|
// BeaconBlockHeaderFromBlockInterface function to retrieve block header from block.
|
|
|
|
func BeaconBlockHeaderFromBlockInterface(block interfaces.BeaconBlock) (*ethpb.BeaconBlockHeader, error) {
|
|
|
|
if block.Body().IsNil() {
|
|
|
|
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 ðpb.BeaconBlockHeader{
|
|
|
|
Slot: block.Slot(),
|
|
|
|
ProposerIndex: block.ProposerIndex(),
|
|
|
|
ParentRoot: block.ParentRoot(),
|
|
|
|
StateRoot: block.StateRoot(),
|
|
|
|
BodyRoot: bodyRoot[:],
|
|
|
|
}, nil
|
|
|
|
}
|