Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

support upgrade hooks to directly pass data #12185

Merged
merged 26 commits into from
Sep 19, 2022

Conversation

NingLin-P
Copy link
Contributor

@NingLin-P NingLin-P commented Sep 4, 2022

Attempt to fix #10824

polkadot address: 1CWyso9Zw46QdR54sTvKJXTUdmZznYZa2aJfNHdG6xQ6L71

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>
Comment on lines 128 to 129
/// TODO: add #[cfg(feature = "try-runtime")], which required changing a lot of `Cargo.toml`
/// files to recorrect features dependencies
Copy link
Contributor Author

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.

}
/// 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>;
Copy link
Contributor Author

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.

Copy link
Contributor

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 Show resolved Hide resolved
Comment on lines 152 to 153
/// TODO: add #[cfg(feature = "try-runtime")], which required changing a lot of `Cargo.toml`
/// files to recorrect features dependencies
Copy link
Member

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"

@@ -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 = ();
Copy link
Contributor

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.

Copy link
Contributor Author

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

Copy link
Contributor

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.

Copy link
Contributor Author

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>.

Copy link
Contributor Author

@NingLin-P NingLin-P Sep 6, 2022

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>
Signed-off-by: linning <linningde25@gmail.com>
Signed-off-by: linning <linningde25@gmail.com>
@kianenigma kianenigma added A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. labels Sep 8, 2022
Copy link
Contributor

@kianenigma kianenigma left a 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>
@NingLin-P
Copy link
Contributor Author

Looks in the right direction, but in general turned out a bit more complicated than anticipated.

Yes, it is a bit complicated, especially impl OnRuntimeUpgrade for Tuple. This PR could be trivial if we can use the unstable associated_type_defaults feature and the nested tuple is still in use. Then we can provide a total default implement for the OnRuntimeUpgrade trait and also make the tuple implement working, thus without introducing any redundant code.

Signed-off-by: linning <linningde25@gmail.com>
@kianenigma
Copy link
Contributor

Yes, it is a bit complicated, especially impl OnRuntimeUpgrade for Tuple. This PR could be trivial if we can use the unstable associated_type_defaults feature and the nested tuple is still in use. Then we can provide a total default implement for the OnRuntimeUpgrade trait and also make the tuple implement working, thus without introducing any redundant code.

I think the current state of the PR is good though, not too much complexity, we can allow a Vec<u8> to be passed in and out, guard is there, and not much complexity is added.

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>
@NingLin-P
Copy link
Contributor Author

I think the current state of the PR is good though, not too much complexity, we can allow a Vec<u8> to be passed in and out, guard is there, and not much complexity is added.

WDYT @NingLin-P? I will do a last review later.

Yes, I agree.

Signed-off-by: linning <linningde25@gmail.com>
Signed-off-by: linning <linningde25@gmail.com>
Copy link
Contributor

@kianenigma kianenigma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, Thanks!

@kianenigma
Copy link
Contributor

Polkadot address?

@kianenigma kianenigma requested a review from bkchr September 14, 2022 15:21
@kianenigma kianenigma added the D2-notlive 💤 PR contains changes in a runtime directory that is not deployed to a chain that requires an audit. label Sep 16, 2022
Copy link
Contributor

@BustaNit BustaNit left a 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

frame/bags-list/src/migrations.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@sam0x17 sam0x17 left a 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 ;)

frame/executive/src/lib.rs Outdated Show resolved Hide resolved
frame/bags-list/src/migrations.rs Outdated Show resolved Hide resolved
utils/frame/try-runtime/cli/src/lib.rs Outdated Show resolved Hide resolved
Signed-off-by: linning <linningde25@gmail.com>
Signed-off-by: linning <linningde25@gmail.com>
@kianenigma
Copy link
Contributor

bot merge

@paritytech-processbot paritytech-processbot bot merged commit ee3c159 into paritytech:master Sep 19, 2022
@kianenigma
Copy link
Contributor

/tip small

@substrate-tip-bot
Copy link

@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

ark0f pushed a commit to gear-tech/substrate that referenced this pull request Feb 27, 2023
* 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>
@zqhxuyuan zqhxuyuan mentioned this pull request Mar 13, 2023
12 tasks
wirednkod added a commit to polkadot-developers/substrate-docs that referenced this pull request Apr 27, 2023
kianenigma added a commit to polkadot-developers/substrate-docs that referenced this pull request May 7, 2023
* 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>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D2-notlive 💤 PR contains changes in a runtime directory that is not deployed to a chain that requires an audit.
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Add ability for try runtime hooks to directly pass data
6 participants