简体   繁体   English

有没有办法像存储比特币块的存储格式那样读取.dat文件?

[英]is there a way to read .dat files as in the storage format for bitcoin blocks?

I've generated some "raw data" that is encoded as .dat from my bitcoin testnet, accordingly I have the file /blk00000.dat , is this the data of my blocks I've generated? 我已经从我的比特币测试网生成了一些编码为.dat “原始数据”,因此,我有文件/blk00000.dat ,这是我生成的块的数据吗? ie the transaction data etc.? 即交易数据等?

It looks like this: 看起来像这样:

0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 00ff ffff ff03 5301
01ff ffff ff01 00f2 052a 0100 0000 2321
02f6 89e4 8543 5d1a 78f9 d08c a612 7e36
2216 20e1 ad70 7576 dbae 41e7 c01d 406a
b7ac 0000 0000 fabf b5da b300 0000 0000
0020 abb4 1583 7333 ff2b fcde f019 f2ad
4f4d 5c69 462f 277e 1c52 8564 53c2 ab2d
7674 1c8d de8d e0fa 11eb 9dfe 798d 4839
5a61 b9d3 439c f4e7 79a5 ebe3 dabe f15d
2930 d791 7357 ffff 7f20 0100 0000 0101
0000 0001 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 ffff ffff 0354 0101 ffff ffff
0100 f205 2a01 0000 0023 2102 f689 e485
435d 1a78 f9d0 8ca6 127e 3622 1620 e1ad
7075 76db ae41 e7c0 1d40 6ab7 ac00 0000
00fa bfb5 dab3 0000 0000 0000 2026 a3de
12ea 2f3a deb5 10bf d337 4390 7371 c2d6

is there a way to render it as human readable JSON, so I can see it as something like this: 有没有一种方法可以将其呈现为人类可读的JSON,因此我可以将其视为这样:

{
    "version" : 90300,
    "protocolversion" : 70002,
    "walletversion" : 60000,
    "balance" : 0.00000000,
    "blocks" : 0,
    "timeoffset" : 0,
    "connections" : 1,
    "proxy" : "",
    "difficulty" : 0.00000000,
    "testnet" : false,
    "keypoololdest" : 1413617762,
    "keypoolsize" : 101,
    "paytxfee" : 0.00000000,
    "relayfee" : 0.00001000,
    "errors" : ""
}

EDIT 编辑

I have 2 bitcoin.conf files, as I have two nodes, they look like this: 我有2个bitcoin.conf文件,因为我有两个节点,它们看起来像这样:

# testnet-box functionality
regtest=1
dnsseed=0
upnp=0

# listen on different ports than default testnet
port=19000
rpcport=19001

# always run a server, even with bitcoin-qt
server=1

# enable SSL for RPC server
#rpcssl=1

rpcallowip=0.0.0.0/0

rpcuser=admin1
rpcpassword=123

It should be noted that I'm using the configuration that I got from here 应该注意的是,我正在使用从这里获得的配置


EDIT II 编辑二

I had a problem with the last step, namely XXX 我在最后一步(XXX)遇到了问题

it resulted in the following error: 它导致以下错误:

在此处输入图片说明

if I had to guess I would say I went wrong on this part 如果我不得不猜测我会说我在这部分上做错了

<path-to-datadirectory> where exactlly should that be? <path-to-datadirectory>到底应该在哪里? where blk00000.dat lives? blk00000.dat住在哪里? the whole directory? 整个目录? or exactly that file? 还是那个文件?

Let's start with an interesting comment from another thread here: 让我们从另一个线程的有趣评论开始:

The data is stored in an application-specific format optimized for compact storage, and wasn't really intended to be easily parsed by other applications. 数据以针对特定应用的格式进行了存储,该格式已针对紧凑型存储进行了优化,而实际上并不是要由其他应用轻松解析。 You can access the data in the chainstate through the gettxout RPC call, and the data in the block index through getblock. 您可以通过gettxout RPC调用访问chainstate中的数据,并通过getblock访问block索引中的数据。 I'll try to find time for a write-up of the exact format. 我会尝试寻找时间写出确切的格式。 Pieter Wuille Jul 29 '13 at 7:17 Pieter Wuille 2013年7月29日在7:17

However, if you are still interested in getting this done, the blockchain is stored in two levelDB databases: one for the blocks and one for the chainstate. 但是,如果您仍然对完成此操作感兴趣,则将区块链存储在两个levelDB数据库中:一个用于块,一个用于chainstate。 There are multiple ways to open that, I would recommend to look into libbitcoin , or bitcoin-leveldb: 有多种打开方式,我建议查看libbitcoin或bitcoin-leveldb:

// Define a threadpool with 1 thread.
threadpool pool(1);
// Create a LevelDB blockchain.
leveldb_blockchain ldb_chain(pool);
// Initialize our global 'chain' pointer from above.
chain = &ldb_chain;
// Start the database using its implementation specific method.
ldb_chain.start("../database", blockchain_started);

The Bitcoin fork of levelDB used in Bitcoin core contains some documentation on how to read and access the data . 比特币核心中使用的levelDB的比特币叉包含一些有关如何读取和访问数据的文档

There is also an excellent thread on Bitcoin Stack Exchange which tells you about the key:value model structure of the database, which is required if you try to access the database manually. Bitcoin Stack Exchange上还有一个很棒的线程,它告诉您数据库的key:value模型结构,如果您尝试手动访问数据库,这是必需的。

Ok your comment made this more clear to me what you need. 好的,您的评论使我更清楚了您的需求。 Here is a totally different approach, thus I'm creating a separate answer. 这是一种完全不同的方法,因此我要创建一个单独的答案。

What you want is a block explorer! 您想要的是一个区块浏览器! You can very easy attach insight to your testnet node. 您可以非常轻松地将见解附加到您的testnet节点。

Index transactions on your node 节点上的索引交易

First thing you have to do is to restart your testnet node with indexing enabled. 您要做的第一件事是在启用索引的情况下重新启动testnet节点。 txindex , addressindex , timestampindex and spentindex needs to be set to true in your bitcoin.conf . 需要在您的bitcoin.conf txindexaddressindextimestampindexspentindex设置为true。

Add reindex=1 to your bitcoin.conf . reindex=1添加到您的bitcoin.conf After you start Bitcoin once with reindex=1 you should remove it, otherwise it will reindex every time the node starts. 使用reindex=1启动一次比特币后,应将其删除,否则它将在每次节点启动时重新索引。 Be sure to leave the other indexes, though. 不过,请务必保留其他索引。

Install bitcore 安装bitcore

Install bitcore-node via NodeJS. 通过NodeJS安装bitcore-node

npm install -g bitcore-node
bitcore-node create -d <path-to-datadirectory> testnode
cd testnode

Install insight API and UI 安装洞察API和UI

Inside your testnode working directory, add insight: 在您的testnode工作目录中,添加见解:

bitcore-node install insight-api
bitcore-node install insight-ui
bitcore-node start

Inspect your blockchain 检查您的区块链

Open a web browser to http://localhost:3001/insight/ to access the Block explorer. 打开Web浏览器,访问http://localhost:3001/insight/以访问Block Explorer。

The API endpoints will be available by default at: http://localhost:3001/insight-api/block-index/0 (example to get the genesis block). 默认情况下, API端点将可用: http://localhost:3001/insight-api/block-index/0 (获取创世记块的示例)。 The API returns JSON: API返回JSON:

{
  "blockHash":"000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"
}

The blockhash you can use to get block details, like http://localhost:3001/insight-api/block/000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f . 可用于获取块详细信息的blockhash,例如http://localhost:3001/insight-api/block/000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f

There is a blckdat-stream project from the wonderful people that make bitcoinjs-lib. 有一个很棒的人组成的blckdat 项目,使之成为了bitcoinjs-lib。

You need to use it together with bitcoinjs-lib; 您需要将其与bitcoinjs-lib一起使用; blckdat-stream only "parses" the dat files in that it chunks them into separate blocks. blckdat-stream仅“解析” dat文件,因为它将它们分块成单独的块。 You need to parse the blocks themselves by bitcoinjs-lib itself. 您需要通过bitcoinjs-lib本身来解析这些块。

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

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