Java Code Examples for org.bitcoinj.core.TransactionConfidence#ConfidenceType

The following examples show how to use org.bitcoinj.core.TransactionConfidence#ConfidenceType . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultCoinSelector.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
public static boolean isSelectable(Transaction tx) {
    // Only pick chain-included transactions, or transactions that are ours and pending.
    TransactionConfidence confidence = tx.getConfidence();
    TransactionConfidence.ConfidenceType type = confidence.getConfidenceType();
    return type.equals(TransactionConfidence.ConfidenceType.BUILDING) ||

            type.equals(TransactionConfidence.ConfidenceType.PENDING) &&
                    confidence.getSource().equals(TransactionConfidence.Source.SELF) &&
                    // In regtest mode we expect to have only one peer, so we won't see transactions propagate.
                    // TODO: The value 1 below dates from a time when transactions we broadcast *to* were counted, set to 0
                    (confidence.numBroadcastPeers() > 1 || tx.getParams().getId().equals(NetworkParameters.ID_REGTEST));
}
 
Example 2
Source File: MyceliumCoinSelector.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
public static boolean isSelectable(Transaction tx) {
    // Only pick chain-included transactions, or transactions that are ours and pending.
    TransactionConfidence confidence = tx.getConfidence();
    TransactionConfidence.ConfidenceType type = confidence.getConfidenceType();
    return type.equals(TransactionConfidence.ConfidenceType.BUILDING) ||
            type.equals(TransactionConfidence.ConfidenceType.PENDING);
}
 
Example 3
Source File: BisqDefaultCoinSelector.java    From bisq-core with GNU Affero General Public License v3.0 5 votes vote down vote up
protected boolean isTxSpendable(Transaction tx) {
    TransactionConfidence confidence = tx.getConfidence();
    TransactionConfidence.ConfidenceType type = confidence.getConfidenceType();
    boolean isConfirmed = type.equals(TransactionConfidence.ConfidenceType.BUILDING);
    boolean isPending = type.equals(TransactionConfidence.ConfidenceType.PENDING);
    boolean isOwnTx = confidence.getSource().equals(TransactionConfidence.Source.SELF);
    return isConfirmed || (isPending && (permitForeignPendingTx || isOwnTx));
}
 
Example 4
Source File: DefaultCoinSelector.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
public static boolean isSelectable(Transaction tx) {
    // Only pick chain-included transactions, or transactions that are ours and pending.
    TransactionConfidence confidence = tx.getConfidence();
    TransactionConfidence.ConfidenceType type = confidence.getConfidenceType();
    return type.equals(TransactionConfidence.ConfidenceType.BUILDING) ||

           type.equals(TransactionConfidence.ConfidenceType.PENDING) &&
           confidence.getSource().equals(TransactionConfidence.Source.SELF) &&
           // In regtest mode we expect to have only one peer, so we won't see transactions propagate.
           // TODO: The value 1 below dates from a time when transactions we broadcast *to* were counted, set to 0
           (confidence.numBroadcastPeers() > 1 || tx.getParams().getId().equals(NetworkParameters.ID_REGTEST));
}
 
Example 5
Source File: DefaultCoinSelector.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
public static boolean isSelectable(Transaction tx) {
    // Only pick chain-included transactions, or transactions that are ours and pending.
    TransactionConfidence confidence = tx.getConfidence();
    TransactionConfidence.ConfidenceType type = confidence.getConfidenceType();
    return type.equals(TransactionConfidence.ConfidenceType.BUILDING) ||

           type.equals(TransactionConfidence.ConfidenceType.PENDING) &&
           confidence.getSource().equals(TransactionConfidence.Source.SELF) &&
           // In regtest mode we expect to have only one peer, so we won't see transactions propagate.
           // TODO: The value 1 below dates from a time when transactions we broadcast *to* were counted, set to 0
           (confidence.numBroadcastPeers() > 1 || tx.getParams().getId().equals(NetworkParameters.ID_REGTEST));
}
 
Example 6
Source File: BisqDefaultCoinSelector.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
protected boolean isTxSpendable(Transaction tx) {
    TransactionConfidence confidence = tx.getConfidence();
    TransactionConfidence.ConfidenceType type = confidence.getConfidenceType();
    boolean isConfirmed = type.equals(TransactionConfidence.ConfidenceType.BUILDING);
    boolean isPending = type.equals(TransactionConfidence.ConfidenceType.PENDING);
    boolean isOwnTx = confidence.getSource().equals(TransactionConfidence.Source.SELF);
    return isConfirmed || (isPending && (permitForeignPendingTx || isOwnTx));
}
 
Example 7
Source File: WalletTest.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void doubleSpends() throws Exception {
    // Test the case where two semantically identical but bitwise different transactions double spend each other.
    // We call the second transaction a "mutant" of the first.
    //
    // This can (and has!) happened when a wallet is cloned between devices, and both devices decide to make the
    // same spend simultaneously - for example due a re-keying operation. It can also happen if there are malicious
    // nodes in the P2P network that are mutating transactions on the fly as occurred during Feb 2014.
    final Coin value = COIN;
    final Coin value2 = valueOf(2, 0);
    // Give us three coins and make sure we have some change.
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, value.add(value2));
    Transaction send1 = checkNotNull(wallet.createSend(OTHER_ADDRESS, value2));
    Transaction send2 = checkNotNull(wallet.createSend(OTHER_ADDRESS, value2));
    byte[] buf = send1.bitcoinSerialize();
    buf[43] = 0;  // Break the signature: bitcoinj won't check in SPV mode and this is easier than other mutations.
    send1 = UNITTEST.getDefaultSerializer().makeTransaction(buf);
    wallet.commitTx(send2);
    wallet.allowSpendingUnconfirmedTransactions();
    assertEquals(value, wallet.getBalance(Wallet.BalanceType.ESTIMATED));
    // Now spend the change. This transaction should die permanently when the mutant appears in the chain.
    Transaction send3 = checkNotNull(wallet.createSend(OTHER_ADDRESS, value));
    wallet.commitTx(send3);
    assertEquals(ZERO, wallet.getBalance());
    final LinkedList<TransactionConfidence> dead = new LinkedList<>();
    final TransactionConfidence.Listener listener = new TransactionConfidence.Listener() {
        @Override
        public void onConfidenceChanged(TransactionConfidence confidence, ChangeReason reason) {
            final TransactionConfidence.ConfidenceType type = confidence.getConfidenceType();
            if (reason == ChangeReason.TYPE && type == TransactionConfidence.ConfidenceType.DEAD)
                dead.add(confidence);
        }
    };
    send2.getConfidence().addEventListener(Threading.SAME_THREAD, listener);
    send3.getConfidence().addEventListener(Threading.SAME_THREAD, listener);
    // Double spend!
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, send1);
    // Back to having one coin.
    assertEquals(value, wallet.getBalance());
    assertEquals(send2.getHash(), dead.poll().getTransactionHash());
    assertEquals(send3.getHash(), dead.poll().getTransactionHash());
}
 
Example 8
Source File: SellerBroadcastPayoutTx.java    From bisq-core with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
protected void run() {
    try {
        runInterceptHook();
        Transaction payoutTx = trade.getPayoutTx();
        checkNotNull(payoutTx, "payoutTx must not be null");

        TransactionConfidence.ConfidenceType confidenceType = payoutTx.getConfidence().getConfidenceType();
        log.debug("payoutTx confidenceType:" + confidenceType);
        if (confidenceType.equals(TransactionConfidence.ConfidenceType.BUILDING) ||
                confidenceType.equals(TransactionConfidence.ConfidenceType.PENDING)) {
            log.debug("payoutTx was already published. confidenceType:" + confidenceType);
            trade.setState(Trade.State.SELLER_PUBLISHED_PAYOUT_TX);
            complete();
        } else {
            processModel.getTradeWalletService().broadcastTx(payoutTx,
                    new TxBroadcaster.Callback() {
                        @Override
                        public void onSuccess(Transaction transaction) {
                            if (!completed) {
                                log.debug("BroadcastTx succeeded. Transaction:" + transaction);
                                trade.setState(Trade.State.SELLER_PUBLISHED_PAYOUT_TX);
                                complete();
                            } else {
                                log.warn("We got the onSuccess callback called after the timeout has been triggered a complete().");
                            }
                        }

                        @Override
                        public void onFailure(TxBroadcastException exception) {
                            if (!completed) {
                                log.error("BroadcastTx failed. Error:" + exception.getMessage());
                                failed(exception);
                            } else {
                                log.warn("We got the onFailure callback called after the timeout has been triggered a complete().");
                            }
                        }
                    });
        }
    } catch (Throwable t) {
        failed(t);
    }
}
 
Example 9
Source File: WalletTest.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void doubleSpends() throws Exception {
    // Test the case where two semantically identical but bitwise different transactions double spend each other.
    // We call the second transaction a "mutant" of the first.
    //
    // This can (and has!) happened when a wallet is cloned between devices, and both devices decide to make the
    // same spend simultaneously - for example due a re-keying operation. It can also happen if there are malicious
    // nodes in the P2P network that are mutating transactions on the fly as occurred during Feb 2014.
    final Coin value = COIN;
    final Coin value2 = valueOf(2, 0);
    // Give us three coins and make sure we have some change.
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, value.add(value2));
    Transaction send1 = checkNotNull(wallet.createSend(OTHER_ADDRESS, value2));
    Transaction send2 = checkNotNull(wallet.createSend(OTHER_ADDRESS, value2));
    byte[] buf = send1.bitcoinSerialize();
    buf[43] = 0;  // Break the signature: bitcoinj won't check in SPV mode and this is easier than other mutations.
    send1 = PARAMS.getDefaultSerializer().makeTransaction(buf);
    wallet.commitTx(send2);
    wallet.allowSpendingUnconfirmedTransactions();
    assertEquals(value, wallet.getBalance(Wallet.BalanceType.ESTIMATED));
    // Now spend the change. This transaction should die permanently when the mutant appears in the chain.
    Transaction send3 = checkNotNull(wallet.createSend(OTHER_ADDRESS, value));
    wallet.commitTx(send3);
    assertEquals(ZERO, wallet.getBalance());
    final LinkedList<TransactionConfidence> dead = new LinkedList<>();
    final TransactionConfidence.Listener listener = new TransactionConfidence.Listener() {
        @Override
        public void onConfidenceChanged(TransactionConfidence confidence, ChangeReason reason) {
            final TransactionConfidence.ConfidenceType type = confidence.getConfidenceType();
            if (reason == ChangeReason.TYPE && type == TransactionConfidence.ConfidenceType.DEAD)
                dead.add(confidence);
        }
    };
    send2.getConfidence().addEventListener(Threading.SAME_THREAD, listener);
    send3.getConfidence().addEventListener(Threading.SAME_THREAD, listener);
    // Double spend!
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, send1);
    // Back to having one coin.
    assertEquals(value, wallet.getBalance());
    assertEquals(send2.getHash(), dead.poll().getTransactionHash());
    assertEquals(send3.getHash(), dead.poll().getTransactionHash());
}
 
Example 10
Source File: WalletTest.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void doubleSpends() throws Exception {
    // Test the case where two semantically identical but bitwise different transactions double spend each other.
    // We call the second transaction a "mutant" of the first.
    //
    // This can (and has!) happened when a wallet is cloned between devices, and both devices decide to make the
    // same spend simultaneously - for example due a re-keying operation. It can also happen if there are malicious
    // nodes in the P2P network that are mutating transactions on the fly as occurred during Feb 2014.
    final Coin value = COIN;
    final Coin value2 = valueOf(2, 0);
    // Give us three coins and make sure we have some change.
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, value.add(value2));
    Transaction send1 = checkNotNull(wallet.createSend(OTHER_ADDRESS, value2));
    Transaction send2 = checkNotNull(wallet.createSend(OTHER_ADDRESS, value2));
    byte[] buf = send1.bitcoinSerialize();
    buf[43] = 0;  // Break the signature: bitcoinj won't check in SPV mode and this is easier than other mutations.
    send1 = PARAMS.getDefaultSerializer().makeTransaction(buf);
    wallet.commitTx(send2);
    wallet.allowSpendingUnconfirmedTransactions();
    assertEquals(value, wallet.getBalance(Wallet.BalanceType.ESTIMATED));
    // Now spend the change. This transaction should die permanently when the mutant appears in the chain.
    Transaction send3 = checkNotNull(wallet.createSend(OTHER_ADDRESS, value));
    wallet.commitTx(send3);
    assertEquals(ZERO, wallet.getBalance());
    final LinkedList<TransactionConfidence> dead = new LinkedList<>();
    final TransactionConfidence.Listener listener = new TransactionConfidence.Listener() {
        @Override
        public void onConfidenceChanged(TransactionConfidence confidence, ChangeReason reason) {
            final TransactionConfidence.ConfidenceType type = confidence.getConfidenceType();
            if (reason == ChangeReason.TYPE && type == TransactionConfidence.ConfidenceType.DEAD)
                dead.add(confidence);
        }
    };
    send2.getConfidence().addEventListener(Threading.SAME_THREAD, listener);
    send3.getConfidence().addEventListener(Threading.SAME_THREAD, listener);
    // Double spend!
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, send1);
    // Back to having one coin.
    assertEquals(value, wallet.getBalance());
    assertEquals(send2.getHash(), dead.poll().getTransactionHash());
    assertEquals(send3.getHash(), dead.poll().getTransactionHash());
}
 
Example 11
Source File: BroadcastPayoutTx.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
protected void run() {
    try {
        runInterceptHook();
        Transaction payoutTx = trade.getPayoutTx();
        checkNotNull(payoutTx, "payoutTx must not be null");

        TransactionConfidence.ConfidenceType confidenceType = payoutTx.getConfidence().getConfidenceType();
        log.debug("payoutTx confidenceType:" + confidenceType);
        if (confidenceType.equals(TransactionConfidence.ConfidenceType.BUILDING) ||
                confidenceType.equals(TransactionConfidence.ConfidenceType.PENDING)) {
            log.debug("payoutTx was already published. confidenceType:" + confidenceType);
            setState();
            complete();
        } else {
            processModel.getTradeWalletService().broadcastTx(payoutTx,
                    new TxBroadcaster.Callback() {
                        @Override
                        public void onSuccess(Transaction transaction) {
                            if (!completed) {
                                log.debug("BroadcastTx succeeded. Transaction:" + transaction);
                                setState();
                                complete();
                            } else {
                                log.warn("We got the onSuccess callback called after the timeout has been triggered a complete().");
                            }
                        }

                        @Override
                        public void onFailure(TxBroadcastException exception) {
                            if (!completed) {
                                log.error("BroadcastTx failed. Error:" + exception.getMessage());
                                failed(exception);
                            } else {
                                log.warn("We got the onFailure callback called after the timeout has been triggered a complete().");
                            }
                        }
                    });
        }
    } catch (Throwable t) {
        failed(t);
    }
}