github.com/prysmaticlabs/prysm@v1.4.4/slasher/db/kv/highest_attestation_test.go (about) 1 package kv 2 3 import ( 4 "context" 5 "testing" 6 7 slashbp "github.com/prysmaticlabs/prysm/proto/slashing" 8 "github.com/prysmaticlabs/prysm/shared/testutil/require" 9 ) 10 11 func TestSaveHighestAttestation(t *testing.T) { 12 db := setupDB(t) 13 ctx := context.Background() 14 15 tests := []struct { 16 name string 17 toSave []*slashbp.HighestAttestation 18 cacheEnabled bool 19 }{ 20 { 21 name: "save to cache", 22 toSave: []*slashbp.HighestAttestation{ 23 { 24 HighestTargetEpoch: 1, 25 HighestSourceEpoch: 0, 26 ValidatorId: 1, 27 }, 28 }, 29 cacheEnabled: true, 30 }, 31 { 32 name: "save to db", 33 toSave: []*slashbp.HighestAttestation{ 34 { 35 HighestTargetEpoch: 1, 36 HighestSourceEpoch: 0, 37 ValidatorId: 2, 38 }, 39 }, 40 cacheEnabled: false, 41 }, 42 } 43 44 for _, tt := range tests { 45 t.Run(tt.name, func(t *testing.T) { 46 for _, att := range tt.toSave { 47 db.highestAttCacheEnabled = tt.cacheEnabled 48 49 require.NoError(t, db.SaveHighestAttestation(ctx, att), "Save highest attestation failed") 50 51 found, err := db.HighestAttestation(ctx, att.ValidatorId) 52 require.NoError(t, err) 53 require.NotNil(t, found) 54 require.Equal(t, att.ValidatorId, found.ValidatorId) 55 require.Equal(t, att.HighestSourceEpoch, found.HighestSourceEpoch) 56 require.Equal(t, att.HighestTargetEpoch, found.HighestTargetEpoch) 57 } 58 }) 59 } 60 } 61 62 func TestFetchNonExistingHighestAttestation(t *testing.T) { 63 db := setupDB(t) 64 ctx := context.Background() 65 66 t.Run("cached", func(t *testing.T) { 67 db.highestAttCacheEnabled = true 68 found, err := db.HighestAttestation(ctx, 1) 69 require.NoError(t, err) 70 require.Equal(t, (*slashbp.HighestAttestation)(nil), found, "should not find HighestAttestation") 71 }) 72 73 t.Run("disk", func(t *testing.T) { 74 db.highestAttCacheEnabled = false 75 found, err := db.HighestAttestation(ctx, 1) 76 require.NoError(t, err) 77 require.Equal(t, (*slashbp.HighestAttestation)(nil), found, "should not find HighestAttestation") 78 }) 79 80 }