Completed upgradeability test

pull/1470/head
vzotova 2019-12-02 16:45:46 +03:00
parent adb11d6785
commit 52bba7884c
1 changed files with 86 additions and 10 deletions

View File

@ -15,14 +15,20 @@ You should have received a copy of the GNU Affero General Public License
along with nucypher. If not, see <https://www.gnu.org/licenses/>. along with nucypher. If not, see <https://www.gnu.org/licenses/>.
""" """
import os import os
import tempfile
import pytest import pytest
import requests import requests
from eth_utils import keccak
from nucypher.blockchain.eth.deployers import NucypherTokenDeployer, StakingEscrowDeployer, PolicyManagerDeployer, \
AdjudicatorDeployer, BaseContractDeployer, UpgradeableContractMixin, DispatcherDeployer
from nucypher.blockchain.eth.interfaces import BlockchainInterfaceFactory, BlockchainDeployerInterface
from nucypher.blockchain.eth.registry import InMemoryContractRegistry, BaseContractRegistry
from nucypher.blockchain.eth.sol.compile import SolidityCompiler, SourceDirs from nucypher.blockchain.eth.sol.compile import SolidityCompiler, SourceDirs
from nucypher.crypto.powers import TransactingPower from nucypher.crypto.powers import TransactingPower
from nucypher.utilities.sandbox.constants import INSECURE_DEVELOPMENT_PASSWORD from nucypher.utilities.sandbox.blockchain import TesterBlockchain
from nucypher.utilities.sandbox.constants import INSECURE_DEVELOPMENT_PASSWORD, STAKING_ESCROW_DEPLOYMENT_SECRET, \
POLICY_MANAGER_DEPLOYMENT_SECRET, ADJUDICATOR_DEPLOYMENT_SECRET
USER = "nucypher" USER = "nucypher"
REPO = "nucypher" REPO = "nucypher"
@ -58,12 +64,82 @@ def download_github_file(source_link: str, target_folder: str):
registry_file.truncate() registry_file.truncate()
# Constructor parameters overrides for previous versions if needed
# 'None' value removes arg from list of constructor parameters
CONSTRUCTOR_OVERRIDES = {
# TODO Test example, update after real usage
StakingEscrowDeployer.contract_name: {"v0.0.0": {"_hoursPerPeriod": 1}}
}
def deploy_earliest_contract(blockchain_interface: BlockchainDeployerInterface,
deployer: BaseContractDeployer,
secret: str):
contract_name = deployer.contract_name
earliest_version, _data = blockchain_interface.find_raw_contract_data(contract_name, "earliest")
try:
overrides = CONSTRUCTOR_OVERRIDES[contract_name][earliest_version]
except KeyError:
overrides = dict()
deployer.deploy(secret_hash=keccak(text=secret), contract_version=earliest_version, **overrides)
def upgrade_to_latest_contract(deployer, secret: str):
old_secret = bytes(secret, encoding='utf-8')
new_secret_hash = keccak(b'new' + old_secret)
deployer.upgrade(existing_secret_plaintext=old_secret,
new_secret_hash=new_secret_hash,
contract_version="latest")
@pytest.mark.slow @pytest.mark.slow
def test_upgradability(): def test_upgradability(temp_dir_path, token_economics):
with tempfile.TemporaryDirectory() as temp_dir: # Prepare remote source for compilation
download_github_dir(GITHUB_SOURCE_LINK, temp_dir) download_github_dir(GITHUB_SOURCE_LINK, temp_dir_path)
solidity_compiler = SolidityCompiler(source_dirs=[SourceDirs(SolidityCompiler.default_contract_dir()), solidity_compiler = SolidityCompiler(source_dirs=[SourceDirs(SolidityCompiler.default_contract_dir()),
SourceDirs(temp_dir)]) SourceDirs(temp_dir_path)])
interfaces = solidity_compiler.compile()
pass # Prepare the blockchain
pass blockchain_interface = BlockchainDeployerInterface(provider_uri='tester://pyevm/2', compiler=solidity_compiler)
blockchain_interface.connect()
origin = blockchain_interface.client.accounts[0]
BlockchainInterfaceFactory.register_interface(interface=blockchain_interface)
blockchain_interface.transacting_power = TransactingPower(password=INSECURE_DEVELOPMENT_PASSWORD, account=origin)
blockchain_interface.transacting_power.activate()
blockchain_interface.connect()
# Check contracts with multiple versions
raw_contracts = blockchain_interface._raw_contract_cache
contract_name = AdjudicatorDeployer.contract_name
test_adjudicator = len(raw_contracts[contract_name]) > 1
contract_name = StakingEscrowDeployer.contract_name
test_staking_escrow = len(raw_contracts[contract_name]) > 1
contract_name = PolicyManagerDeployer.contract_name
test_policy_manager = len(raw_contracts[contract_name]) > 1
if not test_adjudicator and not test_staking_escrow and not test_policy_manager:
return
# Prepare master version of contracts and upgrade to the latest
registry = InMemoryContractRegistry()
token_deployer = NucypherTokenDeployer(registry=registry, deployer_address=origin)
token_deployer.deploy()
staking_escrow_deployer = StakingEscrowDeployer(registry=registry, deployer_address=origin)
deploy_earliest_contract(blockchain_interface, staking_escrow_deployer, secret=STAKING_ESCROW_DEPLOYMENT_SECRET)
assert staking_escrow_deployer.contract.functions.secondsPerPeriod().call() == 3600
if test_staking_escrow:
upgrade_to_latest_contract(staking_escrow_deployer, secret=STAKING_ESCROW_DEPLOYMENT_SECRET)
assert staking_escrow_deployer.contract.functions.secondsPerPeriod().call() == token_economics.seconds_per_period
if test_policy_manager:
policy_manager_deployer = PolicyManagerDeployer(registry=registry, deployer_address=origin)
deploy_earliest_contract(blockchain_interface, policy_manager_deployer, secret=POLICY_MANAGER_DEPLOYMENT_SECRET)
upgrade_to_latest_contract(policy_manager_deployer, secret=POLICY_MANAGER_DEPLOYMENT_SECRET)
if test_adjudicator:
adjudicator_deployer = AdjudicatorDeployer(registry=registry, deployer_address=origin)
deploy_earliest_contract(blockchain_interface, adjudicator_deployer, secret=ADJUDICATOR_DEPLOYMENT_SECRET)
upgrade_to_latest_contract(adjudicator_deployer, secret=ADJUDICATOR_DEPLOYMENT_SECRET)