3

I am trying to understand the internal logic of adding development keys to the local keystore in Substrate for development purposes. For example I can see that the session keys for Alice are being generated and added to the genesis config in /bin/node/cli/src/chain_spec.rs file as follows:

pub fn development_config() -> Result<ChainSpec, String> {
    let wasm_binary =
        WASM_BINARY.ok_or_else(|| "Development wasm binary not available".to_string())?;

    Ok(ChainSpec::from_genesis(
        // Name
        "Development",
        // ID
        "dev",
        ChainType::Development,
        move || {
            testnet_genesis(
                wasm_binary,
                // Initial PoA authorities
                vec![authority_keys_from_seed("Alice")],
                // Sudo account
                get_account_id_from_seed::<sr25519::Public>("Alice"),
                // Pre-funded accounts
                vec![
                    get_account_id_from_seed::<sr25519::Public>("Alice"),
                    get_account_id_from_seed::<sr25519::Public>("Bob"),
                    get_account_id_from_seed::<sr25519::Public>("Alice//stash"),
                    get_account_id_from_seed::<sr25519::Public>("Bob//stash"),
                ],
                true,
            )
        },
        // Bootnodes
        vec![],
        // Telemetry
        None,
        // Protocol ID
        None,
        // Properties
        None,
        // Extensions
        None,
    ))
}

From my understanding the session keys for development also includes ImOnlineId. My question is how exactly are the keys added to the local keystore so that I am able to access my keys as such in the im-online pallet:

    fn local_authority_keys() -> impl Iterator<Item=(u32, T::AuthorityId)> {
        // on-chain storage
        //
        // At index `idx`:
        // 1. A (ImOnline) public key to be used by a validator at index `idx` to send im-online
        //          heartbeats.
        let authorities = Keys::<T>::get();

        // local keystore
        //
        // All `ImOnline` public (+private) keys currently in the local keystore.
        let mut local_keys = T::AuthorityId::all();

        local_keys.sort();

        authorities.into_iter()
            .enumerate()
            .filter_map(move |(index, authority)| {
                local_keys.binary_search(&authority)
                    .ok()
                    .map(|location| (index as u32, local_keys[location].clone()))
            })
    }

While debugging I can find the public key for Alice in local_keys. Asking because I want to develop something similar and testing in development is made easier this way rather than manually putting keys into the keystore.

Jdawg287
  • 117
  • 5

1 Answers1

1
  1. Define a genesis config builder in your pallet. E.g https://github.com/paritytech/substrate/blob/master/frame/im-online/src/lib.rs#L359-L376
  2. Enable the genesis config by adding an Config/Config<T> while construct the runtime. E.g https://github.com/paritytech/substrate/blob/master/bin/node/runtime/src/lib.rs#L1229
  3. Set the genesis at chain_spec. E.g https://github.com/paritytech/substrate/blob/master/bin/node/cli/src/chain_spec.rs#L349

Note: But in this example, these keys are managed by pallet-session. https://github.com/paritytech/substrate/blob/master/bin/node/cli/src/chain_spec.rs#L307-L316.

AurevoirXavier
  • 2,543
  • 2
  • 17
  • 25
  • I did exactly in the examples above for the `chain_spec` and also added the genesis config while constructing runtime. My pallet is using the old format so I use the `add_extra_genesis` macro in the `decl_storage!` to initialize keys. But I am unable to see `local_keys` when I try to retrieve them like `let mut local_keys = T::AuthorityId::all();` – Jdawg287 Sep 10 '21 at 06:40
  • My logs show the following: `2021-09-10 06:41:18.413 INFO ThreadId(24) pallet_customer: authority key from runtime storage: Public(d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d (5GrwvaEF...)) ` `2021-09-10 06:41:18.413 INFO ThreadId(24) pallet_customer: Local authority keys from keystore: []` – Jdawg287 Sep 10 '21 at 06:45