Skip to content
This repository was archived by the owner on Aug 23, 2020. It is now read-only.

Fix: fixes spent states of addresses not getting persisted on tx pruning #1437

Merged
merged 3 commits into from
May 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/com/iota/iri/Iota.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ private void injectDependencies() throws SnapshotException, TransactionPruningEx
ledgerService.init(tangle, snapshotProvider, snapshotService, milestoneService, spentAddressesService,
bundleValidator);
if (transactionPruner != null) {
transactionPruner.init(tangle, snapshotProvider, spentAddressesService, tipsViewModel, configuration);
transactionPruner.init(tangle, snapshotProvider, spentAddressesService, spentAddressesProvider, tipsViewModel, configuration);
}
transactionRequesterWorker.init(tangle, transactionRequester, tipsViewModel, node);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.iota.iri.controllers.TipsViewModel;
import com.iota.iri.service.snapshot.Snapshot;
import com.iota.iri.service.spentaddresses.SpentAddressesProvider;
import com.iota.iri.service.spentaddresses.SpentAddressesService;
import com.iota.iri.storage.Tangle;

Expand Down Expand Up @@ -37,6 +38,14 @@ public interface TransactionPrunerJob {
*/
void setSpentAddressesService(SpentAddressesService spentAddressesService);

/**
* Allows to set the {@link SpentAddressesProvider} that will ensure that spent addresses are written
* to the persistence layer when their corresponding transactions are pruned.
*
* @param spentAddressesProvider service to be injected
*/
void setSpentAddressesProvider(SpentAddressesProvider spentAddressesProvider);

/**
* Allows to set the {@link Tangle} object that this job should work on.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.iota.iri.conf.SnapshotConfig;
import com.iota.iri.controllers.TipsViewModel;
import com.iota.iri.service.snapshot.SnapshotProvider;
import com.iota.iri.service.spentaddresses.SpentAddressesProvider;
import com.iota.iri.service.spentaddresses.SpentAddressesService;
import com.iota.iri.service.transactionpruning.TransactionPruner;
import com.iota.iri.service.transactionpruning.TransactionPrunerJob;
Expand Down Expand Up @@ -67,6 +68,11 @@ public class AsyncTransactionPruner implements TransactionPruner {
*/
private SpentAddressesService spentAddressesService;

/**
* Used to check whether an address is already persisted in the persistence layer.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just want to let you know my opinion.
These types of comments could have simply been placed only on the SpentAddressProvider class file and that's it. Via an IDE you could read it from all classes that use it as dependency.

Right now putting such a comment in every place we use the SpentAddressProvider is kind of repetitive, and pretty much violates DRY (do not repeat yourself). If we want to change the comment we have to change it in multiple places.

The reason we have those is because Hans, who did a great job, really liked to have them everywhere.

Having said that, since currently the entire class looks like this, it is perfectly ok that you added the comment and no need to remove it now.

private SpentAddressesProvider spentAddressesProvider;

/**
* Manager for the tips (required for removing pruned transactions from this manager).
*/
Expand Down Expand Up @@ -123,12 +129,15 @@ public class AsyncTransactionPruner implements TransactionPruner {
* @return the initialized instance itself to allow chaining
*/
public AsyncTransactionPruner init(Tangle tangle, SnapshotProvider snapshotProvider,
SpentAddressesService spentAddressesService, TipsViewModel tipsViewModel,
SpentAddressesService spentAddressesService,
SpentAddressesProvider spentAddressesProvider,
TipsViewModel tipsViewModel,
SnapshotConfig config) {

this.tangle = tangle;
this.snapshotProvider = snapshotProvider;
this.spentAddressesService = spentAddressesService;
this.spentAddressesProvider = spentAddressesProvider;
this.tipsViewModel = tipsViewModel;
this.config = config;

Expand All @@ -150,6 +159,7 @@ public AsyncTransactionPruner init(Tangle tangle, SnapshotProvider snapshotProvi
public void addJob(TransactionPrunerJob job) throws TransactionPruningException {
job.setTransactionPruner(this);
job.setSpentAddressesService(spentAddressesService);
job.setSpentAddressesProvider(spentAddressesProvider);
job.setTangle(tangle);
job.setTipsViewModel(tipsViewModel);
job.setSnapshot(snapshotProvider.getInitialSnapshot());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.iota.iri.controllers.TipsViewModel;
import com.iota.iri.service.snapshot.Snapshot;
import com.iota.iri.service.spentaddresses.SpentAddressesProvider;
import com.iota.iri.service.spentaddresses.SpentAddressesService;
import com.iota.iri.service.transactionpruning.TransactionPruner;
import com.iota.iri.service.transactionpruning.TransactionPrunerJob;
Expand All @@ -24,10 +25,15 @@ public abstract class AbstractTransactionPrunerJob implements TransactionPrunerJ
private TransactionPruner transactionPruner;

/**
* Ascertains that pruned transactions are recorded as spent addresses where necessary
* Ascertains that pruned transactions are recorded as spent addresses where necessary.
*/
protected SpentAddressesService spentAddressesService;

/**
* Ascertains whether transactions are already added to the underlying persistence layer.
*/
protected SpentAddressesProvider spentAddressesProvider;

/**
* Holds a reference to the tangle object which acts as a database interface.
*/
Expand Down Expand Up @@ -96,6 +102,11 @@ public void setSpentAddressesService(SpentAddressesService spentAddressesService
this.spentAddressesService = spentAddressesService;
}

@Override
public void setSpentAddressesProvider(SpentAddressesProvider spentAddressesProvider) {
this.spentAddressesProvider = spentAddressesProvider;
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ private void cleanupMilestoneTransactions() throws TransactionPruningException {
approvedTransaction -> approvedTransaction.snapshotIndex() >= milestoneViewModel.index(),
approvedTransaction -> {
if (approvedTransaction.value() < 0 &&
!spentAddressesService.wasAddressSpentFrom(approvedTransaction.getAddressHash())) {
!spentAddressesProvider.containsAddress(approvedTransaction.getAddressHash())) {
log.warn("Pruned spend transaction " + approvedTransaction.getHash() +
" did not have its spent address recorded. Persisting it now");
spentAddressesService
Expand Down