org.bitcoinj.wallet.Wallet.BalanceType Java Examples

The following examples show how to use org.bitcoinj.wallet.Wallet.BalanceType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WalletTest.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
private Transaction cleanupCommon(Address destination) throws Exception {
    receiveATransaction(wallet, myAddress);

    Coin v2 = valueOf(0, 50);
    SendRequest req = SendRequest.to(destination, v2);
    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

    Coin v1 = valueOf(0, 10);
    Transaction t = sendMoneyToWallet(null, v1, myAddress);
    Threading.waitForUserCode();
    sendMoneyToWallet(null, t);
    assertEquals("Wrong number of PENDING", 2, wallet.getPoolSize(Pool.PENDING));
    assertEquals("Wrong number of UNSPENT", 0, wallet.getPoolSize(Pool.UNSPENT));
    assertEquals("Wrong number of ALL", 3, wallet.getTransactions(true).size());
    assertEquals(valueOf(0, 60), wallet.getBalance(Wallet.BalanceType.ESTIMATED));

    // Now we have another incoming pending
    return t;
}
 
Example #2
Source File: WalletTest.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void outOfOrderPendingTxns() throws Exception {
    // Check that if there are two pending transactions which we receive out of order, they are marked as spent
    // correctly. For instance, we are watching a wallet, someone pays us (A) and we then pay someone else (B)
    // with a change address but the network delivers the transactions to us in order B then A.
    Coin value = COIN;
    Transaction a = createFakeTx(PARAMS, value, myAddress);
    Transaction b = new Transaction(PARAMS);
    b.addInput(a.getOutput(0));
    b.addOutput(CENT, OTHER_ADDRESS);
    Coin v = COIN.subtract(CENT);
    b.addOutput(v, wallet.currentChangeAddress());
    a = roundTripTransaction(PARAMS, a);
    b = roundTripTransaction(PARAMS, b);
    wallet.receivePending(b, null);
    assertEquals(v, wallet.getBalance(Wallet.BalanceType.ESTIMATED));
    wallet.receivePending(a, null);
    assertEquals(v, wallet.getBalance(Wallet.BalanceType.ESTIMATED));
}
 
Example #3
Source File: WalletTest.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void pubkeyOnlyScripts() throws Exception {
    // Verify that we support outputs like OP_PUBKEY and the corresponding inputs.
    ECKey key1 = wallet.freshReceiveKey();
    Coin value = valueOf(5, 0);
    Transaction t1 = createFakeTx(PARAMS, value, key1);
    if (wallet.isPendingTransactionRelevant(t1))
        wallet.receivePending(t1, null);
    // TX should have been seen as relevant.
    assertEquals(value, wallet.getBalance(Wallet.BalanceType.ESTIMATED));
    assertEquals(ZERO, wallet.getBalance(Wallet.BalanceType.AVAILABLE));
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, t1);
    // TX should have been seen as relevant, extracted and processed.
    assertEquals(value, wallet.getBalance(Wallet.BalanceType.AVAILABLE));
    // Spend it and ensure we can spend the <key> OP_CHECKSIG output correctly.
    Transaction t2 = wallet.createSend(OTHER_ADDRESS, value);
    assertNotNull(t2);
    // TODO: This code is messy, improve the Script class and fixinate!
    assertEquals(t2.toString(), 1, t2.getInputs().get(0).getScriptSig().getChunks().size());
    assertTrue(t2.getInputs().get(0).getScriptSig().getChunks().get(0).data.length > 50);
}
 
Example #4
Source File: WalletTest.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void childPaysForParent() throws Exception {
    // Receive confirmed balance to play with.
    Transaction toMe = createFakeTxWithoutChangeAddress(PARAMS, COIN, myAddress);
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, toMe);
    assertEquals(Coin.COIN, wallet.getBalance(BalanceType.ESTIMATED_SPENDABLE));
    assertEquals(Coin.COIN, wallet.getBalance(BalanceType.AVAILABLE_SPENDABLE));
    // Receive unconfirmed coin without fee.
    Transaction toMeWithoutFee = createFakeTxWithoutChangeAddress(PARAMS, COIN, myAddress);
    wallet.receivePending(toMeWithoutFee, null);
    assertEquals(Coin.COIN.multiply(2), wallet.getBalance(BalanceType.ESTIMATED_SPENDABLE));
    assertEquals(Coin.COIN, wallet.getBalance(BalanceType.AVAILABLE_SPENDABLE));
    // Craft a child-pays-for-parent transaction.
    final Coin feeRaise = MILLICOIN;
    final SendRequest sendRequest = SendRequest.childPaysForParent(wallet, toMeWithoutFee, feeRaise);
    wallet.signTransaction(sendRequest);
    wallet.commitTx(sendRequest.tx);
    assertEquals(Transaction.Purpose.RAISE_FEE, sendRequest.tx.getPurpose());
    assertEquals(Coin.COIN.multiply(2).subtract(feeRaise), wallet.getBalance(BalanceType.ESTIMATED_SPENDABLE));
    assertEquals(Coin.COIN, wallet.getBalance(BalanceType.AVAILABLE_SPENDABLE));
}
 
Example #5
Source File: WalletTool.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
public OutputSpec(String spec) throws IllegalArgumentException {
    String[] parts = spec.split(":");
    if (parts.length != 2) {
        throw new IllegalArgumentException("Malformed output specification, must have two parts separated by :");
    }
    String destination = parts[0];
    if ("ALL".equalsIgnoreCase(parts[1]))
        value = wallet.getBalance(BalanceType.ESTIMATED);
    else
        value = parseCoin(parts[1]);
    if (destination.startsWith("0")) {
        // Treat as a raw public key.
        byte[] pubKey = new BigInteger(destination, 16).toByteArray();
        key = ECKey.fromPublicOnly(pubKey);
        addr = null;
    } else {
        // Treat as an address.
        addr = Address.fromBase58(params, destination);
        key = null;
    }
}
 
Example #6
Source File: WalletTest.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void sideChain() throws Exception {
    // 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.
    Coin v1 = COIN;
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, v1);
    assertEquals(v1, wallet.getBalance());
    assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
    assertEquals(1, wallet.getTransactions(true).size());

    Coin v2 = valueOf(0, 50);
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.SIDE_CHAIN, v2);
    assertEquals(2, wallet.getTransactions(true).size());
    assertEquals(v1, wallet.getBalance());
    assertEquals(v1.add(v2), wallet.getBalance(Wallet.BalanceType.ESTIMATED));
}
 
Example #7
Source File: WalletTest.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
private Transaction cleanupCommon(Address destination) throws Exception {
    receiveATransaction(wallet, myAddress);

    Coin v2 = valueOf(0, 50);
    SendRequest req = SendRequest.to(destination, v2);
    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

    Coin v1 = valueOf(0, 10);
    Transaction t = sendMoneyToWallet(null, v1, myAddress);
    Threading.waitForUserCode();
    sendMoneyToWallet(null, t);
    assertEquals("Wrong number of PENDING", 2, wallet.getPoolSize(Pool.PENDING));
    assertEquals("Wrong number of UNSPENT", 0, wallet.getPoolSize(Pool.UNSPENT));
    assertEquals("Wrong number of ALL", 3, wallet.getTransactions(true).size());
    assertEquals(valueOf(0, 60), wallet.getBalance(Wallet.BalanceType.ESTIMATED));

    // Now we have another incoming pending
    return t;
}
 
Example #8
Source File: WalletTest.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
private Transaction cleanupCommon(Address destination) throws Exception {
    receiveATransaction(wallet, myAddress);

    Coin v2 = valueOf(0, 50);
    SendRequest req = SendRequest.to(destination, v2);
    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

    Coin v1 = valueOf(0, 10);
    Transaction t = sendMoneyToWallet(null, v1, myAddress);
    Threading.waitForUserCode();
    sendMoneyToWallet(null, t);
    assertEquals("Wrong number of PENDING", 2, wallet.getPoolSize(Pool.PENDING));
    assertEquals("Wrong number of UNSPENT", 0, wallet.getPoolSize(Pool.UNSPENT));
    assertEquals("Wrong number of ALL", 3, wallet.getTransactions(true).size());
    assertEquals(valueOf(0, 60), wallet.getBalance(Wallet.BalanceType.ESTIMATED));

    // Now we have another incoming pending
    return t;
}
 
Example #9
Source File: WalletTool.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
public OutputSpec(String spec) throws IllegalArgumentException {
    String[] parts = spec.split(":");
    if (parts.length != 2) {
        throw new IllegalArgumentException("Malformed output specification, must have two parts separated by :");
    }
    String destination = parts[0];
    if ("ALL".equalsIgnoreCase(parts[1]))
        value = wallet.getBalance(BalanceType.ESTIMATED);
    else
        value = parseCoin(parts[1]);
    if (destination.startsWith("0")) {
        // Treat as a raw public key.
        byte[] pubKey = new BigInteger(destination, 16).toByteArray();
        key = ECKey.fromPublicOnly(pubKey);
        addr = null;
    } else {
        // Treat as an address.
        addr = Address.fromBase58(params, destination);
        key = null;
    }
}
 
Example #10
Source File: WalletTest.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void sideChain() throws Exception {
    // 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.
    Coin v1 = COIN;
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, v1);
    assertEquals(v1, wallet.getBalance());
    assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
    assertEquals(1, wallet.getTransactions(true).size());

    Coin v2 = valueOf(0, 50);
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.SIDE_CHAIN, v2);
    assertEquals(2, wallet.getTransactions(true).size());
    assertEquals(v1, wallet.getBalance());
    assertEquals(v1.add(v2), wallet.getBalance(Wallet.BalanceType.ESTIMATED));
}
 
Example #11
Source File: WalletTest.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void childPaysForParent() throws Exception {
    // Receive confirmed balance to play with.
    Transaction toMe = createFakeTxWithoutChangeAddress(UNITTEST, COIN, myAddress);
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, toMe);
    assertEquals(Coin.COIN, wallet.getBalance(BalanceType.ESTIMATED_SPENDABLE));
    assertEquals(Coin.COIN, wallet.getBalance(BalanceType.AVAILABLE_SPENDABLE));
    // Receive unconfirmed coin without fee.
    Transaction toMeWithoutFee = createFakeTxWithoutChangeAddress(UNITTEST, COIN, myAddress);
    wallet.receivePending(toMeWithoutFee, null);
    assertEquals(Coin.COIN.multiply(2), wallet.getBalance(BalanceType.ESTIMATED_SPENDABLE));
    assertEquals(Coin.COIN, wallet.getBalance(BalanceType.AVAILABLE_SPENDABLE));
    // Craft a child-pays-for-parent transaction.
    final Coin feeRaise = MILLICOIN;
    final SendRequest sendRequest = SendRequest.childPaysForParent(wallet, toMeWithoutFee, feeRaise);
    wallet.signTransaction(sendRequest);
    wallet.commitTx(sendRequest.tx);
    assertEquals(Transaction.Purpose.RAISE_FEE, sendRequest.tx.getPurpose());
    assertEquals(Coin.COIN.multiply(2).subtract(feeRaise), wallet.getBalance(BalanceType.ESTIMATED_SPENDABLE));
    assertEquals(Coin.COIN, wallet.getBalance(BalanceType.AVAILABLE_SPENDABLE));
}
 
Example #12
Source File: WalletTest.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void outOfOrderPendingTxns() throws Exception {
    // Check that if there are two pending transactions which we receive out of order, they are marked as spent
    // correctly. For instance, we are watching a wallet, someone pays us (A) and we then pay someone else (B)
    // with a change address but the network delivers the transactions to us in order B then A.
    Coin value = COIN;
    Transaction a = createFakeTx(UNITTEST, value, myAddress);
    Transaction b = new Transaction(UNITTEST);
    b.addInput(a.getOutput(0));
    b.addOutput(CENT, OTHER_ADDRESS);
    Coin v = COIN.subtract(CENT);
    b.addOutput(v, wallet.currentChangeAddress());
    a = roundTripTransaction(UNITTEST, a);
    b = roundTripTransaction(UNITTEST, b);
    wallet.receivePending(b, null);
    assertEquals(v, wallet.getBalance(Wallet.BalanceType.ESTIMATED));
    wallet.receivePending(a, null);
    assertEquals(v, wallet.getBalance(Wallet.BalanceType.ESTIMATED));
}
 
Example #13
Source File: WalletTest.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void pubkeyOnlyScripts() throws Exception {
    // Verify that we support outputs like OP_PUBKEY and the corresponding inputs.
    ECKey key1 = wallet.freshReceiveKey();
    Coin value = valueOf(5, 0);
    Transaction t1 = createFakeTx(UNITTEST, value, key1);
    if (wallet.isPendingTransactionRelevant(t1))
        wallet.receivePending(t1, null);
    // TX should have been seen as relevant.
    assertEquals(value, wallet.getBalance(Wallet.BalanceType.ESTIMATED));
    assertEquals(ZERO, wallet.getBalance(Wallet.BalanceType.AVAILABLE));
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, t1);
    // TX should have been seen as relevant, extracted and processed.
    assertEquals(value, wallet.getBalance(Wallet.BalanceType.AVAILABLE));
    // Spend it and ensure we can spend the <key> OP_CHECKSIG output correctly.
    Transaction t2 = wallet.createSend(OTHER_ADDRESS, value);
    assertNotNull(t2);
    // TODO: This code is messy, improve the Script class and fixinate!
    assertEquals(t2.toString(), 1, t2.getInputs().get(0).getScriptSig().getChunks().size());
    assertTrue(t2.getInputs().get(0).getScriptSig().getChunks().get(0).data.length > 50);
}
 
Example #14
Source File: WalletTest.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void pubkeyOnlyScripts() throws Exception {
    // Verify that we support outputs like OP_PUBKEY and the corresponding inputs.
    ECKey key1 = wallet.freshReceiveKey();
    Coin value = valueOf(5, 0);
    Transaction t1 = createFakeTx(PARAMS, value, key1);
    if (wallet.isPendingTransactionRelevant(t1))
        wallet.receivePending(t1, null);
    // TX should have been seen as relevant.
    assertEquals(value, wallet.getBalance(Wallet.BalanceType.ESTIMATED));
    assertEquals(ZERO, wallet.getBalance(Wallet.BalanceType.AVAILABLE));
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, t1);
    // TX should have been seen as relevant, extracted and processed.
    assertEquals(value, wallet.getBalance(Wallet.BalanceType.AVAILABLE));
    // Spend it and ensure we can spend the <key> OP_CHECKSIG output correctly.
    Transaction t2 = wallet.createSend(OTHER_ADDRESS, value);
    assertNotNull(t2);
    // TODO: This code is messy, improve the Script class and fixinate!
    assertEquals(t2.toString(), 1, t2.getInputs().get(0).getScriptSig().getChunks().size());
    assertTrue(t2.getInputs().get(0).getScriptSig().getChunks().get(0).data.length > 50);
}
 
Example #15
Source File: WalletTest.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void outOfOrderPendingTxns() throws Exception {
    // Check that if there are two pending transactions which we receive out of order, they are marked as spent
    // correctly. For instance, we are watching a wallet, someone pays us (A) and we then pay someone else (B)
    // with a change address but the network delivers the transactions to us in order B then A.
    Coin value = COIN;
    Transaction a = createFakeTx(PARAMS, value, myAddress);
    Transaction b = new Transaction(PARAMS);
    b.addInput(a.getOutput(0));
    b.addOutput(CENT, OTHER_ADDRESS);
    Coin v = COIN.subtract(CENT);
    b.addOutput(v, wallet.currentChangeAddress());
    a = roundTripTransaction(PARAMS, a);
    b = roundTripTransaction(PARAMS, b);
    wallet.receivePending(b, null);
    assertEquals(v, wallet.getBalance(Wallet.BalanceType.ESTIMATED));
    wallet.receivePending(a, null);
    assertEquals(v, wallet.getBalance(Wallet.BalanceType.ESTIMATED));
}
 
Example #16
Source File: WalletTest.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void sideChain() throws Exception {
    // 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.
    Coin v1 = COIN;
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, v1);
    assertEquals(v1, wallet.getBalance());
    assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
    assertEquals(1, wallet.getTransactions(true).size());

    Coin v2 = valueOf(0, 50);
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.SIDE_CHAIN, v2);
    assertEquals(2, wallet.getTransactions(true).size());
    assertEquals(v1, wallet.getBalance());
    assertEquals(v1.add(v2), wallet.getBalance(Wallet.BalanceType.ESTIMATED));
}
 
Example #17
Source File: WalletTest.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void childPaysForParent() throws Exception {
    // Receive confirmed balance to play with.
    Transaction toMe = createFakeTxWithoutChangeAddress(PARAMS, COIN, myAddress);
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, toMe);
    assertEquals(Coin.COIN, wallet.getBalance(BalanceType.ESTIMATED_SPENDABLE));
    assertEquals(Coin.COIN, wallet.getBalance(BalanceType.AVAILABLE_SPENDABLE));
    // Receive unconfirmed coin without fee.
    Transaction toMeWithoutFee = createFakeTxWithoutChangeAddress(PARAMS, COIN, myAddress);
    wallet.receivePending(toMeWithoutFee, null);
    assertEquals(Coin.COIN.multiply(2), wallet.getBalance(BalanceType.ESTIMATED_SPENDABLE));
    assertEquals(Coin.COIN, wallet.getBalance(BalanceType.AVAILABLE_SPENDABLE));
    // Craft a child-pays-for-parent transaction.
    final Coin feeRaise = MILLICOIN;
    final SendRequest sendRequest = SendRequest.childPaysForParent(wallet, toMeWithoutFee, feeRaise);
    wallet.signTransaction(sendRequest);
    wallet.commitTx(sendRequest.tx);
    assertEquals(Transaction.Purpose.RAISE_FEE, sendRequest.tx.getPurpose());
    assertEquals(Coin.COIN.multiply(2).subtract(feeRaise), wallet.getBalance(BalanceType.ESTIMATED_SPENDABLE));
    assertEquals(Coin.COIN, wallet.getBalance(BalanceType.AVAILABLE_SPENDABLE));
}
 
Example #18
Source File: WalletTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void balance() throws Exception {
    // Receive 5 coins then half a coin.
    Coin v1 = valueOf(5, 0);
    Coin v2 = valueOf(0, 50);
    Coin expected = valueOf(5, 50);
    assertEquals(0, wallet.getTransactions(true).size());
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, v1);
    assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, v2);
    assertEquals(2, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
    assertEquals(expected, wallet.getBalance());

    // Now spend one coin.
    Coin v3 = COIN;
    Transaction spend = wallet.createSend(OTHER_ADDRESS, v3);
    wallet.commitTx(spend);
    assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.PENDING));

    // Available and estimated balances should not be the same. We don't check the exact available balance here
    // because it depends on the coin selection algorithm.
    assertEquals(valueOf(4, 50), wallet.getBalance(Wallet.BalanceType.ESTIMATED));
    assertFalse(wallet.getBalance(Wallet.BalanceType.AVAILABLE).equals(
                wallet.getBalance(Wallet.BalanceType.ESTIMATED)));

    // Now confirm the transaction by including it into a block.
    sendMoneyToWallet(BlockChain.NewBlockType.BEST_CHAIN, spend);

    // Change is confirmed. We started with 5.50 so we should have 4.50 left.
    Coin v4 = valueOf(4, 50);
    assertEquals(v4, wallet.getBalance(Wallet.BalanceType.AVAILABLE));
}
 
Example #19
Source File: WalletTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
private void receiveATransactionAmount(Wallet wallet, Address toAddress, Coin amount) {
    final ListenableFuture<Coin> availFuture = wallet.getBalanceFuture(amount, Wallet.BalanceType.AVAILABLE);
    final ListenableFuture<Coin> estimatedFuture = wallet.getBalanceFuture(amount, Wallet.BalanceType.ESTIMATED);
    assertFalse(availFuture.isDone());
    assertFalse(estimatedFuture.isDone());
    // Send some pending coins to the wallet.
    Transaction t1 = sendMoneyToWallet(wallet, null, amount, toAddress);
    Threading.waitForUserCode();
    final ListenableFuture<TransactionConfidence> depthFuture = t1.getConfidence().getDepthFuture(1);
    assertFalse(depthFuture.isDone());
    assertEquals(ZERO, wallet.getBalance());
    assertEquals(amount, wallet.getBalance(Wallet.BalanceType.ESTIMATED));
    assertFalse(availFuture.isDone());
    // Our estimated balance has reached the requested level.
    assertTrue(estimatedFuture.isDone());
    assertEquals(1, wallet.getPoolSize(Pool.PENDING));
    assertEquals(0, wallet.getPoolSize(Pool.UNSPENT));
    // Confirm the coins.
    sendMoneyToWallet(wallet, AbstractBlockChain.NewBlockType.BEST_CHAIN, t1);
    assertEquals("Incorrect confirmed tx balance", amount, wallet.getBalance());
    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 ALL pool size", 1, wallet.getTransactions(true).size());
    Threading.waitForUserCode();
    assertTrue(availFuture.isDone());
    assertTrue(estimatedFuture.isDone());
    assertTrue(depthFuture.isDone());
}
 
Example #20
Source File: WalletTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void cleanupFailsDueToSpend() throws Exception {
    Transaction t = cleanupCommon(OTHER_ADDRESS);

    // Now we have another incoming pending.  Spend everything.
    Coin v3 = valueOf(0, 60);
    SendRequest req = SendRequest.to(OTHER_ADDRESS, v3);

    // Force selection of the incoming coin so that we can spend it
    req.coinSelector = new TestCoinSelector();

    wallet.completeTx(req);
    wallet.commitTx(req.tx);

    assertEquals("Wrong number of PENDING", 3, wallet.getPoolSize(WalletTransaction.Pool.PENDING));
    assertEquals("Wrong number of UNSPENT", 0, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
    assertEquals("Wrong number of ALL", 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", 3, wallet.getPoolSize(WalletTransaction.Pool.PENDING));
    assertEquals("Wrong number of UNSPENT", 0, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
    assertEquals("Wrong number of ALL", 4, wallet.getTransactions(true).size());
    assertEquals(ZERO, wallet.getBalance(Wallet.BalanceType.ESTIMATED));
}
 
Example #21
Source File: WalletTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void balanceWithIdenticalOutputs() {
    assertEquals(Coin.ZERO, wallet.getBalance(BalanceType.ESTIMATED));
    Transaction tx = new Transaction(PARAMS);
    tx.addOutput(Coin.COIN, myAddress);
    tx.addOutput(Coin.COIN, myAddress); // identical to the above
    wallet.addWalletTransaction(new WalletTransaction(Pool.UNSPENT, tx));
    assertEquals(Coin.COIN.plus(Coin.COIN), wallet.getBalance(BalanceType.ESTIMATED));
}
 
Example #22
Source File: WalletTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void cleanup() throws Exception {
    Transaction t = cleanupCommon(OTHER_ADDRESS);

    // Consider the new pending as risky and remove it from the wallet
    wallet.setRiskAnalyzer(new TestRiskAnalysis.Analyzer(t));

    wallet.cleanup();
    assertTrue(wallet.isConsistent());
    assertEquals("Wrong number of PENDING", 1, wallet.getPoolSize(WalletTransaction.Pool.PENDING));
    assertEquals("Wrong number of UNSPENT", 0, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
    assertEquals("Wrong number of ALL", 2, wallet.getTransactions(true).size());
    assertEquals(valueOf(0, 50), wallet.getBalance(Wallet.BalanceType.ESTIMATED));
}
 
Example #23
Source File: WalletTool.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
static synchronized void onChange(final CountDownLatch latch) {
    saveWallet(walletFile);
    Coin balance = wallet.getBalance(Wallet.BalanceType.ESTIMATED);
    if (condition.matchBitcoins(balance)) {
        System.out.println(balance.toFriendlyString());
        latch.countDown();
    }
}
 
Example #24
Source File: WalletTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void reset() {
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN, myAddress);
    assertNotEquals(Coin.ZERO, wallet.getBalance(Wallet.BalanceType.ESTIMATED));
    assertNotEquals(0, wallet.getTransactions(false).size());
    assertNotEquals(0, wallet.getUnspents().size());
    wallet.reset();
    assertEquals(Coin.ZERO, wallet.getBalance(Wallet.BalanceType.ESTIMATED));
    assertEquals(0, wallet.getTransactions(false).size());
    assertEquals(0, wallet.getUnspents().size());
}
 
Example #25
Source File: WalletTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void reset() {
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN, myAddress);
    assertNotEquals(Coin.ZERO, wallet.getBalance(Wallet.BalanceType.ESTIMATED));
    assertNotEquals(0, wallet.getTransactions(false).size());
    assertNotEquals(0, wallet.getUnspents().size());
    wallet.reset();
    assertEquals(Coin.ZERO, wallet.getBalance(Wallet.BalanceType.ESTIMATED));
    assertEquals(0, wallet.getTransactions(false).size());
    assertEquals(0, wallet.getUnspents().size());
}
 
Example #26
Source File: WalletTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void cleanup() throws Exception {
    Transaction t = cleanupCommon(OTHER_ADDRESS);

    // Consider the new pending as risky and remove it from the wallet
    wallet.setRiskAnalyzer(new TestRiskAnalysis.Analyzer(t));

    wallet.cleanup();
    assertTrue(wallet.isConsistent());
    assertEquals("Wrong number of PENDING", 1, wallet.getPoolSize(WalletTransaction.Pool.PENDING));
    assertEquals("Wrong number of UNSPENT", 0, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
    assertEquals("Wrong number of ALL", 2, wallet.getTransactions(true).size());
    assertEquals(valueOf(0, 50), wallet.getBalance(Wallet.BalanceType.ESTIMATED));
}
 
Example #27
Source File: WalletTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void balanceWithIdenticalOutputs() {
    assertEquals(Coin.ZERO, wallet.getBalance(BalanceType.ESTIMATED));
    Transaction tx = new Transaction(PARAMS);
    tx.addOutput(Coin.COIN, myAddress);
    tx.addOutput(Coin.COIN, myAddress); // identical to the above
    wallet.addWalletTransaction(new WalletTransaction(Pool.UNSPENT, tx));
    assertEquals(Coin.COIN.plus(Coin.COIN), wallet.getBalance(BalanceType.ESTIMATED));
}
 
Example #28
Source File: WalletTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void cleanupFailsDueToSpend() throws Exception {
    Transaction t = cleanupCommon(OTHER_ADDRESS);

    // Now we have another incoming pending.  Spend everything.
    Coin v3 = valueOf(0, 60);
    SendRequest req = SendRequest.to(OTHER_ADDRESS, v3);

    // Force selection of the incoming coin so that we can spend it
    req.coinSelector = new TestCoinSelector();

    wallet.completeTx(req);
    wallet.commitTx(req.tx);

    assertEquals("Wrong number of PENDING", 3, wallet.getPoolSize(WalletTransaction.Pool.PENDING));
    assertEquals("Wrong number of UNSPENT", 0, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
    assertEquals("Wrong number of ALL", 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", 3, wallet.getPoolSize(WalletTransaction.Pool.PENDING));
    assertEquals("Wrong number of UNSPENT", 0, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
    assertEquals("Wrong number of ALL", 4, wallet.getTransactions(true).size());
    assertEquals(ZERO, wallet.getBalance(Wallet.BalanceType.ESTIMATED));
}
 
Example #29
Source File: WalletTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void balance() throws Exception {
    // Receive 5 coins then half a coin.
    Coin v1 = valueOf(5, 0);
    Coin v2 = valueOf(0, 50);
    Coin expected = valueOf(5, 50);
    assertEquals(0, wallet.getTransactions(true).size());
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, v1);
    assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, v2);
    assertEquals(2, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
    assertEquals(expected, wallet.getBalance());

    // Now spend one coin.
    Coin v3 = COIN;
    Transaction spend = wallet.createSend(OTHER_ADDRESS, v3);
    wallet.commitTx(spend);
    assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.PENDING));

    // Available and estimated balances should not be the same. We don't check the exact available balance here
    // because it depends on the coin selection algorithm.
    assertEquals(valueOf(4, 50), wallet.getBalance(Wallet.BalanceType.ESTIMATED));
    assertFalse(wallet.getBalance(Wallet.BalanceType.AVAILABLE).equals(
                wallet.getBalance(Wallet.BalanceType.ESTIMATED)));

    // Now confirm the transaction by including it into a block.
    sendMoneyToWallet(BlockChain.NewBlockType.BEST_CHAIN, spend);

    // Change is confirmed. We started with 5.50 so we should have 4.50 left.
    Coin v4 = valueOf(4, 50);
    assertEquals(v4, wallet.getBalance(Wallet.BalanceType.AVAILABLE));
}
 
Example #30
Source File: WalletTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
private void receiveATransactionAmount(Wallet wallet, Address toAddress, Coin amount) {
    final ListenableFuture<Coin> availFuture = wallet.getBalanceFuture(amount, Wallet.BalanceType.AVAILABLE);
    final ListenableFuture<Coin> estimatedFuture = wallet.getBalanceFuture(amount, Wallet.BalanceType.ESTIMATED);
    assertFalse(availFuture.isDone());
    assertFalse(estimatedFuture.isDone());
    // Send some pending coins to the wallet.
    Transaction t1 = sendMoneyToWallet(wallet, null, amount, toAddress);
    Threading.waitForUserCode();
    final ListenableFuture<TransactionConfidence> depthFuture = t1.getConfidence().getDepthFuture(1);
    assertFalse(depthFuture.isDone());
    assertEquals(ZERO, wallet.getBalance());
    assertEquals(amount, wallet.getBalance(Wallet.BalanceType.ESTIMATED));
    assertFalse(availFuture.isDone());
    // Our estimated balance has reached the requested level.
    assertTrue(estimatedFuture.isDone());
    assertEquals(1, wallet.getPoolSize(Pool.PENDING));
    assertEquals(0, wallet.getPoolSize(Pool.UNSPENT));
    // Confirm the coins.
    sendMoneyToWallet(wallet, AbstractBlockChain.NewBlockType.BEST_CHAIN, t1);
    assertEquals("Incorrect confirmed tx balance", amount, wallet.getBalance());
    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 ALL pool size", 1, wallet.getTransactions(true).size());
    Threading.waitForUserCode();
    assertTrue(availFuture.isDone());
    assertTrue(estimatedFuture.isDone());
    assertTrue(depthFuture.isDone());
}