Create a Hybrid Node
How to create a Substrate-based node that employs hybrid consensus
This guide demonstrates how to create a Substrate-based node that employs hybrid consensus, using SHA3 proof of work to dictate block authoring and the Grandpa finality gadget to provide deterministic finality. The minimal proof of work consensus lives entirely outside of the runtime.
The Grandpa finality relies on getting its authority sets from the runtime using the Grandpa API. Therefore, you need a runtime that provides this API to successfully compile a node implementing this guide.
Use cases
Customize the consensus mechanisms of a Substrate chain.
Steps preview
Configure the block import pipeline.
Create an import queue.
Spawn the proof-of-work authorship task.
Spawn the Grandpa task.
Configure the block import pipeline
We begin by creating the block import for Grandpa. In addition to the block import itself, we get back a grandpa_link
. This link is a channel over which the block import can communicate with the background task that actually casts Grandpa votes. The details of the Grandpa protocol are beyond the scope of this guide.
In node/src/service.rs
, create the Grandpa block import:
With the grandpa block import created, we can now create the PoW block import. The Pow block import is the outer-most layer of the block import onion and it wraps the grandpa block import.
Create an import queue
With the block imports set up, we can proceed to create the import queue. We make it using PoW's import_queue
helper function. Notice that it requires the entire block import pipeline which we refer to as pow_block_import
because PoW is the outermost layer.
Spawn the PoW authorship task
Any node that is acting as an authority, typically called "miners" in the PoW context, must run a mining worker that is spawned by the task manager.
Spawn the Grandpa task
Grandpa is not CPU intensive, so we use a standard async
worker to listen to and cast Grandpa votes. We begin by creating a Grandpa Config
:
We can then use this config to create an instance of GrandpaParams
.
With the parameters established, we can now create and spawn the authorship future.
Examples
Resources
POW Algorithm trait
Last updated