From 308005df39ee0283dee7ed36f24c51a77c562daa Mon Sep 17 00:00:00 2001 From: Alberto Date: Mon, 13 Jan 2025 15:18:06 +0100 Subject: [PATCH 1/6] feat: add timestamp shortcut --- boa/environment.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/boa/environment.py b/boa/environment.py index 7d849e55..163cc218 100644 --- a/boa/environment.py +++ b/boa/environment.py @@ -371,3 +371,7 @@ def time_travel( self.evm.patch.timestamp += seconds self.evm.patch.block_number += blocks + + def timestamp(self) -> int: + # shortcut for devex + return self.evm.patch.timestamp From 08512e1549dae6ecf7edcb528cf7f57d953fbb64 Mon Sep 17 00:00:00 2001 From: Alberto Date: Mon, 13 Jan 2025 15:18:28 +0100 Subject: [PATCH 2/6] docs: document `env.timestamp()` --- docs/api/env/env.md | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/docs/api/env/env.md b/docs/api/env/env.md index 2f9393c4..cde9a8c8 100644 --- a/docs/api/env/env.md +++ b/docs/api/env/env.md @@ -2,15 +2,17 @@ ### Description -TODO - +A wrapper class around py-evm which provides a "contract-centric" API. More details on the environment architecture in [boa-singleton-env](../../explain/singleton_env.md). -### Attributes +--- -- `eoa`: The account to use as `msg.sender` for top-level calls and `tx.origin` in the context of state mutating function calls. -- `chain`: The global py-evm chain instance. +## `timestamp` ---- +!!! function "`boa.env.timestamp()`" + + **Description** + + Returns the internal pyevm timestamp. A devex shortcut to `boa.env.evm.patch.timestamp`. ## `alias` From 69b42f6b4acb294e93caff54ba027ba680db6505 Mon Sep 17 00:00:00 2001 From: Alberto Date: Mon, 13 Jan 2025 15:18:36 +0100 Subject: [PATCH 3/6] Create test_env_timestamp.py --- tests/unitary/test_env_timestamp.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 tests/unitary/test_env_timestamp.py diff --git a/tests/unitary/test_env_timestamp.py b/tests/unitary/test_env_timestamp.py new file mode 100644 index 00000000..7098a82e --- /dev/null +++ b/tests/unitary/test_env_timestamp.py @@ -0,0 +1,13 @@ +import time + +import boa + + +def test_timestamp_shortcut(): + assert ( + boa.env.timestamp() == boa.env.evm.patch.timestamp + ), "result should equal its shortcut" + + +def test_timestamp_correctness(): + assert abs(boa.env.timestamp() - time.time()) < 1, "time should be present time" From 0b9d41bedf8b2a634340cee984cf0a99ae613f38 Mon Sep 17 00:00:00 2001 From: Charles Cooper Date: Tue, 14 Jan 2025 19:15:47 -0500 Subject: [PATCH 4/6] change to property + setter --- boa/environment.py | 7 ++++++- tests/unitary/test_env_timestamp.py | 20 +++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/boa/environment.py b/boa/environment.py index 163cc218..399796ff 100644 --- a/boa/environment.py +++ b/boa/environment.py @@ -372,6 +372,11 @@ def time_travel( self.evm.patch.timestamp += seconds self.evm.patch.block_number += blocks + # EVM API - access to evm.patch attributes + @property def timestamp(self) -> int: - # shortcut for devex return self.evm.patch.timestamp + + @timestamp.setter + def timestamp(self, val: int) -> None: + self.evm.patch.timestamp = val diff --git a/tests/unitary/test_env_timestamp.py b/tests/unitary/test_env_timestamp.py index 7098a82e..77874322 100644 --- a/tests/unitary/test_env_timestamp.py +++ b/tests/unitary/test_env_timestamp.py @@ -3,11 +3,21 @@ import boa -def test_timestamp_shortcut(): - assert ( - boa.env.timestamp() == boa.env.evm.patch.timestamp - ), "result should equal its shortcut" +def test_env_timestamp(): + assert boa.env.timestamp == boa.env.evm.patch.timestamp + + tmp = boa.env.timestamp + with boa.env.anchor(): + boa.env.timestamp += 1 + # check patch is updated + assert boa.env.timestamp == boa.env.evm.patch.timestamp + assert tmp + 1 == boa.env.timestamp + + # check reset to prior value after anchor + assert tmp == boa.env.timestamp + # sanity check + assert boa.env.timestamp == boa.env.evm.patch.timestamp def test_timestamp_correctness(): - assert abs(boa.env.timestamp() - time.time()) < 1, "time should be present time" + assert abs(boa.env.timestamp - time.time()) < 1, "time should be present time" From 8f418b1a8ed3246ad334317eacc68ef87fa25b66 Mon Sep 17 00:00:00 2001 From: Charles Cooper Date: Tue, 14 Jan 2025 19:19:02 -0500 Subject: [PATCH 5/6] update docs --- docs/api/env/env.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/api/env/env.md b/docs/api/env/env.md index cde9a8c8..d371842b 100644 --- a/docs/api/env/env.md +++ b/docs/api/env/env.md @@ -8,11 +8,13 @@ A wrapper class around py-evm which provides a "contract-centric" API. More deta ## `timestamp` -!!! function "`boa.env.timestamp()`" +!!! property "`boa.env.timestamp`" **Description** - Returns the internal pyevm timestamp. A devex shortcut to `boa.env.evm.patch.timestamp`. + Returns the internal pyevm timestamp. Should be equal to evaluating `block.timestamp`. + + Uses the low level `boa.env.patch` object to ensure that changes to the are rolled back after exiting `boa.env.anchor()` blocks. ## `alias` @@ -37,7 +39,7 @@ A wrapper class around py-evm which provides a "contract-centric" API. More deta **Description** - A context manager which snapshots the state and the vm, and reverts to the snapshot on exit. + A context manager which snapshots the state and the vm, and reverts to the snapshot on exit. Properties in the low-level `boa.env.patch` object are also rolled back after exiting the context manager. --- From 7b6d956af62aaedc366d96a5fd1f6640d817f031 Mon Sep 17 00:00:00 2001 From: Charles Cooper Date: Tue, 14 Jan 2025 19:23:57 -0500 Subject: [PATCH 6/6] increase slack --- tests/unitary/test_env_timestamp.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/unitary/test_env_timestamp.py b/tests/unitary/test_env_timestamp.py index 77874322..935d0aef 100644 --- a/tests/unitary/test_env_timestamp.py +++ b/tests/unitary/test_env_timestamp.py @@ -20,4 +20,7 @@ def test_env_timestamp(): def test_timestamp_correctness(): - assert abs(boa.env.timestamp - time.time()) < 1, "time should be present time" + # amount of "timer slack" to allow in the CI since it may take some + # time between when boa.env is initialized and when this test is run. + timer_slack = 60 + assert abs(boa.env.timestamp - time.time()) < timer_slack, "bad time"