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 time for off-chain testing #1721

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
3 changes: 2 additions & 1 deletion 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
- Basic support for `dyn Trait` to allow cross-contract calls only with trait - [#1673](https://github.com/paritytech/ink/pull/1673)
- E2E: auto detect contracts to be built - [#1691](https://github.com/paritytech/ink/pull/1691)
- Add `set_code_hash` to `EnvAccess` - [#1698](https://github.com/paritytech/ink/pull/1698)
- Add `set_block_timestamp` to off-chain test api `Engine` - [#1721](https://github.com/paritytech/ink/pull/1721)

## Version 4.0.1

Expand Down Expand Up @@ -1931,4 +1932,4 @@ impl Contract {

This is useful if the `impl` block itself does not contain any ink! constructors or messages, but you
still need to access some of the "magic" provided by ink!. In the example above, you would not have
access to `emit_event` without `#[ink(impl)]`.
access to `emit_event` without `#[ink(impl)]`.
5 changes: 5 additions & 0 deletions crates/engine/src/exec_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ impl ExecContext {
pub fn reset(&mut self) {
*self = Default::default();
}

/// Set the block timestamp for the execution context.
pub fn set_block_timestamp(&mut self, block_timestamp: BlockTimestamp) {
self.block_timestamp = block_timestamp
}
}

#[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,
BlockTimestamp,
},
AccountError,
Error,
Expand Down Expand Up @@ -257,6 +258,11 @@ impl Engine {
pub fn set_value_transferred(&mut self, value: Balance) {
self.exec_context.value_transferred = value;
}

/// Set the block timestamp for the execution context.
pub fn set_block_timestamp(&mut self, new_block_timestamp: BlockTimestamp) {
CrommVardek marked this conversation as resolved.
Show resolved Hide resolved
self.exec_context.block_timestamp = new_block_timestamp;
}
}

#[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 @@ -272,3 +272,21 @@ fn ecdsa_recovery_with_secp256k1_crate() {
// then
assert_eq!(output, pubkey.serialize());
}

#[test]
fn setting_getting_block_timestamp() {
// given
let mut engine = Engine::new();
let new_block_timestamp: u64 = 1000;
let output = &mut &mut get_buffer()[..];

// when
engine.advance_block();
engine.set_block_timestamp(new_block_timestamp);
engine.block_timestamp(output);
Daanvdplas marked this conversation as resolved.
Show resolved Hide resolved

// then
let output = <u64 as scale::Decode>::decode(&mut &output[..16])
.expect("decoding value transferred failed");
assert_eq!(output, new_block_timestamp);
}
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 @@ -240,6 +240,16 @@ where
})
}

/// Sets the block timestamp for the next [`advance_block`] invocation.
pub fn set_block_timestamp<T>(value: T::Timestamp)
where
T: Environment<Timestamp = u64>,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
instance.engine.set_block_timestamp(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