Java Code Examples for org.bitcoinj.utils.Threading#waitForUserCode()

The following examples show how to use org.bitcoinj.utils.Threading#waitForUserCode() . 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
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 3
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 4
Source File: WalletTest.java    From bcm-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());
}
 
Example 5
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());
}
 
Example 6
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 7
Source File: PeerTest.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void recursiveDependencyDownload() throws Exception {
    connect();
    // Check that we can download all dependencies of an unconfirmed relevant transaction from the mempool.
    ECKey to = new ECKey();

    final Transaction[] onTx = new Transaction[1];
    peer.addOnTransactionBroadcastListener(Threading.SAME_THREAD, new OnTransactionBroadcastListener() {
        @Override
        public void onTransaction(Peer peer1, Transaction t) {
            onTx[0] = t;
        }
    });

    // Make some fake transactions in the following graph:
    //   t1 -> t2 -> [t5]
    //      -> t3 -> t4 -> [t6]
    //      -> [t7]
    //      -> [t8]
    // The ones in brackets are assumed to be in the chain and are represented only by hashes.
    Transaction t2 = FakeTxBuilder.createFakeTx(UNITTEST, COIN, to);
    Sha256Hash t5hash = t2.getInput(0).getOutpoint().getHash();
    Transaction t4 = FakeTxBuilder.createFakeTx(UNITTEST, COIN, new ECKey());
    Sha256Hash t6hash = t4.getInput(0).getOutpoint().getHash();
    t4.addOutput(COIN, new ECKey());
    Transaction t3 = new Transaction(UNITTEST);
    t3.addInput(t4.getOutput(0));
    t3.addOutput(COIN, new ECKey());
    Transaction t1 = new Transaction(UNITTEST);
    t1.addInput(t2.getOutput(0));
    t1.addInput(t3.getOutput(0));
    Sha256Hash t7hash = Sha256Hash.wrap("2b801dd82f01d17bbde881687bf72bc62e2faa8ab8133d36fcb8c3abe7459da6");
    t1.addInput(new TransactionInput(UNITTEST, t1, new byte[]{}, new TransactionOutPoint(UNITTEST, 0, t7hash)));
    Sha256Hash t8hash = Sha256Hash.wrap("3b801dd82f01d17bbde881687bf72bc62e2faa8ab8133d36fcb8c3abe7459da6");
    t1.addInput(new TransactionInput(UNITTEST, t1, new byte[]{}, new TransactionOutPoint(UNITTEST, 1, t8hash)));
    t1.addOutput(COIN, to);
    t1 = FakeTxBuilder.roundTripTransaction(UNITTEST, t1);
    t2 = FakeTxBuilder.roundTripTransaction(UNITTEST, t2);
    t3 = FakeTxBuilder.roundTripTransaction(UNITTEST, t3);
    t4 = FakeTxBuilder.roundTripTransaction(UNITTEST, t4);

    // Announce the first one. Wait for it to be downloaded.
    InventoryMessage inv = new InventoryMessage(UNITTEST);
    inv.addTransaction(t1);
    inbound(writeTarget, inv);
    GetDataMessage getdata = (GetDataMessage) outbound(writeTarget);
    Threading.waitForUserCode();
    assertEquals(t1.getHash(), getdata.getItems().get(0).hash);
    inbound(writeTarget, t1);
    pingAndWait(writeTarget);
    assertEquals(t1, onTx[0]);
    // We want its dependencies so ask for them.
    ListenableFuture<List<Transaction>> futures = peer.downloadDependencies(t1);
    assertFalse(futures.isDone());
    // It will recursively ask for the dependencies of t1: t2, t3, t7, t8.
    getdata = (GetDataMessage) outbound(writeTarget);
    assertEquals(4, getdata.getItems().size());
    assertEquals(t2.getHash(), getdata.getItems().get(0).hash);
    assertEquals(t3.getHash(), getdata.getItems().get(1).hash);
    assertEquals(t7hash, getdata.getItems().get(2).hash);
    assertEquals(t8hash, getdata.getItems().get(3).hash);
    // Deliver the requested transactions.
    inbound(writeTarget, t2);
    inbound(writeTarget, t3);
    NotFoundMessage notFound = new NotFoundMessage(UNITTEST);
    notFound.addItem(new InventoryItem(InventoryItem.Type.Transaction, t7hash));
    notFound.addItem(new InventoryItem(InventoryItem.Type.Transaction, t8hash));
    inbound(writeTarget, notFound);
    assertFalse(futures.isDone());
    // It will recursively ask for the dependencies of t2: t5 and t4, but not t3 because it already found t4.
    getdata = (GetDataMessage) outbound(writeTarget);
    assertEquals(getdata.getItems().get(0).hash, t2.getInput(0).getOutpoint().getHash());
    // t5 isn't found and t4 is.
    notFound = new NotFoundMessage(UNITTEST);
    notFound.addItem(new InventoryItem(InventoryItem.Type.Transaction, t5hash));
    inbound(writeTarget, notFound);
    assertFalse(futures.isDone());
    // Request t4 ...
    getdata = (GetDataMessage) outbound(writeTarget);
    assertEquals(t4.getHash(), getdata.getItems().get(0).hash);
    inbound(writeTarget, t4);
    // Continue to explore the t4 branch and ask for t6, which is in the chain.
    getdata = (GetDataMessage) outbound(writeTarget);
    assertEquals(t6hash, getdata.getItems().get(0).hash);
    notFound = new NotFoundMessage(UNITTEST);
    notFound.addItem(new InventoryItem(InventoryItem.Type.Transaction, t6hash));
    inbound(writeTarget, notFound);
    pingAndWait(writeTarget);
    // That's it, we explored the entire tree.
    assertTrue(futures.isDone());
    List<Transaction> results = futures.get();
    assertEquals(3, results.size());
    assertTrue(results.contains(t2));
    assertTrue(results.contains(t3));
    assertTrue(results.contains(t4));
}
 
Example 8
Source File: PeerTest.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void recursiveDependencyDownload_depthLimited() throws Exception {
    peer.setDownloadTxDependencies(1); // Depth limit
    connect();

    // Make some fake transactions in the following graph:
    //   t1 -> t2 -> t3 -> [t4]
    // The ones in brackets are assumed to be in the chain and are represented only by hashes.
    Sha256Hash t4hash = Sha256Hash.wrap("2b801dd82f01d17bbde881687bf72bc62e2faa8ab8133d36fcb8c3abe7459da6");
    Transaction t3 = new Transaction(UNITTEST);
    t3.addInput(new TransactionInput(UNITTEST, t3, new byte[]{}, new TransactionOutPoint(UNITTEST, 0, t4hash)));
    t3.addOutput(COIN, new ECKey());
    t3 = FakeTxBuilder.roundTripTransaction(UNITTEST, t3);
    Transaction t2 = new Transaction(UNITTEST);
    t2.addInput(t3.getOutput(0));
    t2.addOutput(COIN, new ECKey());
    t2 = FakeTxBuilder.roundTripTransaction(UNITTEST, t2);
    Transaction t1 = new Transaction(UNITTEST);
    t1.addInput(t2.getOutput(0));
    t1.addOutput(COIN, new ECKey());
    t1 = FakeTxBuilder.roundTripTransaction(UNITTEST, t1);

    // Announce the first one. Wait for it to be downloaded.
    InventoryMessage inv = new InventoryMessage(UNITTEST);
    inv.addTransaction(t1);
    inbound(writeTarget, inv);
    GetDataMessage getdata = (GetDataMessage) outbound(writeTarget);
    Threading.waitForUserCode();
    assertEquals(t1.getHash(), getdata.getItems().get(0).hash);
    inbound(writeTarget, t1);
    pingAndWait(writeTarget);
    // We want its dependencies so ask for them.
    ListenableFuture<List<Transaction>> futures = peer.downloadDependencies(t1);
    assertFalse(futures.isDone());
    // level 1
    getdata = (GetDataMessage) outbound(writeTarget);
    assertEquals(1, getdata.getItems().size());
    assertEquals(t2.getHash(), getdata.getItems().get(0).hash);
    inbound(writeTarget, t2);
    // no level 2
    getdata = (GetDataMessage) outbound(writeTarget);
    assertNull(getdata);

    // That's it, now double check what we've got
    pingAndWait(writeTarget);
    assertTrue(futures.isDone());
    List<Transaction> results = futures.get();
    assertEquals(1, results.size());
    assertTrue(results.contains(t2));
}
 
Example 9
Source File: WalletProtobufSerializerTest.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testAppearedAtChainHeightDepthAndWorkDone() throws Exception {
    // Test the TransactionConfidence appearedAtChainHeight, depth and workDone field are stored.

    BlockChain chain = new BlockChain(UNITTEST, myWallet, new MemoryBlockStore(UNITTEST));

    final ArrayList<Transaction> txns = new ArrayList<>(2);
    myWallet.addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() {
        @Override
        public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
            txns.add(tx);
        }
    });

    // Start by building two blocks on top of the genesis block.
    Block b1 = UNITTEST.getGenesisBlock().createNextBlock(myAddress);
    BigInteger work1 = b1.getWork();
    assertTrue(work1.signum() > 0);

    Block b2 = b1.createNextBlock(myAddress);
    BigInteger work2 = b2.getWork();
    assertTrue(work2.signum() > 0);

    assertTrue(chain.add(b1));
    assertTrue(chain.add(b2));

    // We now have the following chain:
    //     genesis -> b1 -> b2

    // Check the transaction confidence levels are correct before wallet roundtrip.
    Threading.waitForUserCode();
    assertEquals(2, txns.size());

    TransactionConfidence confidence0 = txns.get(0).getConfidence();
    TransactionConfidence confidence1 = txns.get(1).getConfidence();

    assertEquals(1, confidence0.getAppearedAtChainHeight());
    assertEquals(2, confidence1.getAppearedAtChainHeight());

    assertEquals(2, confidence0.getDepthInBlocks());
    assertEquals(1, confidence1.getDepthInBlocks());

    // Roundtrip the wallet and check it has stored the depth and workDone.
    Wallet rebornWallet = roundTrip(myWallet);

    Set<Transaction> rebornTxns = rebornWallet.getTransactions(false);
    assertEquals(2, rebornTxns.size());

    // The transactions are not guaranteed to be in the same order so sort them to be in chain height order if required.
    Iterator<Transaction> it = rebornTxns.iterator();
    Transaction txA = it.next();
    Transaction txB = it.next();

    Transaction rebornTx0, rebornTx1;
    if (txA.getConfidence().getAppearedAtChainHeight() == 1) {
        rebornTx0 = txA;
        rebornTx1 = txB;
    } else {
        rebornTx0 = txB;
        rebornTx1 = txA;
    }

    TransactionConfidence rebornConfidence0 = rebornTx0.getConfidence();
    TransactionConfidence rebornConfidence1 = rebornTx1.getConfidence();

    assertEquals(1, rebornConfidence0.getAppearedAtChainHeight());
    assertEquals(2, rebornConfidence1.getAppearedAtChainHeight());

    assertEquals(2, rebornConfidence0.getDepthInBlocks());
    assertEquals(1, rebornConfidence1.getDepthInBlocks());
}
 
Example 10
Source File: PeerTest.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void recursiveDependencyDownload() throws Exception {
    connect();
    // Check that we can download all dependencies of an unconfirmed relevant transaction from the mempool.
    ECKey to = new ECKey();

    final Transaction[] onTx = new Transaction[1];
    peer.addOnTransactionBroadcastListener(Threading.SAME_THREAD, new OnTransactionBroadcastListener() {
        @Override
        public void onTransaction(Peer peer1, Transaction t) {
            onTx[0] = t;
        }
    });

    // Make some fake transactions in the following graph:
    //   t1 -> t2 -> [t5]
    //      -> t3 -> t4 -> [t6]
    //      -> [t7]
    //      -> [t8]
    // The ones in brackets are assumed to be in the chain and are represented only by hashes.
    Transaction t2 = FakeTxBuilder.createFakeTx(PARAMS, COIN, to);
    Sha256Hash t5hash = t2.getInput(0).getOutpoint().getHash();
    Transaction t4 = FakeTxBuilder.createFakeTx(PARAMS, COIN, new ECKey());
    Sha256Hash t6hash = t4.getInput(0).getOutpoint().getHash();
    t4.addOutput(COIN, new ECKey());
    Transaction t3 = new Transaction(PARAMS);
    t3.addInput(t4.getOutput(0));
    t3.addOutput(COIN, new ECKey());
    Transaction t1 = new Transaction(PARAMS);
    t1.addInput(t2.getOutput(0));
    t1.addInput(t3.getOutput(0));
    Sha256Hash t7hash = Sha256Hash.wrap("2b801dd82f01d17bbde881687bf72bc62e2faa8ab8133d36fcb8c3abe7459da6");
    t1.addInput(new TransactionInput(PARAMS, t1, new byte[]{}, new TransactionOutPoint(PARAMS, 0, t7hash)));
    Sha256Hash t8hash = Sha256Hash.wrap("3b801dd82f01d17bbde881687bf72bc62e2faa8ab8133d36fcb8c3abe7459da6");
    t1.addInput(new TransactionInput(PARAMS, t1, new byte[]{}, new TransactionOutPoint(PARAMS, 1, t8hash)));
    t1.addOutput(COIN, to);
    t1 = FakeTxBuilder.roundTripTransaction(PARAMS, t1);
    t2 = FakeTxBuilder.roundTripTransaction(PARAMS, t2);
    t3 = FakeTxBuilder.roundTripTransaction(PARAMS, t3);
    t4 = FakeTxBuilder.roundTripTransaction(PARAMS, t4);

    // Announce the first one. Wait for it to be downloaded.
    InventoryMessage inv = new InventoryMessage(PARAMS);
    inv.addTransaction(t1);
    inbound(writeTarget, inv);
    GetDataMessage getdata = (GetDataMessage) outbound(writeTarget);
    Threading.waitForUserCode();
    assertEquals(t1.getHash(), getdata.getItems().get(0).hash);
    inbound(writeTarget, t1);
    pingAndWait(writeTarget);
    assertEquals(t1, onTx[0]);
    // We want its dependencies so ask for them.
    ListenableFuture<List<Transaction>> futures = peer.downloadDependencies(t1);
    assertFalse(futures.isDone());
    // It will recursively ask for the dependencies of t1: t2, t3, t7, t8.
    getdata = (GetDataMessage) outbound(writeTarget);
    assertEquals(4, getdata.getItems().size());
    assertEquals(t2.getHash(), getdata.getItems().get(0).hash);
    assertEquals(t3.getHash(), getdata.getItems().get(1).hash);
    assertEquals(t7hash, getdata.getItems().get(2).hash);
    assertEquals(t8hash, getdata.getItems().get(3).hash);
    // Deliver the requested transactions.
    inbound(writeTarget, t2);
    inbound(writeTarget, t3);
    NotFoundMessage notFound = new NotFoundMessage(PARAMS);
    notFound.addItem(new InventoryItem(InventoryItem.Type.Transaction, t7hash));
    notFound.addItem(new InventoryItem(InventoryItem.Type.Transaction, t8hash));
    inbound(writeTarget, notFound);
    assertFalse(futures.isDone());
    // It will recursively ask for the dependencies of t2: t5 and t4, but not t3 because it already found t4.
    getdata = (GetDataMessage) outbound(writeTarget);
    assertEquals(getdata.getItems().get(0).hash, t2.getInput(0).getOutpoint().getHash());
    // t5 isn't found and t4 is.
    notFound = new NotFoundMessage(PARAMS);
    notFound.addItem(new InventoryItem(InventoryItem.Type.Transaction, t5hash));
    inbound(writeTarget, notFound);
    assertFalse(futures.isDone());
    // Request t4 ...
    getdata = (GetDataMessage) outbound(writeTarget);
    assertEquals(t4.getHash(), getdata.getItems().get(0).hash);
    inbound(writeTarget, t4);
    // Continue to explore the t4 branch and ask for t6, which is in the chain.
    getdata = (GetDataMessage) outbound(writeTarget);
    assertEquals(t6hash, getdata.getItems().get(0).hash);
    notFound = new NotFoundMessage(PARAMS);
    notFound.addItem(new InventoryItem(InventoryItem.Type.Transaction, t6hash));
    inbound(writeTarget, notFound);
    pingAndWait(writeTarget);
    // That's it, we explored the entire tree.
    assertTrue(futures.isDone());
    List<Transaction> results = futures.get();
    assertEquals(3, results.size());
    assertTrue(results.contains(t2));
    assertTrue(results.contains(t3));
    assertTrue(results.contains(t4));
}
 
Example 11
Source File: PeerTest.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void recursiveDependencyDownload_depthLimited() throws Exception {
    peer.setDownloadTxDependencies(1); // Depth limit
    connect();

    // Make some fake transactions in the following graph:
    //   t1 -> t2 -> t3 -> [t4]
    // The ones in brackets are assumed to be in the chain and are represented only by hashes.
    Sha256Hash t4hash = Sha256Hash.wrap("2b801dd82f01d17bbde881687bf72bc62e2faa8ab8133d36fcb8c3abe7459da6");
    Transaction t3 = new Transaction(PARAMS);
    t3.addInput(new TransactionInput(PARAMS, t3, new byte[]{}, new TransactionOutPoint(PARAMS, 0, t4hash)));
    t3.addOutput(COIN, new ECKey());
    t3 = FakeTxBuilder.roundTripTransaction(PARAMS, t3);
    Transaction t2 = new Transaction(PARAMS);
    t2.addInput(t3.getOutput(0));
    t2.addOutput(COIN, new ECKey());
    t2 = FakeTxBuilder.roundTripTransaction(PARAMS, t2);
    Transaction t1 = new Transaction(PARAMS);
    t1.addInput(t2.getOutput(0));
    t1.addOutput(COIN, new ECKey());
    t1 = FakeTxBuilder.roundTripTransaction(PARAMS, t1);

    // Announce the first one. Wait for it to be downloaded.
    InventoryMessage inv = new InventoryMessage(PARAMS);
    inv.addTransaction(t1);
    inbound(writeTarget, inv);
    GetDataMessage getdata = (GetDataMessage) outbound(writeTarget);
    Threading.waitForUserCode();
    assertEquals(t1.getHash(), getdata.getItems().get(0).hash);
    inbound(writeTarget, t1);
    pingAndWait(writeTarget);
    // We want its dependencies so ask for them.
    ListenableFuture<List<Transaction>> futures = peer.downloadDependencies(t1);
    assertFalse(futures.isDone());
    // level 1
    getdata = (GetDataMessage) outbound(writeTarget);
    assertEquals(1, getdata.getItems().size());
    assertEquals(t2.getHash(), getdata.getItems().get(0).hash);
    inbound(writeTarget, t2);
    // no level 2
    getdata = (GetDataMessage) outbound(writeTarget);
    assertNull(getdata);

    // That's it, now double check what we've got
    pingAndWait(writeTarget);
    assertTrue(futures.isDone());
    List<Transaction> results = futures.get();
    assertEquals(1, results.size());
    assertTrue(results.contains(t2));
}
 
Example 12
Source File: WalletProtobufSerializerTest.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testAppearedAtChainHeightDepthAndWorkDone() throws Exception {
    // Test the TransactionConfidence appearedAtChainHeight, depth and workDone field are stored.

    BlockChain chain = new BlockChain(PARAMS, myWallet, new MemoryBlockStore(PARAMS));

    final ArrayList<Transaction> txns = new ArrayList<>(2);
    myWallet.addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() {
        @Override
        public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
            txns.add(tx);
        }
    });

    // Start by building two blocks on top of the genesis block.
    Block b1 = PARAMS.getGenesisBlock().createNextBlock(myAddress);
    BigInteger work1 = b1.getWork();
    assertTrue(work1.signum() > 0);

    Block b2 = b1.createNextBlock(myAddress);
    BigInteger work2 = b2.getWork();
    assertTrue(work2.signum() > 0);

    assertTrue(chain.add(b1));
    assertTrue(chain.add(b2));

    // We now have the following chain:
    //     genesis -> b1 -> b2

    // Check the transaction confidence levels are correct before wallet roundtrip.
    Threading.waitForUserCode();
    assertEquals(2, txns.size());

    TransactionConfidence confidence0 = txns.get(0).getConfidence();
    TransactionConfidence confidence1 = txns.get(1).getConfidence();

    assertEquals(1, confidence0.getAppearedAtChainHeight());
    assertEquals(2, confidence1.getAppearedAtChainHeight());

    assertEquals(2, confidence0.getDepthInBlocks());
    assertEquals(1, confidence1.getDepthInBlocks());

    // Roundtrip the wallet and check it has stored the depth and workDone.
    Wallet rebornWallet = roundTrip(myWallet);

    Set<Transaction> rebornTxns = rebornWallet.getTransactions(false);
    assertEquals(2, rebornTxns.size());

    // The transactions are not guaranteed to be in the same order so sort them to be in chain height order if required.
    Iterator<Transaction> it = rebornTxns.iterator();
    Transaction txA = it.next();
    Transaction txB = it.next();

    Transaction rebornTx0, rebornTx1;
     if (txA.getConfidence().getAppearedAtChainHeight() == 1) {
        rebornTx0 = txA;
        rebornTx1 = txB;
    } else {
        rebornTx0 = txB;
        rebornTx1 = txA;
    }

    TransactionConfidence rebornConfidence0 = rebornTx0.getConfidence();
    TransactionConfidence rebornConfidence1 = rebornTx1.getConfidence();

    assertEquals(1, rebornConfidence0.getAppearedAtChainHeight());
    assertEquals(2, rebornConfidence1.getAppearedAtChainHeight());

    assertEquals(2, rebornConfidence0.getDepthInBlocks());
    assertEquals(1, rebornConfidence1.getDepthInBlocks());
}
 
Example 13
Source File: PeerTest.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void recursiveDependencyDownload() throws Exception {
    connect();
    // Check that we can download all dependencies of an unconfirmed relevant transaction from the mempool.
    ECKey to = new ECKey();

    final Transaction[] onTx = new Transaction[1];
    peer.addOnTransactionBroadcastListener(Threading.SAME_THREAD, new OnTransactionBroadcastListener() {
        @Override
        public void onTransaction(Peer peer1, Transaction t) {
            onTx[0] = t;
        }
    });

    // Make some fake transactions in the following graph:
    //   t1 -> t2 -> [t5]
    //      -> t3 -> t4 -> [t6]
    //      -> [t7]
    //      -> [t8]
    // The ones in brackets are assumed to be in the chain and are represented only by hashes.
    Transaction t2 = FakeTxBuilder.createFakeTx(PARAMS, COIN, to);
    Sha256Hash t5hash = t2.getInput(0).getOutpoint().getHash();
    Transaction t4 = FakeTxBuilder.createFakeTx(PARAMS, COIN, new ECKey());
    Sha256Hash t6hash = t4.getInput(0).getOutpoint().getHash();
    t4.addOutput(COIN, new ECKey());
    Transaction t3 = new Transaction(PARAMS);
    t3.addInput(t4.getOutput(0));
    t3.addOutput(COIN, new ECKey());
    Transaction t1 = new Transaction(PARAMS);
    t1.addInput(t2.getOutput(0));
    t1.addInput(t3.getOutput(0));
    Sha256Hash t7hash = Sha256Hash.wrap("2b801dd82f01d17bbde881687bf72bc62e2faa8ab8133d36fcb8c3abe7459da6");
    t1.addInput(new TransactionInput(PARAMS, t1, new byte[]{}, new TransactionOutPoint(PARAMS, 0, t7hash)));
    Sha256Hash t8hash = Sha256Hash.wrap("3b801dd82f01d17bbde881687bf72bc62e2faa8ab8133d36fcb8c3abe7459da6");
    t1.addInput(new TransactionInput(PARAMS, t1, new byte[]{}, new TransactionOutPoint(PARAMS, 1, t8hash)));
    t1.addOutput(COIN, to);
    t1 = FakeTxBuilder.roundTripTransaction(PARAMS, t1);
    t2 = FakeTxBuilder.roundTripTransaction(PARAMS, t2);
    t3 = FakeTxBuilder.roundTripTransaction(PARAMS, t3);
    t4 = FakeTxBuilder.roundTripTransaction(PARAMS, t4);

    // Announce the first one. Wait for it to be downloaded.
    InventoryMessage inv = new InventoryMessage(PARAMS);
    inv.addTransaction(t1);
    inbound(writeTarget, inv);
    GetDataMessage getdata = (GetDataMessage) outbound(writeTarget);
    Threading.waitForUserCode();
    assertEquals(t1.getHash(), getdata.getItems().get(0).hash);
    inbound(writeTarget, t1);
    pingAndWait(writeTarget);
    assertEquals(t1, onTx[0]);
    // We want its dependencies so ask for them.
    ListenableFuture<List<Transaction>> futures = peer.downloadDependencies(t1);
    assertFalse(futures.isDone());
    // It will recursively ask for the dependencies of t1: t2, t3, t7, t8.
    getdata = (GetDataMessage) outbound(writeTarget);
    assertEquals(4, getdata.getItems().size());
    assertEquals(t2.getHash(), getdata.getItems().get(0).hash);
    assertEquals(t3.getHash(), getdata.getItems().get(1).hash);
    assertEquals(t7hash, getdata.getItems().get(2).hash);
    assertEquals(t8hash, getdata.getItems().get(3).hash);
    // Deliver the requested transactions.
    inbound(writeTarget, t2);
    inbound(writeTarget, t3);
    NotFoundMessage notFound = new NotFoundMessage(PARAMS);
    notFound.addItem(new InventoryItem(InventoryItem.Type.Transaction, t7hash));
    notFound.addItem(new InventoryItem(InventoryItem.Type.Transaction, t8hash));
    inbound(writeTarget, notFound);
    assertFalse(futures.isDone());
    // It will recursively ask for the dependencies of t2: t5 and t4, but not t3 because it already found t4.
    getdata = (GetDataMessage) outbound(writeTarget);
    assertEquals(getdata.getItems().get(0).hash, t2.getInput(0).getOutpoint().getHash());
    // t5 isn't found and t4 is.
    notFound = new NotFoundMessage(PARAMS);
    notFound.addItem(new InventoryItem(InventoryItem.Type.Transaction, t5hash));
    inbound(writeTarget, notFound);
    assertFalse(futures.isDone());
    // Request t4 ...
    getdata = (GetDataMessage) outbound(writeTarget);
    assertEquals(t4.getHash(), getdata.getItems().get(0).hash);
    inbound(writeTarget, t4);
    // Continue to explore the t4 branch and ask for t6, which is in the chain.
    getdata = (GetDataMessage) outbound(writeTarget);
    assertEquals(t6hash, getdata.getItems().get(0).hash);
    notFound = new NotFoundMessage(PARAMS);
    notFound.addItem(new InventoryItem(InventoryItem.Type.Transaction, t6hash));
    inbound(writeTarget, notFound);
    pingAndWait(writeTarget);
    // That's it, we explored the entire tree.
    assertTrue(futures.isDone());
    List<Transaction> results = futures.get();
    assertEquals(3, results.size());
    assertTrue(results.contains(t2));
    assertTrue(results.contains(t3));
    assertTrue(results.contains(t4));
}
 
Example 14
Source File: PeerTest.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void recursiveDependencyDownload_depthLimited() throws Exception {
    peer.setDownloadTxDependencies(1); // Depth limit
    connect();

    // Make some fake transactions in the following graph:
    //   t1 -> t2 -> t3 -> [t4]
    // The ones in brackets are assumed to be in the chain and are represented only by hashes.
    Sha256Hash t4hash = Sha256Hash.wrap("2b801dd82f01d17bbde881687bf72bc62e2faa8ab8133d36fcb8c3abe7459da6");
    Transaction t3 = new Transaction(PARAMS);
    t3.addInput(new TransactionInput(PARAMS, t3, new byte[]{}, new TransactionOutPoint(PARAMS, 0, t4hash)));
    t3.addOutput(COIN, new ECKey());
    t3 = FakeTxBuilder.roundTripTransaction(PARAMS, t3);
    Transaction t2 = new Transaction(PARAMS);
    t2.addInput(t3.getOutput(0));
    t2.addOutput(COIN, new ECKey());
    t2 = FakeTxBuilder.roundTripTransaction(PARAMS, t2);
    Transaction t1 = new Transaction(PARAMS);
    t1.addInput(t2.getOutput(0));
    t1.addOutput(COIN, new ECKey());
    t1 = FakeTxBuilder.roundTripTransaction(PARAMS, t1);

    // Announce the first one. Wait for it to be downloaded.
    InventoryMessage inv = new InventoryMessage(PARAMS);
    inv.addTransaction(t1);
    inbound(writeTarget, inv);
    GetDataMessage getdata = (GetDataMessage) outbound(writeTarget);
    Threading.waitForUserCode();
    assertEquals(t1.getHash(), getdata.getItems().get(0).hash);
    inbound(writeTarget, t1);
    pingAndWait(writeTarget);
    // We want its dependencies so ask for them.
    ListenableFuture<List<Transaction>> futures = peer.downloadDependencies(t1);
    assertFalse(futures.isDone());
    // level 1
    getdata = (GetDataMessage) outbound(writeTarget);
    assertEquals(1, getdata.getItems().size());
    assertEquals(t2.getHash(), getdata.getItems().get(0).hash);
    inbound(writeTarget, t2);
    // no level 2
    getdata = (GetDataMessage) outbound(writeTarget);
    assertNull(getdata);

    // That's it, now double check what we've got
    pingAndWait(writeTarget);
    assertTrue(futures.isDone());
    List<Transaction> results = futures.get();
    assertEquals(1, results.size());
    assertTrue(results.contains(t2));
}
 
Example 15
Source File: WalletProtobufSerializerTest.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testAppearedAtChainHeightDepthAndWorkDone() throws Exception {
    // Test the TransactionConfidence appearedAtChainHeight, depth and workDone field are stored.

    BlockChain chain = new BlockChain(PARAMS, myWallet, new MemoryBlockStore(PARAMS));

    final ArrayList<Transaction> txns = new ArrayList<>(2);
    myWallet.addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() {
        @Override
        public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
            txns.add(tx);
        }
    });

    // Start by building two blocks on top of the genesis block.
    Block b1 = PARAMS.getGenesisBlock().createNextBlock(myAddress);
    BigInteger work1 = b1.getWork();
    assertTrue(work1.signum() > 0);

    Block b2 = b1.createNextBlock(myAddress);
    BigInteger work2 = b2.getWork();
    assertTrue(work2.signum() > 0);

    assertTrue(chain.add(b1));
    assertTrue(chain.add(b2));

    // We now have the following chain:
    //     genesis -> b1 -> b2

    // Check the transaction confidence levels are correct before wallet roundtrip.
    Threading.waitForUserCode();
    assertEquals(2, txns.size());

    TransactionConfidence confidence0 = txns.get(0).getConfidence();
    TransactionConfidence confidence1 = txns.get(1).getConfidence();

    assertEquals(1, confidence0.getAppearedAtChainHeight());
    assertEquals(2, confidence1.getAppearedAtChainHeight());

    assertEquals(2, confidence0.getDepthInBlocks());
    assertEquals(1, confidence1.getDepthInBlocks());

    // Roundtrip the wallet and check it has stored the depth and workDone.
    Wallet rebornWallet = roundTrip(myWallet);

    Set<Transaction> rebornTxns = rebornWallet.getTransactions(false);
    assertEquals(2, rebornTxns.size());

    // The transactions are not guaranteed to be in the same order so sort them to be in chain height order if required.
    Iterator<Transaction> it = rebornTxns.iterator();
    Transaction txA = it.next();
    Transaction txB = it.next();

    Transaction rebornTx0, rebornTx1;
     if (txA.getConfidence().getAppearedAtChainHeight() == 1) {
        rebornTx0 = txA;
        rebornTx1 = txB;
    } else {
        rebornTx0 = txB;
        rebornTx1 = txA;
    }

    TransactionConfidence rebornConfidence0 = rebornTx0.getConfidence();
    TransactionConfidence rebornConfidence1 = rebornTx1.getConfidence();

    assertEquals(1, rebornConfidence0.getAppearedAtChainHeight());
    assertEquals(2, rebornConfidence1.getAppearedAtChainHeight());

    assertEquals(2, rebornConfidence0.getDepthInBlocks());
    assertEquals(1, rebornConfidence1.getDepthInBlocks());
}