Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package primecoanalysis;
- import java.io.File;
- import java.io.IOException;
- import java.math.BigInteger;
- import java.util.ArrayList;
- import java.util.Scanner;
- /**
- * Simple parser for blocks saved in the getblock output from primecoind
- *
- * @author Koooooj AFozUk53j5uZCrKAhkcrM6SjnKtGRXCnov
- */
- public class PrimecoAnalysis {//Primeco instead of Primecoin because I fat fingered it and don't feel like chaingin.
- public static void main(String[] pickles) throws IOException {
- File inFile = new File("blockdump.txt");
- Scanner in = new Scanner(inFile);
- ArrayList<Block> blocks = new ArrayList<>();
- while(in.hasNextLine())//read in all blocks
- {
- String openCurly = in.nextLine();
- if(! openCurly.equals("{")) {
- throw new IllegalStateException("open curly failed");
- }
- ArrayList<String> lines = new ArrayList<>();
- String line = in.nextLine();
- while(!line.equals("}")){
- lines.add(line);
- line = in.nextLine();
- }
- blocks.add(new Block(lines));
- if(blocks.size()%100 == 0) {
- System.out.println(blocks.size());
- }
- }
- for(int i = 0; i<blocks.size(); i++)//continuity check.
- {
- if(blocks.get(i).height != i) {
- System.out.println("ERROR!!" + (blocks.get(i).height - i));
- return;
- }
- }
- //analysis!!
- //Longest chains!
- int longestChain = 0, longestChainHeight = 0;
- int longestCC1 = 0, longestCC1Height = 0;
- int longestCC2 = 0, longestCC2Height = 0;
- int longestTWN = 0, longestTWNHeight = 0;
- //Chain type count!
- int countCC1 = 0, countCC2 = 0, countTWN = 0;
- //length histograms!
- int lengthCount[] = new int[20];
- int lengthCountCC1[] = new int[20], lengthCountCC2[] = new int[20], lengthCountTWN[] = new int[20];
- //size histograms!
- final int sizeRange = 200;
- int sizeCount[] = new int[sizeRange];
- int sizeCountCC1[] = new int[sizeRange], sizeCountCC2[] = new int[sizeRange], sizeCountTWN[] = new int[sizeRange];
- final int sizeOffset = 80;
- //Largest/Smallest Prime Origins
- BigInteger largestPrime = BigInteger.ZERO;
- int largestPrimeHeight = 0;
- BigInteger smallestPrime = blocks.get(0).primeorigin;
- int smallestPrimeHeight = 0;
- for(Block b: blocks)
- {
- if(b.chainLength > longestChain) {
- longestChain = b.chainLength;
- longestChainHeight = b.height;
- }
- if(b.primeorigin.compareTo(largestPrime) > 0){//no greater than in BigInteger... grumble grumble grumble
- largestPrime = b.primeorigin;
- largestPrimeHeight = b.height;
- }
- if(b.primeorigin.compareTo(smallestPrime)<0){
- smallestPrime = b.primeorigin;
- smallestPrimeHeight = b.height;
- }
- lengthCount[b.chainLength]++;//will throw out-of-bounds exceptions if not enough space.
- sizeCount[b.primeSize-sizeOffset]++;
- if(b.chainType==ChainType.CC1 && b.chainLength > longestCC1){
- longestCC1 = b.chainLength;
- longestCC1Height = b.height;
- }
- if(b.chainType==ChainType.CC2 && b.chainLength > longestCC2){
- longestCC2 = b.chainLength;
- longestCC2Height = b.height;
- }
- if(b.chainType==ChainType.TWN && b.chainLength > longestTWN){
- longestTWN = b.chainLength;
- longestTWNHeight = b.height;
- }
- switch(b.chainType){
- case CC1:
- countCC1++;
- lengthCountCC1[b.chainLength]++;
- sizeCountCC1[b.primeSize-sizeOffset]++;
- break;
- case CC2:
- countCC2++;
- lengthCountCC2[b.chainLength]++;
- sizeCountCC2[b.primeSize-sizeOffset]++;
- break;
- case TWN:
- countTWN++;
- lengthCountTWN[b.chainLength]++;
- sizeCountTWN[b.primeSize-sizeOffset]++;
- break;
- default: throw new IllegalStateException("A chain has no type!");
- }
- }
- System.out.println("Longest chain is " + longestChain + " primes long, found at height " + longestChainHeight);
- System.out.println("Longest CC1 is " + longestCC1 + " primes long, found at height " + longestCC1Height);
- System.out.println("Longest CC2 is " + longestCC2 + " primes long, found at height " + longestCC2Height);
- System.out.println("Longest TWN is " + longestTWN + " primes long, found at height " + longestTWNHeight);
- System.out.println("\n\n\n");
- 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");
- System.out.println("Length histogram!");
- System.out.println("Length, Total, CC1, CC2, TWN");
- for(int i = 0; i < 20; i++){
- System.out.println(i + " , " + lengthCount[i] + " , " + lengthCountCC1[i] + " , " + lengthCountCC2[i] + " , " + lengthCountTWN[i]);
- }
- System.out.println("\n\n\n");
- System.out.println("Size histogram!");
- System.out.println("Size, Total, CC1, CC2, TWN");
- for(int i = 0; i < sizeRange; i++){
- System.out.println((i + sizeOffset) + " , " + sizeCount[i] + " , " + sizeCountCC1[i] + " , " + sizeCountCC2[i] + " , " + sizeCountTWN[i]);
- }
- System.out.println("\n\n\n");
- System.out.println("The largest prime origin is " + largestPrime + " which occurs at height " + largestPrimeHeight);
- System.out.println("The smallest prime origin is " + smallestPrime + " which occurs at height " + smallestPrimeHeight);
- System.out.println("\n\n\n");
- }
- private static class Block//struct to hold all the data of a block's header, plus the method to parse it from a String
- {
- //All the items in the header, as they are listed in getblock's output
- String hash;
- int confirmations;
- int size;
- int height;
- int version;
- String merkleroot;
- ArrayList<String> tx;
- long time;//no see
- long nonce;//long because it ought to be 32 unsigned bits and java is sutpid--no unsinged modifier
- long bits;
- double difficulty;
- double transition;
- String primechain;
- BigInteger primeorigin;
- String prevriousblockhash;
- String nextblockhash;
- //a few derrived pieces of data
- ChainType chainType;
- int chainLength;//only counting full primes.
- int primeSize;//number of base-10 digits
- //static int n = 0;
- public Block(ArrayList<String> input){
- //System.out.println(n++);
- String tempstring;
- tempstring = input.get(0).split(":")[1];//remove "hash" :
- hash = tempstring.substring(2, tempstring.length()-2);//remove quotes, space, and comma
- //System.out.println(hash);
- tempstring = input.get(1).split(":")[1];
- confirmations = Integer.parseInt(tempstring.substring(1, tempstring.length()-1));
- //System.out.println(confirmations);
- tempstring = input.get(2).split(":")[1];
- size = Integer.parseInt(tempstring.substring(1, tempstring.length()-1));
- //System.out.println(size);
- tempstring = input.get(3).split(":")[1];
- height = Integer.parseInt(tempstring.substring(1, tempstring.length()-1));
- //System.out.println(height);
- tempstring = input.get(4).split(":")[1];
- version = Integer.parseInt(tempstring.substring(1, tempstring.length()-1));
- //System.out.println(version);
- tempstring = input.get(5).split(":")[1];
- merkleroot = tempstring.substring(2, tempstring.length()-2);
- //System.out.println(merkleroot);
- tx = new ArrayList<>();
- do{
- tempstring = input.get(7 + tx.size());
- if(! tempstring.equals(" ],")) {
- tx.add(tempstring.substring(9, tempstring.length()-2));
- }
- //System.out.println(tempstring.substring(9, tempstring.length()-2));
- //System.out.println(tempstring.equals(" ],"));
- }while(!tempstring.equals(" ],"));
- //System.out.println(tx.toString());
- tempstring = input.get(8+tx.size()).split(":")[1];
- time = Long.parseLong(tempstring.substring(1, tempstring.length()-1));
- //System.out.println(time);
- tempstring = input.get(9+tx.size()).split(":")[1];
- nonce = Long.parseLong(tempstring.substring(1, tempstring.length()-1));
- //System.out.println(nonce);
- tempstring = input.get(10+tx.size()).split(":")[1];
- bits = Long.parseLong(tempstring.substring(2, tempstring.length()-2), 16);//hex conversion
- //System.out.println(bits);
- tempstring = input.get(11+tx.size()).split(":")[1];
- difficulty = Double.parseDouble(tempstring.substring(1, tempstring.length()-1));
- //System.out.println(difficulty);
- tempstring = input.get(12+tx.size()).split(":")[1];
- transition = Double.parseDouble(tempstring.substring(1, tempstring.length()-1));
- //System.out.println(transition);
- tempstring = input.get(13+tx.size()).split(":")[1];
- primechain = tempstring.substring(2, tempstring.length()-2);
- //System.out.println(primechain);
- if(primechain.contains("1CC")) {
- chainType = ChainType.CC1;
- }else if(primechain.contains("2CC")){
- chainType = ChainType.CC2;
- }else if(primechain.contains("TWN")){
- chainType = ChainType.TWN;
- }else{
- throw new IllegalStateException("Chain Type undefined: " + primechain);
- }
- //System.out.println(chainType);
- chainLength = Integer.parseInt(primechain.substring(3, 5), 16);
- //System.out.println(chainLength);
- tempstring = input.get(14+tx.size()).split(":")[1];
- primeorigin = new BigInteger(tempstring.substring(2, tempstring.length()-2));
- primeSize = tempstring.substring(2, tempstring.length()-2).length();//it's already in string form so just count the characters
- //System.out.println(primeorigin);
- //System.out.println(primeSize);
- tempstring = input.get(15+tx.size()).split(":")[1];
- prevriousblockhash = tempstring.substring(2, tempstring.length()-2);
- //System.out.println(prevriousblockhash);
- if(height!=0)
- {
- tempstring = input.get(16+tx.size()).split(":")[1];
- nextblockhash = tempstring.substring(2, tempstring.length()-2);
- //System.out.println(nextblockhash);
- }
- }
- }
- //enum to hold the chain type--Cunningham Chain of the 1st or 2nd kind or Bi-twin chain
- private static enum ChainType{CC1, CC2, TWN};
- }
Advertisement
Add Comment
Please, Sign In to add comment