This step shows how to develop a relayer for your chain or project. The relayer plays a role in delivering messages between different chains, which is part of the cross-chain ecosystem.
There are two components required: Chain Listener and Chain Submitter.
Chain listener fetches data from the source chain and Poly chain, including block header, cross-chain events emitted from CCM, and Merkle proofs used to verify the cross-chain message in the Poly chain. The listed interface is necessary.
type IChainListener interface {
// Initialize with config
Init(*config.ListenerConfig, *poly.SDK) error
// Blocks to confirm
Defer() int
// New block check interval
ListenCheck() time.Duration
// Chain Id
ChainId() uint64
// Optional: Fetch block header and header hash, used to submit to Poly chain for verifications
Header(height uint64) (header []byte, hash []byte, err error)
// Optional: Last header sync state in Poly chain.
LastHeaderSync(uint64, uint64) (uint64, error)
// Scan cross chain transactions included in the block
Scan(uint64) ([]*msg.Tx, error)
// Compose cross chain message before submit to Poly chain
Compose(*msg.Tx) error
// Current chain height
LatestHeight() (uint64, error)
}
Chain Submitter delivers messages to the target chain, including validator changes of Poly chain to CCD and cross-chain messages. Additionally, Chain Submitter checks whether the transaction has been verified within the CCD. The listed interface is necessary.
type IChainSubmitter interface {
// Initialize with config
Init(*config.SubmitterConfig) error
// Submit message/transaction to the chain
Submit(msg.Message) error
// Start the thread
Start(context.Context, *sync.WaitGroup, bus.TxBus, bus.DelayedTxBus, msg.PolyComposer) error
// Process the cross chain message from Poly chain
ProcessTx(*msg.Tx, msg.PolyComposer) error
}
##2. Develop Steps on Poly-Relayer.
Poly-Relayer is a relayer project maintained by Poly Network. If you choose to develop based on Poly-Relayer, please follow the listed steps.
The Poly-Relayer project is based on the bridge-common library. So you need to:
- Add chain ID in the
bridge-common
project here. - Add chain client SDK here for common usage.
- Add chain wallet here for common usage.
Implement interface IChainListener
and IChainSubmitter
for the new chain.
Register ChainListener
and ChainSubmitter
in selectors located in the relayer.go
file.
##3. Preparation for Launch
The configurations are required when launching relayer:
- Make sure the necessary configuration is specified in
config.json,
including CCM contract, CCD contract, and other details for the chain. - You can see a sample here.
[!Note|style:flat|label:Notice]
- Specify roles to enable in
roles.json
and see a sample here.
Roles | Quantity Demand | Description |
---|---|---|
HeaderSync | One or multiple for each chain | Submits chain headers to the Poly chain. |
TxListen | Only one for each chain | Observes cross-chain transactions from the source chain and pushes them to the message queue. |
TxCommit | One or multiple for each chain | Consumes the message queue and submits the cross-chain transactions to poly. |
PolyListen | Only One for Poly chain | Observes cross-chain transactions from the Poly chain and pushes them to the message queue. |
PolyCommit | One or multiple for Poly chain | Consumes the message queue and submits the cross-chain transaction to the target chain. |
You are now ready for the relayer, please refer to the chapter Test and Launch for launching details.