|
4 | 4 | "testing"
|
5 | 5 |
|
6 | 6 | eth "github.com/OffchainLabs/prysm/v6/proto/prysm/v1alpha1"
|
| 7 | + "github.com/OffchainLabs/prysm/v6/testing/assert" |
7 | 8 | "github.com/OffchainLabs/prysm/v6/testing/require"
|
| 9 | + "github.com/ethereum/go-ethereum/common/hexutil" |
8 | 10 | )
|
9 | 11 |
|
10 | 12 | func TestDepositSnapshotFromConsensus(t *testing.T) {
|
@@ -102,12 +104,266 @@ func TestProposerSlashing_ToConsensus(t *testing.T) {
|
102 | 104 | require.ErrorContains(t, errNilValue.Error(), err)
|
103 | 105 | }
|
104 | 106 |
|
| 107 | +func TestProposerSlashing_FromConsensus(t *testing.T) { |
| 108 | + input := []*eth.ProposerSlashing{ |
| 109 | + { |
| 110 | + Header_1: ð.SignedBeaconBlockHeader{ |
| 111 | + Header: ð.BeaconBlockHeader{ |
| 112 | + Slot: 1, |
| 113 | + ProposerIndex: 2, |
| 114 | + ParentRoot: []byte{3}, |
| 115 | + StateRoot: []byte{4}, |
| 116 | + BodyRoot: []byte{5}, |
| 117 | + }, |
| 118 | + Signature: []byte{6}, |
| 119 | + }, |
| 120 | + Header_2: ð.SignedBeaconBlockHeader{ |
| 121 | + Header: ð.BeaconBlockHeader{ |
| 122 | + Slot: 7, |
| 123 | + ProposerIndex: 8, |
| 124 | + ParentRoot: []byte{9}, |
| 125 | + StateRoot: []byte{10}, |
| 126 | + BodyRoot: []byte{11}, |
| 127 | + }, |
| 128 | + Signature: []byte{12}, |
| 129 | + }, |
| 130 | + }, |
| 131 | + { |
| 132 | + Header_1: ð.SignedBeaconBlockHeader{ |
| 133 | + Header: ð.BeaconBlockHeader{ |
| 134 | + Slot: 13, |
| 135 | + ProposerIndex: 14, |
| 136 | + ParentRoot: []byte{15}, |
| 137 | + StateRoot: []byte{16}, |
| 138 | + BodyRoot: []byte{17}, |
| 139 | + }, |
| 140 | + Signature: []byte{18}, |
| 141 | + }, |
| 142 | + Header_2: ð.SignedBeaconBlockHeader{ |
| 143 | + Header: ð.BeaconBlockHeader{ |
| 144 | + Slot: 19, |
| 145 | + ProposerIndex: 20, |
| 146 | + ParentRoot: []byte{21}, |
| 147 | + StateRoot: []byte{22}, |
| 148 | + BodyRoot: []byte{23}, |
| 149 | + }, |
| 150 | + Signature: []byte{24}, |
| 151 | + }, |
| 152 | + }, |
| 153 | + } |
| 154 | + |
| 155 | + expectedResult := []*ProposerSlashing{ |
| 156 | + { |
| 157 | + SignedHeader1: &SignedBeaconBlockHeader{ |
| 158 | + Message: &BeaconBlockHeader{ |
| 159 | + Slot: "1", |
| 160 | + ProposerIndex: "2", |
| 161 | + ParentRoot: hexutil.Encode([]byte{3}), |
| 162 | + StateRoot: hexutil.Encode([]byte{4}), |
| 163 | + BodyRoot: hexutil.Encode([]byte{5}), |
| 164 | + }, |
| 165 | + Signature: hexutil.Encode([]byte{6}), |
| 166 | + }, |
| 167 | + SignedHeader2: &SignedBeaconBlockHeader{ |
| 168 | + Message: &BeaconBlockHeader{ |
| 169 | + Slot: "7", |
| 170 | + ProposerIndex: "8", |
| 171 | + ParentRoot: hexutil.Encode([]byte{9}), |
| 172 | + StateRoot: hexutil.Encode([]byte{10}), |
| 173 | + BodyRoot: hexutil.Encode([]byte{11}), |
| 174 | + }, |
| 175 | + Signature: hexutil.Encode([]byte{12}), |
| 176 | + }, |
| 177 | + }, |
| 178 | + { |
| 179 | + SignedHeader1: &SignedBeaconBlockHeader{ |
| 180 | + Message: &BeaconBlockHeader{ |
| 181 | + Slot: "13", |
| 182 | + ProposerIndex: "14", |
| 183 | + ParentRoot: hexutil.Encode([]byte{15}), |
| 184 | + StateRoot: hexutil.Encode([]byte{16}), |
| 185 | + BodyRoot: hexutil.Encode([]byte{17}), |
| 186 | + }, |
| 187 | + Signature: hexutil.Encode([]byte{18}), |
| 188 | + }, |
| 189 | + SignedHeader2: &SignedBeaconBlockHeader{ |
| 190 | + Message: &BeaconBlockHeader{ |
| 191 | + Slot: "19", |
| 192 | + ProposerIndex: "20", |
| 193 | + ParentRoot: hexutil.Encode([]byte{21}), |
| 194 | + StateRoot: hexutil.Encode([]byte{22}), |
| 195 | + BodyRoot: hexutil.Encode([]byte{23}), |
| 196 | + }, |
| 197 | + Signature: hexutil.Encode([]byte{24}), |
| 198 | + }, |
| 199 | + }, |
| 200 | + } |
| 201 | + |
| 202 | + result := ProposerSlashingsFromConsensus(input) |
| 203 | + assert.DeepEqual(t, expectedResult, result) |
| 204 | +} |
| 205 | + |
105 | 206 | func TestAttesterSlashing_ToConsensus(t *testing.T) {
|
106 | 207 | a := &AttesterSlashing{Attestation1: nil, Attestation2: nil}
|
107 | 208 | _, err := a.ToConsensus()
|
108 | 209 | require.ErrorContains(t, errNilValue.Error(), err)
|
109 | 210 | }
|
110 | 211 |
|
| 212 | +func TestAttesterSlashing_FromConsensus(t *testing.T) { |
| 213 | + input := []*eth.AttesterSlashing{ |
| 214 | + { |
| 215 | + Attestation_1: ð.IndexedAttestation{ |
| 216 | + AttestingIndices: []uint64{1, 2}, |
| 217 | + Data: ð.AttestationData{ |
| 218 | + Slot: 3, |
| 219 | + CommitteeIndex: 4, |
| 220 | + BeaconBlockRoot: []byte{5}, |
| 221 | + Source: ð.Checkpoint{ |
| 222 | + Epoch: 6, |
| 223 | + Root: []byte{7}, |
| 224 | + }, |
| 225 | + Target: ð.Checkpoint{ |
| 226 | + Epoch: 8, |
| 227 | + Root: []byte{9}, |
| 228 | + }, |
| 229 | + }, |
| 230 | + Signature: []byte{10}, |
| 231 | + }, |
| 232 | + Attestation_2: ð.IndexedAttestation{ |
| 233 | + AttestingIndices: []uint64{11, 12}, |
| 234 | + Data: ð.AttestationData{ |
| 235 | + Slot: 13, |
| 236 | + CommitteeIndex: 14, |
| 237 | + BeaconBlockRoot: []byte{15}, |
| 238 | + Source: ð.Checkpoint{ |
| 239 | + Epoch: 16, |
| 240 | + Root: []byte{17}, |
| 241 | + }, |
| 242 | + Target: ð.Checkpoint{ |
| 243 | + Epoch: 18, |
| 244 | + Root: []byte{19}, |
| 245 | + }, |
| 246 | + }, |
| 247 | + Signature: []byte{20}, |
| 248 | + }, |
| 249 | + }, |
| 250 | + { |
| 251 | + Attestation_1: ð.IndexedAttestation{ |
| 252 | + AttestingIndices: []uint64{21, 22}, |
| 253 | + Data: ð.AttestationData{ |
| 254 | + Slot: 23, |
| 255 | + CommitteeIndex: 24, |
| 256 | + BeaconBlockRoot: []byte{25}, |
| 257 | + Source: ð.Checkpoint{ |
| 258 | + Epoch: 26, |
| 259 | + Root: []byte{27}, |
| 260 | + }, |
| 261 | + Target: ð.Checkpoint{ |
| 262 | + Epoch: 28, |
| 263 | + Root: []byte{29}, |
| 264 | + }, |
| 265 | + }, |
| 266 | + Signature: []byte{30}, |
| 267 | + }, |
| 268 | + Attestation_2: ð.IndexedAttestation{ |
| 269 | + AttestingIndices: []uint64{31, 32}, |
| 270 | + Data: ð.AttestationData{ |
| 271 | + Slot: 33, |
| 272 | + CommitteeIndex: 34, |
| 273 | + BeaconBlockRoot: []byte{35}, |
| 274 | + Source: ð.Checkpoint{ |
| 275 | + Epoch: 36, |
| 276 | + Root: []byte{37}, |
| 277 | + }, |
| 278 | + Target: ð.Checkpoint{ |
| 279 | + Epoch: 38, |
| 280 | + Root: []byte{39}, |
| 281 | + }, |
| 282 | + }, |
| 283 | + Signature: []byte{40}, |
| 284 | + }, |
| 285 | + }, |
| 286 | + } |
| 287 | + |
| 288 | + expectedResult := []*AttesterSlashing{ |
| 289 | + { |
| 290 | + Attestation1: &IndexedAttestation{ |
| 291 | + AttestingIndices: []string{"1", "2"}, |
| 292 | + Data: &AttestationData{ |
| 293 | + Slot: "3", |
| 294 | + CommitteeIndex: "4", |
| 295 | + BeaconBlockRoot: hexutil.Encode([]byte{5}), |
| 296 | + Source: &Checkpoint{ |
| 297 | + Epoch: "6", |
| 298 | + Root: hexutil.Encode([]byte{7}), |
| 299 | + }, |
| 300 | + Target: &Checkpoint{ |
| 301 | + Epoch: "8", |
| 302 | + Root: hexutil.Encode([]byte{9}), |
| 303 | + }, |
| 304 | + }, |
| 305 | + Signature: hexutil.Encode([]byte{10}), |
| 306 | + }, |
| 307 | + Attestation2: &IndexedAttestation{ |
| 308 | + AttestingIndices: []string{"11", "12"}, |
| 309 | + Data: &AttestationData{ |
| 310 | + Slot: "13", |
| 311 | + CommitteeIndex: "14", |
| 312 | + BeaconBlockRoot: hexutil.Encode([]byte{15}), |
| 313 | + Source: &Checkpoint{ |
| 314 | + Epoch: "16", |
| 315 | + Root: hexutil.Encode([]byte{17}), |
| 316 | + }, |
| 317 | + Target: &Checkpoint{ |
| 318 | + Epoch: "18", |
| 319 | + Root: hexutil.Encode([]byte{19}), |
| 320 | + }, |
| 321 | + }, |
| 322 | + Signature: hexutil.Encode([]byte{20}), |
| 323 | + }, |
| 324 | + }, |
| 325 | + { |
| 326 | + Attestation1: &IndexedAttestation{ |
| 327 | + AttestingIndices: []string{"21", "22"}, |
| 328 | + Data: &AttestationData{ |
| 329 | + Slot: "23", |
| 330 | + CommitteeIndex: "24", |
| 331 | + BeaconBlockRoot: hexutil.Encode([]byte{25}), |
| 332 | + Source: &Checkpoint{ |
| 333 | + Epoch: "26", |
| 334 | + Root: hexutil.Encode([]byte{27}), |
| 335 | + }, |
| 336 | + Target: &Checkpoint{ |
| 337 | + Epoch: "28", |
| 338 | + Root: hexutil.Encode([]byte{29}), |
| 339 | + }, |
| 340 | + }, |
| 341 | + Signature: hexutil.Encode([]byte{30}), |
| 342 | + }, |
| 343 | + Attestation2: &IndexedAttestation{ |
| 344 | + AttestingIndices: []string{"31", "32"}, |
| 345 | + Data: &AttestationData{ |
| 346 | + Slot: "33", |
| 347 | + CommitteeIndex: "34", |
| 348 | + BeaconBlockRoot: hexutil.Encode([]byte{35}), |
| 349 | + Source: &Checkpoint{ |
| 350 | + Epoch: "36", |
| 351 | + Root: hexutil.Encode([]byte{37}), |
| 352 | + }, |
| 353 | + Target: &Checkpoint{ |
| 354 | + Epoch: "38", |
| 355 | + Root: hexutil.Encode([]byte{39}), |
| 356 | + }, |
| 357 | + }, |
| 358 | + Signature: hexutil.Encode([]byte{40}), |
| 359 | + }, |
| 360 | + }, |
| 361 | + } |
| 362 | + |
| 363 | + result := AttesterSlashingsFromConsensus(input) |
| 364 | + assert.DeepEqual(t, expectedResult, result) |
| 365 | +} |
| 366 | + |
111 | 367 | func TestIndexedAttestation_ToConsensus(t *testing.T) {
|
112 | 368 | a := &IndexedAttestation{
|
113 | 369 | AttestingIndices: []string{"1"},
|
|
0 commit comments