From 054defe54c8de38260f2ce5135da751c7f2277a6 Mon Sep 17 00:00:00 2001 From: Andreas Schildbach Date: Sat, 8 Apr 2023 16:25:40 +0200 Subject: [PATCH] Transaction: make more use of `getOutput(index)` --- .../java/org/bitcoinj/core/TransactionOutPoint.java | 2 +- core/src/main/java/org/bitcoinj/wallet/Wallet.java | 4 ++-- .../org/bitcoinj/wallet/WalletProtobufSerializer.java | 2 +- .../test/java/org/bitcoinj/core/BlockChainTest.java | 2 +- .../java/org/bitcoinj/core/FullBlockTestGenerator.java | 10 +++++----- .../org/bitcoinj/wallet/DefaultCoinSelectorTest.java | 2 +- core/src/test/java/org/bitcoinj/wallet/WalletTest.java | 10 +++++----- 7 files changed, 16 insertions(+), 16 deletions(-) diff --git a/core/src/main/java/org/bitcoinj/core/TransactionOutPoint.java b/core/src/main/java/org/bitcoinj/core/TransactionOutPoint.java index 855f252f1..092a9f644 100644 --- a/core/src/main/java/org/bitcoinj/core/TransactionOutPoint.java +++ b/core/src/main/java/org/bitcoinj/core/TransactionOutPoint.java @@ -137,7 +137,7 @@ public class TransactionOutPoint { @Nullable public TransactionOutput getConnectedOutput() { if (fromTx != null) { - return fromTx.getOutputs().get((int) index); + return fromTx.getOutput(index); } else if (connectedOutput != null) { return connectedOutput; } diff --git a/core/src/main/java/org/bitcoinj/wallet/Wallet.java b/core/src/main/java/org/bitcoinj/wallet/Wallet.java index 749bf4109..8466f228c 100644 --- a/core/src/main/java/org/bitcoinj/wallet/Wallet.java +++ b/core/src/main/java/org/bitcoinj/wallet/Wallet.java @@ -5281,7 +5281,7 @@ public class Wallet extends BaseTaggableObject } for (int i = 0; i < req.tx.getOutputs().size(); i++) { TransactionOutput output = TransactionOutput.read( - ByteBuffer.wrap(req.tx.getOutputs().get(i).bitcoinSerialize()), tx); + ByteBuffer.wrap(req.tx.getOutput(i).bitcoinSerialize()), tx); if (req.recipientsPayFees) { // Subtract fee equally from each selected recipient output.setValue(output.getValue().subtract(fee.divide(req.tx.getOutputs().size()))); @@ -5321,7 +5321,7 @@ public class Wallet extends BaseTaggableObject // So instead we raise the change and deduct from the first recipient. Coin missingToNotBeDust = changeOutput.getMinNonDustValue().subtract(changeOutput.getValue()); changeOutput.setValue(changeOutput.getValue().add(missingToNotBeDust)); - TransactionOutput firstOutput = tx.getOutputs().get(0); + TransactionOutput firstOutput = tx.getOutput(0); firstOutput.setValue(firstOutput.getValue().subtract(missingToNotBeDust)); result.updatedOutputValues.set(0, firstOutput.getValue()); if (firstOutput.isDust()) { diff --git a/core/src/main/java/org/bitcoinj/wallet/WalletProtobufSerializer.java b/core/src/main/java/org/bitcoinj/wallet/WalletProtobufSerializer.java index 2e28300c1..4115c218f 100644 --- a/core/src/main/java/org/bitcoinj/wallet/WalletProtobufSerializer.java +++ b/core/src/main/java/org/bitcoinj/wallet/WalletProtobufSerializer.java @@ -732,7 +732,7 @@ public class WalletProtobufSerializer { throw new UnreadableWalletException("Unknown transaction pool: " + txProto.getPool()); } for (int i = 0 ; i < tx.getOutputs().size() ; i++) { - TransactionOutput output = tx.getOutputs().get(i); + TransactionOutput output = tx.getOutput(i); final Protos.TransactionOutput transactionOutput = txProto.getTransactionOutput(i); if (transactionOutput.hasSpentByTransactionHash()) { final ByteString spentByTransactionHash = transactionOutput.getSpentByTransactionHash(); diff --git a/core/src/test/java/org/bitcoinj/core/BlockChainTest.java b/core/src/test/java/org/bitcoinj/core/BlockChainTest.java index 3656cf1e3..8e08e4532 100644 --- a/core/src/test/java/org/bitcoinj/core/BlockChainTest.java +++ b/core/src/test/java/org/bitcoinj/core/BlockChainTest.java @@ -317,7 +317,7 @@ public class BlockChainTest { // Create a tx that gives us some coins, and another that spends it to someone else in the same block. Transaction t1 = FakeTxBuilder.createFakeTx(TESTNET, COIN, addr); Transaction t2 = new Transaction(); - t2.addInput(t1.getOutputs().get(0)); + t2.addInput(t1.getOutput(0)); t2.addOutput(valueOf(2, 0), somebodyElse); b1.addTransaction(t1); b1.addTransaction(t2); diff --git a/core/src/test/java/org/bitcoinj/core/FullBlockTestGenerator.java b/core/src/test/java/org/bitcoinj/core/FullBlockTestGenerator.java index f6b384848..81dfc8b33 100644 --- a/core/src/test/java/org/bitcoinj/core/FullBlockTestGenerator.java +++ b/core/src/test/java/org/bitcoinj/core/FullBlockTestGenerator.java @@ -215,14 +215,14 @@ public class FullBlockTestGenerator { blocks.add(new BlockAndValidity(chainHead, true, false, chainHead.getHash(), 1, "Initial Block")); spendableOutputs.offer(new TransactionOutPointWithValue( new TransactionOutPoint(0, chainHead.getTransactions().get(0).getTxId()), - FIFTY_COINS, chainHead.getTransactions().get(0).getOutputs().get(0).getScriptPubKey())); + FIFTY_COINS, chainHead.getTransactions().get(0).getOutput(0).getScriptPubKey())); for (int i = 1; i < params.getSpendableCoinbaseDepth(); i++) { chainHead = chainHead.createNextBlockWithCoinbase(Block.BLOCK_VERSION_GENESIS, coinbaseOutKeyPubKey, chainHeadHeight); chainHeadHeight++; blocks.add(new BlockAndValidity(chainHead, true, false, chainHead.getHash(), i+1, "Initial Block chain output generation")); spendableOutputs.offer(new TransactionOutPointWithValue( new TransactionOutPoint(0, chainHead.getTransactions().get(0).getTxId()), - FIFTY_COINS, chainHead.getTransactions().get(0).getOutputs().get(0).getScriptPubKey())); + FIFTY_COINS, chainHead.getTransactions().get(0).getOutput(0).getScriptPubKey())); } // Start by building a couple of blocks on top of the genesis block. @@ -1086,21 +1086,21 @@ public class FullBlockTestGenerator { tx2.addOutput(new TransactionOutput(tx2, SATOSHI, new byte[] {OP_TRUE})); addOnlyInputToTransaction(tx2, new TransactionOutPointWithValue( new TransactionOutPoint(0, tx1.getTxId()), - SATOSHI, tx1.getOutputs().get(0).getScriptPubKey())); + SATOSHI, tx1.getOutput(0).getScriptPubKey())); b57p2.addTransaction(tx2); b56p2txToDuplicate1 = new Transaction(); b56p2txToDuplicate1.addOutput(new TransactionOutput(b56p2txToDuplicate1, SATOSHI, new byte[]{OP_TRUE})); addOnlyInputToTransaction(b56p2txToDuplicate1, new TransactionOutPointWithValue( new TransactionOutPoint(0, tx2.getTxId()), - SATOSHI, tx2.getOutputs().get(0).getScriptPubKey())); + SATOSHI, tx2.getOutput(0).getScriptPubKey())); b57p2.addTransaction(b56p2txToDuplicate1); b56p2txToDuplicate2 = new Transaction(); b56p2txToDuplicate2.addOutput(new TransactionOutput(b56p2txToDuplicate2, SATOSHI, new byte[]{})); addOnlyInputToTransaction(b56p2txToDuplicate2, new TransactionOutPointWithValue( new TransactionOutPoint(0, b56p2txToDuplicate1.getTxId()), - SATOSHI, b56p2txToDuplicate1.getOutputs().get(0).getScriptPubKey())); + SATOSHI, b56p2txToDuplicate1.getOutput(0).getScriptPubKey())); b57p2.addTransaction(b56p2txToDuplicate2); } b57p2.solve(); diff --git a/core/src/test/java/org/bitcoinj/wallet/DefaultCoinSelectorTest.java b/core/src/test/java/org/bitcoinj/wallet/DefaultCoinSelectorTest.java index 4394c5f19..82c66542d 100644 --- a/core/src/test/java/org/bitcoinj/wallet/DefaultCoinSelectorTest.java +++ b/core/src/test/java/org/bitcoinj/wallet/DefaultCoinSelectorTest.java @@ -91,7 +91,7 @@ public class DefaultCoinSelectorTest extends TestWithWallet { // Check we selected just the oldest one. CoinSelector selector = wallet.getCoinSelector(); CoinSelection selection = selector.select(COIN, wallet.calculateAllSpendCandidates()); - assertTrue(selection.outputs().contains(t1.getOutputs().get(0))); + assertTrue(selection.outputs().contains(t1.getOutput(0))); assertEquals(COIN, selection.totalValue()); // Check we ordered them correctly (by depth). diff --git a/core/src/test/java/org/bitcoinj/wallet/WalletTest.java b/core/src/test/java/org/bitcoinj/wallet/WalletTest.java index f1d29a373..0d3578a1a 100644 --- a/core/src/test/java/org/bitcoinj/wallet/WalletTest.java +++ b/core/src/test/java/org/bitcoinj/wallet/WalletTest.java @@ -482,8 +482,8 @@ public class WalletTest extends TestWithWallet { assertEquals("Wrong number of tx inputs", 1, t.getInputs().size()); assertEquals("Wrong number of tx outputs",2, t.getOutputs().size()); assertEquals(destination, t.getOutput(0).getScriptPubKey().getToAddress(BitcoinNetwork.TESTNET)); - assertEquals(wallet.currentChangeAddress(), t.getOutputs().get(1).getScriptPubKey().getToAddress(BitcoinNetwork.TESTNET)); - assertEquals(valueOf(0, 50), t.getOutputs().get(1).getValue()); + assertEquals(wallet.currentChangeAddress(), t.getOutput(1).getScriptPubKey().getToAddress(BitcoinNetwork.TESTNET)); + assertEquals(valueOf(0, 50), t.getOutput(1).getValue()); // Check the script runs and signatures verify. t.getInputs().get(0).verify(); } @@ -818,7 +818,7 @@ public class WalletTest extends TestWithWallet { Transaction inbound2 = new Transaction(); inbound2.addOutput(new TransactionOutput(inbound2, coinHalf, myAddress)); assertTrue(outbound1.getWalletOutputs(wallet).size() >= 1); - inbound2.addInput(outbound1.getOutputs().get(0)); + inbound2.addInput(outbound1.getOutput(0)); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, inbound2); assertEquals(coin1, wallet.getBalance()); } @@ -2250,13 +2250,13 @@ public class WalletTest extends TestWithWallet { ECKey key = new ECKey(); SendRequest req = SendRequest.to(key, SATOSHI.multiply(12)); assertArrayEquals(key.getPubKey(), - ScriptPattern.extractKeyFromP2PK(req.tx.getOutputs().get(0).getScriptPubKey())); + ScriptPattern.extractKeyFromP2PK(req.tx.getOutput(0).getScriptPubKey())); } @Test public void sendRequestP2PKHTest() { SendRequest req = SendRequest.to(OTHER_ADDRESS, SATOSHI.multiply(12)); - assertEquals(OTHER_ADDRESS, req.tx.getOutputs().get(0).getScriptPubKey().getToAddress(BitcoinNetwork.TESTNET)); + assertEquals(OTHER_ADDRESS, req.tx.getOutput(0).getScriptPubKey().getToAddress(BitcoinNetwork.TESTNET)); } @Test