You can run Chainhook as a service to evaluate your
if_this / then_that predicates against the Bitcoin blockchain, delivering results—either file appendations or HTTP POST requests to a server you designate—for your application's use case. You can also dynamically register new predicates as the service is running by enabling the predicates registration API.
Setting up a Bitcoin Node
The Bitcoin Core daemon (bitcoind) is a program that implements the Bitcoin protocol for remote procedure call (RPC) use. Chainhook can be set up to interact with the Bitcoin chainstate through bitcoind's ZeroMQ interface, its embedded networking library, passing raw blockchain data to be evaluated for relevant events.
This guide is written to work with the latest Bitcoin Core software containing bitcoind, Bitcoin Core 25.0.
While bitcoind can and will start syncing a Bitcoin node, customizing this node to your use cases beyond supporting a Chainhook is out of scope for this guide. See the Bitcoin wiki for "Running Bitcoin" or bitcoin.org Running A Full Node guide.
- Make note of the path of your
bitcoindexecutable (located within the
bindirectory of the
bitcoin-25.0folder you downloaded above appropriate to your operating system)
- Navigate to your project folder where your Chainhook node will reside, create a new file, and rename it to
bitcoin.conf. Copy the configuration below to this
- Find and copy your Bitcoin data directory and paste to the
datadirfield in the
bitcoin.conffile below. Either copy the default path (see list of default directories by operating system) or copy the custom path you set for your Bitcoin data
- Set a username of your choice for bitcoind and use it in the
rpcuserconfiguration below (
devnetis a default).
- Set a password of your choice for bitcoind and use it in the
rpcpasswordconfiguration below (
devnetis a default).
# Bitcoin Core Configuration
datadir=/path/to/bitcoin/directory/ # Path to Bitcoin directory
# Start zeromq
The below command is a startup process that, if this is your first time syncing a node, might take a few hours to a few days to run. Alternatively, if the directory pointed to in the
datadirfield above contains bitcoin blockchain data, syncing will resume.
Now that you have the
bitcoin.conf file ready with the bitcoind configurations, you can run the bitcoind node. The command takes the form
path/to/bitcoind -conf=path/to/bitcoin.conf, for example:
Once the above command runs, you will see
zmq_url entries in the console's stdout, displaying ZeroMQ logs of your bitcoin node.
In this section, you will configure Chainhook to match the network configurations with the bitcoin config file. First, install the latest version of Chainhook.
Next, you will generate a
Chainhook.toml file to connect Chainhook with your bitcoind node. Navigate to the directory where you want to generate the
Chainhook.toml file and use the following command in your terminal:
chainhook config generate --mainnet
Several network parameters in the generated
Chainhook.toml configuration file need to match those in the
bitcoin.conf file created earlier in the Setting up a Bitcoin Node section. Please update the following parameters accordingly:
bitcoind_rpc_usernamewith the username set for
bitcoind_rpc_passwordwith the password set for
bitcoind_rpc_urlwith the same host and port used for
Additionally, if you want to receive events from the configured Bitcoin node, substitute
bitcoind_zmq_url, as follows:
working_dir = "cache"
# The Http Api allows you to register / deregister
# predicates dynamically.
# This is disabled by default.
# http_port = 20456
# database_uri = "redis://localhost:6379/"
mode = "mainnet"
bitcoind_rpc_url = "http://localhost:8332"
bitcoind_rpc_username = "devnet"
bitcoind_rpc_password = "devnet"
# Bitcoin block events can be received by Chainhook
# either through a Bitcoin node's ZeroMQ interface,
# or through the Stacks node. The Stacks node is
# used by default:
# stacks_node_rpc_url = "http://localhost:20443"
# but zmq can be used instead:
bitcoind_zmq_url = "tcp://0.0.0.0:18543"
max_number_of_bitcoin_predicates = 100
max_number_of_concurrent_bitcoin_scans = 100
max_number_of_stacks_predicates = 10
max_number_of_concurrent_stacks_scans = 10
max_number_of_processing_threads = 16
max_number_of_networking_threads = 16
max_caching_memory_size_mb = 32000
tsv_file_url = "https://archive.hiro.so/mainnet/stacks-blockchain-api/mainnet-stacks-blockchain-api-latest"
Here is a table of the relevant parameters this guide changes in our configuration files.
Scan blockchain based on predicates
Now that your bitcoind and Chainhook configurations are complete, you can define the Chainhook predicates you would like to scan against bitcoin blocks. These predicates are where the user specifies the kinds of blockchain events that trigger Chainhook to deliver a result (either a file appendation or an HTTP POST request). This section helps you with an example JSON file to scan a range of blocks in the blockchain to trigger results. To understand the supported predicates for Bitcoin, refer to how to use chainhooks with bitcoin.
The following is an example to walk you through an
if_this / then_that predicate design that appends event payloads to the configured file destination.
Example 1 -
To generate a sample JSON file with predicates, execute the following command in your terminal:
chainhook predicates new stacking-pool.json --bitcoin
Replace the contents of the
stacking-pool.json file with the following:
"name": "Stacking Pool",
This example demonstrates scanning a portion of the Bitcoin blockchain to capture specific outputs from a Bitcoin address associated with a Stacking pool, Friedgar Pool.
You can get blockchain height and current block by referring to https://explorer.hiro.so/blocks?chain=mainnet
Now, use the following command to scan the blocks based on the predicates defined in the
chainhook predicates scan stacking-pool.json --config-path=./Chainhook.toml
The output of the above command will be a text file
bitcoin-transactions.txt generated based on the predicate definition.
Example 2 -
Let's generate another sample predicate, this time we are going to send the payload to an API endpoint:
chainhook predicates new stacking-pool-api.json --bitcoin
Replace the contents of the
stacking-pool-api.json file with the following:
"name": "Stacking Pool (API)",
start_blockis a required field when using the
Once you are finished setting up your endpoint, use the following command to scan the blocks based on the predicates defined in the
chainhook predicates scan stacking-pool-api.json --config-path=./Chainhook.toml
The above command posts events to the URL, http://localhost:3000/events mentioned in the JSON file.
Initiate Chainhook Service
In the examples above, our Chainhook scanned historical blockchain data against the user's predicates and delivered results. In this next section, let's learn how to set up a Chainhook that acts as an ongoing observer and event-streaming service.
We can start a Chainhook service with an existing predicate. We will also see how we can dynamically register new predicates by making an API call to our Chainhook. In both of these instances, our predicates will be delivering their results to a server set up to recieve results.
- Initiate the chainhook service by passing the predicate path to the command as shown below.
chainhook service start --predicate-path=stacking-pool-api.json --config-path=Chainhook.toml
The above command registers the predicate based on the predicate definition in the
Dynamically Register Predicates
You can also dynamically register new predicates with your Chainhook service.
First, we need to uncomment the following lines of code in the
Chainhook.toml file to enable the predicate registration server.
http_port = 20456
database_uri = "redis://localhost:6379/"
This assumes you have a local instance of Redis running.
Start the Chainhook service by running the following command:
chainhook service start --config-path=Chainhook.toml
To dynamically register a new predicate, send a POST request to the running predicate registration server at
localhost:20456/v1/chainhooks. Include the new predicate in JSON format within the request body. Use the following
curl command as an example:
curl -X POST \
-H "Content-Type: application/json" \
The sample response should look like this:
// Other transactions
Understand the output of the above JSON file with the following details.
applypayload includes the block header and the transactions that triggered the predicate.
rollbackpayload includes the block header and the transactions that triggered the predicate for a past block that is no longer part of the canonical chain and must be reverted. (Note: This is a chief component of Chainhook's reorg aware functionality, maintaining rollback data for blocks near the chaintip.)
You can also run chainhook service by passing multiple predicates. Example:
chainhook service start --predicate-path=predicate_1.json --predicate-path=predicate_2.json --config-path=Chainhook.toml