The coordinator is a service that orchestrates the runtime of the Valence co-processor app. It is responsible for submitting ZK storage proof requests to the co-processor and submitting them to the configured authorizations contract on Neutron.
This document provides a developer-focused overview of the coordinator, explaining its architecture and how to customize it.
The coordinator's code is organized into three main files:
main.rs: The entry point of the application.strategy.rs: Defines theStrategystruct, which holds the coordinator's configuration and state.engine.rs: Implements the core logic of the coordinator.
The main.rs file is responsible for:
- Initializing the env-based configuration: loads environment variables from
.envfile configured during the setup - Loading configuration: reads the
neutron_strategy_config.tomlfile generated by the provisioner, and deserializes it into aNeutronStrategyConfigstruct used to initialize the strategy - Creating a
Strategyinstance: initializes theStrategystruct with the loaded configuration - Starting the coordinator: calls the
startmethod on theStrategyinstance, which spawns a new thread for the coordinator's main loop
The strategy.rs file defines the Strategy struct, which outlines the coordinator's scope of operation.
This struct holds the configuration and state needed for the coordinator to run and execute the business logic, including:
- Clients for interacting with the Neutron domain and the co-processor
- Configuration parameters loaded from
neutron_strategy_config.tomland env variables
The engine.rs file contains the core logic of the coordinator.
All flavors of Valence Coordinators should implement the ValenceCoordinator trait provided by
the valence-coordinator-sdk. See the section below for more information about it.
The key function of a coordinator, cycle, should define a single iteration that will be
executed in a continuous loop of coordinator operations. In the case of this template, it does the following:
- Sleep: pauses for the duration specified by the
STRATEGY_TIMEOUTenvironment variable to prevent firing excessive number of requests with no state changes - Generate Proof Request: constructs a
ControllerInputsstruct with the necessary parameters for the ZK proof request and serializes it as json - Submit Proof Request: sends the json-encoded proof request to the co-processor using the
coprocessor_client.provefunction - Process Proof: decodes the program and domain proofs received from the co-processor
- Submit Proof to Neutron for verification: calls the
post_zkp_on_chainfunction to send the ZK proof results to the authorizations contract on the Neutron network for verification - Tick Processor: ticks the processor contract on Neutron in order to process the message obtained from proof verification
The coordinator is built on top of the Valence Coordinator SDK. This SDK provides a set of utilities and abstractions to simplify the development of coordinators, including:
- A
ValenceCoordinatortrait that defines the main loop of the coordinator - Default implementations for spawning the coordinator and handling basic lifecycle events
- Helper functions for interacting with the Neutron network and the co-processor
- Telemetry and logging utilities
Utilizing valence-coordinator-sdk allows developers to focus on implementing the core logic of their coordinator without having to worry about the boilerplate code required for a robust and reliable service.
The coordinator is configured through a combination of a configuration file and environment variables.
The main configuration file for the coordinator is neutron_strategy_config.toml, which is generated by the provisioner.
These configuration files will be committed to VCS, and should therefore not contain any sensitive information.
Currently, this file contains the following information:
grpc_url: The URL of the Neutron gRPC server.grpc_port: The port of the Neutron gRPC server.chain_id: The chain ID of the Neutron network.coprocessor_app_id: The ID of the co-processor application.authorizations: The address of the authorization contract.processor: The address of the processor contract.cw20: The address of the CW20 token contract.
The following environment variables are used to configure the coordinator:
MNEMONIC: The mnemonic seed phrase for the on-chain signing account.LABEL: A label for the strategy.ERC20_ADDR: The address of the ERC20 token contract on Ethereum.STRATEGY_TIMEOUT: The timeout in seconds between each cycle of the coordinator.ERC20_BALANCES_STORAGE_INDEX: The storage index of the ERC20 token balances map.ETH_SRC_ADDR: The Ethereum address to prove the balance of.
To run the coordinator, execute the following command:
RUST_LOG=info cargo run --bin coordinatorIt will start the coordinator, which will then run in a continuous loop until manually stopped.