github.com/prysmaticlabs/prysm@v1.4.4/validator/slashing-protection/local/standard-protection-format/export_test.go (about) 1 package interchangeformat 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 8 types "github.com/prysmaticlabs/eth2-types" 9 "github.com/prysmaticlabs/prysm/shared/testutil/assert" 10 "github.com/prysmaticlabs/prysm/shared/testutil/require" 11 dbtest "github.com/prysmaticlabs/prysm/validator/db/testing" 12 "github.com/prysmaticlabs/prysm/validator/slashing-protection/local/standard-protection-format/format" 13 ) 14 15 func Test_getSignedAttestationsByPubKey(t *testing.T) { 16 t.Run("OK", func(t *testing.T) { 17 pubKeys := [][48]byte{ 18 {1}, 19 } 20 ctx := context.Background() 21 validatorDB := dbtest.SetupDB(t, pubKeys) 22 23 // No attestation history stored should return empty. 24 signedAttestations, err := signedAttestationsByPubKey(ctx, validatorDB, pubKeys[0]) 25 require.NoError(t, err) 26 assert.Equal(t, 0, len(signedAttestations)) 27 28 // We write a real attesting history to disk for the public key. 29 lowestSourceEpoch := types.Epoch(0) 30 lowestTargetEpoch := types.Epoch(4) 31 32 require.NoError(t, validatorDB.SaveAttestationForPubKey(ctx, pubKeys[0], [32]byte{4}, createAttestation( 33 lowestSourceEpoch, 34 lowestTargetEpoch, 35 ))) 36 require.NoError(t, validatorDB.SaveAttestationForPubKey(ctx, pubKeys[0], [32]byte{5}, createAttestation( 37 lowestSourceEpoch, 38 lowestTargetEpoch+1, 39 ))) 40 41 // We then retrieve the signed attestations and expect a correct result. 42 signedAttestations, err = signedAttestationsByPubKey(ctx, validatorDB, pubKeys[0]) 43 require.NoError(t, err) 44 45 wanted := []*format.SignedAttestation{ 46 { 47 SourceEpoch: "0", 48 TargetEpoch: "4", 49 SigningRoot: "0x0400000000000000000000000000000000000000000000000000000000000000", 50 }, 51 { 52 SourceEpoch: "0", 53 TargetEpoch: "5", 54 SigningRoot: "0x0500000000000000000000000000000000000000000000000000000000000000", 55 }, 56 } 57 assert.DeepEqual(t, wanted, signedAttestations) 58 }) 59 t.Run("old_schema_bug_edge_case_genesis", func(t *testing.T) { 60 pubKeys := [][48]byte{ 61 {1}, 62 } 63 ctx := context.Background() 64 validatorDB := dbtest.SetupDB(t, pubKeys) 65 66 // No attestation history stored should return empty. 67 signedAttestations, err := signedAttestationsByPubKey(ctx, validatorDB, pubKeys[0]) 68 require.NoError(t, err) 69 assert.Equal(t, 0, len(signedAttestations)) 70 71 // We write a real attesting history to disk for the public key with 72 // source epoch 0 and target epoch 1000. 73 lowestSourceEpoch := types.Epoch(0) 74 lowestTargetEpoch := types.Epoch(1000) 75 76 // Next up, we simulate a DB affected by the bug where the next entry 77 // has a target epoch less than the previous one. 78 require.NoError(t, validatorDB.SaveAttestationForPubKey(ctx, pubKeys[0], [32]byte{4}, createAttestation( 79 lowestSourceEpoch, 80 lowestTargetEpoch, 81 ))) 82 require.NoError(t, validatorDB.SaveAttestationForPubKey(ctx, pubKeys[0], [32]byte{5}, createAttestation( 83 1, 84 2, 85 ))) 86 87 // We then retrieve the signed attestations and expect to have 88 // skipped the 0th, corrupted entry. 89 signedAttestations, err = signedAttestationsByPubKey(ctx, validatorDB, pubKeys[0]) 90 require.NoError(t, err) 91 92 wanted := []*format.SignedAttestation{ 93 { 94 SourceEpoch: "1", 95 TargetEpoch: "2", 96 SigningRoot: "0x0500000000000000000000000000000000000000000000000000000000000000", 97 }, 98 } 99 assert.DeepEqual(t, wanted, signedAttestations) 100 }) 101 t.Run("old_schema_bug_edge_case_not_genesis", func(t *testing.T) { 102 pubKeys := [][48]byte{ 103 {1}, 104 } 105 ctx := context.Background() 106 validatorDB := dbtest.SetupDB(t, pubKeys) 107 108 // No attestation history stored should return empty. 109 signedAttestations, err := signedAttestationsByPubKey(ctx, validatorDB, pubKeys[0]) 110 require.NoError(t, err) 111 assert.Equal(t, 0, len(signedAttestations)) 112 113 // We write a real attesting history to disk for the public key with 114 // source epoch 1 and target epoch 1000. 115 lowestSourceEpoch := types.Epoch(1) 116 lowestTargetEpoch := types.Epoch(1000) 117 118 // Next up, we simulate a DB affected by the bug where the next entry 119 // has a target epoch less than the previous one. 120 require.NoError(t, validatorDB.SaveAttestationForPubKey(ctx, pubKeys[0], [32]byte{4}, createAttestation( 121 lowestSourceEpoch, 122 lowestTargetEpoch, 123 ))) 124 require.NoError(t, validatorDB.SaveAttestationForPubKey(ctx, pubKeys[0], [32]byte{5}, createAttestation( 125 1, 126 2, 127 ))) 128 129 // We then retrieve the signed attestations and do not expect changes 130 // as the bug only manifests in the genesis epoch. 131 signedAttestations, err = signedAttestationsByPubKey(ctx, validatorDB, pubKeys[0]) 132 require.NoError(t, err) 133 134 wanted := []*format.SignedAttestation{ 135 { 136 SourceEpoch: "1", 137 TargetEpoch: "1000", 138 SigningRoot: "0x0400000000000000000000000000000000000000000000000000000000000000", 139 }, 140 { 141 SourceEpoch: "1", 142 TargetEpoch: "2", 143 SigningRoot: "0x0500000000000000000000000000000000000000000000000000000000000000", 144 }, 145 } 146 assert.DeepEqual(t, wanted, signedAttestations) 147 }) 148 } 149 150 func Test_getSignedBlocksByPubKey(t *testing.T) { 151 pubKeys := [][48]byte{ 152 {1}, 153 } 154 ctx := context.Background() 155 validatorDB := dbtest.SetupDB(t, pubKeys) 156 157 // No highest and/or lowest signed blocks will return empty. 158 signedBlocks, err := signedBlocksByPubKey(ctx, validatorDB, pubKeys[0]) 159 require.NoError(t, err) 160 assert.Equal(t, 0, len(signedBlocks)) 161 162 // We mark slot 1 as proposed. 163 dummyRoot1 := [32]byte{1} 164 err = validatorDB.SaveProposalHistoryForSlot(ctx, pubKeys[0], 1, dummyRoot1[:]) 165 require.NoError(t, err) 166 167 // We mark slot 3 as proposed but with empty signing root. 168 err = validatorDB.SaveProposalHistoryForSlot(ctx, pubKeys[0], 3, nil) 169 require.NoError(t, err) 170 171 // We mark slot 5 as proposed. 172 dummyRoot2 := [32]byte{2} 173 err = validatorDB.SaveProposalHistoryForSlot(ctx, pubKeys[0], 5, dummyRoot2[:]) 174 require.NoError(t, err) 175 176 // We expect a valid proposal history containing slot 1 and slot 5 only 177 // when we attempt to retrieve it from disk. 178 signedBlocks, err = signedBlocksByPubKey(ctx, validatorDB, pubKeys[0]) 179 require.NoError(t, err) 180 wanted := []*format.SignedBlock{ 181 { 182 Slot: "1", 183 SigningRoot: fmt.Sprintf("%#x", dummyRoot1), 184 }, 185 { 186 Slot: "3", 187 SigningRoot: "0x0000000000000000000000000000000000000000000000000000000000000000", 188 }, 189 { 190 Slot: "5", 191 SigningRoot: fmt.Sprintf("%#x", dummyRoot2), 192 }, 193 } 194 for i, blk := range wanted { 195 assert.DeepEqual(t, blk, signedBlocks[i]) 196 } 197 }