Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting block number for off-chain testing #1806

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added
- Schema generation - [#1765](https://github.com/paritytech/ink/pull/1765)
- Add `set_block_number` to off-chain test api `Engine` - [#1806](https://github.com/paritytech/ink/pull/1806)

### Changed
- E2E: improve call API, remove `build_message` + callback - [#1782](https://github.com/paritytech/ink/pull/1782)
Expand Down
5 changes: 5 additions & 0 deletions crates/engine/src/exec_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ impl ExecContext {
pub fn set_block_timestamp(&mut self, block_timestamp: BlockTimestamp) {
self.block_timestamp = block_timestamp
}

/// Set the block number for the execution context.
pub fn set_block_number(&mut self, block_number: BlockNumber) {
self.block_number = block_number
}
}

#[cfg(test)]
Expand Down
6 changes: 6 additions & 0 deletions crates/engine/src/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::{
types::{
AccountId,
Balance,
BlockNumber,
BlockTimestamp,
},
AccountError,
Expand Down Expand Up @@ -273,6 +274,11 @@ impl Engine {
pub fn set_block_timestamp(&mut self, new_block_timestamp: BlockTimestamp) {
self.exec_context.block_timestamp = new_block_timestamp;
}

/// Set the block number for the execution context.
pub fn set_block_number(&mut self, new_block_number: BlockNumber) {
self.exec_context.block_number = new_block_number;
}
}

#[cfg(test)]
Expand Down
18 changes: 18 additions & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,21 @@ fn setting_getting_block_timestamp() {
.expect("decoding value transferred failed");
assert_eq!(output, new_block_timestamp);
}

#[test]
fn setting_getting_block_number() {
// given
let mut engine = Engine::new();
let new_block_number: u32 = 1000;
let output = &mut &mut get_buffer()[..];

// when
engine.advance_block();
engine.set_block_number(new_block_number);
engine.block_number(output);

// then
let output = <u32 as scale::Decode>::decode(&mut &output[..16])
.expect("decoding value transferred failed");
assert_eq!(output, new_block_number);
}
10 changes: 10 additions & 0 deletions crates/env/src/engine/off_chain/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,16 @@ where
})
}

/// Sets the block number for the next [`advance_block`] invocation.
pub fn set_block_number<T>(value: T::BlockNumber)
where
T: Environment<BlockNumber = u32>,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
instance.engine.set_block_number(value);
})
}

/// Runs the given closure test function with the default configuration
/// for the off-chain environment.
pub fn run_test<T, F>(f: F) -> Result<()>
Expand Down