Guest User

Primecoin Analyzer

a guest
Dec 8th, 2013
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.98 KB | None | 0 0
  1. package primecoanalysis;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.math.BigInteger;
  6. import java.util.ArrayList;
  7. import java.util.Scanner;
  8.  
  9.  
  10. /**
  11.  * Simple parser for blocks saved in the getblock output from primecoind
  12.  *
  13.  * @author Koooooj AFozUk53j5uZCrKAhkcrM6SjnKtGRXCnov
  14.  */
  15. public class PrimecoAnalysis {//Primeco instead of Primecoin because I fat fingered it and don't feel like chaingin.
  16.  
  17.     public static void main(String[] pickles) throws IOException {
  18.         File inFile = new File("blockdump.txt");
  19.         Scanner in = new Scanner(inFile);
  20.         ArrayList<Block> blocks = new ArrayList<>();
  21.         while(in.hasNextLine())//read in all blocks
  22.         {
  23.             String openCurly = in.nextLine();
  24.             if(! openCurly.equals("{")) {
  25.                 throw new IllegalStateException("open curly failed");
  26.             }
  27.             ArrayList<String> lines = new ArrayList<>();
  28.             String line = in.nextLine();
  29.             while(!line.equals("}")){
  30.                 lines.add(line);
  31.                 line = in.nextLine();
  32.             }
  33.             blocks.add(new Block(lines));
  34.             if(blocks.size()%100 == 0) {
  35.                 System.out.println(blocks.size());
  36.             }
  37.            
  38.         }
  39.        
  40.         for(int i = 0; i<blocks.size(); i++)//continuity check.
  41.         {
  42.             if(blocks.get(i).height != i) {
  43.                 System.out.println("ERROR!!" + (blocks.get(i).height - i));
  44.                 return;
  45.             }
  46.         }
  47.        
  48.         //analysis!!
  49.         //Longest chains!
  50.         int longestChain = 0, longestChainHeight = 0;
  51.         int longestCC1 = 0, longestCC1Height = 0;
  52.         int longestCC2 = 0, longestCC2Height = 0;
  53.         int longestTWN = 0, longestTWNHeight = 0;
  54.        
  55.         //Chain type count!
  56.         int countCC1 = 0, countCC2 = 0, countTWN = 0;
  57.        
  58.         //length histograms!
  59.         int lengthCount[] = new int[20];
  60.         int lengthCountCC1[] = new int[20], lengthCountCC2[] = new int[20], lengthCountTWN[] = new int[20];
  61.        
  62.         //size histograms!
  63.         final int sizeRange = 200;
  64.         int sizeCount[] = new int[sizeRange];
  65.         int sizeCountCC1[] = new int[sizeRange], sizeCountCC2[] = new int[sizeRange], sizeCountTWN[] = new int[sizeRange];
  66.         final int sizeOffset = 80;
  67.        
  68.         //Largest/Smallest Prime Origins
  69.         BigInteger largestPrime = BigInteger.ZERO;
  70.         int largestPrimeHeight = 0;
  71.         BigInteger smallestPrime = blocks.get(0).primeorigin;
  72.         int smallestPrimeHeight = 0;
  73.        
  74.         for(Block b: blocks)
  75.         {
  76.             if(b.chainLength > longestChain) {
  77.                 longestChain = b.chainLength;
  78.                 longestChainHeight = b.height;
  79.             }
  80.             if(b.primeorigin.compareTo(largestPrime) > 0){//no greater than in BigInteger... grumble grumble grumble
  81.                 largestPrime = b.primeorigin;
  82.                 largestPrimeHeight = b.height;
  83.             }
  84.             if(b.primeorigin.compareTo(smallestPrime)<0){
  85.                 smallestPrime = b.primeorigin;
  86.                 smallestPrimeHeight = b.height;
  87.             }
  88.            
  89.             lengthCount[b.chainLength]++;//will throw out-of-bounds exceptions if not enough space.
  90.             sizeCount[b.primeSize-sizeOffset]++;
  91.            
  92.             if(b.chainType==ChainType.CC1 && b.chainLength > longestCC1){
  93.                 longestCC1 = b.chainLength;
  94.                 longestCC1Height = b.height;
  95.             }
  96.            
  97.             if(b.chainType==ChainType.CC2 && b.chainLength > longestCC2){
  98.                 longestCC2 = b.chainLength;
  99.                 longestCC2Height = b.height;
  100.             }
  101.            
  102.             if(b.chainType==ChainType.TWN && b.chainLength > longestTWN){
  103.                 longestTWN = b.chainLength;
  104.                 longestTWNHeight = b.height;
  105.             }
  106.            
  107.             switch(b.chainType){
  108.                 case CC1:
  109.                     countCC1++;
  110.                     lengthCountCC1[b.chainLength]++;
  111.                     sizeCountCC1[b.primeSize-sizeOffset]++;
  112.                     break;
  113.                 case CC2:
  114.                     countCC2++;
  115.                     lengthCountCC2[b.chainLength]++;
  116.                     sizeCountCC2[b.primeSize-sizeOffset]++;
  117.                     break;
  118.                 case TWN:
  119.                     countTWN++;
  120.                     lengthCountTWN[b.chainLength]++;
  121.                     sizeCountTWN[b.primeSize-sizeOffset]++;
  122.                     break;
  123.                 default: throw new IllegalStateException("A chain has no type!");
  124.             }
  125.         }
  126.        
  127.         System.out.println("Longest chain is " + longestChain + " primes long, found at height " + longestChainHeight);
  128.         System.out.println("Longest CC1 is " + longestCC1 + " primes long, found at height " + longestCC1Height);
  129.         System.out.println("Longest CC2 is " + longestCC2 + " primes long, found at height " + longestCC2Height);
  130.         System.out.println("Longest TWN is " + longestTWN + " primes long, found at height " + longestTWNHeight);
  131.         System.out.println("\n\n\n");
  132.        
  133.         System.out.println("There are "+ countCC1 + " Cunningham chains of the first kind, " + countCC2 + " of the second, and " + countTWN + " bi-twin chains.\n\n\n\n");
  134.        
  135.         System.out.println("Length histogram!");
  136.         System.out.println("Length, Total, CC1, CC2, TWN");
  137.         for(int i = 0; i < 20; i++){
  138.             System.out.println(i + " , " + lengthCount[i] + " , " + lengthCountCC1[i] + " , " + lengthCountCC2[i] + " , " + lengthCountTWN[i]);
  139.         }
  140.         System.out.println("\n\n\n");
  141.        
  142.         System.out.println("Size histogram!");
  143.         System.out.println("Size, Total, CC1, CC2, TWN");
  144.         for(int i = 0; i < sizeRange; i++){
  145.             System.out.println((i + sizeOffset) + " , " + sizeCount[i] + " , " + sizeCountCC1[i] + " , " + sizeCountCC2[i] + " , " + sizeCountTWN[i]);
  146.         }
  147.         System.out.println("\n\n\n");
  148.        
  149.         System.out.println("The largest prime origin is " + largestPrime + " which occurs at height " + largestPrimeHeight);
  150.         System.out.println("The smallest prime origin is " + smallestPrime + " which occurs at height " + smallestPrimeHeight);
  151.         System.out.println("\n\n\n");
  152.     }
  153.    
  154.     private static class Block//struct to hold all the data of a block's header, plus the method to parse it from a String
  155.     {
  156.         //All the items in the header, as they are listed in getblock's output
  157.         String hash;
  158.         int confirmations;
  159.         int size;
  160.         int height;
  161.         int version;
  162.         String merkleroot;
  163.         ArrayList<String> tx;
  164.         long time;//no see
  165.         long nonce;//long because it ought to be 32 unsigned bits and java is sutpid--no unsinged modifier
  166.         long bits;
  167.         double difficulty;
  168.         double transition;
  169.         String primechain;
  170.         BigInteger primeorigin;
  171.         String prevriousblockhash;
  172.         String nextblockhash;
  173.        
  174.         //a few derrived pieces of data
  175.         ChainType chainType;
  176.         int chainLength;//only counting full primes.
  177.         int primeSize;//number of base-10 digits
  178.        
  179.         //static int n = 0;
  180.        
  181.         public Block(ArrayList<String> input){
  182.             //System.out.println(n++);
  183.             String tempstring;
  184.             tempstring = input.get(0).split(":")[1];//remove "hash" :
  185.             hash = tempstring.substring(2, tempstring.length()-2);//remove quotes, space, and comma
  186.             //System.out.println(hash);
  187.            
  188.             tempstring = input.get(1).split(":")[1];
  189.             confirmations = Integer.parseInt(tempstring.substring(1, tempstring.length()-1));
  190.             //System.out.println(confirmations);
  191.            
  192.             tempstring = input.get(2).split(":")[1];
  193.             size = Integer.parseInt(tempstring.substring(1, tempstring.length()-1));
  194.             //System.out.println(size);
  195.            
  196.             tempstring = input.get(3).split(":")[1];
  197.             height = Integer.parseInt(tempstring.substring(1, tempstring.length()-1));
  198.             //System.out.println(height);
  199.            
  200.             tempstring = input.get(4).split(":")[1];
  201.             version = Integer.parseInt(tempstring.substring(1, tempstring.length()-1));
  202.             //System.out.println(version);
  203.            
  204.             tempstring = input.get(5).split(":")[1];
  205.             merkleroot = tempstring.substring(2, tempstring.length()-2);
  206.             //System.out.println(merkleroot);
  207.            
  208.             tx = new ArrayList<>();
  209.             do{
  210.                 tempstring = input.get(7 + tx.size());
  211.                 if(! tempstring.equals("    ],")) {
  212.                     tx.add(tempstring.substring(9, tempstring.length()-2));
  213.                 }
  214.                 //System.out.println(tempstring.substring(9, tempstring.length()-2));
  215.                 //System.out.println(tempstring.equals("    ],"));
  216.             }while(!tempstring.equals("    ],"));
  217.             //System.out.println(tx.toString());
  218.            
  219.             tempstring = input.get(8+tx.size()).split(":")[1];
  220.             time = Long.parseLong(tempstring.substring(1, tempstring.length()-1));
  221.             //System.out.println(time);
  222.            
  223.             tempstring = input.get(9+tx.size()).split(":")[1];
  224.             nonce = Long.parseLong(tempstring.substring(1, tempstring.length()-1));
  225.             //System.out.println(nonce);
  226.            
  227.             tempstring = input.get(10+tx.size()).split(":")[1];
  228.             bits = Long.parseLong(tempstring.substring(2, tempstring.length()-2), 16);//hex conversion
  229.             //System.out.println(bits);
  230.            
  231.             tempstring = input.get(11+tx.size()).split(":")[1];
  232.             difficulty = Double.parseDouble(tempstring.substring(1, tempstring.length()-1));
  233.             //System.out.println(difficulty);
  234.            
  235.             tempstring = input.get(12+tx.size()).split(":")[1];
  236.             transition = Double.parseDouble(tempstring.substring(1, tempstring.length()-1));
  237.             //System.out.println(transition);
  238.            
  239.             tempstring = input.get(13+tx.size()).split(":")[1];
  240.             primechain = tempstring.substring(2, tempstring.length()-2);
  241.             //System.out.println(primechain);
  242.            
  243.             if(primechain.contains("1CC")) {
  244.                 chainType = ChainType.CC1;
  245.             }else if(primechain.contains("2CC")){
  246.                 chainType = ChainType.CC2;
  247.             }else if(primechain.contains("TWN")){
  248.                 chainType = ChainType.TWN;
  249.             }else{
  250.                 throw new IllegalStateException("Chain Type undefined: " + primechain);
  251.             }
  252.             //System.out.println(chainType);
  253.            
  254.             chainLength = Integer.parseInt(primechain.substring(3, 5), 16);
  255.             //System.out.println(chainLength);
  256.            
  257.            
  258.             tempstring = input.get(14+tx.size()).split(":")[1];
  259.             primeorigin = new BigInteger(tempstring.substring(2, tempstring.length()-2));
  260.             primeSize = tempstring.substring(2, tempstring.length()-2).length();//it's already in string form so just count the characters
  261.             //System.out.println(primeorigin);
  262.             //System.out.println(primeSize);
  263.            
  264.             tempstring = input.get(15+tx.size()).split(":")[1];
  265.             prevriousblockhash = tempstring.substring(2, tempstring.length()-2);
  266.             //System.out.println(prevriousblockhash);
  267.            
  268.             if(height!=0)
  269.             {  
  270.                 tempstring = input.get(16+tx.size()).split(":")[1];
  271.                 nextblockhash = tempstring.substring(2, tempstring.length()-2);
  272.                 //System.out.println(nextblockhash);
  273.             }
  274.  
  275.         }
  276.  
  277.     }
  278.    
  279.     //enum to hold the chain type--Cunningham Chain of the 1st or 2nd kind or Bi-twin chain
  280.     private static enum ChainType{CC1, CC2, TWN};
  281.  
  282. }
Advertisement
Add Comment
Please, Sign In to add comment