subxt_core/config/
mod.rs

1// Copyright 2019-2024 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
11mod default_extrinsic_params;
12mod extrinsic_params;
13
14pub mod polkadot;
15pub mod substrate;
16pub mod transaction_extensions;
17
18use codec::{Decode, Encode};
19use core::fmt::Debug;
20use scale_decode::DecodeAsType;
21use scale_encode::EncodeAsType;
22use serde::{Serialize, de::DeserializeOwned};
23use subxt_metadata::Metadata;
24
25pub use default_extrinsic_params::{DefaultExtrinsicParams, DefaultExtrinsicParamsBuilder};
26pub use extrinsic_params::{ExtrinsicParams, ExtrinsicParamsEncoder};
27pub use polkadot::{PolkadotConfig, PolkadotExtrinsicParams, PolkadotExtrinsicParamsBuilder};
28pub use substrate::{SubstrateConfig, SubstrateExtrinsicParams, SubstrateExtrinsicParamsBuilder};
29pub use transaction_extensions::TransactionExtension;
30
31/// Runtime types.
32// Note: the `Send + Sync + 'static` bound isn't strictly required, but currently deriving
33// TypeInfo automatically applies a 'static bound to all generic types (including this one),
34// And we want the compiler to infer `Send` and `Sync` OK for things which have `T: Config`
35// rather than having to `unsafe impl` them ourselves.
36pub trait Config: Sized + Send + Sync + 'static {
37    /// The account ID type.
38    type AccountId: Debug + Clone + Encode + Decode + Serialize + Send;
39
40    /// The address type.
41    type Address: Debug + Encode + From<Self::AccountId>;
42
43    /// The signature type.
44    type Signature: Debug + Clone + Encode + Decode + Send;
45
46    /// The hashing system (algorithm) being used in the runtime (e.g. Blake2).
47    type Hasher: Debug + Clone + Copy + Hasher + Send + Sync;
48
49    /// The block header.
50    type Header: Debug + Header<Hasher = Self::Hasher> + Sync + Send + DeserializeOwned;
51
52    /// This type defines the extrinsic extra and additional parameters.
53    type ExtrinsicParams: ExtrinsicParams<Self>;
54
55    /// This is used to identify an asset in the `ChargeAssetTxPayment` signed extension.
56    type AssetId: Debug + Clone + Encode + DecodeAsType + EncodeAsType + Send;
57}
58
59/// Given some [`Config`], this returns the type of hash used.
60pub type HashFor<T> = <<T as Config>::Hasher as Hasher>::Output;
61
62/// given some [`Config`], this return the other params needed for its `ExtrinsicParams`.
63pub type ParamsFor<T> = <<T as Config>::ExtrinsicParams as ExtrinsicParams<T>>::Params;
64
65/// Block hashes must conform to a bunch of things to be used in Subxt.
66pub trait Hash:
67    Debug
68    + Copy
69    + Send
70    + Sync
71    + Decode
72    + AsRef<[u8]>
73    + Serialize
74    + DeserializeOwned
75    + Encode
76    + PartialEq
77    + Eq
78    + core::hash::Hash
79{
80}
81impl<T> Hash for T where
82    T: Debug
83        + Copy
84        + Send
85        + Sync
86        + Decode
87        + AsRef<[u8]>
88        + Serialize
89        + DeserializeOwned
90        + Encode
91        + PartialEq
92        + Eq
93        + core::hash::Hash
94{
95}
96
97/// This represents the hasher used by a node to hash things like block headers
98/// and extrinsics.
99pub trait Hasher {
100    /// The type given back from the hash operation
101    type Output: Hash;
102
103    /// Construct a new hasher.
104    fn new(metadata: &Metadata) -> Self;
105
106    /// Hash some bytes to the given output type.
107    fn hash(&self, s: &[u8]) -> Self::Output;
108
109    /// Hash some SCALE encodable type to the given output type.
110    fn hash_of<S: Encode>(&self, s: &S) -> Self::Output {
111        let out = s.encode();
112        self.hash(&out)
113    }
114}
115
116/// This represents the block header type used by a node.
117pub trait Header: Sized + Encode + Decode {
118    /// The block number type for this header.
119    type Number: Into<u64>;
120    /// The hasher used to hash this header.
121    type Hasher: Hasher;
122
123    /// Return the block number of this header.
124    fn number(&self) -> Self::Number;
125
126    /// Hash this header.
127    fn hash_with(&self, hasher: Self::Hasher) -> <Self::Hasher as Hasher>::Output {
128        hasher.hash_of(self)
129    }
130}