Learn how to run Chainhook as a service to evaluate your "if this, then that" predicates against the Bitcoin and Stacks blockchains.
In order to build out a more robust and secure web app, you can run Chainhook as a service to stream your events continously to a server you designate.
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:
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. Update the following parameters accordingly:
1Update bitcoind_rpc_username with the username set for rpcuser in bitcoin.conf.
2Update bitcoind_rpc_password with the password set for rpcpassword in bitcoin.conf.
3Update bitcoind_rpc_url with the same host and port used for rpcport in bitcoin.conf.
Additionally, if you want to receive events from the configured Bitcoin node, substitute stacks_node_rpc_url with bitcoind_zmq_url, as follows:
[storage]
working_dir="cache"
# The http API allows you to register / deregister predicates dynamically.
# This is disabled by default.
# [http_api]
# http_port = 20456
# database_uri = "redis://localhost:6379/"
[network]
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,
Now that your bitcoind and Chainhook configurations are complete, you can define the Chainhook Bitcoin predicate scope you would like to scan for.
These predicates are where you specify the if_this / then_that pattern to trigger Chainhook to deliver a result (either a file appendation or an HTTP POST request).
This example demonstrates scanning a portion of the Bitcoin blockchain to capture specific outputs from a Bitcoin address associated with a Stacking pool, in this case Friedgar Pool. Notice the then_that predicate specifying a file_append.
Note
You can get blockchain height and current block by referring to the Stacks Explorer.
Now, use the following command to scan the blocks based on the predicates defined in the stacking-pool.json file.
The start_block is a required field when using the http_postthen_that predicate.
Once you are finished setting up your endpoint, use the following command to scan the blocks based on the predicates defined in the stacking-pool-api.json file.
In the examples above, your Chainhook scanned historical blockchain data against predicates and delivered results. In this next section, you will learn how to set up a Chainhook that acts as an ongoing observer and event-streaming service.
You can start a Chainhook service with an existing predicate. You can also dynamically register new predicates by making an API call to your chainhook. In both of these instances, your predicates will be delivering their results to a server set up to receive results.
Initiate the chainhook service by passing the predicate path to the command as shown below:
Terminal
$
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 stacking-pool-api.json file.
You can also dynamically register new predicates with your Chainhook service. This means you can start a long-running process that exposes HTTP endpoints to register, deregister, and report on new predicates.
Requirements
This section requires that you have Redis running locally. To install, refer to the Redis documentation.
First, ensure that the following lines of code are uncommented in the Chainhook.toml file to enable the predicate registration server:
[http_api]
http_port=20456
database_uri="redis://localhost:6379/"
If you have an instance of Redis running locally, you can now start the Chainhook service by running the following command:
Terminal
$
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.
Use the following curl command template as an example:
Terminal
$
curl -X POST \
-H "Content-Type: application/json" \
-d @predicate.json \
http://localhost:20456/v1/chainhooks
Note
You can also run chainhook service by passing multiple predicates, ie chainhook service start --predicate-path=predicate_1.json --predicate-path=predicate_2.json --config-path=Chainhook.toml