Skip to content

Commit

Permalink
chore: resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
chungquantin committed Sep 23, 2024
1 parent cbcf66a commit 12c368b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
30 changes: 22 additions & 8 deletions pop-sandbox/README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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]
Expand All @@ -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).
6 changes: 3 additions & 3 deletions pop-sandbox/examples/flipper/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
let _ = env_logger::try_init();
// Now we get the contract bundle from the `BundleProvider` enum. Since the current crate
Expand Down Expand Up @@ -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<dyn std::error::Error>> {
session.deploy_bundle(BundleProvider::local()?, "new", &["true"], NO_SALT, None)?;

Expand All @@ -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<dyn std::error::Error>> {
Expand Down
21 changes: 11 additions & 10 deletions pop-sandbox/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<R> = <R as frame_system::Config>::AccountId;
Expand Down Expand Up @@ -49,7 +47,7 @@ impl<
frame_system::Pallet::<T>::reset_events();
frame_system::Pallet::<T>::initialize(&height, &parent_hash, &Default::default());
pallet_balances::Pallet::<T>::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::<T>::set_timestamp(
// SystemTime::now()
// .duration_since(SystemTime::UNIX_EPOCH)
Expand All @@ -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::<Runtime>::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);

0 comments on commit 12c368b

Please sign in to comment.