Learn how to run Chainhook as a service with a Stacks node to evaluate Stacks blocks against your predicates.
You can run Chainhook as a service to evaluate Stacks blocks against your predicates. You can also dynamically register new predicates by enabling the predicates registration API.
If you followed along with the previous guide on syncing a Stacks node, you should generate a Stacks.toml file within your Stacks node repository. Below is a sample Stacks.toml file:
Note
Ensure that the username, password, and rpc_port values in the Stacks.toml file match the values in the bitcoin.conf file. Also, note the rpc_bind port to use in the Chainhook.toml configuration in the next section.
In this section, you will configure Chainhook to communicate with the network. Run the following command in your terminal to generate the Chainhook.toml file:
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.
4Ensure stacks_node_rpc_url matches the rpc_bind in the Stacks.toml file.
The generated Chainhook.toml file should look like this:
Ensure the following configurations are matched to allow Chainhook to communicate with both Stacks and Bitcoin:
bitcoin.conf
Stacks.toml
Chainhook.toml
rpcuser
username
bitcoind_rpc_username
rpcpassword
password
bitcoind_rpc_password
rpcport
rpc_port
bitcoind_rpc_url
zmqpubhashblock
bitcoind_zmq_url
rpc_bind
stacks_node_rpc_url
endpoint
stacks_events_ingestion_port
Note
The bitcoind_zmq_url is optional when running Chainhook as a service using Stacks because Stacks will pull the blocks from Stacks and the Bitcoin chain.
You can also dynamically register new predicates with your Chainhook service.
Requirements
This section requires that you have Redis running locally. To install, refer to the Redis documentation.
First, ensure that the following lines in the Chainhook.toml file are uncommented to enable the predicate registration server:
Terminal
$
chainhook service start --predicate-path=print-event.json --config-path=Chainhook.toml
Start the Chainhook service with 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 the Chainhook service by passing multiple predicates: chainhook service start --predicate-path=predicate_1.json --predicate-path=predicate_2.json --config-path=Chainhook.toml