-
Notifications
You must be signed in to change notification settings - Fork 2.6k
support upgrade hooks to directly pass data #12185
support upgrade hooks to directly pass data #12185
Conversation
Signed-off-by: linning <linningde25@gmail.com>
Signed-off-by: linning <linningde25@gmail.com>
Signed-off-by: linning <linningde25@gmail.com>
Signed-off-by: linning <linningde25@gmail.com>
Signed-off-by: linning <linningde25@gmail.com>
Signed-off-by: linning <linningde25@gmail.com>
Signed-off-by: linning <linningde25@gmail.com>
frame/support/src/traits/hooks.rs
Outdated
/// TODO: add #[cfg(feature = "try-runtime")], which required changing a lot of `Cargo.toml` | ||
/// files to recorrect features dependencies |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is because almost all crate's try-runtime
feature didn't contain frame-system/try-runtime
, and there is no default implement for PreStateDigest
and pre_upgrade
, thus when compiling these crates errors like frame-system
missing trait items will return.
frame/support/src/traits/hooks.rs
Outdated
} | ||
/// TODO: add #[cfg(feature = "try-runtime")], which required changing a lot of `Cargo.toml` | ||
/// files to recorrect features dependencies | ||
fn pre_upgrade() -> Result<Self::PreStateDigest, &'static str>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had tried to provide a default implement here by PreStateDigest: Default
and return Ok(Self:: PreStateDigest::default())
. But it failed to implement OnRuntimeUpgrade for Tuple
because it has PreStateDigest: (P1, P2, ..)
and rust only implement Default
for tuples up to 12 elements, also #11813 had changed the nested tuple to a flatten one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, that is one issue that we may have intorduced with flatten tuples.
Signed-off-by: linning <linningde25@gmail.com>
frame/support/src/traits/hooks.rs
Outdated
/// TODO: add #[cfg(feature = "try-runtime")], which required changing a lot of `Cargo.toml` | ||
/// files to recorrect features dependencies |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is fine. It just shows that the current code is "wrong"
frame/alliance/src/migration.rs
Outdated
@@ -41,9 +41,15 @@ pub fn migrate<T: Config<I>, I: 'static>() -> Weight { | |||
pub struct Migration<T, I = ()>(PhantomData<(T, I)>); | |||
|
|||
impl<T: Config<I>, I: 'static> OnRuntimeUpgrade for Migration<T, I> { | |||
type PreStateDigest = (); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we make this type be two different generics that FullCodec
in each function, as the issue recommended? Having to define this type also increases the footprint of this PR a lot.
Something like:
fn pre_upgrade<I: Encode>() -> Result<I, ..> {}
fn post_upgrade<I: Decode>(input: I) {}
and we encode and pass the data between the hooks automatically.
This is more or less what we do now with OnRuntimeUpgradeHelpersExt
, and if we do this we can remove this extension trait + make sure the state root is not changing in the pre/post migrate hooks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can't do that, the type parameters of a generic function are instantiated when calling the function not when we implement traits (defining the function).
Using concrete types like:
fn pre_upgrade() -> Result<Vec<u8>, ..> {}
fn post_upgrade(input: Vec<u8>) {}
may work though, but requiring to do encode/decode within pre_upgrade/post_upgrade
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can't do that, the type parameters of a generic function are instantiated when calling the function not when we implement traits (defining the function).
Not sure if this is an issue, can you give it a try?
All in all, I don't want to pollute the code too much for this feature. In rare cases we might need to pass data between the two hooks. Let's just make it Vec<u8>
(or T: Codec
) and move on without making a big change.
Making sure that the hooks don't alter storage is a good change though, let's keep that part.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I had tried,
impl OnRuntimeUpgrade for SomeType {
fn pre_upgrade<u32>() -> Result<u32, &'static str> {
...
}
}
the compiler will recognize u32
as a type parameter instead of a concrete type.
I will try to it Vec<u8>
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will try to it Vec.
I really made progress on this approach, except for one (perhaps final) issue that OnRuntimeUpgrade for Tuple
failed due to Codec
did not implement for tuple with elements more than 18. This may be related to https://github.com/paritytech/parity-scale-codec/blob/46859b803c94f091e3ca644d8468bfadfbd2c539/src/max_encoded_len.rs#L79-L81
It work now, PTAL
Signed-off-by: linning <linningde25@gmail.com>
Signed-off-by: linning <linningde25@gmail.com>
Signed-off-by: linning <linningde25@gmail.com>
Signed-off-by: linning <linningde25@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks in the right direction, but in general turned out a bit more complicated than anticipated.
Signed-off-by: linning <linningde25@gmail.com>
Yes, it is a bit complicated, especially |
I think the current state of the PR is good though, not too much complexity, we can allow a WDYT @NingLin-P? I will do a last review later. |
Signed-off-by: linning <linningde25@gmail.com>
Signed-off-by: linning <linningde25@gmail.com>
Signed-off-by: linning <linningde25@gmail.com>
Yes, I agree. |
Signed-off-by: linning <linningde25@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, Thanks!
Polkadot address? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quick nit. Otherwise LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, would like to see some more detailed docs as mentioned by other reviewers ;)
Signed-off-by: linning <linningde25@gmail.com>
bot merge |
/tip small |
@kianenigma A small tip was successfully submitted for NingLin-P (1CWyso9Zw46QdR54sTvKJXTUdmZznYZa2aJfNHdG6xQ6L71 on polkadot). https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Frpc.polkadot.io#/treasury/tips |
* update interfaces of OnRuntimeUpgrade & Hooks Signed-off-by: linning <linningde25@gmail.com> * remove try-runtime for PreStateDigest Signed-off-by: linning <linningde25@gmail.com> * remove the Default bound of PreStateDigest Signed-off-by: linning <linningde25@gmail.com> * remove try-runtime for PreStateDigest & pre_upgrade Signed-off-by: linning <linningde25@gmail.com> * remove tmp storage between upgrade hooks Signed-off-by: linning <linningde25@gmail.com> * ensure hooks are storage noop Signed-off-by: linning <linningde25@gmail.com> * remove OnRuntimeUpgradeHelpersExt Signed-off-by: linning <linningde25@gmail.com> * cargo check & fmt Signed-off-by: linning <linningde25@gmail.com> * rename PreStateDigest to PreUpgradeState Signed-off-by: linning <linningde25@gmail.com> * replace associate type with codec & vec Signed-off-by: linning <linningde25@gmail.com> * add helper strcut to help encode/decode tuple Signed-off-by: linning <linningde25@gmail.com> * update comment Signed-off-by: linning <linningde25@gmail.com> * fix Signed-off-by: linning <linningde25@gmail.com> * add test Signed-off-by: linning <linningde25@gmail.com> * address comment Signed-off-by: linning <linningde25@gmail.com> * fix doc Signed-off-by: linning <linningde25@gmail.com> * fix ci Signed-off-by: linning <linningde25@gmail.com> * address comment Signed-off-by: linning <linningde25@gmail.com> * add more test cases Signed-off-by: linning <linningde25@gmail.com> * make clippy happy Signed-off-by: linning <linningde25@gmail.com> * fmt Signed-off-by: linning <linningde25@gmail.com> * update comment Signed-off-by: linning <linningde25@gmail.com> * fmt Signed-off-by: linning <linningde25@gmail.com> Signed-off-by: linning <linningde25@gmail.com>
* add files * add local script as well. * remove dep for now.. * a few fixes from Kian * merge revert * Fix more links * fix crates.io * removed the doc for OnRuntimeUpgradeHelpers (removed on PR paritytech/substrate#12185) * Fix some more links * remove non-existent pallets * Fix one more link * Fix one more link * add some more configuration * ignore internal links * Fix config * Fix internallinks * fix more internall links * Ignore crates.io links * Address comments * Make all internal links relative * fmt --------- Co-authored-by: wirednkod <wirednkod@gmail.com>
Attempt to fix #10824
polkadot address: 1CWyso9Zw46QdR54sTvKJXTUdmZznYZa2aJfNHdG6xQ6L71