Creating a custom consensus algorithm summary¶
This chapter summarizes the steps that you need to take to create a custom consensus algorithm:
Decide how the consensus on blocks will be reached on your custom network. Does your customization require an adjustment to one of the three existing algorithms, or would your algorithm be better described as a new approach?
Create a custom consensus rule engine class based on one of the existing three classes:
PowConsensusRuleEngine
,PoSConsensusRulesEngine
, andPoAConsensusRulesEngine
. Because you will be using at least some of the existing consensus rules, choose the rule engine that is closest to the algorithm that you intend to create. Your custom consensus rule engine should inherit from the class you choose. Remember thatPoSConsensusRulesEngine
andPoAConsensusRulesEngine
inherit fromPowConsensusRuleEngine
.Make your consensus options available to the rules. In most cases, you will just be adding to or modifying one of the three existing classes:
ConsensusOptions
,PoAConsensusOptions
, andPoSConsensusOptions
. If your consensus options class inherits from one of the existing classes, you will want to make sure you can override any hardcoded settings in the base class, possibly by writing a new base class constructor, when you instantiate your custom class.Adjust your network class to instantiate your own custom consensus options class.
Create the rules themselves, inheriting from, or creating, any abstract rule classes as required.
Implement your own “mining” class if required. For example, if your customization is essentially the supplied PoS consensus algorithm with customized rules and consensus options, you will not need to alter the
PosMinting
class. In relation to consensus, the important thing about all three “mining” classes is the call they make toConsensusManager.BlockMinedAsync()
when a block is mined. If your mining approach differs to the three existing implementation, you should keep in mind that consensus checks need to be carried out on any newly created blocks.Create an extension method to register your custom consensus algorithm as a Consensus feature in a Full Node build. If you implemented your own “mining” class, you must create a new Mining feature as well.