From f7c09d74ea2616ca1ab189c42394270a38e00411 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Wed, 22 May 2013 12:00:01 +0200 Subject: [PATCH] Introduce Transaction.hasLockTime() and use it where appropriate. --- .../com/google/bitcoin/core/Transaction.java | 27 +++++++++++++------ .../google/bitcoin/core/TransactionInput.java | 3 +++ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/com/google/bitcoin/core/Transaction.java b/core/src/main/java/com/google/bitcoin/core/Transaction.java index 678628fd..2caf1af1 100644 --- a/core/src/main/java/com/google/bitcoin/core/Transaction.java +++ b/core/src/main/java/com/google/bitcoin/core/Transaction.java @@ -553,7 +553,7 @@ public class Transaction extends ChildMessage implements Serializable { // Basic info about the tx. StringBuffer s = new StringBuffer(); s.append(String.format(" %s: %s%n", getHashAsString(), getConfidence())); - if (lockTime > 0) { + if (isTimeLocked()) { String time; if (lockTime < LOCKTIME_THRESHOLD) { time = "block " + lockTime; @@ -1067,6 +1067,21 @@ public class Transaction extends ChildMessage implements Serializable { } } + /** + *

A transaction is time locked if at least one of its inputs is non-final and it has a lock time

+ * + *

To check if this transaction is final at a given height and time, see {@link Transaction#isFinal(int, long)} + *

+ */ + public boolean isTimeLocked() { + if (getLockTime() == 0) + return false; + for (TransactionInput input : getInputs()) + if (input.hasSequence()) + return true; + return false; + } + /** *

Returns true if this transaction is considered finalized and can be placed in a block. Non-finalized * transactions won't be included by miners and can be replaced with newer versions using sequence numbers. @@ -1077,16 +1092,12 @@ public class Transaction extends ChildMessage implements Serializable { * re-activated before this functionality is useful.

*/ public boolean isFinal(int height, long blockTimeSeconds) { - // Time based nLockTime implemented in 0.1.6 long time = getLockTime(); - if (time == 0) - return true; if (time < (time < LOCKTIME_THRESHOLD ? height : blockTimeSeconds)) return true; - for (TransactionInput in : inputs) - if (in.hasSequence()) - return false; - return true; + if (!isTimeLocked()) + return true; + return false; } /** diff --git a/core/src/main/java/com/google/bitcoin/core/TransactionInput.java b/core/src/main/java/com/google/bitcoin/core/TransactionInput.java index 7b777174..bec5c52d 100644 --- a/core/src/main/java/com/google/bitcoin/core/TransactionInput.java +++ b/core/src/main/java/com/google/bitcoin/core/TransactionInput.java @@ -370,6 +370,9 @@ public class TransactionInput extends ChildMessage implements Serializable { out.defaultWriteObject(); } + /** + * @returns true if this transaction's sequence number is set (ie it may be a part of a time-locked transaction) + */ public boolean hasSequence() { return sequence != NO_SEQUENCE; }