- Blockchain By Example
- Bellaj Badr Richard Horrocks Xun (Brian) Wu
- 634字
- 2021-06-10 18:53:27
Running Bitcoin Core
Depending on your OS, you need to create the bitcoin.conf configuration file in the default data directory located under the following paths:
- Windows: %APPDATA%\Bitcoin\
- Mac: $HOME/Library/Application Support/Bitcoin/
- Linux: $HOME/.bitcoin/
In Linux, create a .bitcoin directory using mkdir ~/.bitcoin, then create the bitcoin.conf file using nano ~/.bitcoin/bitcoin.conf.
Add the following lines to bitcoin.conf to define your client configuration (the comments after each # sign explain the parameters):
rpcuser=user_name #Username for JSON-RPC connections
rpcpassword=your_password #Password Username for JSON-RPC connections
server=1 #Tells Bitcoin-Qt and bitcoind to accept JSON-RPC commands
testnet=1 #Run on the test network instead of the real bitcoin network.
prune=550 #Enables pruning mode
Once copied, press Ctrl + X, then Y, and then Enter to save the file.
Now our first client is ready to run on the testnet, which is a bitcoin network created for testing purposes that follows the same rules as a main network. It's a public network using worthless bitcoins. You can use this network to send free transactions and test your applications.
It's now time to run Bitcoin Core. Open a new command line interface (CLI) window, and run the following command:
bitcoin-qt
Bitcoin Core will start running with its standard GUI interface connected to the testnet.
For the first run, it will ask you to set the data directory, which we will set to the default. It will then automatically create a wallet for you, start syncing with the testnet, and download the blockchain:
Alternatively, you could run the bitcoin daemon in CLI mode with the following command:
bitcoind
It's up to you to choose which mode to continue using (bitcoind or bitcoin-qt); the available RPC commands are the same. For my part, I'll continue this guide using btcoin-qt.
As Bitcoin Core starts up, it creates many subdirectories and files in the default data directory (.bitcoin), as shown in the following screenshot:
The main subdirectories are:
- blocks: Stores actual bitcoin blocks
- chainstate: Holds a LevelDB database for available UTXOs (short for Unspent Transaction Outputs)—in other words, a database storing how much money everyone has
- wallet: Contains an encrypted wallet.dat file, which stores the private keys
Even if the network sync is not yet finished, you can open the blocks/ subdirectory to see the blockchain's blocks stored in raw format. Each blk00*.dat file is a collection of several raw blocks:
We will read the content of one of these files later.
While the server (bitcoind or bitcoin-qt) is running, open another Terminal. Let's generate a new address for our wallet by executing bitcoin-cli getnewaddress, as in the following screenshot:
Basically, bitcoin-cli is a tool that enables us to issue RPC commands to bitcoind or bitcoin-qt from the command line (bitcoin-qt users can also access the bitcoin RPC interface by using the Debug console, under the Help menu).
Now we have finished with Bitcoin Core, let's leave it to sync with the blockchain and move on to configuring Electrum.