WIP New storage APIs roughly completed, lots of errors still

This commit is contained in:
James Wilson
2025-09-30 12:38:53 +01:00
parent b720b59d95
commit fc793a8b79
22 changed files with 717 additions and 169 deletions
+1 -1
View File
@@ -23,7 +23,7 @@ pub use crate::tx::payload::dynamic as tx;
pub use crate::constants::address::dynamic as constant;
// Lookup storage values dynamically.
// pub use crate::storage::address::dynamic as storage; // TODO re-add.
pub use crate::storage::address::dynamic as storage;
// Execute runtime API function call dynamically.
pub use crate::runtime_api::payload::dynamic as runtime_api_call;
+19 -10
View File
@@ -7,7 +7,7 @@
use alloc::borrow::Cow;
use frame_decode::storage::IntoEncodableValues;
use scale_decode::DecodeAsType;
use crate::utils::Maybe;
use crate::utils::{Maybe, YesNoMaybe};
/// A storage address. This allows access to a given storage entry, which can then
/// be iterated over or fetched from by providing the relevant set of keys, or
@@ -22,7 +22,7 @@ pub trait Address {
/// Does the address have a default value defined for it.
/// Set to [`crate::utils::Yes`] to enable APIs which require one,
/// or [`crate::utils::Maybe`] to enable APIs which allow one
type HasDefaultValue;
type HasDefaultValue: YesNoMaybe;
/// Does the address point to a map (as opposed to a plain value)?
/// Set to [`crate::utils::Yes`] to enable APIs which require a map,
/// or [`crate::utils::Maybe`] to enable APIs which allow a map.
@@ -63,6 +63,19 @@ impl<KeyParts, Value, HasDefaultValue, IsMap> StaticAddress<KeyParts, Value, Has
}
}
/// Create a new address.
pub fn new(
pallet_name: impl Into<Cow<'static, str>>,
entry_name: impl Into<Cow<'static, str>>,
) -> Self {
Self {
pallet_name: pallet_name.into(),
entry_name: entry_name.into(),
validation_hash: None,
marker: core::marker::PhantomData
}
}
/// Do not validate this storage entry prior to accessing it.
pub fn unvalidated(mut self) -> Self {
self.validation_hash = None;
@@ -75,6 +88,7 @@ impl<KeyParts, Value, HasDefaultValue, IsMap> Address
where
KeyParts: IntoEncodableValues,
Value: DecodeAsType,
HasDefaultValue: YesNoMaybe,
{
type KeyParts = KeyParts;
type Value = Value;
@@ -95,8 +109,8 @@ where
}
/// A dynamic address is simply a [`StaticAddress`] which asserts that the
/// entry could be a map and could have a default value.
pub type DynamicAddress<KeyParts, Value> = StaticAddress<KeyParts, Value, Maybe, Maybe>;
/// entry *might* be a map and *might* have a default value.
pub type DynamicAddress<KeyParts = Vec<scale_value::Value>, Value = scale_value::Value> = StaticAddress<KeyParts, Value, Maybe, Maybe>;
/// Construct a new dynamic storage address. You can define the type of the
/// storage keys and value yourself here, but have no guarantee that they will
@@ -105,11 +119,6 @@ pub fn dynamic<KeyParts: IntoEncodableValues, Value: DecodeAsType>(
pallet_name: impl Into<Cow<'static, str>>,
entry_name: impl Into<Cow<'static, str>>,
) -> DynamicAddress<KeyParts, Value> {
DynamicAddress::<KeyParts, Value> {
pallet_name: pallet_name.into(),
entry_name: entry_name.into(),
validation_hash: None,
marker: core::marker::PhantomData
}
DynamicAddress::<KeyParts, Value>::new(pallet_name, entry_name)
}
+2 -7
View File
@@ -13,6 +13,7 @@ mod multi_signature;
mod static_type;
mod unchecked_extrinsic;
mod wrapper_opaque;
mod yesnomaybe;
use alloc::borrow::ToOwned;
use alloc::format;
@@ -21,6 +22,7 @@ use alloc::vec::Vec;
use codec::{Compact, Decode, Encode};
use derive_where::derive_where;
pub use yesnomaybe::{Yes, No, Maybe, YesNoMaybe};
pub use account_id::AccountId32;
pub use account_id20::AccountId20;
pub use era::Era;
@@ -73,13 +75,6 @@ unsafe impl<T> Sync for PhantomDataSendSync<T> {}
/// as `BTreeMap` which allows us to easily swap the two during codegen.
pub type KeyedVec<K, V> = Vec<(K, V)>;
/// A unit marker enum.
pub enum Yes {}
/// A unit marker enum.
pub enum Maybe {}
/// A unit marker enum.
pub enum No {}
/// A quick helper to encode some bytes to hex.
pub fn to_hex(bytes: impl AsRef<[u8]>) -> String {
format!("0x{}", hex::encode(bytes.as_ref()))
+31
View File
@@ -0,0 +1,31 @@
// Copyright 2019-2025 Parity Technologies (UK) Ltd.
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.
/// A unit marker enum.
pub enum Yes {}
/// A unit marker enum.
pub enum Maybe {}
/// A unit marker enum.
pub enum No {}
/// This is implemented for [`Yes`], [`No`] and [`Maybe`] and
/// allows us to check at runtime which of these types is present.
pub trait YesNoMaybe {
/// [`Yes`]
fn is_yes() -> bool { false }
/// [`No`]
fn is_no() -> bool { false }
/// [`Maybe`]
fn is_maybe() -> bool { false }
}
impl YesNoMaybe for Yes {
fn is_yes() -> bool { true }
}
impl YesNoMaybe for No {
fn is_no() -> bool { true }
}
impl YesNoMaybe for Maybe {
fn is_maybe() -> bool { true }
}