Skip to content

Commit

Permalink
feat(staking): Withdrawals (berachain#536)
Browse files Browse the repository at this point in the history
* net

* bet

* linter

* bet

* bet

* bet

* comments

* bet

* bet

* add todo comment

* linter

* bet

* fix

* bet

* need 2 fix
  • Loading branch information
itsdevbear authored Mar 14, 2024
1 parent b4b4899 commit cf1f4ef
Show file tree
Hide file tree
Showing 41 changed files with 295 additions and 1,377 deletions.
7 changes: 1 addition & 6 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,4 @@ packages:
config:
recursive: True
with-expecter: true
all: True
github.com/berachain/beacon-kit/beacon/execution/logs:
config:
recursive: True
with-expecter: true
include-regex: LogContainer
all: True
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,6 @@ test-unit-fuzz:
go test -fuzz=FuzzHashTreeRoot ./crypto/sha256/... -fuzztime=${MEDIUM_FUZZ_TIME}
go test -fuzz=FuzzQueueSimple ./lib/store/collections/ -fuzztime=${SHORT_FUZZ_TIME}
go test -fuzz=FuzzQueueMulti ./lib/store/collections/ -fuzztime=${SHORT_FUZZ_TIME}
go test -fuzz=FuzzLogCacheSeq ./beacon/execution/logs/ -fuzztime=${SHORT_FUZZ_TIME}
go test -fuzz=FuzzLogCacheConcurrency ./beacon/execution/logs/ -fuzztime=${SHORT_FUZZ_TIME}
go test -fuzz=FuzzProcessLogs ./beacon/execution -fuzztime=${SHORT_FUZZ_TIME}

#################
Expand Down
2 changes: 2 additions & 0 deletions beacon/blockchain/finalize_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ func (s *Service) FinalizeBeaconBlock(
// TODO: put into an actual function / flow
_, err = s.BeaconState(ctx).DequeueDeposits(
uint64(len(blk.GetBody().GetDeposits())))

// TODO: Actually Process Staking Deposits and Withdraws.
return err
}

Expand Down
22 changes: 22 additions & 0 deletions beacon/blockchain/receive_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,28 @@ func (s *Service) validateExecutionOnBlock(
)
}

// expectedMix, err := s.BeaconState(ctx).RandaoMix()
// if err != nil {
// return false, err
// }

// // Ensure the prev randao matches the local state.
// if payload.GetPrevRandao() != expectedMix {
// return false, fmt.Errorf(
// "prev randao does not match, expected: %x, got: %x",
// expectedMix, payload.GetPrevRandao(),
// )
// }

// if expectedTime, err := spec.TimeAtSlot(slot, genesisTime); err != nil {
// return fmt.Errorf("slot or genesis time in state is corrupt, cannot
// compute time: %v", err)
// } else if payload.Timestamp != expectedTime {
// return fmt.Errorf("state at slot %d, genesis time %d, expected execution
// payload time %d, but got %d",
// slot, genesisTime, expectedTime, payload.Timestamp)
// }

// TODO: add some more safety checks here.
return s.es.NotifyNewPayload(
ctx,
Expand Down
4 changes: 3 additions & 1 deletion beacon/builder/local/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,9 @@ func (s *Service) getPayloadAttribute(
)

// Get the expected withdrawals to include in this payload.
withdrawals, err := st.ExpectedWithdrawals()
withdrawals, err := st.ExpectedWithdrawals(
s.BeaconCfg().Limits.MaxWithdrawalsPerPayload,
)
if err != nil {
s.Logger().Error(
"Could not get expected withdrawals to get payload attribute", "error", err)
Expand Down
56 changes: 48 additions & 8 deletions beacon/core/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@
package state

import (
"context"

"github.com/berachain/beacon-kit/beacon/core/randao/types"
beacontypes "github.com/berachain/beacon-kit/beacon/core/types"
enginetypes "github.com/berachain/beacon-kit/engine/types"
"github.com/berachain/beacon-kit/primitives"
)

// BeaconState is the interface for the beacon state. It
Expand All @@ -40,19 +43,22 @@ type BeaconState interface {

// ReadOnlyBeaconState is the interface for a read-only beacon state.
type ReadOnlyBeaconState interface {
// TODO: fill these in as we develop impl
ReadWriteDepositQueue
ReadOnlyWithdrawals

ReadOnlyDeposits
ReadOnlyRandaoMixes
ReadOnlyValidators
ReadOnlyWithdrawals

SetParentBlockRoot([32]byte)
GetParentBlockRoot() [32]byte
}

// WriteOnlyBeaconState is the interface for a write-only beacon state.
type WriteOnlyBeaconState interface {
WriteOnlyDeposits
WriteOnlyRandaoMixes
WriteOnlyValidators
WriteOnlyWithdrawals

SetParentBlockRoot([32]byte)
}

Expand All @@ -68,14 +74,48 @@ type ReadOnlyRandaoMixes interface {
RandaoMix() (types.Mix, error)
}

// WriteOnlyValidators has write access to validator methods.
type WriteOnlyValidators interface {
// Add methods here
}

// ReadOnlyValidators has read access to validator methods.
type ReadOnlyValidators interface {
ValidatorIndexByPubkey(
context.Context,
[]byte,
) (primitives.ValidatorIndex, error)
}

// ReadWriteValidators has read and write access to validator methods.
type ReadWriteDeposits interface {
ReadOnlyDeposits
WriteOnlyDeposits
}

// ReadWriteDepositQueue has read and write access to deposit queue.
type ReadWriteDepositQueue interface {
type WriteOnlyDeposits interface {
EnqueueDeposits([]*beacontypes.Deposit) error
ExpectedDeposits(n uint64) ([]*beacontypes.Deposit, error)
DequeueDeposits(n uint64) ([]*beacontypes.Deposit, error)
DequeueDeposits(uint64) ([]*beacontypes.Deposit, error)
}

// ReadOnlyDeposits has read access to deposit queue.
type ReadOnlyDeposits interface {
ExpectedDeposits(uint64) ([]*beacontypes.Deposit, error)
}

// ReadWriteWithdrawals has read and write access to withdrawal methods.
type ReadWriteWithdrawals interface {
ReadOnlyWithdrawals
WriteOnlyWithdrawals
}

// ReadOnlyWithdrawals only has read access to withdrawal methods.
type ReadOnlyWithdrawals interface {
ExpectedWithdrawals() ([]*enginetypes.Withdrawal, error)
ExpectedWithdrawals(uint64) ([]*enginetypes.Withdrawal, error)
}

// WriteOnlyWithdrawals only has write access to withdrawal methods.
type WriteOnlyWithdrawals interface {
EnqueueWithdrawals([]*enginetypes.Withdrawal) error
}
23 changes: 0 additions & 23 deletions beacon/core/types/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ package types

import (
"strconv"

stakingabi "github.com/berachain/beacon-kit/contracts/abi"
abilib "github.com/berachain/beacon-kit/lib/abi"
"github.com/ethereum/go-ethereum/core/types"
)

// Deposit into the consensus layer from the deposit contract in the execution
Expand All @@ -49,25 +45,6 @@ type Deposit struct {
Index uint64 `json:"index"`
}

// UnmarshalEthLog unmarshals the log into a Deposit.
func (d *Deposit) UnmarshalEthLog(log types.Log) error {
abigenType := &stakingabi.BeaconDepositContractDeposit{}
if err := (abilib.WrappedABI{ABI: stakingabi.DepositContractABI}).
UnpackLogs(abigenType, "Deposit", log); err != nil {
return err
}
if d == nil {
d = &Deposit{}
}

d.Pubkey = abigenType.Pubkey
d.Credentials = abigenType.Credentials
d.Amount = abigenType.Amount
d.Signature = abigenType.Signature
d.Index = abigenType.Index
return nil
}

// String returns a string representation of the Deposit.
func (d *Deposit) String() string {
return "Deposit{" +
Expand Down
2 changes: 1 addition & 1 deletion beacon/core/types/generated.ssz.go

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

74 changes: 0 additions & 74 deletions beacon/execution/logs/allocator.go

This file was deleted.

71 changes: 0 additions & 71 deletions beacon/execution/logs/allocator_options.go

This file was deleted.

41 changes: 0 additions & 41 deletions beacon/execution/logs/builder.go

This file was deleted.

Loading

0 comments on commit cf1f4ef

Please sign in to comment.