mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-27 21:57:16 +00:00
51 lines
1.6 KiB
Go
51 lines
1.6 KiB
Go
// Package blocks contains block processing libraries. These libraries
|
|
// process and verify block specific messages such as PoW receipt root,
|
|
// RANDAO, validator deposits, exits and slashing proofs.
|
|
package blocks
|
|
|
|
import (
|
|
"github.com/pkg/errors"
|
|
"github.com/prysmaticlabs/go-ssz"
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
)
|
|
|
|
// NewGenesisBlock returns the canonical, genesis block for the beacon chain protocol.
|
|
func NewGenesisBlock(stateRoot []byte) *ethpb.BeaconBlock {
|
|
zeroHash := params.BeaconConfig().ZeroHash[:]
|
|
genBlock := ðpb.BeaconBlock{
|
|
ParentRoot: zeroHash,
|
|
StateRoot: stateRoot,
|
|
Body: ðpb.BeaconBlockBody{},
|
|
Signature: params.BeaconConfig().EmptySignature[:],
|
|
}
|
|
return genBlock
|
|
}
|
|
|
|
// BlockFromHeader manufactures a block from its header. It contains all its fields,
|
|
// except for the block body.
|
|
func BlockFromHeader(header *ethpb.BeaconBlockHeader) *ethpb.BeaconBlock {
|
|
return ðpb.BeaconBlock{
|
|
StateRoot: header.StateRoot,
|
|
Slot: header.Slot,
|
|
Signature: header.Signature,
|
|
ParentRoot: header.ParentRoot,
|
|
}
|
|
}
|
|
|
|
// HeaderFromBlock extracts the block header from a block.
|
|
func HeaderFromBlock(block *ethpb.BeaconBlock) (*ethpb.BeaconBlockHeader, error) {
|
|
header := ðpb.BeaconBlockHeader{
|
|
Slot: block.Slot,
|
|
ParentRoot: block.ParentRoot,
|
|
Signature: block.Signature,
|
|
StateRoot: block.StateRoot,
|
|
}
|
|
root, err := ssz.HashTreeRoot(block.Body)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not tree hash block body")
|
|
}
|
|
header.BodyRoot = root[:]
|
|
return header, nil
|
|
}
|