Skip to content

Commit

Permalink
Merge branch 'beacon_state_deposits_cleanup' into check-genesis-deps-el
Browse files Browse the repository at this point in the history
  • Loading branch information
calbera committed Dec 20, 2024
2 parents 13e19f0 + 45821be commit 36f013e
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 181 deletions.
2 changes: 1 addition & 1 deletion beacon/blockchain/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (s *Service[
)
}

if err = s.depositStore.EnqueueDeposits(deposits); err != nil {
if err = s.storageBackend.DepositStore().EnqueueDeposits(deposits); err != nil {
s.logger.Error("Failed to store deposits", "error", err)
s.failedBlocksMu.Lock()
s.failedBlocks[blockNum] = struct{}{}
Expand Down
2 changes: 1 addition & 1 deletion beacon/blockchain/finalize_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (s *Service[

// store the finalized block in the KVStore.
slot := blk.GetSlot()
if err = s.blockStore.Set(blk); err != nil {
if err = s.storageBackend.BlockStore().Set(blk); err != nil {
s.logger.Error(
"failed to store block", "slot", slot, "error", err,
)
Expand Down
24 changes: 19 additions & 5 deletions beacon/blockchain/init_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,21 @@ func (s *Service[
return nil, err
}

// Verify that the genesis deposits root are same between CL and EL.
genesisDepositsCL := genesisData.GetDeposits()
genesisDepositsRootCL := genesisDepositsCL.HashTreeRoot()
genesisDepositsRootEL, err := s.depositContract.GetGenesisDepositsRoot(ctx, 0)
if err != nil {
s.logger.Error("Failed to get genesis deposits root", "error", err)
s.logger.Error("Failed to get genesis deposits root from EL", "error", err)
return nil, err
}
if !genesisDepositsRootCL.Equals(genesisDepositsRootEL) {
if !genesisDepositsCL.HashTreeRoot().Equals(genesisDepositsRootEL) {
return nil, ErrGenesisDepositsRootMismatch
}

// Verify that the genesis deposits count are same between CL and EL.
genesisDepositsCountEL, err := s.depositContract.GetDepositsCount(ctx, 0)
if err != nil {
s.logger.Error("Failed to get genesis deposits count", "error", err)
s.logger.Error("Failed to get genesis deposits count from EL", "error", err)
return nil, err
}
if uint64(len(genesisDepositsCL)) != genesisDepositsCountEL {
Expand All @@ -62,10 +63,23 @@ func (s *Service[
)
}

return s.stateProcessor.InitializePreminedBeaconStateFromEth1(
// Initialize the beacon state from the genesis data.
validatorUpdates, err := s.stateProcessor.InitializePreminedBeaconStateFromEth1(
s.storageBackend.StateFromContext(ctx),
genesisDepositsCL,
genesisData.GetExecutionPayloadHeader(),
genesisData.GetForkVersion(),
)
if err != nil {
return nil, err
}

// After deposits are validated, store the genesis deposits in the deposit store.
if err = s.storageBackend.DepositStore().EnqueueDeposits(
genesisData.GetDeposits(),
); err != nil {
return nil, err
}

return validatorUpdates, nil
}
74 changes: 15 additions & 59 deletions beacon/blockchain/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,10 @@ type Service[
] struct {
// homeDir is the directory for config and data"
homeDir string
// storageBackend represents the backend storage for beacon states and
// associated sidecars.
storageBackend StorageBackend[
AvailabilityStoreT,
BeaconStateT,
DepositStoreT,
]
blobProcessor da.BlobProcessor[
AvailabilityStoreT,
ConsensusSidecarsT,
BlobSidecarsT,
]
// store is the block store for the service.
// TODO: Remove this and use the block store from the storage backend.
blockStore BlockStoreT
// depositStore is the deposit store that stores deposits.
depositStore deposit.Store
// storageBackend represents the backend storage for not state-enforced data.
storageBackend StorageBackend[AvailabilityStoreT, BeaconStateT, BlockStoreT, DepositStoreT]
// blobProcessor is used for processing sidecars.
blobProcessor da.BlobProcessor[AvailabilityStoreT, ConsensusSidecarsT, BlobSidecarsT]
// depositContract is the contract interface for interacting with the
// deposit contract.
depositContract deposit.Contract
Expand All @@ -86,11 +73,7 @@ type Service[
// localBuilder is a local builder for constructing new beacon states.
localBuilder LocalBuilder[BeaconStateT]
// stateProcessor is the state processor for beacon blocks and states.
stateProcessor StateProcessor[
BeaconBlockT,
BeaconStateT,
*transition.Context,
]
stateProcessor StateProcessor[BeaconBlockT, BeaconStateT, *transition.Context]
// metrics is the metrics for the service.
metrics *chainMetrics
// optimisticPayloadBuilds is a flag used when the optimistic payload
Expand All @@ -113,49 +96,28 @@ func NewService[
BlobSidecarsT BlobSidecars[BlobSidecarsT],
](
homeDir string,
storageBackend StorageBackend[
AvailabilityStoreT,
BeaconStateT,
DepositStoreT,
],
blobProcessor da.BlobProcessor[
AvailabilityStoreT,
ConsensusSidecarsT,
BlobSidecarsT,
],
blockStore BlockStoreT,
depositStore deposit.Store,
storageBackend StorageBackend[AvailabilityStoreT, BeaconStateT, BlockStoreT, DepositStoreT],
blobProcessor da.BlobProcessor[AvailabilityStoreT, ConsensusSidecarsT, BlobSidecarsT],
depositContract deposit.Contract,
eth1FollowDistance math.U64,
logger log.Logger,
chainSpec chain.ChainSpec,
executionEngine ExecutionEngine,
localBuilder LocalBuilder[BeaconStateT],
stateProcessor StateProcessor[
BeaconBlockT,
BeaconStateT,
*transition.Context,
],
stateProcessor StateProcessor[BeaconBlockT, BeaconStateT, *transition.Context],
telemetrySink TelemetrySink,
optimisticPayloadBuilds bool,
) *Service[
AvailabilityStoreT, DepositStoreT,
ConsensusBlockT, BeaconBlockT,
BeaconStateT, BlockStoreT,
GenesisT,
ConsensusSidecarsT, BlobSidecarsT,
AvailabilityStoreT, DepositStoreT, ConsensusBlockT, BeaconBlockT, BeaconStateT,
BlockStoreT, GenesisT, ConsensusSidecarsT, BlobSidecarsT,
] {
return &Service[
AvailabilityStoreT, DepositStoreT,
ConsensusBlockT, BeaconBlockT,
BeaconStateT, BlockStoreT,
GenesisT, ConsensusSidecarsT, BlobSidecarsT,
AvailabilityStoreT, DepositStoreT, ConsensusBlockT, BeaconBlockT,
BeaconStateT, BlockStoreT, GenesisT, ConsensusSidecarsT, BlobSidecarsT,
]{
homeDir: homeDir,
storageBackend: storageBackend,
blobProcessor: blobProcessor,
blockStore: blockStore,
depositStore: depositStore,
depositContract: depositContract,
eth1FollowDistance: eth1FollowDistance,
failedBlocks: make(map[math.Slot]struct{}),
Expand All @@ -171,23 +133,17 @@ func NewService[
}

// Name returns the name of the service.
func (s *Service[
_, _, _, _, _, _, _, _, _,
]) Name() string {
func (s *Service[_, _, _, _, _, _, _, _, _]) Name() string {
return "blockchain"
}

func (s *Service[
_, _, _, _, _, _, _, _, _,
]) Start(ctx context.Context) error {
func (s *Service[_, _, _, _, _, _, _, _, _]) Start(ctx context.Context) error {
// Catchup deposits for failed blocks.
go s.depositCatchupFetcher(ctx)

return nil
}

func (s *Service[
_, _, _, _, _, _, _, _, _,
]) Stop() error {
func (s *Service[_, _, _, _, _, _, _, _, _]) Stop() error {
return nil
}
3 changes: 3 additions & 0 deletions beacon/blockchain/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ type StateProcessor[
type StorageBackend[
AvailabilityStoreT any,
BeaconStateT any,
BlockStoreT any,
DepositStoreT any,
] interface {
// AvailabilityStore returns the availability store for the given context.
Expand All @@ -210,6 +211,8 @@ type StorageBackend[
StateFromContext(context.Context) BeaconStateT
// DepositStore retrieves the deposit store.
DepositStore() DepositStoreT
// BlockStore retrieves the block store.
BlockStore() BlockStoreT
}

// TelemetrySink is an interface for sending metrics to a telemetry backend.
Expand Down
34 changes: 16 additions & 18 deletions beacon/validator/block_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
payloadtime "github.com/berachain/beacon-kit/beacon/payload-time"
ctypes "github.com/berachain/beacon-kit/consensus-types/types"
"github.com/berachain/beacon-kit/consensus/types"
"github.com/berachain/beacon-kit/errors"
"github.com/berachain/beacon-kit/primitives/bytes"
"github.com/berachain/beacon-kit/primitives/common"
"github.com/berachain/beacon-kit/primitives/crypto"
Expand Down Expand Up @@ -311,32 +312,29 @@ func (s *Service[
return ErrNilDepositIndexStart
}

blkDeposits, err := s.sb.DepositStore().GetDepositsByIndex(
depositIndex, s.chainSpec.MaxDepositsPerBlock(),
// Grab all previous deposits from genesis up to the current index + max deposits per block.
deposits, err := s.sb.DepositStore().GetDepositsByIndex(
0, depositIndex+s.chainSpec.MaxDepositsPerBlock(),
)
if err != nil {
return err
}

// Set the deposits on the block body.
var eth1Data *ctypes.Eth1Data
body.SetEth1Data(eth1Data.New(deposits.HashTreeRoot(), 0, common.ExecutionHash{}))

// Set just the block deposits (after current index) on the block body.
if uint64(len(deposits)) < depositIndex {
return errors.Wrapf(ErrDepositStoreIncomplete,
"all historical deposits not available, expected: %d, got: %d",
depositIndex, len(deposits),
)
}
s.logger.Info(
"Building block body with local deposits",
"start_index", depositIndex, "num_deposits", len(blkDeposits),
"start_index", depositIndex, "num_deposits", uint64(len(deposits))-depositIndex,
)
body.SetDeposits(blkDeposits)

deposits, err := s.sb.DepositStore().GetDepositsByIndex(0, depositIndex)
if err != nil {
return err
}
deposits = append(deposits, blkDeposits...)

var eth1Data *ctypes.Eth1Data
body.SetEth1Data(eth1Data.New(
deposits.HashTreeRoot(),
0,
common.ExecutionHash{},
))
body.SetDeposits(deposits[depositIndex:])

// Set the graffiti on the block body.
sizedGraffiti := bytes.ExtendToSize([]byte(s.cfg.Graffiti), bytes.B32Size)
Expand Down
4 changes: 4 additions & 0 deletions beacon/validator/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ var (
// ErrNilDepositIndexStart is an error for when the deposit index start is
// nil.
ErrNilDepositIndexStart = errors.New("nil deposit index start")

// ErrDepositStoreIncomplete is an error for when the deposit store has not returned
// the expected amount of deposits. Could be due to pruning when it should not be enabled.
ErrDepositStoreIncomplete = errors.New("deposits from deposit store incomplete")
)
2 changes: 1 addition & 1 deletion cmd/beacond/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func DefaultComponents() []any {
*AvailabilityStore,
*ConsensusBlock, *BeaconBlock,
*BeaconState, *BeaconStateMarshallable,
*BlobSidecar, *BlobSidecars, *ConsensusSidecars, *BlockStore,
*BlobSidecar, *BlobSidecars, *ConsensusSidecars,
*DepositStore, *DepositContract,
*Genesis,
*KVStore, *Logger, *StorageBackend, *BlockStore,
Expand Down
9 changes: 0 additions & 9 deletions consensus-types/types/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,6 @@ func (d *Deposit) GetTree() (*fastssz.Node, error) {
/* Getters and Setters */
/* -------------------------------------------------------------------------- */

// Equals returns true if the Deposit is equal to the other.
func (d *Deposit) Equals(rhs *Deposit) bool {
return d.Pubkey == rhs.Pubkey &&
d.Credentials == rhs.Credentials &&
d.Amount == rhs.Amount &&
d.Signature == rhs.Signature &&
d.Index == rhs.Index
}

// GetAmount returns the deposit amount in gwei.
func (d *Deposit) GetAmount() math.Gwei {
return d.Amount
Expand Down
1 change: 1 addition & 0 deletions consensus-types/types/deposits.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@ func (ds Deposits) DefineSSZ(c *ssz.Codec) {

// HashTreeRoot returns the hash tree root of the Deposits.
func (ds Deposits) HashTreeRoot() common.Root {
// TODO: determine if using HashConcurrent optimizes performance.
return ssz.HashSequential(ds)
}
Loading

0 comments on commit 36f013e

Please sign in to comment.