github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/core/blocks/block_regression_test.go (about) 1 package blocks_test 2 3 import ( 4 "context" 5 "testing" 6 7 types "github.com/prysmaticlabs/eth2-types" 8 "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" 9 "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" 10 v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators" 11 ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" 12 "github.com/prysmaticlabs/prysm/shared/bls" 13 "github.com/prysmaticlabs/prysm/shared/params" 14 "github.com/prysmaticlabs/prysm/shared/testutil" 15 "github.com/prysmaticlabs/prysm/shared/testutil/assert" 16 "github.com/prysmaticlabs/prysm/shared/testutil/require" 17 ) 18 19 func TestProcessAttesterSlashings_RegressionSlashableIndices(t *testing.T) { 20 21 beaconState, privKeys := testutil.DeterministicGenesisState(t, 5500) 22 for _, vv := range beaconState.Validators() { 23 vv.WithdrawableEpoch = types.Epoch(params.BeaconConfig().SlotsPerEpoch) 24 } 25 // This set of indices is very similar to the one from our sapphire testnet 26 // when close to 100 validators were incorrectly slashed. The set is from 0 -5500, 27 // instead of 55000 as it would take too long to generate a state. 28 setA := []uint64{21, 92, 236, 244, 281, 321, 510, 524, 29 538, 682, 828, 858, 913, 920, 922, 959, 1176, 1207, 30 1222, 1229, 1354, 1394, 1436, 1454, 1510, 1550, 31 1552, 1576, 1645, 1704, 1842, 1967, 2076, 2111, 2134, 2307, 32 2343, 2354, 2417, 2524, 2532, 2555, 2740, 2749, 2759, 2762, 33 2800, 2809, 2824, 2987, 3110, 3125, 3559, 3583, 3599, 3608, 34 3657, 3685, 3723, 3756, 3759, 3761, 3820, 3826, 3979, 4030, 35 4141, 4170, 4205, 4247, 4257, 4479, 4492, 4569, 5091, 36 } 37 // Only 2800 is the slashable index. 38 setB := []uint64{1361, 1438, 2383, 2800} 39 expectedSlashedVal := 2800 40 41 root1 := [32]byte{'d', 'o', 'u', 'b', 'l', 'e', '1'} 42 att1 := ðpb.IndexedAttestation{ 43 Data: testutil.HydrateAttestationData(ðpb.AttestationData{Target: ðpb.Checkpoint{Epoch: 0, Root: root1[:]}}), 44 AttestingIndices: setA, 45 Signature: make([]byte, 96), 46 } 47 domain, err := helpers.Domain(beaconState.Fork(), 0, params.BeaconConfig().DomainBeaconAttester, beaconState.GenesisValidatorRoot()) 48 require.NoError(t, err) 49 signingRoot, err := helpers.ComputeSigningRoot(att1.Data, domain) 50 require.NoError(t, err, "Could not get signing root of beacon block header") 51 var aggSigs []bls.Signature 52 for _, index := range setA { 53 sig := privKeys[index].Sign(signingRoot[:]) 54 aggSigs = append(aggSigs, sig) 55 } 56 aggregateSig := bls.AggregateSignatures(aggSigs) 57 att1.Signature = aggregateSig.Marshal() 58 59 root2 := [32]byte{'d', 'o', 'u', 'b', 'l', 'e', '2'} 60 att2 := ðpb.IndexedAttestation{ 61 Data: testutil.HydrateAttestationData(ðpb.AttestationData{ 62 Target: ðpb.Checkpoint{Root: root2[:]}, 63 }), 64 AttestingIndices: setB, 65 Signature: make([]byte, 96), 66 } 67 signingRoot, err = helpers.ComputeSigningRoot(att2.Data, domain) 68 assert.NoError(t, err, "Could not get signing root of beacon block header") 69 aggSigs = []bls.Signature{} 70 for _, index := range setB { 71 sig := privKeys[index].Sign(signingRoot[:]) 72 aggSigs = append(aggSigs, sig) 73 } 74 aggregateSig = bls.AggregateSignatures(aggSigs) 75 att2.Signature = aggregateSig.Marshal() 76 77 slashings := []*ethpb.AttesterSlashing{ 78 { 79 Attestation_1: att1, 80 Attestation_2: att2, 81 }, 82 } 83 84 currentSlot := 2 * params.BeaconConfig().SlotsPerEpoch 85 require.NoError(t, beaconState.SetSlot(currentSlot)) 86 87 b := testutil.NewBeaconBlock() 88 b.Block = ðpb.BeaconBlock{ 89 Body: ðpb.BeaconBlockBody{ 90 AttesterSlashings: slashings, 91 }, 92 } 93 94 newState, err := blocks.ProcessAttesterSlashings(context.Background(), beaconState, b.Block.Body.AttesterSlashings, v.SlashValidator) 95 require.NoError(t, err) 96 newRegistry := newState.Validators() 97 if !newRegistry[expectedSlashedVal].Slashed { 98 t.Errorf("Validator with index %d was not slashed despite performing a double vote", expectedSlashedVal) 99 } 100 101 for idx, val := range newRegistry { 102 if val.Slashed && idx != expectedSlashedVal { 103 t.Errorf("validator with index: %d was unintentionally slashed", idx) 104 } 105 } 106 }