From 12c368bc542b73b0b12e240439b93e30f26c6307 Mon Sep 17 00:00:00 2001 From: chungquantin <56880684+chungquantin@users.noreply.github.com> Date: Mon, 23 Sep 2024 16:29:16 +0700 Subject: [PATCH] chore: resolve comments --- pop-sandbox/README.md | 30 +++++++++++++++++++++-------- pop-sandbox/examples/flipper/lib.rs | 6 +++--- pop-sandbox/src/lib.rs | 21 ++++++++++---------- 3 files changed, 36 insertions(+), 21 deletions(-) diff --git a/pop-sandbox/README.md b/pop-sandbox/README.md index b911cd17..cacfdc22 100644 --- a/pop-sandbox/README.md +++ b/pop-sandbox/README.md @@ -1,6 +1,8 @@ # Pop Sandbox -Implementation of the `pop_drink::Sandbox` struct for the Pop Network runtimes required for the quasi testing with `drink`. +Implementation of the [`pop_drink::Sandbox`](https://github.com/r0gue-io/pop-drink) struct for the Pop Network runtimes (located in `pop-node/runtime`) required for the quasi testing with `drink`. + +In the context of quasi-testing with pop-drink, a sandbox refers to an isolated runtime environment that simulates the behavior of a full node, without requiring an actual node. It can emulate key processes (where runtime `pallets` are involved) such as block initialization, execution, and block finalization. ## Getting Started @@ -12,28 +14,36 @@ pop_drink = { version = "1.0.0", package = "pop-drink" } ### Import Sandbox for the specific runtime -For mainnet +- For `devnet` runtime + +Implementation of the sandbox runtime environment for `devnet` runtime located in `pop-node/runtime/devnet` ```rs -use pop_sandbox::MainnetSandbox; +use pop_sandbox::DevnetSandbox; ``` -For devnet +- For `testnet` runtime + +Implementation of the sandbox runtime environment for `testnet` runtime located in `pop-node/runtime/testnet` ```rs -use pop_sandbox::DevnetSandbox; +use pop_sandbox::TestnetSandbox; ``` -For testnet +- For `mainnet` runtime + +Implementation of the sandbox runtime environment for `mainnet` runtime located in `pop-node/runtime/mainnet` ```rs -use pop_sandbox::TestnetSandbox; +use pop_sandbox::MainnetSandbox; ``` ### Setup test environment for your contract +Below is an example for the contract testing with `pop_drink` and `pop_sandbox` for `devnet` environment using `DevnetSandbox`. + ```rs -use drink::session::Session; +use pop_drink::session::Session; use pop_sandbox::DevnetSandbox as Sandbox; #[drink::contract_bundle_provider] @@ -44,3 +54,7 @@ fn test(mut session: Session) { // Your test case } ``` + +## Examples + +Please find more examples of `pop_drink` tests in the [`pop_drink/examples`](https://github.com/r0gue-io/pop-drink/tree/main/examples). diff --git a/pop-sandbox/examples/flipper/lib.rs b/pop-sandbox/examples/flipper/lib.rs index cf0352a9..c26c6ead 100644 --- a/pop-sandbox/examples/flipper/lib.rs +++ b/pop-sandbox/examples/flipper/lib.rs @@ -74,7 +74,7 @@ mod tests { /// runtime and it exposes a broad API for interacting with it. Session is generic over the /// runtime type, but usually and by default, we use `MinimalSandbox`, which is a minimalistic /// runtime that allows using smart contracts. - #[drink::test(sandbox = pop_sandbox::PopSandbox)] + #[drink::test(sandbox = pop_sandbox::DevnetSandbox)] fn deploy_and_call_a_contract(mut session: Session) -> Result<(), Box> { let _ = env_logger::try_init(); // Now we get the contract bundle from the `BundleProvider` enum. Since the current crate @@ -119,7 +119,7 @@ mod tests { } /// In this testcase we will see how to get and read debug logs from the contract. - #[drink::test(sandbox = pop_sandbox::PopSandbox)] + #[drink::test(sandbox = pop_sandbox::DevnetSandbox)] fn get_debug_logs(mut session: Session) -> Result<(), Box> { session.deploy_bundle(BundleProvider::local()?, "new", &["true"], NO_SALT, None)?; @@ -139,7 +139,7 @@ mod tests { } /// In this testcase we will see how to work with multiple contracts. - #[drink::test(sandbox = pop_sandbox::PopSandbox)] + #[drink::test(sandbox = pop_sandbox::DevnetSandbox)] fn work_with_multiple_contracts( mut session: Session, ) -> Result<(), Box> { diff --git a/pop-sandbox/src/lib.rs b/pop-sandbox/src/lib.rs index dbdb9a06..ad15cf90 100644 --- a/pop-sandbox/src/lib.rs +++ b/pop-sandbox/src/lib.rs @@ -1,13 +1,11 @@ +pub use frame_support::sp_runtime::AccountId32; use frame_support::{ - sp_runtime::{ - traits::{Header, One}, - }, + sp_runtime::traits::{Header, One}, traits::Hooks, }; use frame_system::pallet_prelude::BlockNumberFor; -use pop_runtime_devnet::{BuildStorage, Runtime}; pub use pop_runtime_devnet::Balance; -pub use frame_support::sp_runtime::AccountId32; +use pop_runtime_devnet::{BuildStorage, Runtime}; /// Alias for the account ID type. pub type AccountIdFor = ::AccountId; @@ -49,7 +47,7 @@ impl< frame_system::Pallet::::reset_events(); frame_system::Pallet::::initialize(&height, &parent_hash, &Default::default()); pallet_balances::Pallet::::on_initialize(height); - // TODO: Resolve an issue with pallet-aura to simulate the time. + // TODO: Resolve an issue with pallet-aura to simulate the time: Timestamp slot must match `CurrentSlot` // pallet_timestamp::Pallet::::set_timestamp( // SystemTime::now() // .duration_since(SystemTime::UNIX_EPOCH) @@ -72,16 +70,19 @@ impl< } } -pub struct Sandbox { +/// Sandbox runtime environment for `devnet` runtime. +pub struct DevnetSandbox { ext: sp_io::TestExternalities, } -impl Default for Sandbox { +impl Default for DevnetSandbox { fn default() -> Self { - let balances: Vec<(AccountId32, u128)> = vec![(ALICE, INIT_AMOUNT), (BOB, INIT_AMOUNT), (CHARLIE, INIT_AMOUNT)]; + let balances: Vec<(AccountId32, u128)> = + vec![(ALICE, INIT_AMOUNT), (BOB, INIT_AMOUNT), (CHARLIE, INIT_AMOUNT)]; let ext = BlockBuilder::::new_ext(balances); Self { ext } } } -drink::impl_sandbox!(Sandbox, Runtime, BlockBuilder, ALICE); +/// Implement core functionalities of the `DevnetSandbox`. +drink::impl_sandbox!(DevnetSandbox, Runtime, BlockBuilder, ALICE);