use of de.schildbach.wallet.service.BlockchainService in project bitcoin-wallet by bitcoin-wallet.
the class AbstractWalletActivityViewModel method broadcastTransaction.
public ListenableFuture<Transaction> broadcastTransaction(final Transaction tx) throws VerificationException {
final SettableFuture<Transaction> future = SettableFuture.create();
wallet.observeForever(new Observer<Wallet>() {
@Override
public void onChanged(final Wallet wallet) {
blockchainService.observeForever(new Observer<BlockchainService>() {
@Override
public void onChanged(final BlockchainService blockchainService) {
if (wallet.isTransactionRelevant(tx)) {
wallet.receivePending(tx, null);
final TransactionBroadcast broadcast = blockchainService.broadcastTransaction(tx);
if (broadcast != null) {
broadcast.future().addListener(() -> {
log.info("broadcasting transaction {} complete, dropping all peers", tx.getTxId());
blockchainService.dropAllPeers();
}, Threading.SAME_THREAD);
future.setFuture(broadcast.future());
} else {
log.info("impediments; will send {} later", tx.getTxId());
future.cancel(false);
}
} else {
log.info("tx {} irrelevant", tx.getTxId());
future.cancel(false);
}
AbstractWalletActivityViewModel.this.blockchainService.removeObserver(this);
}
});
AbstractWalletActivityViewModel.this.wallet.removeObserver(this);
}
});
return future;
}
Aggregations