Skip to content

Commit

Permalink
Consolidate unit tests + spectests
Browse files Browse the repository at this point in the history
TODO: Add godocs
  • Loading branch information
prestonvanloon committed May 13, 2024
1 parent 16d3761 commit ee3ad6e
Show file tree
Hide file tree
Showing 15 changed files with 606 additions and 18 deletions.
6 changes: 6 additions & 0 deletions beacon-chain/core/electra/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,27 @@ go_test(
name = "go_default_test",
srcs = [
"churn_test.go",
"consolidations_test.go",
"upgrade_test.go",
"validator_test.go",
],
deps = [
":go_default_library",
"//beacon-chain/core/helpers:go_default_library",
"//beacon-chain/core/signing:go_default_library",
"//beacon-chain/core/time:go_default_library",
"//beacon-chain/state:go_default_library",
"//beacon-chain/state/state-native:go_default_library",
"//config/fieldparams:go_default_library",
"//config/params:go_default_library",
"//consensus-types/primitives:go_default_library",
"//crypto/bls/blst:go_default_library",
"//crypto/bls/common:go_default_library",
"//encoding/bytesutil:go_default_library",
"//math:go_default_library",
"//proto/engine/v1:go_default_library",
"//proto/prysm/v1alpha1:go_default_library",
"//runtime/interop:go_default_library",
"//testing/require:go_default_library",
"//testing/util:go_default_library",
"//time/slots:go_default_library",
Expand Down
48 changes: 30 additions & 18 deletions beacon-chain/core/electra/consolidations.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ import (
"go.opencensus.io/trace"
)

var ErrNilConsolidations = errors.New("nil consolidations")

// ProcessPendingConsolidations --
// ProcessPendingConsolidations implements the spec definition below. This method makes mutating
// calls to the beacon state.
//
// Spec definition:
//
Expand All @@ -40,14 +39,16 @@ var ErrNilConsolidations = errors.New("nil consolidations")
// next_pending_consolidation += 1
//
// state.pending_consolidations = state.pending_consolidations[next_pending_consolidation:]
func ProcessPendingConsolidations(ctx context.Context, st state.BeaconState, activeBalance uint64) (state.BeaconState, error) {
func ProcessPendingConsolidations(ctx context.Context, st state.BeaconState) (state.BeaconState, error) {
ctx, span := trace.StartSpan(ctx, "electra.ProcessPendingConsolidations")
defer span.End()

if st == nil || st.IsNil() {
return nil, errors.New("nil state")
}

currentEpoch := slots.ToEpoch(st.Slot())

var nextPendingConsolidation uint64
pendingConsolidations, err := st.PendingConsolidations()
if err != nil {
Expand All @@ -62,14 +63,18 @@ func ProcessPendingConsolidations(ctx context.Context, st state.BeaconState, act
nextPendingConsolidation++
continue
}
if sourceValidator.WithdrawableEpoch > slots.ToEpoch(st.Slot()) {
if sourceValidator.WithdrawableEpoch > currentEpoch {
break
}

if err := SwitchToCompoundingValidator(ctx, st, pc.TargetIndex); err != nil {
return nil, err
}

activeBalance, err := st.ActiveBalanceAtIndex(pc.SourceIndex)
if err != nil {
return nil, err
}
if err := helpers.DecreaseBalance(st, pc.SourceIndex, activeBalance); err != nil {
return nil, err
}
Expand All @@ -79,15 +84,18 @@ func ProcessPendingConsolidations(ctx context.Context, st state.BeaconState, act
nextPendingConsolidation++
}

// TODO: Check OOB
if err := st.SetPendingConsolidations(pendingConsolidations[nextPendingConsolidation:]); err != nil {
return nil, err
if nextPendingConsolidation > 0 {
// TODO: Check OOB
if err := st.SetPendingConsolidations(pendingConsolidations[nextPendingConsolidation:]); err != nil {
return nil, err
}
}

return st, nil
}

// ProcessConsolidations --
// ProcessConsolidations implements the spec definition below. This method makes mutating calls to
// the beacon state.
//
// Spec definition:
//
Expand Down Expand Up @@ -141,11 +149,21 @@ func ProcessConsolidations(ctx context.Context, st state.BeaconState, cs []*ethp
if st == nil || st.IsNil() {
return nil, errors.New("nil state")
}
if cs == nil {
return nil, ErrNilConsolidations

if len(cs) == 0 {
return st, nil // Nothing to process.
}

domain, err := signing.ComputeDomain(
params.BeaconConfig().DomainConsolidation,
st.Fork().CurrentVersion,
st.GenesisValidatorsRoot(),
)
if err != nil {
return nil, err
}

domain, err := signing.ComputeDomain(params.BeaconConfig().DomainConsolidation, st.Fork().CurrentVersion, st.GenesisValidatorsRoot())
totalBalance, err := helpers.TotalActiveBalance(st)
if err != nil {
return nil, err
}
Expand All @@ -155,22 +173,16 @@ func ProcessConsolidations(ctx context.Context, st state.BeaconState, cs []*ethp
return nil, errors.New("nil consolidation")
}

// TODO(preston): can these be moved outside of the loop?
if n, err := st.NumPendingConsolidations(); err != nil {
return nil, err
} else if n >= params.BeaconConfig().PendingConsolidationsLimit {
return nil, errors.New("pending consolidations queue is full")
}

totalBalance, err := helpers.TotalActiveBalance(st)
if err != nil {
return nil, err
}
if helpers.ConsolidationChurnLimit(math.Gwei(totalBalance)) <= math.Gwei(params.BeaconConfig().MinActivationBalance) {
return nil, errors.New("too little available consolidation churn limit")
}
currentEpoch := slots.ToEpoch(st.Slot())
// END TODO

if c.Message.SourceIndex == c.Message.TargetIndex {
return nil, errors.New("source and target index are the same")
Expand Down
Loading

0 comments on commit ee3ad6e

Please sign in to comment.