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

Ensure chain time is correct when using chain.mine with ganache 7 #1438

Merged
merged 2 commits into from
Feb 14, 2022
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
4 changes: 4 additions & 0 deletions brownie/network/rpc/ganache.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ def sleep(seconds: int) -> int:
def mine(timestamp: Optional[int] = None) -> None:
params = [timestamp] if timestamp else []
_request("evm_mine", params)
if timestamp and web3.clientVersion.lower().startswith("ganache/v7"):
# ganache v7 does not modify the internal time when mining new blocks
# so we also set the time to maintain consistency with v6 behavior
_request("evm_setTime", [(timestamp + 1) * 1000])


def snapshot() -> int:
Expand Down
17 changes: 17 additions & 0 deletions tests/network/state/test_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,30 @@ def test_mine_timestamp(devnetwork, chain):
assert chain.time() - 999999999999 < 3


def test_mine_timestamp_next_block(devnetwork, chain):
chain.mine(timestamp=999999999999)
chain.mine()

assert chain[-1].timestamp >= 999999999999
assert chain.time() >= 999999999999


def test_mine_timedelta(devnetwork, chain):
now = chain.time()
chain.mine(timedelta=12345)

assert 0 <= chain[-1].timestamp - 12345 - now <= 1


def test_mine_timedelta_next_block(devnetwork, chain):
now = chain.time()
chain.mine(timedelta=12345)
chain.mine()

assert chain[-1].timestamp >= now + 12345
assert chain.time() >= now + 12345


def test_mine_multiple_timestamp(devnetwork, chain):
chain.mine(5, timestamp=chain.time() + 123)
timestamps = [i.timestamp for i in list(chain)[-5:]]
Expand Down