실습
비트코인 Blockchain RPCs 사용
아데왕자
2021. 6. 20. 23:47
이전 글: 2021.05.25 - [실습] - 비트코인 풀노드 실행하기
비트코인 풀노드를 실행하면, 이제 비트코인 RPC를 사용할 수 있습니다. 그 중에서 Blockchain의 유용한 정보를 살펴볼 수 있는 Blockchain RPC들을 알아봅시다. 참고로 전체 RPC API 리스트는 여기에서 확인할 수 있습니다.
먼저, bitcoind 프로그램이 RPC를 사용하도록 설정해야합니다. bitcoin.conf 파일에 아래를 추가합니다.
server=1
rpcport=8332
RPC에 인증을 추가하려면 rpcauth 값을 추가로 설정해야합니다. rpcauth.py 스크립트를 통해 생성할 수 있습니다.
$ wget https://raw.githubusercontent.com/bitcoin/bitcoin/master/share/rpcauth/rpcauth.py
$ python rpcauth.py ${USERNAME} ${PASSWORD}
암호는 적지 않으면 랜덤하게 생성됩니다. 얻은 rpcauth 값을 bitcoin.conf에 추가하면 됩니다. 이제 RPC를 사용할 때에 username, password로 인증을 요구하게 됩니다.
Blockchain RPCs
몇 가지 주요 RPC가 잘 작동하는지 아래와 같이 확인할 수 있습니다.
getblockcount: 현재 가장 유효한 블록체인의 높이를 리턴합니다.
$ curl -v --user ${USERNAME}:${PASSWORD} --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getblockcount", "params": []}' -H 'content-type: text/plain;' http://127.0.0.1:8332/ | jq
{
"result": 688154,
"error": null,
"id": "curltest"
}
getblockhash: height에 있는 블록의 hash 값을 리턴합니다.
$ curl -v --user ${USERNAME}:${PASSWORD} --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getblockhash", "params": [688142]}' -H 'content-type: text/plain;' http://127.0.0.1:8332/ | jq
{
"result": "0000000000000000000806977298148e3bcb73acc000c05c1dd631802a67f874",
"error": null,
"id": "curltest"
}
getblockchaininfo: 블록체인의 다양한 상태값을 리턴합니다.
$ curl -v --user ${USERNAME}:${PASSWORD} --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getblockchaininfo", "params": []}' -H 'content-type: text/plain;' http://127.0.0.1:8332/ | jq
{
"result": {
"chain": "main",
"blocks": 688154,
"headers": 688295,
"bestblockhash": "0000000000000000000806977298148e3bcb73acc000c05c1dd631802a67f874",
"difficulty": 19932791027262.74,
"mediantime": 1624085324,
"verificationprogress": 0.9980239400607028,
"initialblockdownload": true,
"chainwork": "00000000000000000000000000000000000000001ed4fdadfa69ab37dba7beb3",
"size_on_disk": 398781626051,
"pruned": false,
"softforks": {
"bip34": {
"type": "buried",
"active": true,
"height": 227931
},
"bip66": {
"type": "buried",
"active": true,
"height": 363725
},
"bip65": {
"type": "buried",
"active": true,
"height": 388381
},
"csv": {
"type": "buried",
"active": true,
"height": 419328
},
"segwit": {
"type": "buried",
"active": true,
"height": 481824
},
"taproot": {
"type": "bip9",
"bip9": {
"status": "locked_in",
"start_time": 1619222400,
"timeout": 1628640000,
"since": 687456,
"min_activation_height": 709632
},
"active": false
}
},
"warnings": ""
},
"error": null,
"id": "curltest"
}
getblock: 블록 hash 값에 대한 정보를 리턴합니다.
$ curl -v --user ${USERNAME}:${PASSWORD} --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getblock", "params": ["0000000000000000000806977298148e3bcb73acc000c05c1dd631802a67f874"]}' -H 'content-type: text/plain;' http://127.0.0.1:8332/ | jq
{
"result": {
"hash": "0000000000000000000806977298148e3bcb73acc000c05c1dd631802a67f874",
"confirmations": 19,
"strippedsize": 852805,
"size": 1439781,
"weight": 3998196,
"height": 688154,
"version": 547356676,
"versionHex": "20a00004",
"merkleroot": "d4c66ce29130cc9bb6e5a90b5029b5b571994decb45dd3e019fb8d6948defbb6",
"tx": [
"b2bc4f573a6120167838679ffb2b94e1126ab9bf5c70546e7004a413ae053b45",
"c08e359807e5ba7a9d7ea01da6982f9ee41271c3ed6781139993a48470651483",
"8954fcaa42818f5b63de9d786f0ae6d16af6cb282bc3cac048dea828fa4d8dbc",
...
"647b865c4531b8a247397229f90c3b7d58ae3ce9291b706e65c7e95be2fbc29b",
"c3188204a7148e9191ee9552deb482ee5b9e40c6f436fb7f3d0f87ea12c4d36d",
"a05c2ecfcbeccd5e0ac5a6e46c1aca4ad662bc36593ee5df6c099a0af1ffd5a4"
],
"time": 1624088864,
"mediantime": 1624085324,
"nonce": 4238424379,
"bits": "170e1ef9",
"difficulty": 19932791027262.74,
"chainwork": "00000000000000000000000000000000000000001ed4fdadfa69ab37dba7beb3",
"nTx": 2570,
"previousblockhash": "0000000000000000000d6fcf907fae467b6288388d5090b851ce0d067a519706",
"nextblockhash": "00000000000000000004b298cbb779458cb70a9f2196aec56e456e4e50000559"
},
"error": null,
"id": "curltest"
}
참고
[1] https://developer.bitcoin.org/reference/rpc/