public class TransactionConfidence extends Object implements Serializable
A TransactionConfidence object tracks data you can use to make a confidence decision about a transaction.
It also contains some pre-canned rules for common scenarios: if you aren't really sure what level of confidence
you need, these should prove useful. You can get a confidence object using Transaction.getConfidence()
.
They cannot be constructed directly.
Confidence in a transaction can come in multiple ways:
Alternatively, you may know that the transaction is "dead", that is, one or more of its inputs have been double spent and will never confirm unless there is another re-org.
TransactionConfidence is updated via the incrementDepthInBlocks()
method to ensure the block depth is up to date.
duplicate()
.Modifier and Type | Class and Description |
---|---|
static class |
TransactionConfidence.ConfidenceType
Describes the state of the transaction in general terms.
|
static interface |
TransactionConfidence.Listener
A confidence listener is informed when the level of
TransactionConfidence is updated by something, like
for example a Wallet . |
static class |
TransactionConfidence.Source
Information about where the transaction was first seen (network, sent direct from peer, created by ourselves).
|
Constructor and Description |
---|
TransactionConfidence(Transaction tx) |
Modifier and Type | Method and Description |
---|---|
void |
addEventListener(TransactionConfidence.Listener listener)
Adds an event listener that will be run when this confidence object is updated.
|
void |
addEventListener(TransactionConfidence.Listener listener,
Executor executor)
Adds an event listener that will be run when this confidence object is updated.
|
TransactionConfidence |
duplicate()
Returns a copy of this object.
|
int |
getAppearedAtChainHeight()
Returns the chain height at which the transaction appeared if confidence type is BUILDING.
|
ListIterator<PeerAddress> |
getBroadcastBy()
Returns a snapshot of
PeerAddress es that announced the transaction. |
TransactionConfidence.ConfidenceType |
getConfidenceType()
Returns a general statement of the level of confidence you can have in this transaction.
|
ListenableFuture<Transaction> |
getDepthFuture(int depth) |
ListenableFuture<Transaction> |
getDepthFuture(int depth,
Executor executor)
Returns a future that completes when the transaction has been confirmed by "depth" blocks.
|
int |
getDepthInBlocks()
Depth in the chain is an approximation of how much time has elapsed since the transaction has been confirmed.
|
Transaction |
getOverridingTransaction()
If this transaction has been overridden by a double spend (is dead), this call returns the overriding transaction.
|
TransactionConfidence.Source |
getSource()
The source of a transaction tries to identify where it came from originally.
|
void |
incrementDepthInBlocks()
Called by the wallet when the tx appears on the best chain and a new block is added to the top.
|
boolean |
markBroadcastBy(PeerAddress address)
Called by a
Peer when a transaction is pending and announced by a peer. |
int |
numBroadcastPeers()
Returns how many peers have been passed to
markBroadcastBy(org.bitcoinj.core.PeerAddress) . |
void |
queueListeners(TransactionConfidence.Listener.ChangeReason reason)
Call this after adjusting the confidence, for cases where listeners should be notified.
|
boolean |
removeEventListener(TransactionConfidence.Listener listener) |
void |
setAppearedAtChainHeight(int appearedAtChainHeight)
The chain height at which the transaction appeared, if it has been seen in the best chain.
|
void |
setConfidenceType(TransactionConfidence.ConfidenceType confidenceType)
Called by other objects in the system, like a
Wallet , when new information about the confidence of a
transaction becomes available. |
void |
setDepthInBlocks(int depth) |
void |
setOverridingTransaction(Transaction overridingTransaction)
Called when the transaction becomes newly dead, that is, we learn that one of its inputs has already been spent
in such a way that the double-spending transaction takes precedence over this one.
|
void |
setSource(TransactionConfidence.Source source)
The source of a transaction tries to identify where it came from originally.
|
String |
toString() |
boolean |
wasBroadcastBy(PeerAddress address)
Returns true if the given address has been seen via markBroadcastBy()
|
public TransactionConfidence(Transaction tx)
public void addEventListener(TransactionConfidence.Listener listener, Executor executor)
Adds an event listener that will be run when this confidence object is updated. The listener will be locked and is likely to be invoked on a peer thread.
Note that this is NOT called when every block arrives. Instead it is called when the transaction
transitions between confidence states, ie, from not being seen in the chain to being seen (not necessarily in
the best chain). If you want to know when the transaction gets buried under another block, consider using
a future from getDepthFuture(int)
.
public void addEventListener(TransactionConfidence.Listener listener)
Adds an event listener that will be run when this confidence object is updated. The listener will be locked and is likely to be invoked on a peer thread.
Note that this is NOT called when every block arrives. Instead it is called when the transaction
transitions between confidence states, ie, from not being seen in the chain to being seen (not necessarily in
the best chain). If you want to know when the transaction gets buried under another block, implement a
BlockChainListener
, attach it to a BlockChain
and then use the getters on the
confidence object to determine the new depth.
public boolean removeEventListener(TransactionConfidence.Listener listener)
public int getAppearedAtChainHeight()
IllegalStateException
- if the confidence type is not BUILDING.public void setAppearedAtChainHeight(int appearedAtChainHeight)
TransactionConfidence.ConfidenceType.BUILDING
and depth to one.public TransactionConfidence.ConfidenceType getConfidenceType()
public void setConfidenceType(TransactionConfidence.ConfidenceType confidenceType)
Wallet
, when new information about the confidence of a
transaction becomes available.public boolean markBroadcastBy(PeerAddress address)
Peer
when a transaction is pending and announced by a peer. The more peers announce the
transaction, the more peers have validated it (assuming your internet connection is not being intercepted).
If confidence is currently unknown, sets it to TransactionConfidence.ConfidenceType.PENDING
. Listeners will be
invoked in this case.address
- IP address of the peer, used as a proxy for identity.public int numBroadcastPeers()
markBroadcastBy(org.bitcoinj.core.PeerAddress)
.public ListIterator<PeerAddress> getBroadcastBy()
PeerAddress
es that announced the transaction.public boolean wasBroadcastBy(PeerAddress address)
public void incrementDepthInBlocks()
public int getDepthInBlocks()
Depth in the chain is an approximation of how much time has elapsed since the transaction has been confirmed. On average there is supposed to be a new block every 10 minutes, but the actual rate may vary. The reference (Satoshi) implementation considers a transaction impractical to reverse after 6 blocks, but as of EOY 2011 network security is high enough that often only one block is considered enough even for high value transactions. For low value transactions like songs, or other cheap items, no blocks at all may be necessary.
If the transaction appears in the top block, the depth is one. If it's anything else (pending, dead, unknown) the depth is zero.
public void setDepthInBlocks(int depth)
public Transaction getOverridingTransaction()
IllegalStateException
- if confidence type is not OVERRIDDEN_BY_DOUBLE_SPEND.public void setOverridingTransaction(@Nullable Transaction overridingTransaction)
public TransactionConfidence duplicate()
public void queueListeners(TransactionConfidence.Listener.ChangeReason reason)
public TransactionConfidence.Source getSource()
CoinSelector
implementations to risk analyze
transactions and decide when to spend them.public void setSource(TransactionConfidence.Source source)
CoinSelector
implementations to risk analyze
transactions and decide when to spend them.public ListenableFuture<Transaction> getDepthFuture(int depth, Executor executor)
public ListenableFuture<Transaction> getDepthFuture(int depth)
Copyright © 2014. All rights reserved.