From cb65bcb839dbcafe2602a9b254d1226cdd6a70f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Miko=C5=82ajczyk?= Date: Wed, 6 Sep 2023 16:08:10 +0200 Subject: [PATCH 1/8] Be generic over account-id --- Cargo.toml | 1 + drink-cli/src/app_state/mod.rs | 9 ++-- drink/src/chain_api.rs | 42 +++++++++++-------- drink/src/contract_api.rs | 42 +++++++++---------- drink/src/lib.rs | 4 +- drink/src/runtime/contracts_node.rs | 7 ++++ drink/src/runtime/minimal.rs | 11 ++++- drink/src/runtime/mod.rs | 15 ++++--- .../src/runtime/pallet_contracts_debugging.rs | 13 +++--- drink/src/session.rs | 31 +++++++------- 10 files changed, 101 insertions(+), 74 deletions(-) create mode 100644 drink/src/runtime/contracts_node.rs diff --git a/Cargo.toml b/Cargo.toml index 89a8b56..af0db96 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,6 +44,7 @@ pallet-timestamp = { version = "21.0.0" } sp-core = { version = "22.0.0" } sp-externalities = { version = "0.20.0" } sp-runtime-interface = { version = "18.0.0" } +contracts-node-runtime = { version = "0.31.0" } # Local dependencies diff --git a/drink-cli/src/app_state/mod.rs b/drink-cli/src/app_state/mod.rs index b62b864..63d780c 100644 --- a/drink-cli/src/app_state/mod.rs +++ b/drink-cli/src/app_state/mod.rs @@ -1,7 +1,8 @@ use std::{env, path::PathBuf}; pub use contracts::{Contract, ContractIndex, ContractRegistry}; -use drink::{runtime::MinimalRuntime, session::Session, Weight, DEFAULT_ACTOR, DEFAULT_GAS_LIMIT}; +use drink::runtime::Runtime; +use drink::{runtime::MinimalRuntime, session::Session, Weight, DEFAULT_GAS_LIMIT}; use sp_core::crypto::AccountId32; pub use user_input::UserInput; @@ -23,7 +24,7 @@ impl Default for ChainInfo { fn default() -> Self { Self { block_height: 0, - actor: DEFAULT_ACTOR, + actor: MinimalRuntime::default_actor(), gas_limit: DEFAULT_GAS_LIMIT, } } @@ -49,8 +50,8 @@ pub struct UiState { impl UiState { pub fn new(pwd_override: Option) -> Self { - let pwd = pwd_override.unwrap_or_else( - || env::current_dir().expect("Failed to get current directory")); + let pwd = pwd_override + .unwrap_or_else(|| env::current_dir().expect("Failed to get current directory")); UiState { pwd, diff --git a/drink/src/chain_api.rs b/drink/src/chain_api.rs index 9d52cf2..424fd3c 100644 --- a/drink/src/chain_api.rs +++ b/drink/src/chain_api.rs @@ -1,11 +1,10 @@ //! Basic chain API. use frame_support::{ - dispatch::Dispatchable, - sp_runtime::{AccountId32, DispatchResultWithInfo}, - traits::tokens::currency::Currency, + dispatch::Dispatchable, sp_runtime::DispatchResultWithInfo, traits::tokens::currency::Currency, }; +use crate::runtime::AccountId; use crate::{DrinkResult, Error, EventRecordOf, Runtime, Sandbox}; /// The runtime call type for a particular runtime. @@ -38,14 +37,14 @@ pub trait ChainApi { /// /// * `address` - The address of the account to add tokens to. /// * `amount` - The number of tokens to add. - fn add_tokens(&mut self, address: AccountId32, amount: u128); + fn add_tokens(&mut self, address: AccountId, amount: u128); /// Return the balance of an account. /// /// # Arguments /// /// * `address` - The address of the account to query. - fn balance(&mut self, address: &AccountId32) -> u128; + fn balance(&mut self, address: &AccountId) -> u128; /// Run an action without modifying the storage. /// @@ -88,13 +87,13 @@ impl ChainApi for Sandbox { }) } - fn add_tokens(&mut self, address: AccountId32, amount: u128) { + fn add_tokens(&mut self, address: AccountId, amount: u128) { self.externalities.execute_with(|| { let _ = pallet_balances::Pallet::::deposit_creating(&address, amount); }); } - fn balance(&mut self, address: &AccountId32) -> u128 { + fn balance(&mut self, address: &AccountId) -> u128 { self.externalities .execute_with(|| pallet_balances::Pallet::::free_balance(address)) } @@ -142,7 +141,7 @@ mod tests { use crate::{ chain_api::{ChainApi, DispatchResultWithInfo, RuntimeCall}, runtime::{minimal::RuntimeEvent, MinimalRuntime}, - AccountId32, Sandbox, DEFAULT_ACTOR, + AccountId32, Sandbox, }; fn make_transfer( @@ -153,20 +152,26 @@ mod tests { let call = RuntimeCall::::Balances( pallet_balances::Call::::transfer { dest, value }, ); - sandbox.runtime_call(call, Some(DEFAULT_ACTOR).into()) + sandbox.runtime_call(call, Some(MinimalRuntime::default_actor()).into()) } #[test] fn dry_run_works() { let mut sandbox = Sandbox::::new().expect("Failed to create sandbox"); - let initial_balance = sandbox.balance(&DEFAULT_ACTOR); + let initial_balance = sandbox.balance(&MinimalRuntime::default_actor()); sandbox.dry_run(|runtime| { - runtime.add_tokens(DEFAULT_ACTOR, 100); - assert_eq!(runtime.balance(&DEFAULT_ACTOR), initial_balance + 100); + runtime.add_tokens(MinimalRuntime::default_actor(), 100); + assert_eq!( + runtime.balance(&MinimalRuntime::default_actor()), + initial_balance + 100 + ); }); - assert_eq!(sandbox.balance(&DEFAULT_ACTOR), initial_balance); + assert_eq!( + sandbox.balance(&MinimalRuntime::default_actor()), + initial_balance + ); } #[test] @@ -174,7 +179,7 @@ mod tests { let mut sandbox = Sandbox::::new().expect("Failed to create sandbox"); const RECIPIENT: AccountId32 = AccountId32::new([2u8; 32]); - assert_ne!(DEFAULT_ACTOR, RECIPIENT); + assert_ne!(MinimalRuntime::default_actor(), RECIPIENT); let initial_balance = sandbox.balance(&RECIPIENT); let result = make_transfer(&mut sandbox, RECIPIENT, 100); @@ -191,7 +196,8 @@ mod tests { let events_before = sandbox.get_current_block_events(); assert!(events_before.is_empty()); - make_transfer(&mut sandbox, DEFAULT_ACTOR, 1).expect("Failed to make transfer"); + make_transfer(&mut sandbox, MinimalRuntime::default_actor(), 1) + .expect("Failed to make transfer"); let events_after = sandbox.get_current_block_events(); assert_eq!(events_after.len(), 1); @@ -202,13 +208,15 @@ mod tests { fn resetting_events() { let mut sandbox = Sandbox::::new().expect("Failed to create sandbox"); - make_transfer(&mut sandbox, DEFAULT_ACTOR, 1).expect("Failed to make transfer"); + make_transfer(&mut sandbox, MinimalRuntime::default_actor(), 1) + .expect("Failed to make transfer"); assert!(!sandbox.get_current_block_events().is_empty()); sandbox.reset_current_block_events(); assert!(sandbox.get_current_block_events().is_empty()); - make_transfer(&mut sandbox, DEFAULT_ACTOR, 1).expect("Failed to make transfer"); + make_transfer(&mut sandbox, MinimalRuntime::default_actor(), 1) + .expect("Failed to make transfer"); assert!(!sandbox.get_current_block_events().is_empty()); } } diff --git a/drink/src/contract_api.rs b/drink/src/contract_api.rs index b0c5235..ca803f4 100644 --- a/drink/src/contract_api.rs +++ b/drink/src/contract_api.rs @@ -1,12 +1,15 @@ //! Contracts API. -use frame_support::{sp_runtime::AccountId32, weights::Weight}; +use frame_support::weights::Weight; use pallet_contracts::{CollectEvents, DebugInfo, Determinism}; use pallet_contracts_primitives::{ Code, CodeUploadResult, ContractExecResult, ContractInstantiateResult, }; -use crate::{runtime::Runtime, EventRecordOf, Sandbox}; +use crate::{ + runtime::{AccountId, Runtime}, + EventRecordOf, Sandbox, +}; /// Interface for contract-related operations. pub trait ContractApi { @@ -28,10 +31,10 @@ pub trait ContractApi { value: u128, data: Vec, salt: Vec, - origin: AccountId32, + origin: AccountId, gas_limit: Weight, storage_deposit_limit: Option, - ) -> ContractInstantiateResult>; + ) -> ContractInstantiateResult, u128, EventRecordOf>; /// Interface for `bare_upload_code` contract call. /// @@ -43,7 +46,7 @@ pub trait ContractApi { fn upload_contract( &mut self, contract_bytes: Vec, - origin: AccountId32, + origin: AccountId, storage_deposit_limit: Option, ) -> CodeUploadResult<::Hash, u128>; @@ -59,10 +62,10 @@ pub trait ContractApi { /// * `storage_deposit_limit` - The storage deposit limit for the contract call. fn call_contract( &mut self, - address: AccountId32, + address: AccountId, value: u128, data: Vec, - origin: AccountId32, + origin: AccountId, gas_limit: Weight, storage_deposit_limit: Option, ) -> ContractExecResult>; @@ -75,10 +78,10 @@ impl ContractApi for Sandbox { value: u128, data: Vec, salt: Vec, - origin: AccountId32, + origin: AccountId, gas_limit: Weight, storage_deposit_limit: Option, - ) -> ContractInstantiateResult> { + ) -> ContractInstantiateResult, u128, EventRecordOf> { self.externalities.execute_with(|| { pallet_contracts::Pallet::::bare_instantiate( origin, @@ -97,7 +100,7 @@ impl ContractApi for Sandbox { fn upload_contract( &mut self, contract_bytes: Vec, - origin: AccountId32, + origin: AccountId, storage_deposit_limit: Option, ) -> CodeUploadResult<::Hash, u128> { self.externalities.execute_with(|| { @@ -112,10 +115,10 @@ impl ContractApi for Sandbox { fn call_contract( &mut self, - address: AccountId32, + address: AccountId, value: u128, data: Vec, - origin: AccountId32, + origin: AccountId, gas_limit: Weight, storage_deposit_limit: Option, ) -> ContractExecResult> { @@ -147,10 +150,7 @@ mod tests { use pallet_contracts::Origin; use super::*; - use crate::{ - chain_api::ChainApi, minimal::RuntimeEvent, MinimalRuntime, DEFAULT_ACTOR, - DEFAULT_GAS_LIMIT, - }; + use crate::{chain_api::ChainApi, minimal::RuntimeEvent, MinimalRuntime, DEFAULT_GAS_LIMIT}; fn compile_module(contract_name: &str) -> Vec { let path = [ @@ -171,7 +171,7 @@ mod tests { let wasm_binary = compile_module("dummy"); let hash = <::Hashing>::hash(&wasm_binary); - let result = sandbox.upload_contract(wasm_binary, DEFAULT_ACTOR, None); + let result = sandbox.upload_contract(wasm_binary, MinimalRuntime::default_actor(), None); assert!(result.is_ok()); assert_eq!(hash, result.unwrap().code_hash); @@ -190,7 +190,7 @@ mod tests { 0, vec![], vec![], - DEFAULT_ACTOR, + MinimalRuntime::default_actor(), DEFAULT_GAS_LIMIT, None, ); @@ -215,7 +215,7 @@ mod tests { 0, vec![], vec![], - DEFAULT_ACTOR, + MinimalRuntime::default_actor(), DEFAULT_GAS_LIMIT, None, ); @@ -231,7 +231,7 @@ mod tests { contract_address.clone(), 0, vec![], - DEFAULT_ACTOR, + MinimalRuntime::default_actor(), DEFAULT_GAS_LIMIT, None, ); @@ -253,7 +253,7 @@ mod tests { events[1].event, RuntimeEvent::Contracts(pallet_contracts::Event::::Called { contract: contract_address, - caller: Origin::Signed(DEFAULT_ACTOR), + caller: Origin::Signed(MinimalRuntime::default_actor()), }), ); } diff --git a/drink/src/lib.rs b/drink/src/lib.rs index e7bc280..def79a5 100644 --- a/drink/src/lib.rs +++ b/drink/src/lib.rs @@ -33,8 +33,6 @@ pub struct Sandbox { _phantom: PhantomData, } -/// Default extrinsic origin. -pub const DEFAULT_ACTOR: AccountId32 = AccountId32::new([1u8; 32]); /// Default gas limit. pub const DEFAULT_GAS_LIMIT: Weight = Weight::from_parts(100_000_000_000, 3 * 1024 * 1024); @@ -43,7 +41,7 @@ impl Sandbox { /// /// Returns an error if the storage could not be initialized. /// - /// The storage is initialized with a genesis block with a single account `DEFAULT_ACTOR` with + /// The storage is initialized with a genesis block with a single account `R::default_actor()` with /// `INITIAL_BALANCE`. pub fn new() -> DrinkResult { let mut storage = GenesisConfig::::default() diff --git a/drink/src/runtime/contracts_node.rs b/drink/src/runtime/contracts_node.rs new file mode 100644 index 0000000..c6e4146 --- /dev/null +++ b/drink/src/runtime/contracts_node.rs @@ -0,0 +1,7 @@ +//! This module provides the runtime of substrate-contracts-node in a form that can be used by +//! the drink framework. + +use crate::runtime::Runtime; +pub use contracts_node_runtime::Runtime as ContractsNodeRuntime; + +impl Runtime for ContractsNodeRuntime {} diff --git a/drink/src/runtime/minimal.rs b/drink/src/runtime/minimal.rs index 2b69ebb..741197d 100644 --- a/drink/src/runtime/minimal.rs +++ b/drink/src/runtime/minimal.rs @@ -19,7 +19,10 @@ pub use pallet_contracts; use pallet_contracts::{DefaultAddressGenerator, Frame, Schedule}; pub use pallet_timestamp; -use crate::{runtime::pallet_contracts_debugging::DrinkDebug, Runtime, DEFAULT_ACTOR}; +use crate::{ + runtime::{pallet_contracts_debugging::DrinkDebug, AccountId}, + Runtime, +}; type Block = frame_system::mocking::MockBlock; @@ -133,7 +136,7 @@ pub const INITIAL_BALANCE: u128 = 1_000_000_000_000_000; impl Runtime for MinimalRuntime { fn initialize_storage(storage: &mut Storage) -> Result<(), String> { pallet_balances::GenesisConfig:: { - balances: vec![(DEFAULT_ACTOR, INITIAL_BALANCE)], + balances: vec![(Self::default_actor(), INITIAL_BALANCE)], } .assimilate_storage(storage) } @@ -164,4 +167,8 @@ impl Runtime for MinimalRuntime { Ok(System::finalize().hash()) } + + fn default_actor() -> AccountId { + AccountId32::new([1u8; 32]) + } } diff --git a/drink/src/runtime/mod.rs b/drink/src/runtime/mod.rs index cf4f54c..7436831 100644 --- a/drink/src/runtime/mod.rs +++ b/drink/src/runtime/mod.rs @@ -1,20 +1,22 @@ //! Module containing the [`Runtime`](Runtime) trait and its example implementations. You can use //! `drink` with any runtime that implements the `Runtime` trait. +// pub mod contracts_node; pub mod minimal; pub mod pallet_contracts_debugging; -use frame_support::sp_runtime::{AccountId32, Storage}; +use frame_support::sp_runtime::Storage; pub use minimal::MinimalRuntime; +/// The type of an account identifier. +pub type AccountId = ::AccountId; + /// A runtime to use. /// /// Must contain at least system, balances and contracts pallets. pub trait Runtime: - frame_system::Config< - AccountId = AccountId32, - Block = frame_system::mocking::MockBlock, - > + pallet_balances::Config + frame_system::Config> + + pallet_balances::Config + pallet_contracts::Config> { /// Initialize the storage at the genesis block. @@ -34,4 +36,7 @@ pub trait Runtime: fn finalize_block(_height: u64) -> Result<::Hash, String> { Ok(Default::default()) } + + /// Default actor for the runtime. + fn default_actor() -> AccountId; } diff --git a/drink/src/runtime/pallet_contracts_debugging.rs b/drink/src/runtime/pallet_contracts_debugging.rs index f2ca81d..7fd837b 100644 --- a/drink/src/runtime/pallet_contracts_debugging.rs +++ b/drink/src/runtime/pallet_contracts_debugging.rs @@ -26,9 +26,7 @@ use pallet_contracts_primitives::ExecReturnValue; use sp_externalities::{decl_extension, ExternalitiesExt}; use sp_runtime_interface::runtime_interface; -use crate::runtime::Runtime; - -type AccountIdOf = ::AccountId; +use crate::runtime::{AccountId, Runtime}; /// The trait that allows injecting custom logic to handle contract debugging directly in the /// contracts pallet. @@ -73,10 +71,10 @@ trait ContractCallDebugger { pub enum DrinkDebug {} impl Tracing for DrinkDebug { - type CallSpan = DrinkCallSpan>; + type CallSpan = DrinkCallSpan>; fn new_call_span( - contract_address: &AccountIdOf, + contract_address: &AccountId, entry_point: ExportedFunction, input_data: &[u8], ) -> Self::CallSpan { @@ -101,11 +99,10 @@ pub struct DrinkCallSpan { pub input_data: Vec, } -impl> CallSpan for DrinkCallSpan { +impl CallSpan for DrinkCallSpan { fn after_call(self, output: &ExecReturnValue) { - let raw_contract_address: &[u8] = self.contract_address.as_ref(); contract_call_debugger::after_call( - raw_contract_address.to_vec(), + self.contract_address.encode(), matches!(self.entry_point, ExportedFunction::Call), self.input_data.to_vec(), output.data.clone(), diff --git a/drink/src/session.rs b/drink/src/session.rs index 2f636d8..e659a4e 100644 --- a/drink/src/session.rs +++ b/drink/src/session.rs @@ -9,8 +9,11 @@ use pallet_contracts_primitives::{ContractExecResult, ContractInstantiateResult} use thiserror::Error; use crate::{ - chain_api::ChainApi, contract_api::ContractApi, pallet_contracts_debugging::DebugExt, - runtime::Runtime, AccountId32, EventRecordOf, Sandbox, DEFAULT_ACTOR, DEFAULT_GAS_LIMIT, + chain_api::ChainApi, + contract_api::ContractApi, + pallet_contracts_debugging::DebugExt, + runtime::{AccountId, Runtime}, + EventRecordOf, Sandbox, DEFAULT_GAS_LIMIT, }; const ZERO_TRANSFER: u128 = 0; @@ -104,13 +107,13 @@ pub enum SessionError { pub struct Session { sandbox: Sandbox, - actor: AccountId32, + actor: AccountId, gas_limit: Weight, transcoder: Option>, - deploy_results: Vec>>, - deploy_returns: Vec, + deploy_results: Vec, u128, EventRecordOf>>, + deploy_returns: Vec>, call_results: Vec>>, call_returns: Vec, } @@ -120,7 +123,7 @@ impl Session { pub fn new(transcoder: Option>) -> Result { Ok(Self { sandbox: Sandbox::new().map_err(SessionError::Drink)?, - actor: DEFAULT_ACTOR, + actor: R::default_actor(), gas_limit: DEFAULT_GAS_LIMIT, transcoder, deploy_results: vec![], @@ -131,12 +134,12 @@ impl Session { } /// Sets a new actor and returns updated `self`. - pub fn with_actor(self, actor: AccountId32) -> Self { + pub fn with_actor(self, actor: AccountId) -> Self { Self { actor, ..self } } /// Sets a new actor and returns the old one. - pub fn set_actor(&mut self, actor: AccountId32) -> AccountId32 { + pub fn set_actor(&mut self, actor: AccountId) -> AccountId { mem::replace(&mut self.actor, actor) } @@ -194,7 +197,7 @@ impl Session { constructor: &str, args: &[String], salt: Vec, - ) -> Result { + ) -> Result, SessionError> { let data = self .transcoder .as_ref() @@ -236,7 +239,7 @@ impl Session { /// Calls the last deployed contract. In case of a successful call, returns `self`. pub fn call_with_address_and( mut self, - address: AccountId32, + address: AccountId, message: &str, args: &[String], ) -> Result { @@ -253,7 +256,7 @@ impl Session { /// result. pub fn call_with_address( &mut self, - address: AccountId32, + address: AccountId, message: &str, args: &[String], ) -> Result { @@ -262,7 +265,7 @@ impl Session { fn call_internal( &mut self, - address: Option, + address: Option>, message: &str, args: &[String], ) -> Result { @@ -314,12 +317,12 @@ impl Session { /// Returns the last result of deploying a contract. pub fn last_deploy_result( &self, - ) -> Option<&ContractInstantiateResult>> { + ) -> Option<&ContractInstantiateResult, u128, EventRecordOf>> { self.deploy_results.last() } /// Returns the address of the last deployed contract. - pub fn last_deploy_return(&self) -> Option { + pub fn last_deploy_return(&self) -> Option> { self.deploy_returns.last().cloned() } From 3e17adfbe3156efcd316dea7a1bb3cef0d0d0e30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Miko=C5=82ajczyk?= Date: Wed, 6 Sep 2023 16:31:11 +0200 Subject: [PATCH 2/8] Be generic over block number --- drink/Cargo.toml | 1 + drink/src/chain_api.rs | 22 ++++++++++++++-------- drink/src/lib.rs | 2 +- drink/src/runtime/contracts_node.rs | 8 ++++++-- drink/src/runtime/mod.rs | 9 ++++++--- 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/drink/Cargo.toml b/drink/Cargo.toml index 830cb43..f0aa38d 100644 --- a/drink/Cargo.toml +++ b/drink/Cargo.toml @@ -20,6 +20,7 @@ pallet-timestamp = { workspace = true } parity-scale-codec = { workspace = true } sp-externalities = { workspace = true } sp-runtime-interface = { workspace = true } +#contracts-node-runtime = { workspace = true } scale-info = { workspace = true } thiserror = { workspace = true } diff --git a/drink/src/chain_api.rs b/drink/src/chain_api.rs index 424fd3c..5c5c9c8 100644 --- a/drink/src/chain_api.rs +++ b/drink/src/chain_api.rs @@ -3,6 +3,8 @@ use frame_support::{ dispatch::Dispatchable, sp_runtime::DispatchResultWithInfo, traits::tokens::currency::Currency, }; +use frame_system::pallet_prelude::BlockNumberFor; +use std::ops::Add; use crate::runtime::AccountId; use crate::{DrinkResult, Error, EventRecordOf, Runtime, Sandbox}; @@ -13,20 +15,23 @@ pub type RuntimeCall = ::RuntimeCall; /// Interface for basic chain operations. pub trait ChainApi { /// Return the current height of the chain. - fn current_height(&mut self) -> u64; + fn current_height(&mut self) -> BlockNumberFor; /// Build a new empty block and return the new height. - fn build_block(&mut self) -> DrinkResult; + fn build_block(&mut self) -> DrinkResult>; /// Build `n` empty blocks and return the new height. /// /// # Arguments /// /// * `n` - The number of blocks to build. - fn build_blocks(&mut self, n: u64) -> DrinkResult { + fn build_blocks(&mut self, n: BlockNumberFor) -> DrinkResult> { let mut last_block = None; - for _ in 0..n { + + let mut iter = BlockNumberFor::::from(0u32); + while iter < n { last_block = Some(self.build_block()?); + iter = iter.add(BlockNumberFor::::from(1u32)); } Ok(last_block.unwrap_or_else(|| self.current_height())) } @@ -73,17 +78,18 @@ pub trait ChainApi { } impl ChainApi for Sandbox { - fn current_height(&mut self) -> u64 { + fn current_height(&mut self) -> BlockNumberFor { self.externalities .execute_with(|| frame_system::Pallet::::block_number()) } - fn build_block(&mut self) -> DrinkResult { + fn build_block(&mut self) -> DrinkResult> { let current_block = self.current_height(); self.externalities.execute_with(|| { let block_hash = R::finalize_block(current_block).map_err(Error::BlockFinalize)?; - R::initialize_block(current_block + 1, block_hash).map_err(Error::BlockInitialize)?; - Ok(current_block + 1) + let new_number = current_block + BlockNumberFor::::from(1u32); + R::initialize_block(new_number, block_hash).map_err(Error::BlockInitialize)?; + Ok(new_number) }) } diff --git a/drink/src/lib.rs b/drink/src/lib.rs index def79a5..b76af83 100644 --- a/drink/src/lib.rs +++ b/drink/src/lib.rs @@ -59,7 +59,7 @@ impl Sandbox { .externalities // We start the chain from the 1st block, so that events are collected (they are not // recorded for the genesis block...). - .execute_with(|| R::initialize_block(1, Default::default())) + .execute_with(|| R::initialize_block(1u32.into(), Default::default())) .map_err(Error::BlockInitialize)?; // We register a noop debug extension by default. diff --git a/drink/src/runtime/contracts_node.rs b/drink/src/runtime/contracts_node.rs index c6e4146..8f826c3 100644 --- a/drink/src/runtime/contracts_node.rs +++ b/drink/src/runtime/contracts_node.rs @@ -1,7 +1,11 @@ //! This module provides the runtime of substrate-contracts-node in a form that can be used by //! the drink framework. -use crate::runtime::Runtime; +use crate::runtime::{AccountId, Runtime}; pub use contracts_node_runtime::Runtime as ContractsNodeRuntime; -impl Runtime for ContractsNodeRuntime {} +impl Runtime for ContractsNodeRuntime { + fn default_actor() -> AccountId { + todo!() + } +} diff --git a/drink/src/runtime/mod.rs b/drink/src/runtime/mod.rs index 7436831..b85e1a8 100644 --- a/drink/src/runtime/mod.rs +++ b/drink/src/runtime/mod.rs @@ -6,6 +6,7 @@ pub mod minimal; pub mod pallet_contracts_debugging; use frame_support::sp_runtime::Storage; +use frame_system::pallet_prelude::BlockNumberFor; pub use minimal::MinimalRuntime; /// The type of an account identifier. @@ -15,7 +16,7 @@ pub type AccountId = ::AccountId; /// /// Must contain at least system, balances and contracts pallets. pub trait Runtime: - frame_system::Config> + frame_system::Config + pallet_balances::Config + pallet_contracts::Config> { @@ -26,14 +27,16 @@ pub trait Runtime: /// Initialize a new block at particular height. fn initialize_block( - _height: u64, + _height: BlockNumberFor, _parent_hash: ::Hash, ) -> Result<(), String> { Ok(()) } /// Finalize a block at particular height. - fn finalize_block(_height: u64) -> Result<::Hash, String> { + fn finalize_block( + _height: BlockNumberFor, + ) -> Result<::Hash, String> { Ok(Default::default()) } From 95b3008ee25c71e7195f2396875fa17949dc41dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Miko=C5=82ajczyk?= Date: Wed, 6 Sep 2023 19:02:22 +0200 Subject: [PATCH 3/8] Cleanup --- Cargo.lock | 21 +++++++++++---------- Cargo.toml | 4 ++-- drink/Cargo.toml | 2 +- drink/src/runtime/contracts_node.rs | 11 ----------- drink/src/runtime/mod.rs | 1 - 5 files changed, 14 insertions(+), 25 deletions(-) delete mode 100644 drink/src/runtime/contracts_node.rs diff --git a/Cargo.lock b/Cargo.lock index a1adf25..ff25a88 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -948,6 +948,7 @@ dependencies = [ "pallet-contracts-primitives", "pallet-timestamp", "parity-scale-codec", + "parity-scale-codec-derive", "scale-info", "sp-externalities", "sp-runtime-interface", @@ -2447,9 +2448,9 @@ dependencies = [ [[package]] name = "parity-scale-codec" -version = "3.6.4" +version = "3.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd8e946cc0cc711189c0b0249fb8b599cbeeab9784d83c415719368bb8d4ac64" +checksum = "0dec8a8073036902368c2cdc0387e85ff9a37054d7e7c98e592145e0c92cd4fb" dependencies = [ "arrayvec 0.7.3", "bitvec", @@ -2462,9 +2463,9 @@ dependencies = [ [[package]] name = "parity-scale-codec-derive" -version = "3.6.4" +version = "3.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2a296c3079b5fefbc499e1de58dc26c09b1b9a5952d26694ee89f04a43ebbb3e" +checksum = "312270ee71e1cd70289dacf597cab7b207aa107d2f28191c2ae45b2ece18a260" dependencies = [ "proc-macro-crate", "proc-macro2", @@ -2619,9 +2620,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.32" +version = "1.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50f3b39ccfb720540debaa0164757101c08ecb8d326b15358ce76a62c7e85965" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" dependencies = [ "proc-macro2", ] @@ -3083,18 +3084,18 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.183" +version = "1.0.188" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32ac8da02677876d532745a130fc9d8e6edfa81a269b107c5b00829b91d8eb3c" +checksum = "cf9e0fcba69a370eed61bcf2b728575f726b50b55cba78064753d708ddc7549e" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.183" +version = "1.0.188" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aafe972d60b0b9bee71a91b92fee2d4fb3c9d7e8f6b179aa99f27203d99a4816" +checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index af0db96..cc572b5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,8 @@ clap = { version = "4.3.4" } contract-build = { version = "3.0.1" } contract-transcode = { version = "3.0.1" } crossterm = { version = "0.26.0" } -parity-scale-codec = { version = "3.6.0" } +parity-scale-codec = { version = "=3.6.5" } +parity-scale-codec-derive = { version = "=3.6.5" } ratatui = { version = "0.21.0" } scale-info = { version = "2.5.0" } thiserror = { version = "1.0.40" } @@ -44,7 +45,6 @@ pallet-timestamp = { version = "21.0.0" } sp-core = { version = "22.0.0" } sp-externalities = { version = "0.20.0" } sp-runtime-interface = { version = "18.0.0" } -contracts-node-runtime = { version = "0.31.0" } # Local dependencies diff --git a/drink/Cargo.toml b/drink/Cargo.toml index f0aa38d..f3042c3 100644 --- a/drink/Cargo.toml +++ b/drink/Cargo.toml @@ -18,9 +18,9 @@ pallet-contracts = { workspace = true } pallet-contracts-primitives = { workspace = true } pallet-timestamp = { workspace = true } parity-scale-codec = { workspace = true } +parity-scale-codec-derive = { workspace = true } sp-externalities = { workspace = true } sp-runtime-interface = { workspace = true } -#contracts-node-runtime = { workspace = true } scale-info = { workspace = true } thiserror = { workspace = true } diff --git a/drink/src/runtime/contracts_node.rs b/drink/src/runtime/contracts_node.rs deleted file mode 100644 index 8f826c3..0000000 --- a/drink/src/runtime/contracts_node.rs +++ /dev/null @@ -1,11 +0,0 @@ -//! This module provides the runtime of substrate-contracts-node in a form that can be used by -//! the drink framework. - -use crate::runtime::{AccountId, Runtime}; -pub use contracts_node_runtime::Runtime as ContractsNodeRuntime; - -impl Runtime for ContractsNodeRuntime { - fn default_actor() -> AccountId { - todo!() - } -} diff --git a/drink/src/runtime/mod.rs b/drink/src/runtime/mod.rs index b85e1a8..7f4c9a6 100644 --- a/drink/src/runtime/mod.rs +++ b/drink/src/runtime/mod.rs @@ -1,7 +1,6 @@ //! Module containing the [`Runtime`](Runtime) trait and its example implementations. You can use //! `drink` with any runtime that implements the `Runtime` trait. -// pub mod contracts_node; pub mod minimal; pub mod pallet_contracts_debugging; From f3cc8931f2b2e02a8ec0a7ff1faf7430c1c00746 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Miko=C5=82ajczyk?= Date: Wed, 6 Sep 2023 19:12:50 +0200 Subject: [PATCH 4/8] Cleanup more --- drink/src/chain_api.rs | 24 +++++++++--------------- drink/src/contract_api.rs | 7 ++++--- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/drink/src/chain_api.rs b/drink/src/chain_api.rs index 5c5c9c8..cc88146 100644 --- a/drink/src/chain_api.rs +++ b/drink/src/chain_api.rs @@ -146,7 +146,7 @@ mod tests { use crate::{ chain_api::{ChainApi, DispatchResultWithInfo, RuntimeCall}, - runtime::{minimal::RuntimeEvent, MinimalRuntime}, + runtime::{minimal::RuntimeEvent, MinimalRuntime, Runtime}, AccountId32, Sandbox, }; @@ -164,20 +164,15 @@ mod tests { #[test] fn dry_run_works() { let mut sandbox = Sandbox::::new().expect("Failed to create sandbox"); - let initial_balance = sandbox.balance(&MinimalRuntime::default_actor()); + let actor = MinimalRuntime::default_actor(); + let initial_balance = sandbox.balance(&actor); sandbox.dry_run(|runtime| { - runtime.add_tokens(MinimalRuntime::default_actor(), 100); - assert_eq!( - runtime.balance(&MinimalRuntime::default_actor()), - initial_balance + 100 - ); + runtime.add_tokens(actor.clone(), 100); + assert_eq!(runtime.balance(&actor), initial_balance + 100); }); - assert_eq!( - sandbox.balance(&MinimalRuntime::default_actor()), - initial_balance - ); + assert_eq!(sandbox.balance(&actor), initial_balance); } #[test] @@ -213,16 +208,15 @@ mod tests { #[test] fn resetting_events() { let mut sandbox = Sandbox::::new().expect("Failed to create sandbox"); + let actor = MinimalRuntime::default_actor(); - make_transfer(&mut sandbox, MinimalRuntime::default_actor(), 1) - .expect("Failed to make transfer"); + make_transfer(&mut sandbox, actor.clone(), 1).expect("Failed to make transfer"); assert!(!sandbox.get_current_block_events().is_empty()); sandbox.reset_current_block_events(); assert!(sandbox.get_current_block_events().is_empty()); - make_transfer(&mut sandbox, MinimalRuntime::default_actor(), 1) - .expect("Failed to make transfer"); + make_transfer(&mut sandbox, actor, 1).expect("Failed to make transfer"); assert!(!sandbox.get_current_block_events().is_empty()); } } diff --git a/drink/src/contract_api.rs b/drink/src/contract_api.rs index ca803f4..0e06698 100644 --- a/drink/src/contract_api.rs +++ b/drink/src/contract_api.rs @@ -208,6 +208,7 @@ mod tests { #[test] fn can_call_contract() { let mut sandbox = Sandbox::::new().unwrap(); + let actor = MinimalRuntime::default_actor(); let wasm_binary = compile_module("dummy"); let result = sandbox.deploy_contract( @@ -215,7 +216,7 @@ mod tests { 0, vec![], vec![], - MinimalRuntime::default_actor(), + actor.clone(), DEFAULT_GAS_LIMIT, None, ); @@ -231,7 +232,7 @@ mod tests { contract_address.clone(), 0, vec![], - MinimalRuntime::default_actor(), + actor.clone(), DEFAULT_GAS_LIMIT, None, ); @@ -253,7 +254,7 @@ mod tests { events[1].event, RuntimeEvent::Contracts(pallet_contracts::Event::::Called { contract: contract_address, - caller: Origin::Signed(MinimalRuntime::default_actor()), + caller: Origin::Signed(actor), }), ); } From f9a4b38b2e59fa12c6005a604a76cc1f4beb1553 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Miko=C5=82ajczyk?= Date: Wed, 6 Sep 2023 19:13:21 +0200 Subject: [PATCH 5/8] fmt --- drink-cli/src/app_state/mod.rs | 7 +++++-- drink-cli/src/executor/error.rs | 6 ++++-- drink-cli/src/main.rs | 5 +++-- drink-cli/src/ui/mod.rs | 3 +-- drink/src/chain_api.rs | 6 +++--- drink/src/lib.rs | 7 ++++--- 6 files changed, 20 insertions(+), 14 deletions(-) diff --git a/drink-cli/src/app_state/mod.rs b/drink-cli/src/app_state/mod.rs index 63d780c..c00e7eb 100644 --- a/drink-cli/src/app_state/mod.rs +++ b/drink-cli/src/app_state/mod.rs @@ -1,8 +1,11 @@ use std::{env, path::PathBuf}; pub use contracts::{Contract, ContractIndex, ContractRegistry}; -use drink::runtime::Runtime; -use drink::{runtime::MinimalRuntime, session::Session, Weight, DEFAULT_GAS_LIMIT}; +use drink::{ + runtime::{MinimalRuntime, Runtime}, + session::Session, + Weight, DEFAULT_GAS_LIMIT, +}; use sp_core::crypto::AccountId32; pub use user_input::UserInput; diff --git a/drink-cli/src/executor/error.rs b/drink-cli/src/executor/error.rs index 515f55a..f56b532 100644 --- a/drink-cli/src/executor/error.rs +++ b/drink-cli/src/executor/error.rs @@ -1,10 +1,12 @@ use thiserror::Error; - #[derive(Error, Debug)] pub(crate) enum BuildError { #[error("Invalid manifest path {manifest_path}: {err}")] - InvalidManifest { manifest_path: std::path::PathBuf, err: anyhow::Error }, + InvalidManifest { + manifest_path: std::path::PathBuf, + err: anyhow::Error, + }, #[error("Contract build failed: {err}")] BuildFailed { err: anyhow::Error }, #[error("Wasm code artifact not generated")] diff --git a/drink-cli/src/main.rs b/drink-cli/src/main.rs index c2f3109..e3c4d96 100644 --- a/drink-cli/src/main.rs +++ b/drink-cli/src/main.rs @@ -1,6 +1,7 @@ +use std::path::PathBuf; + use anyhow::Result; use clap::Parser; -use std::path::PathBuf; use crate::ui::run_ui; @@ -13,7 +14,7 @@ mod ui; #[command(author, version, about, long_about = None)] struct Args { /// Starts the CLI in the provided directory - #[arg(short, long, value_name = "DIRECTORY", )] + #[arg(short, long, value_name = "DIRECTORY")] path: Option, } diff --git a/drink-cli/src/ui/mod.rs b/drink-cli/src/ui/mod.rs index 93988bd..8b85227 100644 --- a/drink-cli/src/ui/mod.rs +++ b/drink-cli/src/ui/mod.rs @@ -6,8 +6,7 @@ mod layout; mod output; mod user_input; -use std::{io, io::Stdout}; -use std::path::PathBuf; +use std::{io, io::Stdout, path::PathBuf}; use anyhow::{anyhow, Result}; use crossterm::{ diff --git a/drink/src/chain_api.rs b/drink/src/chain_api.rs index cc88146..2530caf 100644 --- a/drink/src/chain_api.rs +++ b/drink/src/chain_api.rs @@ -1,13 +1,13 @@ //! Basic chain API. +use std::ops::Add; + use frame_support::{ dispatch::Dispatchable, sp_runtime::DispatchResultWithInfo, traits::tokens::currency::Currency, }; use frame_system::pallet_prelude::BlockNumberFor; -use std::ops::Add; -use crate::runtime::AccountId; -use crate::{DrinkResult, Error, EventRecordOf, Runtime, Sandbox}; +use crate::{runtime::AccountId, DrinkResult, Error, EventRecordOf, Runtime, Sandbox}; /// The runtime call type for a particular runtime. pub type RuntimeCall = ::RuntimeCall; diff --git a/drink/src/lib.rs b/drink/src/lib.rs index b76af83..8691242 100644 --- a/drink/src/lib.rs +++ b/drink/src/lib.rs @@ -16,9 +16,10 @@ use frame_support::{sp_io::TestExternalities, sp_runtime::BuildStorage}; pub use frame_support::{sp_runtime::AccountId32, weights::Weight}; use frame_system::{EventRecord, GenesisConfig}; -use crate::pallet_contracts_debugging::DebugExt; -use crate::runtime::pallet_contracts_debugging::NoopDebugExt; -use crate::runtime::*; +use crate::{ + pallet_contracts_debugging::DebugExt, + runtime::{pallet_contracts_debugging::NoopDebugExt, *}, +}; /// Main result type for the drink crate. pub type DrinkResult = std::result::Result; From 4c0c60ecd261bd229959fd56fe0947d1b2691520 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Miko=C5=82ajczyk?= Date: Wed, 6 Sep 2023 19:16:29 +0200 Subject: [PATCH 6/8] Bump --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cc572b5..a078c5b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ homepage = "https://github.com/Cardinal-Cryptography/drink" license = "Apache-2.0" readme = "README.md" repository = "https://github.com/Cardinal-Cryptography/drink" -version = "0.1.3" +version = "0.1.4" [workspace.dependencies] anyhow = { version = "1.0.71" } @@ -48,4 +48,4 @@ sp-runtime-interface = { version = "18.0.0" } # Local dependencies -drink = { version = "0.1.3", path = "drink" } +drink = { version = "0.1.4", path = "drink" } From ffe32d1284cd5b0c14aaae1099637a64240a07bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Miko=C5=82ajczyk?= Date: Wed, 6 Sep 2023 20:23:54 +0200 Subject: [PATCH 7/8] number should be u32 --- Cargo.lock | 4 ++-- drink/src/runtime/minimal.rs | 13 +++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ff25a88..8963da0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -938,7 +938,7 @@ checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" [[package]] name = "drink" -version = "0.1.3" +version = "0.1.4" dependencies = [ "contract-transcode", "frame-support", @@ -958,7 +958,7 @@ dependencies = [ [[package]] name = "drink-cli" -version = "0.1.3" +version = "0.1.4" dependencies = [ "anyhow", "clap", diff --git a/drink/src/runtime/minimal.rs b/drink/src/runtime/minimal.rs index 741197d..6359dec 100644 --- a/drink/src/runtime/minimal.rs +++ b/drink/src/runtime/minimal.rs @@ -14,6 +14,7 @@ use frame_support::{ }; // Re-export all pallets. pub use frame_system; +use frame_system::pallet_prelude::BlockNumberFor; pub use pallet_balances; pub use pallet_contracts; use pallet_contracts::{DefaultAddressGenerator, Frame, Schedule}; @@ -24,7 +25,7 @@ use crate::{ Runtime, }; -type Block = frame_system::mocking::MockBlock; +type Block = frame_system::mocking::MockBlockU32; frame_support::construct_runtime!( pub enum MinimalRuntime { @@ -48,7 +49,7 @@ impl frame_system::Config for MinimalRuntime { type AccountId = AccountId32; type Lookup = IdentityLookup; type RuntimeEvent = RuntimeEvent; - type BlockHashCount = ConstU64<250>; + type BlockHashCount = ConstU32<250>; type DbWeight = (); type Version = (); type PalletInfo = PalletInfo; @@ -85,8 +86,8 @@ impl pallet_timestamp::Config for MinimalRuntime { } pub enum SandboxRandomness {} -impl Randomness for SandboxRandomness { - fn random(_subject: &[u8]) -> (H256, u64) { +impl Randomness for SandboxRandomness { + fn random(_subject: &[u8]) -> (H256, u32) { todo!("No randomness") } } @@ -141,7 +142,7 @@ impl Runtime for MinimalRuntime { .assimilate_storage(storage) } - fn initialize_block(height: u64, parent_hash: H256) -> Result<(), String> { + fn initialize_block(height: BlockNumberFor, parent_hash: H256) -> Result<(), String> { System::reset_events(); System::initialize(&height, &parent_hash, &Default::default()); @@ -160,7 +161,7 @@ impl Runtime for MinimalRuntime { Ok(()) } - fn finalize_block(height: u64) -> Result { + fn finalize_block(height: BlockNumberFor) -> Result { Contracts::on_finalize(height); Timestamp::on_finalize(height); Balances::on_finalize(height); From 05bdc85604bb2d988c2a68199f5e8dbb1f6dcf23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Miko=C5=82ajczyk?= Date: Wed, 6 Sep 2023 20:30:32 +0200 Subject: [PATCH 8/8] review --- drink-cli/src/app_state/mod.rs | 14 ++++----- drink-cli/src/cli.rs | 2 +- drink-cli/src/executor/contract.rs | 12 +++---- drink-cli/src/executor/mod.rs | 6 ++-- drink-cli/src/ui/current_env.rs | 2 +- drink-cli/src/ui/mod.rs | 8 ++--- drink/src/chain_api.rs | 31 +++++++++---------- drink/src/contract_api.rs | 22 ++++++------- drink/src/lib.rs | 9 ++++-- drink/src/runtime/minimal.rs | 4 +-- drink/src/runtime/mod.rs | 4 +-- .../src/runtime/pallet_contracts_debugging.rs | 6 ++-- drink/src/session.rs | 24 +++++++------- 13 files changed, 72 insertions(+), 72 deletions(-) diff --git a/drink-cli/src/app_state/mod.rs b/drink-cli/src/app_state/mod.rs index c00e7eb..8f2f2f0 100644 --- a/drink-cli/src/app_state/mod.rs +++ b/drink-cli/src/app_state/mod.rs @@ -18,7 +18,7 @@ mod user_input; #[derive(Clone, Eq, PartialEq, Debug)] pub struct ChainInfo { - pub block_height: u64, + pub block_height: u32, pub actor: AccountId32, pub gas_limit: Weight, } @@ -42,7 +42,7 @@ pub enum Mode { #[derive(Clone, Eq, PartialEq, Debug)] pub struct UiState { - pub pwd: PathBuf, + pub cwd: PathBuf, pub mode: Mode, pub user_input: UserInput, @@ -52,12 +52,12 @@ pub struct UiState { } impl UiState { - pub fn new(pwd_override: Option) -> Self { - let pwd = pwd_override + pub fn new(cwd_override: Option) -> Self { + let cwd = cwd_override .unwrap_or_else(|| env::current_dir().expect("Failed to get current directory")); UiState { - pwd, + cwd, mode: Default::default(), user_input: Default::default(), output: Default::default(), @@ -80,11 +80,11 @@ pub struct AppState { } impl AppState { - pub fn new(pwd_override: Option) -> Self { + pub fn new(cwd_override: Option) -> Self { AppState { session: Session::new(None).expect("Failed to create drinking session"), chain_info: Default::default(), - ui_state: UiState::new(pwd_override), + ui_state: UiState::new(cwd_override), contracts: Default::default(), } } diff --git a/drink-cli/src/cli.rs b/drink-cli/src/cli.rs index 9aa420b..af70c2b 100644 --- a/drink-cli/src/cli.rs +++ b/drink-cli/src/cli.rs @@ -13,7 +13,7 @@ pub enum CliCommand { #[clap(alias = "nb")] NextBlock { #[clap(default_value = "1")] - count: u64, + count: u32, }, AddTokens { #[clap(value_parser = AccountId32::from_ss58check)] diff --git a/drink-cli/src/executor/contract.rs b/drink-cli/src/executor/contract.rs index b9086bf..627fc95 100644 --- a/drink-cli/src/executor/contract.rs +++ b/drink-cli/src/executor/contract.rs @@ -13,7 +13,7 @@ use crate::{ }; fn build_result(app_state: &mut AppState) -> Result { - let path_to_cargo_toml = app_state.ui_state.pwd.join(Path::new("Cargo.toml")); + let path_to_cargo_toml = app_state.ui_state.cwd.join(Path::new("Cargo.toml")); let manifest_path = ManifestPath::new(path_to_cargo_toml.clone()).map_err(|err| { BuildError::InvalidManifest { manifest_path: path_to_cargo_toml, @@ -48,7 +48,7 @@ pub fn build(app_state: &mut AppState) { pub fn deploy(app_state: &mut AppState, constructor: String, args: Vec, salt: Vec) { // Get raw contract bytes - let Some((contract_name, contract_file)) = find_wasm_blob(&app_state.ui_state.pwd) else { + let Some((contract_name, contract_file)) = find_wasm_blob(&app_state.ui_state.cwd) else { app_state.print_error("Failed to find contract file"); return; }; @@ -64,7 +64,7 @@ pub fn deploy(app_state: &mut AppState, constructor: String, args: Vec, // Read contract metadata and prepare transcoder let metadata_path = app_state .ui_state - .pwd + .cwd .join(format!("target/ink/{contract_name}.json")); let Ok(transcoder) = ContractMessageTranscoder::load(metadata_path) else { @@ -82,7 +82,7 @@ pub fn deploy(app_state: &mut AppState, constructor: String, args: Vec, app_state.contracts.add(Contract { name: contract_name, address, - base_path: app_state.ui_state.pwd.clone(), + base_path: app_state.ui_state.cwd.clone(), transcoder, }); app_state.print("Contract deployed successfully"); @@ -119,8 +119,8 @@ pub fn call(app_state: &mut AppState, message: String, args: Vec) { } } -fn find_wasm_blob(pwd: &Path) -> Option<(String, PathBuf)> { - let Ok(entries) = fs::read_dir(pwd.join("target/ink")) else { +fn find_wasm_blob(cwd: &Path) -> Option<(String, PathBuf)> { + let Ok(entries) = fs::read_dir(cwd.join("target/ink")) else { return None; }; let Some(file) = entries diff --git a/drink-cli/src/executor/mod.rs b/drink-cli/src/executor/mod.rs index ad79a6f..90dc9c4 100644 --- a/drink-cli/src/executor/mod.rs +++ b/drink-cli/src/executor/mod.rs @@ -29,10 +29,10 @@ pub fn execute(app_state: &mut AppState) -> Result<()> { match cli_command { CliCommand::Clear => app_state.ui_state.output.clear(), CliCommand::ChangeDir { path } => { - let target_dir = app_state.ui_state.pwd.join(path); + let target_dir = app_state.ui_state.cwd.join(path); match env::set_current_dir(target_dir) { Ok(_) => { - app_state.ui_state.pwd = + app_state.ui_state.cwd = env::current_dir().expect("Failed to get current directory"); app_state.print("Directory changed"); } @@ -66,7 +66,7 @@ pub fn execute(app_state: &mut AppState) -> Result<()> { Ok(()) } -fn build_blocks(app_state: &mut AppState, count: u64) { +fn build_blocks(app_state: &mut AppState, count: u32) { app_state.chain_info.block_height = app_state .session .chain_api() diff --git a/drink-cli/src/ui/current_env.rs b/drink-cli/src/ui/current_env.rs index e7de94b..1891510 100644 --- a/drink-cli/src/ui/current_env.rs +++ b/drink-cli/src/ui/current_env.rs @@ -17,7 +17,7 @@ Block height: {} Deployed contracts: {} Current actor: {} Current contract: {{ {} }}"#, - app_state.ui_state.pwd.to_str().unwrap(), + app_state.ui_state.cwd.to_str().unwrap(), app_state.chain_info.block_height, app_state.contracts.count(), app_state.chain_info.actor, diff --git a/drink-cli/src/ui/mod.rs b/drink-cli/src/ui/mod.rs index 8b85227..3748722 100644 --- a/drink-cli/src/ui/mod.rs +++ b/drink-cli/src/ui/mod.rs @@ -28,9 +28,9 @@ use crate::{ type Terminal = ratatui::Terminal>; -pub fn run_ui(pwd: Option) -> Result<()> { +pub fn run_ui(cwd: Option) -> Result<()> { let mut terminal = setup_dedicated_terminal()?; - let app_result = run_ui_app(&mut terminal, pwd); + let app_result = run_ui_app(&mut terminal, cwd); restore_original_terminal(terminal)?; app_result } @@ -53,8 +53,8 @@ fn restore_original_terminal(mut terminal: Terminal) -> Result<()> { terminal.show_cursor().map_err(|e| anyhow!(e)) } -fn run_ui_app(terminal: &mut Terminal, pwd_override: Option) -> Result<()> { - let mut app_state = AppState::new(pwd_override); +fn run_ui_app(terminal: &mut Terminal, cwd_override: Option) -> Result<()> { + let mut app_state = AppState::new(cwd_override); loop { terminal.draw(|f| layout(f, &mut app_state))?; diff --git a/drink/src/chain_api.rs b/drink/src/chain_api.rs index 2530caf..2be3b5d 100644 --- a/drink/src/chain_api.rs +++ b/drink/src/chain_api.rs @@ -1,13 +1,13 @@ //! Basic chain API. -use std::ops::Add; - use frame_support::{ - dispatch::Dispatchable, sp_runtime::DispatchResultWithInfo, traits::tokens::currency::Currency, + dispatch::Dispatchable, + sp_runtime::{DispatchResultWithInfo, Saturating}, + traits::tokens::currency::Currency, }; use frame_system::pallet_prelude::BlockNumberFor; -use crate::{runtime::AccountId, DrinkResult, Error, EventRecordOf, Runtime, Sandbox}; +use crate::{runtime::AccountIdFor, DrinkResult, Error, EventRecordOf, Runtime, Sandbox}; /// The runtime call type for a particular runtime. pub type RuntimeCall = ::RuntimeCall; @@ -25,13 +25,10 @@ pub trait ChainApi { /// # Arguments /// /// * `n` - The number of blocks to build. - fn build_blocks(&mut self, n: BlockNumberFor) -> DrinkResult> { + fn build_blocks(&mut self, n: u32) -> DrinkResult> { let mut last_block = None; - - let mut iter = BlockNumberFor::::from(0u32); - while iter < n { + for _ in 0..n { last_block = Some(self.build_block()?); - iter = iter.add(BlockNumberFor::::from(1u32)); } Ok(last_block.unwrap_or_else(|| self.current_height())) } @@ -42,14 +39,14 @@ pub trait ChainApi { /// /// * `address` - The address of the account to add tokens to. /// * `amount` - The number of tokens to add. - fn add_tokens(&mut self, address: AccountId, amount: u128); + fn add_tokens(&mut self, address: AccountIdFor, amount: u128); /// Return the balance of an account. /// /// # Arguments /// /// * `address` - The address of the account to query. - fn balance(&mut self, address: &AccountId) -> u128; + fn balance(&mut self, address: &AccountIdFor) -> u128; /// Run an action without modifying the storage. /// @@ -84,22 +81,22 @@ impl ChainApi for Sandbox { } fn build_block(&mut self) -> DrinkResult> { - let current_block = self.current_height(); + let mut current_block = self.current_height(); self.externalities.execute_with(|| { let block_hash = R::finalize_block(current_block).map_err(Error::BlockFinalize)?; - let new_number = current_block + BlockNumberFor::::from(1u32); - R::initialize_block(new_number, block_hash).map_err(Error::BlockInitialize)?; - Ok(new_number) + current_block.saturating_inc(); + R::initialize_block(current_block, block_hash).map_err(Error::BlockInitialize)?; + Ok(current_block) }) } - fn add_tokens(&mut self, address: AccountId, amount: u128) { + fn add_tokens(&mut self, address: AccountIdFor, amount: u128) { self.externalities.execute_with(|| { let _ = pallet_balances::Pallet::::deposit_creating(&address, amount); }); } - fn balance(&mut self, address: &AccountId) -> u128 { + fn balance(&mut self, address: &AccountIdFor) -> u128 { self.externalities .execute_with(|| pallet_balances::Pallet::::free_balance(address)) } diff --git a/drink/src/contract_api.rs b/drink/src/contract_api.rs index 0e06698..7104654 100644 --- a/drink/src/contract_api.rs +++ b/drink/src/contract_api.rs @@ -7,7 +7,7 @@ use pallet_contracts_primitives::{ }; use crate::{ - runtime::{AccountId, Runtime}, + runtime::{AccountIdFor, Runtime}, EventRecordOf, Sandbox, }; @@ -31,10 +31,10 @@ pub trait ContractApi { value: u128, data: Vec, salt: Vec, - origin: AccountId, + origin: AccountIdFor, gas_limit: Weight, storage_deposit_limit: Option, - ) -> ContractInstantiateResult, u128, EventRecordOf>; + ) -> ContractInstantiateResult, u128, EventRecordOf>; /// Interface for `bare_upload_code` contract call. /// @@ -46,7 +46,7 @@ pub trait ContractApi { fn upload_contract( &mut self, contract_bytes: Vec, - origin: AccountId, + origin: AccountIdFor, storage_deposit_limit: Option, ) -> CodeUploadResult<::Hash, u128>; @@ -62,10 +62,10 @@ pub trait ContractApi { /// * `storage_deposit_limit` - The storage deposit limit for the contract call. fn call_contract( &mut self, - address: AccountId, + address: AccountIdFor, value: u128, data: Vec, - origin: AccountId, + origin: AccountIdFor, gas_limit: Weight, storage_deposit_limit: Option, ) -> ContractExecResult>; @@ -78,10 +78,10 @@ impl ContractApi for Sandbox { value: u128, data: Vec, salt: Vec, - origin: AccountId, + origin: AccountIdFor, gas_limit: Weight, storage_deposit_limit: Option, - ) -> ContractInstantiateResult, u128, EventRecordOf> { + ) -> ContractInstantiateResult, u128, EventRecordOf> { self.externalities.execute_with(|| { pallet_contracts::Pallet::::bare_instantiate( origin, @@ -100,7 +100,7 @@ impl ContractApi for Sandbox { fn upload_contract( &mut self, contract_bytes: Vec, - origin: AccountId, + origin: AccountIdFor, storage_deposit_limit: Option, ) -> CodeUploadResult<::Hash, u128> { self.externalities.execute_with(|| { @@ -115,10 +115,10 @@ impl ContractApi for Sandbox { fn call_contract( &mut self, - address: AccountId, + address: AccountIdFor, value: u128, data: Vec, - origin: AccountId, + origin: AccountIdFor, gas_limit: Weight, storage_deposit_limit: Option, ) -> ContractExecResult> { diff --git a/drink/src/lib.rs b/drink/src/lib.rs index 8691242..74df6c4 100644 --- a/drink/src/lib.rs +++ b/drink/src/lib.rs @@ -12,9 +12,12 @@ pub mod session; use std::marker::PhantomData; pub use error::Error; -use frame_support::{sp_io::TestExternalities, sp_runtime::BuildStorage}; +use frame_support::{ + sp_io::TestExternalities, + sp_runtime::{traits::One, BuildStorage}, +}; pub use frame_support::{sp_runtime::AccountId32, weights::Weight}; -use frame_system::{EventRecord, GenesisConfig}; +use frame_system::{pallet_prelude::BlockNumberFor, EventRecord, GenesisConfig}; use crate::{ pallet_contracts_debugging::DebugExt, @@ -60,7 +63,7 @@ impl Sandbox { .externalities // We start the chain from the 1st block, so that events are collected (they are not // recorded for the genesis block...). - .execute_with(|| R::initialize_block(1u32.into(), Default::default())) + .execute_with(|| R::initialize_block(BlockNumberFor::::one(), Default::default())) .map_err(Error::BlockInitialize)?; // We register a noop debug extension by default. diff --git a/drink/src/runtime/minimal.rs b/drink/src/runtime/minimal.rs index 6359dec..f8863b6 100644 --- a/drink/src/runtime/minimal.rs +++ b/drink/src/runtime/minimal.rs @@ -21,7 +21,7 @@ use pallet_contracts::{DefaultAddressGenerator, Frame, Schedule}; pub use pallet_timestamp; use crate::{ - runtime::{pallet_contracts_debugging::DrinkDebug, AccountId}, + runtime::{pallet_contracts_debugging::DrinkDebug, AccountIdFor}, Runtime, }; @@ -169,7 +169,7 @@ impl Runtime for MinimalRuntime { Ok(System::finalize().hash()) } - fn default_actor() -> AccountId { + fn default_actor() -> AccountIdFor { AccountId32::new([1u8; 32]) } } diff --git a/drink/src/runtime/mod.rs b/drink/src/runtime/mod.rs index 7f4c9a6..c862c16 100644 --- a/drink/src/runtime/mod.rs +++ b/drink/src/runtime/mod.rs @@ -9,7 +9,7 @@ use frame_system::pallet_prelude::BlockNumberFor; pub use minimal::MinimalRuntime; /// The type of an account identifier. -pub type AccountId = ::AccountId; +pub type AccountIdFor = ::AccountId; /// A runtime to use. /// @@ -40,5 +40,5 @@ pub trait Runtime: } /// Default actor for the runtime. - fn default_actor() -> AccountId; + fn default_actor() -> AccountIdFor; } diff --git a/drink/src/runtime/pallet_contracts_debugging.rs b/drink/src/runtime/pallet_contracts_debugging.rs index 7fd837b..47504a3 100644 --- a/drink/src/runtime/pallet_contracts_debugging.rs +++ b/drink/src/runtime/pallet_contracts_debugging.rs @@ -26,7 +26,7 @@ use pallet_contracts_primitives::ExecReturnValue; use sp_externalities::{decl_extension, ExternalitiesExt}; use sp_runtime_interface::runtime_interface; -use crate::runtime::{AccountId, Runtime}; +use crate::runtime::{AccountIdFor, Runtime}; /// The trait that allows injecting custom logic to handle contract debugging directly in the /// contracts pallet. @@ -71,10 +71,10 @@ trait ContractCallDebugger { pub enum DrinkDebug {} impl Tracing for DrinkDebug { - type CallSpan = DrinkCallSpan>; + type CallSpan = DrinkCallSpan>; fn new_call_span( - contract_address: &AccountId, + contract_address: &AccountIdFor, entry_point: ExportedFunction, input_data: &[u8], ) -> Self::CallSpan { diff --git a/drink/src/session.rs b/drink/src/session.rs index e659a4e..8d3fbba 100644 --- a/drink/src/session.rs +++ b/drink/src/session.rs @@ -12,7 +12,7 @@ use crate::{ chain_api::ChainApi, contract_api::ContractApi, pallet_contracts_debugging::DebugExt, - runtime::{AccountId, Runtime}, + runtime::{AccountIdFor, Runtime}, EventRecordOf, Sandbox, DEFAULT_GAS_LIMIT, }; @@ -107,13 +107,13 @@ pub enum SessionError { pub struct Session { sandbox: Sandbox, - actor: AccountId, + actor: AccountIdFor, gas_limit: Weight, transcoder: Option>, - deploy_results: Vec, u128, EventRecordOf>>, - deploy_returns: Vec>, + deploy_results: Vec, u128, EventRecordOf>>, + deploy_returns: Vec>, call_results: Vec>>, call_returns: Vec, } @@ -134,12 +134,12 @@ impl Session { } /// Sets a new actor and returns updated `self`. - pub fn with_actor(self, actor: AccountId) -> Self { + pub fn with_actor(self, actor: AccountIdFor) -> Self { Self { actor, ..self } } /// Sets a new actor and returns the old one. - pub fn set_actor(&mut self, actor: AccountId) -> AccountId { + pub fn set_actor(&mut self, actor: AccountIdFor) -> AccountIdFor { mem::replace(&mut self.actor, actor) } @@ -197,7 +197,7 @@ impl Session { constructor: &str, args: &[String], salt: Vec, - ) -> Result, SessionError> { + ) -> Result, SessionError> { let data = self .transcoder .as_ref() @@ -239,7 +239,7 @@ impl Session { /// Calls the last deployed contract. In case of a successful call, returns `self`. pub fn call_with_address_and( mut self, - address: AccountId, + address: AccountIdFor, message: &str, args: &[String], ) -> Result { @@ -256,7 +256,7 @@ impl Session { /// result. pub fn call_with_address( &mut self, - address: AccountId, + address: AccountIdFor, message: &str, args: &[String], ) -> Result { @@ -265,7 +265,7 @@ impl Session { fn call_internal( &mut self, - address: Option>, + address: Option>, message: &str, args: &[String], ) -> Result { @@ -317,12 +317,12 @@ impl Session { /// Returns the last result of deploying a contract. pub fn last_deploy_result( &self, - ) -> Option<&ContractInstantiateResult, u128, EventRecordOf>> { + ) -> Option<&ContractInstantiateResult, u128, EventRecordOf>> { self.deploy_results.last() } /// Returns the address of the last deployed contract. - pub fn last_deploy_return(&self) -> Option> { + pub fn last_deploy_return(&self) -> Option> { self.deploy_returns.last().cloned() }