-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improved transactions getting logic. #18
base: multiple-hd-accounts
Are you sure you want to change the base?
Conversation
@@ -1616,7 +1616,7 @@ public boolean isConsistent() { | |||
public void isConsistentOrThrow() throws IllegalStateException { | |||
lock.lock(); | |||
try { | |||
Set<Transaction> transactions = getTransactions(true); | |||
Collection<Transaction> transactions = this.transactions.values(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is safe because we use the same lock as in getTransactions() and we don't change the transactions.
This is performant, as we don't need the Set
property. Only iteration.
@@ -1663,6 +1663,9 @@ boolean isTxConsistent(final Transaction tx, final boolean isSpent) { | |||
log.error("isAvailableForSpending != spentBy"); | |||
return false; | |||
} | |||
if (!isActuallySpent) { | |||
return !isSpent; | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, explain it to me.
if (!isActuallySpent) {
// return isSpent == isActuallySpent;
// return isSpent == false;
return !isSpent;
}
ok, so far so good. I would prefer if (!isActuallySpent) {break;}
though, for simplicity. The inconsistency is an exceptional case, so optimizing boolean expressions at the expense of readability is probably not worth it. Also isActuallySpent
variable becomes obsolete if you write it like this:
boolean isTxConsistent(final Transaction tx, final boolean isSpent) {
for (TransactionOutput o : tx.getOutputs()) {
if (o.isAvailableForSpending()) {
if (o.isMineOrWatched(this)) {
return !isSpent;
}
if (o.getSpentBy() != null) {
log.error("isAvailableForSpending != spentBy");
return false;
}
} else {
if (o.getSpentBy() == null) {
log.error("isAvailableForSpending != spentBy");
return false;
}
}
}
return isSpent;
}
which makes me even more confused about what is going on here.
Something like
boolean isTxConsistent(final Transaction tx, final boolean isSpent) {
Predicate<? super TransactionOutput> isUnspentPredicate = new Predicate<TransactionOutput>() {
@Override
public boolean apply(TransactionOutput transactionOutput) {
return transactionOutput.isAvailableForSpending() && transactionOutput.isMineOrWatched(Wallet.this);
}
};
return isSpent == !Iterators.any(tx.getOutputs().iterator(), isUnspentPredicate);
}
would be readable but what about the getSpentBy()
check? Is it necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good find but unfortunately one call to getTransactions
was changed to inlcude and return also the deads.
@@ -2991,7 +2994,7 @@ private void addWalletTransaction(Pool pool, Transaction tx) { | |||
if (numTransactions > size || numTransactions == 0) { | |||
numTransactions = size; | |||
} | |||
ArrayList<Transaction> all = new ArrayList<Transaction>(getTransactions(includeDead)); | |||
ArrayList<Transaction> all = new ArrayList<Transaction>(transactions.values()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
includeDead is not always true.
Removed unneeded hashset creation where possible, optimised method logic.
Tests passed as multiple-hd-accounts