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