Fix upgradeability and tests

pull/2861/head
vzotova 2021-11-20 17:23:08 +03:00 committed by Kieran Prasch
parent 001913c380
commit ff2802c399
4 changed files with 89 additions and 69 deletions

View File

@ -570,9 +570,6 @@ class StakingEscrowDeployer(BaseContractDeployer, UpgradeableContractMixin, Owna
confirmations: int = 0, confirmations: int = 0,
**overrides): **overrides):
constructor_kwargs = { constructor_kwargs = {
"_genesisHoursPerPeriod": self.economics.genesis_hours_per_period,
"_hoursPerPeriod": self.economics.hours_per_period,
"_minLockedPeriods": self.economics.minimum_locked_periods,
"_minAllowableLockedTokens": self.economics.minimum_allowed_locked, "_minAllowableLockedTokens": self.economics.minimum_allowed_locked,
"_maxAllowableLockedTokens": self.economics.maximum_allowed_locked "_maxAllowableLockedTokens": self.economics.maximum_allowed_locked
} }
@ -597,27 +594,11 @@ class StakingEscrowDeployer(BaseContractDeployer, UpgradeableContractMixin, Owna
gas_limit: int = None, gas_limit: int = None,
confirmations: int = 0, confirmations: int = 0,
**overrides): **overrides):
args = self.economics.staking_deployment_parameters constructor_kwargs = {}
constructor_kwargs = {
"_genesisHoursPerPeriod": args[0],
"_hoursPerPeriod": args[1],
"_issuanceDecayCoefficient": args[2],
"_lockDurationCoefficient1": args[3],
"_lockDurationCoefficient2": args[4],
"_maximumRewardedPeriods": args[5],
"_firstPhaseTotalSupply": args[6],
"_firstPhaseMaxIssuance": args[7],
"_minLockedPeriods": args[8],
"_minAllowableLockedTokens": args[9],
"_maxAllowableLockedTokens": args[10],
"_minWorkerPeriods": args[11]
}
constructor_kwargs.update(overrides) constructor_kwargs.update(overrides)
constructor_kwargs = {k: v for k, v in constructor_kwargs.items() if v is not None} constructor_kwargs = {k: v for k, v in constructor_kwargs.items() if v is not None}
# Force use of the contract addresses from the registry # Force use of the contract addresses from the registry
constructor_kwargs.update({"_token": self.token_contract.address, constructor_kwargs.update({"_token": self.token_contract.address,
"_policyManager": self.policy_manager.address,
"_adjudicator": self.adjudicator.address,
"_workLock": self.worklock.address if self.worklock is not None else NULL_ADDRESS}) "_workLock": self.worklock.address if self.worklock is not None else NULL_ADDRESS})
the_escrow_contract, deploy_receipt = self.blockchain.deploy_contract( the_escrow_contract, deploy_receipt = self.blockchain.deploy_contract(
transacting_power, transacting_power,

View File

@ -149,13 +149,13 @@ contract StakingEscrow is Upgradeable, IERC900History {
NuCypherToken public immutable token; NuCypherToken public immutable token;
WorkLockInterface public immutable workLock; WorkLockInterface public immutable workLock;
uint128 previousPeriodSupply; // outdated uint128 public previousPeriodSupply; // outdated
uint128 currentPeriodSupply; // outdated uint128 public currentPeriodSupply; // outdated
uint16 currentMintingPeriod; // outdated uint16 public currentMintingPeriod; // outdated
mapping (address => StakerInfo) public stakerInfo; mapping (address => StakerInfo) public stakerInfo;
address[] public stakers; address[] public stakers;
mapping (address => address) stakerFromWorker; // outdated mapping (address => address) public stakerFromWorker; // outdated
mapping (uint16 => uint256) stub1; // former slot for lockedPerPeriod mapping (uint16 => uint256) stub1; // former slot for lockedPerPeriod
uint128[] public balanceHistory; uint128[] public balanceHistory;

View File

@ -0,0 +1,15 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity ^0.7.0;
contract OldPolicyManagerMock {
uint32 public immutable secondsPerPeriod = 1 hours;
function register(address _node, uint16 _period) external {}
}
contract OldAdjudicatorMock {
uint256 public immutable rewardCoefficient = 1;
}

View File

@ -19,9 +19,13 @@ import contextlib
from pathlib import Path from pathlib import Path
import requests import requests
from eth_utils import to_wei
from constant_sorrow import constants from constant_sorrow import constants
from web3.exceptions import ValidationError from web3.exceptions import ValidationError
from nucypher.blockchain.economics import BaseEconomics
from nucypher.blockchain.eth.agents import StakingEscrowAgent, WorkLockAgent
from nucypher.blockchain.eth.deployers import ( from nucypher.blockchain.eth.deployers import (
AdjudicatorDeployer, AdjudicatorDeployer,
BaseContractDeployer, BaseContractDeployer,
@ -31,14 +35,14 @@ from nucypher.blockchain.eth.deployers import (
WorklockDeployer WorklockDeployer
) )
from nucypher.blockchain.eth.interfaces import BlockchainDeployerInterface, BlockchainInterfaceFactory from nucypher.blockchain.eth.interfaces import BlockchainDeployerInterface, BlockchainInterfaceFactory
from nucypher.blockchain.eth.registry import InMemoryContractRegistry from nucypher.blockchain.eth.registry import InMemoryContractRegistry, BaseContractRegistry
from nucypher.blockchain.eth.signers.software import Web3Signer from nucypher.blockchain.eth.signers.software import Web3Signer
from nucypher.blockchain.eth.sol.compile.constants import SOLIDITY_SOURCE_ROOT from nucypher.blockchain.eth.sol.compile.constants import SOLIDITY_SOURCE_ROOT, TEST_SOLIDITY_SOURCE_ROOT
from nucypher.blockchain.eth.sol.compile.types import SourceBundle from nucypher.blockchain.eth.sol.compile.types import SourceBundle
from nucypher.crypto.powers import TransactingPower from nucypher.crypto.powers import TransactingPower
from tests.constants import INSECURE_DEVELOPMENT_PASSWORD from tests.constants import INSECURE_DEVELOPMENT_PASSWORD
from tests.fixtures import make_token_economics from tests.fixtures import make_token_economics
from tests.utils.blockchain import free_gas_price_strategy from tests.utils.blockchain import free_gas_price_strategy, TesterBlockchain
USER = "nucypher" USER = "nucypher"
REPO = "nucypher" REPO = "nucypher"
@ -78,20 +82,45 @@ def download_github_file(source_link: str, target_folder: Path):
registry_file.truncate() registry_file.truncate()
def parameters_v611(blockchain_interface: BlockchainDeployerInterface,
transacting_power: TransactingPower,
deployer: BaseContractDeployer):
policy_manager_mock, _ = blockchain_interface.deploy_contract(
transacting_power,
deployer.registry,
"OldPolicyManagerMock"
)
adjudicator_mock, _ = blockchain_interface.deploy_contract(
transacting_power,
deployer.registry,
"OldAdjudicatorMock"
)
parameters = {
"_genesisHoursPerPeriod": 1,
"_hoursPerPeriod": 1,
"_issuanceDecayCoefficient": 1,
"_lockDurationCoefficient1": 1,
"_lockDurationCoefficient2": 2,
"_maximumRewardedPeriods": 1,
"_firstPhaseTotalSupply": 1,
"_firstPhaseMaxIssuance": 1,
"_minLockedPeriods": 2,
"_minAllowableLockedTokens": 0,
"_maxAllowableLockedTokens": deployer.economics.maximum_allowed_locked,
"_minWorkerPeriods": 1,
"_policyManager": policy_manager_mock.address,
"_adjudicator": adjudicator_mock.address
}
return parameters
# Constructor parameters overrides for previous versions if needed # Constructor parameters overrides for previous versions if needed
# All versions below the specified version must use these overrides # All versions below the specified version must use these overrides
# 'None' value removes arg from list of constructor parameters # 'None' value removes arg from list of constructor parameters
CONSTRUCTOR_OVERRIDES = { CONSTRUCTOR_OVERRIDES = {
StakingEscrowDeployer.contract_name: {"v4.2.1": {"_issuanceDecayCoefficient": None, StakingEscrowDeployer.contract_name: {
"_lockDurationCoefficient1": None, "v5.7.1": lambda *args: {"_genesisHoursPerPeriod": None},
"_lockDurationCoefficient2": None, "v6.1.1": parameters_v611
"_maximumRewardedPeriods": None,
"_firstPhaseTotalSupply": None,
"_firstPhaseMaxIssuance": None,
"_miningCoefficient": 2,
"_lockedPeriodsCoefficient": 1,
"_rewardedPeriods": 1},
"v5.7.1": {"_genesisHoursPerPeriod": None}
} }
} }
@ -111,7 +140,10 @@ def deploy_base_contract(blockchain_interface: BlockchainDeployerInterface,
overrides = dict() overrides = dict()
if len(raw_contracts[contract_name]) != 1: if len(raw_contracts[contract_name]) != 1:
try: try:
overrides = CONSTRUCTOR_OVERRIDES[contract_name][latest_version] overrides_func = CONSTRUCTOR_OVERRIDES[contract_name][latest_version]
overrides = overrides_func(blockchain_interface,
transacting_power,
deployer)
except KeyError: except KeyError:
pass pass
@ -135,19 +167,32 @@ def skip_test(blockchain_interface: BlockchainDeployerInterface, contract_name:
return force_skip or len(raw_contracts[contract_name]) == 1 return force_skip or len(raw_contracts[contract_name]) == 1
def prepare_staker(blockchain_interface: TesterBlockchain,
deployer: StakingEscrowDeployer,
transacting_power: TransactingPower):
worklock_agent = WorkLockAgent(registry=deployer.registry)
value = worklock_agent.minimum_allowed_bid
worklock_agent.bid(value=value, transacting_power=transacting_power)
blockchain_interface.time_travel(hours=100)
worklock_agent.verify_bidding_correctness(transacting_power=transacting_power, gas_limit=1000000)
worklock_agent.claim(transacting_power=transacting_power)
def test_upgradeability(temp_dir_path): def test_upgradeability(temp_dir_path):
# Prepare remote source for compilation # Prepare remote source for compilation
download_github_dir(GITHUB_SOURCE_LINK, temp_dir_path) download_github_dir(GITHUB_SOURCE_LINK, temp_dir_path)
# Prepare the blockchain # Prepare the blockchain
BlockchainDeployerInterface.SOURCES = [ TesterBlockchain.SOURCES = [
SourceBundle(base_path=SOLIDITY_SOURCE_ROOT), SourceBundle(base_path=SOLIDITY_SOURCE_ROOT,
other_paths=(TEST_SOLIDITY_SOURCE_ROOT,)),
SourceBundle(base_path=Path(temp_dir_path)) SourceBundle(base_path=Path(temp_dir_path))
] ]
provider_uri = 'tester://pyevm/2' # TODO: Testerchain caching Issues provider_uri = 'tester://pyevm/2' # TODO: Testerchain caching Issues
try: try:
blockchain_interface = BlockchainDeployerInterface(provider_uri=provider_uri, gas_strategy='free') blockchain_interface = TesterBlockchain(gas_strategy='free')
blockchain_interface.provider_uri = provider_uri
blockchain_interface.connect() blockchain_interface.connect()
origin = blockchain_interface.client.accounts[0] origin = blockchain_interface.client.accounts[0]
BlockchainInterfaceFactory.register_interface(interface=blockchain_interface) BlockchainInterfaceFactory.register_interface(interface=blockchain_interface)
@ -158,14 +203,10 @@ def test_upgradeability(temp_dir_path):
economics = make_token_economics(blockchain_interface) economics = make_token_economics(blockchain_interface)
# Check contracts with multiple versions # Check contracts with multiple versions
contract_name = AdjudicatorDeployer.contract_name
skip_adjudicator_test = skip_test(blockchain_interface, contract_name)
contract_name = StakingEscrowDeployer.contract_name contract_name = StakingEscrowDeployer.contract_name
skip_staking_escrow_test = skip_test(blockchain_interface, contract_name) skip_staking_escrow_test = skip_test(blockchain_interface, contract_name)
contract_name = PolicyManagerDeployer.contract_name
skip_policy_manager_test = skip_test(blockchain_interface, contract_name)
if skip_adjudicator_test and skip_staking_escrow_test and skip_policy_manager_test: if skip_staking_escrow_test:
return return
# Prepare master version of contracts and upgrade to the latest # Prepare master version of contracts and upgrade to the latest
@ -177,17 +218,8 @@ def test_upgradeability(temp_dir_path):
staking_escrow_deployer = StakingEscrowDeployer(registry=registry, economics=economics) staking_escrow_deployer = StakingEscrowDeployer(registry=registry, economics=economics)
staking_escrow_deployer.deploy(deployment_mode=constants.INIT, transacting_power=transacting_power) staking_escrow_deployer.deploy(deployment_mode=constants.INIT, transacting_power=transacting_power)
policy_manager_deployer = PolicyManagerDeployer(registry=registry, economics=economics) if not skip_staking_escrow_test:
deploy_base_contract(blockchain_interface, policy_manager_deployer, economics.worklock_supply = economics.maximum_allowed_locked
transacting_power=transacting_power,
skipt_test=skip_policy_manager_test)
adjudicator_deployer = AdjudicatorDeployer(registry=registry, economics=economics)
deploy_base_contract(blockchain_interface, adjudicator_deployer,
transacting_power=transacting_power,
skipt_test=skip_adjudicator_test)
if skip_staking_escrow_test:
worklock_deployer = WorklockDeployer(registry=registry, economics=economics) worklock_deployer = WorklockDeployer(registry=registry, economics=economics)
worklock_deployer.deploy(transacting_power=transacting_power) worklock_deployer.deploy(transacting_power=transacting_power)
@ -197,21 +229,13 @@ def test_upgradeability(temp_dir_path):
skipt_test=skip_staking_escrow_test) skipt_test=skip_staking_escrow_test)
if not skip_staking_escrow_test: if not skip_staking_escrow_test:
# TODO prepare at least one staker before calling upgrade prepare_staker(blockchain_interface=blockchain_interface,
deployer=staking_escrow_deployer,
transacting_power=transacting_power)
staking_escrow_deployer.upgrade(transacting_power=transacting_power, staking_escrow_deployer.upgrade(transacting_power=transacting_power,
contract_version="latest", contract_version="latest",
confirmations=0) confirmations=0)
if not skip_policy_manager_test:
policy_manager_deployer.upgrade(transacting_power=transacting_power,
contract_version="latest",
confirmations=0)
if not skip_adjudicator_test:
adjudicator_deployer.upgrade(transacting_power=transacting_power,
contract_version="latest",
confirmations=0)
finally: finally:
# Unregister interface # TODO: Move to method? # Unregister interface # TODO: Move to method?
with contextlib.suppress(KeyError): with contextlib.suppress(KeyError):