简体   繁体   中英

Accessing chain id and genesis hash from the Substrate runtime during extrinsic execution

I am using Substrate's latest stable pre-v2.0-3e65111 I wanted to access the chain id or genesis hash from the Substrate runtime while processing an extrinsic (I am accepting signed payloads and trying to ensure the payloads were targeted to the right chain). Is it possible to access them? (I know I can do that with polkadot-js) I see trait CheckGenesis but don't see how I can use it to genesis hash? ( new results in empty vector).

You can find the implementation for CheckGenesis here: https://github.com/paritytech/substrate/blob/master/frame/system/src/lib.rs#L1417

impl<T: Trait + Send + Sync> SignedExtension for CheckGenesis<T> {
    type AccountId = T::AccountId;
    type Call = <T as Trait>::Call;
    type AdditionalSigned = T::Hash;
    type DispatchInfo = DispatchInfo;
    type Pre = ();
    const IDENTIFIER: &'static str = "CheckGenesis";

    fn additional_signed(&self) -> Result<Self::AdditionalSigned, TransactionValidityError> {
        Ok(<Module<T>>::block_hash(T::BlockNumber::zero()))
    }
}

From this, you should be able to see that you can access the genesis hash by calling:

frame_system::Module::<T>::block_hash(T::BlockNumber::zero());

Let me know if this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM