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

Commit a0532cb

Browse files
committed
do not confirm non-solid txs
1 parent 6a2653f commit a0532cb

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

src/main/java/com/iota/iri/service/ledger/LedgerService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,11 @@ public interface LedgerService {
101101
* @param visitedTransactions a set of transaction hashes that shall be considered to be visited already
102102
* @param startTransaction the transaction that marks the start of the dag traversal and that has its approvees
103103
* examined
104+
* @param allowGenesisReference Allows for confirmation of a transaction that references genesis
104105
* @return a map of the balance changes (addresses associated to their balance) or {@code null} if the balance could
105106
* not be generated due to inconsistencies
106107
* @throws LedgerException if anything unexpected happens while generating the balance changes
107108
*/
108-
Map<Hash, Long> generateBalanceDiff(Set<Hash> visitedTransactions, Hash startTransaction, int milestoneIndex)
109+
Map<Hash, Long> generateBalanceDiff(Set<Hash> visitedTransactions, Hash startTransaction, int milestoneIndex, boolean allowGenesisReference)
109110
throws LedgerException;
110111
}

src/main/java/com/iota/iri/service/ledger/impl/LedgerServiceImpl.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public boolean isBalanceDiffConsistent(Set<Hash> approvedHashes, Map<Hash, Long>
132132
}
133133
Set<Hash> visitedHashes = new HashSet<>(approvedHashes);
134134
Map<Hash, Long> currentState = generateBalanceDiff(visitedHashes, tip,
135-
snapshotProvider.getLatestSnapshot().getIndex());
135+
snapshotProvider.getLatestSnapshot().getIndex(), true);
136136
if (currentState == null) {
137137
return false;
138138
}
@@ -151,7 +151,7 @@ public boolean isBalanceDiffConsistent(Set<Hash> approvedHashes, Map<Hash, Long>
151151
}
152152

153153
@Override
154-
public Map<Hash, Long> generateBalanceDiff(Set<Hash> visitedTransactions, Hash startTransaction, int milestoneIndex)
154+
public Map<Hash, Long> generateBalanceDiff(Set<Hash> visitedTransactions, Hash startTransaction, int milestoneIndex, boolean allowGenesisReference)
155155
throws LedgerException {
156156

157157
Map<Hash, Long> state = new HashMap<>();
@@ -175,6 +175,18 @@ public Map<Hash, Long> generateBalanceDiff(Set<Hash> visitedTransactions, Hash s
175175

176176
boolean isEmptyTrunk = trunkTransactionViewModel.getType() == TransactionViewModel.PREFILLED_SLOT;
177177
boolean isEmptyBranch = branchTransactionViewModel.getType() == TransactionViewModel.PREFILLED_SLOT;
178+
179+
//Don't confirm non-solid txs.
180+
Hash genesisHash = Hash.NULL_HASH;
181+
if(isEmptyTrunk && !allowGenesisReference){
182+
return null;
183+
}
184+
//proceed only if empty trunk or branch is genesis
185+
if((isEmptyTrunk && !trunkTransactionViewModel.getHash().equals(genesisHash))||
186+
(isEmptyBranch && !branchTransactionViewModel.getHash().equals(genesisHash))){
187+
return null;
188+
}
189+
178190
boolean isApprovedTrunk = (trunkTransactionViewModel.snapshotIndex() > 0) && (trunkTransactionViewModel.snapshotIndex() != milestoneIndex);
179191
boolean isApprovedBranch = (branchTransactionViewModel.snapshotIndex() > 0) && (branchTransactionViewModel.snapshotIndex() != milestoneIndex);
180192
boolean isLeafTrunk = isEmptyTrunk || visitedTransactions.contains(trunkTransactionViewModel.getHash()) || isApprovedTrunk;
@@ -286,7 +298,7 @@ private boolean generateStateDiff(MilestoneViewModel milestone) throws LedgerExc
286298
try {
287299
Hash tail = transactionViewModel.getHash();
288300
Map<Hash, Long> balanceChanges = generateBalanceDiff(new HashSet<>(), tail,
289-
snapshotProvider.getLatestSnapshot().getIndex());
301+
snapshotProvider.getLatestSnapshot().getIndex(), true);
290302
successfullyProcessed = balanceChanges != null;
291303
if (successfullyProcessed) {
292304
milestoneService.updateMilestoneIndexOfMilestoneTransactions(milestone.getHash(),

src/test/java/com/iota/iri/service/ledger/impl/LedgerServiceImplTest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
import java.util.Collections;
99
import java.util.HashSet;
1010
import java.util.List;
11+
import java.util.Map;
1112

13+
import com.iota.iri.model.Hash;
14+
import org.junit.Assert;
1215
import org.junit.Before;
1316
import org.junit.Rule;
1417
import org.junit.Test;
@@ -71,7 +74,19 @@ public void generateBalanceDiffWithPersistsSpentAddresses() throws Exception {
7174
int milestoneIndex = 1;
7275
when(milestoneService.isTransactionConfirmed(tailTx, milestoneIndex)).thenReturn(false);
7376
when(snapshotProvider.getInitialSnapshot().getSolidEntryPoints()).thenReturn(Collections.emptyMap());
74-
ledgerService.generateBalanceDiff(new HashSet<>(), tailTx.getHash(), milestoneIndex);
77+
ledgerService.generateBalanceDiff(new HashSet<>(), tailTx.getHash(), milestoneIndex, true);
7578
verify(spentAddressesService, times(1)).persistValidatedSpentAddressesAsync(eq(bundle));
7679
}
80+
81+
@Test
82+
public void generateBalanceDiffWithGenesisReference() throws Exception {
83+
List<TransactionViewModel> bundle = TangleMockUtils.mockValidBundle(tangle, bundleValidator, 1,
84+
"A", "Z");
85+
TransactionViewModel tailTx = bundle.get(0);
86+
int milestoneIndex = 1;
87+
when(milestoneService.isTransactionConfirmed(tailTx, milestoneIndex)).thenReturn(false);
88+
when(snapshotProvider.getInitialSnapshot().getSolidEntryPoints()).thenReturn(Collections.emptyMap());
89+
Map<Hash, Long> diffMap = ledgerService.generateBalanceDiff(new HashSet<>(), tailTx.getHash(), milestoneIndex, false);
90+
Assert.assertNull("Diff map should be null because genesis trunk reference is not allowed", diffMap);
91+
}
7792
}

0 commit comments

Comments
 (0)