Add/Customize Consensus
Use this guide to configure PoA or PoF for MainChain and SubChain, or to integrate another consensus mechanism through BaseConsensus.
Configure PoA or PoF
-
Choose PoA or PoF for MainChain. The example uses
HRC_CONSENSUS_TYPE;HRC_MAINCHAIN_CONSENSUSis also supported:
MainChain and SubChain use PoA or PoF in the current runtime flow. BFT has a separate implementation under hierachain/consensus/bft/; the API Ledger does not select it, and there is no HRC_BFT_ENABLED environment variable. SubChain uses PoA by default; pass config={"consensus_type": "proof_of_federation"} when initializing it to use PoF.
-
Start API server and verify basic flow works:
-
Provision the signing identity, trusted keys and any API key as described in Quickstart. The example below assumes authentication is disabled in dev/test. Submit an event, then read blocks to confirm commitment before submitting its proof:
curl -s -X POST http://localhost:2661/api/ledger/chains/supply_chain/create curl -s -X POST http://localhost:2661/api/ledger/chains/supply_chain/events \ -H 'Content-Type: application/json' \ -d '{"entity_id":"PROD-001","event_type":"production_complete","details":{"quantity":100}}' curl -s http://localhost:2661/api/ledger/chains/supply_chain/blocks
After the event appears in a persisted block, submit the proof:
Add a consensus implementation
-
Review the standard interface and existing implementations:
- Base:
hierachain/consensus/base_consensus.py - PoA:
hierachain/consensus/proof_of_authority.py - PoF:
hierachain/consensus/proof_of_federation.py - BFT (separate implementation, not selected by current MainChain/SubChain configuration):
hierachain/consensus/bft/
- Base:
-
Create new class extending
BaseConsensus(example):from hierachain.consensus.base_consensus import BaseConsensus from hierachain.core.block import Block class MyConsensus(BaseConsensus): def validate_block(self, block: Block, previous_block: Block) -> bool: raise NotImplementedError("Implement trusted validation") def finalize_block(self, block: Block, authority_id: str | None = None) -> Block: raise NotImplementedError("Implement signed finalization") def can_create_block(self, authority_id: str | None = None) -> bool: raise NotImplementedError("Implement proposer authorization") -
Wiring at initialization point (factory/integration point):
- Update the chain initialization or integration point to construct the new class.
HRC_CONSENSUS_TYPEcurrently supports PoA/PoF; a new name also requires configuration validation and selector support. - If a factory exists, add the mapping case
my_consensus→MyConsensus.
- Update the chain initialization or integration point to construct the new class.
-
Test the implemented
can_create_block(),finalize_block()andvalidate_block()methods, including invalid signatures and unauthorized proposers. Then verify the API integration: submit an event, wait for its persisted Sub-Chain block, and submit its proof. The base interface has noproposeorcommitmethods.
The abstract-method stubs above are intentionally incomplete and cannot finalize valid blocks. Merely defining the class does not install it into MainChain, SubChain or the API.
Related
- Architecture/Consensus: Consensus & Ordering
- Hierarchical Module: Hierarchical
- Config Reference: Config
- API Ledger: API Ledger