Skip to content

Commit

Permalink
Move sandbox to drink_sandbox (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgherveou authored Mar 19, 2024
1 parent 25e416f commit 4e93b94
Show file tree
Hide file tree
Showing 43 changed files with 3,114 additions and 4,301 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.15.0]

- Migrate `Sandbox` related code to `ink_sandbox` crate.

## [Unreleased]

## [0.14.0]
Expand Down
39 changes: 26 additions & 13 deletions Cargo.lock

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

15 changes: 4 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ homepage = "https://github.com/Cardinal-Cryptography/drink"
license = "Apache-2.0"
readme = "README.md"
repository = "https://github.com/Cardinal-Cryptography/drink"
version = "0.14.0"
version = "0.15.0"

[workspace.dependencies]
anyhow = { version = "1.0.71" }
Expand All @@ -41,22 +41,15 @@ serde_json = { version = "1.0" }
syn = { version = "2" }
thiserror = { version = "1.0.40" }
wat = { version = "1.0.71" }
ink_sandbox = { version = "5.0.0" }

# Substrate dependencies

frame-metadata = { version = "16.0.0" }
frame-support = { version = "30.0.0"}
frame-system = { version = "30.0.0" }
pallet-balances = { version = "30.0.0" }
pallet-contracts = { version = "29.0.0" }
pallet-contracts-uapi = { package = "pallet-contracts-uapi-next", version = "=6.0.3", default-features = false }
pallet-timestamp = { version = "29.0.0"}
sp-core = { version = "30.0.0" }
sp-externalities = { version = "0.27.0" }
sp-io = { version = "32.0.0" }
sp-runtime-interface = { version = "26.0.0" }

# Local dependencies

drink = { version = "=0.14.0", path = "drink" }
drink-test-macro = { version = "=0.14.0", path = "drink/test-macro" }
drink = { version = "=0.15.0", path = "drink" }
drink-test-macro = { version = "=0.15.0", path = "drink/test-macro" }
4 changes: 1 addition & 3 deletions drink-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,5 @@ contract-transcode = { workspace = true }
ratatui = { workspace = true, features = ["all-widgets"] }
thiserror = { workspace = true }

pallet-contracts = { workspace = true }
sp-core = { workspace = true, features = ["serde"] }

ink_sandbox = { workspace = true }
drink = { workspace = true, features = ["session"] }
2 changes: 1 addition & 1 deletion drink-cli/src/app_state/contracts.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{path::PathBuf, rc::Rc};

use contract_transcode::ContractMessageTranscoder;
use sp_core::crypto::AccountId32;
use drink::AccountId32;
use ContractIndex::NoContracts;

use crate::app_state::ContractIndex::CurrentContract;
Expand Down
5 changes: 2 additions & 3 deletions drink-cli/src/app_state/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::{env, path::PathBuf};

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

use crate::app_state::output::Output;
Expand All @@ -24,7 +23,7 @@ impl Default for ChainInfo {
Self {
block_height: 0,
actor: MinimalSandbox::default_actor(),
gas_limit: DEFAULT_GAS_LIMIT,
gas_limit: MinimalSandbox::default_gas_limit(),
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions drink-cli/src/app_state/print.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use drink::contracts_api::decode_debug_buffer;
use pallet_contracts::ContractResult;
use drink::{pallet_contracts::ContractResult, sandbox_api::contracts_api::decode_debug_buffer};
use ratatui::{
style::{Color, Modifier, Style},
text::Span,
Expand Down
2 changes: 1 addition & 1 deletion drink-cli/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::Parser;
use sp_core::crypto::{AccountId32, Ss58Codec};
use drink::{AccountId32, Ss58Codec};

#[derive(Parser)]
pub enum CliCommand {
Expand Down
3 changes: 1 addition & 2 deletions drink-cli/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use std::env;

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

use crate::{app_state::AppState, cli::CliCommand};

Expand Down
9 changes: 1 addition & 8 deletions drink/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,13 @@ description = "Minimal sufficient architecture that allows for a fully functiona
[dependencies]
contract-metadata = { workspace = true, optional = true}
contract-transcode = { workspace = true, optional = true }
frame-metadata = { workspace = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
pallet-balances = { workspace = true }
pallet-contracts = { workspace = true }
pallet-contracts-uapi = { workspace = true }
pallet-timestamp = { workspace = true }
parity-scale-codec = { workspace = true }
parity-scale-codec-derive = { workspace = true }
sp-externalities = { workspace = true }
sp-io = { workspace = true }
sp-runtime-interface = { workspace = true }
ink_sandbox = { workspace = true }

paste = { workspace = true }
scale-info = { workspace = true }
serde_json = { workspace = true, optional = true }
thiserror = { workspace = true }
Expand Down
55 changes: 17 additions & 38 deletions drink/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,33 @@
#![warn(missing_docs)]

pub mod errors;
pub mod runtime;
pub mod sandbox;
pub use sandbox::*;
pub mod pallet_contracts_debugging;
#[cfg(feature = "session")]
pub mod session;

#[cfg(feature = "macros")]
pub use drink_test_macro::{contract_bundle_provider, test};
pub use errors::Error;
use frame_support::traits::fungible::Inspect;
pub use frame_support::{
sp_runtime::{AccountId32, DispatchError},
weights::Weight,
pub use frame_support;
pub use ink_sandbox::{
api as sandbox_api, create_sandbox, pallet_balances, pallet_contracts, pallet_timestamp,
sp_externalities, AccountId32, DispatchError, Sandbox, Ss58Codec, Weight,
};
use frame_system::EventRecord;
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, paste,
sp_externalities::Extension, sp_io::TestExternalities,
};

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

/// Alias for `frame-system`'s `RuntimeCall` type.
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>;

/// Copied from pallet-contracts.
pub type EventRecordOf<Runtime> = EventRecord<
<Runtime as frame_system::Config>::RuntimeEvent,
<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>>;

/// Copied from pallet-contracts.
pub type ContractExecResultFor<Runtime> =
ContractExecResult<BalanceOf<Runtime>, EventRecordOf<Runtime>>;

/// Default gas limit.
pub const DEFAULT_GAS_LIMIT: Weight = Weight::from_parts(100_000_000_000, 3 * 1024 * 1024);
/// Minimal Sandbox runtime used for testing contracts with drink!.
#[allow(missing_docs)]
pub mod minimal {
use ink_sandbox::create_sandbox;

// create_sandbox!(MinimalSandbox);
create_sandbox!(
MinimalSandbox,
(),
crate::pallet_contracts_debugging::DrinkDebug
);
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use pallet_contracts::debug::{CallInterceptor, ExecResult, ExportedFunction};
use ink_sandbox::AccountIdFor;
use parity_scale_codec::{Decode, Encode};

use crate::runtime::{
use crate::{
pallet_contracts::{
debug::{CallInterceptor, ExecResult, ExportedFunction},
Config,
},
pallet_contracts_debugging::{runtime::contract_call_debugger, DrinkDebug},
AccountIdFor,
};

impl<R: pallet_contracts::Config> CallInterceptor<R> for DrinkDebug {
impl<R: Config> CallInterceptor<R> for DrinkDebug {
fn intercept_call(
contract_address: &AccountIdFor<R>,
entry_point: &ExportedFunction,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use parity_scale_codec::Encode;
use sp_externalities::{decl_extension, ExternalitiesExt};
use sp_runtime_interface::runtime_interface;

use crate::sp_externalities::{decl_extension, ExternalitiesExt};

/// Contracts pallet outsources debug callbacks through this runtime interface.
///
/// Essentially, in our case, it just exposes extensions to the runtime.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use pallet_contracts::{
debug::{CallSpan, ExportedFunction},
ExecReturnValue, Tracing,
};
use ink_sandbox::AccountIdFor;

use crate::runtime::{pallet_contracts_debugging::DrinkDebug, AccountIdFor};
use crate::{
pallet_contracts::{
debug::{CallSpan, ExportedFunction},
Config, ExecReturnValue, Tracing,
},
pallet_contracts_debugging::DrinkDebug,
};

impl<R: pallet_contracts::Config> Tracing<R> for DrinkDebug {
impl<R: Config> Tracing<R> for DrinkDebug {
type CallSpan = DrinkCallSpan<AccountIdFor<R>>;

fn new_call_span(
Expand Down Expand Up @@ -36,7 +39,7 @@ pub struct DrinkCallSpan<AccountId> {

impl<AccountId: parity_scale_codec::Encode> CallSpan for DrinkCallSpan<AccountId> {
fn after_call(self, output: &ExecReturnValue) {
crate::runtime::pallet_contracts_debugging::runtime::contract_call_debugger::after_call(
crate::pallet_contracts_debugging::runtime::contract_call_debugger::after_call(
self.contract_address.encode(),
matches!(self.entry_point, ExportedFunction::Call),
self.input_data.to_vec(),
Expand Down
13 changes: 0 additions & 13 deletions drink/src/runtime.rs

This file was deleted.

Loading

0 comments on commit 4e93b94

Please sign in to comment.