gear_subxt/config/
mod.rs

1// Copyright 2019-2023 Parity Technologies (UK) Ltd.
2// This file is dual-licensed as Apache-2.0 or GPL-3.0.
3// see LICENSE for license details.
4
5//! This module provides a [`Config`] type, which is used to define various
6//! types that are important in order to speak to a particular chain.
7//! [`SubstrateConfig`] provides a default set of these types suitable for the
8//! default Substrate node implementation, and [`PolkadotConfig`] for a
9//! Polkadot node.
10
11pub mod extrinsic_params;
12pub mod polkadot;
13pub mod substrate;
14
15use codec::{Decode, Encode};
16use core::fmt::Debug;
17use serde::{de::DeserializeOwned, Serialize};
18
19pub use extrinsic_params::ExtrinsicParams;
20pub use polkadot::PolkadotConfig;
21pub use substrate::SubstrateConfig;
22
23/// Runtime types.
24// Note: the 'static bound isn't strictly required, but currently deriving TypeInfo
25// automatically applies a 'static bound to all generic types (including this one),
26// and so until that is resolved, we'll keep the (easy to satisfy) constraint here.
27pub trait Config: 'static {
28    /// Account index (aka nonce) type. This stores the number of previous
29    /// transactions associated with a sender account.
30    type Index: Debug + Copy + Decode + Into<u64>;
31
32    /// The output of the `Hasher` function.
33    type Hash: Debug
34        + Copy
35        + Send
36        + Sync
37        + Decode
38        + AsRef<[u8]>
39        + Serialize
40        + DeserializeOwned
41        + Encode
42        + PartialEq;
43
44    /// The account ID type.
45    type AccountId: Debug + Clone + Encode;
46
47    /// The address type.
48    type Address: Debug + Encode + From<Self::AccountId>;
49
50    /// The signature type.
51    type Signature: Debug + Encode;
52
53    /// The hashing system (algorithm) being used in the runtime (e.g. Blake2).
54    type Hasher: Debug + Hasher<Output = Self::Hash>;
55
56    /// The block header.
57    type Header: Debug + Header<Hasher = Self::Hasher> + Sync + Send + DeserializeOwned;
58
59    /// This type defines the extrinsic extra and additional parameters.
60    type ExtrinsicParams: extrinsic_params::ExtrinsicParams<Self::Index, Self::Hash>;
61}
62
63/// This represents the hasher used by a node to hash things like block headers
64/// and extrinsics.
65pub trait Hasher {
66    /// The type given back from the hash operation
67    type Output;
68
69    /// Hash some bytes to the given output type.
70    fn hash(s: &[u8]) -> Self::Output;
71
72    /// Hash some SCALE encodable type to the given output type.
73    fn hash_of<S: Encode>(s: &S) -> Self::Output {
74        let out = s.encode();
75        Self::hash(&out)
76    }
77}
78
79/// This represents the block header type used by a node.
80pub trait Header: Sized + Encode {
81    /// The block number type for this header.
82    type Number: Into<u64>;
83    /// The hasher used to hash this header.
84    type Hasher: Hasher;
85
86    /// Return the block number of this header.
87    fn number(&self) -> Self::Number;
88
89    /// Hash this header.
90    fn hash(&self) -> <Self::Hasher as Hasher>::Output {
91        Self::Hasher::hash_of(self)
92    }
93}
94
95/// Take a type implementing [`Config`] (eg [`SubstrateConfig`]), and some type which describes the
96/// additional and extra parameters to pass to an extrinsic (see [`ExtrinsicParams`]),
97/// and returns a type implementing [`Config`] with those new [`ExtrinsicParams`].
98pub struct WithExtrinsicParams<T: Config, E: extrinsic_params::ExtrinsicParams<T::Index, T::Hash>> {
99    _marker: std::marker::PhantomData<(T, E)>,
100}
101
102impl<T: Config, E: extrinsic_params::ExtrinsicParams<T::Index, T::Hash>> Config
103    for WithExtrinsicParams<T, E>
104{
105    type Index = T::Index;
106    type Hash = T::Hash;
107    type AccountId = T::AccountId;
108    type Address = T::Address;
109    type Signature = T::Signature;
110    type Hasher = T::Hasher;
111    type Header = T::Header;
112    type ExtrinsicParams = E;
113}
114
115/// implement subxt's Hasher and Header traits for some substrate structs
116#[cfg(feature = "substrate-compat")]
117mod substrate_impls {
118    use super::*;
119    use primitive_types::{H256, U256};
120
121    impl<N, H> Header for sp_runtime::generic::Header<N, H>
122    where
123        Self: Encode,
124        N: Copy + Into<U256> + Into<u64> + TryFrom<U256>,
125        H: sp_runtime::traits::Hash + Hasher,
126    {
127        type Number = N;
128        type Hasher = H;
129
130        fn number(&self) -> Self::Number {
131            self.number
132        }
133    }
134
135    impl Hasher for sp_core::Blake2Hasher {
136        type Output = H256;
137
138        fn hash(s: &[u8]) -> Self::Output {
139            <Self as sp_core::Hasher>::hash(s)
140        }
141    }
142
143    impl Hasher for sp_core::KeccakHasher {
144        type Output = H256;
145
146        fn hash(s: &[u8]) -> Self::Output {
147            <Self as sp_core::Hasher>::hash(s)
148        }
149    }
150}