github.com/prysmaticlabs/prysm@v1.4.4/validator/db/kv/deprecated_attester_protection_test.go (about) 1 package kv 2 3 import ( 4 "context" 5 "testing" 6 7 types "github.com/prysmaticlabs/eth2-types" 8 "github.com/prysmaticlabs/prysm/shared/bytesutil" 9 "github.com/prysmaticlabs/prysm/shared/params" 10 "github.com/prysmaticlabs/prysm/shared/testutil/assert" 11 "github.com/prysmaticlabs/prysm/shared/testutil/require" 12 ) 13 14 func TestNewAttestationHistoryArray(t *testing.T) { 15 ba := newDeprecatedAttestingHistory(0) 16 assert.Equal(t, latestEpochWrittenSize+historySize, len(ba)) 17 ba = newDeprecatedAttestingHistory(params.BeaconConfig().WeakSubjectivityPeriod - 1) 18 assert.Equal(t, latestEpochWrittenSize+historySize*params.BeaconConfig().WeakSubjectivityPeriod, types.Epoch(len(ba))) 19 ba = newDeprecatedAttestingHistory(params.BeaconConfig().WeakSubjectivityPeriod) 20 assert.Equal(t, latestEpochWrittenSize+historySize, len(ba)) 21 ba = newDeprecatedAttestingHistory(params.BeaconConfig().WeakSubjectivityPeriod + 1) 22 assert.Equal(t, latestEpochWrittenSize+historySize+historySize, len(ba)) 23 24 } 25 26 func TestSizeChecks(t *testing.T) { 27 require.ErrorContains(t, "is smaller then minimal size", deprecatedEncodedAttestingHistory{}.assertSize()) 28 require.NoError(t, deprecatedEncodedAttestingHistory{0, 1, 2, 3, 4, 5, 6, 7}.assertSize()) 29 require.ErrorContains(t, "is not a multiple of entry size", deprecatedEncodedAttestingHistory{0, 1, 2, 3, 4, 5, 6, 7, 8}.assertSize()) 30 require.NoError(t, newDeprecatedAttestingHistory(0).assertSize()) 31 require.NoError(t, newDeprecatedAttestingHistory(1).assertSize()) 32 require.NoError(t, newDeprecatedAttestingHistory(params.BeaconConfig().WeakSubjectivityPeriod).assertSize()) 33 require.NoError(t, newDeprecatedAttestingHistory(params.BeaconConfig().WeakSubjectivityPeriod-1).assertSize()) 34 } 35 36 func TestGetLatestEpochWritten(t *testing.T) { 37 ctx := context.Background() 38 ha := newDeprecatedAttestingHistory(0) 39 ha[0] = 28 40 lew, err := ha.getLatestEpochWritten(ctx) 41 require.NoError(t, err) 42 assert.Equal(t, types.Epoch(28), lew) 43 } 44 45 func TestSetLatestEpochWritten(t *testing.T) { 46 ctx := context.Background() 47 ha := newDeprecatedAttestingHistory(0) 48 lew, err := ha.setLatestEpochWritten(ctx, 2828282828) 49 require.NoError(t, err) 50 bytes := lew[:latestEpochWrittenSize] 51 assert.Equal(t, uint64(2828282828), bytesutil.FromBytes8(bytes)) 52 } 53 54 func TestGetTargetData(t *testing.T) { 55 ctx := context.Background() 56 ha := newDeprecatedAttestingHistory(0) 57 td, err := ha.getTargetData(ctx, 0) 58 require.NoError(t, err) 59 assert.DeepEqual(t, emptyHistoryData(), td) 60 td, err = ha.getTargetData(ctx, 1) 61 require.NoError(t, err) 62 var nilHist *deprecatedHistoryData 63 require.Equal(t, nilHist, td) 64 } 65 66 func TestSetTargetData(t *testing.T) { 67 ctx := context.Background() 68 type testStruct struct { 69 name string 70 enc deprecatedEncodedAttestingHistory 71 target types.Epoch 72 source types.Epoch 73 signingRoot []byte 74 expected deprecatedEncodedAttestingHistory 75 error string 76 } 77 tests := []testStruct{ 78 { 79 name: "empty enc", 80 enc: deprecatedEncodedAttestingHistory{}, 81 target: 0, 82 source: 100, 83 signingRoot: []byte{1, 2, 3}, 84 expected: deprecatedEncodedAttestingHistory(nil), 85 error: "encapsulated data size", 86 }, 87 { 88 name: "new enc", 89 enc: newDeprecatedAttestingHistory(0), 90 target: 0, 91 source: 100, 92 signingRoot: []byte{1, 2, 3}, 93 expected: deprecatedEncodedAttestingHistory{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x2, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, 94 error: "", 95 }, 96 { 97 name: "higher target", 98 enc: newDeprecatedAttestingHistory(0), 99 target: 2, 100 source: 100, 101 signingRoot: []byte{1, 2, 3}, 102 expected: deprecatedEncodedAttestingHistory{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x2, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, 103 error: "", 104 }, 105 } 106 for _, tt := range tests { 107 t.Run(tt.name, func(t *testing.T) { 108 enc, err := tt.enc.setTargetData(ctx, 109 tt.target, 110 &deprecatedHistoryData{ 111 Source: tt.source, 112 SigningRoot: tt.signingRoot, 113 }) 114 if tt.error == "" { 115 require.NoError(t, err) 116 td, err := enc.getTargetData(ctx, tt.target) 117 require.NoError(t, err) 118 require.DeepEqual(t, bytesutil.PadTo(tt.signingRoot, 32), td.SigningRoot) 119 require.Equal(t, tt.source, td.Source) 120 return 121 } 122 assert.ErrorContains(t, tt.error, err) 123 require.DeepEqual(t, tt.expected, enc) 124 125 }) 126 } 127 }