Skip to content

Commit 7033981

Browse files
authored
refactor: ink sandbox crate (#2659)
* refactor: move ink_sandbox crate outside e2e * refactor: sandbox_client in sandbox crate * fix: generic support in macro crate * tests: adapt integration tests to ink_sandbox crate * docs: fix docs tests * fix: removed imports * refactor: cleanup * fix: test docs adding ink_sandbox as dev-dependency * chore: add line in CHANGELOG
1 parent 96434d9 commit 7033981

File tree

36 files changed

+235
-181
lines changed

36 files changed

+235
-181
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
### Changed
1616
- Marks the `pallet-revive` host function `account_id` stable - [#2578](https://github.com/use-ink/ink/pull/2578)
1717
- Stabilize `is_contract` - [#2654](https://github.com/use-ink/ink/pull/2654)
18+
- Extract `sandbox` from `ink_e2e` into a new `ink_sandbox` crate - [#2659](https://github.com/use-ink/ink/pull/2659)
1819

1920
### Fixed
2021
- Fix decoding of `HostFn::minimum_balance` return value - [#2656](https://github.com/use-ink/ink/pull/2656)

Cargo.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ members = [
44
"crates/allocator",
55
"crates/e2e",
66
"crates/e2e/macro",
7-
"crates/e2e/sandbox",
87
"crates/engine",
98
"crates/env",
109
"crates/ink",
@@ -15,6 +14,7 @@ members = [
1514
"crates/revive-types",
1615
"crates/prelude",
1716
"crates/primitives",
17+
"crates/sandbox",
1818
"crates/storage",
1919
"crates/storage/traits"
2020
]

crates/e2e/Cargo.toml

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,17 @@ ink = { workspace = true, default-features = true }
2020
ink_env = { workspace = true, default-features = true }
2121
ink_primitives = { workspace = true, default-features = true }
2222
ink_revive_types = { workspace = true, default-features = true }
23-
ink_sandbox = { version = "=6.0.0-alpha.4", path = "./sandbox", optional = true }
2423

2524
cargo_metadata = { workspace = true }
2625
contract-build = { workspace = true }
2726
funty = { workspace = true }
28-
impl-serde = { workspace = true }
2927
jsonrpsee = { workspace = true, features = ["ws-client"] }
3028
serde = { workspace = true, features = ["derive"] }
31-
serde_json = { workspace = true }
3229
tokio = { workspace = true, features = ["rt-multi-thread"] }
3330
tracing = { workspace = true }
3431
tracing-subscriber = { workspace = true, features = ["env-filter"] }
3532
scale = { workspace = true }
3633
subxt = { workspace = true }
37-
subxt-metadata = { workspace = true, optional = true }
3834
subxt-signer = { workspace = true, features = ["subxt", "sr25519", "unstable-eth"] }
3935
thiserror = { workspace = true }
4036
which = { workspace = true }
@@ -49,35 +45,21 @@ sp-weights = { workspace = true }
4945
regex = "1.11.2"
5046
itertools = "0.14.0"
5147

52-
sp-io = { git = "https://github.com/use-ink/polkadot-sdk.git", rev = "a71ec19a94702ea71767ba5ac97603ea6c6305c1", default-features = false, features = ["disable_panic_handler", "disable_oom", "disable_allocator"] }
53-
sp-runtime-interface = { git = "https://github.com/use-ink/polkadot-sdk.git", rev = "a71ec19a94702ea71767ba5ac97603ea6c6305c1", default-features = false, features = ["disable_target_static_assertions"] }
54-
5548
[dev-dependencies]
5649
# Required for the doctest of `MessageBuilder::call`
5750
scale-info = { workspace = true, features = ["derive"] }
5851

5952
[features]
6053
default = [ "std" ]
6154
std = [
62-
"impl-serde/std",
63-
"ink_e2e_macro/std",
6455
"scale-info/std",
6556
"scale/std",
6657
"serde/std",
6758
"sp-core/std",
6859
"sp-keyring/std",
6960
"sp-runtime/std",
70-
"sp-io/std",
71-
"sp-runtime-interface/std",
7261
"sp-weights/std",
7362
"ink_e2e_macro/std",
7463
"ink_revive_types/std",
75-
"ink_sandbox?/std",
7664
"frame-support/std",
7765
]
78-
79-
sandbox = [
80-
"dep:ink_sandbox",
81-
"dep:subxt-metadata",
82-
"ink_e2e_macro/sandbox",
83-
]

crates/e2e/macro/Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,16 @@ darling = { workspace = true }
2323
ink_ir = { workspace = true, default-features = true }
2424
derive_more = { workspace = true, features = ["from"] }
2525
tracing-subscriber = { workspace = true, features = ["env-filter"] }
26-
serde_json = { workspace = true }
2726
syn = { workspace = true }
2827
proc-macro2 = { workspace = true }
2928
quote = { workspace = true }
3029
tracing = { workspace = true }
3130

3231
[dev-dependencies]
3332
ink = { path = "../../ink" }
34-
ink_e2e = { path = "../", features = ["sandbox"] }
33+
ink_e2e = { path = "../" }
34+
ink_sandbox = { path = "../../sandbox" }
3535
temp-env = "0.3.6"
3636

3737
[features]
3838
std = ["derive_more/std"]
39-
sandbox = []

crates/e2e/macro/src/codegen.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,10 @@ impl InkE2ETest {
7878
Backend::Node(node_config) => {
7979
build_full_client(&environment, exec_build_contracts, node_config)
8080
}
81-
#[cfg(any(test, feature = "sandbox"))]
82-
Backend::RuntimeOnly(runtime) => {
83-
build_runtime_client(exec_build_contracts, runtime.into())
81+
Backend::RuntimeOnly(args) => {
82+
let runtime: syn::Path = args.runtime_path();
83+
let client: syn::Path = args.client_path();
84+
build_runtime_client(exec_build_contracts, runtime, client)
8485
}
8586
};
8687

@@ -170,10 +171,13 @@ fn build_full_client(
170171
}
171172
}
172173

173-
#[cfg(any(test, feature = "sandbox"))]
174-
fn build_runtime_client(contracts: TokenStream2, runtime: syn::Path) -> TokenStream2 {
174+
fn build_runtime_client(
175+
contracts: TokenStream2,
176+
runtime: syn::Path,
177+
client: syn::Path,
178+
) -> TokenStream2 {
175179
quote! {
176180
let contracts = #contracts;
177-
let mut client = ::ink_e2e::SandboxClient::<_, #runtime>::new(contracts);
181+
let mut client = #client::<_, #runtime>::new(contracts);
178182
}
179183
}

crates/e2e/macro/src/config.rs

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ pub enum Backend {
2424
///
2525
/// This runs a runtime emulator within `TestExternalities`
2626
/// the same process as the test.
27-
#[cfg(any(test, feature = "sandbox"))]
2827
RuntimeOnly(RuntimeOnly),
2928
}
3029

@@ -64,22 +63,21 @@ impl Node {
6463
}
6564

6665
/// The runtime emulator that should be used within `TestExternalities`
67-
#[cfg(any(test, feature = "sandbox"))]
6866
#[derive(Clone, Eq, PartialEq, Debug, darling::FromMeta)]
69-
pub enum RuntimeOnly {
70-
#[darling(word)]
71-
#[darling(skip)]
72-
Default,
73-
Sandbox(syn::Path),
67+
pub struct RuntimeOnly {
68+
/// The sandbox runtime type (e.g., `ink_sandbox::DefaultSandbox`)
69+
pub sandbox: syn::Path,
70+
/// The client type implementing the backend traits (e.g.,
71+
/// `ink_sandbox::SandboxClient`)
72+
pub client: syn::Path,
7473
}
7574

76-
#[cfg(any(test, feature = "sandbox"))]
77-
impl From<RuntimeOnly> for syn::Path {
78-
fn from(value: RuntimeOnly) -> Self {
79-
match value {
80-
RuntimeOnly::Default => syn::parse_quote! { ::ink_e2e::DefaultSandbox },
81-
RuntimeOnly::Sandbox(path) => path,
82-
}
75+
impl RuntimeOnly {
76+
pub fn runtime_path(&self) -> syn::Path {
77+
self.sandbox.clone()
78+
}
79+
pub fn client_path(&self) -> syn::Path {
80+
self.client.clone()
8381
}
8482
}
8583

@@ -146,7 +144,7 @@ mod tests {
146144
fn config_works_backend_runtime_only() {
147145
let input = quote! {
148146
environment = crate::CustomEnvironment,
149-
backend(runtime_only),
147+
backend(runtime_only(sandbox = ::ink_sandbox::DefaultSandbox, client = ::ink_sandbox::SandboxClient)),
150148
};
151149
let config =
152150
E2EConfig::from_list(&NestedMeta::parse_meta_list(input).unwrap()).unwrap();
@@ -156,7 +154,13 @@ mod tests {
156154
Some(syn::parse_quote! { crate::CustomEnvironment })
157155
);
158156

159-
assert_eq!(config.backend(), Backend::RuntimeOnly(RuntimeOnly::Default));
157+
assert_eq!(
158+
config.backend(),
159+
Backend::RuntimeOnly(RuntimeOnly {
160+
sandbox: syn::parse_quote! { ::ink_sandbox::DefaultSandbox },
161+
client: syn::parse_quote! { ::ink_sandbox::SandboxClient },
162+
})
163+
);
160164
}
161165

162166
#[test]
@@ -168,22 +172,29 @@ mod tests {
168172
let config =
169173
E2EConfig::from_list(&NestedMeta::parse_meta_list(input).unwrap()).unwrap();
170174

171-
assert_eq!(config.backend(), Backend::RuntimeOnly(RuntimeOnly::Default));
175+
assert_eq!(
176+
config.backend(),
177+
Backend::RuntimeOnly(RuntimeOnly {
178+
sandbox: syn::parse_quote! { ::ink_sandbox::DefaultSandbox },
179+
client: syn::parse_quote! { ::ink_sandbox::SandboxClient },
180+
})
181+
);
172182
}
173183

174184
#[test]
175185
fn config_works_runtime_only_with_custom_backend() {
176186
let input = quote! {
177-
backend(runtime_only(sandbox = ::ink_e2e::DefaultSandbox)),
187+
backend(runtime_only(sandbox = ::ink_sandbox::DefaultSandbox, client = ::ink_sandbox::SandboxClient)),
178188
};
179189
let config =
180190
E2EConfig::from_list(&NestedMeta::parse_meta_list(input).unwrap()).unwrap();
181191

182192
assert_eq!(
183193
config.backend(),
184-
Backend::RuntimeOnly(RuntimeOnly::Sandbox(
185-
syn::parse_quote! { ::ink_e2e::DefaultSandbox }
186-
))
194+
Backend::RuntimeOnly(RuntimeOnly {
195+
sandbox: syn::parse_quote! { ::ink_sandbox::DefaultSandbox },
196+
client: syn::parse_quote! { ::ink_sandbox::SandboxClient },
197+
})
187198
);
188199
}
189200

crates/e2e/macro/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ use syn::Result;
6060
/// ```
6161
/// type E2EResult<T> = std::result::Result<T, Box<dyn std::error::Error>>;
6262
///
63-
/// #[ink_e2e::test(backend(runtime_only))]
63+
/// #[ink_sandbox::test(backend(runtime_only(sandbox = ink_sandbox::DefaultSandbox, client = ink_sandbox::SandboxClient)))]
6464
/// async fn runtime_call_works() -> E2EResult<()> {
6565
/// // ...
6666
/// }

crates/e2e/src/error.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -70,29 +70,3 @@ where
7070
<Self as fmt::Debug>::fmt(self, f)
7171
}
7272
}
73-
74-
/// Dummy error type for sandbox_client
75-
#[derive(Debug, thiserror::Error)]
76-
pub struct SandboxErr {
77-
msg: String,
78-
}
79-
80-
impl SandboxErr {
81-
/// Create a new `SandboxErr` with the given message.
82-
#[allow(dead_code)]
83-
pub fn new(msg: String) -> Self {
84-
Self { msg }
85-
}
86-
}
87-
88-
impl From<String> for SandboxErr {
89-
fn from(msg: String) -> Self {
90-
Self { msg }
91-
}
92-
}
93-
94-
impl fmt::Display for SandboxErr {
95-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
96-
write!(f, "SandboxErr: {}", self.msg)
97-
}
98-
}

crates/e2e/src/lib.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ mod contract_results;
2828
mod error;
2929
pub mod events;
3030
mod node_proc;
31-
#[cfg(feature = "sandbox")]
32-
mod sandbox_client;
3331
mod subxt_client;
3432
mod xts;
3533

@@ -44,10 +42,21 @@ pub use backend_calls::{
4442
CallBuilder,
4543
InstantiateBuilder,
4644
};
47-
pub use client_utils::ContractsRegistry;
45+
pub use builders::{
46+
CreateBuilderPartial,
47+
constructor_exec_input,
48+
};
49+
pub use client_utils::{
50+
ContractsRegistry,
51+
code_hash,
52+
salt,
53+
};
4854
pub use contract_results::{
55+
BareInstantiationResult,
4956
CallDryRunResult,
5057
CallResult,
58+
ContractExecResultFor,
59+
ContractResult,
5160
InstantiateDryRunResult,
5261
InstantiationResult,
5362
UploadResult,
@@ -58,11 +67,6 @@ pub use node_proc::{
5867
TestNodeProcess,
5968
TestNodeProcessBuilder,
6069
};
61-
#[cfg(feature = "sandbox")]
62-
pub use sandbox_client::{
63-
Client as SandboxClient,
64-
preset,
65-
};
6670
pub use sp_keyring::Sr25519Keyring;
6771
pub use subxt::{
6872
self,
@@ -85,9 +89,6 @@ pub use tokio;
8589
pub use tracing;
8690
pub use tracing_subscriber;
8791

88-
#[cfg(feature = "sandbox")]
89-
pub use ink_sandbox::DefaultSandbox;
90-
9192
use ink::codegen::ContractCallBuilder;
9293
use ink_env::{
9394
ContractEnv,

0 commit comments

Comments
 (0)