Skip to content

Commit

Permalink
Rename & update SandboxConfig trait (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgherveou committed Mar 11, 2024
1 parent 07463a6 commit f21834d
Show file tree
Hide file tree
Showing 38 changed files with 692 additions and 484 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.13.0]

- Rework Sandbox API to better support custom Runtime

## [Unreleased]

# [0.13.0]
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ crossterm = { version = "0.26.0" }
darling = { version = "0.20.3" }
parity-scale-codec = { version = "3.6.9" }
parity-scale-codec-derive = { version = "3.6.9" }
paste = { version = "1.0.7" }
proc-macro2 = { version = "1" }
quote = { version = "1" }
ratatui = { version = "0.21.0" }
Expand Down
8 changes: 4 additions & 4 deletions drink-cli/src/app_state/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{env, path::PathBuf};

pub use contracts::{Contract, ContractIndex, ContractRegistry};
use drink::{runtime::MinimalRuntime, session::Session, SandboxConfig, Weight, DEFAULT_GAS_LIMIT};
use drink::{runtime::MinimalSandbox, session::Session, Sandbox, Weight, DEFAULT_GAS_LIMIT};
use sp_core::crypto::AccountId32;
pub use user_input::UserInput;

Expand All @@ -23,7 +23,7 @@ impl Default for ChainInfo {
fn default() -> Self {
Self {
block_height: 0,
actor: MinimalRuntime::default_actor(),
actor: MinimalSandbox::default_actor(),
gas_limit: DEFAULT_GAS_LIMIT,
}
}
Expand Down Expand Up @@ -69,7 +69,7 @@ impl Default for UiState {
}

pub struct AppState {
pub session: Session<MinimalRuntime>,
pub session: Session<MinimalSandbox>,
pub chain_info: ChainInfo,
pub ui_state: UiState,
pub contracts: ContractRegistry,
Expand All @@ -78,7 +78,7 @@ pub struct AppState {
impl AppState {
pub fn new(cwd_override: Option<PathBuf>) -> Self {
AppState {
session: Session::new().expect("Failed to create drinking session"),
session: Session::default(),
chain_info: Default::default(),
ui_state: UiState::new(cwd_override),
contracts: Default::default(),
Expand Down
11 changes: 3 additions & 8 deletions drink-cli/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::env;

use anyhow::Result;
use clap::Parser;
use drink::Weight;
use drink::{sandbox::prelude::*, Weight};
use sp_core::crypto::AccountId32;

use crate::{app_state::AppState, cli::CliCommand};
Expand Down Expand Up @@ -67,20 +67,15 @@ pub fn execute(app_state: &mut AppState) -> Result<()> {
}

fn build_blocks(app_state: &mut AppState, count: u32) {
app_state.chain_info.block_height = app_state
.session
.sandbox()
.build_blocks(count)
.expect("Failed to build block - chain is broken");

app_state.chain_info.block_height = app_state.session.sandbox().build_blocks(count);
app_state.print(&format!("{count} blocks built"));
}

fn add_tokens(app_state: &mut AppState, recipient: AccountId32, value: u128) -> Result<()> {
app_state
.session
.sandbox()
.mint_into(recipient.clone(), value)
.mint_into(&recipient, value)
.map_err(|err| anyhow::format_err!("Failed to add token: {err:?}"))?;
app_state.print(&format!("{value} tokens added to {recipient}",));
Ok(())
Expand Down
3 changes: 2 additions & 1 deletion drink/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ sp-externalities = { workspace = true }
sp-io = { workspace = true }
sp-runtime-interface = { workspace = true }

serde_json = { workspace = true, optional = true }
paste = { workspace = true }
scale-info = { workspace = true }
serde_json = { workspace = true, optional = true }
thiserror = { workspace = true }
wat = { workspace = true }

Expand Down
6 changes: 0 additions & 6 deletions drink/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ pub enum Error {
/// Externalities could not be initialized.
#[error("Failed to build storage: {0}")]
StorageBuilding(String),
/// Block couldn't have been initialized.
#[error("Failed to initialize block: {0}")]
BlockInitialize(String),
/// Block couldn't have been finalized.
#[error("Failed to finalize block: {0}")]
BlockFinalize(String),
/// Bundle loading and parsing has failed
#[error("Loading the contract bundle has failed: {0}")]
BundleLoadFailed(String),
Expand Down
17 changes: 9 additions & 8 deletions drink/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@ use pallet_contracts::{ContractExecResult, ContractInstantiateResult};
#[cfg(feature = "session")]
pub use session::mock::{mock_message, ContractMock, MessageMock, MockedCallResult, Selector};
/// Export pallets that are used in the minimal runtime.
pub use {frame_support, frame_system, pallet_balances, pallet_contracts, pallet_timestamp};
pub use {
frame_support, frame_system, pallet_balances, pallet_contracts, pallet_timestamp, paste,
sp_externalities::Extension, sp_io::TestExternalities,
};

pub use crate::runtime::minimal::{self, MinimalRuntime};
use crate::runtime::AccountIdFor;
pub use crate::runtime::minimal::{self, MinimalSandbox};

/// Alias for `frame-system`'s `RuntimeCall` type.
pub type RuntimeCall<Runtime> = <Runtime as frame_system::Config>::RuntimeCall;

/// Alias for `pallet-balances`'s Balance type.
pub type BalanceOf<Runtime> =
<<Runtime as pallet_contracts::Config>::Currency as Inspect<AccountIdFor<Runtime>>>::Balance;
pub type RuntimeCall<R> = <R as frame_system::Config>::RuntimeCall;

/// Main result type for the drink crate.
pub type DrinkResult<T> = std::result::Result<T, Error>;
Expand All @@ -44,6 +42,9 @@ pub type EventRecordOf<Runtime> = EventRecord<
<Runtime as frame_system::Config>::Hash,
>;

type BalanceOf<R> =
<<R as pallet_contracts::Config>::Currency as Inspect<AccountIdFor<R>>>::Balance;

/// Copied from pallet-contracts.
pub type ContractInstantiateResultFor<Runtime> =
ContractInstantiateResult<AccountIdFor<Runtime>, BalanceOf<Runtime>, EventRecordOf<Runtime>>;
Expand Down
6 changes: 3 additions & 3 deletions drink/src/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! Module containing the [`Runtime`](Runtime) trait and its example implementations. You can use
//! `drink` with any runtime that implements the `Runtime` trait.
//! Module containing a [`MinimalSandbox`] that implements the [`crate::Sandbox`] trait.
//! Also contains debugging utilities for the contracts pallet.

pub mod minimal;
pub mod pallet_contracts_debugging;
pub use frame_metadata::RuntimeMetadataPrefixed;
pub use minimal::MinimalRuntime;
pub use minimal::MinimalSandbox;

/// The type of an account identifier.
pub type AccountIdFor<R> = <R as frame_system::Config>::AccountId;
Expand Down
Loading

0 comments on commit f21834d

Please sign in to comment.