Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proper Beacon State Repressentation attempt #6254

Merged
merged 3 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions cl/cltypes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,8 @@ type SignedAggregateAndProof struct {
Signature [96]byte `ssz-size:"96"`
}

// BeaconState is used to create the initial store through checkpoint sync.
// we only use FinalizedCheckpoint field.
type BeaconState struct {
// BellatrixBeaconState is the bellatrix beacon state.
type BeaconStateBellatrix struct {
GenesisTime uint64
GenesisValidatorsRoot [32]byte `ssz-size:"32"`
Slot uint64
Expand Down Expand Up @@ -339,7 +338,7 @@ type BeaconState struct {
}

// BlockRoot retrieves a the state block root from the state.
func (b *BeaconState) BlockRoot() ([32]byte, error) {
func (b *BeaconStateBellatrix) BlockRoot() ([32]byte, error) {
stateRoot, err := b.HashTreeRoot()
if err != nil {
return [32]byte{}, nil
Expand Down
26 changes: 13 additions & 13 deletions cl/cltypes/types_encoding.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions cmd/erigon-cl/core/checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/ledgerwatch/log/v3"
)

func RetrieveBeaconState(ctx context.Context, uri string) (*cltypes.BeaconState, error) {
func RetrieveBeaconState(ctx context.Context, uri string) (*cltypes.BeaconStateBellatrix, error) {
log.Info("[Checkpoint Sync] Requesting beacon state", "uri", uri)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
Expand All @@ -36,7 +36,7 @@ func RetrieveBeaconState(ctx context.Context, uri string) (*cltypes.BeaconState,
return nil, fmt.Errorf("checkpoint sync failed %s", err)
}

beaconState := &cltypes.BeaconState{}
beaconState := &cltypes.BeaconStateBellatrix{}
err = beaconState.UnmarshalSSZ(marshaled)
if err != nil {
return nil, fmt.Errorf("checkpoint sync failed %s", err)
Expand Down
6 changes: 3 additions & 3 deletions cmd/erigon-cl/core/rawdb/accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func EncodeNumber(n uint64) []byte {
}

// WriteBeaconState writes beacon state for specific block to database.
func WriteBeaconState(tx kv.Putter, state *cltypes.BeaconState) error {
func WriteBeaconState(tx kv.Putter, state *cltypes.BeaconStateBellatrix) error {
data, err := utils.EncodeSSZSnappy(state)
if err != nil {
return err
Expand All @@ -33,12 +33,12 @@ func WriteBeaconState(tx kv.Putter, state *cltypes.BeaconState) error {
}

// ReadBeaconState reads beacon state for specific block from database.
func ReadBeaconState(tx kv.Getter, slot uint64) (*cltypes.BeaconState, error) {
func ReadBeaconState(tx kv.Getter, slot uint64) (*cltypes.BeaconStateBellatrix, error) {
data, err := tx.GetOne(kv.BeaconState, EncodeNumber(slot))
if err != nil {
return nil, err
}
state := &cltypes.BeaconState{}
state := &cltypes.BeaconStateBellatrix{}

if len(data) == 0 {
return nil, nil
Expand Down
142 changes: 142 additions & 0 deletions cmd/erigon-cl/core/state/getters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package state

import (
"github.com/ledgerwatch/erigon/cl/cltypes"
"github.com/ledgerwatch/erigon/common"
)

// Just a bunch of simple getters.

func (b *BeaconState) GenesisTime() uint64 {
return b.genesisTime
}

func (b *BeaconState) GenesisValidatorsRoot() common.Hash {
return b.genesisValidatorsRoot
}

func (b *BeaconState) Slot() uint64 {
return b.slot
}

func (b *BeaconState) Fork() *cltypes.Fork {
return b.fork
}

func (b *BeaconState) LatestBlockHeader() *cltypes.BeaconBlockHeader {
return b.latestBlockHeader
}

func (b *BeaconState) BlockRoots() [][32]byte {
return b.blockRoots
}

func (b *BeaconState) StateRoots() [][32]byte {
return b.stateRoots
}

func (b *BeaconState) HistoricalRoots() [][32]byte {
return b.historicalRoots
}

func (b *BeaconState) Eth1Data() *cltypes.Eth1Data {
return b.eth1Data
}

func (b *BeaconState) Eth1DataVotes() []*cltypes.Eth1Data {
return b.eth1DataVotes
}

func (b *BeaconState) Eth1DepositIndex() uint64 {
return b.eth1DepositIndex
}

func (b *BeaconState) Validators() []*cltypes.Validator {
return b.validators
}

func (b *BeaconState) Balances() []uint64 {
return b.balances
}

func (b *BeaconState) RandaoMixes() [][32]byte {
return b.randaoMixes
}

func (b *BeaconState) Slashings() []uint64 {
return b.slashings
}

func (b *BeaconState) PreviousEpochParticipation() []byte {
return b.previousEpochParticipation
}

func (b *BeaconState) CurrentEpochParticipation() []byte {
return b.currentEpochParticipation
}

func (b *BeaconState) JustificationBits() []byte {
return b.justificationBits
}

func (b *BeaconState) PreviousJustifiedCheckpoint() *cltypes.Checkpoint {
return b.previousJustifiedCheckpoint
}

func (b *BeaconState) CurrentJustifiedCheckpoint() *cltypes.Checkpoint {
return b.currentJustifiedCheckpoint
}

func (b *BeaconState) FinalizedCheckpoint() *cltypes.Checkpoint {
return b.finalizedCheckpoint
}

func (b *BeaconState) CurrentSyncCommittee() *cltypes.SyncCommittee {
return b.currentSyncCommittee
}

func (b *BeaconState) NextSyncCommittee() *cltypes.SyncCommittee {
return b.nextSyncCommittee
}

func (b *BeaconState) LatestExecutionPayloadHeader() *cltypes.ExecutionHeader {
return b.latestExecutionPayloadHeader
}

// GetStateSSZObject allows us to use ssz methods.
func (b *BeaconState) GetStateSSZObject() cltypes.ObjectSSZ {
switch b.version {
case BellatrixVersion:
return &cltypes.BeaconStateBellatrix{
GenesisTime: b.genesisTime,
GenesisValidatorsRoot: b.genesisValidatorsRoot,
Slot: b.slot,
Fork: b.fork,
LatestBlockHeader: b.latestBlockHeader,
BlockRoots: b.blockRoots,
StateRoots: b.stateRoots,
HistoricalRoots: b.historicalRoots,
Eth1Data: b.eth1Data,
Eth1DataVotes: b.eth1DataVotes,
Eth1DepositIndex: b.eth1DepositIndex,
Validators: b.validators,
Balances: b.balances,
RandaoMixes: b.randaoMixes,
Slashings: b.slashings,
PreviousEpochParticipation: b.previousEpochParticipation,
CurrentEpochParticipation: b.currentEpochParticipation,
JustificationBits: b.justificationBits,
FinalizedCheckpoint: b.finalizedCheckpoint,
CurrentJustifiedCheckpoint: b.currentJustifiedCheckpoint,
PreviousJustifiedCheckpoint: b.previousJustifiedCheckpoint,
InactivityScores: b.inactivityScores,
CurrentSyncCommittee: b.currentSyncCommittee,
NextSyncCommittee: b.nextSyncCommittee,
LatestExecutionPayloadHeader: b.latestExecutionPayloadHeader,
}
case CapellaVersion:
panic("not implemented")
default:
panic("not a valid version")
}
}
33 changes: 33 additions & 0 deletions cmd/erigon-cl/core/state/leaf_indexes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package state

// All position of all the leaves of the state merkle tree.
const (
GenesisTimeLeafIndex = 0
GenesisValidatorsRootLeafIndex = 1
SlotLeafIndex = 2
ForkLeafIndex = 3
LatestBlockHeaderLeafIndex = 4
BlockRootsLeafIndex = 5
StateRootsLeafIndex = 6
HistoricalRootsLeafIndex = 7
Eth1DataLeafIndex = 8
Eth1DataVotesLeafIndex = 9
Eth1DepositIndexLeafIndex = 10
ValidatorsLeafIndex = 11
BalancesLeafIndex = 12
RandaoMixesLeafIndex = 13
SlashingsLeafIndex = 14
PreviousEpochParticipationLeafIndex = 15
CurrentEpochParticipationLeafIndex = 16
JustificationBitsLeafIndex = 17
PreviousJustifiedCheckpointLeafIndex = 18
CurrentJustifiedCheckpointLeafIndex = 19
FinalizedCheckpointLeafIndex = 20
InactivityScoresLeafIndex = 21
CurrentSyncCommitteeLeafIndex = 22
NextSyncCommitteeLeafIndex = 23
LatestExecutionPayloadHeaderLeafIndex = 24

// Leaves sizes
BellatrixLeavesSize = 25
)
Loading