Skip to content

Commit

Permalink
feat(st): Modularize forkdata (#1194)
Browse files Browse the repository at this point in the history
* bet

* bet

* x

* bet

* bet remove

* bet

* bet

* bet

* x

* bet

* bet

* bet

* bet

* bet

* x

* bet

* bet

* x

* x
  • Loading branch information
itsdevbear authored May 27, 2024
1 parent fb61cd9 commit a3e21bb
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 60 deletions.
1 change: 1 addition & 0 deletions mod/node-builder/pkg/components/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func ProvideRuntime(
*datypes.BlobSidecars,
*transition.Context,
*types.Deposit,
*types.ForkData,
*types.Validator,
types.WithdrawalCredentials,
](
Expand Down
30 changes: 0 additions & 30 deletions mod/state-transition/pkg/core/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
engineprimitives "github.com/berachain/beacon-kit/mod/primitives-engine"
"github.com/berachain/beacon-kit/mod/primitives/pkg/crypto"
"github.com/berachain/beacon-kit/mod/primitives/pkg/math"
ssz "github.com/ferranbt/fastssz"
)

// BeaconState is the interface for the beacon state. It
Expand Down Expand Up @@ -157,32 +156,3 @@ type ReadOnlyEth1Data interface {
type ReadOnlyWithdrawals interface {
ExpectedWithdrawals() ([]*engineprimitives.Withdrawal, error)
}

// Validator represents an interface for a validator with generic type
// ValidatorT.
type Validator[
ValidatorT any,
WithdrawalCredentialsT ~[32]byte,
] interface {
ssz.Marshaler
ssz.HashRoot
// New creates a new validator with the given parameters.
New(
pubkey crypto.BLSPubkey,
withdrawalCredentials WithdrawalCredentialsT,
amount math.Gwei,
effectiveBalanceIncrement math.Gwei,
maxEffectiveBalance math.Gwei,
) ValidatorT
// IsSlashed returns true if the validator is slashed.
IsSlashed() bool
// GetPubkey returns the public key of the validator.
GetPubkey() crypto.BLSPubkey
// GetEffectiveBalance returns the effective balance of the validator in
// Gwei.
GetEffectiveBalance() math.Gwei
// SetEffectiveBalance sets the effective balance of the validator in Gwei.
SetEffectiveBalance(math.Gwei)
// GetWithdrawableEpoch returns the epoch when the validator can withdraw.
GetWithdrawableEpoch() math.Epoch
}
31 changes: 20 additions & 11 deletions mod/state-transition/pkg/core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ type StateProcessor[
BeaconStateT BeaconState[ValidatorT],
BlobSidecarsT BlobSidecars,
ContextT Context,
DepositT Deposit[types.ForkData, WithdrawalCredentialsT],
DepositT Deposit[ForkDataT, WithdrawalCredentialsT],
ForkDataT ForkData[ForkDataT],
ValidatorT Validator[ValidatorT, WithdrawalCredentialsT],
WithdrawalCredentialsT ~[32]byte,
] struct {
Expand All @@ -62,7 +63,8 @@ func NewStateProcessor[
BeaconStateT BeaconState[ValidatorT],
BlobSidecarsT BlobSidecars,
ContextT Context,
DepositT Deposit[types.ForkData, WithdrawalCredentialsT],
DepositT Deposit[ForkDataT, WithdrawalCredentialsT],
ForkDataT ForkData[ForkDataT],
ValidatorT Validator[ValidatorT, WithdrawalCredentialsT],
WithdrawalCredentialsT ~[32]byte,
](
Expand All @@ -76,12 +78,12 @@ func NewStateProcessor[
) *StateProcessor[
BeaconBlockT, BeaconBlockBodyT,
BeaconStateT, BlobSidecarsT, ContextT,
DepositT, ValidatorT, WithdrawalCredentialsT,
DepositT, ForkDataT, ValidatorT, WithdrawalCredentialsT,
] {
return &StateProcessor[
BeaconBlockT, BeaconBlockBodyT,
BeaconStateT, BlobSidecarsT, ContextT,
DepositT, ValidatorT, WithdrawalCredentialsT,
DepositT, ForkDataT, ValidatorT, WithdrawalCredentialsT,
]{
cs: cs,
rp: rp,
Expand All @@ -94,7 +96,8 @@ func NewStateProcessor[
// Transition is the main function for processing a state transition.
func (sp *StateProcessor[
BeaconBlockT, BeaconBlockBodyT, BeaconStateT,
BlobSidecarsT, ContextT, DepositT, ValidatorT, WithdrawalCredentialsT,
BlobSidecarsT, ContextT, DepositT, ForkDataT,
ValidatorT, WithdrawalCredentialsT,
]) Transition(
ctx ContextT,
st BeaconStateT,
Expand Down Expand Up @@ -178,7 +181,8 @@ func (sp *StateProcessor[
// ProcessSlot is run when a slot is missed.
func (sp *StateProcessor[
BeaconBlockT, BeaconBlockBodyT, BeaconStateT,
BlobSidecarsT, ContextT, DepositT, ValidatorT, WithdrawalCredentialsT,
BlobSidecarsT, ContextT, DepositT, ForkDataT,
ValidatorT, WithdrawalCredentialsT,
]) ProcessSlot(
st BeaconStateT,
) ([]*transition.ValidatorUpdate, error) {
Expand Down Expand Up @@ -250,7 +254,8 @@ func (sp *StateProcessor[
// state root.
func (sp *StateProcessor[
BeaconBlockT, BeaconBlockBodyT, BeaconStateT,
BlobSidecarsT, ContextT, DepositT, ValidatorT, WithdrawalCredentialsT,
BlobSidecarsT, ContextT, DepositT, ForkDataT,
ValidatorT, WithdrawalCredentialsT,
]) ProcessBlock(
ctx ContextT,
st BeaconStateT,
Expand Down Expand Up @@ -318,7 +323,8 @@ func (sp *StateProcessor[
// processEpoch processes the epoch and ensures it matches the local state.
func (sp *StateProcessor[
BeaconBlockT, BeaconBlockBodyT, BeaconStateT,
BlobSidecarsT, ContextT, DepositT, ValidatorT, WithdrawalCredentialsT,
BlobSidecarsT, ContextT, DepositT, ForkDataT,
ValidatorT, WithdrawalCredentialsT,
]) processEpoch(
st BeaconStateT,
) ([]*transition.ValidatorUpdate, error) {
Expand All @@ -336,7 +342,8 @@ func (sp *StateProcessor[
// state.
func (sp *StateProcessor[
BeaconBlockT, BeaconBlockBodyT, BeaconStateT,
BlobSidecarsT, ContextT, DepositT, ValidatorT, WithdrawalCredentialsT,
BlobSidecarsT, ContextT, DepositT, ForkDataT,
ValidatorT, WithdrawalCredentialsT,
]) processBlockHeader(
st BeaconStateT,
blk BeaconBlockT,
Expand Down Expand Up @@ -421,7 +428,8 @@ func (sp *StateProcessor[
//nolint:lll
func (sp *StateProcessor[
BeaconBlockT, BeaconBlockBodyT, BeaconStateT,
BlobSidecarsT, ContextT, DepositT, ValidatorT, WithdrawalCredentialsT,
BlobSidecarsT, ContextT, DepositT, ForkDataT,
ValidatorT, WithdrawalCredentialsT,
]) getAttestationDeltas(
st BeaconStateT,
) ([]math.Gwei, []math.Gwei, error) {
Expand All @@ -440,7 +448,8 @@ func (sp *StateProcessor[
//nolint:lll
func (sp *StateProcessor[
BeaconBlockT, BeaconBlockBodyT, BeaconStateT,
BlobSidecarsT, ContextT, DepositT, ValidatorT, WithdrawalCredentialsT,
BlobSidecarsT, ContextT, DepositT, ForkDataT,
ValidatorT, WithdrawalCredentialsT,
]) processRewardsAndPenalties(
st BeaconStateT,
) error {
Expand Down
3 changes: 2 additions & 1 deletion mod/state-transition/pkg/core/state_processor_committee.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import (

func (sp *StateProcessor[
BeaconBlockT, BeaconBlockBodyT, BeaconStateT,
BlobSidecarsT, ContextT, DepositT, ValidatorT, WithdrawalCredentialsT,
BlobSidecarsT, ContextT, DepositT, ForkDataT,
ValidatorT, WithdrawalCredentialsT,
]) processSyncCommitteeUpdates(
st BeaconStateT,
) ([]*transition.ValidatorUpdate, error) {
Expand Down
3 changes: 2 additions & 1 deletion mod/state-transition/pkg/core/state_processor_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ import (
//nolint:gocognit,funlen // todo fix.
func (sp *StateProcessor[
BeaconBlockT, BeaconBlockBodyT, BeaconStateT,
BlobSidecarsT, ContextT, DepositT, ValidatorT, WithdrawalCredentialsT,
BlobSidecarsT, ContextT, DepositT, ForkDataT,
ValidatorT, WithdrawalCredentialsT,
]) InitializePreminedBeaconStateFromEth1(
st BeaconStateT,
deposits []DepositT,
Expand Down
3 changes: 2 additions & 1 deletion mod/state-transition/pkg/core/state_processor_payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ import (
//nolint:funlen // todo fix.
func (sp *StateProcessor[
BeaconBlockT, BeaconBlockBodyT, BeaconStateT,
BlobSidecarsT, ContextT, DepositT, ValidatorT, WithdrawalCredentialsT,
BlobSidecarsT, ContextT, DepositT, ForkDataT,
ValidatorT, WithdrawalCredentialsT,
]) processExecutionPayload(
ctx ContextT,
st BeaconStateT,
Expand Down
6 changes: 4 additions & 2 deletions mod/state-transition/pkg/core/state_processor_randao.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ package core
// ensures it matches the local state.
func (sp *StateProcessor[
BeaconBlockT, BeaconBlockBodyT, BeaconStateT,
BlobSidecarsT, ContextT, DepositT, ValidatorT, WithdrawalCredentialsT,
BlobSidecarsT, ContextT, DepositT, ForkDataT,
ValidatorT, WithdrawalCredentialsT,
]) processRandaoReveal(
st BeaconStateT,
blk BeaconBlockT,
Expand All @@ -44,7 +45,8 @@ func (sp *StateProcessor[
//nolint:lll
func (sp *StateProcessor[
BeaconBlockT, BeaconBlockBodyT, BeaconStateT,
BlobSidecarsT, ContextT, DepositT, ValidatorT, WithdrawalCredentialsT,
BlobSidecarsT, ContextT, DepositT, ForkDataT,
ValidatorT, WithdrawalCredentialsT,
]) processRandaoMixesReset(
st BeaconStateT,
) error {
Expand Down
15 changes: 10 additions & 5 deletions mod/state-transition/pkg/core/state_processor_slashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ import (
//nolint:lll
func (sp *StateProcessor[
BeaconBlockT, BeaconBlockBodyT, BeaconStateT,
BlobSidecarsT, ContextT, DepositT, ValidatorT, WithdrawalCredentialsT,
BlobSidecarsT, ContextT, DepositT, ForkDataT,
ValidatorT, WithdrawalCredentialsT,
]) processSlashingsReset(
st BeaconStateT,
) error {
Expand All @@ -55,7 +56,8 @@ func (sp *StateProcessor[
//nolint:lll,unused // will be used later
func (sp *StateProcessor[
BeaconBlockT, BeaconBlockBodyT, BeaconStateT,
BlobSidecarsT, ContextT, DepositT, ValidatorT, WithdrawalCredentialsT,
BlobSidecarsT, ContextT, DepositT, ForkDataT,
ValidatorT, WithdrawalCredentialsT,
]) processProposerSlashing(
_ BeaconStateT,
// ps ProposerSlashing,
Expand All @@ -69,7 +71,8 @@ func (sp *StateProcessor[
//nolint:lll,unused // will be used later
func (sp *StateProcessor[
BeaconBlockT, BeaconBlockBodyT, BeaconStateT,
BlobSidecarsT, ContextT, DepositT, ValidatorT, WithdrawalCredentialsT,
BlobSidecarsT, ContextT, DepositT, ForkDataT,
ValidatorT, WithdrawalCredentialsT,
]) processAttesterSlashing(
_ BeaconStateT,
// as AttesterSlashing,
Expand All @@ -86,7 +89,8 @@ func (sp *StateProcessor[
//nolint:lll,unused // will be used later
func (sp *StateProcessor[
BeaconBlockT, BeaconBlockBodyT, BeaconStateT,
BlobSidecarsT, ContextT, DepositT, ValidatorT, WithdrawalCredentialsT,
BlobSidecarsT, ContextT, DepositT, ForkDataT,
ValidatorT, WithdrawalCredentialsT,
]) processSlashings(
st BeaconStateT,
) error {
Expand Down Expand Up @@ -140,7 +144,8 @@ func (sp *StateProcessor[
//nolint:unused // will be used later
func (sp *StateProcessor[
BeaconBlockT, BeaconBlockBodyT, BeaconStateT,
BlobSidecarsT, ContextT, DepositT, ValidatorT, WithdrawalCredentialsT,
BlobSidecarsT, ContextT, DepositT, ForkDataT,
ValidatorT, WithdrawalCredentialsT,
]) processSlash(
st BeaconStateT,
val ValidatorT,
Expand Down
22 changes: 14 additions & 8 deletions mod/state-transition/pkg/core/state_processor_staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
package core

import (
"github.com/berachain/beacon-kit/mod/consensus-types/pkg/types"
"github.com/berachain/beacon-kit/mod/errors"
"github.com/berachain/beacon-kit/mod/primitives"
"github.com/berachain/beacon-kit/mod/primitives/pkg/math"
Expand All @@ -38,7 +37,8 @@ import (
// local state.
func (sp *StateProcessor[
BeaconBlockT, BeaconBlockBodyT, BeaconStateT,
BlobSidecarsT, ContextT, DepositT, ValidatorT, WithdrawalCredentialsT,
BlobSidecarsT, ContextT, DepositT, ForkDataT,
ValidatorT, WithdrawalCredentialsT,
]) processOperations(
st BeaconStateT,
blk BeaconBlockT,
Expand Down Expand Up @@ -70,7 +70,8 @@ func (sp *StateProcessor[
// local state.
func (sp *StateProcessor[
BeaconBlockT, BeaconBlockBodyT, BeaconStateT,
BlobSidecarsT, ContextT, DepositT, ValidatorT, WithdrawalCredentialsT,
BlobSidecarsT, ContextT, DepositT, ForkDataT,
ValidatorT, WithdrawalCredentialsT,
]) processDeposits(
st BeaconStateT,
deposits []DepositT,
Expand All @@ -91,7 +92,8 @@ func (sp *StateProcessor[
// processDeposit processes the deposit and ensures it matches the local state.
func (sp *StateProcessor[
BeaconBlockT, BeaconBlockBodyT, BeaconStateT,
BlobSidecarsT, ContextT, DepositT, ValidatorT, WithdrawalCredentialsT,
BlobSidecarsT, ContextT, DepositT, ForkDataT,
ValidatorT, WithdrawalCredentialsT,
]) processDeposit(
st BeaconStateT,
dep DepositT,
Expand Down Expand Up @@ -128,7 +130,8 @@ func (sp *StateProcessor[
// createValidator creates a validator if the deposit is valid.
func (sp *StateProcessor[
BeaconBlockT, BeaconBlockBodyT, BeaconStateT,
BlobSidecarsT, ContextT, DepositT, ValidatorT, WithdrawalCredentialsT,
BlobSidecarsT, ContextT, DepositT, ForkDataT,
ValidatorT, WithdrawalCredentialsT,
]) createValidator(
st BeaconStateT,
dep DepositT,
Expand All @@ -154,8 +157,9 @@ func (sp *StateProcessor[
epoch = sp.cs.SlotToEpoch(slot)

// Verify that the message was signed correctly.
var d ForkDataT
if err = dep.VerifySignature(
(*types.ForkData)(nil).New(
d.New(
version.FromUint32[primitives.Version](
sp.cs.ActiveForkVersionForEpoch(epoch),
), genesisValidatorsRoot,
Expand All @@ -173,7 +177,8 @@ func (sp *StateProcessor[
// addValidatorToRegistry adds a validator to the registry.
func (sp *StateProcessor[
BeaconBlockT, BeaconBlockBodyT, BeaconStateT,
BlobSidecarsT, ContextT, DepositT, ValidatorT, WithdrawalCredentialsT,
BlobSidecarsT, ContextT, DepositT, ForkDataT,
ValidatorT, WithdrawalCredentialsT,
]) addValidatorToRegistry(
st BeaconStateT,
dep DepositT,
Expand Down Expand Up @@ -204,7 +209,8 @@ func (sp *StateProcessor[
//nolint:lll
func (sp *StateProcessor[
BeaconBlockT, BeaconBlockBodyT, BeaconStateT,
BlobSidecarsT, ContextT, DepositT, ValidatorT, WithdrawalCredentialsT,
BlobSidecarsT, ContextT, DepositT, ForkDataT,
ValidatorT, WithdrawalCredentialsT,
]) processWithdrawals(
st BeaconStateT,
body BeaconBlockBodyT,
Expand Down
Loading

0 comments on commit a3e21bb

Please sign in to comment.