The Road to Blockchain System Exploration: Compilation and Exploration of Bitcoin Core

In the previous sections, we studied the underlying algorithms such as elliptic curves and finite fields that Bitcoin and blockchain rely on. The problem is that these algorithms do not exist independently, but are embedded as modules in the entire blockchain system. Therefore, if we don’t understand the system composition or application scenarios of the blockchain, it is difficult for us to understand the concepts derived from these algorithms or their functions. Therefore, in this section we will use the most original mode of the blockchain, which is the core of Bitcoin. Compile and run, first get the initial perceptual experience, and then in the following chapters, we can better understand elliptic curves, finite fields, how to form wallet addresses, and why data transmission in the blockchain system requires all kinds of strange things Strange data compression and other issues.

First, we use the ubuntu system, and then get the bitcoin kernel code through git clone https://github.com/bitcoin/bitcoin.git, note that we need to use v0.21.0 version, because earlier versions will be difficult for some inexplicable reasons Compilation passed, use the following code to switch branches:
git checkout v0.21.0

First of all, we need to install some compilation dependent libraries, use the following command to install:
sudo apt install libevent-dev libboost-system-dev libboost-filesystem-dev libboost-test-dev libboost-thread-dev

sudo apt install libdb-dev libdb+±dev

sudo apt install libminiupnpc-dev

sudo apt install libzmq3-dev

sudo apt install libqt5gui5 libqt5core5a libqt5dbus5 qttools5-dev qttools5-dev-tools

sudo apt install libqrencode-dev

After the code is downloaded, we enter the directory bitcoin. Since the Bitcoin kernel depends on Berkeley db4, we need to pre-install it. The code we downloaded contains the corresponding installation steps. Enter the root directory bitcoin of the downloaded code, and then execute the following command: $
. /contrib/install_db4.sh `pwd`
changes the command to install the dependent library of db4 in the current directory. After the above code runs, it will output the following prompt:

 export BDB_PREFIX='/home/ubuntu/bitcoin/db4'
 ./configure BDB_LIBS="-L${BDB_PREFIX}/lib -ldb_cxx-4.8" BDB_CFLAGS="-I${BDB_PREFIX}/include"

This is to set the environment variable so that the Bitcoin kernel knows how to get the location of the db4 code base when compiling, so we also execute the commands corresponding to the above two lines of code: export BDB_PREFIX='/home/ubuntu/bitcoin/db4
'
. /configure BDB_LIBS="-L BDBPREFIX / lib − ldbcxx − 4.8 " BDBCFLAGS = " − I {BDB_PREFIX}/lib -ldb_cxx-4.8" BDB_CFLAGS="-IBDBPREF I X / l ibl d bcxx4.8"BDBCFLAGS="I{BDB_PREFIX}/include”

Then execute the local script:
./autogen.sh
Then we need to set the compilation options through the config.sh script. During compilation, we need to ignore the Berkeley db version, so execute the following command:
./configure --with-incompatible-bdb
and execute Compile the source code with the following command:
make -j “ ( ( (((( (nproc)+1))"
The above command means to call all the current cpu cores to speed up through parallel compilation. After successful compilation, execute the following command to install the executable file of the Bitcoin kernel, so that we can in Run the bitcoind command directly in the system to start the bitcoin kernel:
sudo make install

After the compilation and installation are completed, we can directly invoke the Bitcoin kernel through the command line. We need to set a configuration file when running for the first time, and create a bitcoin.conf file in the bitcoin installation directory: vim /home/ubuntu/
. bitcoin/bitcoin.conf (note that this is my own path, readers need to determine their own installation path)
and then set the user name and password, its content is similar to the following:
rpcuser=you_name
rpcpassword=your_password
In the configuration file, we can set the bitcoin kernel Take the current server as a "full node", that is, it will download all the data on the Bitcoin chain to the local. If your server has enough memory and bandwidth, you can do this. I estimate that the current online data of the Bitcoin network It should be more than T. Since my server hard disk is only dozens of G, I must limit the upper limit of data interaction between the Bitcoin kernel and the entire network, so I add the following constraints in the configuration file: maxconnections=15 prune
=
5000
minrelaytxfee =0.0001
maxmempool=200
maxreceivebuffer=2500
maxsendbuffer=500

You can check the meaning of these configurations. We won’t waste much time here. After completing the above configuration, we can run the Bitcoin kernel. Use the following command to start the Bitcoin kernel: bitcoind
-daemon

After startup, we can use the Bitcoin client to interact with the kernel:
bitcoin-cli -getinfo
After executing the above command, if you see an output similar to the following, it means that the Bitcoin kernel starts normally:
{ “version”: 210000, “blocks” : 0, “headers”: 120000, “verificationprogress”: 1.142581983030402e-09, “timeoffset”: 1, “connections”: { “in”: 0, “out”: 3, “total”: 3 }, “proxy ": "", "difficulty": 1, "chain": "main", "relayfee": 0.00010000, "warnings": "" }















The commands executed through bitcoin-cli are essentially sending rpc requests to the bitcoin kernel, and then the other party returns the corresponding data. These commands are actually API interfaces provided by the kernel. We can use the following commands to query the specific usage of an interface, for example Execute the following command:
bitcoin-cli help getblockhash
Then the returned data is:
Please add a picture description
According to the prompt, we can obtain the hash value corresponding to the given label block through the getblockhash interface or command. For example, we execute the following command:
bitcoin-cli getblockhash 1000
in The result I got on this machine is:
Please add a picture description
as we will see later, the blockchain uses a variety of compression algorithms to compress and encode data for easy transmission. Here we can decompress a piece of data through the Bitcoin client to experience it and execute the command as follows:

bitcoin-cli decoderawtransaction
 0100000001186f9f998a5aa6f048e51dd8419a14d8a0f1a8a2836dd734d2804fe65fa35779000000008b483045022100884d142d86652a3f47ba4746ec719bbfbd040a570b1deccbb6498c75c4ae24cb02204b9f039ff08df09cbe9f6addac960298cad530a863ea8f53982c09db8f6e381301410484ecc0d46f1918b30928fa0e4ed99f16a0fb4fde0735e7ade8416ab9fe423cc5412336376789d172787ec3457eee41c04f4938de5cc17b4a10fa336a8d752adfffffffff0260e31600000000001976a914ab68025513c3dbd2f7b92a94e0581f5d50f654e788acd0ef8000000000001976a9147f9b1a7fb68d60c536c2fd8aeaa53a8f3cc025a888ac00000000

The result after executing the above command is as follows:

{
    
    
  "txid": "0627052b6f28912f2703066a912ea577f2ce4da4caa5a5fbd8a57286c345c2f2",
  "hash": "0627052b6f28912f2703066a912ea577f2ce4da4caa5a5fbd8a57286c345c2f2",
  "version": 1,
  "size": 258,
  "vsize": 258,
  "weight": 1032,
  "locktime": 0,
  "vin": [
    {
    
    
      "txid": "7957a35fe64f80d234d76d83a2a8f1a0d8149a41d81de548f0a65a8a999f6f18",
      "vout": 0,
      "scriptSig": {
    
    
        "asm": "3045022100884d142d86652a3f47ba4746ec719bbfbd040a570b1deccbb6498c75c4ae24cb02204b9f039ff08df09cbe9f6addac960298cad530a863ea8f53982c09db8f6e3813[ALL] 0484ecc0d46f1918b30928fa0e4ed99f16a0fb4fde0735e7ade8416ab9fe423cc5412336376789d172787ec3457eee41c04f4938de5cc17b4a10fa336a8d752adf",
        "hex": "483045022100884d142d86652a3f47ba4746ec719bbfbd040a570b1deccbb6498c75c4ae24cb02204b9f039ff08df09cbe9f6addac960298cad530a863ea8f53982c09db8f6e381301410484ecc0d46f1918b30928fa0e4ed99f16a0fb4fde0735e7ade8416ab9fe423cc5412336376789d172787ec3457eee41c04f4938de5cc17b4a10fa336a8d752adf"
      },
      "sequence": 4294967295
    }
  ],
  "vout": [
    {
    
    
      "value": 0.01500000,
      "n": 0,
      "scriptPubKey": {
    
    
        "asm": "OP_DUP OP_HASH160 ab68025513c3dbd2f7b92a94e0581f5d50f654e7 OP_EQUALVERIFY OP_CHECKSIG",
        "hex": "76a914ab68025513c3dbd2f7b92a94e0581f5d50f654e788ac",
        "reqSigs": 1,
        "type": "pubkeyhash",
        "addresses": [
          "1GdK9UzpHBzqzX2A9JFP3Di4weBwqgmoQA"
        ]
      }
    },
    {
    
    
      "value": 0.08450000,
      "n": 1,
      "scriptPubKey": {
    
    
        "asm": "OP_DUP OP_HASH160 7f9b1a7fb68d60c536c2fd8aeaa53a8f3cc025a8 OP_EQUALVERIFY OP_CHECKSIG",
        "hex": "76a9147f9b1a7fb68d60c536c2fd8aeaa53a8f3cc025a888ac",
        "reqSigs": 1,
        "type": "pubkeyhash",
        "addresses": [
          "1Cdid9KFAaatwczBwBttQcwXYCpvK8h7FK"
        ]
      }
    }
  ]
}

Later, we will discuss in detail the data compression algorithm and compression format of the blockchain during data transmission. We can also interact with the Bitcoin core through code. Many programming languages ​​provide corresponding interface packages. For example, python and python-bitcoinlib can be used to interact with the Bitcoin core. Let’s look at related examples and first install the library:

pip install python-bitcoinlib

Then create the file python_bitcoin.py and add the following content:

from bitcoin.rpc import RawProxy

#连接比特币内核
p = RawProxy()

info = p.getblockchaininfo()

print(info['blocks'])

The above code queries the Bitcoin kernel for the current number of blocks. After running the above code on my local machine, the returned result is as follows:
434100
means that on my current machine, the kernel has downloaded 434100 blocks. It should be noted that if the Bitcoin kernel we are running is not started in the form of a "full node", then some interfaces cannot return valid data. For example, execute the following command to obtain the hash with block number 277316: bitcoin-cli getblockhash
277316
The result returned after the command is executed is:
0000000000000001b6b9a13b095e96db41c4a928b97ef2d944a9b31b2cc7bdc4
If we use the getblock command and get the block data through the above hash, an error may be returned. For example, execute:
bitcoin-cli getblock 000000000000000 1b6b9a13b095e96db41c4a928b97ef2d944a9b31b2cc7bdc4
returns after running the above code on my local machine:
error code: -1
error message:
Block not available (pruned data)

Due to the limited hard disk space in this section, I did not use the "full node" mode to run the kernel, so when the kernel synchronizes the blockchain data, it only downloads the header information of some blocks to the local, which leads to local inability to Read the data content of the block.

In the next section, we will see how to implement the wallet address using the algorithm we mentioned in the previous sections, and at the same time verify the correctness of our algorithm through the interface provided by the Bitcoin kernel. For more content, please search coding Disney at station b

Guess you like

Origin blog.csdn.net/tyler_download/article/details/131353202