diff --git a/src/main/java/com/iota/iri/service/transactionpruning/impl/PrunedTransactionVerifierImpl.java b/src/main/java/com/iota/iri/service/transactionpruning/impl/PrunedTransactionVerifierImpl.java index 9928392572..31f8ac3cb7 100644 --- a/src/main/java/com/iota/iri/service/transactionpruning/impl/PrunedTransactionVerifierImpl.java +++ b/src/main/java/com/iota/iri/service/transactionpruning/impl/PrunedTransactionVerifierImpl.java @@ -42,6 +42,7 @@ public PrunedTransactionVerifierImpl(PrunedTransactionProvider provider, Transac this.requester = requester; verifiedFalse = new LinkedList<>(); + prunedHashTest = new HashMap<>(); } /** @@ -68,8 +69,8 @@ public boolean isPruned(Hash hash) throws PrunedTransactionException{ return false; } - if (prunedHashTest == null || !prunedHashTest.containsKey(hash)) { - return initializeVerify(hash); + if (!prunedHashTest.containsKey(hash)) { + initializeVerify(hash); } return prunedHashTest.get(hash) >= PRUNED_CERTAIN; @@ -146,17 +147,17 @@ private List addParentForChild(Hash parent, Hash child) { if (parents.containsKey(child)) { list = parents.get(child); } else { - list = parents.put(child, new LinkedList<>()); + list = new LinkedList<>(); + parents.put(child, list); } list.add(parent); return list; } - private boolean initializeVerify(Hash hash) { + private void initializeVerify(Hash hash) { addParentForChild(hash, hash); prunedHashTest.put(hash, 1); - return true; } private Hash getChildForParent(Hash parent) { diff --git a/src/test/java/com/iota/iri/service/transactionpruning/PrunedTransactionVerifierImplTest.java b/src/test/java/com/iota/iri/service/transactionpruning/PrunedTransactionVerifierImplTest.java new file mode 100644 index 0000000000..e15e1c7379 --- /dev/null +++ b/src/test/java/com/iota/iri/service/transactionpruning/PrunedTransactionVerifierImplTest.java @@ -0,0 +1,97 @@ +package com.iota.iri.service.transactionpruning; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnit; +import org.mockito.junit.MockitoRule; + +import com.iota.iri.TransactionTestUtils; +import com.iota.iri.conf.SnapshotConfig; +import com.iota.iri.controllers.TransactionViewModel; +import com.iota.iri.model.Hash; +import com.iota.iri.network.TransactionRequester; +import com.iota.iri.service.transactionpruning.impl.PrunedTransactionProviderImpl; +import com.iota.iri.service.transactionpruning.impl.PrunedTransactionVerifierImpl; + +public class PrunedTransactionVerifierImplTest { + + private static final Hash A = TransactionTestUtils.getRandomTransactionHash(); + private static final Hash B = TransactionTestUtils.getRandomTransactionHash(); + + @Rule + public MockitoRule mockitoRule = MockitoJUnit.rule(); + + @Mock + public SnapshotConfig config; + + @Rule + public final TemporaryFolder dbFolder = new TemporaryFolder(); + + @Rule + public final TemporaryFolder logFolder = new TemporaryFolder(); + + @Mock + PrunedTransactionProviderImpl provider; + + @Mock + TransactionRequester requester; + + PrunedTransactionVerifierImpl verifier; + + @Before + public void setUp() throws PrunedTransactionException { + Mockito.when(config.getPrunedTransactionsDbPath()).thenReturn(dbFolder.getRoot().getAbsolutePath()); + Mockito.when(config.getPrunedTransactionsDbLogPath()).thenReturn(logFolder.getRoot().getAbsolutePath()); + + verifier = new PrunedTransactionVerifierImpl(provider, requester); + } + + @After + public void tearDown() { + dbFolder.delete(); + } + + @Test + public void isPossiblyPrunedTest() throws PrunedTransactionException { + Mockito.when(provider.containsTransaction(A)).thenReturn(true); + + assertTrue(verifier.isPossiblyPruned(A)); + assertFalse(verifier.isPossiblyPruned(B)); + } + + @Test + public void waitingForHashTest() throws PrunedTransactionException { + assertFalse(verifier.isPruned(A)); + assertTrue(verifier.waitingForHash(A)); + + assertFalse(verifier.waitingForHash(B)); + } + + @Test + public void completePruneCheckTest() throws PrunedTransactionException { + // 10 hashes in the pruner is enough to verify + // 0 -> 1 -> 3 -> 7 + // -> 4 -> 8 + // 2 -> 5 -> 9 + // -> 6 + + /* This makes no sense to do now, will continue after PR #1363 + TransactionViewModel[] tvms = new TransactionViewModel[10]; + tvms[9] = TransactionTestUtils.createTransactionFromTrits(TransactionTestUtils.getRandomTransactionTrits()); + tvms[8] = TransactionTestUtils.createTransactionFromTrits(TransactionTestUtils.getRandomTransactionTrits()); + tvms[7] = TransactionTestUtils.createTransactionFromTrits(TransactionTestUtils.getRandomTransactionTrits()); + tvms[6] = TransactionTestUtils.createTransactionFromTrits(TransactionTestUtils.getRandomTransactionTrits()); + + tvms[5] = TransactionTestUtils.createTransactionWithTrunkAndBranch(TransactionTestUtils., trunk, branch) + */ + } + +}