Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove sp_runtime::RuntimeString and replace with Cow<'static, str> or String depending on use case #5693

Merged
merged 11 commits into from
Nov 5, 2024

Conversation

nazar-pc
Copy link
Contributor

@nazar-pc nazar-pc commented Sep 12, 2024

Description

As described in #4001 RuntimeVersion was not encoded consistently using serde. Turned out it was a remnant of old times and no longer actually needed. As such I removed it completely in this PR and replaced with Cow<'static, str> for spec/impl names and String for error cases.

Fixes #4001.

Integration

For downstream projects the upgrade will primarily consist of following two changes:

#[sp_version::runtime_version]
pub const VERSION: RuntimeVersion = RuntimeVersion {
-	spec_name: create_runtime_str!("statemine"),
-	impl_name: create_runtime_str!("statemine"),
+	spec_name: alloc::borrow::Cow::Borrowed("statemine"),
+	impl_name: alloc::borrow::Cow::Borrowed("statemine"),
		fn dispatch_benchmark(
			config: frame_benchmarking::BenchmarkConfig
-		) -> Result<Vec<frame_benchmarking::BenchmarkBatch>, sp_runtime::RuntimeString> {
+		) -> Result<Vec<frame_benchmarking::BenchmarkBatch>, alloc::string::String> {

SCALE encoding/decoding remains the same as before, but serde encoding in runtime has changed from bytes to string (it was like this in std environment already), which most projects shouldn't have issues with. I consider the impact of serde encoding here low due to the type only being used in runtime version struct and mostly limited to runtime internals, where serde encoding/decoding of this data structure is quite unlikely (though we did hit exactly this edge-case ourselves 😅).

Review Notes

Most of the changes are trivial and mechanical, the only non-trivial change is in substrate/primitives/version/proc-macro/src/decl_runtime_version.rs where macro call expectation in sp_version::runtime_version implementation was replaced with function call expectation.

Checklist

  • My PR includes a detailed description as outlined in the "Description" and its two subsections above.
  • My PR follows the labeling requirements of this project (at minimum one label for T required)
    • External contributors: ask maintainers to put the right label on your PR.
  • I have made corresponding changes to the documentation (if applicable)

@nazar-pc nazar-pc requested review from sorpaas, a team and koute as code owners September 12, 2024 15:36
@nazar-pc nazar-pc mentioned this pull request Sep 12, 2024
2 tasks
@nazar-pc nazar-pc force-pushed the remove-RuntimeString branch 2 times, most recently from f0df22e to 308d2a9 Compare September 12, 2024 16:02
@nazar-pc
Copy link
Contributor Author

nazar-pc commented Sep 12, 2024

I do not understand why formatter in CI wants those trailing whitespaces, I'm unable to commit them for some reason in IDE.

UPD: nano did it.

@nazar-pc nazar-pc force-pushed the remove-RuntimeString branch 2 times, most recently from 096e2d6 to f3778c1 Compare September 12, 2024 16:30
@nazar-pc nazar-pc force-pushed the remove-RuntimeString branch from f3778c1 to d9de518 Compare September 12, 2024 18:55
nazar-pc added a commit to autonomys/polkadot-sdk that referenced this pull request Sep 27, 2024
# Conflicts:
#	cumulus/parachains/runtimes/assets/asset-hub-rococo/src/genesis_config_presets.rs
#	polkadot/runtime/rococo/src/genesis_config_presets.rs
#	templates/parachain/runtime/src/genesis_config_presets.rs
@nazar-pc
Copy link
Contributor Author

Fixed merge conflicts and restored create_runtime_str

@nazar-pc
Copy link
Contributor Author

Anything I can do to move this PR forward? It seems to just accumulate merge conflicts over time.

Comment on lines 1040 to 1045
$crate::Cow::Borrowed($y)
}};
}
// Re-export for ^ macro, should be removed once macro is gone
#[doc(hidden)]
pub use alloc::borrow::Cow;
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't work?

Suggested change
$crate::Cow::Borrowed($y)
}};
}
// Re-export for ^ macro, should be removed once macro is gone
#[doc(hidden)]
pub use alloc::borrow::Cow;
$crate::alloc::borrow::Cow::Borrowed($y)
}};
}

Copy link
Member

Choose a reason for hiding this comment

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

Even better to wrap it in a mod __private module, so others dont assume that it is part of the public API.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

$crate::alloc::borrow::Cow::Borrowed($y) does not work because alloc is a private import of the crate.

I only did it like this originally due similar example code on line 52 above. I could move both into __private, but I wanted to keep it close to the now deprecated macro. Let me know if you have a strong opinion about this one or feel free to push changes directly.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah yes, you can also have a __private_create_runtime_str module if you want to keep stuff together.

It is good so user don't start using it in when it is recommended by rustc.

Anyway it is fine.

@gui1117 gui1117 added the T17-primitives Changes to primitives that are not covered by any other label. label Nov 4, 2024
@gui1117
Copy link
Contributor

gui1117 commented Nov 4, 2024

/cmd prdoc --bump major --audience runtime_dev

prdoc/pr_5693.prdoc Outdated Show resolved Hide resolved
prdoc/pr_5693.prdoc Outdated Show resolved Hide resolved
@@ -202,7 +202,7 @@ macro_rules! impl_node_runtime_apis {

fn dispatch_benchmark(
_: frame_benchmarking::BenchmarkConfig
) -> Result<Vec<frame_benchmarking::BenchmarkBatch>, sp_runtime::RuntimeString> {
) -> Result<Vec<frame_benchmarking::BenchmarkBatch>, alloc::string::String> {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
) -> Result<Vec<frame_benchmarking::BenchmarkBatch>, alloc::string::String> {
) -> Result<Vec<frame_benchmarking::BenchmarkBatch>, String> {

This code is never compiled for no_std.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It actually is used like that in CI, had to revert this suggestion

# Conflicts:
#	cumulus/parachains/runtimes/starters/seedling/src/lib.rs
#	cumulus/parachains/runtimes/starters/shell/src/lib.rs
#	cumulus/test/runtime/src/lib.rs
#	substrate/test-utils/runtime/src/lib.rs
Copy link
Contributor Author

@nazar-pc nazar-pc left a comment

Choose a reason for hiding this comment

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

Applied review suggestions improving upgrade experience and resolved merge conflicts

Comment on lines 1040 to 1045
$crate::Cow::Borrowed($y)
}};
}
// Re-export for ^ macro, should be removed once macro is gone
#[doc(hidden)]
pub use alloc::borrow::Cow;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

$crate::alloc::borrow::Cow::Borrowed($y) does not work because alloc is a private import of the crate.

I only did it like this originally due similar example code on line 52 above. I could move both into __private, but I wanted to keep it close to the now deprecated macro. Let me know if you have a strong opinion about this one or feel free to push changes directly.

@nazar-pc nazar-pc requested review from gui1117 and ggwpez November 5, 2024 05:04
@gui1117
Copy link
Contributor

gui1117 commented Nov 5, 2024

needs to fix CI:

error[E0433]: failed to resolve: use of undeclared crate or module `alloc`
   --> cumulus/polkadot-omni-node/lib/src/fake_runtime_api/utils.rs:205:58
    |
205 |                 ) -> Result<Vec<frame_benchmarking::BenchmarkBatch>, alloc::string::String> {
    |                                                                      ^^^^^ use of undeclared crate or module `alloc`
    |
   ::: cumulus/polkadot-omni-node/lib/src/fake_runtime_api/mod.rs:29:2
    |
29  |     impl_node_runtime_apis!(FakeRuntime, CustomBlock, sp_consensus_aura::sr25519::AuthorityId);
    |     ------------------------------------------------------------------------------------------ in this macro invocation
    |
    = help: add `extern crate alloc` to use the `alloc` crate
    = note: this error originates in the macro `impl_node_runtime_apis` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider importing this module
   --> cumulus/polkadot-omni-node/lib/src/fake_runtime_api/mod.rs:26:2
    |
26  +     use std::string;
    |

error[E0433]: failed to resolve: use of undeclared crate or module `alloc`
   --> cumulus/polkadot-omni-node/lib/src/fake_runtime_api/utils.rs:205:58
    |
205 |                 ) -> Result<Vec<frame_benchmarking::BenchmarkBatch>, alloc::string::String> {
    |                                                                      ^^^^^ use of undeclared crate or module `alloc`
    |
   ::: cumulus/polkadot-omni-node/lib/src/fake_runtime_api/mod.rs:36:2
    |
36  |     impl_node_runtime_apis!(FakeRuntime, CustomBlock, sp_consensus_aura::ed25519::AuthorityId);
    |     ------------------------------------------------------------------------------------------ in this macro invocation
    |
    = help: add `extern crate alloc` to use the `alloc` crate
    = note: this error originates in the macro `impl_node_runtime_apis` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider importing this module
   --> cumulus/polkadot-omni-node/lib/src/fake_runtime_api/mod.rs:33:2
    |
33  +     use std::string;
    |

@nazar-pc
Copy link
Contributor Author

nazar-pc commented Nov 5, 2024

Right, that is why it was in there from the beginning, bringing it back too

@bkchr bkchr enabled auto-merge November 5, 2024 21:26
@bkchr bkchr added this pull request to the merge queue Nov 5, 2024
Merged via the queue into paritytech:master with commit c5444f3 Nov 5, 2024
191 of 194 checks passed
@nazar-pc nazar-pc deleted the remove-RuntimeString branch November 12, 2024 12:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T17-primitives Changes to primitives that are not covered by any other label.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

serde for RuntimeString is broken
5 participants