Skip to content

Commit

Permalink
Allow setting block number for off-chain testing (#1806)
Browse files Browse the repository at this point in the history
* Add set_block_number for offchain test api env to resolve #1805

* Update CHANGELOG with PR#1806
  • Loading branch information
ltfschoen authored Jun 13, 2023
1 parent dda4cea commit 2eba99e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
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

0 comments on commit 2eba99e

Please sign in to comment.