Commit c02c5ff2 authored by Andreas Schildbach's avatar Andreas Schildbach

WalletTest: Use sendMoneyToWallet() when faking blocks.

parent f92d11e5
......@@ -26,7 +26,7 @@ public class TransactionOutputTest extends TestWithWallet {
@Test
public void testMultiSigOutputToString() throws Exception {
sendMoneyToWallet(Coin.COIN, AbstractBlockChain.NewBlockType.BEST_CHAIN);
sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, Coin.COIN);
ECKey myKey = new ECKey();
this.wallet.importKey(myKey);
......
......@@ -103,8 +103,8 @@ public class ChannelConnectionTest extends TestWithWallet {
super.setUp();
Utils.setMockClock(); // Use mock clock
Context.propagate(new Context(PARAMS, 3, Coin.ZERO, false)); // Shorter event horizon for unit tests.
sendMoneyToWallet(COIN, AbstractBlockChain.NewBlockType.BEST_CHAIN);
sendMoneyToWallet(COIN, AbstractBlockChain.NewBlockType.BEST_CHAIN);
sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN);
sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN);
wallet.addExtension(new StoredPaymentChannelClientStates(wallet, failBroadcaster));
serverWallet = new Wallet(PARAMS);
serverWallet.addExtension(new StoredPaymentChannelServerStates(serverWallet, failBroadcaster));
......@@ -256,7 +256,7 @@ public class ChannelConnectionTest extends TestWithWallet {
assertTrue(channels.mapChannels.isEmpty());
// 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);
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
// 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);
server = pair.server;
......@@ -850,7 +850,7 @@ public class ChannelConnectionTest extends TestWithWallet {
assertEquals(settlement1, settlement2);
client.receiveMessage(closeMsg);
assertNotNull(wallet.getTransaction(settlement2.getHash())); // Close TX entered the wallet.
sendMoneyToWallet(settlement1, AbstractBlockChain.NewBlockType.BEST_CHAIN);
sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, settlement1);
client.connectionClosed();
server.connectionClosed();
}
......
......@@ -96,7 +96,7 @@ public class PaymentChannelStateTest extends TestWithWallet {
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
serverWallet = new Wallet(PARAMS);
serverKey = serverWallet.freshReceiveKey();
......
......@@ -57,38 +57,53 @@ public class TestWithWallet {
}
@Nullable
protected Transaction sendMoneyToWallet(Wallet wallet, Transaction tx, AbstractBlockChain.NewBlockType type)
protected Transaction sendMoneyToWallet(Wallet wallet, AbstractBlockChain.NewBlockType type, Transaction... transactions)
throws VerificationException {
if (type == null) {
// Pending/broadcast tx.
if (wallet.isPendingTransactionRelevant(tx))
wallet.receivePending(tx, null);
// Pending transaction
for (Transaction tx : transactions)
if (wallet.isPendingTransactionRelevant(tx))
wallet.receivePending(tx, null);
} else {
FakeTxBuilder.BlockPair bp = createFakeBlock(blockStore, Block.BLOCK_HEIGHT_GENESIS, tx);
wallet.receiveFromBlock(tx, bp.storedBlock, type, 0);
FakeTxBuilder.BlockPair bp = createFakeBlock(blockStore, Block.BLOCK_HEIGHT_GENESIS, transactions);
for (Transaction tx : transactions)
wallet.receiveFromBlock(tx, bp.storedBlock, type, 0);
if (type == AbstractBlockChain.NewBlockType.BEST_CHAIN)
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
protected Transaction sendMoneyToWallet(Transaction tx, AbstractBlockChain.NewBlockType type) throws VerificationException {
return sendMoneyToWallet(this.wallet, tx, type);
protected Transaction sendMoneyToWallet(Wallet wallet, AbstractBlockChain.NewBlockType type, Coin value, Address toAddress) throws VerificationException {
return sendMoneyToWallet(wallet, type, createFakeTx(PARAMS, value, toAddress));
}
@Nullable
protected Transaction sendMoneyToWallet(Wallet wallet, Coin value, Address toAddress, AbstractBlockChain.NewBlockType type) throws VerificationException {
return sendMoneyToWallet(wallet, createFakeTx(PARAMS, value, toAddress), type);
protected Transaction sendMoneyToWallet(Wallet wallet, AbstractBlockChain.NewBlockType type, Coin value, ECKey toPubKey) throws VerificationException {
return sendMoneyToWallet(wallet, type, createFakeTx(PARAMS, value, toPubKey));
}
@Nullable
protected Transaction sendMoneyToWallet(Wallet wallet, Coin value, ECKey toPubKey, AbstractBlockChain.NewBlockType type) throws VerificationException {
return sendMoneyToWallet(wallet, createFakeTx(PARAMS, value, toPubKey), type);
protected Transaction sendMoneyToWallet(AbstractBlockChain.NewBlockType type, Transaction... transactions) throws VerificationException {
return sendMoneyToWallet(this.wallet, type, transactions);
}
@Nullable
protected Transaction sendMoneyToWallet(Coin value, AbstractBlockChain.NewBlockType type) throws VerificationException {
return sendMoneyToWallet(this.wallet, createFakeTx(PARAMS, value, myAddress), type);
protected Transaction sendMoneyToWallet(AbstractBlockChain.NewBlockType type, Coin value) throws VerificationException {
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);
}
}
......@@ -68,8 +68,8 @@ public class DefaultCoinSelectorTest extends TestWithWallet {
@Test
public void depthOrdering() throws Exception {
// Send two transactions in two blocks on top of each other.
Transaction t1 = checkNotNull(sendMoneyToWallet(COIN, AbstractBlockChain.NewBlockType.BEST_CHAIN));
Transaction t2 = checkNotNull(sendMoneyToWallet(COIN, AbstractBlockChain.NewBlockType.BEST_CHAIN));
Transaction t1 = checkNotNull(sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN));
Transaction t2 = checkNotNull(sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN));
// Check we selected just the oldest one.
DefaultCoinSelector selector = new DefaultCoinSelector();
......@@ -90,12 +90,12 @@ public class DefaultCoinSelectorTest extends TestWithWallet {
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
// and t3=0.01.
Transaction t1 = checkNotNull(sendMoneyToWallet(COIN, AbstractBlockChain.NewBlockType.BEST_CHAIN));
Transaction t1 = checkNotNull(sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN));
// Padding block.
wallet.notifyNewBestBlock(FakeTxBuilder.createFakeBlock(blockStore, Block.BLOCK_HEIGHT_GENESIS).storedBlock);
final Coin TWO_COINS = COIN.multiply(2);
Transaction t2 = checkNotNull(sendMoneyToWallet(TWO_COINS, AbstractBlockChain.NewBlockType.BEST_CHAIN));
Transaction t3 = checkNotNull(sendMoneyToWallet(CENT, AbstractBlockChain.NewBlockType.BEST_CHAIN));
Transaction t2 = checkNotNull(sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, TWO_COINS));
Transaction t3 = checkNotNull(sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, CENT));
// Should be ordered t2, t1, t3.
ArrayList<TransactionOutput> candidates = new ArrayList<TransactionOutput>();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment