github.com/prysmaticlabs/prysm@v1.4.4/slasher/db/kv/attester_slashings_test.go (about)

     1  package kv
     2  
     3  import (
     4  	"context"
     5  	"sort"
     6  	"testing"
     7  
     8  	types "github.com/prysmaticlabs/eth2-types"
     9  	ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
    10  	"github.com/prysmaticlabs/prysm/shared/bytesutil"
    11  	"github.com/prysmaticlabs/prysm/shared/testutil"
    12  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    13  	dbtypes "github.com/prysmaticlabs/prysm/slasher/db/types"
    14  )
    15  
    16  func TestStore_AttesterSlashingNilBucket(t *testing.T) {
    17  	db := setupDB(t)
    18  	ctx := context.Background()
    19  
    20  	as := &ethpb.AttesterSlashing{
    21  		Attestation_1: testutil.HydrateIndexedAttestation(&ethpb.IndexedAttestation{
    22  			Signature: bytesutil.PadTo([]byte("hello"), 96),
    23  		}),
    24  		Attestation_2: testutil.HydrateIndexedAttestation(&ethpb.IndexedAttestation{
    25  			Signature: bytesutil.PadTo([]byte("hello"), 96),
    26  		}),
    27  	}
    28  	has, _, err := db.HasAttesterSlashing(ctx, as)
    29  	require.NoError(t, err, "HasAttesterSlashing should not return error")
    30  	require.Equal(t, false, has)
    31  
    32  	p, err := db.AttesterSlashings(ctx, dbtypes.SlashingStatus(dbtypes.Active))
    33  	require.NoError(t, err, "Failed to get attester slashing")
    34  	require.NotNil(t, p, "Get should return empty attester slashing array for a non existent key")
    35  	require.Equal(t, 0, len(p), "Get should return empty attester slashing array for a non existent key")
    36  }
    37  
    38  func TestStore_SaveAttesterSlashing(t *testing.T) {
    39  	db := setupDB(t)
    40  	ctx := context.Background()
    41  
    42  	data := testutil.HydrateAttestationData(&ethpb.AttestationData{})
    43  	att := &ethpb.IndexedAttestation{Data: data, Signature: make([]byte, 96)}
    44  	tests := []struct {
    45  		ss dbtypes.SlashingStatus
    46  		as *ethpb.AttesterSlashing
    47  	}{
    48  		{
    49  			ss: dbtypes.Active,
    50  			as: &ethpb.AttesterSlashing{Attestation_1: &ethpb.IndexedAttestation{Data: data, Signature: bytesutil.PadTo([]byte("hello"), 96)}, Attestation_2: att},
    51  		},
    52  		{
    53  			ss: dbtypes.Included,
    54  			as: &ethpb.AttesterSlashing{Attestation_1: &ethpb.IndexedAttestation{Data: data, Signature: bytesutil.PadTo([]byte("hello2"), 96)}, Attestation_2: att},
    55  		},
    56  		{
    57  			ss: dbtypes.Reverted,
    58  			as: &ethpb.AttesterSlashing{Attestation_1: &ethpb.IndexedAttestation{Data: data, Signature: bytesutil.PadTo([]byte("hello3"), 96)}, Attestation_2: att},
    59  		},
    60  	}
    61  
    62  	for _, tt := range tests {
    63  		err := db.SaveAttesterSlashing(ctx, tt.ss, tt.as)
    64  		require.NoError(t, err, "Save attester slashing failed")
    65  
    66  		attesterSlashings, err := db.AttesterSlashings(ctx, tt.ss)
    67  		require.NoError(t, err, "Failed to get attester slashings")
    68  		require.NotNil(t, attesterSlashings)
    69  		require.DeepEqual(t, tt.as, attesterSlashings[0], "Slashing: %v should be part of slashings response: %v", tt.as, attesterSlashings)
    70  	}
    71  }
    72  
    73  func TestStore_SaveAttesterSlashings(t *testing.T) {
    74  	db := setupDB(t)
    75  	ctx := context.Background()
    76  
    77  	ckpt := &ethpb.Checkpoint{Root: make([]byte, 32)}
    78  	data := &ethpb.AttestationData{Source: ckpt, Target: ckpt, BeaconBlockRoot: make([]byte, 32)}
    79  	att := &ethpb.IndexedAttestation{Data: data, Signature: make([]byte, 96)}
    80  	as := []*ethpb.AttesterSlashing{
    81  		{Attestation_1: &ethpb.IndexedAttestation{Signature: bytesutil.PadTo([]byte("1"), 96), Data: data}, Attestation_2: att},
    82  		{Attestation_1: &ethpb.IndexedAttestation{Signature: bytesutil.PadTo([]byte("2"), 96), Data: data}, Attestation_2: att},
    83  		{Attestation_1: &ethpb.IndexedAttestation{Signature: bytesutil.PadTo([]byte("3"), 96), Data: data}, Attestation_2: att},
    84  	}
    85  	err := db.SaveAttesterSlashings(ctx, dbtypes.Active, as)
    86  	require.NoError(t, err, "Save attester slashing failed")
    87  	attesterSlashings, err := db.AttesterSlashings(ctx, dbtypes.Active)
    88  	require.NoError(t, err, "Failed to get attester slashings")
    89  	sort.SliceStable(attesterSlashings, func(i, j int) bool {
    90  		return attesterSlashings[i].Attestation_1.Signature[0] < attesterSlashings[j].Attestation_1.Signature[0]
    91  	})
    92  	require.NotNil(t, attesterSlashings)
    93  	require.DeepSSZEqual(t, as, attesterSlashings, "Slashing: %v should be part of slashings response: %v", as, attesterSlashings)
    94  }
    95  
    96  func TestStore_UpdateAttesterSlashingStatus(t *testing.T) {
    97  	db := setupDB(t)
    98  	ctx := context.Background()
    99  
   100  	data := testutil.HydrateAttestationData(&ethpb.AttestationData{})
   101  
   102  	tests := []struct {
   103  		ss dbtypes.SlashingStatus
   104  		as *ethpb.AttesterSlashing
   105  	}{
   106  		{
   107  			ss: dbtypes.Active,
   108  			as: &ethpb.AttesterSlashing{
   109  				Attestation_1: &ethpb.IndexedAttestation{Data: data, Signature: bytesutil.PadTo([]byte("hello"), 96)},
   110  				Attestation_2: &ethpb.IndexedAttestation{Data: data, Signature: bytesutil.PadTo([]byte("hello"), 96)},
   111  			},
   112  		},
   113  		{
   114  			ss: dbtypes.Active,
   115  			as: &ethpb.AttesterSlashing{
   116  				Attestation_1: &ethpb.IndexedAttestation{Data: data, Signature: bytesutil.PadTo([]byte("hello2"), 96)},
   117  				Attestation_2: &ethpb.IndexedAttestation{Data: data, Signature: bytesutil.PadTo([]byte("hello2"), 96)},
   118  			},
   119  		},
   120  		{
   121  			ss: dbtypes.Active,
   122  			as: &ethpb.AttesterSlashing{
   123  				Attestation_1: &ethpb.IndexedAttestation{Data: data, Signature: bytesutil.PadTo([]byte("hello3"), 96)},
   124  				Attestation_2: &ethpb.IndexedAttestation{Data: data, Signature: bytesutil.PadTo([]byte("hello2"), 96)},
   125  			},
   126  		},
   127  	}
   128  
   129  	for _, tt := range tests {
   130  		err := db.SaveAttesterSlashing(ctx, tt.ss, tt.as)
   131  		require.NoError(t, err, "Save attester slashing failed")
   132  	}
   133  
   134  	for _, tt := range tests {
   135  		has, st, err := db.HasAttesterSlashing(ctx, tt.as)
   136  		require.NoError(t, err, "Failed to get attester slashing")
   137  		require.Equal(t, true, has, "Failed to find attester slashing: %v", tt.as)
   138  		require.Equal(t, tt.ss, st, "Failed to find attester slashing with the correct status: %v", tt.as)
   139  
   140  		err = db.SaveAttesterSlashing(ctx, dbtypes.SlashingStatus(dbtypes.Included), tt.as)
   141  		require.NoError(t, err)
   142  		has, st, err = db.HasAttesterSlashing(ctx, tt.as)
   143  		require.NoError(t, err, "Failed to get attester slashing")
   144  		require.Equal(t, true, has, "Failed to find attester slashing: %v", tt.as)
   145  		require.Equal(t, dbtypes.SlashingStatus(dbtypes.Included), st, "Failed to find attester slashing with the correct status: %v", tt.as)
   146  	}
   147  }
   148  
   149  func TestStore_LatestEpochDetected(t *testing.T) {
   150  	db := setupDB(t)
   151  	ctx := context.Background()
   152  
   153  	e, err := db.GetLatestEpochDetected(ctx)
   154  	require.NoError(t, err, "Get latest epoch detected failed")
   155  	require.Equal(t, types.Epoch(0), e, "Latest epoch detected should have been 0 before setting got: %d", e)
   156  	epoch := types.Epoch(1)
   157  	err = db.SetLatestEpochDetected(ctx, epoch)
   158  	require.NoError(t, err, "Set latest epoch detected failed")
   159  	e, err = db.GetLatestEpochDetected(ctx)
   160  	require.NoError(t, err, "Get latest epoch detected failed")
   161  	require.Equal(t, epoch, e, "Latest epoch detected should have been: %d got: %d", epoch, e)
   162  }