github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/cache/depositcache/deposits_cache_test.go (about) 1 package depositcache 2 3 import ( 4 "bytes" 5 "context" 6 "fmt" 7 "math/big" 8 "testing" 9 10 dbpb "github.com/prysmaticlabs/prysm/proto/beacon/db" 11 ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" 12 "github.com/prysmaticlabs/prysm/shared/bytesutil" 13 "github.com/prysmaticlabs/prysm/shared/params" 14 "github.com/prysmaticlabs/prysm/shared/testutil/assert" 15 "github.com/prysmaticlabs/prysm/shared/testutil/require" 16 "github.com/prysmaticlabs/prysm/shared/trieutil" 17 logTest "github.com/sirupsen/logrus/hooks/test" 18 ) 19 20 const nilDepositErr = "Ignoring nil deposit insertion" 21 22 var _ DepositFetcher = (*DepositCache)(nil) 23 24 func TestInsertDeposit_LogsOnNilDepositInsertion(t *testing.T) { 25 hook := logTest.NewGlobal() 26 dc, err := New() 27 require.NoError(t, err) 28 29 assert.ErrorContains(t, "nil deposit inserted into the cache", dc.InsertDeposit(context.Background(), nil, 1, 0, [32]byte{})) 30 31 require.Equal(t, 0, len(dc.deposits), "Number of deposits changed") 32 assert.Equal(t, nilDepositErr, hook.LastEntry().Message) 33 } 34 35 func TestInsertDeposit_MaintainsSortedOrderByIndex(t *testing.T) { 36 dc, err := New() 37 require.NoError(t, err) 38 39 insertions := []struct { 40 blkNum uint64 41 deposit *ethpb.Deposit 42 index int64 43 expectedErr string 44 }{ 45 { 46 blkNum: 0, 47 deposit: ðpb.Deposit{}, 48 index: 0, 49 expectedErr: "", 50 }, 51 { 52 blkNum: 0, 53 deposit: ðpb.Deposit{}, 54 index: 3, 55 expectedErr: "wanted deposit with index 1 to be inserted but received 3", 56 }, 57 { 58 blkNum: 0, 59 deposit: ðpb.Deposit{}, 60 index: 1, 61 expectedErr: "", 62 }, 63 { 64 blkNum: 0, 65 deposit: ðpb.Deposit{}, 66 index: 4, 67 expectedErr: "wanted deposit with index 2 to be inserted but received 4", 68 }, 69 { 70 blkNum: 0, 71 deposit: ðpb.Deposit{}, 72 index: 2, 73 expectedErr: "", 74 }, 75 } 76 77 for _, ins := range insertions { 78 if ins.expectedErr != "" { 79 assert.ErrorContains(t, ins.expectedErr, dc.InsertDeposit(context.Background(), ins.deposit, ins.blkNum, ins.index, [32]byte{})) 80 } else { 81 assert.NoError(t, dc.InsertDeposit(context.Background(), ins.deposit, ins.blkNum, ins.index, [32]byte{})) 82 } 83 } 84 85 expectedIndices := []int64{0, 1, 2} 86 for i, ei := range expectedIndices { 87 assert.Equal(t, ei, dc.deposits[i].Index, 88 fmt.Sprintf("dc.deposits[%d].Index = %d, wanted %d", i, dc.deposits[i].Index, ei)) 89 } 90 } 91 92 func TestAllDeposits_ReturnsAllDeposits(t *testing.T) { 93 dc, err := New() 94 require.NoError(t, err) 95 96 deposits := []*dbpb.DepositContainer{ 97 { 98 Eth1BlockHeight: 10, 99 Deposit: ðpb.Deposit{}, 100 }, 101 { 102 Eth1BlockHeight: 10, 103 Deposit: ðpb.Deposit{}, 104 }, 105 { 106 Eth1BlockHeight: 10, 107 Deposit: ðpb.Deposit{}, 108 }, 109 { 110 Eth1BlockHeight: 11, 111 Deposit: ðpb.Deposit{}, 112 }, 113 { 114 Eth1BlockHeight: 11, 115 Deposit: ðpb.Deposit{}, 116 }, 117 { 118 Eth1BlockHeight: 12, 119 Deposit: ðpb.Deposit{}, 120 }, 121 { 122 Eth1BlockHeight: 12, 123 Deposit: ðpb.Deposit{}, 124 }, 125 } 126 dc.deposits = deposits 127 128 d := dc.AllDeposits(context.Background(), nil) 129 assert.Equal(t, len(deposits), len(d)) 130 } 131 132 func TestAllDeposits_FiltersDepositUpToAndIncludingBlockNumber(t *testing.T) { 133 dc, err := New() 134 require.NoError(t, err) 135 136 deposits := []*dbpb.DepositContainer{ 137 { 138 Eth1BlockHeight: 10, 139 Deposit: ðpb.Deposit{}, 140 }, 141 { 142 Eth1BlockHeight: 10, 143 Deposit: ðpb.Deposit{}, 144 }, 145 { 146 Eth1BlockHeight: 10, 147 Deposit: ðpb.Deposit{}, 148 }, 149 { 150 Eth1BlockHeight: 11, 151 Deposit: ðpb.Deposit{}, 152 }, 153 { 154 Eth1BlockHeight: 11, 155 Deposit: ðpb.Deposit{}, 156 }, 157 { 158 Eth1BlockHeight: 12, 159 Deposit: ðpb.Deposit{}, 160 }, 161 { 162 Eth1BlockHeight: 12, 163 Deposit: ðpb.Deposit{}, 164 }, 165 } 166 dc.deposits = deposits 167 168 d := dc.AllDeposits(context.Background(), big.NewInt(11)) 169 assert.Equal(t, 5, len(d)) 170 } 171 172 func TestDepositsNumberAndRootAtHeight(t *testing.T) { 173 wantedRoot := bytesutil.PadTo([]byte("root"), 32) 174 t.Run("requesting_last_item_works", func(t *testing.T) { 175 dc, err := New() 176 require.NoError(t, err) 177 dc.deposits = []*dbpb.DepositContainer{ 178 { 179 Eth1BlockHeight: 10, 180 Index: 0, 181 Deposit: ðpb.Deposit{}, 182 }, 183 { 184 Eth1BlockHeight: 10, 185 Index: 1, 186 Deposit: ðpb.Deposit{}, 187 }, 188 { 189 Eth1BlockHeight: 11, 190 Index: 2, 191 Deposit: ðpb.Deposit{}, 192 }, 193 { 194 Eth1BlockHeight: 13, 195 Index: 3, 196 Deposit: ðpb.Deposit{}, 197 DepositRoot: wantedRoot, 198 }, 199 } 200 n, root := dc.DepositsNumberAndRootAtHeight(context.Background(), big.NewInt(13)) 201 assert.Equal(t, 4, int(n)) 202 require.DeepEqual(t, wantedRoot, root[:]) 203 }) 204 t.Run("only_one_item", func(t *testing.T) { 205 dc, err := New() 206 require.NoError(t, err) 207 208 dc.deposits = []*dbpb.DepositContainer{ 209 { 210 Eth1BlockHeight: 10, 211 Index: 0, 212 Deposit: ðpb.Deposit{}, 213 DepositRoot: wantedRoot, 214 }, 215 } 216 n, root := dc.DepositsNumberAndRootAtHeight(context.Background(), big.NewInt(10)) 217 assert.Equal(t, 1, int(n)) 218 require.DeepEqual(t, wantedRoot, root[:]) 219 }) 220 t.Run("none_at_height_some_below", func(t *testing.T) { 221 dc, err := New() 222 require.NoError(t, err) 223 224 dc.deposits = []*dbpb.DepositContainer{ 225 { 226 Eth1BlockHeight: 8, 227 Index: 0, 228 Deposit: ðpb.Deposit{}, 229 }, 230 { 231 Eth1BlockHeight: 9, 232 Index: 1, 233 Deposit: ðpb.Deposit{}, 234 DepositRoot: wantedRoot, 235 }, 236 { 237 Eth1BlockHeight: 11, 238 Index: 2, 239 Deposit: ðpb.Deposit{}, 240 }, 241 } 242 n, root := dc.DepositsNumberAndRootAtHeight(context.Background(), big.NewInt(10)) 243 assert.Equal(t, 2, int(n)) 244 require.DeepEqual(t, wantedRoot, root[:]) 245 }) 246 t.Run("none_at_height_none_below", func(t *testing.T) { 247 dc, err := New() 248 require.NoError(t, err) 249 250 dc.deposits = []*dbpb.DepositContainer{ 251 { 252 Eth1BlockHeight: 8, 253 Index: 0, 254 Deposit: ðpb.Deposit{}, 255 DepositRoot: wantedRoot, 256 }, 257 } 258 n, root := dc.DepositsNumberAndRootAtHeight(context.Background(), big.NewInt(7)) 259 assert.Equal(t, 0, int(n)) 260 require.DeepEqual(t, params.BeaconConfig().ZeroHash, root) 261 }) 262 t.Run("none_at_height_one_below", func(t *testing.T) { 263 dc, err := New() 264 require.NoError(t, err) 265 266 dc.deposits = []*dbpb.DepositContainer{ 267 { 268 Eth1BlockHeight: 8, 269 Index: 0, 270 Deposit: ðpb.Deposit{}, 271 DepositRoot: wantedRoot, 272 }, 273 } 274 n, root := dc.DepositsNumberAndRootAtHeight(context.Background(), big.NewInt(10)) 275 assert.Equal(t, 1, int(n)) 276 require.DeepEqual(t, wantedRoot, root[:]) 277 }) 278 t.Run("some_greater_some_lower", func(t *testing.T) { 279 dc, err := New() 280 require.NoError(t, err) 281 282 dc.deposits = []*dbpb.DepositContainer{ 283 { 284 Eth1BlockHeight: 8, 285 Index: 0, 286 Deposit: ðpb.Deposit{}, 287 }, 288 { 289 Eth1BlockHeight: 8, 290 Index: 1, 291 Deposit: ðpb.Deposit{}, 292 }, 293 { 294 Eth1BlockHeight: 9, 295 Index: 2, 296 Deposit: ðpb.Deposit{}, 297 DepositRoot: wantedRoot, 298 }, 299 { 300 Eth1BlockHeight: 10, 301 Index: 3, 302 Deposit: ðpb.Deposit{}, 303 }, 304 { 305 Eth1BlockHeight: 10, 306 Index: 4, 307 Deposit: ðpb.Deposit{}, 308 }, 309 } 310 n, root := dc.DepositsNumberAndRootAtHeight(context.Background(), big.NewInt(9)) 311 assert.Equal(t, 3, int(n)) 312 require.DeepEqual(t, wantedRoot, root[:]) 313 }) 314 } 315 316 func TestDepositByPubkey_ReturnsFirstMatchingDeposit(t *testing.T) { 317 dc, err := New() 318 require.NoError(t, err) 319 320 dc.deposits = []*dbpb.DepositContainer{ 321 { 322 Eth1BlockHeight: 9, 323 Deposit: ðpb.Deposit{ 324 Data: ðpb.Deposit_Data{ 325 PublicKey: bytesutil.PadTo([]byte("pk0"), 48), 326 WithdrawalCredentials: make([]byte, 32), 327 Signature: make([]byte, 96), 328 }, 329 }, 330 }, 331 { 332 Eth1BlockHeight: 10, 333 Deposit: ðpb.Deposit{ 334 Data: ðpb.Deposit_Data{ 335 PublicKey: bytesutil.PadTo([]byte("pk1"), 48), 336 WithdrawalCredentials: make([]byte, 32), 337 Signature: make([]byte, 96), 338 }, 339 }, 340 }, 341 { 342 Eth1BlockHeight: 11, 343 Deposit: ðpb.Deposit{ 344 Data: ðpb.Deposit_Data{ 345 PublicKey: bytesutil.PadTo([]byte("pk1"), 48), 346 WithdrawalCredentials: make([]byte, 32), 347 Signature: make([]byte, 96), 348 }, 349 }, 350 }, 351 { 352 Eth1BlockHeight: 12, 353 Deposit: ðpb.Deposit{ 354 Data: ðpb.Deposit_Data{ 355 PublicKey: bytesutil.PadTo([]byte("pk2"), 48), 356 WithdrawalCredentials: make([]byte, 32), 357 Signature: make([]byte, 96), 358 }, 359 }, 360 }, 361 } 362 363 pk1 := bytesutil.PadTo([]byte("pk1"), 48) 364 dep, blkNum := dc.DepositByPubkey(context.Background(), pk1) 365 366 if dep == nil || !bytes.Equal(dep.Data.PublicKey, pk1) { 367 t.Error("Returned wrong deposit") 368 } 369 assert.Equal(t, 0, blkNum.Cmp(big.NewInt(10)), 370 fmt.Sprintf("Returned wrong block number %v", blkNum)) 371 } 372 373 func TestFinalizedDeposits_DepositsCachedCorrectly(t *testing.T) { 374 dc, err := New() 375 require.NoError(t, err) 376 377 finalizedDeposits := []*dbpb.DepositContainer{ 378 { 379 Deposit: ðpb.Deposit{ 380 Data: ðpb.Deposit_Data{ 381 PublicKey: bytesutil.PadTo([]byte{0}, 48), 382 WithdrawalCredentials: make([]byte, 32), 383 Signature: make([]byte, 96), 384 }, 385 }, 386 Index: 0, 387 }, 388 { 389 Deposit: ðpb.Deposit{ 390 Data: ðpb.Deposit_Data{ 391 PublicKey: bytesutil.PadTo([]byte{1}, 48), 392 WithdrawalCredentials: make([]byte, 32), 393 Signature: make([]byte, 96), 394 }, 395 }, 396 Index: 1, 397 }, 398 { 399 Deposit: ðpb.Deposit{ 400 Data: ðpb.Deposit_Data{ 401 PublicKey: bytesutil.PadTo([]byte{2}, 48), 402 WithdrawalCredentials: make([]byte, 32), 403 Signature: make([]byte, 96), 404 }, 405 }, 406 Index: 2, 407 }, 408 } 409 dc.deposits = append(finalizedDeposits, &dbpb.DepositContainer{ 410 Deposit: ðpb.Deposit{ 411 Data: ðpb.Deposit_Data{ 412 PublicKey: bytesutil.PadTo([]byte{3}, 48), 413 WithdrawalCredentials: make([]byte, 32), 414 Signature: make([]byte, 96), 415 }, 416 }, 417 Index: 3, 418 }) 419 420 dc.InsertFinalizedDeposits(context.Background(), 2) 421 422 cachedDeposits := dc.FinalizedDeposits(context.Background()) 423 require.NotNil(t, cachedDeposits, "Deposits not cached") 424 assert.Equal(t, int64(2), cachedDeposits.MerkleTrieIndex) 425 426 var deps [][]byte 427 for _, d := range finalizedDeposits { 428 hash, err := d.Deposit.Data.HashTreeRoot() 429 require.NoError(t, err, "Could not hash deposit data") 430 deps = append(deps, hash[:]) 431 } 432 trie, err := trieutil.GenerateTrieFromItems(deps, params.BeaconConfig().DepositContractTreeDepth) 433 require.NoError(t, err, "Could not generate deposit trie") 434 assert.Equal(t, trie.HashTreeRoot(), cachedDeposits.Deposits.HashTreeRoot()) 435 } 436 437 func TestFinalizedDeposits_UtilizesPreviouslyCachedDeposits(t *testing.T) { 438 dc, err := New() 439 require.NoError(t, err) 440 441 oldFinalizedDeposits := []*dbpb.DepositContainer{ 442 { 443 Deposit: ðpb.Deposit{ 444 Data: ðpb.Deposit_Data{ 445 PublicKey: bytesutil.PadTo([]byte{0}, 48), 446 WithdrawalCredentials: make([]byte, 32), 447 Signature: make([]byte, 96), 448 }, 449 }, 450 Index: 0, 451 }, 452 { 453 Deposit: ðpb.Deposit{ 454 Data: ðpb.Deposit_Data{ 455 PublicKey: bytesutil.PadTo([]byte{1}, 48), 456 WithdrawalCredentials: make([]byte, 32), 457 Signature: make([]byte, 96), 458 }, 459 }, 460 Index: 1, 461 }, 462 } 463 newFinalizedDeposit := dbpb.DepositContainer{ 464 Deposit: ðpb.Deposit{ 465 Data: ðpb.Deposit_Data{ 466 PublicKey: bytesutil.PadTo([]byte{2}, 48), 467 WithdrawalCredentials: make([]byte, 32), 468 Signature: make([]byte, 96), 469 }, 470 }, 471 Index: 2, 472 } 473 dc.deposits = oldFinalizedDeposits 474 dc.InsertFinalizedDeposits(context.Background(), 1) 475 // Artificially exclude old deposits so that they can only be retrieved from previously finalized deposits. 476 dc.deposits = []*dbpb.DepositContainer{&newFinalizedDeposit} 477 478 dc.InsertFinalizedDeposits(context.Background(), 2) 479 480 cachedDeposits := dc.FinalizedDeposits(context.Background()) 481 require.NotNil(t, cachedDeposits, "Deposits not cached") 482 assert.Equal(t, int64(2), cachedDeposits.MerkleTrieIndex) 483 484 var deps [][]byte 485 for _, d := range append(oldFinalizedDeposits, &newFinalizedDeposit) { 486 hash, err := d.Deposit.Data.HashTreeRoot() 487 require.NoError(t, err, "Could not hash deposit data") 488 deps = append(deps, hash[:]) 489 } 490 trie, err := trieutil.GenerateTrieFromItems(deps, params.BeaconConfig().DepositContractTreeDepth) 491 require.NoError(t, err, "Could not generate deposit trie") 492 assert.Equal(t, trie.HashTreeRoot(), cachedDeposits.Deposits.HashTreeRoot()) 493 } 494 495 func TestFinalizedDeposits_InitializedCorrectly(t *testing.T) { 496 dc, err := New() 497 require.NoError(t, err) 498 499 finalizedDeposits := dc.finalizedDeposits 500 assert.NotNil(t, finalizedDeposits) 501 assert.NotNil(t, finalizedDeposits.Deposits) 502 assert.Equal(t, int64(-1), finalizedDeposits.MerkleTrieIndex) 503 } 504 505 func TestNonFinalizedDeposits_ReturnsAllNonFinalizedDeposits(t *testing.T) { 506 dc, err := New() 507 require.NoError(t, err) 508 509 finalizedDeposits := []*dbpb.DepositContainer{ 510 { 511 Eth1BlockHeight: 10, 512 Deposit: ðpb.Deposit{ 513 Data: ðpb.Deposit_Data{ 514 PublicKey: bytesutil.PadTo([]byte{0}, 48), 515 WithdrawalCredentials: make([]byte, 32), 516 Signature: make([]byte, 96), 517 }, 518 }, 519 Index: 0, 520 }, 521 { 522 Eth1BlockHeight: 10, 523 Deposit: ðpb.Deposit{ 524 Data: ðpb.Deposit_Data{ 525 PublicKey: bytesutil.PadTo([]byte{1}, 48), 526 WithdrawalCredentials: make([]byte, 32), 527 Signature: make([]byte, 96), 528 }, 529 }, 530 Index: 1, 531 }, 532 } 533 dc.deposits = append(finalizedDeposits, 534 &dbpb.DepositContainer{ 535 Eth1BlockHeight: 10, 536 Deposit: ðpb.Deposit{ 537 Data: ðpb.Deposit_Data{ 538 PublicKey: bytesutil.PadTo([]byte{2}, 48), 539 WithdrawalCredentials: make([]byte, 32), 540 Signature: make([]byte, 96), 541 }, 542 }, 543 Index: 2, 544 }, 545 &dbpb.DepositContainer{ 546 Eth1BlockHeight: 11, 547 Deposit: ðpb.Deposit{ 548 Data: ðpb.Deposit_Data{ 549 PublicKey: bytesutil.PadTo([]byte{3}, 48), 550 WithdrawalCredentials: make([]byte, 32), 551 Signature: make([]byte, 96), 552 }, 553 }, 554 Index: 3, 555 }) 556 dc.InsertFinalizedDeposits(context.Background(), 1) 557 558 deps := dc.NonFinalizedDeposits(context.Background(), nil) 559 assert.Equal(t, 2, len(deps)) 560 } 561 562 func TestNonFinalizedDeposits_ReturnsNonFinalizedDepositsUpToBlockNumber(t *testing.T) { 563 dc, err := New() 564 require.NoError(t, err) 565 566 finalizedDeposits := []*dbpb.DepositContainer{ 567 { 568 Eth1BlockHeight: 10, 569 Deposit: ðpb.Deposit{ 570 Data: ðpb.Deposit_Data{ 571 PublicKey: bytesutil.PadTo([]byte{0}, 48), 572 WithdrawalCredentials: make([]byte, 32), 573 Signature: make([]byte, 96), 574 }, 575 }, 576 Index: 0, 577 }, 578 { 579 Eth1BlockHeight: 10, 580 Deposit: ðpb.Deposit{ 581 Data: ðpb.Deposit_Data{ 582 PublicKey: bytesutil.PadTo([]byte{1}, 48), 583 WithdrawalCredentials: make([]byte, 32), 584 Signature: make([]byte, 96), 585 }, 586 }, 587 Index: 1, 588 }, 589 } 590 dc.deposits = append(finalizedDeposits, 591 &dbpb.DepositContainer{ 592 Eth1BlockHeight: 10, 593 Deposit: ðpb.Deposit{ 594 Data: ðpb.Deposit_Data{ 595 PublicKey: bytesutil.PadTo([]byte{2}, 48), 596 WithdrawalCredentials: make([]byte, 32), 597 Signature: make([]byte, 96), 598 }, 599 }, 600 Index: 2, 601 }, 602 &dbpb.DepositContainer{ 603 Eth1BlockHeight: 11, 604 Deposit: ðpb.Deposit{ 605 Data: ðpb.Deposit_Data{ 606 PublicKey: bytesutil.PadTo([]byte{3}, 48), 607 WithdrawalCredentials: make([]byte, 32), 608 Signature: make([]byte, 96), 609 }, 610 }, 611 Index: 3, 612 }) 613 dc.InsertFinalizedDeposits(context.Background(), 1) 614 615 deps := dc.NonFinalizedDeposits(context.Background(), big.NewInt(10)) 616 assert.Equal(t, 1, len(deps)) 617 } 618 619 func TestPruneProofs_Ok(t *testing.T) { 620 dc, err := New() 621 require.NoError(t, err) 622 623 deposits := []struct { 624 blkNum uint64 625 deposit *ethpb.Deposit 626 index int64 627 }{ 628 { 629 blkNum: 0, 630 deposit: ðpb.Deposit{Proof: makeDepositProof()}, 631 index: 0, 632 }, 633 { 634 blkNum: 0, 635 deposit: ðpb.Deposit{Proof: makeDepositProof()}, 636 index: 1, 637 }, 638 { 639 blkNum: 0, 640 deposit: ðpb.Deposit{Proof: makeDepositProof()}, 641 index: 2, 642 }, 643 { 644 blkNum: 0, 645 deposit: ðpb.Deposit{Proof: makeDepositProof()}, 646 index: 3, 647 }, 648 } 649 650 for _, ins := range deposits { 651 assert.NoError(t, dc.InsertDeposit(context.Background(), ins.deposit, ins.blkNum, ins.index, [32]byte{})) 652 } 653 654 require.NoError(t, dc.PruneProofs(context.Background(), 1)) 655 656 assert.DeepEqual(t, [][]byte(nil), dc.deposits[0].Deposit.Proof) 657 assert.DeepEqual(t, [][]byte(nil), dc.deposits[1].Deposit.Proof) 658 assert.NotNil(t, dc.deposits[2].Deposit.Proof) 659 assert.NotNil(t, dc.deposits[3].Deposit.Proof) 660 } 661 662 func TestPruneProofs_SomeAlreadyPruned(t *testing.T) { 663 dc, err := New() 664 require.NoError(t, err) 665 666 deposits := []struct { 667 blkNum uint64 668 deposit *ethpb.Deposit 669 index int64 670 }{ 671 { 672 blkNum: 0, 673 deposit: ðpb.Deposit{Proof: nil}, 674 index: 0, 675 }, 676 { 677 blkNum: 0, 678 deposit: ðpb.Deposit{Proof: nil}, 679 index: 1, 680 }, 681 { 682 blkNum: 0, 683 deposit: ðpb.Deposit{Proof: makeDepositProof()}, 684 index: 2, 685 }, 686 { 687 blkNum: 0, 688 deposit: ðpb.Deposit{Proof: makeDepositProof()}, 689 index: 3, 690 }, 691 } 692 693 for _, ins := range deposits { 694 assert.NoError(t, dc.InsertDeposit(context.Background(), ins.deposit, ins.blkNum, ins.index, [32]byte{})) 695 } 696 697 require.NoError(t, dc.PruneProofs(context.Background(), 2)) 698 699 assert.DeepEqual(t, [][]byte(nil), dc.deposits[2].Deposit.Proof) 700 } 701 702 func TestPruneProofs_PruneAllWhenDepositIndexTooBig(t *testing.T) { 703 dc, err := New() 704 require.NoError(t, err) 705 706 deposits := []struct { 707 blkNum uint64 708 deposit *ethpb.Deposit 709 index int64 710 }{ 711 { 712 blkNum: 0, 713 deposit: ðpb.Deposit{Proof: makeDepositProof()}, 714 index: 0, 715 }, 716 { 717 blkNum: 0, 718 deposit: ðpb.Deposit{Proof: makeDepositProof()}, 719 index: 1, 720 }, 721 { 722 blkNum: 0, 723 deposit: ðpb.Deposit{Proof: makeDepositProof()}, 724 index: 2, 725 }, 726 { 727 blkNum: 0, 728 deposit: ðpb.Deposit{Proof: makeDepositProof()}, 729 index: 3, 730 }, 731 } 732 733 for _, ins := range deposits { 734 assert.NoError(t, dc.InsertDeposit(context.Background(), ins.deposit, ins.blkNum, ins.index, [32]byte{})) 735 } 736 737 require.NoError(t, dc.PruneProofs(context.Background(), 99)) 738 739 assert.DeepEqual(t, [][]byte(nil), dc.deposits[0].Deposit.Proof) 740 assert.DeepEqual(t, [][]byte(nil), dc.deposits[1].Deposit.Proof) 741 assert.DeepEqual(t, [][]byte(nil), dc.deposits[2].Deposit.Proof) 742 assert.DeepEqual(t, [][]byte(nil), dc.deposits[3].Deposit.Proof) 743 } 744 745 func TestPruneProofs_CorrectlyHandleLastIndex(t *testing.T) { 746 dc, err := New() 747 require.NoError(t, err) 748 749 deposits := []struct { 750 blkNum uint64 751 deposit *ethpb.Deposit 752 index int64 753 }{ 754 { 755 blkNum: 0, 756 deposit: ðpb.Deposit{Proof: makeDepositProof()}, 757 index: 0, 758 }, 759 { 760 blkNum: 0, 761 deposit: ðpb.Deposit{Proof: makeDepositProof()}, 762 index: 1, 763 }, 764 { 765 blkNum: 0, 766 deposit: ðpb.Deposit{Proof: makeDepositProof()}, 767 index: 2, 768 }, 769 { 770 blkNum: 0, 771 deposit: ðpb.Deposit{Proof: makeDepositProof()}, 772 index: 3, 773 }, 774 } 775 776 for _, ins := range deposits { 777 assert.NoError(t, dc.InsertDeposit(context.Background(), ins.deposit, ins.blkNum, ins.index, [32]byte{})) 778 } 779 780 require.NoError(t, dc.PruneProofs(context.Background(), 4)) 781 782 assert.DeepEqual(t, [][]byte(nil), dc.deposits[0].Deposit.Proof) 783 assert.DeepEqual(t, [][]byte(nil), dc.deposits[1].Deposit.Proof) 784 assert.DeepEqual(t, [][]byte(nil), dc.deposits[2].Deposit.Proof) 785 assert.DeepEqual(t, [][]byte(nil), dc.deposits[3].Deposit.Proof) 786 } 787 788 func makeDepositProof() [][]byte { 789 proof := make([][]byte, int(params.BeaconConfig().DepositContractTreeDepth)+1) 790 for i := range proof { 791 proof[i] = make([]byte, 32) 792 } 793 return proof 794 }