用于将比特币区块链数据导出到JSON的工具
bitcoin-etl的Python项目详细描述
比特币ETL
安装比特币ETL:
pip install bitcoin-etl
> bitcoinetl export_blocks_and_transactions --start-block 0 --end-block 500000\ --provider-uri http://user:pass@localhost:8332 --chain bitcoin \ --blocks-output blocks.json --transactions-output transactions.json
支撑链:
- 比特币
- 比特币现金
- 狗粪蛋白
- 莱特币
- 破折号
- zcash
将区块链数据连续流到控制台(Reference):
> pip install bitcoin-etl[streaming] > bitcoinetl stream -p http://user:pass@localhost:8332 --start-block 500000
将区块链数据连续传输到google pub/sub(Reference):
> exportGOOGLE_APPLICATION_CREDENTIALS=/path_to_credentials_file.json > bitcoinetl stream -p http://user:pass@localhost:8332 --start-block 500000 --output projects/your-project/topics/crypto_bitcoin
有关最新版本,请查看回购协议并致电
> pip install -e .[streaming] > python bitcoinetl.py
目录
架构
blocks.json
Field | Type |
---|---|
hash | hex_string |
size | bigint |
stripped_size | bigint |
weight | bigint |
number | bigint |
version | bigint |
merkle_root | hex_string |
timestamp | bigint |
nonce | hex_string |
bits | hex_string |
coinbase_param | hex_string |
transaction_count | bigint |
事务处理.json
Field | Type |
---|---|
hash | hex_string |
size | bigint |
virtual_size | bigint |
version | bigint |
lock_time | bigint |
block_number | bigint |
block_hash | hex_string |
block_timestamp | bigint |
is_coinbase | boolean |
inputs | []transaction_input |
outputs | []transaction_output |
input_count | bigint |
output_count | bigint |
input_value | bigint |
output_value | bigint |
fee | bigint |
事务输入
Field | Type |
---|---|
index | bigint |
spent_transaction_hash | hex_string |
spent_output_index | bigint |
script_asm | string |
script_hex | hex_string |
sequence | bigint |
required_signatures | bigint |
type | string |
addresses | []string |
value | bigint |
事务输出
Field | Type |
---|---|
index | bigint |
script_asm | string |
script_hex | hex_string |
required_signatures | bigint |
type | string |
addresses | []string |
value | bigint |
您可以在schemas中找到列描述
注释:
DogeCoin API返回的输出值在版本1.14之前的客户端中有精度损失。 这是由这个问题引起的https://github.com/dogecoin/dogecoin/issues/1558
使用旧版本导出数据的资源管理器可能会显示不正确的地址余额和交易金额。对于zcash,
vjoinsplit
和valueBalance
字段转换为具有“屏蔽”类型的输入和输出 https://zcash-rpc.github.io/getrawtransaction.html,https://zcash.readthedocs.io/en/latest/rtd_pages/zips/zip-0243.html
导出区块链
安装python 3.5.3+https://www.python.org/downloads/
启动比特币。 通过在终端中执行
$ bitcoin-cli getblockchaininfo
,确保它下载了所需的块。 您可以导出blocks
以下的块,无需等到完全同步安装比特币ETL:
> pip install bitcoin-etl
导出块和事务:
> bitcoinetl export_all --start 0 --end 499999\ --partition-batch-size 100\ --provider-uri http://user:pass@localhost:8332 --chain bitcoin
结果将在
output
子目录中,以配置单元样式分区:output/blocks/start_block=00000000/end_block=00000099/blocks_00000000_00000099.csv output/blocks/start_block=00000100/end_block=00000199/blocks_00000100_=00000199.csv ... output/transactions/start_block=00000000/end_block=00000099/transactions_00000000_00000099.csv ...
如果路径中没有
bitcoinetl
命令,请改用python -m bitcoinetl
。
在Docker中运行
安装Dockerhttps://docs.docker.com/install/
建立Docker图像
> docker build -t bitcoin-etl:latest . > docker image ls
从图像中运行容器
> docker run -v $HOME/output:/bitcoin-etl/output bitcoin-etl:latest export_blocks_and_transactions --start-block 0 --end-block 500000\ --rpc-pass '' --rpc-host 'localhost' --rpc-user '' --blocks-output blocks.json --transactions-output transactions.json
运行流媒体到控制台或发布/订阅
> docker build -t bitcoin-etl:latest-streaming -f Dockerfile_with_streaming . > echo"Stream to console" > docker run bitcoin-etl:latest-streaming stream -p http://user:pass@localhost:8332 --start-block 500000 > echo"Stream to Pub/Sub" > docker run -v /path_to_credentials_file/:/bitcoin-etl/ --env GOOGLE_APPLICATION_CREDENTIALS=/bitcoin-etl/credentials_file.json bitcoin-etl:latest-streaming stream -p http://user:pass@localhost:8332 --start-block 500000 --output projects/your-project/topics/crypto_bitcoin
请参阅https://github.com/blockchain-etl/bitcoin-etl-streaming以将流式应用程序部署到 谷歌Kubernetes引擎。
命令参考
所有命令都接受-h
参数以获取帮助,例如:
> bitcoinetl export_blocks_and_transactions --help Usage: bitcoinetl.py export_blocks_and_transactions [OPTIONS] Export blocks and transactions. Options: -s, --start-block INTEGER Start block -e, --end-block INTEGER End block [required] -b, --batch-size INTEGER The number of blocks to export at a time. -p, --provider-uri TEXT The URI of the remote Bitcoin node -w, --max-workers INTEGER The maximum number of workers. --blocks-output TEXT The output file for blocks. If not provided blocks will not be exported. Use "-"for stdout --transactions-output TEXT The output file for transactions. If not provided transactions will not be exported. Use "-"for stdout --help Show this message and exit.
对于--output
参数,支持的类型是json格式类型是从输出文件名推断出来的。
导出块和事务
> bitcoinetl export_blocks_and_transactions --start-block 0 --end-block 500000\ --provider-uri http://user:pass@localhost:8332 \ --blocks-output blocks.json --transactions-output transactions.json
如果只想导出事务/块,请省略--blocks-output
或--transactions-output
选项。
您可以调整--batch-size
,--max-workers
以获得性能。
注意,事务输入中的required_signatures
、type
、addresses
和value
字段将为空。
使用enrich_transactions填充这些字段。
丰富交易
> bitcoinetl enrich_transactions \ --provider-uri http://user:pass@localhost:8332 \ --transactions-input transactions.json --transactions-output enriched_transactions.json
您可以调整--batch-size
,--max-workers
以获得性能。
获取日期的块范围
> bitcoinetl get_block_range_for_date --provider-uri http://user:pass@localhost:8332 --date=2017-03-01
此命令保证返回在指定的
日期。但是,返回的块范围也可能包含指定日期以外的块,因为块时间不是
单调的你可以过滤
blocks.json
/transactions.json
使用以下命令:
> bitcoinetl filter_items -i blocks.json -o blocks_filtered.json \ -p "datetime.datetime.fromtimestamp(item['timestamp']).astimezone(datetime.timezone.utc).strftime('%Y-%m-%d') == '2017-03-01'"
全部导出
> bitcoinetl export_all --provider-uri http://user:pass@localhost:8332 --start 2018-01-01 --end 2018-01-02
您可以调整--export-batch-size
,--max-workers
以获得性能。
流
> bitcoinetl stream --provider-uri http://user:pass@localhost:8332 --start-block 500000
- 默认情况下,此命令将块和事务输出到控制台。
- 使用
--output
选项指定发布区块链数据的google发布/子主题, 例如projects/your-project/topics/crypto_bitcoin
。块和事务将被推送到projects/your-project/topics/crypto_bitcoin.blocks
和projects/your-project/topics/crypto_bitcoin.transactions
话题。 - 该命令将其状态保存到保存上次同步块号的
last_synced_block.txt
文件中d定期。 - 指定
--start-block
或--last-synced-block-file
选项--last-synced-block-file
应该指向 保存块编号的文件,从中开始对块链数据进行流式处理。 - 使用
--lag
选项指定要落后于区块链头部的块数这是最简单的方法 处理链重组-他们不太可能离头部更远一个街区 - 使用
--chain
选项指定链的类型,例如bitcoin
、litecoin
、dash
、zcash
等。 - 您可以调整
--period-seconds
、--batch-size
、--max-workers
以获得性能
运行测试
> pip install -e .[dev] > echo"The below variables are optional" > exportBITCOINETL_BITCOIN_PROVIDER_URI=http://user:pass@localhost:8332 > exportBITCOINETL_LITECOIN_PROVIDER_URI=http://user:pass@localhost:8331 > exportBITCOINETL_DOGECOIN_PROVIDER_URI=http://user:pass@localhost:8330 > exportBITCOINETL_BITCOIN_CASH_PROVIDER_URI=http://user:pass@localhost:8329 > exportBITCOINETL_DASH_PROVIDER_URI=http://user:pass@localhost:8328 > exportBITCOINETL_ZCASH_PROVIDER_URI=http://user:pass@localhost:8327 > pytest -vv
进行毒性试验
> pip install tox > tox