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

Refactor the host <-> runtime interface machinery #7375

Open
wants to merge 38 commits into
base: master
Choose a base branch
from

Conversation

s0me0ne-unkn0wn
Copy link
Contributor

Keep calm and stand still, it's a hijacking of PR #3689 by @koute. The original description follows; please refer to the original PR for the past discussions.

--- 8< --- cut here --- 8< ---

This PR refactors the way the host functions are defined.

Previously the way a given type was marshalled through the host <-> runtime boundary was hardcoded for every type by the virtue of it implementing the relevant conversion traits.

This had two major consequences:

  • It was not obvious how a given type is going to be passed just by looking at its type alone, masking potentially expensive marshalling strategies. (For example, returning Option<u32> was done through the SCALE codec and involved extra memory allocations!)
  • It was not possible to use multiple marshalling strategies for a single type, making some of the future improvements we'd like to do (e.g. move the runtime memory allocator into the runtime) very hard to do.

So this PR disentangles this mess and makes the marshalling strategies explicit. This makes it much more clear how a given type in a given host function is marshalled, and also makes it possible to use different marshalling strategies for the same type in different host functions.

Before this PR you'd define a host function like this:

    #[runtime_interface]
    trait MyInterface {
        fn say_hello_world(name: &str) {
            println!("Hello {name}!");
        }
    }

and after this PR you'll define it like this:

    #[runtime_interface]
    trait MyInterface {
        fn say_hello_world(name: PassFatPointerAndRead<&str>) {
            println!("Hello {name}!", name);
        }
    }

In this case the strategy for passing the &str is now explicitly specified (PassFatPointerAndRead). Note that the actual API generated by this macro and the way arguments are accessed is completely unchanged! The #[runtime_interface] machinery automatically "strips" away the marshalling strategy wrappers, so neither the body of the say_hello_world function here nor its callers need to be changed.

This is a breaking change only for people who use #[runtime_interface] to define their own custom host functions.

koute added 30 commits March 14, 2024 16:28
@paritytech-workflow-stopper
Copy link

All GitHub workflows were cancelled due to failure one of the required jobs.
Failed workflow url: https://github.com/paritytech/polkadot-sdk/actions/runs/13020733130
Failed job name: check-core-crypto-features

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants