-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #139 from pipermerriam/piper/add-tester-module
add testing module
- Loading branch information
Showing
9 changed files
with
168 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
Testing API | ||
=========== | ||
|
||
.. py:module:: web3.testing | ||
.. py:currentmodule:: web3.testing | ||
.. py:class:: Testing | ||
The ``web3.testing`` object exposes methods to interact with non-standard RPC | ||
APIs that are only available under the python ``eth-testrpc`` package. | ||
|
||
|
||
Methods | ||
------- | ||
|
||
The following methods are available on the ``web3.testing`` namespace. | ||
|
||
|
||
.. py:method:: Testing.timeTravel(timestamp) | ||
* Delegates to ``testing_timeTravel`` RPC Method | ||
|
||
Advances the test blockchain one block setting the new block's timestamp to | ||
the provided integer timestamp. | ||
|
||
|
||
.. py:method:: Testing.mine(num_blocks=1) | ||
* Delegates to ``evm_mine`` RPC Method | ||
|
||
Mines ``num_blocks`` new blocks. | ||
|
||
|
||
.. py:method:: Testing.snapshot() | ||
* Delegates to ``evm_snapshot`` RPC Method | ||
|
||
Takes a snapshot of the current EVM state and returns an integer that can | ||
be used with the ``revert`` method to restore the EVM to this state. | ||
|
||
|
||
.. py:method:: Testing.revert(snapshot_idx=None) | ||
* Delegates to ``evm_revert`` RPC Method | ||
|
||
If no ``snapshot_idx`` is provided this will revert the EVM to the most | ||
recent snapshot. Otherwise reverts the EVM to the snapshot indicated by | ||
the provided ``snapshot_idx`` | ||
|
||
|
||
.. py:method:: Testing.reset() | ||
* Delegates to ``evm_reset`` RPC Method | ||
|
||
Reset the EVM back to the genesis state. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
def test_testing_mine_single_block(web3): | ||
web3.testing.mine() | ||
|
||
before_mining_block = web3.eth.getBlock("latest") | ||
|
||
web3.testing.mine() | ||
|
||
after_mining_block = web3.eth.getBlock("latest") | ||
|
||
assert after_mining_block['number'] - before_mining_block['number'] == 1 | ||
|
||
|
||
def test_testing_mine_multiple_blocks(web3): | ||
web3.testing.mine() | ||
|
||
before_mining_block = web3.eth.getBlock("latest") | ||
|
||
web3.testing.mine(5) | ||
|
||
after_mining_block = web3.eth.getBlock("latest") | ||
|
||
assert after_mining_block['number'] - before_mining_block['number'] == 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
def test_snapshot_revert_to_latest_snapshot(web3): | ||
web3.testing.mine(5) | ||
|
||
block_before_snapshot = web3.eth.getBlock("latest") | ||
|
||
snapshot_idx = web3.testing.snapshot() | ||
|
||
block_after_snapshot = web3.eth.getBlock("latest") | ||
|
||
web3.testing.mine(3) | ||
|
||
block_after_mining = web3.eth.getBlock("latest") | ||
|
||
web3.testing.revert() | ||
|
||
block_after_revert = web3.eth.getBlock("latest") | ||
|
||
assert block_after_mining['number'] > block_before_snapshot['number'] | ||
assert block_before_snapshot['hash'] == block_after_snapshot['hash'] | ||
assert block_after_snapshot['hash'] == block_after_revert['hash'] | ||
|
||
|
||
def test_snapshot_revert_to_specific(web3): | ||
web3.testing.mine(5) | ||
|
||
block_before_snapshot = web3.eth.getBlock("latest") | ||
|
||
snapshot_idx = web3.testing.snapshot() | ||
|
||
block_after_snapshot = web3.eth.getBlock("latest") | ||
|
||
web3.testing.mine() | ||
web3.testing.snapshot() | ||
web3.testing.mine() | ||
web3.testing.snapshot() | ||
web3.testing.mine() | ||
web3.testing.snapshot() | ||
|
||
block_after_mining = web3.eth.getBlock("latest") | ||
|
||
web3.testing.revert(snapshot_idx) | ||
|
||
block_after_revert = web3.eth.getBlock("latest") | ||
|
||
assert block_after_mining['number'] > block_before_snapshot['number'] | ||
assert block_before_snapshot['hash'] == block_after_snapshot['hash'] | ||
assert block_after_snapshot['hash'] == block_after_revert['hash'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
def test_time_traveling(web3): | ||
current_block_time = web3.eth.getBlock("pending")['timestamp'] | ||
|
||
time_travel_to = current_block_time + 12345 | ||
|
||
web3.testing.timeTravel(time_travel_to) | ||
|
||
latest_block_time = web3.eth.getBlock("pending")['timestamp'] | ||
assert latest_block_time >= time_travel_to |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from web3.utils.encoding import ( | ||
to_decimal, | ||
) | ||
from web3.utils.functional import ( | ||
apply_formatters_to_return, | ||
) | ||
|
||
|
||
class Testing(object): | ||
def __init__(self, web3): | ||
self.web3 = web3 | ||
|
||
def timeTravel(self, timestamp): | ||
return self.web3._requestManager.request_blocking("testing_timeTravel", [timestamp]) | ||
|
||
def mine(self, num_blocks=1): | ||
return self.web3._requestManager.request_blocking("evm_mine", [num_blocks]) | ||
|
||
@apply_formatters_to_return(to_decimal) | ||
def snapshot(self): | ||
return self.web3._requestManager.request_blocking("evm_snapshot", []) | ||
|
||
def reset(self): | ||
return self.web3._requestManager.request_blocking("evm_reset", []) | ||
|
||
def revert(self, snapshot_idx=None): | ||
if snapshot_idx is None: | ||
return self.web3._requestManager.request_blocking("evm_revert", []) | ||
else: | ||
return self.web3._requestManager.request_blocking("evm_revert", [snapshot_idx]) |