Skip to content

Commit

Permalink
Transaction: migrate isFinal() to Instant
Browse files Browse the repository at this point in the history
Keep the old method as deprecated.
  • Loading branch information
schildbach committed Sep 2, 2024
1 parent 3cd47de commit 086156e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ private void connectBlock(final Block block, StoredBlock storedPrev, boolean exp
throw new VerificationException("Block failed checkpoint lockin at " + (storedPrev.getHeight() + 1));
if (shouldVerifyTransactions()) {
for (Transaction tx : block.getTransactions())
if (!tx.isFinal(storedPrev.getHeight() + 1, block.getTimeSeconds()))
if (!tx.isFinal(storedPrev.getHeight() + 1, block.time()))
throw new VerificationException("Block contains non-final transaction");
}

Expand Down
12 changes: 9 additions & 3 deletions core/src/main/java/org/bitcoinj/core/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -1674,7 +1674,7 @@ public Sha256Hash findWitnessCommitment() {
* <p>A transaction is time-locked if at least one of its inputs is non-final and it has a lock time. A transaction can
* also have a relative lock time which this method doesn't tell. Use {@link #hasRelativeLockTime()} to find out.</p>
*
* <p>To check if this transaction is final at a given height and time, see {@link Transaction#isFinal(int, long)}
* <p>To check if this transaction is final at a given height and time, see {@link Transaction#isFinal(int, Instant)}
* </p>
*/
public boolean isTimeLocked() {
Expand Down Expand Up @@ -1720,12 +1720,18 @@ public boolean isOptInFullRBF() {
* <p>Note that currently the replacement feature is disabled in Bitcoin Core and will need to be
* re-activated before this functionality is useful.</p>
*/
public boolean isFinal(int height, long blockTimeSeconds) {
public boolean isFinal(int height, Instant blockTime) {
LockTime locktime = lockTime();
return locktime.rawValue() < (locktime instanceof HeightLock ? height : blockTimeSeconds) ||
return locktime.rawValue() < (locktime instanceof HeightLock ? height : blockTime.getEpochSecond()) ||
!isTimeLocked();
}

/** @deprecated use {@link #isFinal(int, Instant)} */
@Deprecated
public boolean isFinal(int height, long blockTimeSeconds) {
return isFinal(height, Instant.ofEpochSecond(blockTimeSeconds));
}

/**
* Returns either the lock time, if it was specified as a timestamp, or an estimate based on the time in
* the current head block if it was specified as a block height.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,13 @@ private Result analyzeIsFinal() {
// If the transaction has a lock time specified in blocks, we consider that if the tx would become final in the
// next block it is not risky (as it would confirm normally).
final int adjustedHeight = height + 1;
final long timeSecs = time.get().getEpochSecond();

if (!tx.isFinal(adjustedHeight, timeSecs)) {
if (!tx.isFinal(adjustedHeight, time.get())) {
nonFinal = tx;
return Result.NON_FINAL;
}
for (Transaction dep : dependencies) {
if (!dep.isFinal(adjustedHeight, timeSecs)) {
if (!dep.isFinal(adjustedHeight, time.get())) {
nonFinal = dep;
return Result.NON_FINAL;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,7 @@ public boolean add(Rule element) {
tx.addOutput(ZERO, OP_TRUE_SCRIPT);
addOnlyInputToTransaction(tx, out18, 0);
b62.addTransaction(tx);
checkState(!tx.isFinal(chainHeadHeight + 17, b62.block.getTimeSeconds()));
checkState(!tx.isFinal(chainHeadHeight + 17, b62.block.time()));
}
b62.solve();
blocks.add(new BlockAndValidity(b62, false, true, b60.getHash(), chainHeadHeight + 18, "b62"));
Expand All @@ -1194,7 +1194,7 @@ public boolean add(Rule element) {
{
b63.block.getTransactions().get(0).setLockTime(0xffffffffL);
b63.block.getTransactions().get(0).getInput(0).setSequenceNumber(0xdeadbeefL);
checkState(!b63.block.getTransactions().get(0).isFinal(chainHeadHeight + 17, b63.block.getTimeSeconds()));
checkState(!b63.block.getTransactions().get(0).isFinal(chainHeadHeight + 17, b63.block.time()));
}
b63.solve();
blocks.add(new BlockAndValidity(b63, false, true, b60.getHash(), chainHeadHeight + 18, "b63"));
Expand Down

0 comments on commit 086156e

Please sign in to comment.