Search in sources :

Example 1 with TransactionBroadcast

use of org.bitcoinj.core.TransactionBroadcast 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;
}
Also used : Transaction(org.bitcoinj.core.Transaction) Wallet(org.bitcoinj.wallet.Wallet) Observer(androidx.lifecycle.Observer) TransactionBroadcast(org.bitcoinj.core.TransactionBroadcast) BlockchainService(de.schildbach.wallet.service.BlockchainService)

Aggregations

Observer (androidx.lifecycle.Observer)1 BlockchainService (de.schildbach.wallet.service.BlockchainService)1 Transaction (org.bitcoinj.core.Transaction)1 TransactionBroadcast (org.bitcoinj.core.TransactionBroadcast)1 Wallet (org.bitcoinj.wallet.Wallet)1