|
19 | 19 | import static tech.pegasys.teku.ethereum.pow.api.DepositConstants.DEPOSIT_CONTRACT_TREE_DEPTH;
|
20 | 20 |
|
21 | 21 | import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
|
| 22 | +import java.util.ArrayList; |
22 | 23 | import java.util.List;
|
23 | 24 | import java.util.Optional;
|
| 25 | +import java.util.function.Function; |
24 | 26 | import org.apache.tuweni.bytes.Bytes32;
|
25 | 27 | import org.junit.jupiter.api.Test;
|
26 | 28 | import org.junit.jupiter.params.ParameterizedTest;
|
@@ -54,13 +56,58 @@ void shouldGenerateSnapshots(final int nonFinalizedDepositCount) throws Exceptio
|
54 | 56 |
|
55 | 57 | if (i >= nonFinalizedDepositCount) {
|
56 | 58 | final DepositTestCase finalisingTestCase = testCases.get(i - nonFinalizedDepositCount);
|
57 |
| - depositTree.finalize(finalisingTestCase.getEth1Data(), finalisingTestCase.getBlockHeight()); |
| 59 | + final Eth1Data correctEth1Data = finalisingTestCase.getEth1Data(); |
| 60 | + depositTree.finalize( |
| 61 | + new Eth1Data( |
| 62 | + correctEth1Data.getDepositRoot(), |
| 63 | + correctEth1Data.getDepositCount().minus(1), |
| 64 | + correctEth1Data.getBlockHash()), |
| 65 | + finalisingTestCase.getBlockHeight()); |
58 | 66 | final Optional<DepositTreeSnapshot> snapshotOptional = depositTree.getSnapshot();
|
59 | 67 | assertThat(snapshotOptional).contains(finalisingTestCase.getSnapshot());
|
60 | 68 | }
|
61 | 69 | }
|
62 | 70 | }
|
63 | 71 |
|
| 72 | + @Test |
| 73 | + void snapshotShouldHaveCorrectDepositCount() { |
| 74 | + final int depositsCount = 10; |
| 75 | + final Bytes32 depositRoot = Bytes32.random(); |
| 76 | + final Bytes32 blockHash = Bytes32.random(); |
| 77 | + final Function<Integer, Eth1Data> eth1Generator = |
| 78 | + deposits -> new Eth1Data(depositRoot, UInt64.valueOf(deposits), blockHash); |
| 79 | + |
| 80 | + final List<Bytes32> leafs = new ArrayList<>(); |
| 81 | + for (int i = 0; i < depositsCount; ++i) { |
| 82 | + leafs.add(Bytes32.random()); |
| 83 | + } |
| 84 | + final DepositTree fullTree = new DepositTree(); |
| 85 | + leafs.forEach(fullTree::pushLeaf); |
| 86 | + fullTree.finalize(eth1Generator.apply(depositsCount), UInt64.ZERO); |
| 87 | + final DepositTreeSnapshot fullTreeSnapshot = fullTree.getSnapshot().orElseThrow(); |
| 88 | + |
| 89 | + for (int finalized = 0; finalized <= depositsCount; ++finalized) { |
| 90 | + final DepositTree customTree = new DepositTree(); |
| 91 | + for (int j = 0; j < depositsCount; ++j) { |
| 92 | + customTree.pushLeaf(leafs.get(j)); |
| 93 | + } |
| 94 | + customTree.finalize(eth1Generator.apply(finalized), UInt64.ZERO); |
| 95 | + final Optional<DepositTreeSnapshot> customTreeSnapshot = customTree.getSnapshot(); |
| 96 | + System.out.println(customTreeSnapshot); |
| 97 | + switch (finalized) { |
| 98 | + case 0 -> assertThat(customTreeSnapshot).isEmpty(); |
| 99 | + case depositsCount -> assertThat(customTreeSnapshot).contains(fullTreeSnapshot); |
| 100 | + default -> { |
| 101 | + assertThat(customTreeSnapshot).isPresent(); |
| 102 | + final DepositTreeSnapshot snapshot = customTreeSnapshot.get(); |
| 103 | + assertThat(snapshot.getDepositCount()).isEqualTo(finalized); |
| 104 | + assertThat(snapshot.getDepositRoot()).isNotEqualTo(fullTreeSnapshot.getDepositRoot()); |
| 105 | + assertThat(snapshot.getFinalized()).isNotEqualTo(fullTreeSnapshot.getFinalized()); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
64 | 111 | @Test
|
65 | 112 | void shouldRestoreFromSnapshots() throws Exception {
|
66 | 113 | final List<DepositTestCase> testCases = loadEipTestCases();
|
|
0 commit comments