Mastering Bitcoin Cp3_Bitcoin Core
Bitcoin Core - The reference implementation
bitcoin is developed by an open commu‐ nity of volunteers ; open source project
Bitcoin Development Environment
first, clone with git(bitcoin is an open-source)
*Selecting a Bitcoin Core Release
git tag : synchronize the local copy with a specific snapshot of the code repository
Git checkput : To synchronize the local code with this version
Git status : check out the version I chose
To synchronize the local code with this version
Building the Bitcoin Core Executables
Compile the source code( approx.. 1hr)
Check the location of bitcoind
Running a Bitcoin Core Node
“nodes” : run mostly by volun‐ teers and some of the businesses that build bitcoin applications
By running a node, you don’t have to rely on any third party to validate a transaction
Running a node, however, requires a permanently connected system with enough resources to process all bitcoin transactions. - may also need a lot of disk space and RAM.
Bit‐ coin Core will not be able to process transactions or update account balances until the full blockchain dataset is downloaded. Make sure you have enough disk space, bandwidth, and time to complete the initial synchronization. You can configure Bitcoin Core to reduce the size of the blockchain by discarding old blocks (see Example 3-2), but it will still download the entire dataset before discarding data.
Why would you want to run a node?
• If you are developing bitcoin software and need to rely on a bitcoin node for pro‐ grammable (API) access to the network and blockchain
• If you are building applications that must validate transactions according to bitcoin’s consensus rules. Typically, bitcoin software companies run several nodes.
• If you want to support bitcoin. Running a node makes the network more robust and able to serve more wallets, more users, and more transactions.
• If you do not want to rely on any third party to process or validate your transac‐ tions.
Running Bitcoin Core for the First Time
Run bitcoind by typing bitcoind into the terminal:
Error -> you need to build a configuration file, with at least an rpcuser and rpcpassword entry
Coniguring the Bitcoin Core Node
Bitcoin Core Application Programming Interface (API)
JSON-RPC interface
Bitcoin-cli : The result is a block hash,
Getting Information on the Bitcoin Core Client Status
Command: getinfo
displays basic information about the status of the bitcoin network node, the wallet, and the blockchain database JSON.
$ bitcoin-cli getinfo
{
"version" : 110200, //software client
"protocolversion" : 70002, 44 //bitcoin protocol: 70002
"blocks" : 396367, //block height(how many blocks are known to this client)
"timeoffset" : 0, "connections" : 15,
"proxy" : "",
"difficulty" : 120033340651.23696899,
"testnet" : false,
"relayfee" : 0.00010000,
"errors" : ""
}
(It will take some time, perhaps more than a day, for the bitcoind client to “catch up” to the current blockchain height as it down‐ loads blocks from other bitcoin clients. You can check its progress using getinfo to see the number of known blocks.)
Exploring and Decoding Transactions
Alice’s transaction ID (txid) :
0627052b6f28912f2703066a912ea577f2ce4da4caa5a5fbd8a57286c345c2f2.
to pass txid as a parameter using API:
$ bitcoin-cli getrawtransaction 0627052b6f28912f2703066a912ea577f2ce4da4caa5a↵ 5fbd8a57286c345c2f2
A transaction ID is not authoritative until a transaction has been confirmed. Absence of a transaction hash in the blockchain does not mean the transaction was not processed. This is known as “transaction malleability,” because transaction hashes can be modi‐ fied prior to confirmation in a block. After confirmation, the txid is immutable and authoritative.
The command getrawtransaction returns a serialized transaction in hexadecimal notation. To decode that, we use the decoderawtransaction command, passing the hex data as a parameter. You can copy the hex returned by getrawtransaction and paste it as a parameter to decoderawtransaction:
The transaction decode shows all the components of this transaction(inputs & outputs)
Used one input and generated two outputs. (one charge back to the sender / one to seller)
Exploring Blocks
Commands: getblock, getblockhash
blocks can be referenced either by the block height or by the block hash.
Alice’s transaction was included in block 277316.
1. use the getblockhash command, which takes the block height as the parameter and returns the block hash for that block:
$ bitcoin-cli getblockhash 277316 0000000000000001b6b9a13b095e96db41c4a928b97ef2d944a9b31b2cc7bdc4
2. Now that we know which block Alice’s transaction, we use the getblock command with the block hash as the parameter:
$ bitcoin-cli getblock 0000000000000001b6b9a13b095e96db41c4a928b97ef2d944a9b3↵ 1b2cc7bdc4 B
3. JSON output à The block contains 419 transactions [AL1] and the 64th transaction listed (0627052b…) is Alice’s coffee payment. The height entry tells us this is the 277316th block in the blockchain.
Using Bitcoin Core’s Programmatic Interface
The bitcoin-cli helper :very useful for exploring the Bitcoin Core API
Bitcoin Core’s API is a JSON-RPC interface.
(JavaScript Object Notation-Remote Procedure Call)-calling functions via a network protocol(http/https)
Curl: the versatile command-line HTTP client to construct one of these JSON-RPC calls:
there are libraries in most every programming language that “wrap” the Bitcoin Core API in a way that makes this a lot simpler
Our example code calculates that the total value transacted in this block is 10,322.07722534 BTC (including 25 BTC reward and 0.0909 BTC in fees). Compare that to the amount reported by a block explorer site by searching for the block hash or height. Some block explorers report the total value excluding the reward and excluding the fees. See if you can spot the difference.[AL2]
[AL1]JSON code 중 “tx” 부분에 [hundreds of transactions…] 합쳐서 419? 보는법 _책48쪽
[AL2]질문