org.bitcoinj.core.BlockChain Java Examples

The following examples show how to use org.bitcoinj.core.BlockChain. 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 GreenBits with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void isConsistent_duplicates() throws Exception {
    // This test ensures that isConsistent catches duplicate transactions, eg, because we submitted the same block
    // twice (this is not allowed).
    Transaction tx = createFakeTx(PARAMS, COIN, myAddress);
    TransactionOutput output = new TransactionOutput(PARAMS, tx, valueOf(0, 5), OTHER_ADDRESS);
    tx.addOutput(output);
    wallet.receiveFromBlock(tx, null, BlockChain.NewBlockType.BEST_CHAIN, 0);

    assertTrue(wallet.isConsistent());

    Transaction txClone = PARAMS.getDefaultSerializer().makeTransaction(tx.bitcoinSerialize());
    try {
        wallet.receiveFromBlock(txClone, null, BlockChain.NewBlockType.BEST_CHAIN, 0);
        fail("Illegal argument not thrown when it should have been.");
    } catch (IllegalStateException ex) {
        // expected
    }
}
 
Example #2
Source File: VersionTallyTest.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testInitialize() throws BlockStoreException {
    final BlockStore blockStore = new MemoryBlockStore(PARAMS);
    final BlockChain chain = new BlockChain(PARAMS, blockStore);

    // Build a historical chain of version 2 blocks
    long timeSeconds = 1231006505;
    StoredBlock chainHead = null;
    for (int height = 0; height < PARAMS.getMajorityWindow(); height++) {
        chainHead = FakeTxBuilder.createFakeBlock(blockStore, 2, timeSeconds, height).storedBlock;
        assertEquals(2, chainHead.getHeader().getVersion());
        timeSeconds += 60;
    }

    VersionTally instance = new VersionTally(PARAMS);
    instance.initialize(blockStore, chainHead);
    assertEquals(PARAMS.getMajorityWindow(), instance.getCountAtOrAbove(2).intValue());
}
 
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 isConsistent_duplicates() throws Exception {
    // This test ensures that isConsistent catches duplicate transactions, eg, because we submitted the same block
    // twice (this is not allowed).
    Transaction tx = createFakeTx(PARAMS, COIN, myAddress);
    TransactionOutput output = new TransactionOutput(PARAMS, tx, valueOf(0, 5), OTHER_ADDRESS);
    tx.addOutput(output);
    wallet.receiveFromBlock(tx, null, BlockChain.NewBlockType.BEST_CHAIN, 0);

    assertTrue(wallet.isConsistent());

    Transaction txClone = PARAMS.getDefaultSerializer().makeTransaction(tx.bitcoinSerialize());
    try {
        wallet.receiveFromBlock(txClone, null, BlockChain.NewBlockType.BEST_CHAIN, 0);
        fail("Illegal argument not thrown when it should have been.");
    } catch (IllegalStateException ex) {
        // expected
    }
}
 
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 watchingScriptsBloomFilter() throws Exception {
    assertFalse(wallet.isRequiringUpdateAllBloomFilter());

    Address watchedAddress = new ECKey().toAddress(PARAMS);
    Transaction t1 = createFakeTx(PARAMS, CENT, watchedAddress);
    TransactionOutPoint outPoint = new TransactionOutPoint(PARAMS, 0, t1);
    wallet.addWatchedAddress(watchedAddress);

    assertTrue(wallet.isRequiringUpdateAllBloomFilter());
    // 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()));

    sendMoneyToWallet(BlockChain.NewBlockType.BEST_CHAIN, t1);
    assertTrue(wallet.getBloomFilter(1e-12).contains(outPoint.unsafeBitcoinSerialize()));
}
 
Example #5
Source File: WalletTest.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void removeScriptsBloomFilter() throws Exception {
    List<Address> addressesForRemoval = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        Address watchedAddress = new ECKey().toAddress(PARAMS);
        addressesForRemoval.add(watchedAddress);
        wallet.addWatchedAddress(watchedAddress);
    }

    wallet.removeWatchedAddresses(addressesForRemoval);

    for (Address addr : addressesForRemoval) {
        Transaction t1 = createFakeTx(PARAMS, CENT, addr);
        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
        assertFalse(wallet.getBloomFilter(1e-12).contains(outPoint.unsafeBitcoinSerialize()));

        sendMoneyToWallet(BlockChain.NewBlockType.BEST_CHAIN, t1);
        assertFalse(wallet.getBloomFilter(1e-12).contains(outPoint.unsafeBitcoinSerialize()));
    }
}
 
Example #6
Source File: VersionTallyTest.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testInitialize() throws BlockStoreException {
    final BlockStore blockStore = new MemoryBlockStore(PARAMS);
    final BlockChain chain = new BlockChain(PARAMS, blockStore);

    // Build a historical chain of version 2 blocks
    long timeSeconds = 1231006505;
    StoredBlock chainHead = null;
    for (int height = 0; height < PARAMS.getMajorityWindow(); height++) {
        chainHead = FakeTxBuilder.createFakeBlock(blockStore, 2, timeSeconds, height).storedBlock;
        assertEquals(2, chainHead.getHeader().getVersion());
        timeSeconds += 60;
    }

    VersionTally instance = new VersionTally(PARAMS);
    instance.initialize(blockStore, chainHead);
    assertEquals(PARAMS.getMajorityWindow(), instance.getCountAtOrAbove(2).intValue());
}
 
Example #7
Source File: WalletTest.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void removeScriptsBloomFilter() throws Exception {
    List<Address> addressesForRemoval = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        Address watchedAddress = LegacyAddress.fromKey(UNITTEST, new ECKey());
        addressesForRemoval.add(watchedAddress);
        wallet.addWatchedAddress(watchedAddress);
    }

    wallet.removeWatchedAddresses(addressesForRemoval);

    for (Address addr : addressesForRemoval) {
        Transaction t1 = createFakeTx(UNITTEST, CENT, addr);
        TransactionOutPoint outPoint = new TransactionOutPoint(UNITTEST, 0, t1);

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

        sendMoneyToWallet(BlockChain.NewBlockType.BEST_CHAIN, t1);
        assertFalse(wallet.getBloomFilter(1e-12).contains(outPoint.unsafeBitcoinSerialize()));
    }
}
 
Example #8
Source File: WalletTest.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void watchingScriptsBloomFilter() throws Exception {
    assertFalse(wallet.isRequiringUpdateAllBloomFilter());

    Address watchedAddress = LegacyAddress.fromKey(UNITTEST, new ECKey());
    Transaction t1 = createFakeTx(UNITTEST, CENT, watchedAddress);
    TransactionOutPoint outPoint = new TransactionOutPoint(UNITTEST, 0, t1);
    wallet.addWatchedAddress(watchedAddress);

    assertTrue(wallet.isRequiringUpdateAllBloomFilter());
    // 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()));

    sendMoneyToWallet(BlockChain.NewBlockType.BEST_CHAIN, t1);
    assertTrue(wallet.getBloomFilter(1e-12).contains(outPoint.unsafeBitcoinSerialize()));
}
 
Example #9
Source File: WalletTest.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void watchingScriptsBloomFilter() throws Exception {
    assertFalse(wallet.isRequiringUpdateAllBloomFilter());

    Address watchedAddress = new ECKey().toAddress(PARAMS);
    Transaction t1 = createFakeTx(PARAMS, CENT, watchedAddress);
    TransactionOutPoint outPoint = new TransactionOutPoint(PARAMS, 0, t1);
    wallet.addWatchedAddress(watchedAddress);

    assertTrue(wallet.isRequiringUpdateAllBloomFilter());
    // 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()));

    sendMoneyToWallet(BlockChain.NewBlockType.BEST_CHAIN, t1);
    assertTrue(wallet.getBloomFilter(1e-12).contains(outPoint.unsafeBitcoinSerialize()));
}
 
Example #10
Source File: WalletTest.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void removeScriptsBloomFilter() throws Exception {
    List<Address> addressesForRemoval = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        Address watchedAddress = new ECKey().toAddress(PARAMS);
        addressesForRemoval.add(watchedAddress);
        wallet.addWatchedAddress(watchedAddress);
    }

    wallet.removeWatchedAddresses(addressesForRemoval);

    for (Address addr : addressesForRemoval) {
        Transaction t1 = createFakeTx(PARAMS, CENT, addr);
        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
        assertFalse(wallet.getBloomFilter(1e-12).contains(outPoint.unsafeBitcoinSerialize()));

        sendMoneyToWallet(BlockChain.NewBlockType.BEST_CHAIN, t1);
        assertFalse(wallet.getBloomFilter(1e-12).contains(outPoint.unsafeBitcoinSerialize()));
    }
}
 
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 isConsistent_duplicates() throws Exception {
    // This test ensures that isConsistent catches duplicate transactions, eg, because we submitted the same block
    // twice (this is not allowed).
    Transaction tx = createFakeTx(UNITTEST, COIN, myAddress);
    TransactionOutput output = new TransactionOutput(UNITTEST, tx, valueOf(0, 5), OTHER_ADDRESS);
    tx.addOutput(output);
    wallet.receiveFromBlock(tx, null, BlockChain.NewBlockType.BEST_CHAIN, 0);

    assertTrue(wallet.isConsistent());

    Transaction txClone = UNITTEST.getDefaultSerializer().makeTransaction(tx.bitcoinSerialize());
    try {
        wallet.receiveFromBlock(txClone, null, BlockChain.NewBlockType.BEST_CHAIN, 0);
        fail("Illegal argument not thrown when it should have been.");
    } catch (IllegalStateException ex) {
        // expected
    }
}
 
Example #12
Source File: VersionTallyTest.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testInitialize() throws BlockStoreException {
    final BlockStore blockStore = new MemoryBlockStore(UNITTEST);
    final BlockChain chain = new BlockChain(UNITTEST, blockStore);

    // Build a historical chain of version 2 blocks
    long timeSeconds = 1231006505;
    StoredBlock chainHead = null;
    for (int height = 0; height < UNITTEST.getMajorityWindow(); height++) {
        chainHead = FakeTxBuilder.createFakeBlock(blockStore, 2, timeSeconds, height).storedBlock;
        assertEquals(2, chainHead.getHeader().getVersion());
        timeSeconds += 60;
    }

    VersionTally instance = new VersionTally(UNITTEST);
    instance.initialize(blockStore, chainHead);
    assertEquals(UNITTEST.getMajorityWindow(), instance.getCountAtOrAbove(2).intValue());
}
 
Example #13
Source File: WalletTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test(expected = InsufficientMoneyException.class)
public void watchingScriptsConfirmed() throws Exception {
    Address watchedAddress = new ECKey().toAddress(PARAMS);
    wallet.addWatchedAddress(watchedAddress);
    sendMoneyToWallet(BlockChain.NewBlockType.BEST_CHAIN, CENT, watchedAddress);
    assertEquals(CENT, wallet.getBalance());

    // We can't spend watched balances
    wallet.createSend(OTHER_ADDRESS, CENT);
}
 
Example #14
Source File: WalletTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void isConsistent_pools() throws Exception {
    // This test ensures that isConsistent catches transactions that are in incompatible pools.
    Transaction tx = createFakeTx(PARAMS, COIN, myAddress);
    TransactionOutput output = new TransactionOutput(PARAMS, tx, valueOf(0, 5), OTHER_ADDRESS);
    tx.addOutput(output);
    wallet.receiveFromBlock(tx, null, BlockChain.NewBlockType.BEST_CHAIN, 0);

    assertTrue(wallet.isConsistent());

    wallet.addWalletTransaction(new WalletTransaction(Pool.PENDING, tx));
    assertFalse(wallet.isConsistent());
}
 
Example #15
Source File: WalletService.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
public void removeNewBestBlockListener(NewBestBlockListener listener) {
    //noinspection deprecation
    final BlockChain chain = walletsSetup.getChain();
    if (isWalletReady() && chain != null)
        chain.removeNewBestBlockListener(listener);
}
 
Example #16
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 #17
Source File: SPV.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void receiveFromBlock(final Transaction tx, final StoredBlock block,
                             final BlockChain.NewBlockType blockType,
                             final int relativityOffset) throws VerificationException {
    if (tx == null)
        throw new RuntimeException("receiveFromBlock got null tx");
    getService().notifyObservers(tx.getHash());
}
 
Example #18
Source File: WalletTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void marriedKeychainBloomFilter() throws Exception {
    createMarriedWallet(2, 2);
    Address address = wallet.currentReceiveAddress();

    assertTrue(wallet.getBloomFilter(0.001).contains(address.getHash160()));

    Transaction t1 = createFakeTx(PARAMS, CENT, address);
    TransactionOutPoint outPoint = new TransactionOutPoint(PARAMS, 0, t1);

    assertFalse(wallet.getBloomFilter(0.001).contains(outPoint.unsafeBitcoinSerialize()));

    sendMoneyToWallet(BlockChain.NewBlockType.BEST_CHAIN, t1);
    assertTrue(wallet.getBloomFilter(0.001).contains(outPoint.unsafeBitcoinSerialize()));
}
 
Example #19
Source File: WalletTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void marriedKeychainBloomFilter() throws Exception {
    createMarriedWallet(2, 2);
    Address address = wallet.currentReceiveAddress();

    assertTrue(wallet.getBloomFilter(0.001).contains(address.getHash160()));

    Transaction t1 = createFakeTx(PARAMS, CENT, address);
    TransactionOutPoint outPoint = new TransactionOutPoint(PARAMS, 0, t1);

    assertFalse(wallet.getBloomFilter(0.001).contains(outPoint.unsafeBitcoinSerialize()));

    sendMoneyToWallet(BlockChain.NewBlockType.BEST_CHAIN, t1);
    assertTrue(wallet.getBloomFilter(0.001).contains(outPoint.unsafeBitcoinSerialize()));
}
 
Example #20
Source File: WalletTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test(expected = InsufficientMoneyException.class)
public void watchingScriptsConfirmed() throws Exception {
    Address watchedAddress = new ECKey().toAddress(PARAMS);
    wallet.addWatchedAddress(watchedAddress);
    sendMoneyToWallet(BlockChain.NewBlockType.BEST_CHAIN, CENT, watchedAddress);
    assertEquals(CENT, wallet.getBalance());

    // We can't spend watched balances
    wallet.createSend(OTHER_ADDRESS, CENT);
}
 
Example #21
Source File: WalletTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void isConsistent_pools() throws Exception {
    // This test ensures that isConsistent catches transactions that are in incompatible pools.
    Transaction tx = createFakeTx(PARAMS, COIN, myAddress);
    TransactionOutput output = new TransactionOutput(PARAMS, tx, valueOf(0, 5), OTHER_ADDRESS);
    tx.addOutput(output);
    wallet.receiveFromBlock(tx, null, BlockChain.NewBlockType.BEST_CHAIN, 0);

    assertTrue(wallet.isConsistent());

    wallet.addWalletTransaction(new WalletTransaction(Pool.PENDING, tx));
    assertFalse(wallet.isConsistent());
}
 
Example #22
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 #23
Source File: WalletService.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
public void addNewBestBlockListener(NewBestBlockListener listener) {
    //noinspection deprecation
    final BlockChain chain = walletsSetup.getChain();
    if (isWalletReady() && chain != null)
        chain.addNewBestBlockListener(listener);
}
 
Example #24
Source File: SPV.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean notifyTransactionIsInBlock(final Sha256Hash txHash, final StoredBlock block,
                                          final BlockChain.NewBlockType blockType,
                                          final int relativityOffset) throws VerificationException {
    Log.d(TAG, "notifyTransactionIsInBlock " + txHash.toString());
    return isUnspentOutpoint(txHash);
}
 
Example #25
Source File: SPV.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void receiveFromBlock(final Transaction tx, final StoredBlock block,
                             final BlockChain.NewBlockType blockType,
                             final int relativityOffset) throws VerificationException {
    if (tx == null)
        throw new RuntimeException("receiveFromBlock got null tx");
    Log.d(TAG, "receiveFromBlock " + tx.getHash().toString());
    addUtxoToValues(tx.getHash(), true);
}
 
Example #26
Source File: WalletService.java    From bisq-core with GNU Affero General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
public void removeNewBestBlockListener(NewBestBlockListener listener) {
    //noinspection deprecation
    final BlockChain chain = walletsSetup.getChain();
    if (isWalletReady() && chain != null)
        chain.removeNewBestBlockListener(listener);
}
 
Example #27
Source File: WalletService.java    From bisq-core with GNU Affero General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
public void addNewBestBlockListener(NewBestBlockListener listener) {
    //noinspection deprecation
    final BlockChain chain = walletsSetup.getChain();
    if (isWalletReady() && chain != null)
        chain.addNewBestBlockListener(listener);
}
 
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 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 #29
Source File: WalletTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void isConsistent_pools() throws Exception {
    // This test ensures that isConsistent catches transactions that are in incompatible pools.
    Transaction tx = createFakeTx(UNITTEST, COIN, myAddress);
    TransactionOutput output = new TransactionOutput(UNITTEST, tx, valueOf(0, 5), OTHER_ADDRESS);
    tx.addOutput(output);
    wallet.receiveFromBlock(tx, null, BlockChain.NewBlockType.BEST_CHAIN, 0);

    assertTrue(wallet.isConsistent());

    wallet.addWalletTransaction(new WalletTransaction(Pool.PENDING, tx));
    assertFalse(wallet.isConsistent());
}
 
Example #30
Source File: WalletTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Test(expected = InsufficientMoneyException.class)
public void watchingScriptsConfirmed() throws Exception {
    Address watchedAddress = LegacyAddress.fromKey(UNITTEST, new ECKey());
    wallet.addWatchedAddress(watchedAddress);
    sendMoneyToWallet(BlockChain.NewBlockType.BEST_CHAIN, CENT, watchedAddress);
    assertEquals(CENT, wallet.getBalance());

    // We can't spend watched balances
    wallet.createSend(OTHER_ADDRESS, CENT);
}