From c0a4f976c61fe504c99427560c41136b5432d461 Mon Sep 17 00:00:00 2001 From: szotov Date: Mon, 9 Sep 2019 17:28:51 +0300 Subject: [PATCH 1/4] Reset state in testerchain --- tests/blockchain/eth/interfaces/test_chains.py | 1 + tests/fixtures.py | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/tests/blockchain/eth/interfaces/test_chains.py b/tests/blockchain/eth/interfaces/test_chains.py index b0efc2e85..1fdc33972 100644 --- a/tests/blockchain/eth/interfaces/test_chains.py +++ b/tests/blockchain/eth/interfaces/test_chains.py @@ -48,6 +48,7 @@ def test_testerchain_creation(testerchain, another_testerchain): assert 'tester' in chain.provider_uri # ... and that there are already some blocks mined + chain.w3.eth.web3.testing.mine(1) assert chain.w3.eth.blockNumber > 0 # Check that we have enough test accounts diff --git a/tests/fixtures.py b/tests/fixtures.py index 3ec358837..de8f84d73 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -23,6 +23,8 @@ import tempfile import maya import pytest +from eth_tester import PyEVMBackend + from constant_sorrow.constants import NON_PAYMENT from sqlalchemy.engine import create_engine from twisted.logger import Logger @@ -72,6 +74,7 @@ from nucypher.utilities.sandbox.constants import ( MOCK_REGISTRY_FILEPATH, TEST_GAS_LIMIT, INSECURE_DEPLOYMENT_SECRET_HASH, + NUMBER_OF_ETH_TEST_ACCOUNTS, ) from nucypher.utilities.sandbox.middleware import MockRestMiddleware from nucypher.utilities.sandbox.policy import generate_random_label @@ -387,6 +390,11 @@ def _testerchain(): def testerchain(_testerchain): testerchain = _testerchain + # Reset chain state + pyevm_backend = testerchain.provider.ethereum_tester.backend + genesis_params = PyEVMBackend._generate_genesis_params(overrides={'gas_limit': TEST_GAS_LIMIT}) + pyevm_backend.reset_to_genesis(genesis_params=genesis_params, num_accounts=NUMBER_OF_ETH_TEST_ACCOUNTS) + coinbase, *addresses = testerchain.client.accounts for address in addresses: From f58eb7a090fea4c5ef4a54c8a726b09f652d62f8 Mon Sep 17 00:00:00 2001 From: szotov Date: Tue, 10 Sep 2019 18:34:20 +0300 Subject: [PATCH 2/4] Monkey patch to prevent gas estimates --- nucypher/utilities/sandbox/blockchain.py | 3 +++ tests/fixtures.py | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/nucypher/utilities/sandbox/blockchain.py b/nucypher/utilities/sandbox/blockchain.py index 4029e6d01..ff25559e6 100644 --- a/nucypher/utilities/sandbox/blockchain.py +++ b/nucypher/utilities/sandbox/blockchain.py @@ -20,6 +20,7 @@ import os from typing import List, Tuple import maya +from eth_tester.exceptions import TransactionFailed from twisted.logger import Logger from web3 import Web3 @@ -265,5 +266,7 @@ class TesterBlockchain(BlockchainDeployerInterface): """Wait for a transaction receipt and return it""" timeout = timeout or self.TIMEOUT result = self.w3.eth.waitForTransactionReceipt(txhash, timeout=timeout) + if result.status == 0: + raise TransactionFailed() return result diff --git a/tests/fixtures.py b/tests/fixtures.py index de8f84d73..bf4c8f0e0 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -369,6 +369,14 @@ def _make_testerchain(): eth._utils.headers.GAS_LIMIT_MINIMUM = TEST_GAS_LIMIT eth._utils.headers.GENESIS_GAS_LIMIT = TEST_GAS_LIMIT eth.vm.forks.frontier.headers.GENESIS_GAS_LIMIT = TEST_GAS_LIMIT + + # Monkey patch to prevent gas estimates + def _get_buffered_gas_estimate(web3, transaction, gas_buffer=100000): + return TEST_GAS_LIMIT + + import web3 + web3.eth.get_buffered_gas_estimate = _get_buffered_gas_estimate + # Create the blockchain testerchain = TesterBlockchain(eth_airdrop=True, free_transactions=True) BlockchainInterfaceFactory.register_interface(interface=testerchain) From 800876638ffc0a611f9d19f4134f6297d782ad72 Mon Sep 17 00:00:00 2001 From: szotov Date: Sun, 15 Sep 2019 18:22:27 +0300 Subject: [PATCH 3/4] Reset chain using block hash --- tests/fixtures.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/fixtures.py b/tests/fixtures.py index bf4c8f0e0..6cbc879c6 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -400,8 +400,8 @@ def testerchain(_testerchain): # Reset chain state pyevm_backend = testerchain.provider.ethereum_tester.backend - genesis_params = PyEVMBackend._generate_genesis_params(overrides={'gas_limit': TEST_GAS_LIMIT}) - pyevm_backend.reset_to_genesis(genesis_params=genesis_params, num_accounts=NUMBER_OF_ETH_TEST_ACCOUNTS) + snapshot = pyevm_backend.chain.get_canonical_block_by_number(0).hash + pyevm_backend.revert_to_snapshot(snapshot) coinbase, *addresses = testerchain.client.accounts From 390bf0ba46bedddd73622e53f8b14819365da30a Mon Sep 17 00:00:00 2001 From: szotov Date: Wed, 18 Sep 2019 18:23:06 +0300 Subject: [PATCH 4/4] Remove unused import --- tests/fixtures.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/fixtures.py b/tests/fixtures.py index 6cbc879c6..7601ae85c 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -74,7 +74,6 @@ from nucypher.utilities.sandbox.constants import ( MOCK_REGISTRY_FILEPATH, TEST_GAS_LIMIT, INSECURE_DEPLOYMENT_SECRET_HASH, - NUMBER_OF_ETH_TEST_ACCOUNTS, ) from nucypher.utilities.sandbox.middleware import MockRestMiddleware from nucypher.utilities.sandbox.policy import generate_random_label