Metadata difference command (#1015)

* diffing pallets and runtime apis

* print diff

* clippy fix and format

* change formatting

* fmt

* diff working with storage details

* fix diff

* cargo fmt

* remove printing of node

* change strings

* handle parsing differently

* clippy fix

* cargo fmt

* more abstraction

* clippy fix and fmt

* add unit test and ordering

* fix small issue
This commit is contained in:
Tadeo Hepperle
2023-06-21 14:33:21 +02:00
committed by GitHub
parent b4eb406ee5
commit 2a990edaca
9 changed files with 541 additions and 23 deletions
+17 -10
View File
@@ -175,7 +175,7 @@ fn get_type_def_hash(
}
/// Obtain the hash representation of a `scale_info::Type` identified by id.
fn get_type_hash(
pub fn get_type_hash(
registry: &PortableRegistry,
id: u32,
visited_ids: &mut HashSet<u32>,
@@ -283,7 +283,7 @@ fn get_runtime_method_hash(
}
/// 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] {
pub 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
@@ -379,14 +379,17 @@ pub fn get_pallet_hash(pallet: PalletMetadata) -> [u8; HASH_LEN] {
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),
)
});
let entries_hash = storage
.entries()
.iter()
.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],
@@ -496,6 +499,7 @@ mod tests {
struct A {
pub b: Box<B>,
}
#[allow(dead_code)]
#[derive(scale_info::TypeInfo)]
struct B {
@@ -507,6 +511,7 @@ mod tests {
#[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.
@@ -525,6 +530,7 @@ mod tests {
// TypeDef::BitSequence.
BitSeq(BitVec<u8, Lsb0>),
}
#[allow(dead_code)]
#[derive(scale_info::TypeInfo)]
// Ensure recursive types and TypeDef variants are captured.
@@ -533,6 +539,7 @@ mod tests {
composite: AccountId32,
type_def: DigestItem,
}
#[allow(dead_code)]
#[derive(scale_info::TypeInfo)]
// Simulate a PalletCallMetadata.