Skip to content

Commit

Permalink
Transaction.updateConfidenceId()
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarguindzberg committed Apr 26, 2019
1 parent 9552da9 commit c5f2120
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
13 changes: 12 additions & 1 deletion core/src/main/java/org/bitcoinj/core/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,12 @@ protected void unCache() {
super.unCache();
cachedTxId = null;
cachedWTxId = null;
confidence = null;
}

protected void updateConfidenceId() {
if (confidence != null) {
Context.get().getConfidenceTable().updateTxId(confidence, this.getTxId());
}
}

protected static int calcLength(byte[] buf, int offset) {
Expand Down Expand Up @@ -882,6 +887,7 @@ public void clearInputs() {
inputs.clear();
// You wanted to reserialize, right?
this.length = this.unsafeBitcoinSerialize().length;
updateConfidenceId();
}

/**
Expand All @@ -904,6 +910,7 @@ public TransactionInput addInput(TransactionInput input) {
input.setParent(this);
inputs.add(input);
adjustLength(inputs.size(), input.length);
updateConfidenceId();
return input;
}

Expand Down Expand Up @@ -989,6 +996,7 @@ public void clearOutputs() {
outputs.clear();
// You wanted to reserialize, right?
this.length = this.unsafeBitcoinSerialize().length;
updateConfidenceId();
}

/**
Expand All @@ -999,6 +1007,7 @@ public TransactionOutput addOutput(TransactionOutput to) {
to.setParent(this);
outputs.add(to);
adjustLength(outputs.size(), to.length);
updateConfidenceId();
return to;
}

Expand Down Expand Up @@ -1457,6 +1466,7 @@ public void setLockTime(long lockTime) {
log.warn("You are setting the lock time on a transaction but none of the inputs have non-default sequence numbers. This will not do what you expect!");
}
this.lockTime = lockTime;
updateConfidenceId();
}

public long getVersion() {
Expand All @@ -1466,6 +1476,7 @@ public long getVersion() {
public void setVersion(int version) {
this.version = version;
unCache();
updateConfidenceId();
}

/** Returns an unmodifiable view of all inputs. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public TransactionConfidence createConfidence(Sha256Hash hash) {
/** The time the transaction was last announced to us. */
private Date lastBroadcastedAt;
/** The Transaction that this confidence object is associated with. */
private final Sha256Hash hash;
private Sha256Hash hash;
// Lazily created listeners array.
private CopyOnWriteArrayList<ListenerRegistration<Listener>> listeners;

Expand Down Expand Up @@ -514,4 +514,8 @@ public synchronized ListenableFuture<TransactionConfidence> getDepthFuture(final
public Sha256Hash getTransactionHash() {
return hash;
}

public void updateTransactionHash(Sha256Hash hash) {
this.hash = hash;
}
}
8 changes: 8 additions & 0 deletions core/src/main/java/org/bitcoinj/core/TransactionInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ public long getSequenceNumber() {
public void setSequenceNumber(long sequence) {
unCache();
this.sequence = sequence;
updateTransactionConfidenceId();
}

/**
Expand Down Expand Up @@ -261,6 +262,7 @@ void setScriptBytes(byte[] scriptBytes) {
// 40 = previous_outpoint (36) + sequence (4)
int newLength = 40 + (scriptBytes == null ? 1 : VarInt.sizeOf(scriptBytes.length) + scriptBytes.length);
adjustLength(newLength - oldLength);
updateTransactionConfidenceId();
}

/**
Expand Down Expand Up @@ -294,6 +296,12 @@ public void setWitness(TransactionWitness witness) {
this.witness = witness;
}

private void updateTransactionConfidenceId() {
if (parent != null) {
getParentTransaction().updateConfidenceId();
}
}

/**
* Determine if the transaction has witnesses.
*
Expand Down
8 changes: 8 additions & 0 deletions core/src/main/java/org/bitcoinj/core/TransactionOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public void setValue(Coin value) {
checkNotNull(value);
unCache();
this.value = value.value;
updateTransactionConfidenceId();
}

/**
Expand Down Expand Up @@ -226,6 +227,13 @@ public Coin getMinNonDustValue() {
return getMinNonDustValue(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE.multiply(3));
}

private void updateTransactionConfidenceId() {
if (parent != null) {
getParentTransaction().updateConfidenceId();
}
}


/**
* Sets this objects availableForSpending flag to false and the spentBy pointer to the given input.
* If the input is null, it means this output was signed over to somebody else rather than one of our own keys.
Expand Down
7 changes: 7 additions & 0 deletions core/src/main/java/org/bitcoinj/core/TxConfidenceTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@
public class TxConfidenceTable {
protected ReentrantLock lock = Threading.lock("txconfidencetable");

public void updateTxId(TransactionConfidence confidence, Sha256Hash newTxId) {
WeakConfidenceReference wcr = table.remove(confidence.getTransactionHash());
//wcr.get().updateTransactionHash(newTxId);
confidence.updateTransactionHash(newTxId);
table.put(newTxId, wcr);
}

private static class WeakConfidenceReference extends WeakReference<TransactionConfidence> {
public Sha256Hash hash;
public WeakConfidenceReference(TransactionConfidence confidence, ReferenceQueue<TransactionConfidence> queue) {
Expand Down

0 comments on commit c5f2120

Please sign in to comment.