简体   繁体   English

读取和解析区块链 DAT 文件

[英]Reading & Parsing Blockchain DAT files

I'm working on some code that reads the DAT files in the Blockchain, and I was trying to use bitcoinj because it seemed fairly straightforward.我正在编写一些读取区块链中 DAT 文件的代码,我尝试使用 bitcoinj,因为它看起来相当简单。 However, I can't seem to get it to actually read the blocks within the DAT file.但是,我似乎无法让它实际读取 DAT 文件中的块。 I've tried many different versions and have made no significant progress.我尝试了许多不同的版本,但没有取得重大进展。

I'm feeling like this should be fairly straightforward, and I'm just missing something simple here.我觉得这应该相当简单,我只是在这里遗漏了一些简单的东西。 To be clear, I'm not trying to write to the Blockchain, just read the DAT files.需要明确的是,我不是要写入区块链,只是读取 DAT 文件。
Thanks!谢谢!

Here is a code snippet.这是一个代码片段。

NetworkParameters np = new MainNetParams();
Context c = new Context( np );
Context.getOrCreate(MainNetParams.get());
List<File> blockChainFiles = new ArrayList<>();
blockChainFiles.add( new File( "blk00000.dat" ) );
BlockFileLoader bfl = new BlockFileLoader(np, blockChainFiles);

int blockNum = 0;
// Iterate over the blocks in the dataset.
for (Block block : bfl) {
...

This code produces the following error:此代码产生以下错误:

Exception in thread "main" java.lang.IllegalStateException: Context does not match implicit network params: org.bitcoinj.params.MainNetParams@9d1d82f2 vs org.bitcoinj.params.MainNetParams@9d1d82f2
at org.bitcoinj.core.Context.getOrCreate(Context.java:147)
at testBitcoin.main(testBitcoin.java:20)

The block .dat files contain multiple blocks in one file, including orphans, separated by magic numbers.块 .dat 文件在一个文件中包含多个块,包括孤立的块,由幻数分隔。

Please refer https://en.bitcoin.it/wiki/Protocol_documentation#Message_structure请参考https://en.bitcoin.it/wiki/Protocol_documentation#Message_structure

Your code doesn't seem to be looking for magic numbers or jumping lengths as specified by message structure.您的代码似乎没有寻找消息结构指定的幻数或跳跃长度。

Just get rid of the complaining line, Context.getOrCreate(MainNetParams.get());只需摆脱抱怨线Context.getOrCreate(MainNetParams.get()); , it's not needed. ,不需要。

The following slightly altered version of your code worked for me:您的代码的以下稍微更改的版本对我有用:

List<File> blockChainFiles = new ArrayList<>();
blockChainFiles.add(new File("blk00000.dat"));
MainNetParams params = MainNetParams.get();
Context context = new Context(params);
BlockFileLoader bfl = new BlockFileLoader(params, blockChainFiles);

// Iterate over the blocks in the dataset.
for (Block block : bfl) {
    System.out.println(block.getHashAsString());
}

You can use my blockchain parser .你可以使用我的区块链解析器 It writtens on Python and can parse all the data from blk*.dat files to the simple text view.它用 Python 编写,可以将 blk*.dat 文件中的所有数据解析为简单的文本视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM