Category: Uncategorized

Run Bitcoin full test node in Linux

This installation is performed on freshly installed Ubuntu system.

sudo apt update
snap install bitcoin-core

This should install Bitcoin Core on Ubuntu machine-

But lets not use GUI for this and setup test node using Bitcoin Daemon.

Bitcoin daemon should be installed in root folder and the executable can be found on this path- /snap/bitcoin-core/141/bin

Use below command to find the version of the daemon-

bitcoin-core.daemon --version

Run command –

bitcoin-core.daemon

Here the last line its expecting a bitcoin.conf file in location- /home/sandy/snap/bitcoin-core/common/.bitcoin/bitcoin.conf. This oath might be different for you.

Create a bitcoin.conf file at this location and paste in following to configure bitcoin daemon to run in test node.

Set the chain=test

set path to blocksdir and datadir

set txindex=1 to index the transaction

Set rpcuser and rpcpassword to use JSON rpc api

server=1 tells Bitcoin-QT to accept JSON-RPC commands.

# [chain]
# Test Network.
chain=test

# [core]
# Specify a non-default location to store blockchain data.
blocksdir=/home/sandy/snap/bitcoin-core/common/.bitcoin
# Specify a non-default location to store blockchain and other data.
datadir=/home/sandy/snap/bitcoin-core/common/.bitcoin

# Options only for mainnet
[main]

# Options only for testnet
[test]

# Options only for regtest
[regtest]

txindex=1

server=1

rpcuser=your_rpc_user
rpcpassword=your_rpc_pwd

rpcallowip=*

-txindex
Maintain a full transaction index, used by the getrawtransaction rpc
call (default: 0)

-rpcpassword=
Password for JSON-RPC connections

-rpcuser=
Username for JSON-RPC connections

Run following command to run the daemon in test node. This will download the data which is around 29gb at the time of writing this blog.

bitcoin-core.daemon -txindex -rpcuser=your_rpc_user -rpcpassword=your_rpc_pwd

This should now start the daemon and will check if the data is available, if not this should start download the blocks.

First it wll pre-Synchronize the block headers and then start Synchronizing headers. At the time of writing this blog there are around 2428000 blocks in test node.

Once synchronizing is complete it should start downloading and indexing blocks-

2023-07-23T15:14:46Z UpdateTip: new best=0000000000002f4ee789f0bd19066bccd78172ca71995505996fbff1a3341eac height=271984 version=0x00000002 log2_work=60.719797 tx=2107245 date=’2014-08-05T09:37:28Z’ progress=0.031835 cache=337.4MiB(2518207txo)

Here the height is the block it has synchronised and the progress. When the progress is near to 1 it will come to completion of synchronising the node.

In the above example 0000000000002f4ee789f0bd19066bccd78172ca71995505996fbff1a3341eac is a block hash

progress when near to 1 means it about to complete synchronising all blocks.

A new folder called testnet3 will be created in .bitcoin folder – This should be in user folder and not root folder.

For me it took less than 2 hours to sync all blocks.

2023-07-23T16:55:23Z UpdateTip: new best=0000000000000022020dfc2655e4489eb54a8ae306080a47d1fa0be46e0c43a8 height=2443382 version=0x31636000 log2_work=75.416383 tx=66092181 date='2023-07-23T16:40:35Z' progress=0.999999 cache=479.2MiB(3458766txo)
2023-07-23T16:56:04Z New outbound peer connected: version: 70015, blocks=2443382, peer=76 (block-relay-only)
2023-07-23T17:00:42Z Saw new header hash=0000000000001019c41ee044adf1c02d347ed52d875eefeb8e629f0a5baa4a8b height=2443383
2023-07-23T17:00:42Z [net] Saw new cmpctblock header hash=0000000000001019c41ee044adf1c02d347ed52d875eefeb8e629f0a5baa4a8b peer=3
2023-07-23T17:00:42Z UpdateTip: new best=0000000000001019c41ee044adf1c02d347ed52d875eefeb8e629f0a5baa4a8b height=2443383 version=0x29e7a000 log2_work=75.416383 tx=66092225 date='2023-07-23T17:00:37Z' progress=1.000000 cache=479.2MiB(3458790txo)

The last block to sync was 2443382

It also listens to any new block been created on chain- 2443383 which happened after 5 minutes. See highlighted.

Quick check-

Pick the block – 2443383

bitcoin-core.cli -rpcuser=xxxx -rpcpassword=xxxxx getblockhash 2443382

It should print the hash-

0000000000000022020dfc2655e4489eb54a8ae306080a47d1fa0be46e0c43a8

Take this hash and use command to get block

bitcoin-core.cli -rpcuser=xxxxxx -rpcpassword=xxxxx getblock 0000000000000022020dfc2655e4489eb54a8ae306080a47d1fa0be46e0c43a8

Output – should show details of the block along with the transactions- I have trimmed the transactions here due to space.

{
  "hash": "0000000000000022020dfc2655e4489eb54a8ae306080a47d1fa0be46e0c43a8",
  "confirmations": 2,
  "height": 2443382,
  "version": 828596224,
  "versionHex": "31636000",
  "merkleroot": "9e0403fef59a803426851f3c428306c15d81360f665f37cc62549825056c6823",
  "time": 1690130435,
  "mediantime": 1690129060,
  "nonce": 2392034863,
  "bits": "192495f8",
  "difficulty": 117392538.8721802,
  "chainwork": "000000000000000000000000000000000000000000000aad36ccc3b8310b4b65",
  "nTx": 26,
  "previousblockhash": "000000000000000b994cbbfe015d77aaa70140af817b6d28d228da690d1ce4da",
  "nextblockhash": "0000000000001019c41ee044adf1c02d347ed52d875eefeb8e629f0a5baa4a8b",
  "strippedsize": 5511,
  "size": 8258,
  "weight": 24791,
  "tx": [
    "b96a95b85e2ec270b471afded7d49c7db46d5002ac43de0bbcc13a4dd972f763",
    "823f5a59b5c6878ba68f21fdfdc9fe796fc879a471d69fcd0430200d9e5aba9a",
    
  ]
}

GetRawTransactions

bitcoin-core.cli -rpcuser=xxxx -rpcpassword=xxxxx getrawtransaction b96a95b85e2ec270b471afded7d49c7db46d5002ac43de0bbcc13a4dd972f763

Output-

010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff1e0376482519444d47426c6f636b636861696e309e3c095f01000000000000ffffffff02b29a270000000000160014b23716e183ba0949c55d6cac21a3e94176eed1120000000000000000266a24aa21a9eddcc8960d06c43b2eaab1e03370081155bac04c26ea7282b8dffaf7c2c452d2220120000000000000000000000000000000000000000000000000000000000000000000000000

This confirms that the blocks are indexed as the getrawtransaction looks for node and not your own wallet transaction.

How to get transaction details will put that in another blog.

Hope this helps to configure full test node on your system.

Performance

To run the daemon blockonly option – 

bitcoind -daemon -blocksonly

.By default, bitcoind relays transactions while you’re syncing and using -blocksonly turns that off. It probably won’t make a huge difference but should save you some bandwidth. 

Increase dbcache

-dbcache= The default for this is 450. If you have 8GB of RAM, you could set this to 4096

Python- Class Method

Class Methods are like Class Varaibles which works on Class but not on object level.

So this methods are bound to class and not object. See class varaibles here

Clas Methods are decorated with @classmethod attribute to the method in class.

This method can be called without creating instance of the class.

See below example definition and usage of class method.

In the above example get_instance_counter() method is decorated as @classmethod and a cls variable is passed. This holdsthe class reference for which the method is called.

The constructor works normal with __init__ method and updates calss variable counter increments the counter when an instance is created. This class variable accessed in the get_instance_counter() method.

You can also provision Python class to have multiple constructor with class method. See here – How to add multiple constructor to Python class?