Search in sources :

Example 56 with Transaction

use of org.bitcoinj.core.Transaction in project catena-java by alinush.

the class TxUtils method findDoubleSpendsAgainst.

/**
 * Checks if any of the transactions in txs spend the same outputs as the transactions in candidates.
 *
 * The ordering of the transactions in the collection associated with the output is the following.
 * (1) first, the tx from 'txs'
 * (2) second, any double spends from 'candidates'
 *
 * @param txs
 * @param candidates
 * @return
 */
public static Map<TransactionOutPoint, List<Transaction>> findDoubleSpendsAgainst(Iterator<Transaction> txsIt, Map<Sha256Hash, Transaction> candidates) {
    // Maps an outpoint to a list of transactions that spend it (ideally, that list should be of size one)
    ArrayListMultimap<TransactionOutPoint, Transaction> outpoints = ArrayListMultimap.create();
    while (txsIt.hasNext()) {
        Transaction tx = txsIt.next();
        // Coinbase TXs cannot double spend
        if (tx.isCoinBase())
            continue;
        // Compile a set of outpoints that are spent by tx.
        for (TransactionInput input : tx.getInputs()) {
            outpoints.put(input.getOutpoint(), tx);
        }
    }
    // Now for each candidate transaction, see if it spends any outpoints as this tx.
    for (Transaction tx : candidates.values()) {
        for (TransactionInput input : tx.getInputs()) {
            TransactionOutPoint outpoint = input.getOutpoint();
            // double spends amongst txs in candidates, which might not be desired by callers.
            if (outpoints.containsKey(outpoint)) {
                // It does, it's a double spend against the candidates, which makes it relevant.
                outpoints.put(outpoint, tx);
            }
        }
    }
    // Now clear <outp, list(tx)> pairs where the sizeof(list) == 1 (i.e., no double spend)
    Map<TransactionOutPoint, List<Transaction>> doubleSpends = Multimaps.asMap(outpoints);
    Iterator<List<Transaction>> it = doubleSpends.values().iterator();
    while (it.hasNext()) {
        int numSpends = it.next().size();
        if (numSpends < 2) {
            it.remove();
        }
    }
    return doubleSpends;
}
Also used : Transaction(org.bitcoinj.core.Transaction) List(java.util.List) TransactionOutPoint(org.bitcoinj.core.TransactionOutPoint) TransactionInput(org.bitcoinj.core.TransactionInput) TransactionOutPoint(org.bitcoinj.core.TransactionOutPoint)

Example 57 with Transaction

use of org.bitcoinj.core.Transaction in project catena-java by alinush.

the class TxUtils method findDoubleSpendsAmongst.

public static Map<TransactionOutPoint, List<Transaction>> findDoubleSpendsAmongst(Collection<Transaction> candidates) {
    ArrayListMultimap<TransactionOutPoint, Transaction> map = ArrayListMultimap.create();
    // "spends" <o, tx> pair to the map.
    for (Transaction tx : candidates) {
        for (TransactionInput input : tx.getInputs()) {
            TransactionOutPoint outpoint = input.getOutpoint();
            map.put(outpoint, tx);
        }
    }
    // Now clear <o, list(tx)> pairs where the sizeof(list) == 1 (i.e., no double spend)
    Map<TransactionOutPoint, List<Transaction>> doubleSpends = Multimaps.asMap(map);
    Iterator<List<Transaction>> it = doubleSpends.values().iterator();
    while (it.hasNext()) {
        int numSpends = it.next().size();
        if (numSpends < 2) {
            it.remove();
        }
    }
    return doubleSpends;
}
Also used : Transaction(org.bitcoinj.core.Transaction) List(java.util.List) TransactionOutPoint(org.bitcoinj.core.TransactionOutPoint) TransactionInput(org.bitcoinj.core.TransactionInput) TransactionOutPoint(org.bitcoinj.core.TransactionOutPoint)

Example 58 with Transaction

use of org.bitcoinj.core.Transaction in project catena-java by alinush.

the class CatenaServer method appendStatement.

/**
 * Appends a statement to the Catena chain. This function first commits
 * the statement on disk, then schedules it for publication on the
 * blockchain.
 *
 * @param statement
 * @throws InsufficientMoneyException
 *
 * @return
 */
public Transaction appendStatement(byte[] statement) throws InsufficientMoneyException {
    Transaction tx = wallet.appendStatement(statement);
    // Broadcast Catena transaction
    // 
    // WARNING: Sending the TX this way by calling sendCatenaTxOfflline
    // (it used to be Wallet::sendCoinsOffline but we subclassed Wallet) and then
    // peerGroup().broadcastTransaction() might not work in future version of
    // bitcoinj
    final SendResult result = new SendResult();
    result.tx = tx;
    result.broadcast = peerGroup().broadcastTransaction(tx);
    result.broadcastComplete = result.broadcast.future();
    log.trace("Catena server TX (" + tx.getHash() + ") for '" + statement + "' after sending: " + tx);
    return tx;
}
Also used : Transaction(org.bitcoinj.core.Transaction) SendResult(org.bitcoinj.wallet.Wallet.SendResult)

Example 59 with Transaction

use of org.bitcoinj.core.Transaction in project catena-java by alinush.

the class CatenaApp method listStatements.

protected static void listStatements(boolean isFwd, int numPrint) {
    int numStmts = wallet.getNumStatements();
    Iterator<CatenaStatement> it = wallet.statementIterator(isFwd);
    int c = isFwd ? 1 : numStmts;
    for (int i = 0; i < numPrint; i++) {
        if (it.hasNext()) {
            CatenaStatement s = it.next();
            Transaction prevTx = CatenaUtils.getPrevCatenaTx(wallet, s.getTxHash());
            System.out.printf("Statement #%d: %s (tx %s, prev %s)\n", c, s.getAsString(), s.getTxHash(), prevTx.getHash());
            c = isFwd ? c + 1 : c - 1;
        }
    }
}
Also used : Transaction(org.bitcoinj.core.Transaction)

Example 60 with Transaction

use of org.bitcoinj.core.Transaction in project catena-java by alinush.

the class CatenaUtils method getNextCatenaTx.

public static Transaction getNextCatenaTx(Wallet wallet, Sha256Hash currHash) {
    checkNotNull(currHash);
    Transaction currTxn = wallet.getTransaction(currHash);
    checkNotNull(currTxn);
    return getNextCatenaTx(wallet, currTxn);
}
Also used : Transaction(org.bitcoinj.core.Transaction)

Aggregations

Transaction (org.bitcoinj.core.Transaction)214 Coin (org.bitcoinj.core.Coin)71 TransactionInput (org.bitcoinj.core.TransactionInput)48 TransactionOutput (org.bitcoinj.core.TransactionOutput)42 TransactionOutPoint (org.bitcoinj.core.TransactionOutPoint)38 Address (org.bitcoinj.core.Address)35 ECKey (org.bitcoinj.core.ECKey)32 Script (org.bitcoinj.script.Script)32 ArrayList (java.util.ArrayList)31 HashMap (java.util.HashMap)29 SendRequest (org.bitcoinj.wallet.SendRequest)25 Wallet (org.bitcoinj.wallet.Wallet)25 IOException (java.io.IOException)24 List (java.util.List)24 InsufficientMoneyException (org.bitcoinj.core.InsufficientMoneyException)20 MyTransactionOutPoint (com.samourai.wallet.send.MyTransactionOutPoint)19 AddressFormatException (org.bitcoinj.core.AddressFormatException)19 Sha256Hash (org.bitcoinj.core.Sha256Hash)19 UTXO (com.samourai.wallet.send.UTXO)17 Nullable (javax.annotation.Nullable)17