Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.95 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.File;
  4. import java.util.LinkedList;
  5. import java.util.List;
  6. import java.util.Locale;
  7.  
  8.  
  9. import org.bitcoinj.core.*;
  10. import org.bitcoinj.params.MainNetParams;
  11. import org.bitcoinj.utils.BlockFileLoader;
  12.  
  13.  
  14. public class BitcoinReader{
  15.  
  16.     //This is the path where my blk*.dats can be found
  17.     static String PATH = "d:/bitcoin_blockchain/blocks/";
  18.  
  19.     public void read() {
  20.  
  21.         // Just some initial setup
  22.         NetworkParameters np = new MainNetParams();
  23.         Context.getOrCreate(MainNetParams.get());
  24.  
  25.         // We create a BlockFileLoader object by passing a list of files.
  26.         // The list of files is built with the method buildList(), see
  27.         // below for its definition.
  28.         BlockFileLoader loader = new BlockFileLoader(np,buildList());
  29.  
  30.         int blockCounter = 0;
  31.  
  32.         try{
  33.             for (Block block : loader) {
  34.  
  35.                 blockCounter++;
  36.                 // There you can see the progress
  37.                 System.out.println("Block #"+blockCounter);
  38.  
  39.                 //i get the exception after this block #481815
  40.                 if(blockCounter == 481815){
  41.                     System.out.println("Debugging");
  42.                 }
  43.             }
  44.         } catch (Exception e){
  45.             e.printStackTrace();
  46.         }
  47.  
  48.     }
  49.  
  50.  
  51.     // The method returns a list of files in a directory according to a certain
  52.     // pattern (block files have name blkNNNNN.dat)
  53.     private List<File> buildList() {
  54.         List<File> list = new LinkedList<File>();
  55.         for (int i = 0; true; i++) {
  56.             File file = new File(PATH + String.format(Locale.US, "blk%05d.dat", i));
  57.             if (!file.exists())
  58.                 break;
  59.             list.add(file);
  60.         }
  61.         return list;
  62.     }
  63.  
  64.  
  65.     // Main method: simply invoke everything
  66.     public static void main(String[] args) {
  67.         BitcoinReader br = new BitcoinReader();
  68.         br.read();
  69.     }
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement