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

Commit

Permalink
do not confirm non-solid txs
Browse files Browse the repository at this point in the history
  • Loading branch information
acha-bill committed Mar 25, 2020
1 parent 8ad07c2 commit 89a9c48
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/iota/iri/service/ledger/LedgerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,12 @@ public interface LedgerService {
* @param enforceExtraRules enforce {@link com.iota.iri.BundleValidator#validateBundleTransactionsApproval(List)}
* and {@link com.iota.iri.BundleValidator#validateBundleTailApproval(Tangle, List)}.
* Enforcing them may break backwards compatibility.
* @param allowGenesisReference Allows for confirmation of a transaction that references genesis
* @return a map of the balance changes (addresses associated to their balance) or {@code null} if the balance could
* not be generated due to inconsistencies
* @throws LedgerException if anything unexpected happens while generating the balance changes
*/
Map<Hash, Long> generateBalanceDiff(Set<Hash> visitedTransactions, Hash startTransaction, int milestoneIndex,
boolean enforceExtraRules)
boolean enforceExtraRules, boolean allowGenesisReference)
throws LedgerException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public boolean isBalanceDiffConsistent(Set<Hash> approvedHashes, Map<Hash, Long>
}
Set<Hash> visitedHashes = new HashSet<>(approvedHashes);
Map<Hash, Long> currentState = generateBalanceDiff(visitedHashes, tip,
snapshotProvider.getLatestSnapshot().getIndex(), true);
snapshotProvider.getLatestSnapshot().getIndex(), true, false);
if (currentState == null) {
return false;
}
Expand All @@ -152,7 +152,7 @@ public boolean isBalanceDiffConsistent(Set<Hash> approvedHashes, Map<Hash, Long>

@Override
public Map<Hash, Long> generateBalanceDiff(Set<Hash> visitedTransactions, Hash startTransaction, int milestoneIndex,
boolean enforceExtraRules)
boolean enforceExtraRules, boolean allowGenesisReference)
throws LedgerException {

Map<Hash, Long> state = new HashMap<>();
Expand All @@ -176,6 +176,18 @@ public Map<Hash, Long> generateBalanceDiff(Set<Hash> visitedTransactions, Hash s

boolean isEmptyTrunk = trunkTransactionViewModel.getType() == TransactionViewModel.PREFILLED_SLOT;
boolean isEmptyBranch = branchTransactionViewModel.getType() == TransactionViewModel.PREFILLED_SLOT;

//Don't confirm non-solid txs.
Hash genesisHash = Hash.NULL_HASH;
if(isEmptyTrunk && !allowGenesisReference){
return null;
}
//proceed only if empty trunk or branch is genesis
if((isEmptyTrunk && !trunkTransactionViewModel.getHash().equals(genesisHash))||
(isEmptyBranch && !branchTransactionViewModel.getHash().equals(genesisHash))){
return null;
}

boolean isApprovedTrunk = (trunkTransactionViewModel.snapshotIndex() > 0) && (trunkTransactionViewModel.snapshotIndex() != milestoneIndex);
boolean isApprovedBranch = (branchTransactionViewModel.snapshotIndex() > 0) && (branchTransactionViewModel.snapshotIndex() != milestoneIndex);
boolean isLeafTrunk = isEmptyTrunk || visitedTransactions.contains(trunkTransactionViewModel.getHash()) || isApprovedTrunk;
Expand Down Expand Up @@ -287,7 +299,7 @@ private boolean generateStateDiff(MilestoneViewModel milestone) throws LedgerExc
try {
Hash tail = transactionViewModel.getHash();
Map<Hash, Long> balanceChanges = generateBalanceDiff(new HashSet<>(), tail,
snapshotProvider.getLatestSnapshot().getIndex(), false);
snapshotProvider.getLatestSnapshot().getIndex(), false, true);
successfullyProcessed = balanceChanges != null;
if (successfullyProcessed) {
milestoneService.updateMilestoneIndexOfMilestoneTransactions(milestone.getHash(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import com.iota.iri.model.Hash;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -71,7 +74,19 @@ public void generateBalanceDiffWithPersistsSpentAddresses() throws Exception {
int milestoneIndex = 1;
when(milestoneService.isTransactionConfirmed(tailTx, milestoneIndex)).thenReturn(false);
when(snapshotProvider.getInitialSnapshot().getSolidEntryPoints()).thenReturn(Collections.emptyMap());
ledgerService.generateBalanceDiff(new HashSet<>(), tailTx.getHash(), milestoneIndex, true);
ledgerService.generateBalanceDiff(new HashSet<>(), tailTx.getHash(), milestoneIndex, true, true);
verify(spentAddressesService, times(1)).persistValidatedSpentAddressesAsync(eq(bundle));
}

@Test
public void generateBalanceDiffWithGenesisReference() throws Exception{
List<TransactionViewModel> bundle = TangleMockUtils.mockValidBundle(tangle, bundleValidator, 1,
"A", "Z");
TransactionViewModel tailTx = bundle.get(0);
int milestoneIndex = 1;
when(milestoneService.isTransactionConfirmed(tailTx, milestoneIndex)).thenReturn(false);
when(snapshotProvider.getInitialSnapshot().getSolidEntryPoints()).thenReturn(Collections.emptyMap());
Map<Hash, Long> diffMap = ledgerService.generateBalanceDiff(new HashSet<>(), tailTx.getHash(), milestoneIndex, true, false);
Assert.assertNull("Diff map should be null because genesis trunk reference is not allowed", diffMap);
}
}

0 comments on commit 89a9c48

Please sign in to comment.