frame_support/traits/
dynamic_params.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Types and traits for dynamic parameters.
19//!
20//! Can be used by 3rd party macros to define dynamic parameters that are compatible with the the
21//! `parameters` pallet.
22
23use codec::MaxEncodedLen;
24use frame_support::Parameter;
25
26/// A dynamic parameter store across an aggregated KV type.
27pub trait RuntimeParameterStore {
28	type AggregatedKeyValue: AggregatedKeyValue;
29
30	/// Get the value of a parametrized key.
31	///
32	/// Should return `None` if no explicit value was set instead of a default.
33	fn get<KV, K>(key: K) -> Option<K::Value>
34	where
35		KV: AggregatedKeyValue,
36		K: Key + Into<<KV as AggregatedKeyValue>::Key>,
37		<KV as AggregatedKeyValue>::Key: IntoKey<
38			<<Self as RuntimeParameterStore>::AggregatedKeyValue as AggregatedKeyValue>::Key,
39		>,
40		<<Self as RuntimeParameterStore>::AggregatedKeyValue as AggregatedKeyValue>::Value:
41			TryIntoKey<<KV as AggregatedKeyValue>::Value>,
42		<KV as AggregatedKeyValue>::Value: TryInto<K::WrappedValue>;
43}
44
45/// A dynamic parameter store across a concrete KV type.
46pub trait ParameterStore<KV: AggregatedKeyValue> {
47	/// Get the value of a parametrized key.
48	fn get<K>(key: K) -> Option<K::Value>
49	where
50		K: Key + Into<<KV as AggregatedKeyValue>::Key>,
51		<KV as AggregatedKeyValue>::Value: TryInto<K::WrappedValue>;
52}
53
54/// Key of a dynamic parameter.
55pub trait Key {
56	/// The value that the key is parametrized with.
57	type Value;
58
59	/// An opaque representation of `Self::Value`.
60	type WrappedValue: Into<Self::Value>;
61}
62
63/// The aggregated key-value type of a dynamic parameter store.
64pub trait AggregatedKeyValue: Parameter {
65	/// The aggregated key type.
66	type Key: Parameter + MaxEncodedLen;
67
68	/// The aggregated value type.
69	type Value: Parameter + MaxEncodedLen;
70
71	/// Split the aggregated key-value type into its parts.
72	fn into_parts(self) -> (Self::Key, Option<Self::Value>);
73}
74
75impl AggregatedKeyValue for () {
76	type Key = ();
77	type Value = ();
78
79	fn into_parts(self) -> (Self::Key, Option<Self::Value>) {
80		((), None)
81	}
82}
83
84/// Allows to create a `ParameterStore` from a `RuntimeParameterStore`.
85///
86/// This concretization is useful when configuring pallets, since a pallet will require a parameter
87/// store for its own KV type and not the aggregated runtime-wide KV type.
88pub struct ParameterStoreAdapter<PS, KV>(core::marker::PhantomData<(PS, KV)>);
89
90impl<PS, KV> ParameterStore<KV> for ParameterStoreAdapter<PS, KV>
91where
92	PS: RuntimeParameterStore,
93	KV: AggregatedKeyValue,
94	<KV as AggregatedKeyValue>::Key:
95		IntoKey<<<PS as RuntimeParameterStore>::AggregatedKeyValue as AggregatedKeyValue>::Key>,
96	<KV as AggregatedKeyValue>::Value: TryFromKey<
97		<<PS as RuntimeParameterStore>::AggregatedKeyValue as AggregatedKeyValue>::Value,
98	>,
99{
100	fn get<K>(key: K) -> Option<K::Value>
101	where
102		K: Key + Into<<KV as AggregatedKeyValue>::Key>,
103		<KV as AggregatedKeyValue>::Value: TryInto<K::WrappedValue>,
104	{
105		PS::get::<KV, K>(key)
106	}
107}
108
109// workaround for rust bug https://github.com/rust-lang/rust/issues/51445
110mod workaround {
111	pub trait FromKey<T>: Sized {
112		#[must_use]
113		fn from_key(value: T) -> Self;
114	}
115
116	pub trait IntoKey<T>: Sized {
117		#[must_use]
118		fn into_key(self) -> T;
119	}
120
121	impl<T, U> IntoKey<U> for T
122	where
123		U: FromKey<T>,
124	{
125		fn into_key(self) -> U {
126			U::from_key(self)
127		}
128	}
129
130	pub trait TryIntoKey<T>: Sized {
131		type Error;
132
133		fn try_into_key(self) -> Result<T, Self::Error>;
134	}
135
136	pub trait TryFromKey<T>: Sized {
137		type Error;
138
139		fn try_from_key(value: T) -> Result<Self, Self::Error>;
140	}
141
142	impl<T, U> TryIntoKey<U> for T
143	where
144		U: TryFromKey<T>,
145	{
146		type Error = U::Error;
147
148		fn try_into_key(self) -> Result<U, U::Error> {
149			U::try_from_key(self)
150		}
151	}
152}
153pub use workaround::*;