mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-11 23:31:07 +00:00
Static Metadata Validation (#478)
* metadata: Implement MetadataHashable for deterministic hashing Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Hash `scale_info::Field` Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Hash `scale_info::Variant` Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Hash `scale_info::TypeDef` Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Hash pallet metadata Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Avoid data representation collision via unique identifiers Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Finalize hashing on recursive types Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Cache recursive calls Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Move `MetadataHashable` to codegen to avoid cyclic dependency Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Add pallet unique hash Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Wrap metadata as owned Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt: Use MetadataHashable wrapper for clients Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt: Generate runtime pallet uid from metadata Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * Validate metadata compatibility at the pallet level Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * Update polkadot.rs Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * Modify examples and tests for the new API Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Implement metadata uid Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * Update polkadot with TryFrom implementation Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * client: Change `to_runtime_api` to reflect TryFrom changes Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * client: Skip full metadata validation option Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Add option to skip pallet validation for TransactionApi Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Add option to skip pallet validation for StorageApi Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * Update polkadot.rs with ability to skip pallet validation Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Change `MetadataHashable` to per function implementation Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Use metadata hashes functions Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt: Use metadata hashes functions Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Make `get_type_uid` private Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen, subxt: Rename metadata functions `*_uid` to `*_hash` Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Update `get_field_hash` to use `codec::Encode` Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt: Update polkadot.rs Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen, subxt: Move metadata check from client to subxt::Metadata Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen, subxt: Rename metadata check functions to follow `*_hash` naming Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Update polkadot.rs to reflect naming changes Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Use `encode_to` for metadata generation Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Update polkadot.rs to reflect `encode_to` changes Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Specific name for visited set Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Provide cache to hashing functions Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Compute metadata hash by sorted pallets Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Get extrinsic hash Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Extend metadata hash with extrinsic and metadata type Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Add cache as metadata parameter Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen, subxt: Update metadata hash to use cache Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Implement Default trait for MetadataHasherCache Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Add cache for pallets Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Move functionality to metadata crate Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen, subxt: Use subxt-metadata crate Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Remove metdata hashing functionality Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Add documentation Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Fix vector capacity to include extrinisc and type hash Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Add empty CLI Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata-cli: Fetch metadata from substrate nodes Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata-cli: Log metadata hashes of provided nodes Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata-cli: Group compatible nodes by metadata Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata-cli: Simplify hash map insertion Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata-cli: Move full metadata check to function Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata-cli: Group metadata validation at the pallet level Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt: Persist metadata cache Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Move compatibility cli from subxt-metadata to subxt-cli Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Remove cli from subxt-metadata Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * cli: Fix clippy Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Fix compatible metadata when pallets are registered in different order Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * tests: Handle result of pallet hashing Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Remove type cache for deterministic hashing Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Add test assets from `substrate-node-template` tag `polkadot-v0.9.17` Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata-tests: Check cache hashing for Balances pallet Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Fix `get_type_hash` clippy issue Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata-tests: Compare one time cache with persistent cache Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata-test: Check metadata hash populates cache for pallets Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata-tests: Simplify `cache_deterministic_hash` test Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata-tests: Check deterministic metadata for different order pallets Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Fix clippy Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Implement TransactionApiUnchecked for skipping pallet validation Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Implement StorageApiUnchecked for skipping pallet validation Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Remove skip_pallet_validation boolean Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt: Implement ClientUnchecked for skipping metadata validation Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * Update polkadot.rs Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * Update examples of rpc_call to skip metadata Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt: Remove heck dependency Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Add pallet name as an identifier for pallet hashing Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Implement MetadataHashDetails Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Adjust testing to `MetadataHashDetails` interface Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Remove extra `pallet_name` Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt: Update polkadot.rs Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt: Fix clippy issue Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Change StorageApi to support `_unchecked` methods Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Change TransactionApi to support `_unchecked` methods Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt: Switch back from `TryFrom` to `From` for `subxt::Client` Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen, subxt: Remove `ClientUnchecked` Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Expose `validate_metadata` as validation of compatibility method Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * examples: Update to the new interface Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt: Update test integration to latest interface Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt: Update polkadot.rs Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata/tests: Check different pallet index order Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata/tests: Check recursive type hashing Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata/tests: Check recursive types registered in different order Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata/tests: Fix recursive types warning Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata/tests: Remove test assets Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata/tests: Extend tests to verify cached pallet values Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * examples: Add metadata compatiblity example Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * examples: Revert balance_transfer to initial form Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Add ConstantsApi metadata check Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * tests: Modify tests to accomodate ConstantsApi changes Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * examples: Modify verified version Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt: Generate polkadot.rs from `0.9.18-4542a603cc-aarch64-macos` Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * examples: Update polkadot_metadata.scale from `0.9.18-4542a603cc-aarch64-macos` Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Update documentation Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * tests: Modify default pallet usage Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata/tests: Remove hex dependency Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata/tests: Add MetadataTestType to capture complex types Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata/tests: Update tests to use complex types Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata/tests: Check metadata correctness via extending pallets Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata/tests: Extend pallet hash with Events Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata/tests: Extend pallet hash with constants Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata/tests: Extend pallet hash with error Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * examples: Extend metadata compatibiliy with StorageApi and ConstantsApi Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * Modify comments and documentation Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Benchmarks for full validation and pallet validation Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata/benches: Fix clippy Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Hash metadata just by inspecting the provided pallets Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Make pallets generic over T for `AsRef<str>` Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Expose the name of the pallets composing the metadata Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt/tests: Update polkadot.rs with pallets name Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Obtain metadata hash only by inspecting pallets Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen,subxt: Extend the metadata hash to utilize just pallets Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt/tests: Update polkadot.rs with client metadata has per pallet Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata/tests: Test `get_metadata_per_pallet_hash` correctness Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Fix clippy Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata/benches: Fix decode of metadata Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Fix clippy Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * [static metadata] validate storage, calls and constants per call (#507) * validate storage, calls and constants per call * fix typo * cache per-thing hashing, move an Arc, remove some unused bits * create hash_cache to simplify metadata call/constant/storage caching * simplify/robustify the caching logic to help prevent mismatch between get and set * cargo fmt * Fix clippy Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * bench the per-call metadata functions * metadata: Add test for `node_template_runtime_variant` Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * ensure criteron cli opts work * group benchmarks and avoid unwrap issues * metadata: Check template runtime for handling the pallet swap order case Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Remove debug logs Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Optimise by removing field's name and type_name and type's path Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Refactor `get_type_hash` to break recursion earlier Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt: Add tests for `hash_cache` Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt: Add tests for checking Metadata Inner cache Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Check semantic changes inside enum and struct fields Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Add enums named differently with compatible semantic meaning Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Guard testing of release versions for `node_template_runtime` Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * Improve documentation Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * Update polkadot.rs Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata/tests: Manually construct type of `node_template_runtimeL::Call` Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * no more special Call handling, avoid a little cloning, and actually sort by name * remove unused deps and fmt * RuntimeMetadataLastVersion => RuntimeMetadataV14 * remove a bunch of allocations in the metadata hashing, speed up from ~17ms to ~5ms * update release docs to release metadata crate too * subxt: Remove codegen dependency Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt: Replace std RwLock with parking_lot Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt/tests: Add ws address to `TestNodeProcess` Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt/tests: Add metadata validation integration test Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt: Allow setting metadata on the ClientBuilder Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt/tests: Check incompatible metadatas Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * metadata: Fix constant hashing for deterministic output Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt/tests: Check metadata validation for constants Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt/tests: Test validation for calls Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt/tests: Test validation for storage Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt: Expose `set_metadata` for testing only Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt: Guard metadata tests under integration-tests Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> Co-authored-by: James Wilson <james@jsdw.me>
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
// Copyright 2019-2022 Parity Technologies (UK) Ltd.
|
||||
// This file is part of subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use parking_lot::RwLock;
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
collections::HashMap,
|
||||
};
|
||||
|
||||
/// A cache with the simple goal of storing 32 byte hashes against pallet+item keys
|
||||
#[derive(Default, Debug)]
|
||||
pub struct HashCache {
|
||||
inner: RwLock<HashMap<PalletItemKey<'static>, [u8; 32]>>,
|
||||
}
|
||||
|
||||
impl HashCache {
|
||||
/// get a hash out of the cache by its pallet and item key. If the item doesn't exist,
|
||||
/// run the function provided to obtain a hash to insert (or bail with some error on failure).
|
||||
pub fn get_or_insert<F, E>(
|
||||
&self,
|
||||
pallet: &str,
|
||||
item: &str,
|
||||
f: F,
|
||||
) -> Result<[u8; 32], E>
|
||||
where
|
||||
F: FnOnce() -> Result<[u8; 32], E>,
|
||||
{
|
||||
let maybe_hash = self
|
||||
.inner
|
||||
.read()
|
||||
.get(&PalletItemKey::new(pallet, item))
|
||||
.copied();
|
||||
|
||||
if let Some(hash) = maybe_hash {
|
||||
return Ok(hash)
|
||||
}
|
||||
|
||||
let hash = f()?;
|
||||
self.inner.write().insert(
|
||||
PalletItemKey::new(pallet.to_string(), item.to_string()),
|
||||
hash,
|
||||
);
|
||||
|
||||
Ok(hash)
|
||||
}
|
||||
}
|
||||
|
||||
/// This exists so that we can look items up in the cache using &strs, without having to allocate
|
||||
/// Strings first (as you'd have to do to construct something like an `&(String,String)` key).
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
struct PalletItemKey<'a> {
|
||||
pallet: Cow<'a, str>,
|
||||
item: Cow<'a, str>,
|
||||
}
|
||||
|
||||
impl<'a> PalletItemKey<'a> {
|
||||
fn new(pallet: impl Into<Cow<'a, str>>, item: impl Into<Cow<'a, str>>) -> Self {
|
||||
PalletItemKey {
|
||||
pallet: pallet.into(),
|
||||
item: item.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn hash_cache_validation() {
|
||||
let cache = HashCache::default();
|
||||
|
||||
let pallet = "System";
|
||||
let item = "Account";
|
||||
let mut call_number = 0;
|
||||
let value = cache.get_or_insert(pallet, item, || -> Result<[u8; 32], ()> {
|
||||
call_number += 1;
|
||||
Ok([0; 32])
|
||||
});
|
||||
|
||||
assert_eq!(
|
||||
cache
|
||||
.inner
|
||||
.read()
|
||||
.get(&PalletItemKey::new(pallet, item))
|
||||
.unwrap(),
|
||||
&value.unwrap()
|
||||
);
|
||||
assert_eq!(value.unwrap(), [0; 32]);
|
||||
assert_eq!(call_number, 1);
|
||||
|
||||
// Further calls must be hashed.
|
||||
let value = cache.get_or_insert(pallet, item, || -> Result<[u8; 32], ()> {
|
||||
call_number += 1;
|
||||
Ok([0; 32])
|
||||
});
|
||||
assert_eq!(call_number, 1);
|
||||
assert_eq!(value.unwrap(), [0; 32]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,592 @@
|
||||
// Copyright 2019-2022 Parity Technologies (UK) Ltd.
|
||||
// This file is part of subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use super::hash_cache::HashCache;
|
||||
use crate::Call;
|
||||
use codec::Error as CodecError;
|
||||
use frame_metadata::{
|
||||
PalletConstantMetadata,
|
||||
RuntimeMetadata,
|
||||
RuntimeMetadataPrefixed,
|
||||
RuntimeMetadataV14,
|
||||
StorageEntryMetadata,
|
||||
META_RESERVED,
|
||||
};
|
||||
use scale_info::{
|
||||
form::PortableForm,
|
||||
Type,
|
||||
Variant,
|
||||
};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
convert::TryFrom,
|
||||
sync::{
|
||||
Arc,
|
||||
RwLock,
|
||||
},
|
||||
};
|
||||
|
||||
/// Metadata error.
|
||||
#[derive(Debug, thiserror::Error, PartialEq)]
|
||||
pub enum MetadataError {
|
||||
/// Module is not in metadata.
|
||||
#[error("Pallet not found")]
|
||||
PalletNotFound,
|
||||
/// Pallet is not in metadata.
|
||||
#[error("Pallet index {0} not found")]
|
||||
PalletIndexNotFound(u8),
|
||||
/// Call is not in metadata.
|
||||
#[error("Call not found")]
|
||||
CallNotFound,
|
||||
/// Event is not in metadata.
|
||||
#[error("Pallet {0}, Event {0} not found")]
|
||||
EventNotFound(u8, u8),
|
||||
/// Event is not in metadata.
|
||||
#[error("Pallet {0}, Error {0} not found")]
|
||||
ErrorNotFound(u8, u8),
|
||||
/// Storage is not in metadata.
|
||||
#[error("Storage not found")]
|
||||
StorageNotFound,
|
||||
/// Storage type does not match requested type.
|
||||
#[error("Storage type error")]
|
||||
StorageTypeError,
|
||||
/// Default error.
|
||||
#[error("Failed to decode default: {0}")]
|
||||
DefaultError(CodecError),
|
||||
/// Failure to decode constant value.
|
||||
#[error("Failed to decode constant value: {0}")]
|
||||
ConstantValueError(CodecError),
|
||||
/// Constant is not in metadata.
|
||||
#[error("Constant not found")]
|
||||
ConstantNotFound,
|
||||
/// Type is not in metadata.
|
||||
#[error("Type {0} missing from type registry")]
|
||||
TypeNotFound(u32),
|
||||
/// Runtime pallet metadata is incompatible with the static one.
|
||||
#[error("Pallet {0} has incompatible metadata")]
|
||||
IncompatiblePalletMetadata(&'static str),
|
||||
/// Runtime metadata is not fully compatible with the static one.
|
||||
#[error("Node metadata is not fully compatible")]
|
||||
IncompatibleMetadata,
|
||||
}
|
||||
|
||||
/// Runtime metadata.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Metadata {
|
||||
inner: Arc<MetadataInner>,
|
||||
}
|
||||
|
||||
// We hide the innards behind an Arc so that it's easy to clone and share.
|
||||
#[derive(Debug)]
|
||||
struct MetadataInner {
|
||||
metadata: RuntimeMetadataV14,
|
||||
pallets: HashMap<String, PalletMetadata>,
|
||||
events: HashMap<(u8, u8), EventMetadata>,
|
||||
errors: HashMap<(u8, u8), ErrorMetadata>,
|
||||
// The hashes uniquely identify parts of the metadata; different
|
||||
// hashes mean some type difference exists between static and runtime
|
||||
// versions. We cache them here to avoid recalculating:
|
||||
cached_metadata_hash: RwLock<Option<[u8; 32]>>,
|
||||
cached_call_hashes: HashCache,
|
||||
cached_constant_hashes: HashCache,
|
||||
cached_storage_hashes: HashCache,
|
||||
}
|
||||
|
||||
impl Metadata {
|
||||
/// Returns a reference to [`PalletMetadata`].
|
||||
pub fn pallet(&self, name: &'static str) -> Result<&PalletMetadata, MetadataError> {
|
||||
self.inner
|
||||
.pallets
|
||||
.get(name)
|
||||
.ok_or(MetadataError::PalletNotFound)
|
||||
}
|
||||
|
||||
/// Returns the metadata for the event at the given pallet and event indices.
|
||||
pub fn event(
|
||||
&self,
|
||||
pallet_index: u8,
|
||||
event_index: u8,
|
||||
) -> Result<&EventMetadata, MetadataError> {
|
||||
let event = self
|
||||
.inner
|
||||
.events
|
||||
.get(&(pallet_index, event_index))
|
||||
.ok_or(MetadataError::EventNotFound(pallet_index, event_index))?;
|
||||
Ok(event)
|
||||
}
|
||||
|
||||
/// Returns the metadata for the error at the given pallet and error indices.
|
||||
pub fn error(
|
||||
&self,
|
||||
pallet_index: u8,
|
||||
error_index: u8,
|
||||
) -> Result<&ErrorMetadata, MetadataError> {
|
||||
let error = self
|
||||
.inner
|
||||
.errors
|
||||
.get(&(pallet_index, error_index))
|
||||
.ok_or(MetadataError::ErrorNotFound(pallet_index, error_index))?;
|
||||
Ok(error)
|
||||
}
|
||||
|
||||
/// Resolve a type definition.
|
||||
pub fn resolve_type(&self, id: u32) -> Option<&Type<PortableForm>> {
|
||||
self.inner.metadata.types.resolve(id)
|
||||
}
|
||||
|
||||
/// Return the runtime metadata.
|
||||
pub fn runtime_metadata(&self) -> &RuntimeMetadataV14 {
|
||||
&self.inner.metadata
|
||||
}
|
||||
|
||||
/// Obtain the unique hash for a specific storage entry.
|
||||
pub fn storage_hash<S: crate::StorageEntry>(
|
||||
&self,
|
||||
) -> Result<[u8; 32], MetadataError> {
|
||||
self.inner
|
||||
.cached_storage_hashes
|
||||
.get_or_insert(S::PALLET, S::STORAGE, || {
|
||||
subxt_metadata::get_storage_hash(
|
||||
&self.inner.metadata,
|
||||
S::PALLET,
|
||||
S::STORAGE,
|
||||
)
|
||||
.map_err(|e| {
|
||||
match e {
|
||||
subxt_metadata::NotFound::Pallet => MetadataError::PalletNotFound,
|
||||
subxt_metadata::NotFound::Item => MetadataError::StorageNotFound,
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/// Obtain the unique hash for a constant.
|
||||
pub fn constant_hash(
|
||||
&self,
|
||||
pallet: &str,
|
||||
constant: &str,
|
||||
) -> Result<[u8; 32], MetadataError> {
|
||||
self.inner
|
||||
.cached_constant_hashes
|
||||
.get_or_insert(pallet, constant, || {
|
||||
subxt_metadata::get_constant_hash(&self.inner.metadata, pallet, constant)
|
||||
.map_err(|e| {
|
||||
match e {
|
||||
subxt_metadata::NotFound::Pallet => {
|
||||
MetadataError::PalletNotFound
|
||||
}
|
||||
subxt_metadata::NotFound::Item => {
|
||||
MetadataError::ConstantNotFound
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/// Obtain the unique hash for a call.
|
||||
pub fn call_hash<C: crate::Call>(&self) -> Result<[u8; 32], MetadataError> {
|
||||
self.inner
|
||||
.cached_call_hashes
|
||||
.get_or_insert(C::PALLET, C::FUNCTION, || {
|
||||
subxt_metadata::get_call_hash(
|
||||
&self.inner.metadata,
|
||||
C::PALLET,
|
||||
C::FUNCTION,
|
||||
)
|
||||
.map_err(|e| {
|
||||
match e {
|
||||
subxt_metadata::NotFound::Pallet => MetadataError::PalletNotFound,
|
||||
subxt_metadata::NotFound::Item => MetadataError::CallNotFound,
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/// Obtain the unique hash for this metadata.
|
||||
pub fn metadata_hash<T: AsRef<str>>(&self, pallets: &[T]) -> [u8; 32] {
|
||||
if let Some(hash) = *self.inner.cached_metadata_hash.read().unwrap() {
|
||||
return hash
|
||||
}
|
||||
|
||||
let hash = subxt_metadata::get_metadata_per_pallet_hash(
|
||||
self.runtime_metadata(),
|
||||
pallets,
|
||||
);
|
||||
*self.inner.cached_metadata_hash.write().unwrap() = Some(hash);
|
||||
|
||||
hash
|
||||
}
|
||||
}
|
||||
|
||||
/// Metadata for a specific pallet.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PalletMetadata {
|
||||
index: u8,
|
||||
name: String,
|
||||
calls: HashMap<String, u8>,
|
||||
storage: HashMap<String, StorageEntryMetadata<PortableForm>>,
|
||||
constants: HashMap<String, PalletConstantMetadata<PortableForm>>,
|
||||
}
|
||||
|
||||
impl PalletMetadata {
|
||||
/// Get the name of the pallet.
|
||||
pub fn name(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
|
||||
/// Get the index of this pallet.
|
||||
pub fn index(&self) -> u8 {
|
||||
self.index
|
||||
}
|
||||
|
||||
/// Attempt to resolve a call into an index in this pallet, failing
|
||||
/// if the call is not found in this pallet.
|
||||
pub fn call_index<C>(&self) -> Result<u8, MetadataError>
|
||||
where
|
||||
C: Call,
|
||||
{
|
||||
let fn_index = *self
|
||||
.calls
|
||||
.get(C::FUNCTION)
|
||||
.ok_or(MetadataError::CallNotFound)?;
|
||||
Ok(fn_index)
|
||||
}
|
||||
|
||||
/// Return [`StorageEntryMetadata`] given some storage key.
|
||||
pub fn storage(
|
||||
&self,
|
||||
key: &str,
|
||||
) -> Result<&StorageEntryMetadata<PortableForm>, MetadataError> {
|
||||
self.storage.get(key).ok_or(MetadataError::StorageNotFound)
|
||||
}
|
||||
|
||||
/// Get a constant's metadata by name.
|
||||
pub fn constant(
|
||||
&self,
|
||||
key: &str,
|
||||
) -> Result<&PalletConstantMetadata<PortableForm>, MetadataError> {
|
||||
self.constants
|
||||
.get(key)
|
||||
.ok_or(MetadataError::ConstantNotFound)
|
||||
}
|
||||
}
|
||||
|
||||
/// Metadata for specific events.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct EventMetadata {
|
||||
pallet: String,
|
||||
event: String,
|
||||
variant: Variant<PortableForm>,
|
||||
}
|
||||
|
||||
impl EventMetadata {
|
||||
/// Get the name of the pallet from which the event was emitted.
|
||||
pub fn pallet(&self) -> &str {
|
||||
&self.pallet
|
||||
}
|
||||
|
||||
/// Get the name of the pallet event which was emitted.
|
||||
pub fn event(&self) -> &str {
|
||||
&self.event
|
||||
}
|
||||
|
||||
/// Get the type def variant for the pallet event.
|
||||
pub fn variant(&self) -> &Variant<PortableForm> {
|
||||
&self.variant
|
||||
}
|
||||
}
|
||||
|
||||
/// Metadata for specific errors.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ErrorMetadata {
|
||||
pallet: String,
|
||||
error: String,
|
||||
variant: Variant<PortableForm>,
|
||||
}
|
||||
|
||||
impl ErrorMetadata {
|
||||
/// Get the name of the pallet from which the error originates.
|
||||
pub fn pallet(&self) -> &str {
|
||||
&self.pallet
|
||||
}
|
||||
|
||||
/// Get the name of the specific pallet error.
|
||||
pub fn error(&self) -> &str {
|
||||
&self.error
|
||||
}
|
||||
|
||||
/// Get the description of the specific pallet error.
|
||||
pub fn description(&self) -> &[String] {
|
||||
self.variant.docs()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum InvalidMetadataError {
|
||||
#[error("Invalid prefix")]
|
||||
InvalidPrefix,
|
||||
#[error("Invalid version")]
|
||||
InvalidVersion,
|
||||
#[error("Type {0} missing from type registry")]
|
||||
MissingType(u32),
|
||||
#[error("Type {0} was not a variant/enum type")]
|
||||
TypeDefNotVariant(u32),
|
||||
}
|
||||
|
||||
impl TryFrom<RuntimeMetadataPrefixed> for Metadata {
|
||||
type Error = InvalidMetadataError;
|
||||
|
||||
fn try_from(metadata: RuntimeMetadataPrefixed) -> Result<Self, Self::Error> {
|
||||
if metadata.0 != META_RESERVED {
|
||||
return Err(InvalidMetadataError::InvalidPrefix)
|
||||
}
|
||||
let metadata = match metadata.1 {
|
||||
RuntimeMetadata::V14(meta) => meta,
|
||||
_ => return Err(InvalidMetadataError::InvalidVersion),
|
||||
};
|
||||
|
||||
let get_type_def_variant = |type_id: u32| {
|
||||
let ty = metadata
|
||||
.types
|
||||
.resolve(type_id)
|
||||
.ok_or(InvalidMetadataError::MissingType(type_id))?;
|
||||
if let scale_info::TypeDef::Variant(var) = ty.type_def() {
|
||||
Ok(var)
|
||||
} else {
|
||||
Err(InvalidMetadataError::TypeDefNotVariant(type_id))
|
||||
}
|
||||
};
|
||||
let pallets = metadata
|
||||
.pallets
|
||||
.iter()
|
||||
.map(|pallet| {
|
||||
let calls = pallet.calls.as_ref().map_or(Ok(HashMap::new()), |call| {
|
||||
let type_def_variant = get_type_def_variant(call.ty.id())?;
|
||||
let calls = type_def_variant
|
||||
.variants()
|
||||
.iter()
|
||||
.map(|v| (v.name().clone(), v.index()))
|
||||
.collect();
|
||||
Ok(calls)
|
||||
})?;
|
||||
|
||||
let storage = pallet.storage.as_ref().map_or(HashMap::new(), |storage| {
|
||||
storage
|
||||
.entries
|
||||
.iter()
|
||||
.map(|entry| (entry.name.clone(), entry.clone()))
|
||||
.collect()
|
||||
});
|
||||
|
||||
let constants = pallet
|
||||
.constants
|
||||
.iter()
|
||||
.map(|constant| (constant.name.clone(), constant.clone()))
|
||||
.collect();
|
||||
|
||||
let pallet_metadata = PalletMetadata {
|
||||
index: pallet.index,
|
||||
name: pallet.name.to_string(),
|
||||
calls,
|
||||
storage,
|
||||
constants,
|
||||
};
|
||||
|
||||
Ok((pallet.name.to_string(), pallet_metadata))
|
||||
})
|
||||
.collect::<Result<_, _>>()?;
|
||||
|
||||
let pallet_events = metadata
|
||||
.pallets
|
||||
.iter()
|
||||
.filter_map(|pallet| {
|
||||
pallet.event.as_ref().map(|event| {
|
||||
let type_def_variant = get_type_def_variant(event.ty.id())?;
|
||||
Ok((pallet, type_def_variant))
|
||||
})
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
let events = pallet_events
|
||||
.iter()
|
||||
.flat_map(|(pallet, type_def_variant)| {
|
||||
type_def_variant.variants().iter().map(move |var| {
|
||||
let key = (pallet.index, var.index());
|
||||
let value = EventMetadata {
|
||||
pallet: pallet.name.clone(),
|
||||
event: var.name().clone(),
|
||||
variant: var.clone(),
|
||||
};
|
||||
(key, value)
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
|
||||
let pallet_errors = metadata
|
||||
.pallets
|
||||
.iter()
|
||||
.filter_map(|pallet| {
|
||||
pallet.error.as_ref().map(|error| {
|
||||
let type_def_variant = get_type_def_variant(error.ty.id())?;
|
||||
Ok((pallet, type_def_variant))
|
||||
})
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
let errors = pallet_errors
|
||||
.iter()
|
||||
.flat_map(|(pallet, type_def_variant)| {
|
||||
type_def_variant.variants().iter().map(move |var| {
|
||||
let key = (pallet.index, var.index());
|
||||
let value = ErrorMetadata {
|
||||
pallet: pallet.name.clone(),
|
||||
error: var.name().clone(),
|
||||
variant: var.clone(),
|
||||
};
|
||||
(key, value)
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok(Metadata {
|
||||
inner: Arc::new(MetadataInner {
|
||||
metadata,
|
||||
pallets,
|
||||
events,
|
||||
errors,
|
||||
cached_metadata_hash: Default::default(),
|
||||
cached_call_hashes: Default::default(),
|
||||
cached_constant_hashes: Default::default(),
|
||||
cached_storage_hashes: Default::default(),
|
||||
}),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::StorageEntryKey;
|
||||
|
||||
fn load_metadata() -> Metadata {
|
||||
let bytes = test_runtime::METADATA;
|
||||
let meta: RuntimeMetadataPrefixed =
|
||||
codec::Decode::decode(&mut &*bytes).expect("Cannot decode scale metadata");
|
||||
|
||||
Metadata::try_from(meta)
|
||||
.expect("Cannot translate runtime metadata to internal Metadata")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn metadata_inner_cache() {
|
||||
// Note: Dependency on test_runtime can be removed if complex metadata
|
||||
// is manually constructed.
|
||||
let metadata = load_metadata();
|
||||
|
||||
let hash = metadata.metadata_hash(&["System"]);
|
||||
// Check inner caching.
|
||||
assert_eq!(
|
||||
metadata.inner.cached_metadata_hash.read().unwrap().unwrap(),
|
||||
hash
|
||||
);
|
||||
|
||||
// Currently the caching does not take into account different pallets
|
||||
// as the intended behavior is to use this method only once.
|
||||
// Enforce this behavior into testing.
|
||||
let hash_old = metadata.metadata_hash(&["Balances"]);
|
||||
assert_eq!(hash_old, hash);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn metadata_call_inner_cache() {
|
||||
let metadata = load_metadata();
|
||||
|
||||
#[derive(codec::Encode)]
|
||||
struct ValidCall;
|
||||
impl crate::Call for ValidCall {
|
||||
const PALLET: &'static str = "System";
|
||||
const FUNCTION: &'static str = "fill_block";
|
||||
}
|
||||
|
||||
let hash = metadata.call_hash::<ValidCall>();
|
||||
|
||||
let mut call_number = 0;
|
||||
let hash_cached = metadata.inner.cached_call_hashes.get_or_insert(
|
||||
"System",
|
||||
"fill_block",
|
||||
|| -> Result<[u8; 32], MetadataError> {
|
||||
call_number += 1;
|
||||
Ok([0; 32])
|
||||
},
|
||||
);
|
||||
|
||||
// Check function is never called (e.i, value fetched from cache).
|
||||
assert_eq!(call_number, 0);
|
||||
assert_eq!(hash.unwrap(), hash_cached.unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn metadata_constant_inner_cache() {
|
||||
let metadata = load_metadata();
|
||||
|
||||
let hash = metadata.constant_hash("System", "BlockWeights");
|
||||
|
||||
let mut call_number = 0;
|
||||
let hash_cached = metadata.inner.cached_constant_hashes.get_or_insert(
|
||||
"System",
|
||||
"BlockWeights",
|
||||
|| -> Result<[u8; 32], MetadataError> {
|
||||
call_number += 1;
|
||||
Ok([0; 32])
|
||||
},
|
||||
);
|
||||
|
||||
// Check function is never called (e.i, value fetched from cache).
|
||||
assert_eq!(call_number, 0);
|
||||
assert_eq!(hash.unwrap(), hash_cached.unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn metadata_storage_inner_cache() {
|
||||
let metadata = load_metadata();
|
||||
|
||||
#[derive(codec::Encode)]
|
||||
struct ValidStorage;
|
||||
impl crate::StorageEntry for ValidStorage {
|
||||
const PALLET: &'static str = "System";
|
||||
const STORAGE: &'static str = "Account";
|
||||
type Value = ();
|
||||
|
||||
fn key(&self) -> StorageEntryKey {
|
||||
unreachable!("Should not be called");
|
||||
}
|
||||
}
|
||||
|
||||
let hash = metadata.storage_hash::<ValidStorage>();
|
||||
|
||||
let mut call_number = 0;
|
||||
let hash_cached = metadata.inner.cached_storage_hashes.get_or_insert(
|
||||
"System",
|
||||
"Account",
|
||||
|| -> Result<[u8; 32], MetadataError> {
|
||||
call_number += 1;
|
||||
Ok([0; 32])
|
||||
},
|
||||
);
|
||||
|
||||
// Check function is never called (e.i, value fetched from cache).
|
||||
assert_eq!(call_number, 0);
|
||||
assert_eq!(hash.unwrap(), hash_cached.unwrap());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// Copyright 2019-2022 Parity Technologies (UK) Ltd.
|
||||
// This file is part of subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
mod hash_cache;
|
||||
mod metadata_type;
|
||||
|
||||
pub use metadata_type::{
|
||||
ErrorMetadata,
|
||||
EventMetadata,
|
||||
InvalidMetadataError,
|
||||
Metadata,
|
||||
MetadataError,
|
||||
PalletMetadata,
|
||||
};
|
||||
Reference in New Issue
Block a user