com.google.bitcoin.core.Wallet.SendRequest

Here are the examples of the java api class com.google.bitcoin.core.Wallet.SendRequest taken from open source projects.

1. WalletTest#testCategory2WithChange()

Project: multibit
Source File: WalletTest.java
View license
@Test
public void testCategory2WithChange() throws Exception {
    // Specifically target case 2 with significant change
    // Make sure TestWithWallet isnt doing anything crazy.
    assertEquals(0, wallet.getTransactions(true).size());
    Address notMyAddr = new ECKey().toAddress(params);
    // Generate a ton of small outputs
    StoredBlock block = new StoredBlock(makeSolvedTestBlock(blockStore, notMyAddr), BigInteger.ONE, 1);
    int i = 0;
    while (i <= CENT.divide(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE.multiply(BigInteger.TEN)).longValue()) {
        Transaction tx = createFakeTxWithChangeAddress(params, Transaction.REFERENCE_DEFAULT_MIN_TX_FEE.multiply(BigInteger.TEN), myAddress, notMyAddr);
        // Keep every transaction unique
        tx.getInput(0).setSequenceNumber(i++);
        wallet.receiveFromBlock(tx, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, i);
    }
    // The selector will choose 2 with MIN_TX_FEE fee
    SendRequest request1 = SendRequest.to(notMyAddr, CENT.add(BigInteger.ONE));
    wallet.completeTx(request1);
    assertEquals(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE, request1.fee);
    // We should have spent all inputs
    assertEquals(request1.tx.getInputs().size(), i);
    // and gotten change back
    assertEquals(2, request1.tx.getOutputs().size());
}

2. WalletTest#testEmptyRandomWallet()

Project: multibit
Source File: WalletTest.java
View license
@Test
public void testEmptyRandomWallet() throws Exception {
    // Add a random set of outputs
    StoredBlock block = new StoredBlock(makeSolvedTestBlock(blockStore, new ECKey().toAddress(params)), BigInteger.ONE, 1);
    Random rng = new Random();
    for (int i = 0; i < rng.nextInt(100) + 1; i++) {
        Transaction tx = createFakeTx(params, BigInteger.valueOf(rng.nextInt((int) Utils.COIN.longValue())), myAddress);
        wallet.receiveFromBlock(tx, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, i);
    }
    SendRequest request = SendRequest.emptyWallet(new ECKey().toAddress(params));
    wallet.completeTx(request);
    wallet.commitTx(request.tx);
    assertEquals(BigInteger.ZERO, wallet.getBalance());
}

3. WalletTest#basicCategoryStepTest()

Project: multibit
Source File: WalletTest.java
View license
@Test
public void basicCategoryStepTest() throws Exception {
    // Creates spends that step through the possible fee solver categories
    SendRequest.DEFAULT_FEE_PER_KB = BigInteger.ZERO;
    // Make sure TestWithWallet isnt doing anything crazy.
    assertEquals(0, wallet.getTransactions(true).size());
    Address notMyAddr = new ECKey().toAddress(params);
    // Generate a ton of small outputs
    StoredBlock block = new StoredBlock(makeSolvedTestBlock(blockStore, notMyAddr), BigInteger.ONE, 1);
    int i = 0;
    while (i <= CENT.divide(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE).longValue()) {
        Transaction tx = createFakeTxWithChangeAddress(params, Transaction.REFERENCE_DEFAULT_MIN_TX_FEE, myAddress, notMyAddr);
        // Keep every transaction unique
        tx.getInput(0).setSequenceNumber(i++);
        wallet.receiveFromBlock(tx, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, i);
    }
    // Create a spend that will throw away change (category 3 type 2 in which the change causes fee which is worth more than change)
    SendRequest request1 = SendRequest.to(notMyAddr, CENT.add(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE).subtract(BigInteger.ONE));
    wallet.completeTx(request1);
    assertEquals(BigInteger.ONE, request1.fee);
    // We should have spent all inputs
    assertEquals(request1.tx.getInputs().size(), i);
    // Give us one more input...
    Transaction tx1 = createFakeTxWithChangeAddress(params, Transaction.REFERENCE_DEFAULT_MIN_TX_FEE, myAddress, notMyAddr);
    // Keep every transaction unique
    tx1.getInput(0).setSequenceNumber(i++);
    wallet.receiveFromBlock(tx1, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, i);
    // ... and create a spend that will throw away change (category 3 type 1 in which the change causes dust output)
    SendRequest request2 = SendRequest.to(notMyAddr, CENT.add(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE).subtract(BigInteger.ONE));
    wallet.completeTx(request2);
    assertEquals(BigInteger.ONE, request2.fee);
    // We should have spent all inputs - 1
    assertEquals(request2.tx.getInputs().size(), i - 1);
    // Give us one more input...
    Transaction tx2 = createFakeTxWithChangeAddress(params, Transaction.REFERENCE_DEFAULT_MIN_TX_FEE, myAddress, notMyAddr);
    // Keep every transaction unique
    tx2.getInput(0).setSequenceNumber(i++);
    wallet.receiveFromBlock(tx2, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, i);
    // ... and create a spend that will throw away change (category 3 type 1 in which the change causes dust output)
    // but that also could have been category 2 if it wanted
    SendRequest request3 = SendRequest.to(notMyAddr, CENT.add(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE).subtract(BigInteger.ONE));
    wallet.completeTx(request3);
    assertEquals(BigInteger.ONE, request3.fee);
    // We should have spent all inputs - 2
    assertEquals(request3.tx.getInputs().size(), i - 2);
    //
    SendRequest request4 = SendRequest.to(notMyAddr, CENT.add(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE).subtract(BigInteger.ONE));
    request4.feePerKb = Transaction.REFERENCE_DEFAULT_MIN_TX_FEE.divide(BigInteger.valueOf(request3.tx.bitcoinSerialize().length));
    wallet.completeTx(request4);
    assertEquals(BigInteger.ONE, request4.fee);
    // We should have spent all inputs - 2
    assertEquals(request4.tx.getInputs().size(), i - 2);
    // Give us a few more inputs...
    while (wallet.getBalance().compareTo(CENT.shiftLeft(1)) < 0) {
        Transaction tx3 = createFakeTxWithChangeAddress(params, Transaction.REFERENCE_DEFAULT_MIN_TX_FEE, myAddress, notMyAddr);
        // Keep every transaction unique
        tx3.getInput(0).setSequenceNumber(i++);
        wallet.receiveFromBlock(tx3, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, i);
    }
    // ...that is just slightly less than is needed for category 1
    SendRequest request5 = SendRequest.to(notMyAddr, CENT.add(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE).subtract(BigInteger.ONE));
    wallet.completeTx(request5);
    assertEquals(BigInteger.ONE, request5.fee);
    // We should have no change output
    assertEquals(1, request5.tx.getOutputs().size());
    // Give us one more input...
    Transaction tx4 = createFakeTxWithChangeAddress(params, Transaction.REFERENCE_DEFAULT_MIN_TX_FEE, myAddress, notMyAddr);
    // Keep every transaction unique
    tx4.getInput(0).setSequenceNumber(i);
    wallet.receiveFromBlock(tx4, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, i);
    // ... that puts us in category 1 (no fee!)
    SendRequest request6 = SendRequest.to(notMyAddr, CENT.add(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE).subtract(BigInteger.ONE));
    wallet.completeTx(request6);
    assertEquals(BigInteger.ZERO, request6.fee);
    // We should have a change output
    assertEquals(2, request6.tx.getOutputs().size());
    SendRequest.DEFAULT_FEE_PER_KB = Transaction.REFERENCE_DEFAULT_MIN_TX_FEE;
}

4. WalletTest#testCompleteTxWithExistingInputs()

Project: multibit
Source File: WalletTest.java
View license
@Test
public void testCompleteTxWithExistingInputs() throws Exception {
    // Tests calling completeTx with a SendRequest that already has a few inputs in it
    // Make sure TestWithWallet isnt doing anything crazy.
    assertEquals(0, wallet.getTransactions(true).size());
    Address notMyAddr = new ECKey().toAddress(params);
    // Generate a few outputs to us
    StoredBlock block = new StoredBlock(makeSolvedTestBlock(blockStore, notMyAddr), BigInteger.ONE, 1);
    Transaction tx1 = createFakeTx(params, Utils.COIN, myAddress);
    wallet.receiveFromBlock(tx1, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, 0);
    Transaction tx2 = createFakeTx(params, Utils.COIN, myAddress);
    assertTrue(!tx1.getHash().equals(tx2.getHash()));
    wallet.receiveFromBlock(tx2, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, 1);
    Transaction tx3 = createFakeTx(params, CENT, myAddress);
    wallet.receiveFromBlock(tx3, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, 2);
    SendRequest request1 = SendRequest.to(notMyAddr, CENT);
    // If we just complete as-is, we will use one of the COIN outputs to get higher priority,
    // resulting in a change output
    wallet.completeTx(request1);
    assertEquals(1, request1.tx.getInputs().size());
    assertEquals(2, request1.tx.getOutputs().size());
    assertEquals(CENT, request1.tx.getOutput(0).getValue());
    assertEquals(Utils.COIN.subtract(CENT), request1.tx.getOutput(1).getValue());
    // Now create an identical request2 and add an unsigned spend of the CENT output
    SendRequest request2 = SendRequest.to(notMyAddr, CENT);
    request2.tx.addInput(tx3.getOutput(0));
    // Now completeTx will result in one input, one output
    wallet.completeTx(request2);
    assertEquals(1, request2.tx.getInputs().size());
    assertEquals(1, request2.tx.getOutputs().size());
    assertEquals(CENT, request2.tx.getOutput(0).getValue());
    // Make sure it was properly signed
    request2.tx.getInput(0).getScriptSig().correctlySpends(request2.tx, 0, tx3.getOutput(0).getScriptPubKey(), true);
    // However, if there is no connected output, we will grab a COIN output anyway and add the CENT to fee
    SendRequest request3 = SendRequest.to(notMyAddr, CENT);
    request3.tx.addInput(new TransactionInput(params, request3.tx, new byte[] {}, new TransactionOutPoint(params, 0, tx3.getHash())));
    // Now completeTx will result in two inputs, two outputs and a fee of a CENT
    // Note that it is simply assumed that the inputs are correctly signed, though in fact the first is not
    wallet.completeTx(request3);
    assertEquals(2, request3.tx.getInputs().size());
    assertEquals(2, request3.tx.getOutputs().size());
    assertEquals(CENT, request3.tx.getOutput(0).getValue());
    assertEquals(Utils.COIN.subtract(CENT), request3.tx.getOutput(1).getValue());
    SendRequest request4 = SendRequest.to(notMyAddr, CENT);
    request4.tx.addInput(tx3.getOutput(0));
    // Now if we manually sign it, completeTx will not replace our signature
    request4.tx.signInputs(SigHash.ALL, wallet);
    byte[] scriptSig = request4.tx.getInput(0).getScriptBytes();
    wallet.completeTx(request4);
    assertEquals(1, request4.tx.getInputs().size());
    assertEquals(1, request4.tx.getOutputs().size());
    assertEquals(CENT, request4.tx.getOutput(0).getValue());
    assertArrayEquals(scriptSig, request4.tx.getInput(0).getScriptBytes());
}

5. WalletTest#feePerKbCategoryJumpTest()

Project: multibit
Source File: WalletTest.java
View license
@Test
public void feePerKbCategoryJumpTest() throws Exception {
    // Simple test of boundary condition on fee per kb in category fee solver
    // Make sure TestWithWallet isnt doing anything crazy.
    assertEquals(0, wallet.getTransactions(true).size());
    Address notMyAddr = new ECKey().toAddress(params);
    // Generate a ton of small outputs
    StoredBlock block = new StoredBlock(makeSolvedTestBlock(blockStore, notMyAddr), BigInteger.ONE, 1);
    Transaction tx = createFakeTx(params, Utils.COIN, myAddress);
    wallet.receiveFromBlock(tx, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, 0);
    Transaction tx2 = createFakeTx(params, CENT, myAddress);
    wallet.receiveFromBlock(tx2, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, 1);
    Transaction tx3 = createFakeTx(params, BigInteger.ONE, myAddress);
    wallet.receiveFromBlock(tx3, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, 2);
    // Create a transaction who's max size could be up to 1000 (if signatures were maximum size)
    SendRequest request1 = SendRequest.to(notMyAddr, Utils.COIN.subtract(CENT.multiply(BigInteger.valueOf(17))));
    for (int i = 0; i < 16; i++) request1.tx.addOutput(CENT, notMyAddr);
    request1.tx.addOutput(new TransactionOutput(params, request1.tx, CENT, new byte[16]));
    request1.fee = BigInteger.ONE;
    request1.feePerKb = BigInteger.ONE;
    // We get a category 2 using COIN+CENT
    // It spends COIN + 1(fee) and because its output is thus < CENT, we have to pay MIN_TX_FEE
    // When it tries category 1, its too large and requires COIN + 2 (fee)
    // This adds the next input, but still has a < CENT output which means it cant reach category 1
    wallet.completeTx(request1);
    assertEquals(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE, request1.fee);
    assertEquals(2, request1.tx.getInputs().size());
    // We then add one more satoshi output to the wallet
    Transaction tx4 = createFakeTx(params, BigInteger.ONE, myAddress);
    wallet.receiveFromBlock(tx4, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, 3);
    // Create a transaction who's max size could be up to 1000 (if signatures were maximum size)
    SendRequest request2 = SendRequest.to(notMyAddr, Utils.COIN.subtract(CENT.multiply(BigInteger.valueOf(17))));
    for (int i = 0; i < 16; i++) request2.tx.addOutput(CENT, notMyAddr);
    request2.tx.addOutput(new TransactionOutput(params, request2.tx, CENT, new byte[16]));
    request2.feePerKb = BigInteger.ONE;
    // The process is the same as above, but now we can complete category 1 with one more input, and pay a fee of 2
    wallet.completeTx(request2);
    assertEquals(BigInteger.valueOf(2), request2.fee);
    assertEquals(4, request2.tx.getInputs().size());
}

6. WalletTest#cleanupCommon()

Project: multibit
Source File: WalletTest.java
View license
private Transaction cleanupCommon(Address destination) throws Exception {
    receiveATransaction(wallet, myAddress);
    BigInteger v2 = toNanoCoins(0, 50);
    SendRequest req = SendRequest.to(destination, v2);
    req.fee = toNanoCoins(0, 1);
    wallet.completeTx(req);
    Transaction t2 = req.tx;
    // Broadcast the transaction and commit.
    broadcastAndCommit(wallet, t2);
    // At this point we have one pending and one spent
    BigInteger v1 = toNanoCoins(0, 10);
    Transaction t = sendMoneyToWallet(wallet, v1, myAddress, null);
    Threading.waitForUserCode();
    sendMoneyToWallet(wallet, t, null);
    assertEquals("Wrong number of PENDING.4", 2, wallet.getPoolSize(Pool.PENDING));
    assertEquals("Wrong number of UNSPENT.4", 0, wallet.getPoolSize(Pool.UNSPENT));
    assertEquals("Wrong number of ALL.4", 3, wallet.getTransactions(true).size());
    assertEquals(toNanoCoins(0, 59), wallet.getBalance(Wallet.BalanceType.ESTIMATED));
    // Now we have another incoming pending
    return t;
}

7. WalletTest#cleanupFailsDueToSpend()

Project: multibit
Source File: WalletTest.java
View license
@Test
public void cleanupFailsDueToSpend() throws Exception {
    Address destination = new ECKey().toAddress(params);
    Transaction t = cleanupCommon(destination);
    // Now we have another incoming pending.  Spend everything.
    BigInteger v3 = toNanoCoins(0, 58);
    SendRequest req = SendRequest.to(destination, v3);
    // Force selection of the incoming coin so that we can spend it
    req.coinSelector = new TestCoinSelector();
    req.fee = toNanoCoins(0, 1);
    wallet.completeTx(req);
    wallet.commitTx(req.tx);
    assertEquals("Wrong number of PENDING.5", 3, wallet.getPoolSize(WalletTransaction.Pool.PENDING));
    assertEquals("Wrong number of UNSPENT.5", 0, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
    assertEquals("Wrong number of ALL.5", 4, wallet.getTransactions(true).size());
    // Consider the new pending as risky and try to remove it from the wallet
    wallet.setRiskAnalyzer(new TestRiskAnalysis.Analyzer(t));
    wallet.cleanup();
    assertTrue(wallet.isConsistent());
    // The removal should have failed
    assertEquals("Wrong number of PENDING.5", 3, wallet.getPoolSize(WalletTransaction.Pool.PENDING));
    assertEquals("Wrong number of UNSPENT.5", 0, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
    assertEquals("Wrong number of ALL.5", 4, wallet.getTransactions(true).size());
    assertEquals(toNanoCoins(0, 0), wallet.getBalance(Wallet.BalanceType.ESTIMATED));
}

8. WalletTest#customTransactionSpending()

Project: multibit
Source File: WalletTest.java
View license
@Test
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.
    BigInteger v1 = Utils.toNanoCoins(3, 0);
    sendMoneyToWallet(v1, AbstractBlockChain.NewBlockType.BEST_CHAIN);
    assertEquals(v1, wallet.getBalance());
    assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
    assertEquals(1, wallet.getTransactions(true).size());
    ECKey k2 = new ECKey();
    Address a2 = k2.toAddress(params);
    BigInteger v2 = toNanoCoins(0, 50);
    BigInteger v3 = toNanoCoins(0, 75);
    BigInteger v4 = toNanoCoins(1, 25);
    Transaction t2 = new Transaction(params);
    t2.addOutput(v2, a2);
    t2.addOutput(v3, a2);
    t2.addOutput(v4, a2);
    SendRequest req = SendRequest.forTx(t2);
    req.ensureMinRequiredFee = false;
    wallet.completeTx(req);
    // Do some basic sanity checks.
    assertEquals(1, t2.getInputs().size());
    assertEquals(myAddress, t2.getInputs().get(0).getScriptSig().getFromAddress(params));
    assertEquals(TransactionConfidence.ConfidenceType.UNKNOWN, t2.getConfidence().getConfidenceType());
    // We have NOT proven that the signature is correct!
    wallet.commitTx(t2);
    assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.PENDING));
    assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.SPENT));
    assertEquals(2, wallet.getTransactions(true).size());
}

9. WalletTest#spendOutputFromPendingTransaction()

Project: multibit
Source File: WalletTest.java
View license
//    @Test
//    public void autosaveImmediate() throws Exception {
//        // Test that the wallet will save itself automatically when it changes.
//        File f = File.createTempFile("bitcoinj-unit-test", null);
//        Sha256Hash hash1 = Sha256Hash.hashFileContents(f);
//        // Start with zero delay and ensure the wallet file changes after adding a key.
//        wallet.autosaveToFile(f, 0, TimeUnit.SECONDS, null);
//        ECKey key = new ECKey();
//        wallet.addKey(key);
//        Sha256Hash hash2 = Sha256Hash.hashFileContents(f);
//        assertFalse("Wallet not saved after addKey", hash1.equals(hash2));  // File has changed.
//
//        Transaction t1 = createFakeTx(params, toNanoCoins(5, 0), key);
//        if (wallet.isPendingTransactionRelevant(t1))
//            wallet.receivePending(t1, null);
//        Sha256Hash hash3 = Sha256Hash.hashFileContents(f);
//        assertFalse("Wallet not saved after receivePending", hash2.equals(hash3));  // File has changed again.
//    }
//    @Test
//    public void autosaveDelayed() throws Exception {
//        // Test that the wallet will save itself automatically when it changes, but not immediately and near-by
//        // updates are coalesced together. This test is a bit racy, it assumes we can complete the unit test within
//        // an auto-save cycle of 1 second.
//        final File[] results = new File[2];
//        final CountDownLatch latch = new CountDownLatch(3);
//        File f = File.createTempFile("bitcoinj-unit-test", null);
//        Sha256Hash hash1 = Sha256Hash.hashFileContents(f);
//        wallet.autosaveToFile(f, 1, TimeUnit.SECONDS,
//                new WalletFiles.Listener() {
//                    public void onBeforeAutoSave(File tempFile) {
//                        results[0] = tempFile;
//                    }
//
//                    public void onAfterAutoSave(File newlySavedFile) {
//                        results[1] = newlySavedFile;
//                        latch.countDown();
//                    }
//                }
//        );
//        ECKey key = new ECKey();
//        wallet.addKey(key);
//        Sha256Hash hash2 = Sha256Hash.hashFileContents(f);
//        assertFalse(hash1.equals(hash2));  // File has changed immediately despite the delay, as keys are important.
//        assertNotNull(results[0]);
//        assertEquals(f, results[1]);
//        results[0] = results[1] = null;
//
//        Block b0 = createFakeBlock(blockStore).block;
//        chain.add(b0);
//        Sha256Hash hash3 = Sha256Hash.hashFileContents(f);
//        assertEquals(hash2, hash3);  // File has NOT changed yet. Just new blocks with no txns - delayed.
//        assertNull(results[0]);
//        assertNull(results[1]);
//
//        Transaction t1 = createFakeTx(params, toNanoCoins(5, 0), key);
//        Block b1 = createFakeBlock(blockStore, t1).block;
//        chain.add(b1);
//        Sha256Hash hash4 = Sha256Hash.hashFileContents(f);
//        assertFalse(hash3.equals(hash4));  // File HAS changed.
//        results[0] = results[1] = null;
//
//        // A block that contains some random tx we don't care about.
//        Block b2 = b1.createNextBlock(new ECKey().toAddress(params));
//        chain.add(b2);
//        assertEquals(hash4, Sha256Hash.hashFileContents(f));  // File has NOT changed.
//        assertNull(results[0]);
//        assertNull(results[1]);
//
//        // Wait for an auto-save to occur.
//        latch.await();
//        assertFalse(hash4.equals(Sha256Hash.hashFileContents(f)));  // File has now changed.
//        assertNotNull(results[0]);
//        assertEquals(f, results[1]);
//    }
@Test
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.
    BigInteger v1 = Utils.toNanoCoins(1, 0);
    sendMoneyToWallet(v1, AbstractBlockChain.NewBlockType.BEST_CHAIN);
    // First create our current transaction
    ECKey k2 = new ECKey();
    wallet.addKey(k2);
    BigInteger v2 = toNanoCoins(0, 50);
    Transaction t2 = new Transaction(params);
    TransactionOutput o2 = new TransactionOutput(params, t2, v2, k2.toAddress(params));
    t2.addOutput(o2);
    SendRequest req = SendRequest.forTx(t2);
    req.ensureMinRequiredFee = false;
    wallet.completeTx(req);
    // Commit t2, so it is placed in the pending pool
    wallet.commitTx(t2);
    assertEquals(0, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
    assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.PENDING));
    assertEquals(2, wallet.getTransactions(true).size());
    // Now try to the spend the output.
    ECKey k3 = new ECKey();
    BigInteger v3 = toNanoCoins(0, 25);
    Transaction t3 = new Transaction(params);
    t3.addOutput(v3, k3.toAddress(params));
    t3.addInput(o2);
    t3.signInputs(SigHash.ALL, wallet);
    // Commit t3, so the coins from the pending t2 are spent
    wallet.commitTx(t3);
    assertEquals(0, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
    assertEquals(2, wallet.getPoolSize(WalletTransaction.Pool.PENDING));
    assertEquals(3, wallet.getTransactions(true).size());
    // Now the output of t2 must not be available for spending
    assertFalse(o2.isAvailableForSpending());
}

10. WalletTest#testEmptyWallet()

Project: multibit
Source File: WalletTest.java
View license
@Test
public void testEmptyWallet() throws Exception {
    Address outputKey = new ECKey().toAddress(params);
    // Add exactly 0.01
    StoredBlock block = new StoredBlock(makeSolvedTestBlock(blockStore, outputKey), BigInteger.ONE, 1);
    Transaction tx = createFakeTx(params, CENT, myAddress);
    wallet.receiveFromBlock(tx, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, 0);
    SendRequest request = SendRequest.emptyWallet(outputKey);
    wallet.completeTx(request);
    wallet.commitTx(request.tx);
    assertEquals(BigInteger.ZERO, wallet.getBalance());
    assertEquals(CENT, request.tx.getOutput(0).getValue());
    // Add 1 confirmed cent and 1 unconfirmed cent. Verify only one cent is emptied because of the coin selection
    // policies that are in use by default.
    block = new StoredBlock(makeSolvedTestBlock(blockStore, outputKey), BigInteger.ONE, 1);
    tx = createFakeTx(params, CENT, myAddress);
    wallet.receiveFromBlock(tx, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, 0);
    tx = createFakeTx(params, CENT, myAddress);
    wallet.receivePending(tx, null);
    request = SendRequest.emptyWallet(outputKey);
    wallet.completeTx(request);
    wallet.commitTx(request.tx);
    assertEquals(BigInteger.ZERO, wallet.getBalance());
    assertEquals(CENT, request.tx.getOutput(0).getValue());
    // Add just under 0.01
    StoredBlock block2 = new StoredBlock(block.getHeader().createNextBlock(outputKey), BigInteger.ONE, 2);
    tx = createFakeTx(params, CENT.subtract(BigInteger.ONE), myAddress);
    wallet.receiveFromBlock(tx, block2, AbstractBlockChain.NewBlockType.BEST_CHAIN, 0);
    request = SendRequest.emptyWallet(outputKey);
    wallet.completeTx(request);
    wallet.commitTx(request.tx);
    assertEquals(BigInteger.ZERO, wallet.getBalance());
    assertEquals(CENT.subtract(BigInteger.ONE).subtract(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE), request.tx.getOutput(0).getValue());
    // Add an unsendable value
    StoredBlock block3 = new StoredBlock(block2.getHeader().createNextBlock(outputKey), BigInteger.ONE, 3);
    BigInteger outputValue = Transaction.MIN_NONDUST_OUTPUT.add(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE).subtract(BigInteger.ONE);
    tx = createFakeTx(params, outputValue, myAddress);
    wallet.receiveFromBlock(tx, block3, AbstractBlockChain.NewBlockType.BEST_CHAIN, 0);
    request = SendRequest.emptyWallet(outputKey);
    try {
        wallet.completeTx(request);
        fail();
    } catch (InsufficientMoneyException.CouldNotAdjustDownwards e) {
    }
    request.ensureMinRequiredFee = false;
    wallet.completeTx(request);
    wallet.commitTx(request.tx);
    assertEquals(BigInteger.ZERO, wallet.getBalance());
    assertEquals(outputValue, request.tx.getOutput(0).getValue());
}