-
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.
Electra: beacon-chain/core/helpers (#13921)
* Electra helpers * Electra helper tests and other fixes * @terencechain feedback
- Loading branch information
1 parent
bf5e667
commit 8df62a5
Showing
7 changed files
with
663 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,52 @@ | ||
package helpers | ||
|
||
import ( | ||
"github.com/prysmaticlabs/prysm/v5/config/params" | ||
) | ||
|
||
// BalanceChurnLimit for the current active balance, in gwei. | ||
// New in Electra EIP-7251: https://eips.ethereum.org/EIPS/eip-7251 | ||
// | ||
// Spec definition: | ||
// | ||
// def get_balance_churn_limit(state: BeaconState) -> Gwei: | ||
// """ | ||
// Return the churn limit for the current epoch. | ||
// """ | ||
// churn = max( | ||
// MIN_PER_EPOCH_CHURN_LIMIT_ELECTRA, | ||
// get_total_active_balance(state) // CHURN_LIMIT_QUOTIENT | ||
// ) | ||
// return churn - churn % EFFECTIVE_BALANCE_INCREMENT | ||
func BalanceChurnLimit(activeBalanceGwei uint64) uint64 { | ||
churn := max( | ||
params.BeaconConfig().MinPerEpochChurnLimitElectra, | ||
(activeBalanceGwei / params.BeaconConfig().ChurnLimitQuotient), | ||
) | ||
return churn - churn%params.BeaconConfig().EffectiveBalanceIncrement | ||
} | ||
|
||
// ActivationExitChurnLimit for the current active balance, in gwei. | ||
// New in Electra EIP-7251: https://eips.ethereum.org/EIPS/eip-7251 | ||
// | ||
// Spec definition: | ||
// | ||
// def get_activation_exit_churn_limit(state: BeaconState) -> Gwei: | ||
// """ | ||
// Return the churn limit for the current epoch dedicated to activations and exits. | ||
// """ | ||
// return min(MAX_PER_EPOCH_ACTIVATION_EXIT_CHURN_LIMIT, get_balance_churn_limit(state)) | ||
func ActivationExitChurnLimit(activeBalanceGwei uint64) uint64 { | ||
return min(params.BeaconConfig().MaxPerEpochActivationExitChurnLimit, BalanceChurnLimit(activeBalanceGwei)) | ||
} | ||
|
||
// ConsolidationChurnLimit for the current active balance, in gwei. | ||
// New in EIP-7251: https://eips.ethereum.org/EIPS/eip-7251 | ||
// | ||
// Spec definition: | ||
// | ||
// def get_consolidation_churn_limit(state: BeaconState) -> Gwei: | ||
// return get_balance_churn_limit(state) - get_activation_exit_churn_limit(state) | ||
func ConsolidationChurnLimit(activeBalanceGwei uint64) uint64 { | ||
return BalanceChurnLimit(activeBalanceGwei) - ActivationExitChurnLimit(activeBalanceGwei) | ||
} |
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,71 @@ | ||
package helpers_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/helpers" | ||
"github.com/prysmaticlabs/prysm/v5/config/params" | ||
"github.com/prysmaticlabs/prysm/v5/testing/assert" | ||
) | ||
|
||
func TestBalanceChurnLimit(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
activeBalance uint64 | ||
expected uint64 | ||
}{ | ||
{ | ||
name: "less than MIN_PER_EPOCH_CHURN_LIMIT_ELECTRA", | ||
activeBalance: 111, | ||
expected: params.BeaconConfig().MinPerEpochChurnLimitElectra, | ||
}, | ||
{ | ||
name: "modulo EFFECTIVE_BALANCE_INCREMENT", | ||
activeBalance: 111 + params.BeaconConfig().MinPerEpochChurnLimitElectra*params.BeaconConfig().ChurnLimitQuotient, | ||
expected: params.BeaconConfig().MinPerEpochChurnLimitElectra, | ||
}, | ||
{ | ||
name: "more than MIN_PER_EPOCH_CHURN_LIMIT_ELECTRA", | ||
activeBalance: 2000 * params.BeaconConfig().EffectiveBalanceIncrement * params.BeaconConfig().ChurnLimitQuotient, | ||
expected: 2000 * params.BeaconConfig().EffectiveBalanceIncrement, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assert.Equal(t, tt.expected, helpers.BalanceChurnLimit(tt.activeBalance)) | ||
}) | ||
} | ||
} | ||
|
||
func TestActivationExitChurnLimit(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
activeBalance uint64 | ||
expected uint64 | ||
}{ | ||
{ | ||
name: "less than MAX_PER_EPOCH_ACTIVATION_EXIT_CHURN_LIMIT", | ||
activeBalance: 1, | ||
expected: params.BeaconConfig().MinPerEpochChurnLimitElectra, | ||
}, | ||
{ | ||
name: "more than MAX_PER_EPOCH_ACTIVATION_EXIT_CHURN_LIMIT", | ||
activeBalance: 2000 * params.BeaconConfig().EffectiveBalanceIncrement * params.BeaconConfig().ChurnLimitQuotient, | ||
expected: params.BeaconConfig().MaxPerEpochActivationExitChurnLimit, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assert.Equal(t, tt.expected, helpers.ActivationExitChurnLimit(tt.activeBalance)) | ||
}) | ||
} | ||
} | ||
|
||
// FuzzConsolidationChurnLimit exercises BalanceChurnLimit and ActivationExitChurnLimit | ||
func FuzzConsolidationChurnLimit(f *testing.F) { | ||
f.Fuzz(func(t *testing.T, activeBalance uint64) { | ||
helpers.ConsolidationChurnLimit(activeBalance) | ||
}) | ||
} |
Oops, something went wrong.