【问题标题】:How are the development keys (Alice) added to the local keystore from `chain_specs.rs`?如何将开发密钥(Alice)从 `chain_specs.rs` 添加到本地密钥库?
【发布时间】:2021-09-10 05:59:47
【问题描述】:

我试图了解将开发密钥添加到 Substrate 中的本地密钥库以用于开发目的的内部逻辑。例如,我可以看到 Alicesession keys 正在生成并添加到 /bin/node/cli/src/chain_spec.rs 文件中的 genesis 配置中,如下所示:

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,
    ))
}

据我了解,用于开发的session keys 还包括ImOnlineId。我的问题是如何将密钥添加到本地密钥库,以便我能够在im-online 托盘中访问我的密钥:

    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()))
            })
    }

在调试时,我可以在 local_keys 中找到 Alice 的公钥。 询问是因为我想开发类似的东西,并且通过这种方式在开发中进行测试变得更容易,而不是手动将密钥放入密钥库。

【问题讨论】:

    标签: blockchain substrate polkadot


    【解决方案1】:
    1. 在您的托盘中定义一个 Genesis 配置构建器。例如https://github.com/paritytech/substrate/blob/master/frame/im-online/src/lib.rs#L359-L376
    2. 在构建运行时时添加Config/Config&lt;T&gt; 来启用创世配置。例如https://github.com/paritytech/substrate/blob/master/bin/node/runtime/src/lib.rs#L1229
    3. 将起源设置为chain_spec。例如https://github.com/paritytech/substrate/blob/master/bin/node/cli/src/chain_spec.rs#L349

    注意:但在本例中,这些键是由托盘会话管理的。 https://github.com/paritytech/substrate/blob/master/bin/node/cli/src/chain_spec.rs#L307-L316.

    【讨论】:

    • 我在上面的示例中为chain_spec 所做的完全一样,并且还在构建运行时时添加了创世配置。我的托盘使用旧格式,所以我使用decl_storage! 中的add_extra_genesis 宏来初始化键。但是当我尝试像let mut local_keys = T::AuthorityId::all(); 一样检索它们时,我看不到local_keys
    • 我的日志显示如下: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: []
    猜你喜欢
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-10
    相关资源
    最近更新 更多