I've read the rustdocs about ChainSpecExtensions. For example https://paritytech.github.io/substrate/master/sc_service/trait.ChainSpecExtension.html and also several related pages that you can find by clicking the links from that one.

So far I've managed to get a simple extension properly included in my chain spec. I've defined my extension like this:

#[derive(Clone, Debug, Deserialize, Serialize, sc_chain_spec::ChainSpecExtension)]
/// A chain spec extension for a chain that starts with no real consensus rules (manual seal) but later adds a PoW requirement.
pub struct EventualPowExtension {
    /// The block at which we start enforcing a PoW is present
    pow_start: u64,
    /// the difficulty threshold of the PoW. Only relevant after block pow_start.
    difficulty: u64,

This code compiles, I can see the new fields included in my chain spec json when I save it using node-template build-spec --chain local > my-example-spec.json, and I can even extract the typed data to pass to my consensus layer using code like this:

use core::any::{Any, TypeId};
use crate::chain_spec::MyExtension;

let type_id = TypeId::of::<MyExtension>();
let extracted_spec = config
    .expect("Should be my extension type");

println!("Extracted Spec is: {:?}", extracted_spec);

Now my next goal, and where I am stuck is to use the forks feature. I see that the ChainSpecExtension trait has a forks method. I would like to use this feature to allow adjusting the PoW difficulty as the chain progresses. (I know this represents a hard fork. I know this could be done forklessly using a runtime api. This is for learning purposes only.)

In such a scenario, I would like to have a BTreeMap<BlockNumber, Option<Difficulty>> Where None represents no real consensus (aka manual seal) and Some(difficulty) represents a PoW threshold. This allows adjusting the PoW threshold via hard fork as the chain evolves.

What I can't figure out is how to actually get my chain spec extension to use this Forks feature. Is there any example of this? Or could anyone point me in the right direction?



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.