Skip to content

Commit

Permalink
validate deposits against deposit store ones
Browse files Browse the repository at this point in the history
  • Loading branch information
abi87 committed Oct 30, 2024
1 parent 160cc88 commit 6a191d1
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 19 deletions.
4 changes: 2 additions & 2 deletions beacond/cmd/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ func DefaultComponents() []any {
],
components.ProvideStateProcessor[
*BeaconBlock, *BeaconBlockBody, *BeaconBlockHeader,
*BeaconState, *BeaconStateMarshallable, *Deposit, *ExecutionPayload,
*ExecutionPayloadHeader, *KVStore,
*BeaconState, *BeaconStateMarshallable, *Deposit, *DepositStore,
*ExecutionPayload, *ExecutionPayloadHeader, *KVStore,
],
components.ProvideKVStore[*BeaconBlockHeader, *ExecutionPayloadHeader],
components.ProvideStorageBackend[
Expand Down
3 changes: 2 additions & 1 deletion mod/beacon/validator/block_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ func (s *Service[
envelope, err := s.retrieveExecutionPayload(ctx, st, blk)
if err != nil {
return blk, sidecars, err
} else if envelope == nil {
}
if envelope == nil {
return blk, sidecars, ErrNilPayload
}

Expand Down
9 changes: 7 additions & 2 deletions mod/node-core/pkg/components/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type StateProcessorInput[
ExecutionPayloadT, ExecutionPayloadHeaderT, WithdrawalsT,
],
ExecutionPayloadHeaderT ExecutionPayloadHeader[ExecutionPayloadHeaderT],
DepositT Deposit[DepositT, *ForkData, WithdrawalCredentials],
WithdrawalT Withdrawal[WithdrawalT],
WithdrawalsT Withdrawals[WithdrawalT],
] struct {
Expand All @@ -47,7 +48,8 @@ type StateProcessorInput[
PayloadID,
WithdrawalsT,
]
Signer crypto.BLSSigner
DepositStore DepositStore[DepositT]
Signer crypto.BLSSigner
}

// ProvideStateProcessor provides the state processor to the depinject
Expand All @@ -66,6 +68,7 @@ func ProvideStateProcessor[
],
BeaconStateMarshallableT any,
DepositT Deposit[DepositT, *ForkData, WithdrawalCredentials],
DepositStoreT DepositStore[DepositT],
ExecutionPayloadT ExecutionPayload[
ExecutionPayloadT, ExecutionPayloadHeaderT, WithdrawalsT,
],
Expand All @@ -78,7 +81,8 @@ func ProvideStateProcessor[
WithdrawalT Withdrawal[WithdrawalT],
](
in StateProcessorInput[
ExecutionPayloadT, ExecutionPayloadHeaderT, WithdrawalT, WithdrawalsT,
ExecutionPayloadT, ExecutionPayloadHeaderT,
DepositT, WithdrawalT, WithdrawalsT,
],
) *core.StateProcessor[
BeaconBlockT, BeaconBlockBodyT, BeaconBlockHeaderT,
Expand Down Expand Up @@ -107,6 +111,7 @@ func ProvideStateProcessor[
](
in.ChainSpec,
in.ExecutionEngine,
in.DepositStore,
in.Signer,
)
}
3 changes: 3 additions & 0 deletions mod/state-transition/pkg/core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ type StateProcessor[
executionEngine ExecutionEngine[
ExecutionPayloadT, ExecutionPayloadHeaderT, WithdrawalsT,
]
ds DepositStore[DepositT]
}

// NewStateProcessor creates a new state processor.
Expand Down Expand Up @@ -136,6 +137,7 @@ func NewStateProcessor[
executionEngine ExecutionEngine[
ExecutionPayloadT, ExecutionPayloadHeaderT, WithdrawalsT,
],
ds DepositStore[DepositT],
signer crypto.BLSSigner,
) *StateProcessor[
BeaconBlockT, BeaconBlockBodyT, BeaconBlockHeaderT,
Expand All @@ -152,6 +154,7 @@ func NewStateProcessor[
cs: cs,
executionEngine: executionEngine,
signer: signer,
ds: ds,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func TestInitialize(t *testing.T) {
](
cs,
execEngine,
nil, // TODO: add deposit store
mocksSigner,
)

Expand Down
41 changes: 27 additions & 14 deletions mod/state-transition/pkg/core/state_processor_staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
package core

import (
"fmt"
"reflect"

"github.com/berachain/beacon-kit/mod/errors"
"github.com/berachain/beacon-kit/mod/primitives/pkg/common"
"github.com/berachain/beacon-kit/mod/primitives/pkg/math"
Expand All @@ -36,26 +39,36 @@ func (sp *StateProcessor[
st BeaconStateT,
blk BeaconBlockT,
) error {
// Verify that outstanding deposits are processed up to the maximum number
// of deposits.
deposits := blk.GetBody().GetDeposits()
index, err := st.GetEth1DepositIndex()
// Verify that outstanding deposits matches those listed by contract
depositIndex, err := st.GetEth1DepositIndex()
if err != nil {
return err
}
eth1Data, err := st.GetEth1Data()

stateDeposits, err := sp.ds.GetDepositsByIndex(
depositIndex,
sp.cs.MaxDepositsPerBlock(),
)
if err != nil {
return err
}
depositCount := min(
sp.cs.MaxDepositsPerBlock(),
eth1Data.GetDepositCount().Unwrap()-index,
)
_ = depositCount
// TODO: Update eth1data count and check this.
// if uint64(len(deposits)) != depositCount {
// return errors.New("deposit count mismatch")
// }

deposits := blk.GetBody().GetDeposits()
if len(stateDeposits) != len(deposits) {
return fmt.Errorf("deposits mismatched lengths, state: %d, payload: %d",
len(stateDeposits),
len(deposits),
)
}

for i, sd := range stateDeposits {
if !reflect.DeepEqual(sd, deposits[i]) {
return fmt.Errorf("deposits mismatched, idx %d state: %v, payload: %v",
i, sd, deposits[i],
)
}
}

return sp.processDeposits(st, deposits)
}

Expand Down
9 changes: 9 additions & 0 deletions mod/state-transition/pkg/core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@ type Deposit[
) error
}

// DepositStore defines the interface for deposit storage.
type DepositStore[DepositT any] interface {
// GetDepositsByIndex returns `numView` expected deposits.
GetDepositsByIndex(
startIndex uint64,
numView uint64,
) ([]DepositT, error)
}

type ExecutionPayload[
ExecutionPayloadT, ExecutionPayloadHeaderT, WithdrawalsT any,
] interface {
Expand Down

0 comments on commit 6a191d1

Please sign in to comment.