Java Code Examples for org.bitcoinj.testing.FakeTxBuilder#roundTripTransaction()

The following examples show how to use org.bitcoinj.testing.FakeTxBuilder#roundTripTransaction() . 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: 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 2
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 3
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 4
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 5
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 6
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));
}