Exercise: View Block Data
We've covered how data is formatted, transmitted, and stored in Bitcoin. Now, let's see it in action by looking at some real blockchain data.
In the next unit we'll walk through setting up a Bitcoin node, but for now, let's test out viewing Bitcoin blockchain data using this bitcoin-cli sandbox, https://bitcoindev.network/bitcoin-cli-sandbox/.
This site provides us access to a server that is running the Bitcoin core daemon, bitcoind. Here we can use the bitcoin command-line interface, bitcoin-cli, to run some commands and take a look at how transaction data is stored on the blockchain.
But first, let's provide some background on what we are doing here. Via this website, we won't be interacting with the mainnet blockchain. Instead, we'll be interacting with our own bitcoin network called a regtest network.
When running Bitcoin core you can run on three different networks:
- Mainnet: This is THE Bitcoin network. Transactions on this network have real value. Testing any applications here is highly risky.
- Testnet: This is a separate, much smaller network, designed specifically for testing. Transactions on this network exchange testnet coins. You can acquire testnet bitcoins at a testnet faucet such as https://testnet-faucet.mempool.co/. Testnet coins, by design, have no monetary value. They are simply exchanged between developers who are testing Bitcoin applications.
- Regtest: This is an entirely local network. You could create a regtest network in your home for you and your roommates. Or set one up at the office, etc. It does not link to mainnet or testnet in any way. It is your own private Bitcoin network.
As we will be playing with a regtest network here, you'll find that there isn't yet any blockchain data to be viewed. Let's see that for ourselves by running a few bitcoin-cli commands. You can find lists of bitcoin-cli commands and guides at these two links:
- https://chainquery.com/bitcoin-cli
- https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_calls_list
Let' start by viewing some data on this sandbox node:
$ bitcoin-cli -getinfo
Notice here that our balance is zero, and the number of blocks is also zero.
Generate a new address.
$ bitcoin-cli getnewaddress
We can verify that there are currently zero blockchains in our blockchain with the command
$ bitcoin-cli getblockcount
So, to add some data to our blockchain we'll need to mine some blocks, which gives us a mining reward. Run the below command to generate 10 new blocks.
$ bitcoin-cli -regtest generatetoaddress 10[your address]
Note: If your balance is still showing as zero after generating some mining rewards, try mining more blocks with this command.
$ bitcoin-cli generate 101
We should now see the hashes of the 10 blocks mined. Let's run the below commands again.
$ bitcoin-cli -getinfo
$ bitcoin-cli getblockcount
Now choose one of the hashes from the generated blocks and copy it so that we can take a closer look with this command…
$ bitcoin-cli getblock [block hash]
In the output, we get a variety of data including the number of confirmations, the block height, the Merkle root, and a list of transactions. At this point, all our blocks will only have one transaction, the coinbase transaction which gave us our mining reward. Let's copy that transaction ID, you'll find it labeled "tx", and take a closer look at it.
$ bitcoin-cli gettransaction [transaction ID]
You can see that the amount of the transaction is 50BTC which is appropriate for a new Bitcoin network. You can see the number of confirmations, the timestamp, transaction details, and more.
Later, we'll set up our own regtest nodes and work on constructing transactions via the bitcoin-cli interface.
Source: Saylor Academy This work is licensed under a Creative Commons Attribution 4.0 License.