From 89b0ca4de30e1116cce1c9e1ac6820c10cec8b5b Mon Sep 17 00:00:00 2001 From: Cyril Carlier Date: Wed, 15 Mar 2023 22:09:12 +0100 Subject: [PATCH 01/12] Add Engine::set_block_time and test accordingly --- crates/engine/src/exec_context.rs | 17 +++++++---------- crates/engine/src/ext.rs | 6 ++++++ crates/engine/src/tests.rs | 17 +++++++++++++++++ 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/crates/engine/src/exec_context.rs b/crates/engine/src/exec_context.rs index 32d069e471e..c7aa839bfbb 100644 --- a/crates/engine/src/exec_context.rs +++ b/crates/engine/src/exec_context.rs @@ -12,12 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use super::types::{ - AccountId, - Balance, - BlockNumber, - BlockTimestamp, -}; +use super::types::{AccountId, Balance, BlockNumber, BlockTimestamp}; /// The context of a contract execution. #[cfg_attr(test, derive(Debug, PartialEq, Eq))] @@ -64,14 +59,16 @@ impl ExecContext { pub fn reset(&mut self) { *self = Default::default(); } + + /// Set the execution context block timestamp + pub fn set_block_timestamp(&mut self, block_timestamp: BlockTimestamp) { + self.block_timestamp = block_timestamp + } } #[cfg(test)] mod tests { - use super::{ - AccountId, - ExecContext, - }; + use super::{AccountId, ExecContext}; #[test] fn basic_operations() { diff --git a/crates/engine/src/ext.rs b/crates/engine/src/ext.rs index f6d302407c0..0475522b242 100644 --- a/crates/engine/src/ext.rs +++ b/crates/engine/src/ext.rs @@ -394,6 +394,12 @@ impl Engine { set_output(output, &block_number[..]) } + /// Set the execution context block timestamp (expressed in ms). + pub fn set_block_timestamp(&mut self, block_timestamp : BlockTimestamp) -> Result { + self.exec_context.set_block_timestamp(block_timestamp); + Ok(()) + } + /// Returns the timestamp of the current block. pub fn block_timestamp(&self, output: &mut &mut [u8]) { let block_timestamp: Vec = diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 80d0f1c8ef2..ae861b96090 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -272,3 +272,20 @@ 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.set_block_timestamp(new_block_timestamp).unwrap(); + engine.block_timestamp(output); + + // then + let output = ::decode(&mut &output[..16]) + .expect("decoding value transferred failed"); + assert_eq!(output, new_block_timestamp); +} \ No newline at end of file From f294037777a50bcc59afecafb49d1531df5437aa Mon Sep 17 00:00:00 2001 From: Cyril Carlier Date: Sat, 18 Mar 2023 09:19:23 +0100 Subject: [PATCH 02/12] Implement set_block_timestamp for offchain test api env. --- crates/engine/src/ext.rs | 6 ------ crates/engine/src/test_api.rs | 7 ++++++- crates/engine/src/tests.rs | 2 +- crates/env/src/engine/off_chain/test_api.rs | 11 +++++++++++ 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/crates/engine/src/ext.rs b/crates/engine/src/ext.rs index 0475522b242..f6d302407c0 100644 --- a/crates/engine/src/ext.rs +++ b/crates/engine/src/ext.rs @@ -394,12 +394,6 @@ impl Engine { set_output(output, &block_number[..]) } - /// Set the execution context block timestamp (expressed in ms). - pub fn set_block_timestamp(&mut self, block_timestamp : BlockTimestamp) -> Result { - self.exec_context.set_block_timestamp(block_timestamp); - Ok(()) - } - /// Returns the timestamp of the current block. pub fn block_timestamp(&self, output: &mut &mut [u8]) { let block_timestamp: Vec = diff --git a/crates/engine/src/test_api.rs b/crates/engine/src/test_api.rs index 4ee8d67745d..5bbd0dd8b2c 100644 --- a/crates/engine/src/test_api.rs +++ b/crates/engine/src/test_api.rs @@ -16,7 +16,7 @@ use crate::{ ext::Engine, types::{ AccountId, - Balance, + Balance, BlockTimestamp, }, AccountError, Error, @@ -257,6 +257,11 @@ impl Engine { pub fn set_value_transferred(&mut self, value: Balance) { self.exec_context.value_transferred = value; } + + pub fn set_block_timestamp(&mut self, new_block_timestamp: BlockTimestamp) + { + self.exec_context.block_timestamp = new_block_timestamp; + } } #[cfg(test)] diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index ae861b96090..ee35ebc006a 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -281,7 +281,7 @@ fn setting_getting_block_timestamp() { let output = &mut &mut get_buffer()[..]; // when - engine.set_block_timestamp(new_block_timestamp).unwrap(); + engine.set_block_timestamp(new_block_timestamp); engine.block_timestamp(output); // then diff --git a/crates/env/src/engine/off_chain/test_api.rs b/crates/env/src/engine/off_chain/test_api.rs index 3e5137b9088..456bb69759e 100644 --- a/crates/env/src/engine/off_chain/test_api.rs +++ b/crates/env/src/engine/off_chain/test_api.rs @@ -240,6 +240,17 @@ where }) } +/// Sets block timestamp for the next advance block. +pub fn set_block_timestamp(value: T::Timestamp) +where + T: Environment, +{ + ::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(f: F) -> Result<()> From 24c9d86eff496564fcb43567a4312943f5bd1803 Mon Sep 17 00:00:00 2001 From: Cyril Carlier Date: Sat, 18 Mar 2023 09:58:08 +0100 Subject: [PATCH 03/12] Fix formatting --- crates/engine/src/exec_context.rs | 7 ++++++- crates/engine/src/test_api.rs | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/exec_context.rs b/crates/engine/src/exec_context.rs index c7aa839bfbb..497c61bc255 100644 --- a/crates/engine/src/exec_context.rs +++ b/crates/engine/src/exec_context.rs @@ -12,7 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -use super::types::{AccountId, Balance, BlockNumber, BlockTimestamp}; +use super::types::{ + AccountId, + Balance, + BlockNumber, + BlockTimestamp +}; /// The context of a contract execution. #[cfg_attr(test, derive(Debug, PartialEq, Eq))] diff --git a/crates/engine/src/test_api.rs b/crates/engine/src/test_api.rs index 5bbd0dd8b2c..bb09b696889 100644 --- a/crates/engine/src/test_api.rs +++ b/crates/engine/src/test_api.rs @@ -16,7 +16,8 @@ use crate::{ ext::Engine, types::{ AccountId, - Balance, BlockTimestamp, + Balance, + BlockTimestamp, }, AccountError, Error, From e0a10a35f751bc0c0b637a8bc54a9119f16032c7 Mon Sep 17 00:00:00 2001 From: Cyril Carlier Date: Sat, 18 Mar 2023 09:59:33 +0100 Subject: [PATCH 04/12] Fix formatting --- crates/engine/src/exec_context.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/exec_context.rs b/crates/engine/src/exec_context.rs index 497c61bc255..38631b026de 100644 --- a/crates/engine/src/exec_context.rs +++ b/crates/engine/src/exec_context.rs @@ -16,7 +16,7 @@ use super::types::{ AccountId, Balance, BlockNumber, - BlockTimestamp + BlockTimestamp, }; /// The context of a contract execution. @@ -73,7 +73,10 @@ impl ExecContext { #[cfg(test)] mod tests { - use super::{AccountId, ExecContext}; + use super::{ + AccountId, + ExecContext, + }; #[test] fn basic_operations() { From 247049891e6f22410ec68d89c2610173e6d522d3 Mon Sep 17 00:00:00 2001 From: Cyril Carlier Date: Sat, 18 Mar 2023 09:59:54 +0100 Subject: [PATCH 05/12] Fix formatting --- crates/engine/src/exec_context.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/exec_context.rs b/crates/engine/src/exec_context.rs index 38631b026de..80605ad14e6 100644 --- a/crates/engine/src/exec_context.rs +++ b/crates/engine/src/exec_context.rs @@ -13,9 +13,9 @@ // limitations under the License. use super::types::{ - AccountId, - Balance, - BlockNumber, + AccountId, + Balance, + BlockNumber, BlockTimestamp, }; @@ -74,7 +74,7 @@ impl ExecContext { #[cfg(test)] mod tests { use super::{ - AccountId, + AccountId, ExecContext, }; From 8238e01fd093b0f040fa0abc84e6e32bb4a2aa28 Mon Sep 17 00:00:00 2001 From: Cyril Carlier Date: Sat, 18 Mar 2023 10:34:50 +0100 Subject: [PATCH 06/12] Fix formatting --- crates/engine/src/test_api.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/engine/src/test_api.rs b/crates/engine/src/test_api.rs index bb09b696889..37a2cb1396a 100644 --- a/crates/engine/src/test_api.rs +++ b/crates/engine/src/test_api.rs @@ -259,8 +259,7 @@ impl Engine { self.exec_context.value_transferred = value; } - pub fn set_block_timestamp(&mut self, new_block_timestamp: BlockTimestamp) - { + pub fn set_block_timestamp(&mut self, new_block_timestamp: BlockTimestamp) { self.exec_context.block_timestamp = new_block_timestamp; } } From e99d177e01f3592c3176e11cad85151241e1b71f Mon Sep 17 00:00:00 2001 From: Cyril Carlier Date: Sat, 18 Mar 2023 12:29:53 +0100 Subject: [PATCH 07/12] Fix formatting --- crates/engine/src/exec_context.rs | 2 +- crates/engine/src/test_api.rs | 2 +- crates/engine/src/tests.rs | 6 +++--- crates/env/src/engine/off_chain/test_api.rs | 1 - 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/crates/engine/src/exec_context.rs b/crates/engine/src/exec_context.rs index 80605ad14e6..ab0c6ec3ab1 100644 --- a/crates/engine/src/exec_context.rs +++ b/crates/engine/src/exec_context.rs @@ -65,7 +65,7 @@ impl ExecContext { *self = Default::default(); } - /// Set the execution context block timestamp + /// Set the execution context block timestamp pub fn set_block_timestamp(&mut self, block_timestamp: BlockTimestamp) { self.block_timestamp = block_timestamp } diff --git a/crates/engine/src/test_api.rs b/crates/engine/src/test_api.rs index 37a2cb1396a..db0203cd5cf 100644 --- a/crates/engine/src/test_api.rs +++ b/crates/engine/src/test_api.rs @@ -16,7 +16,7 @@ use crate::{ ext::Engine, types::{ AccountId, - Balance, + Balance, BlockTimestamp, }, AccountError, diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index ee35ebc006a..f3255ba2a41 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -275,12 +275,12 @@ fn ecdsa_recovery_with_secp256k1_crate() { #[test] fn setting_getting_block_timestamp() { - // given + // given let mut engine = Engine::new(); let new_block_timestamp: u64 = 1000; let output = &mut &mut get_buffer()[..]; - // when + // when engine.set_block_timestamp(new_block_timestamp); engine.block_timestamp(output); @@ -288,4 +288,4 @@ fn setting_getting_block_timestamp() { let output = ::decode(&mut &output[..16]) .expect("decoding value transferred failed"); assert_eq!(output, new_block_timestamp); -} \ No newline at end of file +} diff --git a/crates/env/src/engine/off_chain/test_api.rs b/crates/env/src/engine/off_chain/test_api.rs index 456bb69759e..4359c0e2e82 100644 --- a/crates/env/src/engine/off_chain/test_api.rs +++ b/crates/env/src/engine/off_chain/test_api.rs @@ -250,7 +250,6 @@ where }) } - /// Runs the given closure test function with the default configuration /// for the off-chain environment. pub fn run_test(f: F) -> Result<()> From 8428abc1801a9e2c3a05bc53f0428e87e3419cbe Mon Sep 17 00:00:00 2001 From: Cyril Carlier Date: Tue, 21 Mar 2023 20:41:46 +0100 Subject: [PATCH 08/12] Description set_set_block_timestamp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Michael Müller --- crates/env/src/engine/off_chain/test_api.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/env/src/engine/off_chain/test_api.rs b/crates/env/src/engine/off_chain/test_api.rs index 4359c0e2e82..14b6c65fa7f 100644 --- a/crates/env/src/engine/off_chain/test_api.rs +++ b/crates/env/src/engine/off_chain/test_api.rs @@ -240,7 +240,7 @@ where }) } -/// Sets block timestamp for the next advance block. +/// Sets the block timestamp for the next [`advance_block`] invocation. pub fn set_block_timestamp(value: T::Timestamp) where T: Environment, From 420b767eda59d588ea2ac46ace17394422249940 Mon Sep 17 00:00:00 2001 From: Cyril Carlier Date: Tue, 21 Mar 2023 20:42:51 +0100 Subject: [PATCH 09/12] Add description for test_api's set_block_timestamp method. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Michael Müller --- crates/engine/src/test_api.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/engine/src/test_api.rs b/crates/engine/src/test_api.rs index db0203cd5cf..f99175ca935 100644 --- a/crates/engine/src/test_api.rs +++ b/crates/engine/src/test_api.rs @@ -259,6 +259,7 @@ impl Engine { 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) { self.exec_context.block_timestamp = new_block_timestamp; } From 6ee6bebe8d2d3b0044b3ee2700c275fc10103d4d Mon Sep 17 00:00:00 2001 From: Cyril Carlier Date: Tue, 21 Mar 2023 20:59:40 +0100 Subject: [PATCH 10/12] Adding an advance_block() call in setting_getting_block_timestamp test --- crates/engine/src/tests.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index f3255ba2a41..b6bc5f6601a 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -281,6 +281,7 @@ fn setting_getting_block_timestamp() { let output = &mut &mut get_buffer()[..]; // when + engine.advance_block(); engine.set_block_timestamp(new_block_timestamp); engine.block_timestamp(output); From a7304c12ed82462c8cadd45bdedee944260dd571 Mon Sep 17 00:00:00 2001 From: Cyril Carlier Date: Wed, 22 Mar 2023 08:49:53 +0100 Subject: [PATCH 11/12] Add Set_block_timestamp unreleased feature --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f48ecdf137e..c85d2b0f154 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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)]`. \ No newline at end of file +access to `emit_event` without `#[ink(impl)]`. From 9bf16e0e120661dfae2fe076a7a06a597a56cfe2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20M=C3=BCller?= Date: Thu, 23 Mar 2023 13:46:19 +0100 Subject: [PATCH 12/12] Update crates/engine/src/exec_context.rs --- crates/engine/src/exec_context.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/engine/src/exec_context.rs b/crates/engine/src/exec_context.rs index ab0c6ec3ab1..57cc985dbd4 100644 --- a/crates/engine/src/exec_context.rs +++ b/crates/engine/src/exec_context.rs @@ -65,7 +65,7 @@ impl ExecContext { *self = Default::default(); } - /// Set the execution context block timestamp + /// Set the block timestamp for the execution context. pub fn set_block_timestamp(&mut self, block_timestamp: BlockTimestamp) { self.block_timestamp = block_timestamp }