Package org.bitcoinj.core
Class TransactionBroadcast
java.lang.Object
org.bitcoinj.core.TransactionBroadcast
Represents a single transaction broadcast that we are performing. A broadcast occurs after a new transaction is created
(typically by a
Wallet
) and needs to be sent to the network. A broadcast can succeed or fail. A success is
defined as seeing the transaction be announced by peers via inv messages, thus indicating their acceptance. A failure
is defined as not reaching acceptance within a timeout period, or getting an explicit reject message from a peer
indicating that the transaction was not acceptable.-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
An interface for receiving progress information on the propagation of the tx, from 0.0 to 1.0 -
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionWait for confirmation the transaction has been relayed.Wait for confirmation the transaction has been sent to a remote peer.Deprecated.Broadcast the transaction and wait for confirmation that the transaction has been received by the appropriate number of Peers before completing.Broadcast this transaction to the proper calculated number of peers.static TransactionBroadcast
createMockBroadcast
(Transaction tx, CompletableFuture<Transaction> future) future()
Deprecated.UseawaitRelayed()
(and maybeCompletableFuture.thenApply(Function)
)void
setDropPeersAfterBroadcast
(boolean dropPeersAfterBroadcast) void
setMinConnections
(int minConnections) void
Sets the given callback for receiving progress values, which will run on the user thread.void
setProgressCallback
(TransactionBroadcast.ProgressCallback callback, Executor executor) Sets the given callback for receiving progress values, which will run on the given executor.
-
Field Details
-
random
Used for shuffling the peers before broadcast: unit tests can replace this to make themselves deterministic.
-
-
Method Details
-
transaction
-
createMockBroadcast
public static TransactionBroadcast createMockBroadcast(Transaction tx, CompletableFuture<Transaction> future) -
future
Deprecated.UseawaitRelayed()
(and maybeCompletableFuture.thenApply(Function)
)- Returns:
- future that completes when some number of remote peers has rebroadcast the transaction
-
setMinConnections
public void setMinConnections(int minConnections) -
setDropPeersAfterBroadcast
public void setDropPeersAfterBroadcast(boolean dropPeersAfterBroadcast) -
broadcastOnly
Broadcast this transaction to the proper calculated number of peers. Returns a future that completes when the message has been "sent" to a set of remote peers. TheTransactionBroadcast
itself is the returned type/value for the future.The complete broadcast process includes the following steps:
- Wait until enough
Peer
s are connected. - Broadcast the transaction to a determined number of
Peer
s - Wait for confirmation from a determined number of remote peers that they have received the broadcast
- Mark
awaitRelayed()
()} ("seen future") as complete
It should further be noted that "broadcast" in this class means that
MessageWriteTarget.writeBytes(byte[])
has completed successfully which means the message has been sent to the "OS network buffer" -- seeMessageWriteTarget.writeBytes(byte[])
or its implementation.- Returns:
- A future that completes when the message has been sent (or at least buffered) to the correct number of remote Peers. The future will complete exceptionally if any of the peer broadcasts fails.
- Wait until enough
-
broadcastAndAwaitRelay
Broadcast the transaction and wait for confirmation that the transaction has been received by the appropriate number of Peers before completing.- Returns:
- A future that completes when the message has been relayed by the appropriate number of remote peers
-
awaitRelayed
Wait for confirmation the transaction has been relayed.- Returns:
- A future that completes when the message has been relayed by the appropriate number of remote peers
-
awaitSent
Wait for confirmation the transaction has been sent to a remote peer. (Or at least buffered to be sent to a peer.)- Returns:
- A future that completes when the message has been relayed by the appropriate number of remote peers
-
broadcast
Deprecated.UsebroadcastAndAwaitRelay()
orbroadcastOnly()
as appropriateIf you migrate tobroadcastAndAwaitRelay()
and need aCompletableFuture
that returnsTransaction
you can use:CompletableFuture<Transaction> seenFuture = broadcast .broadcastAndAwaitRelay() .thenApply(TransactionBroadcast::transaction);
-
setProgressCallback
Sets the given callback for receiving progress values, which will run on the user thread. SeeThreading
for details. If the broadcast has already started then the callback will be invoked immediately with the current progress. -
setProgressCallback
public void setProgressCallback(TransactionBroadcast.ProgressCallback callback, @Nullable Executor executor) Sets the given callback for receiving progress values, which will run on the given executor. If the executor is null then the callback will run on a network thread and may be invoked multiple times in parallel. You probably want to provide your UI thread or Threading.USER_THREAD for the second parameter. If the broadcast has already started then the callback will be invoked immediately with the current progress.
-
broadcastAndAwaitRelay()
orbroadcastOnly()
as appropriate