Skip to content

Commit

Permalink
Merge branch 'dep-mt-sm' of github.com:berachain/beacon-kit into deps…
Browse files Browse the repository at this point in the history
…-robust
  • Loading branch information
calbera committed Dec 18, 2024
2 parents 199e5ad + 0762070 commit 8599bc9
Show file tree
Hide file tree
Showing 14 changed files with 153 additions and 89 deletions.
2 changes: 1 addition & 1 deletion beacon/validator/block_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ func (s *Service[
executionPayload = envelope.GetExecutionPayload()
)
body.SetEth1Data(eth1Data.New(
depositRoot, math.U64(depositIndex-1+uint64(len(deposits))), executionPayload.BlockHash,
depositRoot, math.U64(depositIndex+uint64(len(deposits))), executionPayload.BlockHash,
))

// Set the graffiti on the block body.
Expand Down
5 changes: 5 additions & 0 deletions consensus-types/types/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ func NewDeposit(
return &Deposit{Proof: proof, Data: depositData}
}

// Empty creates an empty Deposit instance.
func (d *Deposit) Empty() *Deposit {
return &Deposit{}
}

// GetProof returns the proof as a slice of common.Root.
func (d *Deposit) GetProof() []common.Root {
return d.Proof[:]
Expand Down
3 changes: 3 additions & 0 deletions node-core/components/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type StateProcessorInput[
*engineprimitives.PayloadAttributes,
PayloadID,
]
DepositStore DepositStore
Signer crypto.BLSSigner
TelemetrySink *metrics.TelemetrySink
}
Expand All @@ -54,6 +55,7 @@ func ProvideStateProcessor[
BeaconBlockT BeaconBlock[BeaconBlockT],
BeaconStateT BeaconState[BeaconStateT, BeaconStateMarshallableT, KVStoreT],
BeaconStateMarshallableT any,
DepositStoreT DepositStore,
KVStoreT BeaconStore[KVStoreT],
](
in StateProcessorInput[LoggerT],
Expand All @@ -71,6 +73,7 @@ func ProvideStateProcessor[
in.Logger.With("service", "state-processor"),
in.ChainSpec,
in.ExecutionEngine,
in.DepositStore,
in.Signer,
crypto.GetAddressFromPubKey,
in.TelemetrySink,
Expand Down
3 changes: 2 additions & 1 deletion primitives/merkle/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ func VerifyProof[RootT, ProofT ~[32]byte](
return IsValidMerkleBranch(
leaf,
proof,
uint8(len(proof)), //#nosec:G701 // we check the length of the proof above.
//#nosec:G701 // we check the length of the proof above.
uint8(len(proof)),
merkleIndex,
root,
)
Expand Down
4 changes: 3 additions & 1 deletion state-transition/core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func setupState(
noop.NewLogger[any](),
cs,
execEngine,
depositStore,
mocksSigner,
func(bytes.B48) ([]byte, error) {
return dummyProposerAddr, nil
Expand Down Expand Up @@ -231,6 +232,7 @@ func moveToEndOfEpoch(
sp *TestStateProcessorT,
st *TestBeaconStateT,
ctx *transition.Context,
eth1Data *types.Eth1Data,
) *types.BeaconBlock {
t.Helper()
blk := tip
Expand All @@ -249,7 +251,7 @@ func moveToEndOfEpoch(
},
BaseFeePerGas: math.NewU256(0),
},
Eth1Data: &types.Eth1Data{},
Eth1Data: eth1Data,
Deposits: types.Deposits{},
},
)
Expand Down
16 changes: 4 additions & 12 deletions state-transition/core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ type StateProcessor[
fGetAddressFromPubKey func(crypto.BLSPubkey) ([]byte, error)
// executionEngine is the engine responsible for executing transactions.
executionEngine ExecutionEngine
// ds allows checking payload deposits against the deposit contract
ds DepositStore
// metrics is the metrics for the service.
metrics *stateProcessorMetrics
}
Expand All @@ -68,6 +70,7 @@ func NewStateProcessor[
logger log.Logger,
cs chain.ChainSpec,
executionEngine ExecutionEngine,
ds DepositStore,
signer crypto.BLSSigner,
fGetAddressFromPubKey func(crypto.BLSPubkey) ([]byte, error),
telemetrySink TelemetrySink,
Expand All @@ -86,6 +89,7 @@ func NewStateProcessor[
executionEngine: executionEngine,
signer: signer,
fGetAddressFromPubKey: fGetAddressFromPubKey,
ds: ds,
metrics: newStateProcessorMetrics(telemetrySink),
}
}
Expand Down Expand Up @@ -209,10 +213,6 @@ func (sp *StateProcessor[
return err
}

if err := sp.processEth1Data(st, blk); err != nil {
return err
}

if err := sp.processExecutionPayload(ctx, st, blk); err != nil {
return err
}
Expand Down Expand Up @@ -380,14 +380,6 @@ func (sp *StateProcessor[
return st.SetLatestBlockHeader(lbh)
}

// processEth1Data as defined in the Ethereum 2.0 specification, but without eth1 votes.
// https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#eth1-data
func (sp *StateProcessor[
BeaconBlockT, BeaconStateT, _, _,
]) processEth1Data(st BeaconStateT, blk BeaconBlockT) error {
return st.SetEth1Data(blk.GetBody().GetEth1Data())
}

// processEffectiveBalanceUpdates as defined in the Ethereum 2.0 specification.
// https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#effective-balances-updates
func (sp *StateProcessor[
Expand Down
2 changes: 1 addition & 1 deletion state-transition/core/state_processor_genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func TestInitialize(t *testing.T) {
// deposit index is set to the last accepted deposit.
latestValIdx, err := st.GetEth1DepositIndex()
require.NoError(t, err)
require.Equal(t, uint64(len(genDeposits)-1), latestValIdx)
require.Equal(t, uint64(len(genDeposits)), latestValIdx)
}

func checkValidatorNonBartio(
Expand Down
36 changes: 27 additions & 9 deletions state-transition/core/state_processor_staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,44 @@ import (
func (sp *StateProcessor[
BeaconBlockT, BeaconStateT, _, _,
]) processOperations(st BeaconStateT, blk BeaconBlockT) error {
// Verify that outstanding deposits are processed up to the maximum number of deposits.
eth1Data, err := st.GetEth1Data()
depositIndex, err := st.GetEth1DepositIndex()
if err != nil {
return err
}
depositCount := eth1Data.GetDepositCount().Unwrap()
depositIndex, err := st.GetEth1DepositIndex()

// Verify that the provided deposit root in eth1data is consistent with our local view of
// the deposit tree.
localDeposits, localDepositsRoot, err := sp.ds.GetDepositsByIndex(
depositIndex, sp.cs.MaxDepositsPerBlock(),
)
if err != nil {
return err
}
deposits := blk.GetBody().GetDeposits()
if uint64(len(deposits)) != min(sp.cs.MaxDepositsPerBlock(), depositCount-depositIndex) {
eth1Data := blk.GetBody().GetEth1Data()
if eth1Data.DepositRoot != localDepositsRoot {
return errors.New("local deposit tree root does not match the block deposit tree root")
}

// Verify that the provided deposit count is consistent with our local view of the
// deposit tree.
if uint64(len(localDeposits)) != min(
sp.cs.MaxDepositsPerBlock(),
eth1Data.DepositCount.Unwrap()-depositIndex,
) {
return errors.Wrapf(
ErrDepositCountMismatch, "expected: %d, got: %d",
min(sp.cs.MaxDepositsPerBlock(), depositCount-depositIndex), len(deposits),
min(sp.cs.MaxDepositsPerBlock(), eth1Data.DepositCount.Unwrap()-depositIndex),
len(localDeposits),
)
}

// Process each deposit.
for _, dep := range deposits {
// The provided eth1data is valid, accept it and set locally.
if err = st.SetEth1Data(eth1Data); err != nil {
return err
}

// Process each deposit in the block.
for _, dep := range blk.GetBody().GetDeposits() {
if err = sp.processDeposit(st, dep); err != nil {
return err
}
Expand Down
Loading

0 comments on commit 8599bc9

Please sign in to comment.