Transaction: make more use of getOutput(index)

This commit is contained in:
Andreas Schildbach 2023-04-08 16:25:40 +02:00
parent 3e67ef679c
commit 054defe54c
7 changed files with 16 additions and 16 deletions

View File

@ -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;
}

View File

@ -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()) {

View File

@ -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();

View File

@ -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);

View File

@ -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();

View File

@ -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).

View File

@ -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