@Test
 public void coinbaseTxns() throws Exception {
   // Covers issue 420 where the outpoint index of a coinbase tx input was being mis-serialized.
   Block b =
       params
           .getGenesisBlock()
           .createNextBlockWithCoinbase(
               Block.BLOCK_VERSION_GENESIS,
               myKey.getPubKey(),
               FIFTY_COINS,
               Block.BLOCK_HEIGHT_GENESIS);
   Transaction coinbase = b.getTransactions().get(0);
   assertTrue(coinbase.isCoinBase());
   BlockChain chain = new BlockChain(params, myWallet, new MemoryBlockStore(params));
   assertTrue(chain.add(b));
   // Wallet now has a coinbase tx in it.
   assertEquals(1, myWallet.getTransactions(true).size());
   assertTrue(myWallet.getTransaction(coinbase.getHash()).isCoinBase());
   Wallet wallet2 = roundTrip(myWallet);
   assertEquals(1, wallet2.getTransactions(true).size());
   assertTrue(wallet2.getTransaction(coinbase.getHash()).isCoinBase());
 }
Exemplo n.º 2
0
  public static void main(String[] args) throws Exception {
    NetworkParameters params = TestNet3Params.get();

    // Bitcoinj supports hierarchical deterministic wallets (or "HD Wallets"):
    // https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
    // HD wallets allow you to restore your wallet simply from a root seed. This seed can be
    // represented using a short mnemonic sentence as described in BIP 39:
    // https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki

    // Here we restore our wallet from a seed with no passphrase. Also have a look at the
    // BackupToMnemonicSeed.java example that shows how to backup a wallet by creating a mnemonic
    // sentence.
    String seedCode =
        "yard impulse luxury drive today throw farm pepper survey wreck glass federal";
    String passphrase = "";
    Long creationtime = 1409478661L;

    DeterministicSeed seed = new DeterministicSeed(seedCode, null, passphrase, creationtime);

    // The wallet class provides a easy fromSeed() function that loads a new wallet from a given
    // seed.
    Wallet wallet = Wallet.fromSeed(params, seed);

    // Because we are importing an existing wallet which might already have transactions we must
    // re-download the blockchain to make the wallet picks up these transactions
    // You can find some information about this in the guides:
    // https://bitcoinj.github.io/working-with-the-wallet#setup
    // To do this we clear the transactions of the wallet and delete a possible existing blockchain
    // file before we download the blockchain again further down.
    System.out.println(wallet.toString());
    wallet.clearTransactions(0);
    File chainFile = new File("restore-from-seed.spvchain");
    if (chainFile.exists()) {
      chainFile.delete();
    }

    // Setting up the BlochChain, the BlocksStore and connecting to the network.
    SPVBlockStore chainStore = new SPVBlockStore(params, chainFile);
    BlockChain chain = new BlockChain(params, chainStore);
    PeerGroup peers = new PeerGroup(params, chain);
    peers.addPeerDiscovery(new DnsDiscovery(params));

    // Now we need to hook the wallet up to the blockchain and the peers. This registers event
    // listeners that notify our wallet about new transactions.
    chain.addWallet(wallet);
    peers.addWallet(wallet);

    DownloadProgressTracker bListener =
        new DownloadProgressTracker() {
          @Override
          public void doneDownload() {
            System.out.println("blockchain downloaded");
          }
        };

    // Now we re-download the blockchain. This replays the chain into the wallet. Once this is
    // completed our wallet should know of all its transactions and print the correct balance.
    peers.start();
    peers.startBlockChainDownload(bListener);

    bListener.await();

    // Print a debug message with the details about the wallet. The correct balance should now be
    // displayed.
    System.out.println(wallet.toString());

    // shutting down again
    peers.stop();
  }
  @Test
  public void testAppearedAtChainHeightDepthAndWorkDone() throws Exception {
    // Test the TransactionConfidence appearedAtChainHeight, depth and workDone field are stored.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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