0

在我的 java 类中,我从 blk...dat 的文件中提取交易的哈希,然后通过读取 URL:https ://blockchain.info/rawtx/+ hash 获取相应交易的数据。有一次,我的程序在调用 1370 次 url 后停止调用,另外一次,我的程序在调用 2243 次 url 后停止调用。无论我等待多长时间,该程序都不会终止,也不会继续进行。

在这种情况下发生了什么?

由于调用次数超过了此处描述的硬限制:https ://blockchain.info/api ,我怀疑这不是我缺少 API-Key 的问题。但在这种情况下,我怎样才能获得 API-Key?我没有网站(java-application 用于我的学士论文),似乎网上有不少人在从 blockchain.info 获取 API-Key 时遇到问题。

可能是因为我正在使用的网络质量,如果是的话,有没有办法用我的java程序解决它?

如果这是 blockchain.info 方面的固有问题,有人可以推荐我另一种方法来获得类似的交易数据吗?

我的代码:

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();
    }
}
4

0 回答 0