Add proper test Custom values (#1147)

* add proper tests for custom values

* remove try operators

* use sustrate compat for import of hash

* add license and hex

* add script to artifacts.sh

* custom values with ids not in metadata can be accessed in static interface

* fmt and clippy

* access bytes of custom values directly, even if type id wrong

* final fixes

* removing substrate-compat flag from ui tests

* Update subxt/src/custom_values/custom_values_client.rs

Co-authored-by: James Wilson <james@jsdw.me>

* remove types access in type generator

* 2 extra lines

---------

Co-authored-by: James Wilson <james@jsdw.me>
This commit is contained in:
Tadeo Hepperle
2023-09-12 15:46:12 +02:00
committed by GitHub
parent 022d709d02
commit c8462defab
22 changed files with 208 additions and 44 deletions
@@ -10,6 +10,9 @@ use crate::metadata::DecodeWithMetadata;
pub trait CustomValueAddress {
/// The type of the custom value.
type Target: DecodeWithMetadata;
/// Should be set to `Yes` for Dynamic values and static values that have a valid type.
/// Should be `()` for custom values, that have an invalid type id.
type IsDecodable;
/// the name (key) by which the custom value can be accessed in the metadata.
fn name(&self) -> &str;
@@ -22,24 +25,28 @@ pub trait CustomValueAddress {
impl CustomValueAddress for str {
type Target = DecodedValueThunk;
type IsDecodable = Yes;
fn name(&self) -> &str {
self
}
}
/// Used to signal whether a [`CustomValueAddress`] can be decoded.
pub struct Yes;
/// A static address to a custom value.
pub struct StaticAddress<R> {
pub struct StaticAddress<ReturnTy, IsDecodable> {
name: &'static str,
hash: Option<[u8; 32]>,
phantom: PhantomData<R>,
phantom: PhantomData<(ReturnTy, IsDecodable)>,
}
impl<R> StaticAddress<R> {
impl<ReturnTy, IsDecodable> StaticAddress<ReturnTy, IsDecodable> {
#[doc(hidden)]
/// Creates a new StaticAddress.
pub fn new_static(name: &'static str, hash: [u8; 32]) -> Self {
StaticAddress {
pub fn new_static(name: &'static str, hash: [u8; 32]) -> StaticAddress<ReturnTy, IsDecodable> {
StaticAddress::<ReturnTy, IsDecodable> {
name,
hash: Some(hash),
phantom: PhantomData,
@@ -56,8 +63,9 @@ impl<R> StaticAddress<R> {
}
}
impl<R: DecodeWithMetadata> CustomValueAddress for StaticAddress<R> {
impl<R: DecodeWithMetadata, Y> CustomValueAddress for StaticAddress<R, Y> {
type Target = R;
type IsDecodable = Y;
fn name(&self) -> &str {
self.name
@@ -1,5 +1,5 @@
use crate::client::OfflineClientT;
use crate::custom_values::custom_value_address::CustomValueAddress;
use crate::custom_values::custom_value_address::{CustomValueAddress, Yes};
use crate::error::MetadataError;
use crate::metadata::DecodeWithMetadata;
use crate::{Config, Error};
@@ -26,7 +26,7 @@ impl<T, Client> CustomValuesClient<T, Client> {
impl<T: Config, Client: OfflineClientT<T>> CustomValuesClient<T, Client> {
/// Access a custom value by the address it is registered under. This can be just a [str] to get back a dynamic value,
/// or a static address from the generated static interface to get a value of a static type returned.
pub fn at<Address: CustomValueAddress + ?Sized>(
pub fn at<Address: CustomValueAddress<IsDecodable = Yes> + ?Sized>(
&self,
address: &Address,
) -> Result<Address::Target, Error> {
@@ -48,6 +48,24 @@ impl<T: Config, Client: OfflineClientT<T>> CustomValuesClient<T, Client> {
Ok(value)
}
/// Access the bytes of a custom value by the address it is registered under.
pub fn bytes_at<Address: CustomValueAddress + ?Sized>(
&self,
address: &Address,
) -> Result<Vec<u8>, Error> {
// 1. Validate custom value shape if hash given:
self.validate(address)?;
// 2. Return the underlying bytes:
let metadata = self.client.metadata();
let custom = metadata.custom();
let custom_value = custom
.get(address.name())
.ok_or_else(|| MetadataError::CustomValueNameNotFound(address.name().to_string()))?;
Ok(custom_value.bytes().to_vec())
}
/// Run the validation logic against some custom value address you'd like to access. Returns `Ok(())`
/// if the address is valid (or if it's not possible to check since the address has no validation hash).
/// Returns an error if the address was not valid (wrong name, type or raw bytes)
+1 -1
View File
@@ -7,5 +7,5 @@
mod custom_value_address;
mod custom_values_client;
pub use custom_value_address::{CustomValueAddress, StaticAddress};
pub use custom_value_address::{CustomValueAddress, StaticAddress, Yes};
pub use custom_values_client::CustomValuesClient;