0

In my java class, I extract the hash of transactions from the files of blk...dat, then obtain the data of the corresponding transactions via reading from the URL: https://blockchain.info/rawtx/+ hash. At one time, my program stopped calling after calling the urls for 1370 times, at the other time, my program stopped calling after calling the urls for 2243 times. The program is not terminated and will not proceed no matter how long I wait.

What is going on in this case?

Since the number of call exceeds the the hard limits described here: https://blockchain.info/api, I suspect that it is not a problem of I lacking an API-Key. But in the case it is the reason, how could I get an API-Key? I don't have a website (the java-application is for my bachelor thesis), and it seems that quite a few people online have issue with obtaining the API-Key from blockchain.info.

Could it because of something about the network quality I am using, if yes, is there any way to work around it with my java program?

If it is a inherent problem from the side of blockchain.info, could someone recommend me an alternative way to attain similar data of transactions?

My code:

import org.bitcoinj.core.*;
import org.bitcoinj.core.Transaction;
import org.bitcoinj.utils.*;
import java.io.*;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.*;
import org.bitcoinj.params.MainNetParams;


import org.json.*;

public class ParseFromDat {
    private static String readAll(Reader rd) throws IOException {
        StringBuilder sb = new StringBuilder();
        int cp;
        while ((cp = rd.read()) != -1) {
            sb.append((char) cp);
        }
        return sb.toString();
    }

    public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
        InputStream is = new URL(url).openStream();
        try {
            BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
            String jsonText = readAll(rd);
            JSONObject json = new JSONObject(jsonText);
            return json;
        } finally {
            is.close();
        }
    }

    public static void main(String[] args) throws IOException, JSONException {
        Context.getOrCreate(MainNetParams.get());

        // Arm the blockchain file loader.
        NetworkParameters np = new MainNetParams();
        List<File> blockChainFiles = new ArrayList<File>();
        blockChainFiles.add(new File("C:\\Users\\...\\AppData\\Roaming\\Bitcoin\\blocks\\blk00514.dat"));
        BlockFileLoader bfl = new BlockFileLoader(np, blockChainFiles);

        // define files to be written into
        PrintWriter transactions = new PrintWriter(new File("./csvs/transactions.csv"));
        StringBuilder traStr = new StringBuilder();
        traStr.append("tranHashString:ID(Trans)\n");
        PrintWriter sendAdd = new PrintWriter(new File("./csvs/sendAdd.csv"));
        StringBuilder sendStr = new StringBuilder();
        sendStr.append("addr:ID(SendAdd),value,tranHashString,addr_tag_link,addr_tag\n");
        PrintWriter receiveAdd = new PrintWriter(new File("./csvs/receiveAdd.csv"));
        StringBuilder receiStr = new StringBuilder();
        receiStr.append("addr:ID(ReceAdd),value,tranHashString,addr_tag_link,addr_tag\n");

        PrintWriter inTran = new PrintWriter(new File("./csvs/intran.csv"));
        StringBuilder inStr = new StringBuilder();
        inStr.append(":START_ID(SendAdd),:END_ID(Trans)\n");
        PrintWriter outTran = new PrintWriter(new File("./csvs/outtran.csv"));
        StringBuilder outStr = new StringBuilder();
        outStr.append(":START_ID(Trans),:END_ID(ReceAdd)\n");

        // Iterate over the blocks in the dataset.
        int counter = 0;
        for (Block block : bfl) {
            System.out.println("aaaa");
            List<Transaction> trans = block.getTransactions();
            for (Transaction ta : trans) {
                System.out.println("bbbb");
                traStr.append(ta.getHashAsString());
                traStr.append("\n");        
                System.out.println("bbbb1 " + (counter++));
                System.out.println(ta.getHashAsString());
                JSONObject json = readJsonFromUrl("https://blockchain.info/rawtx/" + ta.getHashAsString());
                JSONArray inputs = json.getJSONArray("inputs");
                for (int i = 0; i < inputs.length(); i++) {
                    if (inputs.getJSONObject(i).has("prev_out")) { // it is possible that the value is null even when prev_out is not null
                        System.out.println("ccc");
                        JSONObject prevOut = (JSONObject) (inputs.getJSONObject(i).get("prev_out"));
                        inStr.append(prevOut.get("addr").toString());
                        inStr.append(',');
                        inStr.append(ta.getHashAsString());
                        inStr.append("\n");

                        sendStr.append(prevOut.get("addr").toString());
                        sendStr.append(',');
                        sendStr.append(prevOut.get("value").toString());
                        sendStr.append(',');
                        sendStr.append(ta.getHashAsString());

                        if (prevOut.has("addr_tag_link") || prevOut.has("addr_tag")) {
                            if (prevOut.has("addr_tag_link") && prevOut.has("addr_tag")) {
                                sendStr.append(prevOut.get("addr_tag_link").toString());
                                sendStr.append(',');
                                sendStr.append(prevOut.get("addr_tag").toString());
                            } else if (prevOut.has("addr_tag_link")) {
                                sendStr.append(prevOut.get("addr_tag_link").toString());
                                sendStr.append(',');
                                sendStr.append("null");
                            } else {
                                sendStr.append("null");
                                sendStr.append(',');
                                sendStr.append(prevOut.get("addr_tag").toString());
                            }
                        }
                        sendStr.append("\n");
                    }else{
                        continue;
                    }
                }
                JSONArray out = json.getJSONArray("out");
                for (int i = 0; i < out.length(); i++) {
                    JSONObject outItem = out.getJSONObject(i);
                    if (outItem.has("addr")) { // it is possible that the add and value is null?
                        outStr.append(ta.getHashAsString());
                        outStr.append(',');
                        outStr.append(outItem.get("addr").toString());
                        outStr.append("\n");

                        receiStr.append(outItem.get("addr").toString());
                        receiStr.append(',');
                        receiStr.append(outItem.get("value").toString());
                        receiStr.append(',');
                        receiStr.append(ta.getHashAsString());

                        if (outItem.has("addr_tag_link") || outItem.has("addr_tag")) {
                            if (outItem.has("addr_tag_link") && outItem.has("addr_tag")) {
                                receiStr.append(outItem.get("addr_tag_link").toString());
                                receiStr.append(',');
                                receiStr.append(outItem.get("addr_tag").toString());
                            } else if (outItem.has("addr_tag_link")) {
                                receiStr.append(outItem.get("addr_tag_link").toString());
                                receiStr.append(',');
                                receiStr.append("null");
                            } else {
                                receiStr.append("null");
                                receiStr.append(',');
                                receiStr.append(outItem.get("addr_tag").toString());
                            }
                        }
                        receiStr.append("\n");
                    }else{
                        continue;
                    }

                }

            }

        }
        transactions.write(traStr.toString());
        transactions.close();
        sendAdd.write(sendStr.toString());
        sendAdd.close();
        receiveAdd.write(receiStr.toString());
        receiveAdd.close();
        inTran.write(inStr.toString());
        inTran.close();
        outTran.write(outStr.toString());
        outTran.close();
    }
}
Aqqqq
  • 816
  • 2
  • 10
  • 27
  • Could you post the code that call blockchain.info and the response? – Fi3 Feb 27 '17 at 11:11
  • Added. Also the program is not terminated, it just stops at a point where the URL should be called. – Aqqqq Feb 27 '17 at 11:29
  • An example of the returned value of ta.getHashAsString() ? – Fi3 Feb 27 '17 at 11:40
  • 22d1da81833f6b4f249ad72eda8989c19025b947037f1b006510ffed7cfd247e – Aqqqq Feb 27 '17 at 12:22
  • It seems that you are [looking for the wrong tx](https://blockchain.info/rawtx/22d1da81833f6b4f249ad72eda8989c19025b947037f1b006510ffed7cfd%E2%80%8C%E2%80%8B247e) Try instead to pass to `readJsonFromUrl` [this url](https://blockchain.info/rawtx/4a2af9e066164355c48ff42adb363f345952d5e724de4e61092c4f7a4bbcafbd) What does `readJsonFromUrl` return with the latter url? – Fi3 Feb 27 '17 at 16:39
  • I don't think the problem is the getHashAsString being the wrong tx is the problem. The last getHashAsString output of one of the program run is: d20eac4d9fd6dda041c6354ba36fcc6a9252361880c7eb6c2d91068b867f7117 which is a valid tx: https://blockchain.info/rawtx/d20eac4d9fd6dda041c6354ba36fcc6a9252361880c7eb6c2d91068b867f7117 – Aqqqq Feb 27 '17 at 19:56

0 Answers0