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

Rename & update SandboxConfig trait #105

Merged
merged 1 commit into from
Mar 11, 2024
Merged

Rename & update SandboxConfig trait #105

merged 1 commit into from
Mar 11, 2024

Conversation

pgherveou
Copy link
Collaborator

@pgherveou pgherveou commented Feb 22, 2024

Follow up from #99

I realized that the current design was not working as I wanted.
Specifically, when working with parachain and xcm_simulator::TestExt, I need to use the existing externalities, and
the custom execute_with to dispatch messages properly.

  • The Sandbox is now created inside the macro (renamed create_minimal_sandbox) along with the runtime. The default setup now uses the MinimalSandbox created through the macro, instead of (the now deleted) Sandbox struct.

  • The trait SandboxConfig is renamed Sandbox and exposes these two new APIs

    fn execute_with<T>(&mut self, execute: impl FnOnce() -> T) -> T;
    fn dry_run<T>(&mut self, action: impl FnOnce(&mut Self) -> T) -> T;
  • fn initialize_storage is removed since this can be done when the Sandbox is created, and can be customized there instead of being part of the trait.
  • The api in sandbox/*_api.rs are updated to be traits with blanket implementations for T: Sandbox instead of impl blocks.

These updates let us create custom sandbox environment that have full control over the storage and execution.
An example of this, is the MockNetworkSandbox defined in ink!
that uses under the hood the xcm simulator to test connected smart contract that need to interact with a Network of chains.

@pgherveou pgherveou marked this pull request as ready for review February 22, 2024 14:12
@pmikolajczyk41 pmikolajczyk41 self-requested a review February 22, 2024 14:16
Copy link
Member

@pmikolajczyk41 pmikolajczyk41 left a comment

Choose a reason for hiding this comment

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

I'm not that big fan of the thread_local solution, but I don't want to block your work on this - let's merge it now and see if this causes any problems

also, two kind requests:

  • please bump version
  • in another PR, can you please add an example showcasing how you can use drink with custom runtime? even if it requires some manual work, I think it'd be good to have it covered here

deuszx
deuszx previously requested changes Feb 23, 2024
Copy link
Collaborator

@deuszx deuszx left a comment

Choose a reason for hiding this comment

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

Please convince me that we need the thread_local! and RefCell.

drink/src/runtime/minimal.rs Outdated Show resolved Hide resolved
@pgherveou
Copy link
Collaborator Author

pgherveou commented Feb 25, 2024

@deuszx @pmikolajczyk41 updated the initial fix.

To avoid using the hacky RefCell & thread_local solution, I updated (one more time 😓) how everything is setup.

  • The Sandbox is now created inside the macro (renamed create_minimal_sandbox) along with the runtime. So we use MinimalSandbox instead of Sandbox for the default setup.
  • The trait SandboxConfig is renamed Sandbox and exposes these two new APIs
    fn execute_with<T>(&mut self, execute: impl FnOnce() -> T) -> T;
    fn dry_run<T>(&mut self, action: impl FnOnce(&mut Self) -> T) -> T;

initialize_storage is removed since this can be done when the Sandbox is created, and can be customized there instead of being part of the trait.

  • The api in sandbox/*_api.rs are updated to be traits with blanket implementations for T: Sandbox instead of impl blocks.

This needs another pass to make sure some doc string are still up to date, but hopefully this address your concern.

@deuszx
Copy link
Collaborator

deuszx commented Feb 26, 2024

Thanks @pgherveou , I'll review it this week. Also, before I forget, remember to bump minor version of the crate.

@pgherveou
Copy link
Collaborator Author

Thanks @pgherveou , I'll review it this week. Also, before I forget, remember to bump minor version of the crate.

Gentle ping for review 🙏

@SkymanOne SkymanOne mentioned this pull request Feb 29, 2024
@pgherveou pgherveou changed the title Update SandboxConfig Rename & update SandboxConfig trait Mar 6, 2024
Cargo.toml Outdated Show resolved Hide resolved
Cargo.toml Outdated Show resolved Hide resolved
drink/src/runtime.rs Show resolved Hide resolved
self.call_internal(Some(address), message, args, endowment)
}

fn call_internal<S: AsRef<str> + Debug, T: Decode>(
fn call_internal<S: AsRef<str> + Debug, V: Decode>(
Copy link
Member

Choose a reason for hiding this comment

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

+1 for s/T/V

drink/src/sandbox/timestamp_api.rs Outdated Show resolved Hide resolved
@pgherveou
Copy link
Collaborator Author

@deuszx you are still blocking this, would be nice if you could remove the "change requested" or delegate your review to someone else?

CHANGELOG.md Show resolved Hide resolved
@@ -69,7 +69,7 @@ impl Default for UiState {
}

pub struct AppState {
pub session: Session<MinimalRuntime>,
pub session: Session<MinimalSandbox>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it's kinda weird that Session is now parameterised with the Sandbox - it encloses the sandbox but the parameter type should be something that was the common denominator for both - session and sandbox.

Cannot point my finger to anything specific, so this won't be a blocker, but I just have a gut feeling that previous typing was more "appropriate".

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

it encloses the sandbox but the parameter type should be something that was the common denominator for both

The sandbox itself enclose the runtime so the common denominator still stands

drink/src/lib.rs Outdated
Comment on lines 35 to 37
pub type BalanceOf<Runtime> =
<<Runtime as pallet_contracts::Config>::Currency as Inspect<AccountIdFor<Runtime>>>::Balance;
pub type BalanceOf<R> = <R as pallet_balances::Config>::Balance;
Copy link
Collaborator

Choose a reason for hiding this comment

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

What w twist (https://github.com/inkdevhub/drink/pull/106/files#r1509471655). @pmikolajczyk41 , are you OK with this change? Since you made the previous one and had some arguments it was "more correct".

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ah this can go away, I probably missed that in the merge

drink/src/lib.rs Outdated Show resolved Hide resolved
drink/src/lib.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@deuszx deuszx left a comment

Choose a reason for hiding this comment

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

As requested @pgherveou , I'm unblocking the PR (apologies for the holding off, I forgot that request changes block the merge) but I can't approve it with clear conscience - I don't have enough time to review it thoroughly at the moment but I know it's important for you so I'm deferring to @pmikolajczyk41 here.

@deuszx deuszx dismissed their stale review March 9, 2024 16:08

As stated in the comment (didn't know it won't automatically dismiss it).

@pgherveou pgherveou merged commit f21834d into main Mar 11, 2024
2 checks passed
@pgherveou pgherveou deleted the pg/update-macros branch March 11, 2024 10:25
@pgherveou pgherveou mentioned this pull request Mar 14, 2024
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.

3 participants