Skip to content
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

Open
wants to merge 3 commits into
base: multiple-hd-accounts
Choose a base branch
from

Conversation

sergeylappo
Copy link

@sergeylappo sergeylappo commented Apr 9, 2018

Removed unneeded hashset creation where possible, optimised method logic.
Tests passed as multiple-hd-accounts

@@ -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();
Copy link

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;
}
Copy link

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?

Copy link

@Giszmo Giszmo left a 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());
Copy link

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.

@sergeylappo sergeylappo removed their assignment Jul 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants