Skip to content

Commit

Permalink
More tests for branching logic in ExitEpochAndUpdateChurn
Browse files Browse the repository at this point in the history
  • Loading branch information
prestonvanloon committed May 3, 2024
1 parent 02528d3 commit 00d1f54
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 1 deletion.
2 changes: 2 additions & 0 deletions beacon-chain/state/state-native/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ go_test(
],
embed = [":go_default_library"],
deps = [
"//beacon-chain/core/helpers:go_default_library",
"//beacon-chain/core/transition:go_default_library",
"//beacon-chain/state:go_default_library",
"//beacon-chain/state/state-native/types:go_default_library",
Expand All @@ -148,6 +149,7 @@ go_test(
"//testing/assert:go_default_library",
"//testing/require:go_default_library",
"//testing/util:go_default_library",
"//time/slots:go_default_library",
"@com_github_ethereum_go_ethereum//common/hexutil:go_default_library",
"@com_github_golang_snappy//:go_default_library",
"@com_github_google_go_cmp//cmp:go_default_library",
Expand Down
163 changes: 162 additions & 1 deletion beacon-chain/state/state-native/setters_churn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import (
"testing"

"github.com/golang/snappy"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/helpers"
state_native "github.com/prysmaticlabs/prysm/v5/beacon-chain/state/state-native"
"github.com/prysmaticlabs/prysm/v5/config/params"
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
"github.com/prysmaticlabs/prysm/v5/math"
eth "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v5/testing/require"
"github.com/prysmaticlabs/prysm/v5/testing/util"
"github.com/prysmaticlabs/prysm/v5/time/slots"
)

func TestExitEpochAndUpdateChurn(t *testing.T) {
func TestExitEpochAndUpdateChurn_SpectestCase(t *testing.T) {
// Load a serialized Electra state from disk.
// The spec tests shows that the exit epoch is 262 for validator 0 performing a voluntary exit.
serializedBytes, err := util.BazelFileBytes("tests/mainnet/electra/operations/voluntary_exit/pyspec_tests/exit_existing_churn_and_churn_limit_balance/pre.ssz_snappy")
Expand All @@ -31,9 +34,167 @@ func TestExitEpochAndUpdateChurn(t *testing.T) {
require.NoError(t, err)
require.Equal(t, primitives.Epoch(262), ee)

p := s.ToProto()
pb, ok := p.(*eth.BeaconStateElectra)
if !ok {
t.Fatal("wrong proto")
}
require.Equal(t, math.Gwei(127000000000), pb.ExitBalanceToConsume)
require.Equal(t, primitives.Epoch(262), pb.EarliestExitEpoch)

// Fails for versions older than electra
s, err = state_native.InitializeFromProtoDeneb(&eth.BeaconStateDeneb{})
require.NoError(t, err)
_, err = s.ExitEpochAndUpdateChurn(10)
require.ErrorContains(t, "not supported", err)
}

func TestExitEpochAndUpdateChurn(t *testing.T) {
slot := primitives.Slot(10_000_000)
epoch := slots.ToEpoch(slot)
t.Run("state earliest exit epoch is old", func(t *testing.T) {
st, err := state_native.InitializeFromProtoElectra(&eth.BeaconStateElectra{
Slot: slot,
Validators: []*eth.Validator{
{
EffectiveBalance: params.BeaconConfig().MaxEffectiveBalanceElectra,
},
},
Balances: []uint64{params.BeaconConfig().MaxEffectiveBalanceElectra},
EarliestExitEpoch: epoch - params.BeaconConfig().MaxSeedLookahead*2, // Old, relative to slot.
ExitBalanceToConsume: math.Gwei(20_000_000),
})
require.NoError(t, err)
activeBal, err := helpers.TotalActiveBalance(st)
require.NoError(t, err)

exitBal := math.Gwei(10_000_000)

wantExitBalToConsume := helpers.ActivationExitChurnLimit(math.Gwei(activeBal)) - exitBal

ee, err := st.ExitEpochAndUpdateChurn(exitBal)
require.NoError(t, err)

wantExitEpoch := helpers.ActivationExitEpoch(epoch)
require.Equal(t, wantExitEpoch, ee)

p := st.ToProto()
pb, ok := p.(*eth.BeaconStateElectra)
if !ok {
t.Fatal("wrong proto")
}
require.Equal(t, wantExitBalToConsume, pb.ExitBalanceToConsume)
require.Equal(t, wantExitEpoch, pb.EarliestExitEpoch)
})

t.Run("state exit bal to consume is less than activation exit churn limit", func(t *testing.T) {
st, err := state_native.InitializeFromProtoElectra(&eth.BeaconStateElectra{
Slot: slot,
Validators: []*eth.Validator{
{
EffectiveBalance: params.BeaconConfig().MaxEffectiveBalanceElectra,
},
},
Balances: []uint64{params.BeaconConfig().MaxEffectiveBalanceElectra},
EarliestExitEpoch: epoch,
ExitBalanceToConsume: math.Gwei(20_000_000),
})
require.NoError(t, err)
activeBal, err := helpers.TotalActiveBalance(st)
require.NoError(t, err)

activationExitChurnLimit := helpers.ActivationExitChurnLimit(math.Gwei(activeBal))
exitBal := activationExitChurnLimit * 2

wantExitBalToConsume := math.Gwei(0)

ee, err := st.ExitEpochAndUpdateChurn(exitBal)
require.NoError(t, err)

wantExitEpoch := helpers.ActivationExitEpoch(epoch) + 1
require.Equal(t, wantExitEpoch, ee)

p := st.ToProto()
pb, ok := p.(*eth.BeaconStateElectra)
if !ok {
t.Fatal("wrong proto")
}
require.Equal(t, wantExitBalToConsume, pb.ExitBalanceToConsume)
require.Equal(t, wantExitEpoch, pb.EarliestExitEpoch)
})

t.Run("state earliest exit epoch is in the future and exit balance is less than state", func(t *testing.T) {
st, err := state_native.InitializeFromProtoElectra(&eth.BeaconStateElectra{
Slot: slot,
Validators: []*eth.Validator{
{
EffectiveBalance: params.BeaconConfig().MaxEffectiveBalanceElectra,
},
},
Balances: []uint64{params.BeaconConfig().MaxEffectiveBalanceElectra},
EarliestExitEpoch: epoch + 10_000,
ExitBalanceToConsume: math.Gwei(20_000_000),
})
require.NoError(t, err)

exitBal := math.Gwei(10_000_000)

wantExitBalToConsume := math.Gwei(20_000_000) - exitBal

ee, err := st.ExitEpochAndUpdateChurn(exitBal)
require.NoError(t, err)

wantExitEpoch := epoch + 10_000
require.Equal(t, wantExitEpoch, ee)

p := st.ToProto()
pb, ok := p.(*eth.BeaconStateElectra)
if !ok {
t.Fatal("wrong proto")
}
require.Equal(t, wantExitBalToConsume, pb.ExitBalanceToConsume)
require.Equal(t, wantExitEpoch, pb.EarliestExitEpoch)
})

t.Run("state earliest exit epoch is in the future and exit balance exceeds state", func(t *testing.T) {
st, err := state_native.InitializeFromProtoElectra(&eth.BeaconStateElectra{
Slot: slot,
Validators: []*eth.Validator{
{
EffectiveBalance: params.BeaconConfig().MaxEffectiveBalanceElectra,
},
},
Balances: []uint64{params.BeaconConfig().MaxEffectiveBalanceElectra},
EarliestExitEpoch: epoch + 10_000,
ExitBalanceToConsume: math.Gwei(20_000_000),
})
require.NoError(t, err)

exitBal := math.Gwei(40_000_000)
activeBal, err := helpers.TotalActiveBalance(st)
require.NoError(t, err)
activationExitChurnLimit := helpers.ActivationExitChurnLimit(math.Gwei(activeBal))
wantExitBalToConsume := activationExitChurnLimit - 20_000_000

ee, err := st.ExitEpochAndUpdateChurn(exitBal)
require.NoError(t, err)

wantExitEpoch := epoch + 10_000 + 1
require.Equal(t, wantExitEpoch, ee)

p := st.ToProto()
pb, ok := p.(*eth.BeaconStateElectra)
if !ok {
t.Fatal("wrong proto")
}
require.Equal(t, wantExitBalToConsume, pb.ExitBalanceToConsume)
require.Equal(t, wantExitEpoch, pb.EarliestExitEpoch)
})

t.Run("earlier than electra returns error", func(t *testing.T) {
st, err := state_native.InitializeFromProtoDeneb(&eth.BeaconStateDeneb{})
require.NoError(t, err)
_, err = st.ExitEpochAndUpdateChurn(0)
require.ErrorContains(t, "is not supported", err)
})
}

0 comments on commit 00d1f54

Please sign in to comment.