3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-16 04:05:50 +00:00

WalletTest: Use sendMoneyToWallet() when faking blocks.

This commit is contained in:
Andreas Schildbach 2016-03-24 23:15:09 +01:00
parent f92d11e54a
commit c02c5ff249
6 changed files with 170 additions and 196 deletions

View File

@ -26,7 +26,7 @@ public class TransactionOutputTest extends TestWithWallet {
@Test @Test
public void testMultiSigOutputToString() throws Exception { public void testMultiSigOutputToString() throws Exception {
sendMoneyToWallet(Coin.COIN, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, Coin.COIN);
ECKey myKey = new ECKey(); ECKey myKey = new ECKey();
this.wallet.importKey(myKey); this.wallet.importKey(myKey);

View File

@ -223,9 +223,9 @@ public class WalletTest extends TestWithWallet {
// At this point we have one pending and one spent // At this point we have one pending and one spent
Coin v1 = valueOf(0, 10); Coin v1 = valueOf(0, 10);
Transaction t = sendMoneyToWallet(wallet, v1, myAddress, null); Transaction t = sendMoneyToWallet(null, v1, myAddress);
Threading.waitForUserCode(); Threading.waitForUserCode();
sendMoneyToWallet(wallet, t, null); sendMoneyToWallet(null, t);
assertEquals("Wrong number of PENDING", 2, wallet.getPoolSize(Pool.PENDING)); assertEquals("Wrong number of PENDING", 2, wallet.getPoolSize(Pool.PENDING));
assertEquals("Wrong number of UNSPENT", 0, wallet.getPoolSize(Pool.UNSPENT)); assertEquals("Wrong number of UNSPENT", 0, wallet.getPoolSize(Pool.UNSPENT));
assertEquals("Wrong number of ALL", 3, wallet.getTransactions(true).size()); assertEquals("Wrong number of ALL", 3, wallet.getTransactions(true).size());
@ -372,7 +372,7 @@ public class WalletTest extends TestWithWallet {
assertFalse(availFuture.isDone()); assertFalse(availFuture.isDone());
assertFalse(estimatedFuture.isDone()); assertFalse(estimatedFuture.isDone());
// Send some pending coins to the wallet. // Send some pending coins to the wallet.
Transaction t1 = sendMoneyToWallet(wallet, amount, toAddress, null); Transaction t1 = sendMoneyToWallet(wallet, null, amount, toAddress);
Threading.waitForUserCode(); Threading.waitForUserCode();
final ListenableFuture<TransactionConfidence> depthFuture = t1.getConfidence().getDepthFuture(1); final ListenableFuture<TransactionConfidence> depthFuture = t1.getConfidence().getDepthFuture(1);
assertFalse(depthFuture.isDone()); assertFalse(depthFuture.isDone());
@ -384,7 +384,7 @@ public class WalletTest extends TestWithWallet {
assertEquals(1, wallet.getPoolSize(Pool.PENDING)); assertEquals(1, wallet.getPoolSize(Pool.PENDING));
assertEquals(0, wallet.getPoolSize(Pool.UNSPENT)); assertEquals(0, wallet.getPoolSize(Pool.UNSPENT));
// Confirm the coins. // Confirm the coins.
sendMoneyToWallet(wallet, t1, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(wallet, AbstractBlockChain.NewBlockType.BEST_CHAIN, t1);
assertEquals("Incorrect confirmed tx balance", amount, wallet.getBalance()); assertEquals("Incorrect confirmed tx balance", amount, wallet.getBalance());
assertEquals("Incorrect confirmed tx PENDING pool size", 0, wallet.getPoolSize(Pool.PENDING)); assertEquals("Incorrect confirmed tx PENDING pool size", 0, wallet.getPoolSize(Pool.PENDING));
assertEquals("Incorrect confirmed tx UNSPENT pool size", 1, wallet.getPoolSize(Pool.UNSPENT)); assertEquals("Incorrect confirmed tx UNSPENT pool size", 1, wallet.getPoolSize(Pool.UNSPENT));
@ -441,10 +441,7 @@ public class WalletTest extends TestWithWallet {
wallet.commitTx(t3); wallet.commitTx(t3);
assertTrue(wallet.isConsistent()); assertTrue(wallet.isConsistent());
// t2 and t3 gets confirmed in the same block. // t2 and t3 gets confirmed in the same block.
BlockPair bp = createFakeBlock(blockStore, Block.BLOCK_HEIGHT_GENESIS, t2, t3); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, t2, t3);
wallet.receiveFromBlock(t2, bp.storedBlock, AbstractBlockChain.NewBlockType.BEST_CHAIN, 0);
wallet.receiveFromBlock(t3, bp.storedBlock, AbstractBlockChain.NewBlockType.BEST_CHAIN, 1);
wallet.notifyNewBestBlock(bp.storedBlock);
assertTrue(wallet.isConsistent()); assertTrue(wallet.isConsistent());
return wallet; return wallet;
} }
@ -455,7 +452,7 @@ public class WalletTest extends TestWithWallet {
public void customTransactionSpending() throws Exception { public void customTransactionSpending() throws Exception {
// We'll set up a wallet that receives a coin, then sends a coin of lesser value and keeps the change. // We'll set up a wallet that receives a coin, then sends a coin of lesser value and keeps the change.
Coin v1 = valueOf(3, 0); Coin v1 = valueOf(3, 0);
sendMoneyToWallet(v1, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, v1);
assertEquals(v1, wallet.getBalance()); assertEquals(v1, wallet.getBalance());
assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT)); assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
assertEquals(1, wallet.getTransactions(true).size()); assertEquals(1, wallet.getTransactions(true).size());
@ -488,13 +485,13 @@ public class WalletTest extends TestWithWallet {
// The wallet receives a coin on the main chain, then on a side chain. Balance is equal to both added together // The wallet receives a coin on the main chain, then on a side chain. Balance is equal to both added together
// as we assume the side chain tx is pending and will be included shortly. // as we assume the side chain tx is pending and will be included shortly.
Coin v1 = COIN; Coin v1 = COIN;
sendMoneyToWallet(v1, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, v1);
assertEquals(v1, wallet.getBalance()); assertEquals(v1, wallet.getBalance());
assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT)); assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
assertEquals(1, wallet.getTransactions(true).size()); assertEquals(1, wallet.getTransactions(true).size());
Coin v2 = valueOf(0, 50); Coin v2 = valueOf(0, 50);
sendMoneyToWallet(v2, AbstractBlockChain.NewBlockType.SIDE_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.SIDE_CHAIN, v2);
assertEquals(2, wallet.getTransactions(true).size()); assertEquals(2, wallet.getTransactions(true).size());
assertEquals(v1, wallet.getBalance()); assertEquals(v1, wallet.getBalance());
assertEquals(v1.add(v2), wallet.getBalance(Wallet.BalanceType.ESTIMATED)); assertEquals(v1.add(v2), wallet.getBalance(Wallet.BalanceType.ESTIMATED));
@ -507,9 +504,9 @@ public class WalletTest extends TestWithWallet {
Coin v2 = valueOf(0, 50); Coin v2 = valueOf(0, 50);
Coin expected = valueOf(5, 50); Coin expected = valueOf(5, 50);
assertEquals(0, wallet.getTransactions(true).size()); assertEquals(0, wallet.getTransactions(true).size());
sendMoneyToWallet(v1, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, v1);
assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT)); assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
sendMoneyToWallet(v2, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, v2);
assertEquals(2, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT)); assertEquals(2, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
assertEquals(expected, wallet.getBalance()); assertEquals(expected, wallet.getBalance());
@ -526,8 +523,7 @@ public class WalletTest extends TestWithWallet {
wallet.getBalance(Wallet.BalanceType.ESTIMATED))); wallet.getBalance(Wallet.BalanceType.ESTIMATED)));
// Now confirm the transaction by including it into a block. // Now confirm the transaction by including it into a block.
StoredBlock b3 = createFakeBlock(blockStore, Block.BLOCK_HEIGHT_GENESIS, spend).storedBlock; sendMoneyToWallet(BlockChain.NewBlockType.BEST_CHAIN, spend);
wallet.receiveFromBlock(spend, b3, BlockChain.NewBlockType.BEST_CHAIN, 0);
// Change is confirmed. We started with 5.50 so we should have 4.50 left. // Change is confirmed. We started with 5.50 so we should have 4.50 left.
Coin v4 = valueOf(4, 50); Coin v4 = valueOf(4, 50);
@ -572,7 +568,7 @@ public class WalletTest extends TestWithWallet {
// Receive some money. // Receive some money.
Coin oneCoin = COIN; Coin oneCoin = COIN;
Transaction tx1 = sendMoneyToWallet(oneCoin, AbstractBlockChain.NewBlockType.BEST_CHAIN); Transaction tx1 = sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, oneCoin);
Threading.waitForUserCode(); Threading.waitForUserCode();
assertEquals(null, txn[1]); // onCoinsSent not called. assertEquals(null, txn[1]); // onCoinsSent not called.
assertEquals(tx1, confTxns.getFirst()); // onTransactionConfidenceChanged called assertEquals(tx1, confTxns.getFirst()); // onTransactionConfidenceChanged called
@ -588,7 +584,7 @@ public class WalletTest extends TestWithWallet {
// createSend is stateless. // createSend is stateless.
txn[0] = txn[1] = null; txn[0] = txn[1] = null;
confTxns.clear(); confTxns.clear();
sendMoneyToWallet(send1, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, send1);
Threading.waitForUserCode(); Threading.waitForUserCode();
assertEquals(Coin.valueOf(0, 90), wallet.getBalance()); assertEquals(Coin.valueOf(0, 90), wallet.getBalance());
assertEquals(null, txn[0]); assertEquals(null, txn[0]);
@ -600,10 +596,10 @@ public class WalletTest extends TestWithWallet {
Transaction send2 = wallet.createSend(OTHER_ADDRESS, valueOf(0, 10)); Transaction send2 = wallet.createSend(OTHER_ADDRESS, valueOf(0, 10));
// What we'd really like to do is prove Bitcoin Core would accept it .... no such luck unfortunately. // What we'd really like to do is prove Bitcoin Core would accept it .... no such luck unfortunately.
wallet.commitTx(send2); wallet.commitTx(send2);
sendMoneyToWallet(send2, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, send2);
assertEquals(Coin.valueOf(0, 80), wallet.getBalance()); assertEquals(Coin.valueOf(0, 80), wallet.getBalance());
Threading.waitForUserCode(); Threading.waitForUserCode();
BlockPair b4 = createFakeBlock(blockStore, Block.BLOCK_HEIGHT_GENESIS); FakeTxBuilder.BlockPair b4 = createFakeBlock(blockStore, Block.BLOCK_HEIGHT_GENESIS);
confTxns.clear(); confTxns.clear();
wallet.notifyNewBestBlock(b4.storedBlock); wallet.notifyNewBestBlock(b4.storedBlock);
Threading.waitForUserCode(); Threading.waitForUserCode();
@ -613,7 +609,7 @@ public class WalletTest extends TestWithWallet {
@Test @Test
public void balances() throws Exception { public void balances() throws Exception {
Coin nanos = COIN; Coin nanos = COIN;
Transaction tx1 = sendMoneyToWallet(nanos, AbstractBlockChain.NewBlockType.BEST_CHAIN); Transaction tx1 = sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, nanos);
assertEquals(nanos, tx1.getValueSentToMe(wallet)); assertEquals(nanos, tx1.getValueSentToMe(wallet));
assertTrue(tx1.getWalletOutputs(wallet).size() >= 1); assertTrue(tx1.getWalletOutputs(wallet).size() >= 1);
// Send 0.10 to somebody else. // Send 0.10 to somebody else.
@ -712,7 +708,7 @@ public class WalletTest extends TestWithWallet {
// Note that tx is no longer valid: it spends more than it imports. However checking transactions balance // Note that tx is no longer valid: it spends more than it imports. However checking transactions balance
// correctly isn't possible in SPV mode because value is a property of outputs not inputs. Without all // correctly isn't possible in SPV mode because value is a property of outputs not inputs. Without all
// transactions you can't check they add up. // transactions you can't check they add up.
sendMoneyToWallet(tx, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, tx);
// Now the other guy creates a transaction which spends that change. // Now the other guy creates a transaction which spends that change.
Transaction tx2 = new Transaction(PARAMS); Transaction tx2 = new Transaction(PARAMS);
tx2.addInput(output); tx2.addInput(output);
@ -726,7 +722,7 @@ public class WalletTest extends TestWithWallet {
// This test covers bug 64 (False double spends). Check that if we create a spend and it's immediately sent // This test covers bug 64 (False double spends). Check that if we create a spend and it's immediately sent
// back to us, this isn't considered as a double spend. // back to us, this isn't considered as a double spend.
Coin coin1 = COIN; Coin coin1 = COIN;
sendMoneyToWallet(coin1, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, coin1);
// Send half to some other guy. Sending only half then waiting for a confirm is important to ensure the tx is // Send half to some other guy. Sending only half then waiting for a confirm is important to ensure the tx is
// in the unspent pool, not pending or spent. // in the unspent pool, not pending or spent.
Coin coinHalf = valueOf(0, 50); Coin coinHalf = valueOf(0, 50);
@ -734,14 +730,14 @@ public class WalletTest extends TestWithWallet {
assertEquals(1, wallet.getTransactions(true).size()); assertEquals(1, wallet.getTransactions(true).size());
Transaction outbound1 = wallet.createSend(OTHER_ADDRESS, coinHalf); Transaction outbound1 = wallet.createSend(OTHER_ADDRESS, coinHalf);
wallet.commitTx(outbound1); wallet.commitTx(outbound1);
sendMoneyToWallet(outbound1, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, outbound1);
assertTrue(outbound1.getWalletOutputs(wallet).size() <= 1); //the change address at most assertTrue(outbound1.getWalletOutputs(wallet).size() <= 1); //the change address at most
// That other guy gives us the coins right back. // That other guy gives us the coins right back.
Transaction inbound2 = new Transaction(PARAMS); Transaction inbound2 = new Transaction(PARAMS);
inbound2.addOutput(new TransactionOutput(PARAMS, inbound2, coinHalf, myAddress)); inbound2.addOutput(new TransactionOutput(PARAMS, inbound2, coinHalf, myAddress));
assertTrue(outbound1.getWalletOutputs(wallet).size() >= 1); assertTrue(outbound1.getWalletOutputs(wallet).size() >= 1);
inbound2.addInput(outbound1.getOutputs().get(0)); inbound2.addInput(outbound1.getOutputs().get(0));
sendMoneyToWallet(inbound2, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, inbound2);
assertEquals(coin1, wallet.getBalance()); assertEquals(coin1, wallet.getBalance());
} }
@ -752,8 +748,8 @@ public class WalletTest extends TestWithWallet {
// frees up the other outputs and makes them spendable again. // frees up the other outputs and makes them spendable again.
// Receive 1 coin and then 2 coins in separate transactions. // Receive 1 coin and then 2 coins in separate transactions.
sendMoneyToWallet(COIN, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN);
sendMoneyToWallet(valueOf(2, 0), AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, valueOf(2, 0));
// Create a send to a merchant of all our coins. // Create a send to a merchant of all our coins.
Transaction send1 = wallet.createSend(OTHER_ADDRESS, valueOf(2, 90)); Transaction send1 = wallet.createSend(OTHER_ADDRESS, valueOf(2, 90));
// Create a double spend of just the first one. // Create a double spend of just the first one.
@ -764,7 +760,7 @@ public class WalletTest extends TestWithWallet {
wallet.commitTx(send1); wallet.commitTx(send1);
assertEquals(ZERO, wallet.getBalance()); // change of 10 cents is not yet mined so not included in the balance. assertEquals(ZERO, wallet.getBalance()); // change of 10 cents is not yet mined so not included in the balance.
// Receive a block that overrides the send1 using send2. // Receive a block that overrides the send1 using send2.
sendMoneyToWallet(send2, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, send2);
// send1 got rolled back and replaced with a smaller send that only used one of our received coins, thus ... // send1 got rolled back and replaced with a smaller send that only used one of our received coins, thus ...
assertEquals(valueOf(2, 0), wallet.getBalance()); assertEquals(valueOf(2, 0), wallet.getBalance());
assertTrue(wallet.isConsistent()); assertTrue(wallet.isConsistent());
@ -781,7 +777,7 @@ public class WalletTest extends TestWithWallet {
final Coin value = COIN; final Coin value = COIN;
final Coin value2 = valueOf(2, 0); final Coin value2 = valueOf(2, 0);
// Give us three coins and make sure we have some change. // Give us three coins and make sure we have some change.
sendMoneyToWallet(value.add(value2), AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, value.add(value2));
Transaction send1 = checkNotNull(wallet.createSend(OTHER_ADDRESS, value2)); Transaction send1 = checkNotNull(wallet.createSend(OTHER_ADDRESS, value2));
Transaction send2 = checkNotNull(wallet.createSend(OTHER_ADDRESS, value2)); Transaction send2 = checkNotNull(wallet.createSend(OTHER_ADDRESS, value2));
byte[] buf = send1.bitcoinSerialize(); byte[] buf = send1.bitcoinSerialize();
@ -806,7 +802,7 @@ public class WalletTest extends TestWithWallet {
send2.getConfidence().addEventListener(Threading.SAME_THREAD, listener); send2.getConfidence().addEventListener(Threading.SAME_THREAD, listener);
send3.getConfidence().addEventListener(Threading.SAME_THREAD, listener); send3.getConfidence().addEventListener(Threading.SAME_THREAD, listener);
// Double spend! // Double spend!
sendMoneyToWallet(send1, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, send1);
// Back to having one coin. // Back to having one coin.
assertEquals(value, wallet.getBalance()); assertEquals(value, wallet.getBalance());
assertEquals(send2.getHash(), dead.poll().getTransactionHash()); assertEquals(send2.getHash(), dead.poll().getTransactionHash());
@ -846,7 +842,7 @@ public class WalletTest extends TestWithWallet {
// Receive 1 BTC. // Receive 1 BTC.
Coin nanos = COIN; Coin nanos = COIN;
sendMoneyToWallet(nanos, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, nanos);
Transaction received = wallet.getTransactions(false).iterator().next(); Transaction received = wallet.getTransactions(false).iterator().next();
// Create a send to a merchant. // Create a send to a merchant.
Transaction send1 = wallet.createSend(OTHER_ADDRESS, valueOf(0, 50)); Transaction send1 = wallet.createSend(OTHER_ADDRESS, valueOf(0, 50));
@ -858,7 +854,7 @@ public class WalletTest extends TestWithWallet {
wallet.commitTx(send1); wallet.commitTx(send1);
assertEquals(send1, received.getOutput(0).getSpentBy().getParentTransaction()); assertEquals(send1, received.getOutput(0).getSpentBy().getParentTransaction());
// Receive a block that overrides it. // Receive a block that overrides it.
sendMoneyToWallet(send2, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, send2);
Threading.waitForUserCode(); Threading.waitForUserCode();
assertEquals(send1, eventDead[0]); assertEquals(send1, eventDead[0]);
assertEquals(send2, eventReplacement[0]); assertEquals(send2, eventReplacement[0]);
@ -871,7 +867,7 @@ public class WalletTest extends TestWithWallet {
wallet.receivePending(doubleSpends.t1, null); wallet.receivePending(doubleSpends.t1, null);
assertEquals(TransactionConfidence.ConfidenceType.PENDING, assertEquals(TransactionConfidence.ConfidenceType.PENDING,
doubleSpends.t1.getConfidence().getConfidenceType()); doubleSpends.t1.getConfidence().getConfidenceType());
sendMoneyToWallet(doubleSpends.t2, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, doubleSpends.t2);
Threading.waitForUserCode(); Threading.waitForUserCode();
assertEquals(TransactionConfidence.ConfidenceType.DEAD, assertEquals(TransactionConfidence.ConfidenceType.DEAD,
doubleSpends.t1.getConfidence().getConfidenceType()); doubleSpends.t1.getConfidence().getConfidenceType());
@ -901,7 +897,7 @@ public class WalletTest extends TestWithWallet {
try { try {
wallet.allowSpendingUnconfirmedTransactions(); wallet.allowSpendingUnconfirmedTransactions();
Transaction txARoot = sendMoneyToWallet(valueOf(10, 0), AbstractBlockChain.NewBlockType.BEST_CHAIN); Transaction txARoot = sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, valueOf(10, 0));
SendRequest a1Req = SendRequest.to(OTHER_ADDRESS, valueOf(1, 0)); SendRequest a1Req = SendRequest.to(OTHER_ADDRESS, valueOf(1, 0));
a1Req.tx.addInput(txARoot.getOutput(0)); a1Req.tx.addInput(txARoot.getOutput(0));
a1Req.shuffleOutputs = false; a1Req.shuffleOutputs = false;
@ -921,7 +917,7 @@ public class WalletTest extends TestWithWallet {
wallet.commitTx(txA2); wallet.commitTx(txA2);
wallet.commitTx(txA3); wallet.commitTx(txA3);
Transaction txBRoot = sendMoneyToWallet(valueOf(100, 0), AbstractBlockChain.NewBlockType.BEST_CHAIN); Transaction txBRoot = sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, valueOf(100, 0));
SendRequest b1Req = SendRequest.to(OTHER_ADDRESS, valueOf(11, 0)); SendRequest b1Req = SendRequest.to(OTHER_ADDRESS, valueOf(11, 0));
b1Req.tx.addInput(txBRoot.getOutput(0)); b1Req.tx.addInput(txBRoot.getOutput(0));
b1Req.shuffleOutputs = false; b1Req.shuffleOutputs = false;
@ -1117,13 +1113,11 @@ public class WalletTest extends TestWithWallet {
try { try {
wallet.allowSpendingUnconfirmedTransactions(); wallet.allowSpendingUnconfirmedTransactions();
sendMoneyToWallet(valueOf(2, 0), AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, valueOf(2, 0));
Transaction send1 = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(1, 0))); Transaction send1 = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(1, 0)));
Transaction send2 = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(1, 20))); Transaction send2 = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(1, 20)));
FakeTxBuilder.BlockPair bp1 = createFakeBlock(blockStore, 1, send1); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, send1);
wallet.receiveFromBlock(send1, bp1.storedBlock, AbstractBlockChain.NewBlockType.BEST_CHAIN, 0);
wallet.notifyNewBestBlock(bp1.storedBlock);
assertUnspent(send1); assertUnspent(send1);
wallet.receivePending(send2, null); wallet.receivePending(send2, null);
@ -1141,16 +1135,14 @@ public class WalletTest extends TestWithWallet {
try { try {
wallet.allowSpendingUnconfirmedTransactions(); wallet.allowSpendingUnconfirmedTransactions();
sendMoneyToWallet(valueOf(2, 0), AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, valueOf(2, 0));
Transaction send1 = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(1, 0))); Transaction send1 = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(1, 0)));
Transaction send2 = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(1, 20))); Transaction send2 = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(1, 20)));
wallet.commitTx(send1); wallet.commitTx(send1);
assertPending(send1); assertPending(send1);
Transaction send1b = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(0, 50))); Transaction send1b = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(0, 50)));
FakeTxBuilder.BlockPair bp1 = createFakeBlock(blockStore, 1, send2); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, send2);
wallet.receiveFromBlock(send2, bp1.storedBlock, AbstractBlockChain.NewBlockType.BEST_CHAIN, 0);
wallet.notifyNewBestBlock(bp1.storedBlock);
assertDead(send1); assertDead(send1);
assertUnspent(send2); assertUnspent(send2);
@ -1194,7 +1186,7 @@ public class WalletTest extends TestWithWallet {
CoinSelector originalCoinSelector = wallet.getCoinSelector(); CoinSelector originalCoinSelector = wallet.getCoinSelector();
try { try {
wallet.allowSpendingUnconfirmedTransactions(); wallet.allowSpendingUnconfirmedTransactions();
sendMoneyToWallet(valueOf(2, 0), AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, valueOf(2, 0));
Transaction send1 = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(1, 0))); Transaction send1 = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(1, 0)));
Transaction send2 = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(1, 20))); Transaction send2 = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(1, 20)));
wallet.commitTx(send1); wallet.commitTx(send1);
@ -1220,7 +1212,7 @@ public class WalletTest extends TestWithWallet {
CoinSelector originalCoinSelector = wallet.getCoinSelector(); CoinSelector originalCoinSelector = wallet.getCoinSelector();
try { try {
wallet.allowSpendingUnconfirmedTransactions(); wallet.allowSpendingUnconfirmedTransactions();
Transaction send1 = sendMoneyToWallet(valueOf(2, 0), AbstractBlockChain.NewBlockType.BEST_CHAIN); Transaction send1 = sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, valueOf(2, 0));
Transaction send1a = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(1, 0))); Transaction send1a = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(1, 0)));
wallet.commitTx(send1a); wallet.commitTx(send1a);
Transaction send1b = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(0, 50))); Transaction send1b = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(0, 50)));
@ -1232,7 +1224,7 @@ public class WalletTest extends TestWithWallet {
Transaction send1e = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(0, 06))); Transaction send1e = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(0, 06)));
wallet.commitTx(send1e); wallet.commitTx(send1e);
Transaction send2 = sendMoneyToWallet(valueOf(200, 0), AbstractBlockChain.NewBlockType.BEST_CHAIN); Transaction send2 = sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, valueOf(200, 0));
SendRequest req2a = SendRequest.to(OTHER_ADDRESS, valueOf(100, 0)); SendRequest req2a = SendRequest.to(OTHER_ADDRESS, valueOf(100, 0));
req2a.tx.addInput(send2.getOutput(0)); req2a.tx.addInput(send2.getOutput(0));
@ -1330,11 +1322,11 @@ public class WalletTest extends TestWithWallet {
assertEquals(TransactionConfidence.ConfidenceType.PENDING, assertEquals(TransactionConfidence.ConfidenceType.PENDING,
notifiedTx[0].getConfidence().getConfidenceType()); notifiedTx[0].getConfidence().getConfidenceType());
// Send a block with nothing interesting. Verify we don't get a callback. // Send a block with nothing interesting. Verify we don't get a callback.
wallet.notifyNewBestBlock(createFakeBlock(blockStore, Block.BLOCK_HEIGHT_GENESIS).storedBlock); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN);
Threading.waitForUserCode(); Threading.waitForUserCode();
assertNull(reasons[0]); assertNull(reasons[0]);
final Transaction t1Copy = PARAMS.getDefaultSerializer().makeTransaction(t1.bitcoinSerialize()); final Transaction t1Copy = PARAMS.getDefaultSerializer().makeTransaction(t1.bitcoinSerialize());
sendMoneyToWallet(t1Copy, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, t1Copy);
Threading.waitForUserCode(); Threading.waitForUserCode();
assertFalse(flags[0]); assertFalse(flags[0]);
assertTrue(flags[1]); assertTrue(flags[1]);
@ -1365,7 +1357,7 @@ public class WalletTest extends TestWithWallet {
}); });
// Receive some coins. // Receive some coins.
Coin nanos = COIN; Coin nanos = COIN;
sendMoneyToWallet(nanos, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, nanos);
// Create a spend with them, but don't commit it (ie it's from somewhere else but using our keys). This TX // Create a spend with them, but don't commit it (ie it's from somewhere else but using our keys). This TX
// will have change as we don't spend our entire balance. // will have change as we don't spend our entire balance.
Coin halfNanos = valueOf(0, 50); Coin halfNanos = valueOf(0, 50);
@ -1430,7 +1422,7 @@ public class WalletTest extends TestWithWallet {
assertEquals(nanos, wallet.getBalance(Wallet.BalanceType.ESTIMATED)); assertEquals(nanos, wallet.getBalance(Wallet.BalanceType.ESTIMATED));
// Now receive a double spend on the main chain. // Now receive a double spend on the main chain.
called[0] = called[1] = null; called[0] = called[1] = null;
sendMoneyToWallet(t2, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, t2);
Threading.waitForUserCode(); Threading.waitForUserCode();
assertEquals(ZERO, wallet.getBalance()); assertEquals(ZERO, wallet.getBalance());
assertEquals(t1, called[0]); // dead assertEquals(t1, called[0]); // dead
@ -1441,9 +1433,9 @@ public class WalletTest extends TestWithWallet {
public void transactionsList() throws Exception { public void transactionsList() throws Exception {
// Check the wallet can give us an ordered list of all received transactions. // Check the wallet can give us an ordered list of all received transactions.
Utils.setMockClock(); Utils.setMockClock();
Transaction tx1 = sendMoneyToWallet(COIN, AbstractBlockChain.NewBlockType.BEST_CHAIN); Transaction tx1 = sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN);
Utils.rollMockClock(60 * 10); Utils.rollMockClock(60 * 10);
Transaction tx2 = sendMoneyToWallet(valueOf(0, 5), AbstractBlockChain.NewBlockType.BEST_CHAIN); Transaction tx2 = sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, valueOf(0, 5));
// Check we got them back in order. // Check we got them back in order.
List<Transaction> transactions = wallet.getTransactionsByTime(); List<Transaction> transactions = wallet.getTransactionsByTime();
assertEquals(tx2, transactions.get(0)); assertEquals(tx2, transactions.get(0));
@ -1506,7 +1498,7 @@ public class WalletTest extends TestWithWallet {
Coin coin1 = COIN; Coin coin1 = COIN;
Coin coinHalf = valueOf(0, 50); Coin coinHalf = valueOf(0, 50);
// Start by giving us 1 coin. // Start by giving us 1 coin.
sendMoneyToWallet(coin1, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, coin1);
// Send half to ourselves. We should then have a balance available to spend of zero. // Send half to ourselves. We should then have a balance available to spend of zero.
assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT)); assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
assertEquals(1, wallet.getTransactions(true).size()); assertEquals(1, wallet.getTransactions(true).size());
@ -1514,7 +1506,7 @@ public class WalletTest extends TestWithWallet {
wallet.commitTx(outbound1); wallet.commitTx(outbound1);
// We should have a zero available balance before the next block. // We should have a zero available balance before the next block.
assertEquals(ZERO, wallet.getBalance()); assertEquals(ZERO, wallet.getBalance());
sendMoneyToWallet(outbound1, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, outbound1);
// We should have a balance of 1 BTC after the block is received. // We should have a balance of 1 BTC after the block is received.
assertEquals(coin1, wallet.getBalance()); assertEquals(coin1, wallet.getBalance());
} }
@ -1561,8 +1553,7 @@ public class WalletTest extends TestWithWallet {
// TX should have been seen as relevant. // TX should have been seen as relevant.
assertEquals(value, wallet.getBalance(Wallet.BalanceType.ESTIMATED)); assertEquals(value, wallet.getBalance(Wallet.BalanceType.ESTIMATED));
assertEquals(ZERO, wallet.getBalance(Wallet.BalanceType.AVAILABLE)); assertEquals(ZERO, wallet.getBalance(Wallet.BalanceType.AVAILABLE));
Block b1 = createFakeBlock(blockStore, Block.BLOCK_HEIGHT_GENESIS, t1).block; sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, t1);
chain.add(b1);
// TX should have been seen as relevant, extracted and processed. // TX should have been seen as relevant, extracted and processed.
assertEquals(value, wallet.getBalance(Wallet.BalanceType.AVAILABLE)); assertEquals(value, wallet.getBalance(Wallet.BalanceType.AVAILABLE));
// Spend it and ensure we can spend the <key> OP_CHECKSIG output correctly. // Spend it and ensure we can spend the <key> OP_CHECKSIG output correctly.
@ -1640,9 +1631,7 @@ public class WalletTest extends TestWithWallet {
public void watchingScriptsConfirmed() throws Exception { public void watchingScriptsConfirmed() throws Exception {
Address watchedAddress = new ECKey().toAddress(PARAMS); Address watchedAddress = new ECKey().toAddress(PARAMS);
wallet.addWatchedAddress(watchedAddress); wallet.addWatchedAddress(watchedAddress);
Transaction t1 = createFakeTx(PARAMS, CENT, watchedAddress); sendMoneyToWallet(BlockChain.NewBlockType.BEST_CHAIN, CENT, watchedAddress);
StoredBlock b3 = createFakeBlock(blockStore, Block.BLOCK_HEIGHT_GENESIS, t1).storedBlock;
wallet.receiveFromBlock(t1, b3, BlockChain.NewBlockType.BEST_CHAIN, 0);
assertEquals(CENT, wallet.getBalance()); assertEquals(CENT, wallet.getBalance());
// We can't spend watched balances // We can't spend watched balances
@ -1659,15 +1648,14 @@ public class WalletTest extends TestWithWallet {
Transaction t1 = createFakeTx(PARAMS, CENT, watchedAddress); Transaction t1 = createFakeTx(PARAMS, CENT, watchedAddress);
Transaction t2 = createFakeTx(PARAMS, COIN, OTHER_ADDRESS); Transaction t2 = createFakeTx(PARAMS, COIN, OTHER_ADDRESS);
StoredBlock b1 = createFakeBlock(blockStore, Block.BLOCK_HEIGHT_GENESIS, t1).storedBlock; sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, t1);
assertEquals(baseElements + 2, wallet.getBloomFilterElementCount());
Transaction st2 = new Transaction(PARAMS); Transaction st2 = new Transaction(PARAMS);
st2.addOutput(CENT, OTHER_ADDRESS); st2.addOutput(CENT, OTHER_ADDRESS);
st2.addOutput(COIN, OTHER_ADDRESS); st2.addOutput(COIN, OTHER_ADDRESS);
st2.addInput(t1.getOutput(0)); st2.addInput(t1.getOutput(0));
st2.addInput(t2.getOutput(0)); st2.addInput(t2.getOutput(0));
wallet.receiveFromBlock(t1, b1, BlockChain.NewBlockType.BEST_CHAIN, 0); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, st2);
assertEquals(baseElements + 2, wallet.getBloomFilterElementCount());
wallet.receiveFromBlock(st2, b1, BlockChain.NewBlockType.BEST_CHAIN, 0);
assertEquals(baseElements + 2, wallet.getBloomFilterElementCount()); assertEquals(baseElements + 2, wallet.getBloomFilterElementCount());
assertEquals(CENT, st2.getValueSentFromMe(wallet)); assertEquals(CENT, st2.getValueSentFromMe(wallet));
} }
@ -1677,18 +1665,15 @@ public class WalletTest extends TestWithWallet {
assertFalse(wallet.isRequiringUpdateAllBloomFilter()); assertFalse(wallet.isRequiringUpdateAllBloomFilter());
Address watchedAddress = new ECKey().toAddress(PARAMS); Address watchedAddress = new ECKey().toAddress(PARAMS);
Transaction t1 = createFakeTx(PARAMS, CENT, watchedAddress);
TransactionOutPoint outPoint = new TransactionOutPoint(PARAMS, 0, t1);
wallet.addWatchedAddress(watchedAddress); wallet.addWatchedAddress(watchedAddress);
assertTrue(wallet.isRequiringUpdateAllBloomFilter()); assertTrue(wallet.isRequiringUpdateAllBloomFilter());
Transaction t1 = createFakeTx(PARAMS, CENT, watchedAddress);
StoredBlock b1 = createFakeBlock(blockStore, Block.BLOCK_HEIGHT_GENESIS, t1).storedBlock;
TransactionOutPoint outPoint = new TransactionOutPoint(PARAMS, 0, t1);
// Note that this has a 1e-12 chance of failing this unit test due to a false positive // Note that this has a 1e-12 chance of failing this unit test due to a false positive
assertFalse(wallet.getBloomFilter(1e-12).contains(outPoint.unsafeBitcoinSerialize())); assertFalse(wallet.getBloomFilter(1e-12).contains(outPoint.unsafeBitcoinSerialize()));
wallet.receiveFromBlock(t1, b1, BlockChain.NewBlockType.BEST_CHAIN, 0); sendMoneyToWallet(BlockChain.NewBlockType.BEST_CHAIN, t1);
assertTrue(wallet.getBloomFilter(1e-12).contains(outPoint.unsafeBitcoinSerialize())); assertTrue(wallet.getBloomFilter(1e-12).contains(outPoint.unsafeBitcoinSerialize()));
} }
@ -1739,14 +1724,12 @@ public class WalletTest extends TestWithWallet {
for (Address addr : addressesForRemoval) { for (Address addr : addressesForRemoval) {
Transaction t1 = createFakeTx(PARAMS, CENT, addr); Transaction t1 = createFakeTx(PARAMS, CENT, addr);
StoredBlock b1 = createFakeBlock(blockStore, Block.BLOCK_HEIGHT_GENESIS, t1).storedBlock;
TransactionOutPoint outPoint = new TransactionOutPoint(PARAMS, 0, t1); TransactionOutPoint outPoint = new TransactionOutPoint(PARAMS, 0, t1);
// Note that this has a 1e-12 chance of failing this unit test due to a false positive // Note that this has a 1e-12 chance of failing this unit test due to a false positive
assertFalse(wallet.getBloomFilter(1e-12).contains(outPoint.unsafeBitcoinSerialize())); assertFalse(wallet.getBloomFilter(1e-12).contains(outPoint.unsafeBitcoinSerialize()));
wallet.receiveFromBlock(t1, b1, BlockChain.NewBlockType.BEST_CHAIN, 0); sendMoneyToWallet(BlockChain.NewBlockType.BEST_CHAIN, t1);
assertFalse(wallet.getBloomFilter(1e-12).contains(outPoint.unsafeBitcoinSerialize())); assertFalse(wallet.getBloomFilter(1e-12).contains(outPoint.unsafeBitcoinSerialize()));
} }
} }
@ -1759,13 +1742,11 @@ public class WalletTest extends TestWithWallet {
assertTrue(wallet.getBloomFilter(0.001).contains(address.getHash160())); assertTrue(wallet.getBloomFilter(0.001).contains(address.getHash160()));
Transaction t1 = createFakeTx(PARAMS, CENT, address); Transaction t1 = createFakeTx(PARAMS, CENT, address);
StoredBlock b1 = createFakeBlock(blockStore, Block.BLOCK_HEIGHT_GENESIS, t1).storedBlock;
TransactionOutPoint outPoint = new TransactionOutPoint(PARAMS, 0, t1); TransactionOutPoint outPoint = new TransactionOutPoint(PARAMS, 0, t1);
assertFalse(wallet.getBloomFilter(0.001).contains(outPoint.unsafeBitcoinSerialize())); assertFalse(wallet.getBloomFilter(0.001).contains(outPoint.unsafeBitcoinSerialize()));
wallet.receiveFromBlock(t1, b1, BlockChain.NewBlockType.BEST_CHAIN, 0); sendMoneyToWallet(BlockChain.NewBlockType.BEST_CHAIN, t1);
assertTrue(wallet.getBloomFilter(0.001).contains(outPoint.unsafeBitcoinSerialize())); assertTrue(wallet.getBloomFilter(0.001).contains(outPoint.unsafeBitcoinSerialize()));
} }
@ -1817,23 +1798,19 @@ public class WalletTest extends TestWithWallet {
assertEquals(f, results[1]); assertEquals(f, results[1]);
results[0] = results[1] = null; results[0] = results[1] = null;
Block b0 = createFakeBlock(blockStore, Block.BLOCK_HEIGHT_GENESIS).block; sendMoneyToWallet(BlockChain.NewBlockType.BEST_CHAIN);
chain.add(b0);
Sha256Hash hash3 = Sha256Hash.of(f); Sha256Hash hash3 = Sha256Hash.of(f);
assertEquals(hash2, hash3); // File has NOT changed yet. Just new blocks with no txns - delayed. assertEquals(hash2, hash3); // File has NOT changed yet. Just new blocks with no txns - delayed.
assertNull(results[0]); assertNull(results[0]);
assertNull(results[1]); assertNull(results[1]);
Transaction t1 = createFakeTx(PARAMS, valueOf(5, 0), key); sendMoneyToWallet(BlockChain.NewBlockType.BEST_CHAIN, valueOf(5, 0), key);
Block b1 = createFakeBlock(blockStore, Block.BLOCK_HEIGHT_GENESIS, t1).block;
chain.add(b1);
Sha256Hash hash4 = Sha256Hash.of(f); Sha256Hash hash4 = Sha256Hash.of(f);
assertFalse(hash3.equals(hash4)); // File HAS changed. assertFalse(hash3.equals(hash4)); // File HAS changed.
results[0] = results[1] = null; results[0] = results[1] = null;
// A block that contains some random tx we don't care about. // A block that contains some random tx we don't care about.
Block b2 = b1.createNextBlock(OTHER_ADDRESS); sendMoneyToWallet(BlockChain.NewBlockType.BEST_CHAIN, Coin.COIN, OTHER_ADDRESS);
chain.add(b2);
assertEquals(hash4, Sha256Hash.of(f)); // File has NOT changed. assertEquals(hash4, Sha256Hash.of(f)); // File has NOT changed.
assertNull(results[0]); assertNull(results[0]);
assertNull(results[1]); assertNull(results[1]);
@ -1851,9 +1828,7 @@ public class WalletTest extends TestWithWallet {
ECKey key2 = new ECKey(); ECKey key2 = new ECKey();
wallet.importKey(key2); wallet.importKey(key2);
assertEquals(hash5, Sha256Hash.of(f)); // File has NOT changed. assertEquals(hash5, Sha256Hash.of(f)); // File has NOT changed.
Transaction t2 = createFakeTx(PARAMS, valueOf(5, 0), key2); sendMoneyToWallet(BlockChain.NewBlockType.BEST_CHAIN, valueOf(5, 0), key2);
Block b3 = createFakeBlock(blockStore, Block.BLOCK_HEIGHT_GENESIS, t2).block;
chain.add(b3);
Thread.sleep(2000); // Wait longer than autosave delay. TODO Fix the racyness. Thread.sleep(2000); // Wait longer than autosave delay. TODO Fix the racyness.
assertEquals(hash5, Sha256Hash.of(f)); // File has still NOT changed. assertEquals(hash5, Sha256Hash.of(f)); // File has still NOT changed.
assertNull(results[0]); assertNull(results[0]);
@ -1864,7 +1839,7 @@ public class WalletTest extends TestWithWallet {
public void spendOutputFromPendingTransaction() throws Exception { public void spendOutputFromPendingTransaction() throws Exception {
// We'll set up a wallet that receives a coin, then sends a coin of lesser value and keeps the change. // We'll set up a wallet that receives a coin, then sends a coin of lesser value and keeps the change.
Coin v1 = COIN; Coin v1 = COIN;
sendMoneyToWallet(v1, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, v1);
// First create our current transaction // First create our current transaction
ECKey k2 = wallet.freshReceiveKey(); ECKey k2 = wallet.freshReceiveKey();
Coin v2 = valueOf(0, 50); Coin v2 = valueOf(0, 50);
@ -1911,9 +1886,7 @@ public class WalletTest extends TestWithWallet {
// Add a change address to ensure this tx is relevant. // Add a change address to ensure this tx is relevant.
tx2.addOutput(CENT, wallet.currentChangeAddress()); tx2.addOutput(CENT, wallet.currentChangeAddress());
wallet.receivePending(tx2, null); wallet.receivePending(tx2, null);
BlockPair bp = createFakeBlock(blockStore, Block.BLOCK_HEIGHT_GENESIS, tx1); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, tx1);
wallet.receiveFromBlock(tx1, bp.storedBlock, AbstractBlockChain.NewBlockType.BEST_CHAIN, 0);
wallet.notifyNewBestBlock(bp.storedBlock);
assertEquals(ZERO, wallet.getBalance()); assertEquals(ZERO, wallet.getBalance());
assertEquals(1, wallet.getPoolSize(Pool.SPENT)); assertEquals(1, wallet.getPoolSize(Pool.SPENT));
assertEquals(1, wallet.getPoolSize(Pool.PENDING)); assertEquals(1, wallet.getPoolSize(Pool.PENDING));
@ -2115,7 +2088,7 @@ public class WalletTest extends TestWithWallet {
encryptedWallet.importKeysAndEncrypt(ImmutableList.of(key), PASSWORD1); encryptedWallet.importKeysAndEncrypt(ImmutableList.of(key), PASSWORD1);
assertEquals(1, encryptedWallet.getImportedKeys().size()); assertEquals(1, encryptedWallet.getImportedKeys().size());
assertEquals(key.getPubKeyPoint(), encryptedWallet.getImportedKeys().get(0).getPubKeyPoint()); assertEquals(key.getPubKeyPoint(), encryptedWallet.getImportedKeys().get(0).getPubKeyPoint());
sendMoneyToWallet(encryptedWallet, Coin.COIN, key.toAddress(PARAMS), AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(encryptedWallet, AbstractBlockChain.NewBlockType.BEST_CHAIN, Coin.COIN, key.toAddress(PARAMS));
assertEquals(Coin.COIN, encryptedWallet.getBalance()); assertEquals(Coin.COIN, encryptedWallet.getBalance());
SendRequest req = Wallet.SendRequest.emptyWallet(OTHER_ADDRESS); SendRequest req = Wallet.SendRequest.emptyWallet(OTHER_ADDRESS);
req.aesKey = checkNotNull(encryptedWallet.getKeyCrypter()).deriveKey(PASSWORD1); req.aesKey = checkNotNull(encryptedWallet.getKeyCrypter()).deriveKey(PASSWORD1);
@ -2129,7 +2102,7 @@ public class WalletTest extends TestWithWallet {
final int ITERATIONS = 10; final int ITERATIONS = 10;
Transaction[] txns = new Transaction[ITERATIONS]; Transaction[] txns = new Transaction[ITERATIONS];
for (int i = 0; i < ITERATIONS; i++) { for (int i = 0; i < ITERATIONS; i++) {
txns[i] = sendMoneyToWallet(COIN, AbstractBlockChain.NewBlockType.BEST_CHAIN); txns[i] = sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN);
} }
// Check that we spend transactions in order of reception. // Check that we spend transactions in order of reception.
for (int i = 0; i < ITERATIONS; i++) { for (int i = 0; i < ITERATIONS; i++) {
@ -2143,7 +2116,7 @@ public class WalletTest extends TestWithWallet {
@Test(expected = Wallet.ExceededMaxTransactionSize.class) @Test(expected = Wallet.ExceededMaxTransactionSize.class)
public void respectMaxStandardSize() throws Exception { public void respectMaxStandardSize() throws Exception {
// Check that we won't create txns > 100kb. Average tx size is ~220 bytes so this would have to be enormous. // Check that we won't create txns > 100kb. Average tx size is ~220 bytes so this would have to be enormous.
sendMoneyToWallet(valueOf(100, 0), AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, valueOf(100, 0));
Transaction tx = new Transaction(PARAMS); Transaction tx = new Transaction(PARAMS);
byte[] bits = new byte[20]; byte[] bits = new byte[20];
new Random().nextBytes(bits); new Random().nextBytes(bits);
@ -2272,14 +2245,11 @@ public class WalletTest extends TestWithWallet {
@Test @Test
public void feeSolverAndCoinSelectionTest_dustySendRequested() throws Exception { public void feeSolverAndCoinSelectionTest_dustySendRequested() throws Exception {
// Generate a few outputs to us that are far too small to spend reasonably // Generate a few outputs to us that are far too small to spend reasonably
StoredBlock block = new StoredBlock(makeSolvedTestBlock(blockStore, OTHER_ADDRESS), BigInteger.ONE, 1);
Transaction tx1 = createFakeTx(PARAMS, SATOSHI, myAddress); Transaction tx1 = createFakeTx(PARAMS, SATOSHI, myAddress);
wallet.receiveFromBlock(tx1, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, 0);
Transaction tx2 = createFakeTx(PARAMS, SATOSHI, myAddress); Transaction tx2 = createFakeTx(PARAMS, SATOSHI, myAddress);
assertNotEquals(tx1.getHash(), tx2.getHash()); assertNotEquals(tx1.getHash(), tx2.getHash());
wallet.receiveFromBlock(tx2, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, 1);
Transaction tx3 = createFakeTx(PARAMS, SATOSHI.multiply(10), myAddress); Transaction tx3 = createFakeTx(PARAMS, SATOSHI.multiply(10), myAddress);
wallet.receiveFromBlock(tx3, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, 2); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, tx1, tx2, tx3);
// Not allowed to send dust. // Not allowed to send dust.
try { try {
@ -2301,9 +2271,7 @@ public class WalletTest extends TestWithWallet {
// Tests basic fee solving works // Tests basic fee solving works
// Add some reasonable-sized outputs // Add some reasonable-sized outputs
StoredBlock block = new StoredBlock(makeSolvedTestBlock(blockStore, OTHER_ADDRESS), BigInteger.ONE, 1); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, Coin.COIN);
Transaction tx4 = createFakeTx(PARAMS, Coin.COIN, myAddress);
wallet.receiveFromBlock(tx4, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, 0);
// Simple test to make sure if we have an ouput < 0.01 we get a fee // Simple test to make sure if we have an ouput < 0.01 we get a fee
SendRequest request1 = SendRequest.to(OTHER_ADDRESS, CENT.subtract(SATOSHI)); SendRequest request1 = SendRequest.to(OTHER_ADDRESS, CENT.subtract(SATOSHI));
@ -2435,56 +2403,58 @@ public class WalletTest extends TestWithWallet {
@Test @Test
public void feeSolverAndCoinSelectionTests2() throws Exception { public void feeSolverAndCoinSelectionTests2() throws Exception {
Transaction tx5 = createFakeTx(PARAMS, CENT, myAddress); Transaction tx5 = sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, CENT);
StoredBlock block = new StoredBlock(makeSolvedTestBlock(blockStore, OTHER_ADDRESS), BigInteger.ONE, 1);
wallet.receiveFromBlock(tx5, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, 0);
assertEquals(CENT, wallet.getBalance()); assertEquals(CENT, wallet.getBalance());
// Now test coin selection properly selects coin*depth // Now test coin selection properly selects coin*depth
for (int i = 0; i < 100; i++) { for (int i = 0; i < 197; i++)
block = new StoredBlock(makeSolvedTestBlock(blockStore, OTHER_ADDRESS), BigInteger.ONE, 1); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN);
wallet.notifyNewBestBlock(block);
}
block = new StoredBlock(makeSolvedTestBlock(blockStore, OTHER_ADDRESS), BigInteger.ONE, 1); Transaction tx6 = sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN);
Transaction tx6 = createFakeTx(PARAMS, COIN, myAddress);
wallet.receiveFromBlock(tx6, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, 1);
assertTrue(tx5.getOutput(0).isMine(wallet)); assertTrue(tx5.getOutput(0).isMine(wallet));
assertTrue(tx5.getOutput(0).isAvailableForSpending()); assertTrue(tx5.getOutput(0).isAvailableForSpending());
assertEquals(100, tx5.getConfidence().getDepthInBlocks()); assertEquals(199, tx5.getConfidence().getDepthInBlocks());
assertTrue(tx6.getOutput(0).isMine(wallet)); assertTrue(tx6.getOutput(0).isMine(wallet));
assertTrue(tx6.getOutput(0).isAvailableForSpending()); assertTrue(tx6.getOutput(0).isAvailableForSpending());
assertEquals(1, tx6.getConfidence().getDepthInBlocks()); assertEquals(1, tx6.getConfidence().getDepthInBlocks());
// tx5 and tx6 have exactly the same coin*depth, so the larger should be selected... // tx5 has higher coin*depth than tx6...
assertTrue(tx5.getOutput(0).getValue().multiply(tx5.getConfidence().getDepthInBlocks())
.isGreaterThan(tx6.getOutput(0).getValue().multiply(tx6.getConfidence().getDepthInBlocks())));
// ...so tx5 should be selected
Transaction spend12 = wallet.createSend(OTHER_ADDRESS, CENT); Transaction spend12 = wallet.createSend(OTHER_ADDRESS, CENT);
assertEquals(2, spend12.getOutputs().size()); assertEquals(1, spend12.getInputs().size());
assertEquals(COIN, spend12.getOutput(0).getValue().add(spend12.getOutput(1).getValue())); assertEquals(CENT, spend12.getInput(0).getValue());
wallet.notifyNewBestBlock(block); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN);
assertTrue(tx5.getOutput(0).isMine(wallet)); assertTrue(tx5.getOutput(0).isMine(wallet));
assertTrue(tx5.getOutput(0).isAvailableForSpending()); assertTrue(tx5.getOutput(0).isAvailableForSpending());
assertEquals(101, tx5.getConfidence().getDepthInBlocks()); assertEquals(200, tx5.getConfidence().getDepthInBlocks());
assertTrue(tx6.getOutput(0).isMine(wallet));
assertTrue(tx6.getOutput(0).isAvailableForSpending());
assertEquals(1, tx6.getConfidence().getDepthInBlocks());
// Now tx5 has slightly higher coin*depth than tx6...
Transaction spend13 = wallet.createSend(OTHER_ADDRESS, CENT);
assertEquals(1, spend13.getOutputs().size());
assertEquals(CENT, spend13.getOutput(0).getValue());
block = new StoredBlock(makeSolvedTestBlock(blockStore, OTHER_ADDRESS), BigInteger.ONE, 1);
wallet.notifyNewBestBlock(block);
assertTrue(tx5.getOutput(0).isMine(wallet));
assertTrue(tx5.getOutput(0).isAvailableForSpending());
assertEquals(102, tx5.getConfidence().getDepthInBlocks());
assertTrue(tx6.getOutput(0).isMine(wallet)); assertTrue(tx6.getOutput(0).isMine(wallet));
assertTrue(tx6.getOutput(0).isAvailableForSpending()); assertTrue(tx6.getOutput(0).isAvailableForSpending());
assertEquals(2, tx6.getConfidence().getDepthInBlocks()); assertEquals(2, tx6.getConfidence().getDepthInBlocks());
// Now tx6 has higher coin*depth than tx5... // Now tx5 and tx6 have exactly the same coin*depth...
Transaction spend14 = wallet.createSend(OTHER_ADDRESS, CENT); assertEquals(tx5.getOutput(0).getValue().multiply(tx5.getConfidence().getDepthInBlocks()),
assertEquals(2, spend14.getOutputs().size()); tx6.getOutput(0).getValue().multiply(tx6.getConfidence().getDepthInBlocks()));
assertEquals(COIN, spend14.getOutput(0).getValue().add(spend14.getOutput(1).getValue())); // ...so the larger tx6 should be selected
Transaction spend13 = wallet.createSend(OTHER_ADDRESS, COIN);
assertEquals(1, spend13.getInputs().size());
assertEquals(COIN, spend13.getInput(0).getValue());
sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN);
assertTrue(tx5.getOutput(0).isMine(wallet));
assertTrue(tx5.getOutput(0).isAvailableForSpending());
assertEquals(201, tx5.getConfidence().getDepthInBlocks());
assertTrue(tx6.getOutput(0).isMine(wallet));
assertTrue(tx6.getOutput(0).isAvailableForSpending());
assertEquals(3, tx6.getConfidence().getDepthInBlocks());
// Now tx5 has lower coin*depth than tx6...
assertTrue(tx5.getOutput(0).getValue().multiply(tx5.getConfidence().getDepthInBlocks())
.isLessThan(tx6.getOutput(0).getValue().multiply(tx6.getConfidence().getDepthInBlocks())));
// ...so tx6 should be selected
Transaction spend14 = wallet.createSend(OTHER_ADDRESS, COIN);
assertEquals(1, spend14.getInputs().size());
assertEquals(COIN, spend14.getInput(0).getValue());
// Now test feePerKb // Now test feePerKb
SendRequest request15 = SendRequest.to(OTHER_ADDRESS, CENT); SendRequest request15 = SendRequest.to(OTHER_ADDRESS, CENT);
@ -2681,7 +2651,7 @@ public class WalletTest extends TestWithWallet {
spendTx5.addInput(tx5.getOutput(0)); spendTx5.addInput(tx5.getOutput(0));
wallet.signTransaction(SendRequest.forTx(spendTx5)); wallet.signTransaction(SendRequest.forTx(spendTx5));
wallet.receiveFromBlock(spendTx5, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, 4); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, spendTx5);
assertEquals(COIN, wallet.getBalance()); assertEquals(COIN, wallet.getBalance());
// Ensure change is discarded if it results in a fee larger than the chain (same as 8 and 9 but with feePerKb) // Ensure change is discarded if it results in a fee larger than the chain (same as 8 and 9 but with feePerKb)
@ -2868,13 +2838,10 @@ public class WalletTest extends TestWithWallet {
// Simple test of boundary condition on fee per kb in category fee solver // Simple test of boundary condition on fee per kb in category fee solver
// Generate a ton of small outputs // Generate a ton of small outputs
StoredBlock block = new StoredBlock(makeSolvedTestBlock(blockStore, OTHER_ADDRESS), BigInteger.ONE, 1);
Transaction tx = createFakeTx(PARAMS, COIN, myAddress); Transaction tx = createFakeTx(PARAMS, COIN, myAddress);
wallet.receiveFromBlock(tx, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, 0);
Transaction tx2 = createFakeTx(PARAMS, CENT, myAddress); Transaction tx2 = createFakeTx(PARAMS, CENT, myAddress);
wallet.receiveFromBlock(tx2, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, 1);
Transaction tx3 = createFakeTx(PARAMS, SATOSHI, myAddress); Transaction tx3 = createFakeTx(PARAMS, SATOSHI, myAddress);
wallet.receiveFromBlock(tx3, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, 2); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, tx, tx2, tx3);
// Create a transaction who's max size could be up to 1000 (if signatures were maximum size) // Create a transaction who's max size could be up to 1000 (if signatures were maximum size)
SendRequest request1 = SendRequest.to(OTHER_ADDRESS, COIN.subtract(CENT.multiply(17))); SendRequest request1 = SendRequest.to(OTHER_ADDRESS, COIN.subtract(CENT.multiply(17)));
@ -2893,7 +2860,7 @@ public class WalletTest extends TestWithWallet {
// We then add one more satoshi output to the wallet // We then add one more satoshi output to the wallet
Transaction tx4 = createFakeTx(PARAMS, SATOSHI, myAddress); Transaction tx4 = createFakeTx(PARAMS, SATOSHI, myAddress);
wallet.receiveFromBlock(tx4, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, 3); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, tx4);
// Create a transaction who's max size could be up to 1000 (if signatures were maximum size) // Create a transaction who's max size could be up to 1000 (if signatures were maximum size)
SendRequest request2 = SendRequest.to(OTHER_ADDRESS, COIN.subtract(CENT.multiply(17))); SendRequest request2 = SendRequest.to(OTHER_ADDRESS, COIN.subtract(CENT.multiply(17)));
@ -2990,7 +2957,7 @@ public class WalletTest extends TestWithWallet {
} }
}); });
sendMoneyToWallet(COIN, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN);
log.info("Wait for user thread"); log.info("Wait for user thread");
Threading.waitForUserCode(); Threading.waitForUserCode();
log.info("... and test flag."); log.info("... and test flag.");
@ -3056,8 +3023,7 @@ public class WalletTest extends TestWithWallet {
public void childPaysForParent() throws Exception { public void childPaysForParent() throws Exception {
// Receive confirmed balance to play with. // Receive confirmed balance to play with.
Transaction toMe = createFakeTxWithoutChangeAddress(PARAMS, COIN, myAddress); Transaction toMe = createFakeTxWithoutChangeAddress(PARAMS, COIN, myAddress);
wallet.receiveFromBlock(toMe, createFakeBlock(blockStore, toMe).storedBlock, sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, toMe);
AbstractBlockChain.NewBlockType.BEST_CHAIN, 0);
assertEquals(Coin.COIN, wallet.getBalance(BalanceType.ESTIMATED_SPENDABLE)); assertEquals(Coin.COIN, wallet.getBalance(BalanceType.ESTIMATED_SPENDABLE));
assertEquals(Coin.COIN, wallet.getBalance(BalanceType.AVAILABLE_SPENDABLE)); assertEquals(Coin.COIN, wallet.getBalance(BalanceType.AVAILABLE_SPENDABLE));
// Receive unconfirmed coin without fee. // Receive unconfirmed coin without fee.
@ -3089,9 +3055,9 @@ public class WalletTest extends TestWithWallet {
key2.setCreationTimeSeconds(Utils.currentTimeSeconds() - 86400); key2.setCreationTimeSeconds(Utils.currentTimeSeconds() - 86400);
wallet.importKey(key1); wallet.importKey(key1);
wallet.importKey(key2); wallet.importKey(key2);
sendMoneyToWallet(wallet, CENT, key1.toAddress(PARAMS), AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(wallet, AbstractBlockChain.NewBlockType.BEST_CHAIN, CENT, key1.toAddress(PARAMS));
sendMoneyToWallet(wallet, CENT, key2.toAddress(PARAMS), AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(wallet, AbstractBlockChain.NewBlockType.BEST_CHAIN, CENT, key2.toAddress(PARAMS));
sendMoneyToWallet(wallet, CENT, key2.toAddress(PARAMS), AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(wallet, AbstractBlockChain.NewBlockType.BEST_CHAIN, CENT, key2.toAddress(PARAMS));
Date compromiseTime = Utils.now(); Date compromiseTime = Utils.now();
assertEquals(0, broadcaster.size()); assertEquals(0, broadcaster.size());
assertFalse(wallet.isKeyRotating(key1)); assertFalse(wallet.isKeyRotating(key1));
@ -3113,15 +3079,15 @@ public class WalletTest extends TestWithWallet {
assertFalse(wallet.isKeyRotating(rotatingToKey)); assertFalse(wallet.isKeyRotating(rotatingToKey));
assertEquals(3, tx.getInputs().size()); assertEquals(3, tx.getInputs().size());
// It confirms. // It confirms.
sendMoneyToWallet(tx, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, tx);
// Now receive some more money to the newly derived address via a new block and check that nothing happens. // Now receive some more money to the newly derived address via a new block and check that nothing happens.
sendMoneyToWallet(wallet, CENT, toAddress, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(wallet, AbstractBlockChain.NewBlockType.BEST_CHAIN, CENT, toAddress);
assertTrue(wallet.doMaintenance(null, true).get().isEmpty()); assertTrue(wallet.doMaintenance(null, true).get().isEmpty());
assertEquals(0, broadcaster.size()); assertEquals(0, broadcaster.size());
// Receive money via a new block on key1 and ensure it shows up as a maintenance task. // Receive money via a new block on key1 and ensure it shows up as a maintenance task.
sendMoneyToWallet(wallet, CENT, key1.toAddress(PARAMS), AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(wallet, AbstractBlockChain.NewBlockType.BEST_CHAIN, CENT, key1.toAddress(PARAMS));
wallet.doMaintenance(null, true); wallet.doMaintenance(null, true);
tx = broadcaster.waitForTransactionAndSucceed(); tx = broadcaster.waitForTransactionAndSucceed();
assertNotNull(wallet.findKeyFromPubHash(tx.getOutput(0).getScriptPubKey().getPubKeyHash())); assertNotNull(wallet.findKeyFromPubHash(tx.getOutput(0).getScriptPubKey().getPubKeyHash()));
@ -3161,8 +3127,8 @@ public class WalletTest extends TestWithWallet {
wallet = new Wallet(PARAMS); wallet = new Wallet(PARAMS);
ECKey key1 = wallet.freshReceiveKey(); ECKey key1 = wallet.freshReceiveKey();
ECKey key2 = wallet.freshReceiveKey(); ECKey key2 = wallet.freshReceiveKey();
sendMoneyToWallet(wallet, CENT, key1.toAddress(PARAMS), AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(wallet, AbstractBlockChain.NewBlockType.BEST_CHAIN, CENT, key1.toAddress(PARAMS));
sendMoneyToWallet(wallet, CENT, key2.toAddress(PARAMS), AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(wallet, AbstractBlockChain.NewBlockType.BEST_CHAIN, CENT, key2.toAddress(PARAMS));
DeterministicKey watchKey1 = wallet.getWatchingKey(); DeterministicKey watchKey1 = wallet.getWatchingKey();
// A day later, we get compromised. // A day later, we get compromised.
@ -3206,7 +3172,7 @@ public class WalletTest extends TestWithWallet {
wallet.upgradeToDeterministic(null); wallet.upgradeToDeterministic(null);
DeterministicKey badWatchingKey = wallet.getWatchingKey(); DeterministicKey badWatchingKey = wallet.getWatchingKey();
assertEquals(badKey.getCreationTimeSeconds(), badWatchingKey.getCreationTimeSeconds()); assertEquals(badKey.getCreationTimeSeconds(), badWatchingKey.getCreationTimeSeconds());
sendMoneyToWallet(wallet, CENT, badWatchingKey.toAddress(PARAMS), AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(wallet, AbstractBlockChain.NewBlockType.BEST_CHAIN, CENT, badWatchingKey.toAddress(PARAMS));
// Now we set the rotation time to the time we started making good keys. This should create a new HD chain. // Now we set the rotation time to the time we started making good keys. This should create a new HD chain.
wallet.setKeyRotationTime(goodKey.getCreationTimeSeconds()); wallet.setKeyRotationTime(goodKey.getCreationTimeSeconds());
@ -3243,7 +3209,7 @@ public class WalletTest extends TestWithWallet {
Utils.setMockClock(); Utils.setMockClock();
Utils.rollMockClock(86400); Utils.rollMockClock(86400);
for (int i = 0; i < 800; i++) { for (int i = 0; i < 800; i++) {
sendMoneyToWallet(wallet, CENT, address, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, CENT, address);
} }
MockTransactionBroadcaster broadcaster = new MockTransactionBroadcaster(wallet); MockTransactionBroadcaster broadcaster = new MockTransactionBroadcaster(wallet);
@ -3280,7 +3246,7 @@ public class WalletTest extends TestWithWallet {
@Test (expected = ECKey.MissingPrivateKeyException.class) @Test (expected = ECKey.MissingPrivateKeyException.class)
public void completeTxPartiallySignedThrows() throws Exception { public void completeTxPartiallySignedThrows() throws Exception {
sendMoneyToWallet(wallet, CENT, wallet.freshReceiveKey(), AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, CENT, wallet.freshReceiveKey());
SendRequest req = SendRequest.emptyWallet(OTHER_ADDRESS); SendRequest req = SendRequest.emptyWallet(OTHER_ADDRESS);
wallet.completeTx(req); wallet.completeTx(req);
// Delete the sigs // Delete the sigs
@ -3312,7 +3278,7 @@ public class WalletTest extends TestWithWallet {
public void completeTxPartiallySignedMarriedThrowsByDefault() throws Exception { public void completeTxPartiallySignedMarriedThrowsByDefault() throws Exception {
createMarriedWallet(2, 2, false); createMarriedWallet(2, 2, false);
myAddress = wallet.currentAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS); myAddress = wallet.currentAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS);
sendMoneyToWallet(wallet, COIN, myAddress, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN, myAddress);
Wallet.SendRequest req = Wallet.SendRequest.emptyWallet(OTHER_ADDRESS); Wallet.SendRequest req = Wallet.SendRequest.emptyWallet(OTHER_ADDRESS);
wallet.completeTx(req); wallet.completeTx(req);
@ -3322,7 +3288,7 @@ public class WalletTest extends TestWithWallet {
// create married wallet without signer // create married wallet without signer
createMarriedWallet(2, 2, false); createMarriedWallet(2, 2, false);
myAddress = wallet.currentAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS); myAddress = wallet.currentAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS);
sendMoneyToWallet(wallet, COIN, myAddress, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN, myAddress);
Wallet.SendRequest req = Wallet.SendRequest.emptyWallet(OTHER_ADDRESS); Wallet.SendRequest req = Wallet.SendRequest.emptyWallet(OTHER_ADDRESS);
req.missingSigsMode = missSigMode; req.missingSigsMode = missSigMode;
@ -3349,9 +3315,9 @@ public class WalletTest extends TestWithWallet {
// Send three transactions, with one being an address type and the other being a raw CHECKSIG type pubkey only, // Send three transactions, with one being an address type and the other being a raw CHECKSIG type pubkey only,
// and the final one being a key we do have. We expect the first two inputs to be dummy values and the last // and the final one being a key we do have. We expect the first two inputs to be dummy values and the last
// to be signed correctly. // to be signed correctly.
Transaction t1 = sendMoneyToWallet(wallet, CENT, pub.toAddress(PARAMS), AbstractBlockChain.NewBlockType.BEST_CHAIN); Transaction t1 = sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, CENT, pub.toAddress(PARAMS));
Transaction t2 = sendMoneyToWallet(wallet, CENT, pub, AbstractBlockChain.NewBlockType.BEST_CHAIN); Transaction t2 = sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, CENT, pub);
Transaction t3 = sendMoneyToWallet(wallet, CENT, priv2, AbstractBlockChain.NewBlockType.BEST_CHAIN); Transaction t3 = sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, CENT, priv2);
Wallet.SendRequest req = Wallet.SendRequest.emptyWallet(OTHER_ADDRESS); Wallet.SendRequest req = Wallet.SendRequest.emptyWallet(OTHER_ADDRESS);
req.missingSigsMode = missSigMode; req.missingSigsMode = missSigMode;
@ -3400,8 +3366,7 @@ public class WalletTest extends TestWithWallet {
assertEquals(Coin.ZERO, wallet.getBalance(Wallet.BalanceType.ESTIMATED)); assertEquals(Coin.ZERO, wallet.getBalance(Wallet.BalanceType.ESTIMATED));
assertTrue(bool.get()); assertTrue(bool.get());
// Confirm it in the same manner as how Bloom filtered blocks do. Verify it shows up. // Confirm it in the same manner as how Bloom filtered blocks do. Verify it shows up.
StoredBlock block = createFakeBlock(blockStore, Block.BLOCK_HEIGHT_GENESIS, tx).storedBlock; sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, tx);
wallet.notifyTransactionIsInBlock(tx.getHash(), block, AbstractBlockChain.NewBlockType.BEST_CHAIN, 1);
assertEquals(COIN, wallet.getBalance()); assertEquals(COIN, wallet.getBalance());
} }
@ -3534,8 +3499,8 @@ public class WalletTest extends TestWithWallet {
.build(); .build();
wallet.addAndActivateHDChain(chain); wallet.addAndActivateHDChain(chain);
myAddress = wallet.currentAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS); Address myAddress = wallet.currentAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS);
sendMoneyToWallet(wallet, COIN, myAddress, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(wallet, AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN, myAddress);
Wallet.SendRequest req = Wallet.SendRequest.emptyWallet(OTHER_ADDRESS); Wallet.SendRequest req = Wallet.SendRequest.emptyWallet(OTHER_ADDRESS);
req.missingSigsMode = Wallet.MissingSigsMode.USE_DUMMY_SIG; req.missingSigsMode = Wallet.MissingSigsMode.USE_DUMMY_SIG;
@ -3590,7 +3555,7 @@ public class WalletTest extends TestWithWallet {
@Test @Test
public void reset() { public void reset() {
sendMoneyToWallet(wallet, COIN, myAddress, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN, myAddress);
assertNotEquals(Coin.ZERO, wallet.getBalance(Wallet.BalanceType.ESTIMATED)); assertNotEquals(Coin.ZERO, wallet.getBalance(Wallet.BalanceType.ESTIMATED));
assertNotEquals(0, wallet.getTransactions(false).size()); assertNotEquals(0, wallet.getTransactions(false).size());
assertNotEquals(0, wallet.getUnspents().size()); assertNotEquals(0, wallet.getUnspents().size());
@ -3602,14 +3567,10 @@ public class WalletTest extends TestWithWallet {
@Test @Test
public void totalReceivedSent() throws Exception { public void totalReceivedSent() throws Exception {
// Receive 4 BTC in 2 separate transactions // Receive 4 BTC in 2 separate transactions
Transaction toMe1 = createFakeTxWithoutChangeAddress(PARAMS, COIN.multiply(2), myAddress); Transaction toMe1 = createFakeTxWithoutChangeAddress(PARAMS, COIN.multiply(2), myAddress);
Transaction toMe2 = createFakeTxWithoutChangeAddress(PARAMS, COIN.multiply(2), myAddress); Transaction toMe2 = createFakeTxWithoutChangeAddress(PARAMS, COIN.multiply(2), myAddress);
StoredBlock block1 = createFakeBlock(blockStore, toMe1, toMe2).storedBlock; sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, toMe1, toMe2);
wallet.receiveFromBlock(toMe1, block1, AbstractBlockChain.NewBlockType.BEST_CHAIN, 0);
wallet.receiveFromBlock(toMe2, block1, AbstractBlockChain.NewBlockType.BEST_CHAIN, 0);
wallet.notifyNewBestBlock(block1);
// Check we calculate the total received correctly // Check we calculate the total received correctly
assertEquals(Coin.COIN.multiply(4), wallet.getTotalReceived()); assertEquals(Coin.COIN.multiply(4), wallet.getTotalReceived());
@ -3617,9 +3578,7 @@ public class WalletTest extends TestWithWallet {
// Send 3 BTC in a single transaction // Send 3 BTC in a single transaction
SendRequest req = SendRequest.to(OTHER_ADDRESS, Coin.COIN.multiply(3)); SendRequest req = SendRequest.to(OTHER_ADDRESS, Coin.COIN.multiply(3));
wallet.completeTx(req); wallet.completeTx(req);
StoredBlock block2 = createFakeBlock(blockStore, req.tx).storedBlock; sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, req.tx);
wallet.receiveFromBlock(req.tx, block2, AbstractBlockChain.NewBlockType.BEST_CHAIN, 0);
wallet.notifyNewBestBlock(block2);
// Check that we still have the same totalReceived, since the above tx will have sent us change back // Check that we still have the same totalReceived, since the above tx will have sent us change back
assertEquals(Coin.COIN.multiply(4),wallet.getTotalReceived()); assertEquals(Coin.COIN.multiply(4),wallet.getTotalReceived());

View File

@ -103,8 +103,8 @@ public class ChannelConnectionTest extends TestWithWallet {
super.setUp(); super.setUp();
Utils.setMockClock(); // Use mock clock Utils.setMockClock(); // Use mock clock
Context.propagate(new Context(PARAMS, 3, Coin.ZERO, false)); // Shorter event horizon for unit tests. Context.propagate(new Context(PARAMS, 3, Coin.ZERO, false)); // Shorter event horizon for unit tests.
sendMoneyToWallet(COIN, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN);
sendMoneyToWallet(COIN, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN);
wallet.addExtension(new StoredPaymentChannelClientStates(wallet, failBroadcaster)); wallet.addExtension(new StoredPaymentChannelClientStates(wallet, failBroadcaster));
serverWallet = new Wallet(PARAMS); serverWallet = new Wallet(PARAMS);
serverWallet.addExtension(new StoredPaymentChannelServerStates(serverWallet, failBroadcaster)); serverWallet.addExtension(new StoredPaymentChannelServerStates(serverWallet, failBroadcaster));
@ -256,7 +256,7 @@ public class ChannelConnectionTest extends TestWithWallet {
assertTrue(channels.mapChannels.isEmpty()); assertTrue(channels.mapChannels.isEmpty());
// Send the settle TX to the client wallet. // Send the settle TX to the client wallet.
sendMoneyToWallet(settleTx, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, settleTx);
assertTrue(client.state().getState() == PaymentChannelClientState.State.CLOSED); assertTrue(client.state().getState() == PaymentChannelClientState.State.CLOSED);
server.close(); server.close();
@ -649,7 +649,7 @@ public class ChannelConnectionTest extends TestWithWallet {
// Now check that if the server has a lower min size than what we are willing to spend, we do actually open // Now check that if the server has a lower min size than what we are willing to spend, we do actually open
// a channel of that size. // a channel of that size.
sendMoneyToWallet(COIN.multiply(10), AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN.multiply(10));
pair = ChannelTestUtils.makeRecorders(serverWallet, mockBroadcaster); pair = ChannelTestUtils.makeRecorders(serverWallet, mockBroadcaster);
server = pair.server; server = pair.server;
@ -850,7 +850,7 @@ public class ChannelConnectionTest extends TestWithWallet {
assertEquals(settlement1, settlement2); assertEquals(settlement1, settlement2);
client.receiveMessage(closeMsg); client.receiveMessage(closeMsg);
assertNotNull(wallet.getTransaction(settlement2.getHash())); // Close TX entered the wallet. assertNotNull(wallet.getTransaction(settlement2.getHash())); // Close TX entered the wallet.
sendMoneyToWallet(settlement1, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, settlement1);
client.connectionClosed(); client.connectionClosed();
server.connectionClosed(); server.connectionClosed();
} }

View File

@ -96,7 +96,7 @@ public class PaymentChannelStateTest extends TestWithWallet {
return null; return null;
} }
})); }));
sendMoneyToWallet(COIN, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN);
chain = new BlockChain(PARAMS, wallet, blockStore); // Recreate chain as sendMoneyToWallet will confuse it chain = new BlockChain(PARAMS, wallet, blockStore); // Recreate chain as sendMoneyToWallet will confuse it
serverWallet = new Wallet(PARAMS); serverWallet = new Wallet(PARAMS);
serverKey = serverWallet.freshReceiveKey(); serverKey = serverWallet.freshReceiveKey();

View File

@ -57,38 +57,53 @@ public class TestWithWallet {
} }
@Nullable @Nullable
protected Transaction sendMoneyToWallet(Wallet wallet, Transaction tx, AbstractBlockChain.NewBlockType type) protected Transaction sendMoneyToWallet(Wallet wallet, AbstractBlockChain.NewBlockType type, Transaction... transactions)
throws VerificationException { throws VerificationException {
if (type == null) { if (type == null) {
// Pending/broadcast tx. // Pending transaction
if (wallet.isPendingTransactionRelevant(tx)) for (Transaction tx : transactions)
wallet.receivePending(tx, null); if (wallet.isPendingTransactionRelevant(tx))
wallet.receivePending(tx, null);
} else { } else {
FakeTxBuilder.BlockPair bp = createFakeBlock(blockStore, Block.BLOCK_HEIGHT_GENESIS, tx); FakeTxBuilder.BlockPair bp = createFakeBlock(blockStore, Block.BLOCK_HEIGHT_GENESIS, transactions);
wallet.receiveFromBlock(tx, bp.storedBlock, type, 0); for (Transaction tx : transactions)
wallet.receiveFromBlock(tx, bp.storedBlock, type, 0);
if (type == AbstractBlockChain.NewBlockType.BEST_CHAIN) if (type == AbstractBlockChain.NewBlockType.BEST_CHAIN)
wallet.notifyNewBestBlock(bp.storedBlock); wallet.notifyNewBestBlock(bp.storedBlock);
} }
return wallet.getTransaction(tx.getHash()); // Can be null if tx is a double spend that's otherwise irrelevant. if (transactions.length == 1)
return wallet.getTransaction(transactions[0].getHash()); // Can be null if tx is a double spend that's otherwise irrelevant.
else
return null;
} }
@Nullable @Nullable
protected Transaction sendMoneyToWallet(Transaction tx, AbstractBlockChain.NewBlockType type) throws VerificationException { protected Transaction sendMoneyToWallet(Wallet wallet, AbstractBlockChain.NewBlockType type, Coin value, Address toAddress) throws VerificationException {
return sendMoneyToWallet(this.wallet, tx, type); return sendMoneyToWallet(wallet, type, createFakeTx(PARAMS, value, toAddress));
} }
@Nullable @Nullable
protected Transaction sendMoneyToWallet(Wallet wallet, Coin value, Address toAddress, AbstractBlockChain.NewBlockType type) throws VerificationException { protected Transaction sendMoneyToWallet(Wallet wallet, AbstractBlockChain.NewBlockType type, Coin value, ECKey toPubKey) throws VerificationException {
return sendMoneyToWallet(wallet, createFakeTx(PARAMS, value, toAddress), type); return sendMoneyToWallet(wallet, type, createFakeTx(PARAMS, value, toPubKey));
} }
@Nullable @Nullable
protected Transaction sendMoneyToWallet(Wallet wallet, Coin value, ECKey toPubKey, AbstractBlockChain.NewBlockType type) throws VerificationException { protected Transaction sendMoneyToWallet(AbstractBlockChain.NewBlockType type, Transaction... transactions) throws VerificationException {
return sendMoneyToWallet(wallet, createFakeTx(PARAMS, value, toPubKey), type); return sendMoneyToWallet(this.wallet, type, transactions);
} }
@Nullable @Nullable
protected Transaction sendMoneyToWallet(Coin value, AbstractBlockChain.NewBlockType type) throws VerificationException { protected Transaction sendMoneyToWallet(AbstractBlockChain.NewBlockType type, Coin value) throws VerificationException {
return sendMoneyToWallet(this.wallet, createFakeTx(PARAMS, value, myAddress), type); return sendMoneyToWallet(this.wallet, type, value, myAddress);
}
@Nullable
protected Transaction sendMoneyToWallet(AbstractBlockChain.NewBlockType type, Coin value, Address toAddress) throws VerificationException {
return sendMoneyToWallet(this.wallet, type, value, toAddress);
}
@Nullable
protected Transaction sendMoneyToWallet(AbstractBlockChain.NewBlockType type, Coin value, ECKey toPubKey) throws VerificationException {
return sendMoneyToWallet(this.wallet, type, value, toPubKey);
} }
} }

View File

@ -68,8 +68,8 @@ public class DefaultCoinSelectorTest extends TestWithWallet {
@Test @Test
public void depthOrdering() throws Exception { public void depthOrdering() throws Exception {
// Send two transactions in two blocks on top of each other. // Send two transactions in two blocks on top of each other.
Transaction t1 = checkNotNull(sendMoneyToWallet(COIN, AbstractBlockChain.NewBlockType.BEST_CHAIN)); Transaction t1 = checkNotNull(sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN));
Transaction t2 = checkNotNull(sendMoneyToWallet(COIN, AbstractBlockChain.NewBlockType.BEST_CHAIN)); Transaction t2 = checkNotNull(sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN));
// Check we selected just the oldest one. // Check we selected just the oldest one.
DefaultCoinSelector selector = new DefaultCoinSelector(); DefaultCoinSelector selector = new DefaultCoinSelector();
@ -90,12 +90,12 @@ public class DefaultCoinSelectorTest extends TestWithWallet {
public void coinAgeOrdering() throws Exception { public void coinAgeOrdering() throws Exception {
// Send three transactions in four blocks on top of each other. Coin age of t1 is 1*4=4, coin age of t2 = 2*2=4 // Send three transactions in four blocks on top of each other. Coin age of t1 is 1*4=4, coin age of t2 = 2*2=4
// and t3=0.01. // and t3=0.01.
Transaction t1 = checkNotNull(sendMoneyToWallet(COIN, AbstractBlockChain.NewBlockType.BEST_CHAIN)); Transaction t1 = checkNotNull(sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN));
// Padding block. // Padding block.
wallet.notifyNewBestBlock(FakeTxBuilder.createFakeBlock(blockStore, Block.BLOCK_HEIGHT_GENESIS).storedBlock); wallet.notifyNewBestBlock(FakeTxBuilder.createFakeBlock(blockStore, Block.BLOCK_HEIGHT_GENESIS).storedBlock);
final Coin TWO_COINS = COIN.multiply(2); final Coin TWO_COINS = COIN.multiply(2);
Transaction t2 = checkNotNull(sendMoneyToWallet(TWO_COINS, AbstractBlockChain.NewBlockType.BEST_CHAIN)); Transaction t2 = checkNotNull(sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, TWO_COINS));
Transaction t3 = checkNotNull(sendMoneyToWallet(CENT, AbstractBlockChain.NewBlockType.BEST_CHAIN)); Transaction t3 = checkNotNull(sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, CENT));
// Should be ordered t2, t1, t3. // Should be ordered t2, t1, t3.
ArrayList<TransactionOutput> candidates = new ArrayList<TransactionOutput>(); ArrayList<TransactionOutput> candidates = new ArrayList<TransactionOutput>();