mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-12 04:30:04 +00:00
Process slashing with precompute (#3797)
* Implemented ProcessSlashingsPrecompute * Tests for ProcessSlashingsPrecompute * Gaz * Lint * Removed comment
This commit is contained in:
parent
89ef6d6648
commit
aedd38092f
@ -7,6 +7,7 @@ go_library(
|
||||
"justification_finalization.go",
|
||||
"new.go",
|
||||
"reward_penalty.go",
|
||||
"slashing.go",
|
||||
"type.go",
|
||||
],
|
||||
importpath = "github.com/prysmaticlabs/prysm/beacon-chain/core/epoch/precompute",
|
||||
@ -31,6 +32,7 @@ go_test(
|
||||
"justification_finalization_test.go",
|
||||
"new_test.go",
|
||||
"reward_penalty_test.go",
|
||||
"slashing_test.go",
|
||||
],
|
||||
embed = [":go_default_library"],
|
||||
deps = [
|
||||
@ -41,6 +43,7 @@ go_test(
|
||||
"//proto/eth/v1alpha1:go_default_library",
|
||||
"//shared/params:go_default_library",
|
||||
"//shared/testutil:go_default_library",
|
||||
"@com_github_gogo_protobuf//proto:go_default_library",
|
||||
"@com_github_prysmaticlabs_go_bitfield//:go_default_library",
|
||||
],
|
||||
)
|
||||
|
34
beacon-chain/core/epoch/precompute/slashing.go
Normal file
34
beacon-chain/core/epoch/precompute/slashing.go
Normal file
@ -0,0 +1,34 @@
|
||||
package precompute
|
||||
|
||||
import (
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
||||
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
||||
"github.com/prysmaticlabs/prysm/shared/mathutil"
|
||||
"github.com/prysmaticlabs/prysm/shared/params"
|
||||
)
|
||||
|
||||
// ProcessSlashingsPrecompute processes the slashed validators during epoch processing.
|
||||
// This is an optimized version by passing in precomputed total epoch balances.
|
||||
func ProcessSlashingsPrecompute(state *pb.BeaconState, p *Balance) *pb.BeaconState {
|
||||
currentEpoch := helpers.CurrentEpoch(state)
|
||||
exitLength := params.BeaconConfig().EpochsPerSlashingsVector
|
||||
|
||||
// Compute the sum of state slashings
|
||||
totalSlashing := uint64(0)
|
||||
for _, slashing := range state.Slashings {
|
||||
totalSlashing += slashing
|
||||
}
|
||||
|
||||
// Compute slashing for each validator.
|
||||
for index, validator := range state.Validators {
|
||||
correctEpoch := (currentEpoch + exitLength/2) == validator.WithdrawableEpoch
|
||||
if validator.Slashed && correctEpoch {
|
||||
minSlashing := mathutil.Min(totalSlashing*3, p.CurrentEpoch)
|
||||
increment := params.BeaconConfig().EffectiveBalanceIncrement
|
||||
penaltyNumerator := validator.EffectiveBalance / increment * minSlashing
|
||||
penalty := penaltyNumerator / p.CurrentEpoch * increment
|
||||
state = helpers.DecreaseBalance(state, uint64(index), penalty)
|
||||
}
|
||||
}
|
||||
return state
|
||||
}
|
123
beacon-chain/core/epoch/precompute/slashing_test.go
Normal file
123
beacon-chain/core/epoch/precompute/slashing_test.go
Normal file
@ -0,0 +1,123 @@
|
||||
package precompute
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gogo/protobuf/proto"
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
||||
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
||||
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
|
||||
"github.com/prysmaticlabs/prysm/shared/params"
|
||||
)
|
||||
|
||||
func TestProcessSlashingsPrecompute_NotSlashed(t *testing.T) {
|
||||
s := &pb.BeaconState{
|
||||
Slot: 0,
|
||||
Validators: []*ethpb.Validator{{Slashed: true}},
|
||||
Balances: []uint64{params.BeaconConfig().MaxEffectiveBalance},
|
||||
Slashings: []uint64{0, 1e9},
|
||||
}
|
||||
bp := &Balance{CurrentEpoch: params.BeaconConfig().MaxEffectiveBalance}
|
||||
newState := ProcessSlashingsPrecompute(s, bp)
|
||||
wanted := params.BeaconConfig().MaxEffectiveBalance
|
||||
if newState.Balances[0] != wanted {
|
||||
t.Errorf("Wanted slashed balance: %d, got: %d", wanted, newState.Balances[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessSlashingsPrecompute_SlashedLess(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
state *pb.BeaconState
|
||||
want uint64
|
||||
}{
|
||||
{
|
||||
state: &pb.BeaconState{
|
||||
Validators: []*ethpb.Validator{
|
||||
{Slashed: true,
|
||||
WithdrawableEpoch: params.BeaconConfig().EpochsPerSlashingsVector / 2,
|
||||
EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
|
||||
{ExitEpoch: params.BeaconConfig().FarFutureEpoch, EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance}},
|
||||
Balances: []uint64{params.BeaconConfig().MaxEffectiveBalance, params.BeaconConfig().MaxEffectiveBalance},
|
||||
Slashings: []uint64{0, 1e9},
|
||||
},
|
||||
// penalty = validator balance / increment * (3*total_penalties) / total_balance * increment
|
||||
// 3000000000 = (32 * 1e9) / (1 * 1e9) * (3*1e9) / (32*1e9) * (1 * 1e9)
|
||||
want: uint64(29000000000), // 32 * 1e9 - 3000000000
|
||||
},
|
||||
{
|
||||
state: &pb.BeaconState{
|
||||
Validators: []*ethpb.Validator{
|
||||
{Slashed: true,
|
||||
WithdrawableEpoch: params.BeaconConfig().EpochsPerSlashingsVector / 2,
|
||||
EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
|
||||
{ExitEpoch: params.BeaconConfig().FarFutureEpoch, EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
|
||||
{ExitEpoch: params.BeaconConfig().FarFutureEpoch, EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
|
||||
},
|
||||
Balances: []uint64{params.BeaconConfig().MaxEffectiveBalance, params.BeaconConfig().MaxEffectiveBalance, params.BeaconConfig().MaxEffectiveBalance},
|
||||
Slashings: []uint64{0, 1e9},
|
||||
},
|
||||
// penalty = validator balance / increment * (3*total_penalties) / total_balance * increment
|
||||
// 1000000000 = (32 * 1e9) / (1 * 1e9) * (3*1e9) / (64*1e9) * (1 * 1e9)
|
||||
want: uint64(31000000000), // 32 * 1e9 - 1000000000
|
||||
},
|
||||
{
|
||||
state: &pb.BeaconState{
|
||||
Validators: []*ethpb.Validator{
|
||||
{Slashed: true,
|
||||
WithdrawableEpoch: params.BeaconConfig().EpochsPerSlashingsVector / 2,
|
||||
EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
|
||||
{ExitEpoch: params.BeaconConfig().FarFutureEpoch, EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
|
||||
{ExitEpoch: params.BeaconConfig().FarFutureEpoch, EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
|
||||
},
|
||||
Balances: []uint64{params.BeaconConfig().MaxEffectiveBalance, params.BeaconConfig().MaxEffectiveBalance, params.BeaconConfig().MaxEffectiveBalance},
|
||||
Slashings: []uint64{0, 2 * 1e9},
|
||||
},
|
||||
// penalty = validator balance / increment * (3*total_penalties) / total_balance * increment
|
||||
// 3000000000 = (32 * 1e9) / (1 * 1e9) * (3*2e9) / (64*1e9) * (1 * 1e9)
|
||||
want: uint64(29000000000), // 32 * 1e9 - 3000000000
|
||||
},
|
||||
{
|
||||
state: &pb.BeaconState{
|
||||
Validators: []*ethpb.Validator{
|
||||
{Slashed: true,
|
||||
WithdrawableEpoch: params.BeaconConfig().EpochsPerSlashingsVector / 2,
|
||||
EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance - params.BeaconConfig().EffectiveBalanceIncrement},
|
||||
{ExitEpoch: params.BeaconConfig().FarFutureEpoch, EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance - params.BeaconConfig().EffectiveBalanceIncrement}},
|
||||
Balances: []uint64{params.BeaconConfig().MaxEffectiveBalance - params.BeaconConfig().EffectiveBalanceIncrement, params.BeaconConfig().MaxEffectiveBalance - params.BeaconConfig().EffectiveBalanceIncrement},
|
||||
Slashings: []uint64{0, 1e9},
|
||||
},
|
||||
// penalty = validator balance / increment * (3*total_penalties) / total_balance * increment
|
||||
// 3000000000 = (32 * 1e9 - 1*1e9) / (1 * 1e9) * (3*1e9) / (31*1e9) * (1 * 1e9)
|
||||
want: uint64(28000000000), // 31 * 1e9 - 3000000000
|
||||
},
|
||||
}
|
||||
|
||||
for i, tt := range tests {
|
||||
t.Run(string(i), func(t *testing.T) {
|
||||
helpers.ClearAllCaches()
|
||||
|
||||
ab := uint64(0)
|
||||
for i, b := range tt.state.Balances {
|
||||
// Skip validator 0 since it's slashed
|
||||
if i == 0 {
|
||||
continue
|
||||
}
|
||||
ab += b
|
||||
}
|
||||
bp := &Balance{CurrentEpoch: ab}
|
||||
|
||||
original := proto.Clone(tt.state)
|
||||
newState := ProcessSlashingsPrecompute(tt.state, bp)
|
||||
|
||||
if newState.Balances[0] != tt.want {
|
||||
t.Errorf(
|
||||
"ProcessSlashings({%v}) = newState; newState.Balances[0] = %d; wanted %d",
|
||||
original,
|
||||
newState.Balances[0],
|
||||
tt.want,
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user