-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
eip-7251: process_effective_balance_updates (#14003)
* eip-7251: process_effective_balance_updates Spectests for process_effective_balance_updates process_effective_balance_updates unit tests * PR feedback from the amazing @rkapka --------- Co-authored-by: james-prysm <90280386+james-prysm@users.noreply.github.com>
- Loading branch information
1 parent
8e6d39a
commit 2542189
Showing
9 changed files
with
239 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
beacon-chain/core/electra/effective_balance_updates_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package electra_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/electra" | ||
"github.com/prysmaticlabs/prysm/v5/beacon-chain/state" | ||
state_native "github.com/prysmaticlabs/prysm/v5/beacon-chain/state/state-native" | ||
"github.com/prysmaticlabs/prysm/v5/config/params" | ||
eth "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" | ||
"github.com/prysmaticlabs/prysm/v5/testing/require" | ||
) | ||
|
||
func TestProcessEffectiveBalnceUpdates(t *testing.T) { | ||
effBalanceInc := params.BeaconConfig().EffectiveBalanceIncrement | ||
hysteresisInc := effBalanceInc / params.BeaconConfig().HysteresisQuotient | ||
downwardThreshold := hysteresisInc * params.BeaconConfig().HysteresisDownwardMultiplier | ||
upwardThreshold := hysteresisInc * params.BeaconConfig().HysteresisUpwardMultiplier | ||
|
||
tests := []struct { | ||
name string | ||
state state.BeaconState | ||
wantErr bool | ||
check func(*testing.T, state.BeaconState) | ||
}{ | ||
{ | ||
name: "validator with compounding withdrawal credentials updates effective balance", | ||
state: func() state.BeaconState { | ||
pb := ð.BeaconStateElectra{ | ||
Validators: []*eth.Validator{ | ||
{ | ||
EffectiveBalance: params.BeaconConfig().MinActivationBalance, | ||
WithdrawalCredentials: []byte{params.BeaconConfig().CompoundingWithdrawalPrefixByte, 0x11}, | ||
}, | ||
}, | ||
Balances: []uint64{ | ||
params.BeaconConfig().MaxEffectiveBalanceElectra * 2, | ||
}, | ||
} | ||
st, err := state_native.InitializeFromProtoElectra(pb) | ||
require.NoError(t, err) | ||
return st | ||
}(), | ||
check: func(t *testing.T, bs state.BeaconState) { | ||
val, err := bs.ValidatorAtIndex(0) | ||
require.NoError(t, err) | ||
require.Equal(t, params.BeaconConfig().MaxEffectiveBalanceElectra, val.EffectiveBalance) | ||
}, | ||
}, | ||
{ | ||
name: "validator without compounding withdrawal credentials updates effective balance", | ||
state: func() state.BeaconState { | ||
pb := ð.BeaconStateElectra{ | ||
Validators: []*eth.Validator{ | ||
{ | ||
EffectiveBalance: params.BeaconConfig().MinActivationBalance / 2, | ||
WithdrawalCredentials: nil, | ||
}, | ||
}, | ||
Balances: []uint64{ | ||
params.BeaconConfig().MaxEffectiveBalanceElectra, | ||
}, | ||
} | ||
st, err := state_native.InitializeFromProtoElectra(pb) | ||
require.NoError(t, err) | ||
return st | ||
}(), | ||
check: func(t *testing.T, bs state.BeaconState) { | ||
val, err := bs.ValidatorAtIndex(0) | ||
require.NoError(t, err) | ||
require.Equal(t, params.BeaconConfig().MinActivationBalance, val.EffectiveBalance) | ||
}, | ||
}, | ||
{ | ||
name: "validator effective balance moves only when outside of threshold", | ||
state: func() state.BeaconState { | ||
pb := ð.BeaconStateElectra{ | ||
Validators: []*eth.Validator{ | ||
{ | ||
EffectiveBalance: params.BeaconConfig().MinActivationBalance, | ||
WithdrawalCredentials: []byte{params.BeaconConfig().CompoundingWithdrawalPrefixByte, 0x11}, | ||
}, | ||
{ | ||
EffectiveBalance: params.BeaconConfig().MinActivationBalance, | ||
WithdrawalCredentials: []byte{params.BeaconConfig().CompoundingWithdrawalPrefixByte, 0x11}, | ||
}, | ||
{ | ||
EffectiveBalance: params.BeaconConfig().MinActivationBalance, | ||
WithdrawalCredentials: []byte{params.BeaconConfig().CompoundingWithdrawalPrefixByte, 0x11}, | ||
}, | ||
{ | ||
EffectiveBalance: params.BeaconConfig().MinActivationBalance, | ||
WithdrawalCredentials: []byte{params.BeaconConfig().CompoundingWithdrawalPrefixByte, 0x11}, | ||
}, | ||
}, | ||
Balances: []uint64{ | ||
params.BeaconConfig().MinActivationBalance - downwardThreshold - 1, // beyond downward threshold | ||
params.BeaconConfig().MinActivationBalance - downwardThreshold + 1, // within downward threshold | ||
params.BeaconConfig().MinActivationBalance + upwardThreshold + 1, // beyond upward threshold | ||
params.BeaconConfig().MinActivationBalance + upwardThreshold - 1, // within upward threshold | ||
}, | ||
} | ||
st, err := state_native.InitializeFromProtoElectra(pb) | ||
require.NoError(t, err) | ||
return st | ||
}(), | ||
check: func(t *testing.T, bs state.BeaconState) { | ||
// validator 0 has a balance diff exceeding the threshold so a diff should be applied to | ||
// effective balance and it moves by effective balance increment. | ||
val, err := bs.ValidatorAtIndex(0) | ||
require.NoError(t, err) | ||
require.Equal(t, params.BeaconConfig().MinActivationBalance-params.BeaconConfig().EffectiveBalanceIncrement, val.EffectiveBalance) | ||
|
||
// validator 1 has a balance diff within the threshold so the effective balance should not | ||
// have changed. | ||
val, err = bs.ValidatorAtIndex(1) | ||
require.NoError(t, err) | ||
require.Equal(t, params.BeaconConfig().MinActivationBalance, val.EffectiveBalance) | ||
|
||
// Validator 2 has a balance diff exceeding the threshold so a diff should be applied to the | ||
// effective balance and it moves by effective balance increment. | ||
val, err = bs.ValidatorAtIndex(2) | ||
require.NoError(t, err) | ||
require.Equal(t, params.BeaconConfig().MinActivationBalance+params.BeaconConfig().EffectiveBalanceIncrement, val.EffectiveBalance) | ||
|
||
// Validator 3 has a balance diff within the threshold so the effective balance should not | ||
// have changed. | ||
val, err = bs.ValidatorAtIndex(3) | ||
require.NoError(t, err) | ||
require.Equal(t, params.BeaconConfig().MinActivationBalance, val.EffectiveBalance) | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := electra.ProcessEffectiveBalanceUpdates(tt.state) | ||
require.Equal(t, tt.wantErr, err != nil, "unexpected error returned wanted error=nil (%s), got error=%s", tt.wantErr, err) | ||
if tt.check != nil { | ||
tt.check(t, tt.state) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
testing/spectest/mainnet/electra/epoch_processing/effective_balance_updates_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package epoch_processing | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/prysmaticlabs/prysm/v5/testing/spectest/shared/electra/epoch_processing" | ||
) | ||
|
||
func TestMainnet_electra_EpochProcessing_EffectiveBalanceUpdates(t *testing.T) { | ||
epoch_processing.RunEffectiveBalanceUpdatesTests(t, "mainnet") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
testing/spectest/minimal/electra/epoch_processing/effective_balance_updates_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package epoch_processing | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/prysmaticlabs/prysm/v5/testing/spectest/shared/electra/epoch_processing" | ||
) | ||
|
||
func TestMinimal_electra_EpochProcessing_EffectiveBalanceUpdates(t *testing.T) { | ||
epoch_processing.RunEffectiveBalanceUpdatesTests(t, "minimal") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
testing/spectest/shared/electra/epoch_processing/effective_balance_updates.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package epoch_processing | ||
|
||
import ( | ||
"path" | ||
"testing" | ||
|
||
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/electra" | ||
"github.com/prysmaticlabs/prysm/v5/beacon-chain/state" | ||
"github.com/prysmaticlabs/prysm/v5/testing/require" | ||
"github.com/prysmaticlabs/prysm/v5/testing/spectest/utils" | ||
) | ||
|
||
// RunEffectiveBalanceUpdatesTests executes "epoch_processing/effective_balance_updates" tests. | ||
func RunEffectiveBalanceUpdatesTests(t *testing.T, config string) { | ||
require.NoError(t, utils.SetConfig(t, config)) | ||
|
||
testFolders, testsFolderPath := utils.TestFolders(t, config, "electra", "epoch_processing/effective_balance_updates/pyspec_tests") | ||
for _, folder := range testFolders { | ||
t.Run(folder.Name(), func(t *testing.T) { | ||
folderPath := path.Join(testsFolderPath, folder.Name()) | ||
RunEpochOperationTest(t, folderPath, processEffectiveBalanceUpdatesWrapper) | ||
}) | ||
} | ||
} | ||
|
||
func processEffectiveBalanceUpdatesWrapper(t *testing.T, st state.BeaconState) (state.BeaconState, error) { | ||
err := electra.ProcessEffectiveBalanceUpdates(st) | ||
require.NoError(t, err, "Could not process final updates") | ||
return st, nil | ||
} |