2019-09-20 01:11:19 +00:00
import os
2019-09-01 04:01:37 +00:00
from nucypher . blockchain . eth . agents import (
PolicyManagerAgent ,
StakingEscrowAgent ,
AdjudicatorAgent ,
2019-08-31 23:16:01 +00:00
ContractAgency
2019-09-01 04:01:37 +00:00
)
2020-02-24 23:29:11 +00:00
from nucypher . blockchain . eth . constants import (
NUCYPHER_TOKEN_CONTRACT_NAME ,
STAKING_ESCROW_CONTRACT_NAME ,
POLICY_MANAGER_CONTRACT_NAME ,
ADJUDICATOR_CONTRACT_NAME ,
DISPATCHER_CONTRACT_NAME
)
2019-09-20 00:23:48 +00:00
from nucypher . blockchain . eth . deployers import StakingEscrowDeployer
2020-02-24 23:29:11 +00:00
from nucypher . blockchain . eth . registry import LocalContractRegistry , InMemoryContractRegistry
2020-01-14 22:52:50 +00:00
from nucypher . cli . commands . deploy import deploy
2019-09-20 01:11:19 +00:00
from nucypher . utilities . sandbox . constants import (
TEST_PROVIDER_URI ,
INSECURE_DEVELOPMENT_PASSWORD ,
INSECURE_DEPLOYMENT_SECRET_PLAINTEXT
)
2019-07-26 16:38:01 +00:00
2019-09-20 05:24:51 +00:00
ALTERNATE_REGISTRY_FILEPATH = ' /tmp/nucypher-test-registry-alternate.json '
2020-02-24 23:29:11 +00:00
ALTERNATE_REGISTRY_FILEPATH_2 = ' /tmp/nucypher-test-registry-alternate-2.json '
2019-07-28 01:46:19 +00:00
2020-01-17 02:45:50 +00:00
def test_nucypher_deploy_inspect_no_deployments ( click_runner , testerchain , new_local_registry ) :
2019-07-28 01:46:19 +00:00
2019-09-01 04:01:37 +00:00
status_command = ( ' inspect ' ,
2019-07-28 01:46:19 +00:00
' --provider ' , TEST_PROVIDER_URI ,
2020-01-17 02:45:50 +00:00
' --registry-infile ' , new_local_registry . filepath ,
2019-07-28 01:46:19 +00:00
' --poa ' )
result = click_runner . invoke ( deploy , status_command , catch_exceptions = False )
assert result . exit_code == 0
2020-01-17 02:45:50 +00:00
assert ' not enrolled ' in result . output
2019-07-28 01:46:19 +00:00
2020-02-15 18:50:42 +00:00
def test_set_range ( click_runner , testerchain , agency_local_registry ) :
minimum , default , maximum = 10 , 20 , 30
status_command = ( ' set-range ' ,
' --provider ' , TEST_PROVIDER_URI ,
' --registry-infile ' , agency_local_registry . filepath ,
' --poa ' ,
' --minimum ' , minimum ,
' --default ' , default ,
' --maximum ' , maximum )
account_index = ' 0 \n '
yes = ' Y \n '
user_input = account_index + yes + yes
result = click_runner . invoke ( deploy ,
status_command ,
input = user_input ,
catch_exceptions = False )
assert result . exit_code == 0
assert f " range [ { minimum } , { maximum } ] " in result . output
assert f " default value { default } " in result . output
2020-01-17 02:45:50 +00:00
def test_nucypher_deploy_inspect_fully_deployed ( click_runner , agency_local_registry ) :
2019-07-28 01:46:19 +00:00
2020-01-17 02:45:50 +00:00
staking_agent = ContractAgency . get_agent ( StakingEscrowAgent , registry = agency_local_registry )
policy_agent = ContractAgency . get_agent ( PolicyManagerAgent , registry = agency_local_registry )
adjudicator_agent = ContractAgency . get_agent ( AdjudicatorAgent , registry = agency_local_registry )
2019-09-01 04:01:37 +00:00
status_command = ( ' inspect ' ,
2020-01-17 02:45:50 +00:00
' --registry-infile ' , agency_local_registry . filepath ,
2019-07-28 01:46:19 +00:00
' --provider ' , TEST_PROVIDER_URI ,
' --poa ' )
2019-09-01 04:01:37 +00:00
result = click_runner . invoke ( deploy ,
status_command ,
catch_exceptions = False )
2019-07-28 01:46:19 +00:00
assert result . exit_code == 0
2019-09-01 04:01:37 +00:00
assert staking_agent . owner in result . output
assert policy_agent . owner in result . output
assert adjudicator_agent . owner in result . output
2020-02-15 18:50:42 +00:00
minimum , default , maximum = 10 , 20 , 30
assert ' Range ' in result . output
assert f " { minimum } wei " in result . output
assert f " { default } wei " in result . output
assert f " { maximum } wei " in result . output
2019-09-01 04:01:37 +00:00
2020-01-17 02:45:50 +00:00
def test_transfer_ownership ( click_runner , testerchain , agency_local_registry ) :
2019-07-28 01:46:19 +00:00
2020-01-17 02:45:50 +00:00
staking_agent = ContractAgency . get_agent ( StakingEscrowAgent , registry = agency_local_registry )
policy_agent = ContractAgency . get_agent ( PolicyManagerAgent , registry = agency_local_registry )
adjudicator_agent = ContractAgency . get_agent ( AdjudicatorAgent , registry = agency_local_registry )
2019-07-28 01:46:19 +00:00
2019-09-01 04:01:37 +00:00
assert staking_agent . owner == testerchain . etherbase_account
assert policy_agent . owner == testerchain . etherbase_account
assert adjudicator_agent . owner == testerchain . etherbase_account
2019-07-26 16:38:01 +00:00
maclane = testerchain . unassigned_accounts [ 0 ]
2019-07-28 01:46:19 +00:00
2019-07-26 16:38:01 +00:00
ownership_command = ( ' transfer-ownership ' ,
2020-01-17 02:45:50 +00:00
' --registry-infile ' , agency_local_registry . filepath ,
2019-07-28 01:46:19 +00:00
' --provider ' , TEST_PROVIDER_URI ,
2019-07-26 16:38:01 +00:00
' --target-address ' , maclane ,
' --poa ' )
account_index = ' 0 \n '
yes = ' Y \n '
2019-09-01 04:01:37 +00:00
user_input = account_index + yes + yes
2019-07-26 16:38:01 +00:00
result = click_runner . invoke ( deploy ,
ownership_command ,
input = user_input ,
catch_exceptions = False )
assert result . exit_code == 0
2019-09-01 04:01:37 +00:00
assert staking_agent . owner == maclane
assert policy_agent . owner == maclane
assert adjudicator_agent . owner == maclane
2019-07-26 16:38:01 +00:00
2019-09-01 04:01:37 +00:00
michwill = testerchain . unassigned_accounts [ 1 ]
2019-07-26 16:38:01 +00:00
ownership_command = ( ' transfer-ownership ' ,
2019-07-28 01:46:19 +00:00
' --deployer-address ' , maclane ,
2019-07-26 16:38:01 +00:00
' --contract-name ' , STAKING_ESCROW_CONTRACT_NAME ,
2020-01-17 02:45:50 +00:00
' --registry-infile ' , agency_local_registry . filepath ,
2019-07-28 01:46:19 +00:00
' --provider ' , TEST_PROVIDER_URI ,
' --target-address ' , michwill ,
2019-07-26 16:38:01 +00:00
' --poa ' )
user_input = yes
result = click_runner . invoke ( deploy ,
ownership_command ,
input = user_input ,
catch_exceptions = False )
assert result . exit_code == 0
2019-09-01 04:01:37 +00:00
assert staking_agent . owner != maclane
assert staking_agent . owner == michwill
2019-09-20 00:23:48 +00:00
2020-01-17 02:45:50 +00:00
def test_bare_contract_deployment_to_alternate_registry ( click_runner , agency_local_registry ) :
2019-09-20 01:11:19 +00:00
2019-09-20 05:24:51 +00:00
if os . path . exists ( ALTERNATE_REGISTRY_FILEPATH ) :
os . remove ( ALTERNATE_REGISTRY_FILEPATH )
assert not os . path . exists ( ALTERNATE_REGISTRY_FILEPATH )
2019-09-20 00:23:48 +00:00
command = ( ' contracts ' ,
' --contract-name ' , StakingEscrowDeployer . contract_name ,
2020-02-21 15:15:54 +00:00
' --mode ' , ' bare ' ,
2019-09-20 00:23:48 +00:00
' --provider ' , TEST_PROVIDER_URI ,
2020-01-17 02:45:50 +00:00
' --registry-infile ' , agency_local_registry . filepath ,
2019-09-20 05:24:51 +00:00
' --registry-outfile ' , ALTERNATE_REGISTRY_FILEPATH ,
2019-11-30 16:38:23 +00:00
' --poa ' ,
' --ignore-deployed ' )
2019-09-20 00:23:48 +00:00
user_input = ' 0 \n ' + ' Y \n ' + ' DEPLOY '
result = click_runner . invoke ( deploy , command , input = user_input , catch_exceptions = False )
assert result . exit_code == 0
2019-09-20 01:11:19 +00:00
# Verify alternate registry output
2020-01-17 02:45:50 +00:00
assert os . path . exists ( agency_local_registry . filepath )
2019-09-20 05:24:51 +00:00
assert os . path . exists ( ALTERNATE_REGISTRY_FILEPATH )
new_registry = LocalContractRegistry ( filepath = ALTERNATE_REGISTRY_FILEPATH )
2020-01-17 02:45:50 +00:00
assert agency_local_registry != new_registry
2019-09-20 01:11:19 +00:00
2020-01-17 02:45:50 +00:00
old_enrolled_names = list ( agency_local_registry . enrolled_names ) . count ( StakingEscrowDeployer . contract_name )
2019-09-20 01:11:19 +00:00
new_enrolled_names = list ( new_registry . enrolled_names ) . count ( StakingEscrowDeployer . contract_name )
assert new_enrolled_names == old_enrolled_names + 1
2019-09-20 00:23:48 +00:00
2020-01-15 12:19:05 +00:00
# TODO: test to validate retargetting via multisig, specifically, building the transaction
2020-01-17 02:45:50 +00:00
def test_manual_proxy_retargeting ( testerchain , click_runner , token_economics ) :
2019-09-20 01:11:19 +00:00
# A local, alternate filepath registry exists
2019-09-20 05:24:51 +00:00
assert os . path . exists ( ALTERNATE_REGISTRY_FILEPATH )
local_registry = LocalContractRegistry ( filepath = ALTERNATE_REGISTRY_FILEPATH )
2019-09-20 00:23:48 +00:00
deployer = StakingEscrowDeployer ( deployer_address = testerchain . etherbase_account ,
registry = local_registry ,
economics = token_economics )
2020-02-21 13:54:03 +00:00
proxy_deployer = deployer . get_proxy_deployer ( )
2019-09-20 00:23:48 +00:00
2020-01-14 03:02:17 +00:00
# Un-targeted enrollment is indeed un targeted by the proxy
2020-02-21 13:54:03 +00:00
untargeted_deployment = deployer . get_latest_enrollment ( )
2019-09-20 01:11:19 +00:00
assert proxy_deployer . target_contract . address != untargeted_deployment . address
# MichWill still owns this proxy.
michwill = testerchain . unassigned_accounts [ 1 ]
assert proxy_deployer . target_contract . functions . owner ( ) . call ( ) == michwill
2019-09-20 00:23:48 +00:00
command = ( ' upgrade ' ,
' --retarget ' ,
2019-09-20 01:11:19 +00:00
' --deployer-address ' , michwill ,
2019-09-20 00:23:48 +00:00
' --contract-name ' , StakingEscrowDeployer . contract_name ,
' --target-address ' , untargeted_deployment . address ,
' --provider ' , TEST_PROVIDER_URI ,
2019-09-20 05:24:51 +00:00
' --registry-infile ' , ALTERNATE_REGISTRY_FILEPATH ,
2019-09-20 00:23:48 +00:00
' --poa ' )
2019-09-20 01:11:19 +00:00
# Reveal the secret and upgrade
2019-09-20 00:23:48 +00:00
old_secret = INSECURE_DEPLOYMENT_SECRET_PLAINTEXT . decode ( )
user_input = ' 0 \n ' + ' Y \n ' + f ' { old_secret } \n ' + ( f ' { INSECURE_DEVELOPMENT_PASSWORD } \n ' * 2 ) + ' Y \n '
result = click_runner . invoke ( deploy , command , input = user_input , catch_exceptions = False )
assert result . exit_code == 0
2019-09-20 01:11:19 +00:00
# The proxy target has been updated.
2020-02-21 13:54:03 +00:00
proxy_deployer = deployer . get_proxy_deployer ( )
2019-09-20 01:11:19 +00:00
assert proxy_deployer . target_contract . address == untargeted_deployment . address
2020-01-15 12:19:05 +00:00
2020-02-24 23:29:11 +00:00
def test_manual_deployment_of_idle_network ( click_runner ) :
if os . path . exists ( ALTERNATE_REGISTRY_FILEPATH_2 ) :
os . remove ( ALTERNATE_REGISTRY_FILEPATH_2 )
assert not os . path . exists ( ALTERNATE_REGISTRY_FILEPATH_2 )
registry = LocalContractRegistry ( filepath = ALTERNATE_REGISTRY_FILEPATH_2 )
registry . write ( InMemoryContractRegistry ( ) . read ( ) ) # FIXME: Manual deployments from scratch require an existing but empty registry (i.e., a json file just with "[]")
# 1. Deploy NuCypherToken
command = ( ' contracts ' ,
' --contract-name ' , NUCYPHER_TOKEN_CONTRACT_NAME ,
' --provider ' , TEST_PROVIDER_URI ,
' --registry-infile ' , ALTERNATE_REGISTRY_FILEPATH_2 ,
' --poa ' )
user_input = ' 0 \n ' + ' Y \n ' + INSECURE_DEVELOPMENT_PASSWORD
result = click_runner . invoke ( deploy , command , input = user_input , catch_exceptions = False )
assert result . exit_code == 0
assert os . path . exists ( ALTERNATE_REGISTRY_FILEPATH_2 )
new_registry = LocalContractRegistry ( filepath = ALTERNATE_REGISTRY_FILEPATH_2 )
deployed_contracts = [ NUCYPHER_TOKEN_CONTRACT_NAME ]
assert list ( new_registry . enrolled_names ) == deployed_contracts
# 2. Deploy StakingEscrow in IDLE mode
command = ( ' contracts ' ,
' --contract-name ' , STAKING_ESCROW_CONTRACT_NAME ,
' --mode ' , ' idle ' ,
' --provider ' , TEST_PROVIDER_URI ,
' --registry-infile ' , ALTERNATE_REGISTRY_FILEPATH_2 ,
' --poa ' )
secret = INSECURE_DEPLOYMENT_SECRET_PLAINTEXT . decode ( )
user_input = ' 0 \n ' + ' Y \n ' + ( f ' { secret } \n ' * 2 )
result = click_runner . invoke ( deploy , command , input = user_input , catch_exceptions = False )
assert result . exit_code == 0
deployed_contracts . extend ( [ STAKING_ESCROW_CONTRACT_NAME , DISPATCHER_CONTRACT_NAME ] )
assert list ( new_registry . enrolled_names ) == deployed_contracts
# 3. Deploy PolicyManager
command = ( ' contracts ' ,
' --contract-name ' , POLICY_MANAGER_CONTRACT_NAME ,
' --provider ' , TEST_PROVIDER_URI ,
' --registry-infile ' , ALTERNATE_REGISTRY_FILEPATH_2 ,
' --poa ' )
user_input = ' 0 \n ' + ' Y \n ' + ( f ' { secret } \n ' * 2 )
result = click_runner . invoke ( deploy , command , input = user_input , catch_exceptions = False )
assert result . exit_code == 0
deployed_contracts . extend ( [ POLICY_MANAGER_CONTRACT_NAME , DISPATCHER_CONTRACT_NAME ] )
assert list ( new_registry . enrolled_names ) == deployed_contracts
# 4. Deploy Adjudicator
command = ( ' contracts ' ,
' --contract-name ' , ADJUDICATOR_CONTRACT_NAME ,
' --provider ' , TEST_PROVIDER_URI ,
' --registry-infile ' , ALTERNATE_REGISTRY_FILEPATH_2 ,
' --poa ' )
user_input = ' 0 \n ' + ' Y \n ' + ( f ' { secret } \n ' * 2 )
result = click_runner . invoke ( deploy , command , input = user_input , catch_exceptions = False )
assert result . exit_code == 0
deployed_contracts . extend ( [ ADJUDICATOR_CONTRACT_NAME , DISPATCHER_CONTRACT_NAME ] )
assert list ( new_registry . enrolled_names ) == deployed_contracts
# 5. Activate StakingEscrow
command = ( ' contracts ' ,
' --contract-name ' , STAKING_ESCROW_CONTRACT_NAME ,
' --activate ' ,
' --provider ' , TEST_PROVIDER_URI ,
' --registry-infile ' , ALTERNATE_REGISTRY_FILEPATH_2 ,
' --poa ' )
user_input = ' 0 \n ' + ' Y \n ' + ' Y \n '
result = click_runner . invoke ( deploy , command , input = user_input , catch_exceptions = False )
assert result . exit_code == 0
assert list ( new_registry . enrolled_names ) == deployed_contracts