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

The following examples show how to use org.bitcoinj.testing.FakeTxBuilder#createFakeTx() . 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: DefaultRiskAnalysisTest.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void transactionVersions() throws Exception {
    Transaction tx = FakeTxBuilder.createFakeTx(MAINNET);
    tx.setVersion(1);
    DefaultRiskAnalysis analysis = DefaultRiskAnalysis.FACTORY.create(wallet, tx, NO_DEPS);
    assertEquals(RiskAnalysis.Result.OK, analysis.analyze());

    tx.setVersion(2);
    analysis = DefaultRiskAnalysis.FACTORY.create(wallet, tx, NO_DEPS);
    assertEquals(RiskAnalysis.Result.OK, analysis.analyze());

    tx.setVersion(3);
    analysis = DefaultRiskAnalysis.FACTORY.create(wallet, tx, NO_DEPS);
    assertEquals(RiskAnalysis.Result.NON_STANDARD, analysis.analyze());
    assertEquals(tx, analysis.getNonStandard());
}
 
Example 2
Source File: BlockChainTest.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void intraBlockDependencies() throws Exception {
    // Covers issue 166 in which transactions that depend on each other inside a block were not always being
    // considered relevant.
    Address somebodyElse = LegacyAddress.fromKey(UNITTEST, new ECKey());
    Block b1 = UNITTEST.getGenesisBlock().createNextBlock(somebodyElse);
    ECKey key = wallet.freshReceiveKey();
    Address addr = LegacyAddress.fromKey(UNITTEST, key);
    // Create a tx that gives us some coins, and another that spends it to someone else in the same block.
    Transaction t1 = FakeTxBuilder.createFakeTx(UNITTEST, COIN, addr);
    Transaction t2 = new Transaction(UNITTEST);
    t2.addInput(t1.getOutputs().get(0));
    t2.addOutput(valueOf(2, 0), somebodyElse);
    b1.addTransaction(t1);
    b1.addTransaction(t2);
    b1.solve();
    chain.add(b1);
    assertEquals(Coin.ZERO, wallet.getBalance());
}
 
Example 3
Source File: BlockChainTest.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void intraBlockDependencies() throws Exception {
    // Covers issue 166 in which transactions that depend on each other inside a block were not always being
    // considered relevant.
    Address somebodyElse = new ECKey().toAddress(PARAMS);
    Block b1 = PARAMS.getGenesisBlock().createNextBlock(somebodyElse);
    ECKey key = wallet.freshReceiveKey();
    Address addr = key.toAddress(PARAMS);
    // Create a tx that gives us some coins, and another that spends it to someone else in the same block.
    Transaction t1 = FakeTxBuilder.createFakeTx(PARAMS, COIN, addr);
    Transaction t2 = new Transaction(PARAMS);
    t2.addInput(t1.getOutputs().get(0));
    t2.addOutput(valueOf(2, 0), somebodyElse);
    b1.addTransaction(t1);
    b1.addTransaction(t2);
    b1.solve();
    chain.add(b1);
    assertEquals(Coin.ZERO, wallet.getBalance());
}
 
Example 4
Source File: ElectrumMultiWalletTest.java    From java-stratum with Apache License 2.0 6 votes vote down vote up
@Test
public void markKeysAsUsedDisorder() throws Exception {
    control.replay();
    DeterministicKey key1 = wallet.currentReceiveKey();
    String a1 = "mfsh3sGu8SzxRZXDRPMbwdCykDfdiXLTVQ";
    String a2 = "mpkchvF3Twgpd5AEmrRZM3TENT8V7Ygi8T";

    Transaction tx1 = FakeTxBuilder.createFakeTx(params, Coin.CENT, new Address(params, a2));
    multiWallet.addPendingDownload(tx1.getHash());
    multiWallet.receive(tx1, 0);
    DeterministicKey key2 = wallet.currentReceiveKey();
    assertEquals(wallet.currentReceiveKey(), key1);

    Transaction tx2 = FakeTxBuilder.createFakeTx(params, Coin.CENT, new Address(params, a1));
    multiWallet.addPendingDownload(tx2.getHash());
    multiWallet.receive(tx2, 0);
    assertNotEquals(wallet.currentReceiveKey(), key2);
    assertNotEquals(wallet.currentReceiveKey().toAddress(params), a1);
    assertNotEquals(wallet.currentReceiveKey().toAddress(params), a2);
    control.verify();
}
 
Example 5
Source File: BlockChainTest.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void intraBlockDependencies() throws Exception {
    // Covers issue 166 in which transactions that depend on each other inside a block were not always being
    // considered relevant.
    Address somebodyElse = new ECKey().toAddress(PARAMS);
    Block b1 = PARAMS.getGenesisBlock().createNextBlock(somebodyElse);
    ECKey key = wallet.freshReceiveKey();
    Address addr = key.toAddress(PARAMS);
    // Create a tx that gives us some coins, and another that spends it to someone else in the same block.
    Transaction t1 = FakeTxBuilder.createFakeTx(PARAMS, COIN, addr);
    Transaction t2 = new Transaction(PARAMS);
    t2.addInput(t1.getOutputs().get(0));
    t2.addOutput(valueOf(2, 0), somebodyElse);
    b1.addTransaction(t1);
    b1.addTransaction(t2);
    b1.solve();
    chain.add(b1);
    assertEquals(Coin.ZERO, wallet.getBalance());
}
 
Example 6
Source File: DefaultRiskAnalysisTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void optInFullRBF() throws Exception {
    Transaction tx = FakeTxBuilder.createFakeTx(MAINNET);
    tx.getInput(0).setSequenceNumber(TransactionInput.NO_SEQUENCE - 2);
    DefaultRiskAnalysis analysis = DefaultRiskAnalysis.FACTORY.create(wallet, tx, NO_DEPS);
    assertEquals(RiskAnalysis.Result.NON_FINAL, analysis.analyze());
    assertEquals(tx, analysis.getNonFinal());
}
 
Example 7
Source File: DefaultRiskAnalysisTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void relativeLockTime() throws Exception {
    Transaction tx = FakeTxBuilder.createFakeTx(MAINNET);
    tx.setVersion(2);
    checkState(!tx.hasRelativeLockTime());

    tx.getInput(0).setSequenceNumber(TransactionInput.NO_SEQUENCE);
    DefaultRiskAnalysis analysis = DefaultRiskAnalysis.FACTORY.create(wallet, tx, NO_DEPS);
    assertEquals(RiskAnalysis.Result.OK, analysis.analyze());

    tx.getInput(0).setSequenceNumber(0);
    analysis = DefaultRiskAnalysis.FACTORY.create(wallet, tx, NO_DEPS);
    assertEquals(RiskAnalysis.Result.NON_FINAL, analysis.analyze());
    assertEquals(tx, analysis.getNonFinal());
}
 
Example 8
Source File: ChainSplitTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void orderingInsideBlock() throws Exception {
    // Test that transactions received in the same block have their ordering preserved when reorganising.
    // This covers issue 468.

    // Receive some money to the wallet.
    Transaction t1 = FakeTxBuilder.createFakeTx(UNITTEST, COIN, coinsTo);
    final Block b1 = FakeTxBuilder.makeSolvedTestBlock(UNITTEST.genesisBlock, t1);
    chain.add(b1);

    // Send a couple of payments one after the other (so the second depends on the change output of the first).
    wallet.allowSpendingUnconfirmedTransactions();
    Transaction t2 = checkNotNull(wallet.createSend(LegacyAddress.fromKey(UNITTEST, new ECKey()), CENT));
    wallet.commitTx(t2);
    Transaction t3 = checkNotNull(wallet.createSend(LegacyAddress.fromKey(UNITTEST, new ECKey()), CENT));
    wallet.commitTx(t3);
    chain.add(FakeTxBuilder.makeSolvedTestBlock(b1, t2, t3));

    final Coin coins0point98 = COIN.subtract(CENT).subtract(CENT);
    assertEquals(coins0point98, wallet.getBalance());

    // Now round trip the wallet and force a re-org.
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    wallet.saveToFileStream(bos);
    wallet = Wallet.loadFromFileStream(new ByteArrayInputStream(bos.toByteArray()));
    final Block b2 = FakeTxBuilder.makeSolvedTestBlock(b1, t2, t3);
    final Block b3 = FakeTxBuilder.makeSolvedTestBlock(b2);
    chain.add(b2);
    chain.add(b3);

    // And verify that the balance is as expected. Because new ECKey() is non-deterministic, if the order
    // isn't being stored correctly this should fail 50% of the time.
    assertEquals(coins0point98, wallet.getBalance());
}
 
Example 9
Source File: DefaultRiskAnalysisTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void optInFullRBF() throws Exception {
    Transaction tx = FakeTxBuilder.createFakeTx(PARAMS);
    tx.getInput(0).setSequenceNumber(TransactionInput.NO_SEQUENCE - 2);
    DefaultRiskAnalysis analysis = DefaultRiskAnalysis.FACTORY.create(wallet, tx, NO_DEPS);
    assertEquals(RiskAnalysis.Result.NON_FINAL, analysis.analyze());
    assertEquals(tx, analysis.getNonFinal());
}
 
Example 10
Source File: ChainSplitTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void orderingInsideBlock() throws Exception {
    // Test that transactions received in the same block have their ordering preserved when reorganising.
    // This covers issue 468.

    // Receive some money to the wallet.
    Transaction t1 = FakeTxBuilder.createFakeTx(PARAMS, COIN, coinsTo);
    final Block b1 = FakeTxBuilder.makeSolvedTestBlock(PARAMS.genesisBlock, t1);
    chain.add(b1);

    // Send a couple of payments one after the other (so the second depends on the change output of the first).
    wallet.allowSpendingUnconfirmedTransactions();
    Transaction t2 = checkNotNull(wallet.createSend(new ECKey().toAddress(PARAMS), CENT));
    wallet.commitTx(t2);
    Transaction t3 = checkNotNull(wallet.createSend(new ECKey().toAddress(PARAMS), CENT));
    wallet.commitTx(t3);
    chain.add(FakeTxBuilder.makeSolvedTestBlock(b1, t2, t3));

    final Coin coins0point98 = COIN.subtract(CENT).subtract(CENT);
    assertEquals(coins0point98, wallet.getBalance());

    // Now round trip the wallet and force a re-org.
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    wallet.saveToFileStream(bos);
    wallet = Wallet.loadFromFileStream(new ByteArrayInputStream(bos.toByteArray()));
    final Block b2 = FakeTxBuilder.makeSolvedTestBlock(b1, t2, t3);
    final Block b3 = FakeTxBuilder.makeSolvedTestBlock(b2);
    chain.add(b2);
    chain.add(b3);

    // And verify that the balance is as expected. Because new ECKey() is non-deterministic, if the order
    // isn't being stored correctly this should fail 50% of the time.
    assertEquals(coins0point98, wallet.getBalance());
}
 
Example 11
Source File: ElectrumMultiWalletTest.java    From java-stratum with Apache License 2.0 5 votes vote down vote up
@Test
public void markKeysAsUsed() throws Exception {
    control.replay();
    DeterministicKey key1 = wallet.currentReceiveKey();
    Transaction tx1 = FakeTxBuilder.createFakeTx(params, Coin.CENT, key1.toAddress(params));
    multiWallet.addPendingDownload(tx1.getHash());
    multiWallet.receive(tx1, 0);
    DeterministicKey key2 = wallet.currentReceiveKey();
    assertNotEquals(wallet.currentReceiveKey(), key1);
    Transaction tx2 = FakeTxBuilder.createFakeTx(params, Coin.CENT, key2.toAddress(params));
    multiWallet.addPendingDownload(tx2.getHash());
    multiWallet.receive(tx2, 0);
    assertNotEquals(wallet.currentReceiveKey(), key2);
    control.verify();
}
 
Example 12
Source File: DefaultRiskAnalysisTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void optInFullRBF() throws Exception {
    Transaction tx = FakeTxBuilder.createFakeTx(PARAMS);
    tx.getInput(0).setSequenceNumber(TransactionInput.NO_SEQUENCE - 2);
    DefaultRiskAnalysis analysis = DefaultRiskAnalysis.FACTORY.create(wallet, tx, NO_DEPS);
    assertEquals(RiskAnalysis.Result.NON_FINAL, analysis.analyze());
    assertEquals(tx, analysis.getNonFinal());
}
 
Example 13
Source File: ChainSplitTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void orderingInsideBlock() throws Exception {
    // Test that transactions received in the same block have their ordering preserved when reorganising.
    // This covers issue 468.

    // Receive some money to the wallet.
    Transaction t1 = FakeTxBuilder.createFakeTx(PARAMS, COIN, coinsTo);
    final Block b1 = FakeTxBuilder.makeSolvedTestBlock(PARAMS.genesisBlock, t1);
    chain.add(b1);

    // Send a couple of payments one after the other (so the second depends on the change output of the first).
    wallet.allowSpendingUnconfirmedTransactions();
    Transaction t2 = checkNotNull(wallet.createSend(new ECKey().toAddress(PARAMS), CENT));
    wallet.commitTx(t2);
    Transaction t3 = checkNotNull(wallet.createSend(new ECKey().toAddress(PARAMS), CENT));
    wallet.commitTx(t3);
    chain.add(FakeTxBuilder.makeSolvedTestBlock(b1, t2, t3));

    final Coin coins0point98 = COIN.subtract(CENT).subtract(CENT);
    assertEquals(coins0point98, wallet.getBalance());

    // Now round trip the wallet and force a re-org.
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    wallet.saveToFileStream(bos);
    wallet = Wallet.loadFromFileStream(new ByteArrayInputStream(bos.toByteArray()));
    final Block b2 = FakeTxBuilder.makeSolvedTestBlock(b1, t2, t3);
    final Block b3 = FakeTxBuilder.makeSolvedTestBlock(b2);
    chain.add(b2);
    chain.add(b3);

    // And verify that the balance is as expected. Because new ECKey() is non-deterministic, if the order
    // isn't being stored correctly this should fail 50% of the time.
    assertEquals(coins0point98, wallet.getBalance());
}
 
Example 14
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 15
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 16
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));
}