From 228033f7a379abbd1026df8eace0d41f1674c485 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Tue, 13 Apr 2021 18:32:49 -0700 Subject: [PATCH] Pass SlashValidator func as argument to `ProcessProposer[Attester]Slashings` (#8763) * Pass slash validator func as argument * Gazelle --- beacon-chain/core/blocks/BUILD.bazel | 1 + beacon-chain/core/blocks/attester_slashing.go | 4 ++-- beacon-chain/core/blocks/attester_slashing_test.go | 7 ++++--- .../core/blocks/block_operations_fuzz_test.go | 5 +++-- beacon-chain/core/blocks/block_regression_test.go | 3 ++- beacon-chain/core/blocks/proposer_slashing.go | 9 +++++---- beacon-chain/core/blocks/proposer_slashing_test.go | 9 +++++---- beacon-chain/core/blocks/spectest/BUILD.bazel | 2 ++ .../core/blocks/spectest/attester_slashing_test.go | 7 ++++++- .../core/blocks/spectest/proposer_slashing_test.go | 7 ++++++- beacon-chain/core/state/BUILD.bazel | 1 + beacon-chain/core/state/transition.go | 12 ++++++++++-- beacon-chain/core/state/transition_no_verify_sig.go | 5 +++-- 13 files changed, 50 insertions(+), 22 deletions(-) diff --git a/beacon-chain/core/blocks/BUILD.bazel b/beacon-chain/core/blocks/BUILD.bazel index 11ae214b0..326d4d0cb 100644 --- a/beacon-chain/core/blocks/BUILD.bazel +++ b/beacon-chain/core/blocks/BUILD.bazel @@ -71,6 +71,7 @@ go_test( shard_count = 2, deps = [ "//beacon-chain/core/helpers:go_default_library", + "//beacon-chain/core/validators:go_default_library", "//beacon-chain/p2p/types:go_default_library", "//beacon-chain/state/interface:go_default_library", "//beacon-chain/state/stateV0:go_default_library", diff --git a/beacon-chain/core/blocks/attester_slashing.go b/beacon-chain/core/blocks/attester_slashing.go index 4338c927e..dd635ef5c 100644 --- a/beacon-chain/core/blocks/attester_slashing.go +++ b/beacon-chain/core/blocks/attester_slashing.go @@ -8,7 +8,6 @@ import ( types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" - v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" "github.com/prysmaticlabs/prysm/shared/attestationutil" "github.com/prysmaticlabs/prysm/shared/slashutil" @@ -38,6 +37,7 @@ func ProcessAttesterSlashings( ctx context.Context, beaconState iface.BeaconState, b *ethpb.SignedBeaconBlock, + slashFunc slashValidatorFunc, ) (iface.BeaconState, error) { if err := helpers.VerifyNilBeaconBlock(b); err != nil { return nil, err @@ -62,7 +62,7 @@ func ProcessAttesterSlashings( return nil, err } if helpers.IsSlashableValidator(val.ActivationEpoch(), val.WithdrawableEpoch(), val.Slashed(), currentEpoch) { - beaconState, err = v.SlashValidator(beaconState, types.ValidatorIndex(validatorIndex)) + beaconState, err = slashFunc(beaconState, types.ValidatorIndex(validatorIndex)) if err != nil { return nil, errors.Wrapf(err, "could not slash validator index %d", validatorIndex) diff --git a/beacon-chain/core/blocks/attester_slashing_test.go b/beacon-chain/core/blocks/attester_slashing_test.go index 53715a4ed..6f2787e05 100644 --- a/beacon-chain/core/blocks/attester_slashing_test.go +++ b/beacon-chain/core/blocks/attester_slashing_test.go @@ -8,6 +8,7 @@ import ( ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" + v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators" "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bls" @@ -57,7 +58,7 @@ func TestProcessAttesterSlashings_DataNotSlashable(t *testing.T) { AttesterSlashings: slashings, }, } - _, err = blocks.ProcessAttesterSlashings(context.Background(), beaconState, b) + _, err = blocks.ProcessAttesterSlashings(context.Background(), beaconState, b, v.SlashValidator) assert.ErrorContains(t, "attestations are not slashable", err) } @@ -92,7 +93,7 @@ func TestProcessAttesterSlashings_IndexedAttestationFailedToVerify(t *testing.T) }, } - _, err = blocks.ProcessAttesterSlashings(context.Background(), beaconState, b) + _, err = blocks.ProcessAttesterSlashings(context.Background(), beaconState, b, v.SlashValidator) assert.ErrorContains(t, "validator indices count exceeds MAX_VALIDATORS_PER_COMMITTEE", err) } @@ -144,7 +145,7 @@ func TestProcessAttesterSlashings_AppliesCorrectStatus(t *testing.T) { }, } - newState, err := blocks.ProcessAttesterSlashings(context.Background(), beaconState, b) + newState, err := blocks.ProcessAttesterSlashings(context.Background(), beaconState, b, v.SlashValidator) require.NoError(t, err) newRegistry := newState.Validators() diff --git a/beacon-chain/core/blocks/block_operations_fuzz_test.go b/beacon-chain/core/blocks/block_operations_fuzz_test.go index 0e2fb6ebd..bc0491d20 100644 --- a/beacon-chain/core/blocks/block_operations_fuzz_test.go +++ b/beacon-chain/core/blocks/block_operations_fuzz_test.go @@ -7,6 +7,7 @@ import ( fuzz "github.com/google/gofuzz" types "github.com/prysmaticlabs/eth2-types" eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" + v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators" "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/params" @@ -172,7 +173,7 @@ func TestFuzzProcessProposerSlashings_10000(t *testing.T) { fuzzer.Fuzz(b) s, err := stateV0.InitializeFromProtoUnsafe(state) require.NoError(t, err) - r, err := ProcessProposerSlashings(ctx, s, b) + r, err := ProcessProposerSlashings(ctx, s, b, v.SlashValidator) if err != nil && r != nil { t.Fatalf("return value should be nil on err. found: %v on error: %v for state: %v and block: %v", r, err, state, b) } @@ -203,7 +204,7 @@ func TestFuzzProcessAttesterSlashings_10000(t *testing.T) { fuzzer.Fuzz(b) s, err := stateV0.InitializeFromProtoUnsafe(state) require.NoError(t, err) - r, err := ProcessAttesterSlashings(ctx, s, b) + r, err := ProcessAttesterSlashings(ctx, s, b, v.SlashValidator) if err != nil && r != nil { t.Fatalf("return value should be nil on err. found: %v on error: %v for state: %v and block: %v", r, err, state, b) } diff --git a/beacon-chain/core/blocks/block_regression_test.go b/beacon-chain/core/blocks/block_regression_test.go index a183263ac..aad0a576b 100644 --- a/beacon-chain/core/blocks/block_regression_test.go +++ b/beacon-chain/core/blocks/block_regression_test.go @@ -8,6 +8,7 @@ import ( ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" + v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators" "github.com/prysmaticlabs/prysm/shared/bls" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" @@ -90,7 +91,7 @@ func TestProcessAttesterSlashings_RegressionSlashableIndices(t *testing.T) { }, } - newState, err := blocks.ProcessAttesterSlashings(context.Background(), beaconState, b) + newState, err := blocks.ProcessAttesterSlashings(context.Background(), beaconState, b, v.SlashValidator) require.NoError(t, err) newRegistry := newState.Validators() if !newRegistry[expectedSlashedVal].Slashed { diff --git a/beacon-chain/core/blocks/proposer_slashing.go b/beacon-chain/core/blocks/proposer_slashing.go index d839b9f12..923ebe405 100644 --- a/beacon-chain/core/blocks/proposer_slashing.go +++ b/beacon-chain/core/blocks/proposer_slashing.go @@ -6,13 +6,15 @@ import ( "github.com/gogo/protobuf/proto" "github.com/pkg/errors" + types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" - v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" "github.com/prysmaticlabs/prysm/shared/params" ) +type slashValidatorFunc func(iface.BeaconState, types.ValidatorIndex) (iface.BeaconState, error) + // ProcessProposerSlashings is one of the operations performed // on each processed beacon block to slash proposers based on // slashing conditions if any slashable events occurred. @@ -42,6 +44,7 @@ func ProcessProposerSlashings( _ context.Context, beaconState iface.BeaconState, b *ethpb.SignedBeaconBlock, + slashFunc slashValidatorFunc, ) (iface.BeaconState, error) { if err := helpers.VerifyNilBeaconBlock(b); err != nil { return nil, err @@ -56,9 +59,7 @@ func ProcessProposerSlashings( if err = VerifyProposerSlashing(beaconState, slashing); err != nil { return nil, errors.Wrapf(err, "could not verify proposer slashing %d", idx) } - beaconState, err = v.SlashValidator( - beaconState, slashing.Header_1.Header.ProposerIndex, - ) + beaconState, err = slashFunc(beaconState, slashing.Header_1.Header.ProposerIndex) if err != nil { return nil, errors.Wrapf(err, "could not slash proposer index %d", slashing.Header_1.Header.ProposerIndex) } diff --git a/beacon-chain/core/blocks/proposer_slashing_test.go b/beacon-chain/core/blocks/proposer_slashing_test.go index 1bb336b78..4f2fb0c75 100644 --- a/beacon-chain/core/blocks/proposer_slashing_test.go +++ b/beacon-chain/core/blocks/proposer_slashing_test.go @@ -9,6 +9,7 @@ import ( ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" + v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" @@ -49,7 +50,7 @@ func TestProcessProposerSlashings_UnmatchedHeaderSlots(t *testing.T) { }, } want := "mismatched header slots" - _, err := blocks.ProcessProposerSlashings(context.Background(), beaconState, b) + _, err := blocks.ProcessProposerSlashings(context.Background(), beaconState, b, v.SlashValidator) assert.ErrorContains(t, want, err) } @@ -82,7 +83,7 @@ func TestProcessProposerSlashings_SameHeaders(t *testing.T) { }, } want := "expected slashing headers to differ" - _, err := blocks.ProcessProposerSlashings(context.Background(), beaconState, b) + _, err := blocks.ProcessProposerSlashings(context.Background(), beaconState, b, v.SlashValidator) assert.ErrorContains(t, want, err) } @@ -132,7 +133,7 @@ func TestProcessProposerSlashings_ValidatorNotSlashable(t *testing.T) { "validator with key %#x is not slashable", bytesutil.ToBytes48(beaconState.Validators()[0].PublicKey), ) - _, err = blocks.ProcessProposerSlashings(context.Background(), beaconState, b) + _, err = blocks.ProcessProposerSlashings(context.Background(), beaconState, b, v.SlashValidator) assert.ErrorContains(t, want, err) } @@ -171,7 +172,7 @@ func TestProcessProposerSlashings_AppliesCorrectStatus(t *testing.T) { block := testutil.NewBeaconBlock() block.Block.Body.ProposerSlashings = slashings - newState, err := blocks.ProcessProposerSlashings(context.Background(), beaconState, block) + newState, err := blocks.ProcessProposerSlashings(context.Background(), beaconState, block, v.SlashValidator) require.NoError(t, err) newStateVals := newState.Validators() diff --git a/beacon-chain/core/blocks/spectest/BUILD.bazel b/beacon-chain/core/blocks/spectest/BUILD.bazel index 91b9c703d..f3ca32606 100644 --- a/beacon-chain/core/blocks/spectest/BUILD.bazel +++ b/beacon-chain/core/blocks/spectest/BUILD.bazel @@ -36,6 +36,7 @@ go_test( "//beacon-chain/core/helpers:go_default_library", "//beacon-chain/core/state:go_default_library", "//beacon-chain/core/state/stateutils:go_default_library", + "//beacon-chain/core/validators:go_default_library", "//beacon-chain/state/interface:go_default_library", "//beacon-chain/state/stateV0:go_default_library", "//proto/beacon/p2p/v1:go_default_library", @@ -72,6 +73,7 @@ go_test( "//beacon-chain/core/helpers:go_default_library", "//beacon-chain/core/state:go_default_library", "//beacon-chain/core/state/stateutils:go_default_library", + "//beacon-chain/core/validators:go_default_library", "//beacon-chain/state/interface:go_default_library", "//beacon-chain/state/stateV0:go_default_library", "//proto/beacon/p2p/v1:go_default_library", diff --git a/beacon-chain/core/blocks/spectest/attester_slashing_test.go b/beacon-chain/core/blocks/spectest/attester_slashing_test.go index 1a0abf1fc..5bde8edd0 100644 --- a/beacon-chain/core/blocks/spectest/attester_slashing_test.go +++ b/beacon-chain/core/blocks/spectest/attester_slashing_test.go @@ -1,11 +1,14 @@ package spectest import ( + "context" "path" "testing" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" + v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators" + iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" "github.com/prysmaticlabs/prysm/shared/params/spectest" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/require" @@ -24,7 +27,9 @@ func runAttesterSlashingTest(t *testing.T, config string) { require.NoError(t, attSlashing.UnmarshalSSZ(attSlashingFile), "Failed to unmarshal") body := ðpb.BeaconBlockBody{AttesterSlashings: []*ethpb.AttesterSlashing{attSlashing}} - testutil.RunBlockOperationTest(t, folderPath, body, blocks.ProcessAttesterSlashings) + testutil.RunBlockOperationTest(t, folderPath, body, func(ctx context.Context, s iface.BeaconState, b *ethpb.SignedBeaconBlock) (iface.BeaconState, error) { + return blocks.ProcessAttesterSlashings(ctx, s, b, v.SlashValidator) + }) }) } } diff --git a/beacon-chain/core/blocks/spectest/proposer_slashing_test.go b/beacon-chain/core/blocks/spectest/proposer_slashing_test.go index a58232f60..cded2b8bc 100644 --- a/beacon-chain/core/blocks/spectest/proposer_slashing_test.go +++ b/beacon-chain/core/blocks/spectest/proposer_slashing_test.go @@ -1,11 +1,14 @@ package spectest import ( + "context" "path" "testing" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" + v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators" + iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" "github.com/prysmaticlabs/prysm/shared/params/spectest" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/require" @@ -24,7 +27,9 @@ func runProposerSlashingTest(t *testing.T, config string) { require.NoError(t, proposerSlashing.UnmarshalSSZ(proposerSlashingFile), "Failed to unmarshal") body := ðpb.BeaconBlockBody{ProposerSlashings: []*ethpb.ProposerSlashing{proposerSlashing}} - testutil.RunBlockOperationTest(t, folderPath, body, blocks.ProcessProposerSlashings) + testutil.RunBlockOperationTest(t, folderPath, body, func(ctx context.Context, s iface.BeaconState, b *ethpb.SignedBeaconBlock) (iface.BeaconState, error) { + return blocks.ProcessProposerSlashings(ctx, s, b, v.SlashValidator) + }) }) } } diff --git a/beacon-chain/core/state/BUILD.bazel b/beacon-chain/core/state/BUILD.bazel index b37da901d..31fbd60a3 100644 --- a/beacon-chain/core/state/BUILD.bazel +++ b/beacon-chain/core/state/BUILD.bazel @@ -31,6 +31,7 @@ go_library( "//beacon-chain/core/epoch/precompute:go_default_library", "//beacon-chain/core/helpers:go_default_library", "//beacon-chain/core/state/interop:go_default_library", + "//beacon-chain/core/validators:go_default_library", "//beacon-chain/state/interface:go_default_library", "//beacon-chain/state/stateV0:go_default_library", "//proto/beacon/p2p/v1:go_default_library", diff --git a/beacon-chain/core/state/transition.go b/beacon-chain/core/state/transition.go index 86e64b869..72643893f 100644 --- a/beacon-chain/core/state/transition.go +++ b/beacon-chain/core/state/transition.go @@ -17,6 +17,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/core/epoch/precompute" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/core/state/interop" + v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" "github.com/prysmaticlabs/prysm/shared/mathutil" "github.com/prysmaticlabs/prysm/shared/params" @@ -30,6 +31,13 @@ type processFunc func(context.Context, iface.BeaconState, *ethpb.SignedBeaconBlo var processDepositsFunc = func(ctx context.Context, s iface.BeaconState, blk *ethpb.SignedBeaconBlock) (iface.BeaconState, error) { return b.ProcessDeposits(ctx, s, blk.Block.Body.Deposits) } +var processProposerSlashingFunc = func(ctx context.Context, s iface.BeaconState, blk *ethpb.SignedBeaconBlock) (iface.BeaconState, error) { + return b.ProcessProposerSlashings(ctx, s, blk, v.SlashValidator) +} + +var processAttesterSlashingFunc = func(ctx context.Context, s iface.BeaconState, blk *ethpb.SignedBeaconBlock) (iface.BeaconState, error) { + return b.ProcessAttesterSlashings(ctx, s, blk, v.SlashValidator) +} // This defines the processing block routine as outlined in eth2 spec: // https://github.com/ethereum/eth2.0-specs/blob/dev/specs/phase0/beacon-chain.md#block-processing @@ -38,8 +46,8 @@ var processingPipeline = []processFunc{ b.ProcessRandao, b.ProcessEth1DataInBlock, VerifyOperationLengths, - b.ProcessProposerSlashings, - b.ProcessAttesterSlashings, + processProposerSlashingFunc, + processAttesterSlashingFunc, b.ProcessAttestations, processDepositsFunc, b.ProcessVoluntaryExits, diff --git a/beacon-chain/core/state/transition_no_verify_sig.go b/beacon-chain/core/state/transition_no_verify_sig.go index 1f739eae7..fdd463feb 100644 --- a/beacon-chain/core/state/transition_no_verify_sig.go +++ b/beacon-chain/core/state/transition_no_verify_sig.go @@ -8,6 +8,7 @@ import ( "github.com/pkg/errors" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" + v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" "github.com/prysmaticlabs/prysm/shared/bls" "github.com/prysmaticlabs/prysm/shared/featureconfig" @@ -244,11 +245,11 @@ func ProcessOperationsNoVerifyAttsSigs( return nil, errors.Wrap(err, "could not verify operation lengths") } - state, err := b.ProcessProposerSlashings(ctx, state, signedBeaconBlock) + state, err := b.ProcessProposerSlashings(ctx, state, signedBeaconBlock, v.SlashValidator) if err != nil { return nil, errors.Wrap(err, "could not process block proposer slashings") } - state, err = b.ProcessAttesterSlashings(ctx, state, signedBeaconBlock) + state, err = b.ProcessAttesterSlashings(ctx, state, signedBeaconBlock, v.SlashValidator) if err != nil { return nil, errors.Wrap(err, "could not process block attester slashings") }