In Substrate, consensus is break into 2 parts:
Let’s first discover how the consensus module cope with others
https://github.com/paritytech/substrate/blob/28a30847513ec41ce66c7b07bf750d93e786d41d/bin/node/cli/benches/block_production.rs#L171C41-L171C41
This is the code to benchmark block production. By looking into the code, we can learn how block authoring (consensus module) fits into the whole block production system.
let mut block_builder = client.new_block(Default::default()).unwrap();
block_builder.push(extrinsic_set_time(1)).unwrap();
import_block(client, block_builder.build().unwrap());
Claude Answer
What is the running process of the PoW consensus module? How it is cope with other parts of substrate?
- The PoW consensus is registered as an "engine" in the Substrate node. This injects it into the import queue and block authoring logic.
- When the node imports a new block, it goes through the import queue.
- The PowVerifier does preliminary seal verification on the block before import. This avoids duplicate verification.
- The PowBlockImport does full verification on seal, parent difficulty, etc. and imports the block. It updates the auxiliary difficulty storage.
- For block authoring, the mining worker periodically fetches the best chain head via the SelectChain fork-choice logic.
- It calls into the PowAlgorithm to calculate the next difficulty target.
- The Proposer creates the inherents and builds the block proposal with this difficulty.
- The miner tries to find a valid seal for this new block and submits it back via the MiningHandle.
- This flows back into the import queue to be verified and imported.
Block Finalization