org.bitcoinj.testing.FakeTxBuilder Java Examples

The following examples show how to use org.bitcoinj.testing.FakeTxBuilder. 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: 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 #2
Source File: TransactionInputTest.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testStandardWalletDisconnect() throws Exception {
    NetworkParameters params = UnitTestParams.get();
    Wallet w = new Wallet(new Context(params));
    w.setCoinSelector(new AllowUnconfirmedCoinSelector());
    Address a = w.currentReceiveAddress();
    Transaction tx1 = FakeTxBuilder.createFakeTxWithoutChangeAddress(params, Coin.COIN, a);
    w.receivePending(tx1, null);
    Transaction tx2 = new Transaction(params);
    tx2.addOutput(Coin.valueOf(99000000), new ECKey());
    w.completeTx(SendRequest.forTx(tx2));

    TransactionInput txInToDisconnect = tx2.getInput(0);

    assertEquals(tx1, txInToDisconnect.getOutpoint().fromTx);
    assertNull(txInToDisconnect.getOutpoint().connectedOutput);

    txInToDisconnect.disconnect();

    assertNull(txInToDisconnect.getOutpoint().fromTx);
    assertNull(txInToDisconnect.getOutpoint().connectedOutput);
}
 
Example #3
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 #4
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 #5
Source File: PaymentProtocolTest.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testPaymentMessage() throws Exception {
    // Create
    List<Transaction> transactions = new LinkedList<>();
    transactions.add(FakeTxBuilder.createFakeTx(NETWORK_PARAMS, AMOUNT, TO_ADDRESS));
    Coin refundAmount = Coin.SATOSHI;
    Address refundAddress = new ECKey().toAddress(NETWORK_PARAMS);
    Payment payment = PaymentProtocol.createPaymentMessage(transactions, refundAmount, refundAddress, MEMO,
            MERCHANT_DATA);
    byte[] paymentBytes = payment.toByteArray();

    // Parse
    Payment parsedPayment = Payment.parseFrom(paymentBytes);
    List<Transaction> parsedTransactions = PaymentProtocol.parseTransactionsFromPaymentMessage(NETWORK_PARAMS,
            parsedPayment);
    assertEquals(transactions, parsedTransactions);
    assertEquals(1, parsedPayment.getRefundToCount());
    assertEquals(MEMO, parsedPayment.getMemo());
    assertArrayEquals(MERCHANT_DATA, parsedPayment.getMerchantData().toByteArray());
}
 
Example #6
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 #7
Source File: TransactionInputTest.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testStandardWalletDisconnect() throws Exception {
    NetworkParameters params = UnitTestParams.get();
    Wallet w = new Wallet(new Context(params));
    w.setCoinSelector(new AllowUnconfirmedCoinSelector());
    Address a = w.currentReceiveAddress();
    Transaction tx1 = FakeTxBuilder.createFakeTxWithoutChangeAddress(params, Coin.COIN, a);
    w.receivePending(tx1, null);
    Transaction tx2 = new Transaction(params);
    tx2.addOutput(Coin.valueOf(99000000), new ECKey());
    w.completeTx(SendRequest.forTx(tx2));

    TransactionInput txInToDisconnect = tx2.getInput(0);

    assertEquals(tx1, txInToDisconnect.getOutpoint().fromTx);
    assertNull(txInToDisconnect.getOutpoint().connectedOutput);

    txInToDisconnect.disconnect();

    assertNull(txInToDisconnect.getOutpoint().fromTx);
    assertNull(txInToDisconnect.getOutpoint().connectedOutput);
}
 
Example #8
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 #9
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 #10
Source File: PaymentProtocolTest.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testPaymentMessage() throws Exception {
    // Create
    List<Transaction> transactions = new LinkedList<>();
    transactions.add(FakeTxBuilder.createFakeTx(NETWORK_PARAMS, AMOUNT, TO_ADDRESS));
    Coin refundAmount = Coin.SATOSHI;
    Address refundAddress = new ECKey().toAddress(NETWORK_PARAMS);
    Payment payment = PaymentProtocol.createPaymentMessage(transactions, refundAmount, refundAddress, MEMO,
            MERCHANT_DATA);
    byte[] paymentBytes = payment.toByteArray();

    // Parse
    Payment parsedPayment = Payment.parseFrom(paymentBytes);
    List<Transaction> parsedTransactions = PaymentProtocol.parseTransactionsFromPaymentMessage(NETWORK_PARAMS,
            parsedPayment);
    assertEquals(transactions, parsedTransactions);
    assertEquals(1, parsedPayment.getRefundToCount());
    assertEquals(MEMO, parsedPayment.getMemo());
    assertArrayEquals(MERCHANT_DATA, parsedPayment.getMerchantData().toByteArray());
}
 
Example #11
Source File: PaymentProtocolTest.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testPaymentMessage() throws Exception {
    // Create
    List<Transaction> transactions = new LinkedList<>();
    transactions.add(FakeTxBuilder.createFakeTx(UNITTEST, AMOUNT, TO_ADDRESS));
    Coin refundAmount = Coin.SATOSHI;
    Address refundAddress = LegacyAddress.fromKey(UNITTEST, new ECKey());
    Payment payment = PaymentProtocol.createPaymentMessage(transactions, refundAmount, refundAddress, MEMO,
            MERCHANT_DATA);
    byte[] paymentBytes = payment.toByteArray();

    // Parse
    Payment parsedPayment = Payment.parseFrom(paymentBytes);
    List<Transaction> parsedTransactions = PaymentProtocol.parseTransactionsFromPaymentMessage(UNITTEST,
            parsedPayment);
    assertEquals(transactions, parsedTransactions);
    assertEquals(1, parsedPayment.getRefundToCount());
    assertEquals(MEMO, parsedPayment.getMemo());
    assertArrayEquals(MERCHANT_DATA, parsedPayment.getMerchantData().toByteArray());
}
 
Example #12
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 #13
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 #14
Source File: TransactionInputTest.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testStandardWalletDisconnect() throws Exception {
    Wallet w = new Wallet(new Context(UNITTEST));
    w.setCoinSelector(new AllowUnconfirmedCoinSelector());
    Address a = w.currentReceiveAddress();
    Transaction tx1 = FakeTxBuilder.createFakeTxWithoutChangeAddress(UNITTEST, Coin.COIN, a);
    w.receivePending(tx1, null);
    Transaction tx2 = new Transaction(UNITTEST);
    tx2.addOutput(Coin.valueOf(99000000), new ECKey());
    w.completeTx(SendRequest.forTx(tx2));

    TransactionInput txInToDisconnect = tx2.getInput(0);

    assertEquals(tx1, txInToDisconnect.getOutpoint().fromTx);
    assertNull(txInToDisconnect.getOutpoint().connectedOutput);

    txInToDisconnect.disconnect();

    assertNull(txInToDisconnect.getOutpoint().fromTx);
    assertNull(txInToDisconnect.getOutpoint().connectedOutput);
}
 
Example #15
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 #16
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 #17
Source File: BlockChainTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
private void testDeprecatedBlockVersion(final long deprecatedVersion, final long newVersion)
        throws Exception {
    final BlockStore versionBlockStore = new MemoryBlockStore(PARAMS);
    final BlockChain versionChain = new BlockChain(PARAMS, versionBlockStore);

    // Build a historical chain of version 3 blocks
    long timeSeconds = 1231006505;
    int height = 0;
    FakeTxBuilder.BlockPair chainHead = null;

    // Put in just enough v2 blocks to be a minority
    for (height = 0; height < (PARAMS.getMajorityWindow() - PARAMS.getMajorityRejectBlockOutdated()); height++) {
        chainHead = FakeTxBuilder.createFakeBlock(versionBlockStore, deprecatedVersion, timeSeconds, height);
        versionChain.add(chainHead.block);
        timeSeconds += 60;
    }
    // Fill the rest of the window with v3 blocks
    for (; height < PARAMS.getMajorityWindow(); height++) {
        chainHead = FakeTxBuilder.createFakeBlock(versionBlockStore, newVersion, timeSeconds, height);
        versionChain.add(chainHead.block);
        timeSeconds += 60;
    }

    chainHead = FakeTxBuilder.createFakeBlock(versionBlockStore, deprecatedVersion, timeSeconds, height);
    // Trying to add a new v2 block should result in rejection
    thrown.expect(VerificationException.BlockVersionOutOfDate.class);
    try {
        versionChain.add(chainHead.block);
    } catch(final VerificationException ex) {
        throw (Exception) ex.getCause();
    }
}
 
Example #18
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 #19
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 #20
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 #21
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 #22
Source File: BlockChainTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
private void testDeprecatedBlockVersion(final long deprecatedVersion, final long newVersion)
        throws Exception {
    final BlockStore versionBlockStore = new MemoryBlockStore(UNITTEST);
    final BlockChain versionChain = new BlockChain(UNITTEST, versionBlockStore);

    // Build a historical chain of version 3 blocks
    long timeSeconds = 1231006505;
    int height = 0;
    FakeTxBuilder.BlockPair chainHead = null;

    // Put in just enough v2 blocks to be a minority
    for (height = 0; height < (UNITTEST.getMajorityWindow() - UNITTEST.getMajorityRejectBlockOutdated()); height++) {
        chainHead = FakeTxBuilder.createFakeBlock(versionBlockStore, deprecatedVersion, timeSeconds, height);
        versionChain.add(chainHead.block);
        timeSeconds += 60;
    }
    // Fill the rest of the window with v3 blocks
    for (; height < UNITTEST.getMajorityWindow(); height++) {
        chainHead = FakeTxBuilder.createFakeBlock(versionBlockStore, newVersion, timeSeconds, height);
        versionChain.add(chainHead.block);
        timeSeconds += 60;
    }

    chainHead = FakeTxBuilder.createFakeBlock(versionBlockStore, deprecatedVersion, timeSeconds, height);
    // Trying to add a new v2 block should result in rejection
    thrown.expect(VerificationException.BlockVersionOutOfDate.class);
    try {
        versionChain.add(chainHead.block);
    } catch (final VerificationException ex) {
        throw (Exception) ex.getCause();
    }
}
 
Example #23
Source File: BlockChainTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
private void testDeprecatedBlockVersion(final long deprecatedVersion, final long newVersion)
        throws Exception {
    final BlockStore versionBlockStore = new MemoryBlockStore(PARAMS);
    final BlockChain versionChain = new BlockChain(PARAMS, versionBlockStore);

    // Build a historical chain of version 3 blocks
    long timeSeconds = 1231006505;
    int height = 0;
    FakeTxBuilder.BlockPair chainHead = null;

    // Put in just enough v2 blocks to be a minority
    for (height = 0; height < (PARAMS.getMajorityWindow() - PARAMS.getMajorityRejectBlockOutdated()); height++) {
        chainHead = FakeTxBuilder.createFakeBlock(versionBlockStore, deprecatedVersion, timeSeconds, height);
        versionChain.add(chainHead.block);
        timeSeconds += 60;
    }
    // Fill the rest of the window with v3 blocks
    for (; height < PARAMS.getMajorityWindow(); height++) {
        chainHead = FakeTxBuilder.createFakeBlock(versionBlockStore, newVersion, timeSeconds, height);
        versionChain.add(chainHead.block);
        timeSeconds += 60;
    }

    chainHead = FakeTxBuilder.createFakeBlock(versionBlockStore, deprecatedVersion, timeSeconds, height);
    // Trying to add a new v2 block should result in rejection
    thrown.expect(VerificationException.BlockVersionOutOfDate.class);
    try {
        versionChain.add(chainHead.block);
    } catch(final VerificationException ex) {
        throw (Exception) ex.getCause();
    }
}
 
Example #24
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 #25
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 #26
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 #27
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 #28
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 #29
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 #30
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));
}