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

[WIP 5/n] feat(deposits): deposit system by block height #2292

Draft
wants to merge 8 commits into
base: dep-mt-sm
Choose a base branch
from
Draft
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
13 changes: 8 additions & 5 deletions beacon/blockchain/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,22 @@ const defaultRetryInterval = 20 * time.Second
func (s *Service[
_, _, _, _, _, _, _, _, _,
]) depositFetcher(ctx context.Context, blockNum math.U64) {
if blockNum <= s.eth1FollowDistance {
eth1FollowDistance := math.U64(s.chainSpec.Eth1FollowDistance())
if blockNum <= eth1FollowDistance {
s.logger.Info(
"depositFetcher, nothing to fetch",
"block num", blockNum, "eth1FollowDistance", s.eth1FollowDistance,
"block_num", blockNum, "eth1_follow_distance", eth1FollowDistance,
)
return
}

s.fetchAndStoreDeposits(ctx, blockNum-s.eth1FollowDistance)
s.fetchAndStoreDeposits(ctx, blockNum-eth1FollowDistance)
}

func (s *Service[
_, _, _, _, _, _, _, _, _,
]) fetchAndStoreDeposits(ctx context.Context, blockNum math.U64) {
deposits, _, err := s.depositContract.ReadDeposits(ctx, blockNum)
deposits, indexes, executionHash, err := s.depositContract.ReadDeposits(ctx, blockNum)
if err != nil {
s.logger.Error("Failed to read deposits", "error", err)
s.metrics.sink.IncrementCounter(
Expand All @@ -67,7 +68,9 @@ func (s *Service[
)
}

if err = s.depositStore.EnqueueDepositDatas(deposits); err != nil {
if err = s.depositStore.EnqueueDepositDatas(
deposits, indexes, executionHash, blockNum,
); err != nil {
s.logger.Error("Failed to store deposits", "error", err)
s.failedBlocksMu.Lock()
s.failedBlocks[blockNum] = struct{}{}
Expand Down
25 changes: 23 additions & 2 deletions beacon/blockchain/init_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,36 @@ func (s *Service[
return nil, err
}

genesisDeposits := genesisData.GetDepositDatas()
// Store the genesis deposits.
genesisDepositDatas := genesisData.GetDepositDatas()
genesisDepositIndexes := make([]uint64, len(genesisDepositDatas))
for i := range genesisDepositDatas {
genesisDepositIndexes[i] = uint64(i)
}
genesisExecutionPayloadHeader := genesisData.GetExecutionPayloadHeader()
if err := s.depositStore.EnqueueDepositDatas(genesisDeposits); err != nil {
if err := s.depositStore.EnqueueDepositDatas(
genesisDepositDatas,
genesisDepositIndexes,
genesisExecutionPayloadHeader.BlockHash,
genesisExecutionPayloadHeader.Number,
); err != nil {
s.logger.Error("Failed to store genesis deposits", "error", err)
return nil, err
}

// Get the genesis deposits, with their proofs.
genesisDeposits, genesisDepositsRoot, err := s.depositStore.GetDepositsByIndex(
0, uint64(len(genesisDepositDatas)),
)
if err != nil {
s.logger.Error("Failed to retrieve genesis deposits with proofs", "error", err)
return nil, err
}

return s.stateProcessor.InitializePreminedBeaconStateFromEth1(
s.storageBackend.StateFromContext(ctx),
genesisDeposits,
genesisDepositsRoot,
genesisExecutionPayloadHeader,
genesisData.GetForkVersion(),
)
Expand Down
30 changes: 7 additions & 23 deletions beacon/blockchain/pruning.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package blockchain

import (
"github.com/berachain/beacon-kit/chain-spec/chain"
ctypes "github.com/berachain/beacon-kit/consensus-types/types"
)

func (s *Service[
Expand All @@ -35,35 +34,20 @@ func (s *Service[
return err
}

// prune deposit store
start, end = depositPruneRangeFn(beaconBlk.GetBody().GetDepositDatas(), s.chainSpec)
err = s.depositStore.Prune(start, end)

if err != nil {
// Delete the deposits of the block previous to the one we just stored.
// TODO: remove this here. Pruning should be invoked when we know for sure that
// the deposits were included in a block.
finalizedEthBlock := beaconBlk.GetBody().GetExecutionPayload().Number.Unwrap()
height := finalizedEthBlock - s.chainSpec.Eth1FollowDistance() - 1
if err = s.depositStore.Prune(height); err != nil {
return err
}

return nil
}

// depositPruneRangeFn effectively prunes at most the max deposits per block
// behind the last included deposit.
func depositPruneRangeFn(deposits []*ctypes.DepositData, cs chain.ChainSpec) (uint64, uint64) {
if len(deposits) == 0 || cs.MaxDepositsPerBlock() == 0 {
return 0, 0
}

index := deposits[len(deposits)-1].GetIndex().Unwrap()

if index < cs.MaxDepositsPerBlock() {
return 0, index
}
return index - cs.MaxDepositsPerBlock(), index
}

//nolint:unparam // this is ok
func availabilityPruneRangeFn(
slot uint64, cs chain.ChainSpec) (uint64, uint64) {
func availabilityPruneRangeFn(slot uint64, cs chain.ChainSpec) (uint64, uint64) {
window := cs.MinEpochsForBlobsSidecarsRequest() * cs.SlotsPerEpoch()
if slot < window {
return 0, 0
Expand Down
4 changes: 0 additions & 4 deletions beacon/blockchain/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ type Service[
// depositContract is the contract interface for interacting with the
// deposit contract.
depositContract deposit.Contract
// eth1FollowDistance is the follow distance for Ethereum 1.0 blocks.
eth1FollowDistance math.U64
// failedBlocksMu protects failedBlocks for concurrent access.
failedBlocksMu sync.RWMutex
// failedBlocks is a map of blocks that failed to be processed
Expand Down Expand Up @@ -105,7 +103,6 @@ func NewService[
blockStore BlockStoreT,
depositStore deposit.Store,
depositContract deposit.Contract,
eth1FollowDistance math.U64,
logger log.Logger,
chainSpec chain.ChainSpec,
executionEngine ExecutionEngine[PayloadAttributesT],
Expand All @@ -127,7 +124,6 @@ func NewService[
blockStore: blockStore,
depositStore: depositStore,
depositContract: depositContract,
eth1FollowDistance: eth1FollowDistance,
failedBlocks: make(map[math.Slot]struct{}),
logger: logger,
chainSpec: chainSpec,
Expand Down
2 changes: 1 addition & 1 deletion beacon/blockchain/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ type StateProcessor[
// state
// from the eth1 deposits.
InitializePreminedBeaconStateFromEth1(
BeaconStateT, *ctypes.ExecutionPayloadHeader, common.Version,
BeaconStateT, ctypes.Deposits, common.Root, *ctypes.ExecutionPayloadHeader, common.Version,
) (transition.ValidatorUpdates, error)
// ProcessSlots processes the state transition for a range of slots.
ProcessSlots(
Expand Down
1 change: 0 additions & 1 deletion beacon/validator/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ package validator
import "github.com/berachain/beacon-kit/errors"

var (

// ErrNilPayload is an error for when there is no payload
// in a beacon block.
ErrNilPayload = errors.New("nil payload in beacon block")
Expand Down
20 changes: 5 additions & 15 deletions cli/commands/genesis/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,31 +59,21 @@ func CollectGenesisDepositsCmd() *cobra.Command {
return err
}

var deposits []*types.DepositData
if deposits, err = CollectValidatorJSONFiles(
filepath.Join(config.RootDir, "config", "premined-deposits"),
appGenesis,
genesisInfo := &types.Genesis{}

if genesisInfo.DepositDatas, err = CollectValidatorJSONFiles(
filepath.Join(config.RootDir, "config", "premined-deposits"), appGenesis,
); err != nil {
return errors.Wrap(
err,
"failed to collect validator json files",
)
}

genesisInfo := &types.Genesis{}

if err = json.Unmarshal(
appGenesisState["beacon"], genesisInfo,
); err != nil {
if err = json.Unmarshal(appGenesisState["beacon"], genesisInfo); err != nil {
return errors.Wrap(err, "failed to unmarshal beacon genesis")
}

for i, deposit := range deposits {
//#nosec:G701 // won't realistically overflow.
deposit.Index = uint64(i)
genesisInfo.DepositDatas = append(genesisInfo.DepositDatas, deposit)
}

appGenesisState["beacon"], err = json.Marshal(genesisInfo)
if err != nil {
return errors.Wrap(err, "failed to marshal beacon genesis")
Expand Down
4 changes: 1 addition & 3 deletions cmd/beacond/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
*ConsensusBlock, *BeaconBlock,
*BeaconState, *BeaconStateMarshallable,
*BlobSidecar, *BlobSidecars, *ConsensusSidecars, *BlockStore,
*DepositStore, *DepositContract,

Check failure on line 47 in cmd/beacond/defaults.go

View workflow job for this annotation

GitHub Actions / nilaway

*DepositStore does not satisfy "github.com/berachain/beacon-kit/node-core/components".DepositStore (wrong type for method GetDepositsByIndex)
*Genesis,
*KVStore, *Logger, *StorageBackend, *BlockStore,
],
Expand Down Expand Up @@ -72,15 +72,13 @@
*BlockStore, *BeaconState,
*BeaconStateMarshallable,
*ConsensusSidecars, *BlobSidecar, *BlobSidecars,
*DepositStore,

Check failure on line 75 in cmd/beacond/defaults.go

View workflow job for this annotation

GitHub Actions / nilaway

*DepositStore does not satisfy "github.com/berachain/beacon-kit/node-core/components".DepositStore (wrong type for method GetDepositsByIndex)
*Genesis, *KVStore, *Logger,
NodeAPIContext,
],
components.ProvideSidecarFactory[*BeaconBlock],
components.ProvideStateProcessor[
*Logger, *BeaconBlock,
*BeaconState, *BeaconStateMarshallable, *DepositStore,
*KVStore,
*Logger, *BeaconBlock, *BeaconState, *BeaconStateMarshallable, *DepositStore, *KVStore,

Check failure on line 81 in cmd/beacond/defaults.go

View workflow job for this annotation

GitHub Actions / nilaway

*DepositStore does not satisfy "github.com/berachain/beacon-kit/node-core/components".DepositStore (wrong type for method GetDepositsByIndex)
],
components.ProvideKVStore,
components.ProvideStorageBackend[
Expand All @@ -93,7 +91,7 @@
components.ProvideValidatorService[
*AvailabilityStore, *BeaconBlock,
*BeaconState, *BeaconStateMarshallable,
*BlockStore, *BlobSidecar, *BlobSidecars, *DepositStore,

Check failure on line 94 in cmd/beacond/defaults.go

View workflow job for this annotation

GitHub Actions / nilaway

*DepositStore does not satisfy "github.com/berachain/beacon-kit/node-core/components".DepositStore (wrong type for method GetDepositsByIndex)
*KVStore, *Logger,
*StorageBackend,
],
Expand All @@ -107,7 +105,7 @@
components.ProvideNodeAPIBackend[
*AvailabilityStore, *BeaconBlock,
*BlockStore, *BeaconState,
*BeaconStateMarshallable, *BlobSidecars, *DepositStore,

Check failure on line 108 in cmd/beacond/defaults.go

View workflow job for this annotation

GitHub Actions / nilaway

*DepositStore does not satisfy "github.com/berachain/beacon-kit/node-core/components".DepositStore (wrong type for method GetDepositsByIndex)
*KVStore, *CometBFTService, *StorageBackend,
],
)
Expand Down
3 changes: 0 additions & 3 deletions cmd/beacond/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
*BeaconState,
*BlobSidecar,
*BlobSidecars,
*DepositStore,

Check failure on line 146 in cmd/beacond/types.go

View workflow job for this annotation

GitHub Actions / nilaway

*DepositStore does not satisfy "github.com/berachain/beacon-kit/beacon/validator".DepositStore (wrong type for method GetDepositsByIndex)
]
)

Expand Down Expand Up @@ -218,7 +218,7 @@
*BlobSidecars,
*BlockStore,
sdk.Context,
*DepositStore,

Check failure on line 221 in cmd/beacond/types.go

View workflow job for this annotation

GitHub Actions / nilaway

*DepositStore does not satisfy backend.DepositStore (wrong type for method GetDepositsByIndex)
*CometBFTService,
*KVStore,
*StorageBackend,
Expand Down Expand Up @@ -253,7 +253,4 @@
type (
// DAPruner is a type alias for the DA pruner.
DAPruner = pruner.Pruner[*IndexDB]

// DepositPruner is a type alias for the deposit pruner.
DepositPruner = pruner.Pruner[*DepositStore]
)
4 changes: 1 addition & 3 deletions consensus-types/types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ func generateValidBeaconBlock() *types.BeaconBlock {
Eth1Data: &types.Eth1Data{},
Deposits: []*types.Deposit{
{
Data: &types.DepositData{
Index: 1,
},
Data: &types.DepositData{Amount: 1},
},
},
BlobKzgCommitments: []eip4844.KZGCommitment{
Expand Down
27 changes: 2 additions & 25 deletions consensus-types/types/deposit_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
)

// DepositDataSize is the size of the SSZ encoding of a DepositData.
const DepositDataSize = 192 // 48 + 32 + 8 + 96 + 8
const DepositDataSize = 184 // 48 + 32 + 8 + 96

// Compile-time assertions to ensure Deposit implements necessary interfaces.
var (
Expand All @@ -50,8 +50,6 @@ type DepositData struct {
Amount math.Gwei `json:"amount"`
// Signature of the deposit data.
Signature crypto.BLSSignature `json:"signature"`
// Index of the deposit in the deposit contract.
Index uint64 `json:"index"`
}

// NewDepositData creates a new DepositData instance.
Expand All @@ -60,14 +58,12 @@ func NewDepositData(
credentials WithdrawalCredentials,
amount math.Gwei,
signature crypto.BLSSignature,
index uint64,
) *DepositData {
return &DepositData{
Pubkey: pubkey,
Credentials: credentials,
Amount: amount,
Signature: signature,
Index: index,
}
}

Expand All @@ -82,10 +78,9 @@ func (d *DepositData) New(
credentials WithdrawalCredentials,
amount math.Gwei,
signature crypto.BLSSignature,
index uint64,
) *DepositData {
return NewDepositData(
pubkey, credentials, amount, signature, index,
pubkey, credentials, amount, signature,
)
}

Expand Down Expand Up @@ -117,7 +112,6 @@ func (d *DepositData) DefineSSZ(c *ssz.Codec) {
ssz.DefineStaticBytes(c, &d.Credentials)
ssz.DefineUint64(c, &d.Amount)
ssz.DefineStaticBytes(c, &d.Signature)
ssz.DefineUint64(c, &d.Index)
}

// MarshalSSZ marshals the DepositData object to SSZ format.
Expand Down Expand Up @@ -171,9 +165,6 @@ func (d *DepositData) HashTreeRootWith(hh fastssz.HashWalker) error {
// Field (3) 'Signature'
hh.PutBytes(d.Signature[:])

// Field (4) 'Index'
hh.PutUint64(d.Index)

hh.Merkleize(indx)
return nil
}
Expand All @@ -187,15 +178,6 @@ func (d *DepositData) GetTree() (*fastssz.Node, error) {
/* Getters and Setters */
/* -------------------------------------------------------------------------- */

// Equals returns true if the DepositData is equal to the other.
func (d *DepositData) Equals(rhs *DepositData) 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 *DepositData) GetAmount() math.Gwei {
return d.Amount
Expand All @@ -206,11 +188,6 @@ func (d *DepositData) GetPubkey() crypto.BLSPubkey {
return d.Pubkey
}

// GetIndex returns the index of the deposit in the deposit contract.
func (d *DepositData) GetIndex() math.U64 {
return math.U64(d.Index)
}

// GetSignature returns the signature of the deposit data.
func (d *DepositData) GetSignature() crypto.BLSSignature {
return d.Signature
Expand Down
4 changes: 0 additions & 4 deletions consensus-types/types/deposit_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,12 @@ func generateValidDeposit() *types.DepositData {
var signature crypto.BLSSignature
var credentials types.WithdrawalCredentials
amount := math.Gwei(32)
index := uint64(1)

return &types.DepositData{
Pubkey: pubKey,
Credentials: credentials,
Amount: amount,
Signature: signature,
Index: index,
}
}

Expand All @@ -57,7 +55,6 @@ func TestDeposit_New(t *testing.T) {
deposit.Credentials,
deposit.Amount,
deposit.Signature,
deposit.Index,
)
require.Equal(t, deposit, newDeposit)
}
Expand Down Expand Up @@ -150,5 +147,4 @@ func TestDeposit_Getters(t *testing.T) {
require.Equal(t, deposit.Credentials, deposit.GetWithdrawalCredentials())
require.Equal(t, deposit.Amount, deposit.GetAmount())
require.Equal(t, deposit.Signature, deposit.GetSignature())
require.Equal(t, math.U64(deposit.Index), deposit.GetIndex())
}
Loading
Loading