From 8f2a24239049929156ba010fc051f4b3a4eb1dd1 Mon Sep 17 00:00:00 2001 From: Luca Moser Date: Wed, 11 Sep 2019 16:58:39 +0200 Subject: [PATCH] stops submitting zero val bundles to the spent addresses service --- .../impl/SpentAddressesServiceImpl.java | 3 ++ .../impl/SpentAddressesServiceImplTest.java | 45 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/test/java/com/iota/iri/service/spentaddresses/impl/SpentAddressesServiceImplTest.java diff --git a/src/main/java/com/iota/iri/service/spentaddresses/impl/SpentAddressesServiceImpl.java b/src/main/java/com/iota/iri/service/spentaddresses/impl/SpentAddressesServiceImpl.java index 905d639dac..4d6df23813 100644 --- a/src/main/java/com/iota/iri/service/spentaddresses/impl/SpentAddressesServiceImpl.java +++ b/src/main/java/com/iota/iri/service/spentaddresses/impl/SpentAddressesServiceImpl.java @@ -95,6 +95,9 @@ public void persistSpentAddresses(Collection transactions) } public void persistValidatedSpentAddressesAsync(Collection transactions) { + if(transactions.stream().noneMatch(tx -> tx.value() < 0)){ + return; + } asyncSpentAddressesPersistor.submit(() -> { try { List spentAddresses = transactions.stream() diff --git a/src/test/java/com/iota/iri/service/spentaddresses/impl/SpentAddressesServiceImplTest.java b/src/test/java/com/iota/iri/service/spentaddresses/impl/SpentAddressesServiceImplTest.java new file mode 100644 index 0000000000..b0fc84e97a --- /dev/null +++ b/src/test/java/com/iota/iri/service/spentaddresses/impl/SpentAddressesServiceImplTest.java @@ -0,0 +1,45 @@ +package com.iota.iri.service.spentaddresses.impl; + +import com.iota.iri.BundleValidator; +import com.iota.iri.TangleMockUtils; +import com.iota.iri.controllers.TransactionViewModel; +import com.iota.iri.service.snapshot.SnapshotProvider; +import com.iota.iri.service.spentaddresses.SpentAddressesProvider; +import com.iota.iri.storage.Tangle; +import org.junit.Rule; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnit; +import org.mockito.junit.MockitoRule; + +import java.util.List; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +public class SpentAddressesServiceImplTest { + + @Rule + public MockitoRule mockitoRule = MockitoJUnit.rule(); + + @Mock + private Tangle tangle; + + @Mock + private BundleValidator bundleValidator; + + @Mock + private SnapshotProvider snapshotProvider; + + @Mock + private SpentAddressesProvider spentAddressesProvider; + + @Test + public void doesntPersistZeroValueBundles() throws Exception { + SpentAddressesServiceImpl spentAddressesService = new SpentAddressesServiceImpl(); + spentAddressesService.init(tangle, snapshotProvider, spentAddressesProvider, bundleValidator, null); + List bundle = TangleMockUtils.mockValidBundle(tangle, bundleValidator, 1); + spentAddressesService.persistValidatedSpentAddressesAsync(bundle); + verify(spentAddressesProvider, never()).saveAddressesBatch(any()); + } +}