Introduce Metadata type (#974)

* WIP new Metadata type

* Finish basic Metadata impl inc hashing and validation

* remove caching from metadata; can add that higher up

* remove caches

* update retain to use Metadata

* clippy fixes

* update codegen to use Metadata

* clippy

* WIP fixing subxt lib

* WIP fixing tests, rebuild artifacts, fix OrderedMap::retain

* get --all-targets compiling

* move DispatchError type lookup back to being optional

* cargo clippy

* fix docs

* re-use VariantIndex to get variants

* add docs and enforce docs on metadata crate

* fix docs

* add test and fix docs

* cargo fmt

* address review comments

* update lockfiles

* ExactSizeIter so we can ask for len() of things (and hopefully soon is_empty()
This commit is contained in:
James Wilson
2023-05-25 10:35:21 +01:00
committed by GitHub
parent f344d0dd4d
commit b9f5419095
64 changed files with 6818 additions and 5719 deletions
+8
View File
@@ -0,0 +1,8 @@
// Copyright 2019-2023 Parity Technologies (UK) Ltd.
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.
pub mod ordered_map;
pub mod retain;
pub mod validation;
pub mod variant_index;
+137
View File
@@ -0,0 +1,137 @@
// Copyright 2019-2023 Parity Technologies (UK) Ltd.
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.
use std::collections::HashMap;
/// A minimal ordered map to let one search for
/// things by key or get the values in insert order.
#[derive(Debug, Clone)]
pub struct OrderedMap<K, V> {
values: Vec<V>,
map: HashMap<K, usize>,
}
impl<K, V> Default for OrderedMap<K, V> {
fn default() -> Self {
Self {
values: Default::default(),
map: Default::default(),
}
}
}
impl<K, V> OrderedMap<K, V>
where
K: PartialEq + Eq + std::hash::Hash,
{
/// Create a new, empty [`OrderedMap`].
pub fn new() -> Self {
Self::default()
}
/// Number of entries in the map.
#[allow(dead_code)]
pub fn len(&self) -> usize {
self.values.len()
}
/// Is the map empty.
#[allow(dead_code)]
pub fn is_empty(&self) -> bool {
self.values.is_empty()
}
/// Retain specific entries.
pub fn retain<F>(&mut self, mut f: F)
where
F: FnMut(&V) -> bool,
{
let values = std::mem::take(&mut self.values);
let map = std::mem::take(&mut self.map);
// Filter the values, storing a map from old to new positions:
let mut new_values = Vec::new();
let mut old_pos_to_new_pos = HashMap::new();
for (pos, value) in values.into_iter().enumerate().filter(|(_, v)| f(v)) {
old_pos_to_new_pos.insert(pos, new_values.len());
new_values.push(value);
}
// Update the values now we've filtered them:
self.values = new_values;
// Rebuild the map using the new positions:
self.map = map
.into_iter()
.filter_map(|(k, v)| old_pos_to_new_pos.get(&v).map(|v2| (k, *v2)))
.collect();
}
/// Push/insert an item to the end of the map.
pub fn push_insert(&mut self, key: K, value: V) {
let idx = self.values.len();
self.values.push(value);
self.map.insert(key, idx);
}
/// Get an item by its key.
pub fn get_by_key<Q>(&self, key: &Q) -> Option<&V>
where
K: std::borrow::Borrow<Q>,
Q: std::hash::Hash + Eq + ?Sized,
{
self.map.get(key).and_then(|&v| self.values.get(v))
}
/// Get an item by its index.
pub fn get_by_index(&self, i: usize) -> Option<&V> {
self.values.get(i)
}
/// Access the underlying values.
pub fn values(&self) -> &[V] {
&self.values
}
/// Mutable access to the underlying values.
pub fn values_mut(&mut self) -> &mut [V] {
&mut self.values
}
/// Return the underlying values.
pub fn into_values(self) -> Vec<V> {
self.values
}
}
impl<K, V> FromIterator<(K, V)> for OrderedMap<K, V>
where
K: PartialEq + Eq + std::hash::Hash,
{
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
let mut map = OrderedMap::new();
for (k, v) in iter {
map.push_insert(k, v)
}
map
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn retain() {
let mut m = OrderedMap::from_iter([(1, 'a'), (2, 'b'), (3, 'c')]);
m.retain(|v| *v != 'b');
assert_eq!(m.get_by_key(&1), Some(&'a'));
assert_eq!(m.get_by_key(&2), None);
assert_eq!(m.get_by_key(&3), Some(&'c'));
assert_eq!(m.values(), &['a', 'c'])
}
}
+318
View File
@@ -0,0 +1,318 @@
// Copyright 2019-2023 Parity Technologies (UK) Ltd.
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.
//! Utility functions to generate a subset of the metadata.
use crate::{
ExtrinsicMetadata, Metadata, PalletMetadataInner, RuntimeApiMetadataInner, StorageEntryType,
};
use scale_info::TypeDef;
use std::collections::{BTreeMap, HashSet};
/// Collect all type IDs needed to represent the provided pallet.
fn collect_pallet_types(pallet: &PalletMetadataInner, type_ids: &mut HashSet<u32>) {
if let Some(storage) = &pallet.storage {
for entry in storage.entries() {
match entry.entry_type {
StorageEntryType::Plain(ty) => {
type_ids.insert(ty);
}
StorageEntryType::Map {
key_ty, value_ty, ..
} => {
type_ids.insert(key_ty);
type_ids.insert(value_ty);
}
}
}
}
if let Some(ty) = pallet.call_ty {
type_ids.insert(ty);
}
if let Some(ty) = pallet.event_ty {
type_ids.insert(ty);
}
for constant in pallet.constants.values() {
type_ids.insert(constant.ty);
}
if let Some(ty) = pallet.error_ty {
type_ids.insert(ty);
}
}
/// Update all type IDs of the provided pallet using the new type IDs from the portable registry.
fn update_pallet_types(pallet: &mut PalletMetadataInner, map_ids: &BTreeMap<u32, u32>) {
if let Some(storage) = &mut pallet.storage {
for entry in storage.entries.values_mut() {
match &mut entry.entry_type {
StorageEntryType::Plain(ty) => {
update_type(ty, map_ids);
}
StorageEntryType::Map {
key_ty, value_ty, ..
} => {
update_type(key_ty, map_ids);
update_type(value_ty, map_ids);
}
}
}
}
if let Some(ty) = &mut pallet.call_ty {
update_type(ty, map_ids);
}
if let Some(ty) = &mut pallet.event_ty {
update_type(ty, map_ids);
}
if let Some(ty) = &mut pallet.error_ty {
update_type(ty, map_ids);
}
for constant in pallet.constants.values_mut() {
update_type(&mut constant.ty, map_ids);
}
}
/// Collect all type IDs needed to represent the extrinsic metadata.
fn collect_extrinsic_types(extrinsic: &ExtrinsicMetadata, type_ids: &mut HashSet<u32>) {
type_ids.insert(extrinsic.ty);
for signed in &extrinsic.signed_extensions {
type_ids.insert(signed.extra_ty);
type_ids.insert(signed.additional_ty);
}
}
/// Update all type IDs of the provided extrinsic metadata using the new type IDs from the portable registry.
fn update_extrinsic_types(extrinsic: &mut ExtrinsicMetadata, map_ids: &BTreeMap<u32, u32>) {
update_type(&mut extrinsic.ty, map_ids);
for signed in &mut extrinsic.signed_extensions {
update_type(&mut signed.extra_ty, map_ids);
update_type(&mut signed.additional_ty, map_ids);
}
}
/// Collect all type IDs needed to represent the runtime APIs.
fn collect_runtime_api_types(api: &RuntimeApiMetadataInner, type_ids: &mut HashSet<u32>) {
for method in api.methods.values() {
for input in &method.inputs {
type_ids.insert(input.ty);
}
type_ids.insert(method.output_ty);
}
}
/// Update all type IDs of the provided runtime APIs metadata using the new type IDs from the portable registry.
fn update_runtime_api_types(apis: &mut [RuntimeApiMetadataInner], map_ids: &BTreeMap<u32, u32>) {
for api in apis {
for method in api.methods.values_mut() {
for input in &mut method.inputs {
update_type(&mut input.ty, map_ids);
}
update_type(&mut method.output_ty, map_ids);
}
}
}
/// Update the given type using the new type ID from the portable registry.
///
/// # Panics
///
/// Panics if the [`scale_info::PortableRegistry`] did not retain all needed types.
fn update_type(ty: &mut u32, map_ids: &BTreeMap<u32, u32>) {
let old_id = *ty;
let new_id = map_ids
.get(&old_id)
.copied()
.unwrap_or_else(|| panic!("PortableRegistry did not retain type id {old_id}. This is a bug. Please open an issue."));
*ty = new_id;
}
/// Strip any pallets out of the RuntimeCall type that aren't the ones we want to keep.
/// The RuntimeCall type is referenced in a bunch of places, so doing this prevents us from
/// holding on to stuff in pallets we've asked not to keep.
fn retain_pallets_in_runtime_call_type<F>(metadata: &mut Metadata, mut filter: F)
where
F: FnMut(&str) -> bool,
{
let extrinsic_ty = metadata
.types
.types
.get_mut(metadata.extrinsic.ty as usize)
.expect("Metadata should contain extrinsic type in registry");
let Some(call_ty) = extrinsic_ty.ty.type_params
.iter_mut()
.find(|ty| ty.name == "Call")
.and_then(|ty| ty.ty) else { return; };
let call_ty = metadata
.types
.types
.get_mut(call_ty.id as usize)
.expect("Metadata should contain Call type information");
let TypeDef::Variant(variant) = &mut call_ty.ty.type_def else {
panic!("Metadata Call type is expected to be a variant type");
};
// Remove all variants from the call type that aren't the pallet(s) we want to keep.
variant.variants.retain(|v| filter(&v.name));
}
/// Generate a subset of the metadata that contains only the
/// types needed to represent the provided pallets and runtime APIs.
///
/// # Note
///
/// Used to strip metadata of unneeded information and to reduce the
/// binary size.
///
/// # Panics
///
/// Panics if the [`scale_info::PortableRegistry`] did not retain all needed types,
/// or the metadata does not contain the "sp_runtime::DispatchError" type.
pub fn retain_metadata<F, G>(
metadata: &mut Metadata,
mut pallets_filter: F,
mut runtime_apis_filter: G,
) where
F: FnMut(&str) -> bool,
G: FnMut(&str) -> bool,
{
let mut type_ids = HashSet::new();
// There is a special RuntimeCall type which points to all pallets and call types by default.
// This brings in a significant chunk of types. We trim this down to only include variants
// for the pallets we're retaining, to avoid this.
retain_pallets_in_runtime_call_type(metadata, &mut pallets_filter);
// Filter our pallet list to only those pallets we want to keep. Keep hold of all
// type IDs in the pallets we're keeping. Retain all, if no filter specified.
metadata.pallets.retain(|pallet| {
let should_retain = pallets_filter(&pallet.name);
if should_retain {
collect_pallet_types(pallet, &mut type_ids);
}
should_retain
});
// We index pallets by their u8 index for easy access. Rebuild this index.
metadata.pallets_by_index = metadata
.pallets
.values()
.iter()
.enumerate()
.map(|(pos, p)| (p.index, pos))
.collect();
// Keep the extrinsic stuff referenced in our metadata.
collect_extrinsic_types(&metadata.extrinsic, &mut type_ids);
// Keep the "runtime" type ID, since it's referenced in our metadata.
type_ids.insert(metadata.runtime_ty);
// Keep only the runtime API types that the filter allows for. Keep hold of all
// type IDs in the runtime apis we're keeping. Retain all, if no filter specified.
metadata.apis.retain(|api| {
let should_retain = runtime_apis_filter(&api.name);
if should_retain {
collect_runtime_api_types(api, &mut type_ids);
}
should_retain
});
// Additionally, subxt depends on the `DispatchError` type existing; we use the same
// logic here that is used when building our `Metadata`.
let dispatch_error_ty = metadata
.types
.types
.iter()
.find(|ty| ty.ty.path.segments == ["sp_runtime", "DispatchError"])
.expect("Metadata must contain sp_runtime::DispatchError");
type_ids.insert(dispatch_error_ty.id);
// Now, keep the type IDs we've asked for. This recursively keeps any types referenced from these.
// This will return a map from old to new type ID, because IDs may change.
let map_ids = metadata.types.retain(|id| type_ids.contains(&id));
// And finally, we can go and update all of our type IDs in the metadata as a result of this:
for pallets in metadata.pallets.values_mut() {
update_pallet_types(pallets, &map_ids);
}
update_extrinsic_types(&mut metadata.extrinsic, &map_ids);
update_type(&mut metadata.runtime_ty, &map_ids);
update_runtime_api_types(metadata.apis.values_mut(), &map_ids);
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Metadata;
use codec::Decode;
use frame_metadata::{RuntimeMetadata, RuntimeMetadataPrefixed};
use std::{fs, path::Path};
fn load_metadata() -> Metadata {
let bytes = fs::read(Path::new("../artifacts/polkadot_metadata_full.scale"))
.expect("Cannot read metadata blob");
let meta: RuntimeMetadataPrefixed =
Decode::decode(&mut &*bytes).expect("Cannot decode scale metadata");
match meta.1 {
RuntimeMetadata::V14(v14) => v14.try_into().unwrap(),
RuntimeMetadata::V15(v15) => v15.try_into().unwrap(),
_ => panic!("Unsupported metadata version {:?}", meta.1),
}
}
#[test]
fn retain_one_pallet() {
let metadata_cache = load_metadata();
// Retain one pallet at a time ensuring the test does not panic.
for pallet in metadata_cache.pallets() {
let mut metadata = metadata_cache.clone();
retain_metadata(
&mut metadata,
|pallet_name| pallet_name == pallet.name(),
|_| true,
);
assert_eq!(metadata.pallets.len(), 1);
assert_eq!(
&*metadata.pallets.get_by_index(0).unwrap().name,
pallet.name()
);
}
}
#[test]
fn retain_one_runtime_api() {
let metadata_cache = load_metadata();
// Retain one runtime API at a time ensuring the test does not panic.
for runtime_api in metadata_cache.runtime_api_traits() {
let mut metadata = metadata_cache.clone();
retain_metadata(
&mut metadata,
|_| true,
|runtime_api_name| runtime_api_name == runtime_api.name(),
);
assert_eq!(metadata.apis.len(), 1);
assert_eq!(
&*metadata.apis.get_by_index(0).unwrap().name,
runtime_api.name()
);
}
}
}
+844
View File
@@ -0,0 +1,844 @@
// Copyright 2019-2023 Parity Technologies (UK) Ltd.
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.
//! Utility functions for metadata validation.
use crate::{
ExtrinsicMetadata, Metadata, PalletMetadata, RuntimeApiMetadata, RuntimeApiMethodMetadata,
StorageEntryMetadata, StorageEntryType,
};
use scale_info::{form::PortableForm, Field, PortableRegistry, TypeDef, Variant};
use std::collections::HashSet;
/// Predefined value to be returned when we already visited a type.
const MAGIC_RECURSIVE_TYPE_VALUE: &[u8] = &[123];
// The number of bytes our `hash` function produces.
const HASH_LEN: usize = 32;
/// Internal byte representation for various metadata types utilized for
/// generating deterministic hashes between different rust versions.
#[repr(u8)]
enum TypeBeingHashed {
Composite,
Variant,
Sequence,
Array,
Tuple,
Primitive,
Compact,
BitSequence,
}
/// Hashing function utilized internally.
fn hash(data: &[u8]) -> [u8; HASH_LEN] {
sp_core_hashing::twox_256(data)
}
/// XOR two hashes together. Only use this when you don't care about the order
/// of the things you're hashing together.
fn xor(a: [u8; HASH_LEN], b: [u8; HASH_LEN]) -> [u8; HASH_LEN] {
let mut out = [0u8; HASH_LEN];
for (idx, (a, b)) in a.into_iter().zip(b).enumerate() {
out[idx] = a ^ b;
}
out
}
// Combine some number of HASH_LEN byte hashes and output a single HASH_LEN
// byte hash to uniquely represent the inputs.
macro_rules! count_idents {
() => { 0 };
($n:ident $($rest:ident)*) => { 1 + count_idents!($($rest)*) }
}
macro_rules! concat_and_hash_n {
($name:ident($($arg:ident)+)) => {
fn $name($($arg: &[u8; HASH_LEN]),+) -> [u8; HASH_LEN] {
let mut out = [0u8; HASH_LEN * count_idents!($($arg)+)];
let mut start = 0;
$(
out[start..start+HASH_LEN].copy_from_slice(&$arg[..]);
#[allow(unused_assignments)]
{ start += HASH_LEN; }
)+
hash(&out)
}
}
}
concat_and_hash_n!(concat_and_hash2(a b));
concat_and_hash_n!(concat_and_hash3(a b c));
concat_and_hash_n!(concat_and_hash4(a b c d));
concat_and_hash_n!(concat_and_hash5(a b c d e));
/// Obtain the hash representation of a `scale_info::Field`.
fn get_field_hash(
registry: &PortableRegistry,
field: &Field<PortableForm>,
visited_ids: &mut HashSet<u32>,
) -> [u8; HASH_LEN] {
let field_name_bytes = match &field.name {
Some(name) => hash(name.as_bytes()),
None => [0u8; HASH_LEN],
};
concat_and_hash2(
&field_name_bytes,
&get_type_hash(registry, field.ty.id, visited_ids),
)
}
/// Obtain the hash representation of a `scale_info::Variant`.
fn get_variant_hash(
registry: &PortableRegistry,
var: &Variant<PortableForm>,
visited_ids: &mut HashSet<u32>,
) -> [u8; HASH_LEN] {
let variant_name_bytes = hash(var.name.as_bytes());
let variant_field_bytes = var.fields.iter().fold([0u8; HASH_LEN], |bytes, field| {
// EncodeAsType and DecodeAsType don't care about variant field ordering,
// so XOR the fields to ensure that it doesn't matter.
xor(bytes, get_field_hash(registry, field, visited_ids))
});
concat_and_hash2(&variant_name_bytes, &variant_field_bytes)
}
/// Obtain the hash representation of a `scale_info::TypeDef`.
fn get_type_def_hash(
registry: &PortableRegistry,
ty_def: &TypeDef<PortableForm>,
visited_ids: &mut HashSet<u32>,
) -> [u8; HASH_LEN] {
match ty_def {
TypeDef::Composite(composite) => {
let composite_id_bytes = [TypeBeingHashed::Composite as u8; HASH_LEN];
let composite_field_bytes =
composite
.fields
.iter()
.fold([0u8; HASH_LEN], |bytes, field| {
// With EncodeAsType and DecodeAsType we no longer care which order the fields are in,
// as long as all of the names+types are there. XOR to not care about ordering.
xor(bytes, get_field_hash(registry, field, visited_ids))
});
concat_and_hash2(&composite_id_bytes, &composite_field_bytes)
}
TypeDef::Variant(variant) => {
let variant_id_bytes = [TypeBeingHashed::Variant as u8; HASH_LEN];
let variant_field_bytes =
variant.variants.iter().fold([0u8; HASH_LEN], |bytes, var| {
// With EncodeAsType and DecodeAsType we no longer care which order the variants are in,
// as long as all of the names+types are there. XOR to not care about ordering.
xor(bytes, get_variant_hash(registry, var, visited_ids))
});
concat_and_hash2(&variant_id_bytes, &variant_field_bytes)
}
TypeDef::Sequence(sequence) => concat_and_hash2(
&[TypeBeingHashed::Sequence as u8; HASH_LEN],
&get_type_hash(registry, sequence.type_param.id, visited_ids),
),
TypeDef::Array(array) => {
// Take length into account too; different length must lead to different hash.
let array_id_bytes = {
let mut a = [0u8; HASH_LEN];
a[0] = TypeBeingHashed::Array as u8;
a[1..5].copy_from_slice(&array.len.to_be_bytes());
a
};
concat_and_hash2(
&array_id_bytes,
&get_type_hash(registry, array.type_param.id, visited_ids),
)
}
TypeDef::Tuple(tuple) => {
let mut bytes = hash(&[TypeBeingHashed::Tuple as u8]);
for field in &tuple.fields {
bytes = concat_and_hash2(&bytes, &get_type_hash(registry, field.id, visited_ids));
}
bytes
}
TypeDef::Primitive(primitive) => {
// Cloning the 'primitive' type should essentially be a copy.
hash(&[TypeBeingHashed::Primitive as u8, primitive.clone() as u8])
}
TypeDef::Compact(compact) => concat_and_hash2(
&[TypeBeingHashed::Compact as u8; HASH_LEN],
&get_type_hash(registry, compact.type_param.id, visited_ids),
),
TypeDef::BitSequence(bitseq) => concat_and_hash3(
&[TypeBeingHashed::BitSequence as u8; HASH_LEN],
&get_type_hash(registry, bitseq.bit_order_type.id, visited_ids),
&get_type_hash(registry, bitseq.bit_store_type.id, visited_ids),
),
}
}
/// Obtain the hash representation of a `scale_info::Type` identified by id.
fn get_type_hash(
registry: &PortableRegistry,
id: u32,
visited_ids: &mut HashSet<u32>,
) -> [u8; HASH_LEN] {
// Guard against recursive types and return a fixed arbitrary hash
if !visited_ids.insert(id) {
return hash(MAGIC_RECURSIVE_TYPE_VALUE);
}
let ty = registry
.resolve(id)
.expect("Type ID provided by the metadata is registered; qed");
get_type_def_hash(registry, &ty.type_def, visited_ids)
}
/// Obtain the hash representation of a `frame_metadata::v15::ExtrinsicMetadata`.
fn get_extrinsic_hash(
registry: &PortableRegistry,
extrinsic: &ExtrinsicMetadata,
) -> [u8; HASH_LEN] {
let mut visited_ids = HashSet::<u32>::new();
let mut bytes = concat_and_hash2(
&get_type_hash(registry, extrinsic.ty, &mut visited_ids),
&[extrinsic.version; 32],
);
for signed_extension in extrinsic.signed_extensions.iter() {
bytes = concat_and_hash4(
&bytes,
&hash(signed_extension.identifier.as_bytes()),
&get_type_hash(registry, signed_extension.extra_ty, &mut visited_ids),
&get_type_hash(registry, signed_extension.additional_ty, &mut visited_ids),
)
}
bytes
}
/// Get the hash corresponding to a single storage entry.
fn get_storage_entry_hash(
registry: &PortableRegistry,
entry: &StorageEntryMetadata,
visited_ids: &mut HashSet<u32>,
) -> [u8; HASH_LEN] {
let mut bytes = concat_and_hash3(
&hash(entry.name.as_bytes()),
// Cloning 'entry.modifier' should essentially be a copy.
&[entry.modifier as u8; HASH_LEN],
&hash(&entry.default),
);
match &entry.entry_type {
StorageEntryType::Plain(ty) => {
concat_and_hash2(&bytes, &get_type_hash(registry, *ty, visited_ids))
}
StorageEntryType::Map {
hashers,
key_ty,
value_ty,
} => {
for hasher in hashers {
// Cloning the hasher should essentially be a copy.
bytes = concat_and_hash2(&bytes, &[*hasher as u8; HASH_LEN]);
}
concat_and_hash3(
&bytes,
&get_type_hash(registry, *key_ty, visited_ids),
&get_type_hash(registry, *value_ty, visited_ids),
)
}
}
}
/// Get the hash corresponding to a single runtime API method.
fn get_runtime_method_hash(
registry: &PortableRegistry,
trait_name: &str,
method_metadata: &RuntimeApiMethodMetadata,
visited_ids: &mut HashSet<u32>,
) -> [u8; HASH_LEN] {
// The trait name is part of the runtime API call that is being
// generated for this method. Therefore the trait name is strongly
// connected to the method in the same way as a parameter is
// to the method.
let mut bytes = concat_and_hash2(
&hash(trait_name.as_bytes()),
&hash(method_metadata.name.as_bytes()),
);
for input in &method_metadata.inputs {
bytes = concat_and_hash3(
&bytes,
&hash(input.name.as_bytes()),
&get_type_hash(registry, input.ty, visited_ids),
);
}
bytes = concat_and_hash2(
&bytes,
&get_type_hash(registry, method_metadata.output_ty, visited_ids),
);
bytes
}
/// Obtain the hash of all of a runtime API trait, including all of its methods.
fn get_runtime_trait_hash(trait_metadata: RuntimeApiMetadata) -> [u8; HASH_LEN] {
let mut visited_ids = HashSet::new();
let trait_name = &*trait_metadata.inner.name;
let method_bytes = trait_metadata
.methods()
.fold([0u8; HASH_LEN], |bytes, method_metadata| {
// We don't care what order the trait methods exist in, and want the hash to
// be identical regardless. For this, we can just XOR the hashes for each method
// together; we'll get the same output whichever order they are XOR'd together in,
// so long as each individual method is the same.
xor(
bytes,
get_runtime_method_hash(
trait_metadata.types,
trait_name,
method_metadata,
&mut visited_ids,
),
)
});
concat_and_hash2(&hash(trait_name.as_bytes()), &method_bytes)
}
/// Obtain the hash for a specific storage item, or an error if it's not found.
pub fn get_storage_hash(pallet: &PalletMetadata, entry_name: &str) -> Option<[u8; HASH_LEN]> {
let storage = pallet.storage()?;
let entry = storage.entry_by_name(entry_name)?;
let hash = get_storage_entry_hash(pallet.types, entry, &mut HashSet::new());
Some(hash)
}
/// Obtain the hash for a specific constant, or an error if it's not found.
pub fn get_constant_hash(pallet: &PalletMetadata, constant_name: &str) -> Option<[u8; HASH_LEN]> {
let constant = pallet.constant_by_name(constant_name)?;
// We only need to check that the type of the constant asked for matches.
let bytes = get_type_hash(pallet.types, constant.ty, &mut HashSet::new());
Some(bytes)
}
/// Obtain the hash for a specific call, or an error if it's not found.
pub fn get_call_hash(pallet: &PalletMetadata, call_name: &str) -> Option<[u8; HASH_LEN]> {
let call_variant = pallet.call_variant_by_name(call_name)?;
// hash the specific variant representing the call we are interested in.
let hash = get_variant_hash(pallet.types, call_variant, &mut HashSet::new());
Some(hash)
}
/// Obtain the hash of a specific runtime API function, or an error if it's not found.
pub fn get_runtime_api_hash(
runtime_apis: &RuntimeApiMetadata,
method_name: &str,
) -> Option<[u8; HASH_LEN]> {
let trait_name = &*runtime_apis.inner.name;
let method_metadata = runtime_apis.method_by_name(method_name)?;
Some(get_runtime_method_hash(
runtime_apis.types,
trait_name,
method_metadata,
&mut HashSet::new(),
))
}
/// Obtain the hash representation of a `frame_metadata::v15::PalletMetadata`.
pub fn get_pallet_hash(pallet: PalletMetadata) -> [u8; HASH_LEN] {
let mut visited_ids = HashSet::<u32>::new();
let registry = pallet.types;
let call_bytes = match pallet.call_ty_id() {
Some(calls) => get_type_hash(registry, calls, &mut visited_ids),
None => [0u8; HASH_LEN],
};
let event_bytes = match pallet.event_ty_id() {
Some(event) => get_type_hash(registry, event, &mut visited_ids),
None => [0u8; HASH_LEN],
};
let error_bytes = match pallet.error_ty_id() {
Some(error) => get_type_hash(registry, error, &mut visited_ids),
None => [0u8; HASH_LEN],
};
let constant_bytes = pallet.constants().fold([0u8; HASH_LEN], |bytes, constant| {
// We don't care what order the constants occur in, so XOR together the combinations
// of (constantName, constantType) to make the order we see them irrelevant.
let constant_hash = concat_and_hash2(
&hash(constant.name.as_bytes()),
&get_type_hash(registry, constant.ty(), &mut visited_ids),
);
xor(bytes, constant_hash)
});
let storage_bytes = match pallet.storage() {
Some(storage) => {
let prefix_hash = hash(storage.prefix().as_bytes());
let entries_hash = storage.entries().fold([0u8; HASH_LEN], |bytes, entry| {
// We don't care what order the storage entries occur in, so XOR them together
// to make the order irrelevant.
xor(
bytes,
get_storage_entry_hash(registry, entry, &mut visited_ids),
)
});
concat_and_hash2(&prefix_hash, &entries_hash)
}
None => [0u8; HASH_LEN],
};
// Hash all of the above together:
concat_and_hash5(
&call_bytes,
&event_bytes,
&error_bytes,
&constant_bytes,
&storage_bytes,
)
}
/// Obtain a hash representation of our metadata or some part of it.
/// This is obtained by calling [`crate::Metadata::hasher()`].
pub struct MetadataHasher<'a> {
metadata: &'a Metadata,
specific_pallets: Option<Vec<&'a str>>,
}
impl<'a> MetadataHasher<'a> {
/// Create a new [`MetadataHasher`]
pub(crate) fn new(metadata: &'a Metadata) -> Self {
Self {
metadata,
specific_pallets: None,
}
}
/// Only hash the provided pallets instead of hashing every pallet.
pub fn only_these_pallets<S: AsRef<str>>(&mut self, specific_pallets: &'a [S]) -> &mut Self {
self.specific_pallets = Some(specific_pallets.iter().map(|n| n.as_ref()).collect());
self
}
/// Hash the given metadata.
pub fn hash(&self) -> [u8; HASH_LEN] {
let mut visited_ids = HashSet::<u32>::new();
let metadata = self.metadata;
let pallet_hash = metadata.pallets().fold([0u8; HASH_LEN], |bytes, pallet| {
// If specific pallets are given, only include this pallet if it's
// in the list.
if let Some(specific_pallets) = &self.specific_pallets {
if specific_pallets.iter().all(|&p| p != pallet.name()) {
return bytes;
}
}
// We don't care what order the pallets are seen in, so XOR their
// hashes together to be order independent.
xor(bytes, get_pallet_hash(pallet))
});
let apis_hash = metadata
.runtime_api_traits()
.fold([0u8; HASH_LEN], |bytes, api| {
// We don't care what order the runtime APIs are seen in, so XOR
xor(bytes, get_runtime_trait_hash(api))
});
let extrinsic_hash = get_extrinsic_hash(&metadata.types, &metadata.extrinsic);
let runtime_hash = get_type_hash(&metadata.types, metadata.runtime_ty(), &mut visited_ids);
concat_and_hash4(&pallet_hash, &apis_hash, &extrinsic_hash, &runtime_hash)
}
}
#[cfg(test)]
mod tests {
use super::*;
use bitvec::{order::Lsb0, vec::BitVec};
use frame_metadata::v15;
use scale_info::meta_type;
// Define recursive types.
#[allow(dead_code)]
#[derive(scale_info::TypeInfo)]
struct A {
pub b: Box<B>,
}
#[allow(dead_code)]
#[derive(scale_info::TypeInfo)]
struct B {
pub a: Box<A>,
}
// Define TypeDef supported types.
#[allow(dead_code)]
#[derive(scale_info::TypeInfo)]
// TypeDef::Composite with TypeDef::Array with Typedef::Primitive.
struct AccountId32([u8; HASH_LEN]);
#[allow(dead_code)]
#[derive(scale_info::TypeInfo)]
// TypeDef::Variant.
enum DigestItem {
PreRuntime(
// TypeDef::Array with primitive.
[::core::primitive::u8; 4usize],
// TypeDef::Sequence.
::std::vec::Vec<::core::primitive::u8>,
),
Other(::std::vec::Vec<::core::primitive::u8>),
// Nested TypeDef::Tuple.
RuntimeEnvironmentUpdated(((i8, i16), (u32, u64))),
// TypeDef::Compact.
Index(#[codec(compact)] ::core::primitive::u8),
// TypeDef::BitSequence.
BitSeq(BitVec<u8, Lsb0>),
}
#[allow(dead_code)]
#[derive(scale_info::TypeInfo)]
// Ensure recursive types and TypeDef variants are captured.
struct MetadataTestType {
recursive: A,
composite: AccountId32,
type_def: DigestItem,
}
#[allow(dead_code)]
#[derive(scale_info::TypeInfo)]
// Simulate a PalletCallMetadata.
enum Call {
#[codec(index = 0)]
FillBlock { ratio: AccountId32 },
#[codec(index = 1)]
Remark { remark: DigestItem },
}
fn build_default_extrinsic() -> v15::ExtrinsicMetadata {
v15::ExtrinsicMetadata {
ty: meta_type::<()>(),
version: 0,
signed_extensions: vec![],
}
}
fn default_pallet() -> v15::PalletMetadata {
v15::PalletMetadata {
name: "Test",
storage: None,
calls: None,
event: None,
constants: vec![],
error: None,
index: 0,
docs: vec![],
}
}
fn build_default_pallets() -> Vec<v15::PalletMetadata> {
vec![
v15::PalletMetadata {
name: "First",
calls: Some(v15::PalletCallMetadata {
ty: meta_type::<MetadataTestType>(),
}),
..default_pallet()
},
v15::PalletMetadata {
name: "Second",
index: 1,
calls: Some(v15::PalletCallMetadata {
ty: meta_type::<(DigestItem, AccountId32, A)>(),
}),
..default_pallet()
},
]
}
fn pallets_to_metadata(pallets: Vec<v15::PalletMetadata>) -> Metadata {
v15::RuntimeMetadataV15::new(
pallets,
build_default_extrinsic(),
meta_type::<()>(),
vec![],
)
.try_into()
.expect("can build valid metadata")
}
#[test]
fn different_pallet_index() {
let pallets = build_default_pallets();
let mut pallets_swap = pallets.clone();
let metadata = pallets_to_metadata(pallets);
// Change the order in which pallets are registered.
pallets_swap.swap(0, 1);
pallets_swap[0].index = 0;
pallets_swap[1].index = 1;
let metadata_swap = pallets_to_metadata(pallets_swap);
let hash = MetadataHasher::new(&metadata).hash();
let hash_swap = MetadataHasher::new(&metadata_swap).hash();
// Changing pallet order must still result in a deterministic unique hash.
assert_eq!(hash, hash_swap);
}
#[test]
fn recursive_type() {
let mut pallet = default_pallet();
pallet.calls = Some(v15::PalletCallMetadata {
ty: meta_type::<A>(),
});
let metadata = pallets_to_metadata(vec![pallet]);
// Check hashing algorithm finishes on a recursive type.
MetadataHasher::new(&metadata).hash();
}
#[test]
/// Ensure correctness of hashing when parsing the `metadata.types`.
///
/// Having a recursive structure `A: { B }` and `B: { A }` registered in different order
/// `types: { { id: 0, A }, { id: 1, B } }` and `types: { { id: 0, B }, { id: 1, A } }`
/// must produce the same deterministic hashing value.
fn recursive_types_different_order() {
let mut pallets = build_default_pallets();
pallets[0].calls = Some(v15::PalletCallMetadata {
ty: meta_type::<A>(),
});
pallets[1].calls = Some(v15::PalletCallMetadata {
ty: meta_type::<B>(),
});
pallets[1].index = 1;
let mut pallets_swap = pallets.clone();
let metadata = pallets_to_metadata(pallets);
pallets_swap.swap(0, 1);
pallets_swap[0].index = 0;
pallets_swap[1].index = 1;
let metadata_swap = pallets_to_metadata(pallets_swap);
let hash = MetadataHasher::new(&metadata).hash();
let hash_swap = MetadataHasher::new(&metadata_swap).hash();
// Changing pallet order must still result in a deterministic unique hash.
assert_eq!(hash, hash_swap);
}
#[test]
fn pallet_hash_correctness() {
let compare_pallets_hash = |lhs: &v15::PalletMetadata, rhs: &v15::PalletMetadata| {
let metadata = pallets_to_metadata(vec![lhs.clone()]);
let hash = MetadataHasher::new(&metadata).hash();
let metadata = pallets_to_metadata(vec![rhs.clone()]);
let new_hash = MetadataHasher::new(&metadata).hash();
assert_ne!(hash, new_hash);
};
// Build metadata progressively from an empty pallet to a fully populated pallet.
let mut pallet = default_pallet();
let pallet_lhs = pallet.clone();
pallet.storage = Some(v15::PalletStorageMetadata {
prefix: "Storage",
entries: vec![v15::StorageEntryMetadata {
name: "BlockWeight",
modifier: v15::StorageEntryModifier::Default,
ty: v15::StorageEntryType::Plain(meta_type::<u8>()),
default: vec![],
docs: vec![],
}],
});
compare_pallets_hash(&pallet_lhs, &pallet);
let pallet_lhs = pallet.clone();
// Calls are similar to:
//
// ```
// pub enum Call {
// call_name_01 { arg01: type },
// call_name_02 { arg01: type, arg02: type }
// }
// ```
pallet.calls = Some(v15::PalletCallMetadata {
ty: meta_type::<Call>(),
});
compare_pallets_hash(&pallet_lhs, &pallet);
let pallet_lhs = pallet.clone();
// Events are similar to Calls.
pallet.event = Some(v15::PalletEventMetadata {
ty: meta_type::<Call>(),
});
compare_pallets_hash(&pallet_lhs, &pallet);
let pallet_lhs = pallet.clone();
pallet.constants = vec![v15::PalletConstantMetadata {
name: "BlockHashCount",
ty: meta_type::<u64>(),
value: vec![96u8, 0, 0, 0],
docs: vec![],
}];
compare_pallets_hash(&pallet_lhs, &pallet);
let pallet_lhs = pallet.clone();
pallet.error = Some(v15::PalletErrorMetadata {
ty: meta_type::<MetadataTestType>(),
});
compare_pallets_hash(&pallet_lhs, &pallet);
}
#[test]
fn metadata_per_pallet_hash_correctness() {
let pallets = build_default_pallets();
// Build metadata with just the first pallet.
let metadata_one = pallets_to_metadata(vec![pallets[0].clone()]);
// Build metadata with both pallets.
let metadata_both = pallets_to_metadata(pallets);
// Hashing will ignore any non-existant pallet and return the same result.
let hash = MetadataHasher::new(&metadata_one)
.only_these_pallets(&["First", "Second"])
.hash();
let hash_rhs = MetadataHasher::new(&metadata_one)
.only_these_pallets(&["First"])
.hash();
assert_eq!(hash, hash_rhs, "hashing should ignore non-existant pallets");
// Hashing one pallet from metadata with 2 pallets inserted will ignore the second pallet.
let hash_second = MetadataHasher::new(&metadata_both)
.only_these_pallets(&["First"])
.hash();
assert_eq!(
hash_second, hash,
"hashing one pallet should ignore the others"
);
// Check hashing with all pallets.
let hash_second = MetadataHasher::new(&metadata_both)
.only_these_pallets(&["First", "Second"])
.hash();
assert_ne!(
hash_second, hash,
"hashing both pallets should produce a different result from hashing just one pallet"
);
}
#[test]
fn field_semantic_changes() {
// Get a hash representation of the provided meta type,
// inserted in the context of pallet metadata call.
let to_hash = |meta_ty| {
let pallet = v15::PalletMetadata {
calls: Some(v15::PalletCallMetadata { ty: meta_ty }),
..default_pallet()
};
let metadata = pallets_to_metadata(vec![pallet]);
MetadataHasher::new(&metadata).hash()
};
#[allow(dead_code)]
#[derive(scale_info::TypeInfo)]
enum EnumA1 {
First { hi: u8, bye: String },
Second(u32),
Third,
}
#[allow(dead_code)]
#[derive(scale_info::TypeInfo)]
enum EnumA2 {
Second(u32),
Third,
First { bye: String, hi: u8 },
}
// EncodeAsType and DecodeAsType only care about enum variant names
// and not indexes or field ordering or the enum name itself..
assert_eq!(
to_hash(meta_type::<EnumA1>()),
to_hash(meta_type::<EnumA2>())
);
#[allow(dead_code)]
#[derive(scale_info::TypeInfo)]
struct StructB1 {
hello: bool,
another: [u8; 32],
}
#[allow(dead_code)]
#[derive(scale_info::TypeInfo)]
struct StructB2 {
another: [u8; 32],
hello: bool,
}
// As with enums, struct names and field orders are irrelevant as long as
// the field names and types are the same.
assert_eq!(
to_hash(meta_type::<StructB1>()),
to_hash(meta_type::<StructB2>())
);
#[allow(dead_code)]
#[derive(scale_info::TypeInfo)]
enum EnumC1 {
First(u8),
}
#[allow(dead_code)]
#[derive(scale_info::TypeInfo)]
enum EnumC2 {
Second(u8),
}
// The enums are binary compatible, but the variants have different names, so
// semantically they are different and should not be equal.
assert_ne!(
to_hash(meta_type::<EnumC1>()),
to_hash(meta_type::<EnumC2>())
);
#[allow(dead_code)]
#[derive(scale_info::TypeInfo)]
enum EnumD1 {
First { a: u8 },
}
#[allow(dead_code)]
#[derive(scale_info::TypeInfo)]
enum EnumD2 {
First { b: u8 },
}
// Named fields contain a different semantic meaning ('a' and 'b') despite
// being binary compatible, so hashes should be different.
assert_ne!(
to_hash(meta_type::<EnumD1>()),
to_hash(meta_type::<EnumD2>())
);
#[allow(dead_code)]
#[derive(scale_info::TypeInfo)]
struct StructE1 {
a: u32,
}
#[allow(dead_code)]
#[derive(scale_info::TypeInfo)]
struct StructE2 {
b: u32,
}
// Similar to enums, struct fields that contain a different semantic meaning
// ('a' and 'b') despite being binary compatible will have different hashes.
assert_ne!(
to_hash(meta_type::<StructE1>()),
to_hash(meta_type::<StructE2>())
);
}
}
+86
View File
@@ -0,0 +1,86 @@
// Copyright 2019-2023 Parity Technologies (UK) Ltd.
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.
use scale_info::{form::PortableForm, PortableRegistry, TypeDef, Variant};
use std::collections::HashMap;
/// Given some type ID and type registry, build a couple of
/// indexes to look up variants by index or name. If the ID provided
/// is not a variant, the index will be empty.
///
/// API optimized for dealing with the `Option<u32>` variant type IDs
/// that we get in metadata pallets.
#[derive(Debug, Clone)]
pub struct VariantIndex {
by_name: HashMap<String, usize>,
by_index: HashMap<u8, usize>,
}
impl VariantIndex {
/// Build indexes from the optional variant ID.
pub fn build(variant_id: Option<u32>, types: &PortableRegistry) -> Self {
let Some(variants) = Self::get(variant_id, types) else {
return Self::empty()
};
let mut by_name = HashMap::new();
let mut by_index = HashMap::new();
for (pos, variant) in variants.iter().enumerate() {
by_name.insert(variant.name.to_owned(), pos);
by_index.insert(variant.index, pos);
}
Self { by_name, by_index }
}
/// Build an empty index.
pub fn empty() -> Self {
Self {
by_name: Default::default(),
by_index: Default::default(),
}
}
/// Get the variants we're pointing at; None if this isn't possible.
pub fn get(
variant_id: Option<u32>,
types: &PortableRegistry,
) -> Option<&[Variant<PortableForm>]> {
let Some(variant_id) = variant_id else {
return None
};
let TypeDef::Variant(v) = &types.resolve(variant_id)?.type_def else {
return None
};
Some(&v.variants)
}
/// Lookup a variant by name; `None` if the type is not a variant or name isn't found.
pub fn lookup_by_name<'a, K>(
&self,
name: &K,
variant_id: Option<u32>,
types: &'a PortableRegistry,
) -> Option<&'a Variant<PortableForm>>
where
String: std::borrow::Borrow<K>,
K: std::hash::Hash + Eq + ?Sized,
{
let pos = *self.by_name.get(name)?;
let variants = Self::get(variant_id, types)?;
variants.get(pos)
}
/// Lookup a variant by index; `None` if the type is not a variant or index isn't found.
pub fn lookup_by_index<'a>(
&self,
index: u8,
variant_id: Option<u32>,
types: &'a PortableRegistry,
) -> Option<&'a Variant<PortableForm>> {
let pos = *self.by_index.get(&index)?;
let variants = Self::get(variant_id, types)?;
variants.get(pos)
}
}