mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-23 11:57:18 +00:00
152 lines
5.2 KiB
Go
152 lines
5.2 KiB
Go
// Package blocks contains block processing libraries according to
|
|
// the Ethereum beacon chain spec.
|
|
package blocks
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/prysmaticlabs/prysm/v3/beacon-chain/state"
|
|
fieldparams "github.com/prysmaticlabs/prysm/v3/config/fieldparams"
|
|
"github.com/prysmaticlabs/prysm/v3/config/params"
|
|
"github.com/prysmaticlabs/prysm/v3/consensus-types/blocks"
|
|
"github.com/prysmaticlabs/prysm/v3/consensus-types/interfaces"
|
|
"github.com/prysmaticlabs/prysm/v3/encoding/bytesutil"
|
|
enginev1 "github.com/prysmaticlabs/prysm/v3/proto/engine/v1"
|
|
ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1"
|
|
)
|
|
|
|
// NewGenesisBlock returns the canonical, genesis block for the beacon chain protocol.
|
|
func NewGenesisBlock(stateRoot []byte) *ethpb.SignedBeaconBlock {
|
|
zeroHash := params.BeaconConfig().ZeroHash[:]
|
|
block := ðpb.SignedBeaconBlock{
|
|
Block: ðpb.BeaconBlock{
|
|
ParentRoot: zeroHash,
|
|
StateRoot: bytesutil.PadTo(stateRoot, 32),
|
|
Body: ðpb.BeaconBlockBody{
|
|
RandaoReveal: make([]byte, fieldparams.BLSSignatureLength),
|
|
Eth1Data: ðpb.Eth1Data{
|
|
DepositRoot: make([]byte, 32),
|
|
BlockHash: make([]byte, 32),
|
|
},
|
|
Graffiti: make([]byte, 32),
|
|
},
|
|
},
|
|
Signature: params.BeaconConfig().EmptySignature[:],
|
|
}
|
|
return block
|
|
}
|
|
|
|
var ErrUnrecognizedState = errors.New("unknown underlying type for state.BeaconState value")
|
|
|
|
func NewGenesisBlockForState(ctx context.Context, st state.BeaconState) (interfaces.ReadOnlySignedBeaconBlock, error) {
|
|
root, err := st.HashTreeRoot(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ps := st.ToProto()
|
|
switch ps.(type) {
|
|
case *ethpb.BeaconState:
|
|
return blocks.NewSignedBeaconBlock(ðpb.SignedBeaconBlock{
|
|
Block: ðpb.BeaconBlock{
|
|
ParentRoot: params.BeaconConfig().ZeroHash[:],
|
|
StateRoot: root[:],
|
|
Body: ðpb.BeaconBlockBody{
|
|
RandaoReveal: make([]byte, fieldparams.BLSSignatureLength),
|
|
Eth1Data: ðpb.Eth1Data{
|
|
DepositRoot: make([]byte, 32),
|
|
BlockHash: make([]byte, 32),
|
|
},
|
|
Graffiti: make([]byte, 32),
|
|
},
|
|
},
|
|
Signature: params.BeaconConfig().EmptySignature[:],
|
|
})
|
|
case *ethpb.BeaconStateAltair:
|
|
return blocks.NewSignedBeaconBlock(ðpb.SignedBeaconBlockAltair{
|
|
Block: ðpb.BeaconBlockAltair{
|
|
ParentRoot: params.BeaconConfig().ZeroHash[:],
|
|
StateRoot: root[:],
|
|
Body: ðpb.BeaconBlockBodyAltair{
|
|
RandaoReveal: make([]byte, fieldparams.BLSSignatureLength),
|
|
Eth1Data: ðpb.Eth1Data{
|
|
DepositRoot: make([]byte, 32),
|
|
BlockHash: make([]byte, 32),
|
|
},
|
|
Graffiti: make([]byte, 32),
|
|
SyncAggregate: ðpb.SyncAggregate{
|
|
SyncCommitteeBits: make([]byte, fieldparams.SyncCommitteeLength/8),
|
|
SyncCommitteeSignature: make([]byte, fieldparams.BLSSignatureLength),
|
|
},
|
|
},
|
|
},
|
|
Signature: params.BeaconConfig().EmptySignature[:],
|
|
})
|
|
case *ethpb.BeaconStateBellatrix:
|
|
return blocks.NewSignedBeaconBlock(ðpb.SignedBeaconBlockBellatrix{
|
|
Block: ðpb.BeaconBlockBellatrix{
|
|
ParentRoot: params.BeaconConfig().ZeroHash[:],
|
|
StateRoot: root[:],
|
|
Body: ðpb.BeaconBlockBodyBellatrix{
|
|
RandaoReveal: make([]byte, 96),
|
|
Eth1Data: ðpb.Eth1Data{
|
|
DepositRoot: make([]byte, 32),
|
|
BlockHash: make([]byte, 32),
|
|
},
|
|
Graffiti: make([]byte, 32),
|
|
SyncAggregate: ðpb.SyncAggregate{
|
|
SyncCommitteeBits: make([]byte, fieldparams.SyncCommitteeLength/8),
|
|
SyncCommitteeSignature: make([]byte, fieldparams.BLSSignatureLength),
|
|
},
|
|
ExecutionPayload: &enginev1.ExecutionPayload{
|
|
ParentHash: make([]byte, 32),
|
|
FeeRecipient: make([]byte, 20),
|
|
StateRoot: make([]byte, 32),
|
|
ReceiptsRoot: make([]byte, 32),
|
|
LogsBloom: make([]byte, 256),
|
|
PrevRandao: make([]byte, 32),
|
|
BaseFeePerGas: make([]byte, 32),
|
|
BlockHash: make([]byte, 32),
|
|
Transactions: make([][]byte, 0),
|
|
},
|
|
},
|
|
},
|
|
Signature: params.BeaconConfig().EmptySignature[:],
|
|
})
|
|
case *ethpb.BeaconStateCapella:
|
|
return blocks.NewSignedBeaconBlock(ðpb.SignedBeaconBlockCapella{
|
|
Block: ðpb.BeaconBlockCapella{
|
|
ParentRoot: params.BeaconConfig().ZeroHash[:],
|
|
StateRoot: root[:],
|
|
Body: ðpb.BeaconBlockBodyCapella{
|
|
RandaoReveal: make([]byte, 96),
|
|
Eth1Data: ðpb.Eth1Data{
|
|
DepositRoot: make([]byte, 32),
|
|
BlockHash: make([]byte, 32),
|
|
},
|
|
Graffiti: make([]byte, 32),
|
|
SyncAggregate: ðpb.SyncAggregate{
|
|
SyncCommitteeBits: make([]byte, fieldparams.SyncCommitteeLength/8),
|
|
SyncCommitteeSignature: make([]byte, fieldparams.BLSSignatureLength),
|
|
},
|
|
ExecutionPayload: &enginev1.ExecutionPayloadCapella{
|
|
ParentHash: make([]byte, 32),
|
|
FeeRecipient: make([]byte, 20),
|
|
StateRoot: make([]byte, 32),
|
|
ReceiptsRoot: make([]byte, 32),
|
|
LogsBloom: make([]byte, 256),
|
|
PrevRandao: make([]byte, 32),
|
|
BaseFeePerGas: make([]byte, 32),
|
|
BlockHash: make([]byte, 32),
|
|
Transactions: make([][]byte, 0),
|
|
Withdrawals: make([]*enginev1.Withdrawal, 0),
|
|
},
|
|
},
|
|
},
|
|
Signature: params.BeaconConfig().EmptySignature[:],
|
|
})
|
|
default:
|
|
return nil, ErrUnrecognizedState
|
|
}
|
|
}
|