From 0a58837db06c9059278c09f7eafa044f3e1de89e Mon Sep 17 00:00:00 2001 From: Andreas Schildbach Date: Sat, 16 Jan 2016 16:41:05 +0100 Subject: [PATCH] TransactionInput: Add getConnectedTransaction(), as this seems to be a relevant usecase. --- .../main/java/org/bitcoinj/core/TransactionInput.java | 10 ++++++++++ core/src/main/java/org/bitcoinj/core/Wallet.java | 8 ++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/bitcoinj/core/TransactionInput.java b/core/src/main/java/org/bitcoinj/core/TransactionInput.java index 7532f2d7..918bee4b 100644 --- a/core/src/main/java/org/bitcoinj/core/TransactionInput.java +++ b/core/src/main/java/org/bitcoinj/core/TransactionInput.java @@ -432,6 +432,16 @@ public class TransactionInput extends ChildMessage { return getOutpoint().getConnectedOutput(); } + /** + * Returns the connected transaction, assuming the input was connected with + * {@link TransactionInput#connect(TransactionOutput)} or variants at some point. If it wasn't connected, then + * this method returns null. + */ + @Nullable + public Transaction getConnectedTransaction() { + return getOutpoint().fromTx; + } + /** Returns a copy of the input detached from its containing transaction, if need be. */ public TransactionInput duplicateDetached() { return new TransactionInput(params, null, bitcoinSerialize(), 0); diff --git a/core/src/main/java/org/bitcoinj/core/Wallet.java b/core/src/main/java/org/bitcoinj/core/Wallet.java index f1b432b7..d7b2ab21 100644 --- a/core/src/main/java/org/bitcoinj/core/Wallet.java +++ b/core/src/main/java/org/bitcoinj/core/Wallet.java @@ -2189,7 +2189,7 @@ public class Wallet extends BaseTaggableObject // Otherwise we saw a transaction spend our coins, but we didn't try and spend them ourselves yet. // The outputs are already marked as spent by the connect call above, so check if there are any more for // us to use. Move if not. - Transaction connected = checkNotNull(input.getOutpoint().fromTx); + Transaction connected = checkNotNull(input.getConnectedTransaction()); log.info(" marked {} as spent", input.getOutpoint()); maybeMovePool(connected, "prevtx"); // Just because it's connected doesn't mean it's actually ours: sometimes we have total visibility. @@ -2244,7 +2244,7 @@ public class Wallet extends BaseTaggableObject spent.remove(tx.getHash()); addWalletTransaction(Pool.DEAD, tx); for (TransactionInput deadInput : tx.getInputs()) { - Transaction connected = deadInput.getOutpoint().fromTx; + Transaction connected = deadInput.getConnectedTransaction(); if (connected == null) continue; if (connected.getConfidence().getConfidenceType() != ConfidenceType.DEAD && deadInput.getConnectedOutput().getSpentBy() != null && deadInput.getConnectedOutput().getSpentBy().equals(deadInput)) { checkState(myUnspents.add(deadInput.getConnectedOutput())); @@ -2272,13 +2272,13 @@ public class Wallet extends BaseTaggableObject for (TransactionInput input : overridingTx.getInputs()) { TransactionInput.ConnectionResult result = input.connect(unspent, TransactionInput.ConnectMode.DISCONNECT_ON_CONFLICT); if (result == TransactionInput.ConnectionResult.SUCCESS) { - maybeMovePool(input.getOutpoint().fromTx, "kill"); + maybeMovePool(input.getConnectedTransaction(), "kill"); myUnspents.remove(input.getConnectedOutput()); log.info("Removing from UNSPENTS: {}", input.getConnectedOutput()); } else { result = input.connect(spent, TransactionInput.ConnectMode.DISCONNECT_ON_CONFLICT); if (result == TransactionInput.ConnectionResult.SUCCESS) { - maybeMovePool(input.getOutpoint().fromTx, "kill"); + maybeMovePool(input.getConnectedTransaction(), "kill"); myUnspents.remove(input.getConnectedOutput()); log.info("Removing from UNSPENTS: {}", input.getConnectedOutput()); }