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;
}
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;
}
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;
}
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;
}
}
}
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);
}
Aggregations