From c57775d747789ff4ef83443cd48802f1220afe1a Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Fri, 15 Dec 2023 18:15:53 +0400 Subject: [PATCH 1/7] improve decode err output --- Cargo.lock | 1 + substrate/frame/support/Cargo.toml | 1 + .../traits/try_runtime/decode_entire_state.rs | 44 ++++++++++++++----- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f858df0abe8e..2f82f7dca64d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5520,6 +5520,7 @@ dependencies = [ "frame-metadata", "frame-support-procedural", "frame-system", + "hex", "impl-trait-for-tuples", "k256", "log", diff --git a/substrate/frame/support/Cargo.toml b/substrate/frame/support/Cargo.toml index 07f9075c82b3..003327778846 100644 --- a/substrate/frame/support/Cargo.toml +++ b/substrate/frame/support/Cargo.toml @@ -34,6 +34,7 @@ sp-weights = { path = "../../primitives/weights", default-features = false } sp-debug-derive = { path = "../../primitives/debug-derive", default-features = false } sp-metadata-ir = { path = "../../primitives/metadata-ir", default-features = false } tt-call = "1.0.8" +hex = { default-features = false, version = "0.4.3", features = ["alloc"] } macro_magic = "0.5.0" frame-support-procedural = { path = "procedural", default-features = false } paste = "1.0" diff --git a/substrate/frame/support/src/traits/try_runtime/decode_entire_state.rs b/substrate/frame/support/src/traits/try_runtime/decode_entire_state.rs index afbf291a0bbb..28eb8f1df56b 100644 --- a/substrate/frame/support/src/traits/try_runtime/decode_entire_state.rs +++ b/substrate/frame/support/src/traits/try_runtime/decode_entire_state.rs @@ -67,7 +67,7 @@ impl TryDecodeEntireStorage for Tuple { } /// A value could not be decoded. -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Clone, PartialEq, Eq)] pub struct TryDecodeEntireStorageError { /// The key of the undecodable value. pub key: Vec, @@ -81,9 +81,22 @@ impl core::fmt::Display for TryDecodeEntireStorageError { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!( f, - "Failed to decode storage item `{}::{}`", + "`{}::{}` key `{}` is undecodable", &sp_std::str::from_utf8(&self.info.pallet_name).unwrap_or(""), &sp_std::str::from_utf8(&self.info.storage_name).unwrap_or(""), + hex::encode(&self.key) + ) + } +} + +impl core::fmt::Debug for TryDecodeEntireStorageError { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!( + f, + "key: {} value: {} info: {:?}", + hex::encode(&self.key), + hex::encode(self.raw.clone().unwrap_or_default()), + self.info ) } } @@ -103,30 +116,41 @@ fn decode_storage_info( None => Ok(0), Some(bytes) => { let len = bytes.len(); - let _ = ::decode_all(&mut bytes.as_ref()).map_err(|_| { - vec![TryDecodeEntireStorageError { + if let Err(e) = ::decode_all(&mut bytes.as_ref()).map_err(|_| { + TryDecodeEntireStorageError { key: key.to_vec(), raw: Some(bytes.to_vec()), info: info.clone(), - }] - })?; + } + }) { + return Err(e); + }; - Ok::>(len) + Ok::(len) }, }; - decoded += decode_key(&next_key)?; + let mut errors = vec![]; loop { match sp_io::storage::next_key(&next_key) { Some(key) if key.starts_with(&info.prefix) => { - decoded += decode_key(&key)?; + match decode_key(&key) { + Ok(bytes) => { + decoded += bytes; + }, + Err(e) => errors.push(e), + } next_key = key; }, _ => break, } } - Ok(decoded) + if errors.is_empty() { + Ok(decoded) + } else { + Err(errors) + } } impl TryDecodeEntireStorage From d1de7acc2baf994f70cecdd28f8500bfdf36d49d Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Fri, 15 Dec 2023 20:28:00 +0400 Subject: [PATCH 2/7] ? syntax --- .../support/src/traits/try_runtime/decode_entire_state.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/substrate/frame/support/src/traits/try_runtime/decode_entire_state.rs b/substrate/frame/support/src/traits/try_runtime/decode_entire_state.rs index 28eb8f1df56b..f497f6ee654f 100644 --- a/substrate/frame/support/src/traits/try_runtime/decode_entire_state.rs +++ b/substrate/frame/support/src/traits/try_runtime/decode_entire_state.rs @@ -122,9 +122,7 @@ fn decode_storage_info( raw: Some(bytes.to_vec()), info: info.clone(), } - }) { - return Err(e); - }; + })?; Ok::(len) }, From 587c57bb6dac3b50f816940579b7874cbaf8f7b5 Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Fri, 15 Dec 2023 20:29:13 +0400 Subject: [PATCH 3/7] clean up --- .../frame/support/src/traits/try_runtime/decode_entire_state.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/substrate/frame/support/src/traits/try_runtime/decode_entire_state.rs b/substrate/frame/support/src/traits/try_runtime/decode_entire_state.rs index f497f6ee654f..5f0b79273baf 100644 --- a/substrate/frame/support/src/traits/try_runtime/decode_entire_state.rs +++ b/substrate/frame/support/src/traits/try_runtime/decode_entire_state.rs @@ -116,7 +116,7 @@ fn decode_storage_info( None => Ok(0), Some(bytes) => { let len = bytes.len(); - if let Err(e) = ::decode_all(&mut bytes.as_ref()).map_err(|_| { + let _ = ::decode_all(&mut bytes.as_ref()).map_err(|_| { TryDecodeEntireStorageError { key: key.to_vec(), raw: Some(bytes.to_vec()), From 28404111e166360972ff48b993a4694237e33c18 Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Mon, 18 Dec 2023 11:35:22 +0400 Subject: [PATCH 4/7] fix missing first key --- .../support/src/traits/try_runtime/decode_entire_state.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/substrate/frame/support/src/traits/try_runtime/decode_entire_state.rs b/substrate/frame/support/src/traits/try_runtime/decode_entire_state.rs index 5f0b79273baf..b1debaddd217 100644 --- a/substrate/frame/support/src/traits/try_runtime/decode_entire_state.rs +++ b/substrate/frame/support/src/traits/try_runtime/decode_entire_state.rs @@ -109,7 +109,6 @@ impl core::fmt::Debug for TryDecodeEntireStorageError { fn decode_storage_info( info: StorageInfo, ) -> Result> { - let mut next_key = info.prefix.clone(); let mut decoded = 0; let decode_key = |key: &[u8]| match sp_io::storage::get(key) { @@ -129,16 +128,17 @@ fn decode_storage_info( }; let mut errors = vec![]; + let mut next_key = Some(info.prefix.clone()); loop { - match sp_io::storage::next_key(&next_key) { + match next_key { Some(key) if key.starts_with(&info.prefix) => { match decode_key(&key) { Ok(bytes) => { decoded += bytes; }, Err(e) => errors.push(e), - } - next_key = key; + }; + next_key = sp_io::storage::next_key(&key); }, _ => break, } From cd673c5281b3b9b6ba2ea71d606118d6ff496f11 Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Mon, 18 Dec 2023 11:59:33 +0400 Subject: [PATCH 5/7] remove hex and add test --- Cargo.lock | 1 - substrate/frame/support/Cargo.toml | 1 - .../src/traits/try_runtime/decode_entire_state.rs | 12 +++++++++--- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2f82f7dca64d..f858df0abe8e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5520,7 +5520,6 @@ dependencies = [ "frame-metadata", "frame-support-procedural", "frame-system", - "hex", "impl-trait-for-tuples", "k256", "log", diff --git a/substrate/frame/support/Cargo.toml b/substrate/frame/support/Cargo.toml index 003327778846..07f9075c82b3 100644 --- a/substrate/frame/support/Cargo.toml +++ b/substrate/frame/support/Cargo.toml @@ -34,7 +34,6 @@ sp-weights = { path = "../../primitives/weights", default-features = false } sp-debug-derive = { path = "../../primitives/debug-derive", default-features = false } sp-metadata-ir = { path = "../../primitives/metadata-ir", default-features = false } tt-call = "1.0.8" -hex = { default-features = false, version = "0.4.3", features = ["alloc"] } macro_magic = "0.5.0" frame-support-procedural = { path = "procedural", default-features = false } paste = "1.0" diff --git a/substrate/frame/support/src/traits/try_runtime/decode_entire_state.rs b/substrate/frame/support/src/traits/try_runtime/decode_entire_state.rs index b1debaddd217..d5dc93fcf28f 100644 --- a/substrate/frame/support/src/traits/try_runtime/decode_entire_state.rs +++ b/substrate/frame/support/src/traits/try_runtime/decode_entire_state.rs @@ -84,7 +84,7 @@ impl core::fmt::Display for TryDecodeEntireStorageError { "`{}::{}` key `{}` is undecodable", &sp_std::str::from_utf8(&self.info.pallet_name).unwrap_or(""), &sp_std::str::from_utf8(&self.info.storage_name).unwrap_or(""), - hex::encode(&self.key) + array_bytes::bytes2hex("0x", &self.key) ) } } @@ -94,8 +94,8 @@ impl core::fmt::Debug for TryDecodeEntireStorageError { write!( f, "key: {} value: {} info: {:?}", - hex::encode(&self.key), - hex::encode(self.raw.clone().unwrap_or_default()), + array_bytes::bytes2hex("0x", &self.key), + array_bytes::bytes2hex("0x", self.raw.clone().unwrap_or_default()), self.info ) } @@ -375,6 +375,12 @@ mod tests { // two bytes, cannot be decoded into u32. sp_io::storage::set(&Map::hashed_key_for(2), &[0u8, 1]); assert!(Map::try_decode_entire_state().is_err()); + assert_eq!(Map::try_decode_entire_state().unwrap_err().len(), 1); + + // multiple errs in the same map are be detected + sp_io::storage::set(&Map::hashed_key_for(3), &[0u8, 1]); + assert!(Map::try_decode_entire_state().is_err()); + assert_eq!(Map::try_decode_entire_state().unwrap_err().len(), 2); }) } From 048fe642b0dbe64b20ba557449dc68b2d98eed54 Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Wed, 20 Dec 2023 12:10:23 +0400 Subject: [PATCH 6/7] improve log --- substrate/frame/executive/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/substrate/frame/executive/src/lib.rs b/substrate/frame/executive/src/lib.rs index b351819f612b..3faeba8d0795 100644 --- a/substrate/frame/executive/src/lib.rs +++ b/substrate/frame/executive/src/lib.rs @@ -409,9 +409,9 @@ where ) -> Result<(), TryRuntimeError> { match res { Ok(bytes) => { - log::debug!( + log::info!( target: LOG_TARGET, - "decoded the entire state ({bytes} bytes)", + "✅ Entire runtime state decodes without error. {} bytes total.", ); Ok(()) From 7dae7a0040bce04a77da96e0ae2b5b347115de85 Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Wed, 20 Dec 2023 12:18:25 +0400 Subject: [PATCH 7/7] fix --- substrate/frame/executive/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/substrate/frame/executive/src/lib.rs b/substrate/frame/executive/src/lib.rs index 3faeba8d0795..f2cc3b631688 100644 --- a/substrate/frame/executive/src/lib.rs +++ b/substrate/frame/executive/src/lib.rs @@ -412,6 +412,7 @@ where log::info!( target: LOG_TARGET, "✅ Entire runtime state decodes without error. {} bytes total.", + bytes ); Ok(())