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

时间:2016-06-29 09:30:26

标签: json database encoding bitcoin blockchain

我已经生成了一些"原始数据"从我的比特币测试网络编码为.dat,因此我有文件/blk00000.dat,这是我生成的块的数据吗?即交易数据等?

看起来像这样:

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

有没有办法将它呈现为人类可读的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" : ""
}

修改

我有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

应该注意的是,我使用的是here

的配置

编辑II

我在最后一步遇到了问题,即XXX

导致以下错误:

enter image description here

如果我不得不猜测我会说我在这部分出错了

<path-to-datadirectory>应该在哪里? blk00000.dat生活在哪里?整个目录?还是那个文件?

3 个答案:

答案 0 :(得分:3)

让我们从另一个主题的有趣评论开始:

  

数据以针对紧凑型存储而优化的特定于应用程序的格式存储,并非真正意图被其他应用程序轻松解析。您可以通过gettxout RPC调用访问chainstate中的数据,通过getblock访问块索引中的数据。我会试着抽出时间来记录确切的格式。 - Pieter Wuille Jul 29 '13 at 7:17

但是,如果您仍然对完成此操作感兴趣,则区块链存储在两个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);

比特币核心中使用的levelDB的比特币分叉包含some documentation on how to read and access the data

还有an excellent thread on Bitcoin Stack Exchange告诉您数据库的关键:值模型结构,如果您尝试手动访问数据库,则需要该结构。

答案 1 :(得分:3)

好的,你的评论让我更清楚你需要什么。这是一种完全不同的方法,因此我正在创建一个单独的答案。

你想要的是一个块浏览器!您可以非常轻松地将insight附加到您的testnet节点。

节点上的索引事务

您要做的第一件事是在启用索引的情况下重新启动您的testnet节点。您需要在txindex中将addressindextimestampindexspentindexbitcoin.conf设置为true。

reindex=1添加到bitcoin.conf。使用reindex=1启动比特币一次后,您应将其删除,否则每次节点启动时都会重新编制索引。但是请务必保留其他索引。

安装bitcore

通过NodeJS安装bitcore-node

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

安装insight API和UI

testnode工作目录中,添加洞察力:

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

检查您的区块链

打开网络浏览器http://localhost:3001/insight/以访问“阻止资源管理器”。

API endpoints默认情况下可用于:http://localhost:3001/insight-api/block-index/0(例如获取创世块)。 API返回JSON:

{
  "blockHash":"000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"
}

可用于获取阻止详细信息的阻止,例如http://localhost:3001/insight-api/block/000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f

答案 2 :(得分:0)

有一个来自精彩人士的blckdat-stream项目,可以制作bitcoinjs-lib。

你需要和bitcoinjs-lib一起使用; blckdat-stream only&#34;解析&#34;将它们分块到单独的块中的dat文件。您需要通过bitcoinjs-lib本身解析块本身。