Skip to content
This repository has been archived by the owner on Feb 11, 2025. It is now read-only.

Commit

Permalink
More minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pgherveou committed Feb 22, 2024
1 parent d35b9c1 commit bd5ad50
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 32 deletions.
7 changes: 1 addition & 6 deletions drink-cli/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,7 @@ 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"));
}

Expand Down
1 change: 0 additions & 1 deletion drink/src/runtime/minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ mod construct_runtime {
type FreezeIdentifier = ();
type MaxLocks = ();
type MaxReserves = ();
type MaxHolds = ConstU32<1>;
type MaxFreezes = ();
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeFreezeReason = RuntimeFreezeReason;
Expand Down
6 changes: 3 additions & 3 deletions drink/src/sandbox/contracts_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ mod tests {

#[test]
fn can_upload_code() {
let mut sandbox = Sandbox::<MinimalRuntime>::default();
let sandbox = Sandbox::<MinimalRuntime>::default();
let wasm_binary = compile_module("dummy");
let hash = <<MinimalRuntime as frame_system::Config>::Hashing>::hash(&wasm_binary);

Expand All @@ -218,7 +218,7 @@ mod tests {

#[test]
fn can_deploy_contract() {
let mut sandbox = Sandbox::<MinimalRuntime>::default();
let sandbox = Sandbox::<MinimalRuntime>::default();
let wasm_binary = compile_module("dummy");

let events_before = sandbox.events();
Expand Down Expand Up @@ -254,7 +254,7 @@ mod tests {

#[test]
fn can_call_contract() {
let mut sandbox = Sandbox::<MinimalRuntime>::default();
let sandbox = Sandbox::<MinimalRuntime>::default();
let actor = MinimalRuntime::default_actor();
let wasm_binary = compile_module("dummy");

Expand Down
11 changes: 5 additions & 6 deletions drink/src/sandbox/runtime_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,28 @@ use frame_support::sp_runtime::traits::Saturating;
use frame_system::pallet_prelude::BlockNumberFor;

use super::Sandbox;
use crate::DrinkResult;

impl<Config: crate::SandboxConfig> Sandbox<Config> {
/// Build a new empty block and return the new height.
pub fn build_block(&mut self) -> DrinkResult<BlockNumberFor<Config::Runtime>> {
pub fn build_block(&self) -> BlockNumberFor<Config::Runtime> {
self.execute_with(|| {
let mut current_block = frame_system::Pallet::<Config::Runtime>::block_number();
let block_hash = Config::finalize_block(current_block);
current_block.saturating_inc();
Config::initialize_block(current_block, block_hash);
Ok(current_block)
current_block
})
}
/// Build `n` empty blocks and return the new height.
///
/// # Arguments
///
/// * `n` - The number of blocks to build.
pub fn build_blocks(&mut self, n: u32) -> DrinkResult<BlockNumberFor<Config::Runtime>> {
pub fn build_blocks(&self, n: u32) -> BlockNumberFor<Config::Runtime> {
let mut last_block = None;
for _ in 0..n {
last_block = Some(self.build_block()?);
last_block = Some(self.build_block());
}
Ok(last_block.unwrap_or_else(|| self.block_number()))
last_block.unwrap_or_else(|| self.block_number())
}
}
24 changes: 12 additions & 12 deletions drink/src/sandbox/system_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ use crate::{EventRecordOf, RuntimeCall, SandboxConfig};

impl<Config: SandboxConfig> Sandbox<Config> {
/// Return the current height of the chain.
pub fn block_number(&mut self) -> BlockNumberFor<Config::Runtime> {
pub fn block_number(&self) -> BlockNumberFor<Config::Runtime> {
self.execute_with(frame_system::Pallet::<Config::Runtime>::block_number)
}

/// Return the events of the current block so far.
pub fn events(&mut self) -> Vec<EventRecordOf<Config::Runtime>> {
pub fn events(&self) -> Vec<EventRecordOf<Config::Runtime>> {
self.execute_with(frame_system::Pallet::<Config::Runtime>::events)
}

/// Reset the events of the current block.
pub fn reset_events(&mut self) {
pub fn reset_events(&self) {
self.execute_with(frame_system::Pallet::<Config::Runtime>::reset_events)
}

Expand All @@ -31,7 +31,7 @@ impl<Config: SandboxConfig> Sandbox<Config> {
pub fn runtime_call<
Origin: Into<<RuntimeCall<Config::Runtime> as Dispatchable>::RuntimeOrigin>,
>(
&mut self,
&self,
call: RuntimeCall<Config::Runtime>,
origin: Origin,
) -> DispatchResultWithInfo<<RuntimeCall<Config::Runtime> as Dispatchable>::PostInfo> {
Expand All @@ -49,7 +49,7 @@ mod tests {
};

fn make_transfer(
sandbox: &mut Sandbox<MinimalRuntime>,
sandbox: &Sandbox<MinimalRuntime>,
dest: AccountId32,
value: u128,
) -> DispatchResultWithInfo<<RuntimeCall<MinimalRuntime> as Dispatchable>::PostInfo> {
Expand Down Expand Up @@ -85,12 +85,12 @@ mod tests {

#[test]
fn runtime_call_works() {
let mut sandbox = Sandbox::<MinimalRuntime>::default();
let sandbox = Sandbox::<MinimalRuntime>::default();

const RECIPIENT: AccountId32 = AccountId32::new([2u8; 32]);
let initial_balance = sandbox.free_balance(&RECIPIENT);

let result = make_transfer(&mut sandbox, RECIPIENT, 100);
let result = make_transfer(&sandbox, RECIPIENT, 100);
assert!(result.is_ok());

let expected_balance = initial_balance + 100;
Expand All @@ -99,13 +99,13 @@ mod tests {

#[test]
fn current_events() {
let mut sandbox = Sandbox::<MinimalRuntime>::default();
let sandbox = Sandbox::<MinimalRuntime>::default();
const RECIPIENT: AccountId32 = AccountId32::new([2u8; 32]);

let events_before = sandbox.events();
assert!(events_before.is_empty());

make_transfer(&mut sandbox, RECIPIENT, 1).expect("Failed to make transfer");
make_transfer(&sandbox, RECIPIENT, 1).expect("Failed to make transfer");

let events_after = sandbox.events();
assert!(!events_after.is_empty());
Expand All @@ -117,16 +117,16 @@ mod tests {

#[test]
fn resetting_events() {
let mut sandbox = Sandbox::<MinimalRuntime>::default();
let sandbox = Sandbox::<MinimalRuntime>::default();
const RECIPIENT: AccountId32 = AccountId32::new([3u8; 32]);

make_transfer(&mut sandbox, RECIPIENT.clone(), 1).expect("Failed to make transfer");
make_transfer(&sandbox, RECIPIENT.clone(), 1).expect("Failed to make transfer");

assert!(!sandbox.events().is_empty());
sandbox.reset_events();
assert!(sandbox.events().is_empty());

make_transfer(&mut sandbox, RECIPIENT, 1).expect("Failed to make transfer");
make_transfer(&sandbox, RECIPIENT, 1).expect("Failed to make transfer");
assert!(!sandbox.events().is_empty());
}
}
4 changes: 2 additions & 2 deletions drink/src/sandbox/timestamp_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ mod tests {

#[test]
fn getting_and_setting_timestamp_works() {
let mut sandbox = Sandbox::<MinimalRuntime>::default();
let sandbox = Sandbox::<MinimalRuntime>::default();
for timestamp in 0..10 {
assert_ne!(sandbox.get_timestamp(), timestamp);
sandbox.set_timestamp(timestamp);
assert_eq!(sandbox.get_timestamp(), timestamp);

sandbox.build_block().expect("Failed to build block");
sandbox.build_block();
}
}
}
4 changes: 2 additions & 2 deletions examples/runtime-interaction/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod tests {
#[test]
fn we_can_make_a_token_transfer_call() {
// We create a sandbox object, which represents a blockchain runtime.
let mut sandbox = Sandbox::<MinimalRuntime>::default();
let sandbox = Sandbox::<MinimalRuntime>::default();

// Bob will be the recipient of the transfer.
const BOB: AccountId32 = AccountId32::new([2u8; 32]);
Expand Down Expand Up @@ -39,7 +39,7 @@ mod tests {

#[test]
fn we_can_work_with_the_contracts_pallet_in_low_level() {
let mut sandbox = Sandbox::<MinimalRuntime>::default();
let sandbox = Sandbox::<MinimalRuntime>::default();

// A few runtime calls are also available directly from the sandbox. This includes a part of
// the contracts API.
Expand Down

0 comments on commit bd5ad50

Please sign in to comment.