A Beginner's Guide to Bitcoin Blockchain Development (Part 2)

A Beginner's Guide to Bitcoin Blockchain Development (Part 2)

This article uses Docker containers to quickly install and configure the Bitcoin test network (bitcoin-testnet) of a private node as a development test environment on the commonly used Ubuntu 14.04 operating system. It also uses Node.js as an example to illustrate how to call the RPC interface service provided by the Bitcoin wallet node to implement some specific Bitcoin-related account and transaction operations.

RPC (Remote Procedure Call) is a protocol that requests services from a remote computer program over the network without having to understand the underlying network technology.
On the Bitcoin wiki page (https://en.bitcoin.it/wiki/API_reference_(JSON-RPC)), you can see that in addition to Node.js, there are many other languages ​​that can call Bitcoin's RPC. You can refer to the content of this article to choose a language that suits you for specific experiments.
In the above webpage, you can also read further and understand the list of commands that can be called by Bitcoin RPC (https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_calls_list).

Regarding how to install the Ubuntu 14.04 operating system and Node.js, if necessary, you can refer to the instructions in the previous article "Bitcoin Blockchain Development from Beginner to Advanced Guide 1" for installation (http://www.8btc.com/blockchain_develope_lesson_1).

1. Install the Docker operating environment

1. Use the apt-get command to install the Docker container support software:

$sudo apt-get install docker.io

2. Create a soft link
$sudo ln -sf /usr/bin/docker.io /usr/local/bin/docker

3. Check the Docker version
$ docker –version
If you see a message similar to "Docker version 1.6.2, build 7c8fca2", it means you have successfully installed Docker on Ubuntu 14.04.

For more information about how to install and run Docker, you can search online for more information or visit this page: "Docker First Try" (http://my.oschina.net/lamciuloeng/blog/226107)

2. Install and run the Bitcoin test network (bitcoin-testnet)

1. Download the Docker image of the Bitcoin test network (bitcoin-testnet)
$sudo docker pull freewil/bitcoin-testnet-box

2. Run the Docker image
$sudo docker run -t -i -p 19001:19001 -p 19011:19011 freewil/bitcoin-testnet-box

Note: The ports 19001 and 19011 in the above command are configured to provide RPC services to the two nodes.

3. After entering the Docker runtime environment, enter the following command to start the Bitcoin test network:
$ make start
After successful startup, two Bitcoin test wallet nodes will be simulated and run locally to form a private Bitcoin test network.

Enter the following command to view the test network node status information:
$ make getinfo

The displayed prompt information is as follows, and the Chinese version is an explanation of some key information:
bitcoin-cli -datadir=1 getinfo //Information of the first wallet node
{
"version": 120100,
"protocolversion": 70012,
“walletversion”: 60000,
“balance”: 0.00000000, //The account balance of the first wallet node, initially 0
"blocks": 0, //The number of blocks that have been generated. The initial startup is 0. Further commands can be used to simulate the generation of block data "timeoffset": 0,
"connections": 1,
“proxy”: “”,
"difficulty": 4.656542373906925e-10,
“testnet”: false,
"keypoololdest": 1467253951,
"keypoolsize": 101,
“paytxfee”: 0.00000000,
"relayfee": 0.00001000,
“errors”: “”
}
bitcoin-cli -datadir=2 getinfo //Information of the second wallet node
{
"version": 120100,
"protocolversion": 70012,
“walletversion”: 60000,
"balance": 0.00000000,
"blocks": 0,
"timeoffset": 0,
"connections": 1,
“proxy”: “”,
"difficulty": 4.656542373906925e-10,
“testnet”: false,
"keypoololdest": 1467253951,
"keypoolsize": 101,
“paytxfee”: 0.00000000,
"relayfee": 0.00001000,
“errors”: “”
}
4. Initialize and test blockchain data <br/>Enter the following commands in the Docker run window to initialize and create basic blockchain data for further program examples.
Note: In the official Bitcoin network environment, it takes an average of 10 minutes to generate a new block. However, in the special testnet environment here, blocks can be generated instantly and in batches through simple command control, which is convenient for program development and testing.

make generate
//Description: Simulate the generation of a new block record

make generate BLOCKS=200
//Description: Simulate the generation of 200 new block records

make getinfo
//Description: Check the latest wallet status including balance information. You can see that the account balance of the first wallet node has changed to 5050.00000000 BTC, which is the test bitcoin generated by simulated block mining.

make sendfrom1 ADDRESS=mkiytxYA6kxUC8iTnzLPgMfCphnz91zRfZ AMOUNT=10
//Description: Transfer 10 BTC to the specified test wallet address. Note that the test transfer address "mkiytxYA6kxUC8iTnzLPgMfCphnz91zRfZ" in the command corresponds to the test wallet address required for subsequent test program examples.

make generate BLOCKS=10
//Description: Simulate the generation of 10 new block records to ensure that the above transfer transactions are sufficiently confirmed

make getinfo
//Description: Check the latest wallet status including balance information. You can see that the account balance of the first wallet node has changed to 5539.99996160 BTC, which means that 10 BTC plus a small amount of miner fees have been spent on transactions.

3. Install the RPC support library for Node.js\
There are many commonly used Node.js RPC support libraries. Here we choose the open source project kapitalize.

Open a new terminal command line interface on the Ubuntu desktop and enter the following command to install:
$ npm install kapitalize

For more information about the Kapitalize open source project, please refer to: https://github.com/shamoons/Kapitalize


4. Run the sample program

Copy and save the following sample code to the test environment (save the file name as RpcTestnet.js), and enter the following command in the command line to run and see the running results:
node RpcTestnet.js

Note: Each time you run the test code, you need to enter "make generate BLOCKS=10" in the command line of the Docker runtime environment to simulate the generation of new block records so that the transaction records generated by the test code can be effectively confirmed.

The source code of the sample program RpcTestnet.js is as follows (the source code file can be downloaded here):

/********************* Starting point of sample code*************************/

/********************* End of sample code**********************/
Based on this program, after further understanding of the Bitcoin protocol, we can call the RPC interface to further develop more complex functions, such as building specific transaction data packets (such as multi-signature data) to meet specific business needs. We will introduce this in depth in the future.


<<:  Brexit causes depreciation of RMB, does this mean more opportunities for Bitcoin?

>>:  If Ethereum doesn’t want to fail on TheDAO, solving smart contract and management issues is the right path

Recommend

Marriage line shows the relationship between men and women

Marriage line shows the relationship between men ...

What facial features do women have that will bring bad luck to their husbands?

In fact, it can be found in life that some women ...

How to tell what kind of people can't have a happy marriage?

Marriage is very important to everyone, and every...

What kind of man is worth marrying?

Women are always pursuing perfection, even though...

The second Spider Miners Club was grandly held in Shenzhen

On December 19, 2019, the second session of Spide...

What kind of career line is the best in palmistry?

The career line, also known as the jade pillar li...

Walking posture and personality

People have different moods and temperaments, so ...

Grayscale: BTC can replace some Nasdaq 100 investments

Nasdaq-100 stocks and Bitcoin are distinct but co...

A beautiful but unlovable woman

People all like to appreciate beautiful things, a...

Coin Zone Trends: Bitcoin Price Trends Based on Big Data This Week (2017-08-29)

Rebound without volume, short-term watch more les...

What does a mole on a man’s face mean?

In mole physiognomy, different moles represent di...

Judging from the characteristics of a woman's wealth and nobility from her face

It is said that appearance reflects the heart. In...

The face of a person who is always laughing and noisy in life

In life, there are people who are very optimistic...