TransactionConfidence: Convert to CompletableFuture

This commit is contained in:
Sean Gilligan 2022-03-03 18:31:19 -08:00
parent 52babf52b0
commit eb56d71f8f
3 changed files with 12 additions and 16 deletions

View file

@ -19,8 +19,6 @@ package org.bitcoinj.core;
import com.google.common.collect.*; import com.google.common.collect.*;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.SettableFuture;
import org.bitcoinj.utils.*; import org.bitcoinj.utils.*;
import org.bitcoinj.wallet.CoinSelector; import org.bitcoinj.wallet.CoinSelector;
import org.bitcoinj.wallet.Wallet; import org.bitcoinj.wallet.Wallet;
@ -489,23 +487,23 @@ public class TransactionConfidence {
* depth to one will wait until it appears in a block on the best chain, and zero will wait until it has been seen * depth to one will wait until it appears in a block on the best chain, and zero will wait until it has been seen
* on the network. * on the network.
*/ */
public synchronized ListenableFuture<TransactionConfidence> getDepthFuture(final int depth, Executor executor) { public synchronized ListenableCompletableFuture<TransactionConfidence> getDepthFuture(final int depth, Executor executor) {
final SettableFuture<TransactionConfidence> result = SettableFuture.create(); final ListenableCompletableFuture<TransactionConfidence> result = new ListenableCompletableFuture<>();
if (getDepthInBlocks() >= depth) { if (getDepthInBlocks() >= depth) {
result.set(this); result.complete(this);
} }
addEventListener(executor, new Listener() { addEventListener(executor, new Listener() {
@Override public void onConfidenceChanged(TransactionConfidence confidence, ChangeReason reason) { @Override public void onConfidenceChanged(TransactionConfidence confidence, ChangeReason reason) {
if (getDepthInBlocks() >= depth) { if (getDepthInBlocks() >= depth) {
removeEventListener(this); removeEventListener(this);
result.set(confidence); result.complete(confidence);
} }
} }
}); });
return result; return result;
} }
public synchronized ListenableFuture<TransactionConfidence> getDepthFuture(final int depth) { public synchronized ListenableCompletableFuture<TransactionConfidence> getDepthFuture(final int depth) {
return getDepthFuture(depth, Threading.USER_THREAD); return getDepthFuture(depth, Threading.USER_THREAD);
} }

View file

@ -77,6 +77,7 @@ import java.math.BigInteger;
import java.net.InetAddress; import java.net.InetAddress;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.*; import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
@ -423,7 +424,7 @@ public class WalletTest extends TestWithWallet {
// Send some pending coins to the wallet. // Send some pending coins to the wallet.
Transaction t1 = sendMoneyToWallet(wallet, null, amount, toAddress); Transaction t1 = sendMoneyToWallet(wallet, null, amount, toAddress);
Threading.waitForUserCode(); Threading.waitForUserCode();
final ListenableFuture<TransactionConfidence> depthFuture = t1.getConfidence().getDepthFuture(1); final CompletableFuture<TransactionConfidence> depthFuture = t1.getConfidence().getDepthFuture(1);
assertFalse(depthFuture.isDone()); assertFalse(depthFuture.isDone());
assertEquals(ZERO, wallet.getBalance(Wallet.BalanceType.AVAILABLE)); assertEquals(ZERO, wallet.getBalance(Wallet.BalanceType.AVAILABLE));
assertEquals(amount, wallet.getBalance(Wallet.BalanceType.ESTIMATED)); assertEquals(amount, wallet.getBalance(Wallet.BalanceType.ESTIMATED));

View file

@ -104,19 +104,16 @@ public class ForwardingService {
// to be double spent, no harm done. Wallet.allowSpendingUnconfirmedTransactions() would have to // to be double spent, no harm done. Wallet.allowSpendingUnconfirmedTransactions() would have to
// be called in onSetupCompleted() above. But we don't do that here to demonstrate the more common // be called in onSetupCompleted() above. But we don't do that here to demonstrate the more common
// case of waiting for a block. // case of waiting for a block.
Futures.addCallback(tx.getConfidence().getDepthFuture(1), new FutureCallback<TransactionConfidence>() {
@Override tx.getConfidence().getDepthFuture(1).whenComplete((result, t) -> {
public void onSuccess(TransactionConfidence result) { if (result != null) {
System.out.println("Confirmation received."); System.out.println("Confirmation received.");
forwardCoins(); forwardCoins();
} } else {
@Override
public void onFailure(Throwable t) {
// This kind of future can't fail, just rethrow in case something weird happens. // This kind of future can't fail, just rethrow in case something weird happens.
throw new RuntimeException(t); throw new RuntimeException(t);
} }
}, MoreExecutors.directExecutor()); });
}); });
Address sendToAddress = LegacyAddress.fromKey(params, kit.wallet().currentReceiveKey()); Address sendToAddress = LegacyAddress.fromKey(params, kit.wallet().currentReceiveKey());