2018-11-04 19:23:11 +00:00
|
|
|
"""
|
|
|
|
This file is part of nucypher.
|
|
|
|
|
|
|
|
nucypher is free software: you can redistribute it and/or modify
|
2019-03-05 02:50:11 +00:00
|
|
|
it under the terms of the GNU Affero General Public License as published by
|
2018-11-04 19:23:11 +00:00
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
nucypher is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-03-05 02:50:11 +00:00
|
|
|
GNU Affero General Public License for more details.
|
2018-11-04 19:23:11 +00:00
|
|
|
|
2019-03-05 02:50:11 +00:00
|
|
|
You should have received a copy of the GNU Affero General Public License
|
2018-11-04 19:23:11 +00:00
|
|
|
along with nucypher. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
"""
|
2019-07-09 21:21:00 +00:00
|
|
|
|
|
|
|
import pytest
|
2019-07-10 09:41:14 +00:00
|
|
|
from eth_utils import keccak
|
2018-10-17 16:33:20 +00:00
|
|
|
|
2019-08-15 03:29:24 +00:00
|
|
|
from nucypher.blockchain.eth.agents import StakingEscrowAgent, ContractAgency
|
2019-07-10 12:00:52 +00:00
|
|
|
from nucypher.blockchain.eth.deployers import (StakingEscrowDeployer,
|
2019-07-09 21:21:00 +00:00
|
|
|
DispatcherDeployer)
|
2019-09-19 20:13:43 +00:00
|
|
|
from nucypher.crypto.api import keccak_digest
|
|
|
|
from nucypher.utilities.sandbox.constants import STAKING_ESCROW_DEPLOYMENT_SECRET, TEST_PROVIDER_URI
|
2018-10-17 16:33:20 +00:00
|
|
|
|
|
|
|
|
2019-07-29 14:28:48 +00:00
|
|
|
def test_staking_escrow_deployment(staking_escrow_deployer, deployment_progress):
|
2019-07-10 09:41:14 +00:00
|
|
|
secret_hash = keccak(text=STAKING_ESCROW_DEPLOYMENT_SECRET)
|
2019-07-29 14:28:48 +00:00
|
|
|
deployment_receipts = staking_escrow_deployer.deploy(secret_hash=secret_hash, progress=deployment_progress)
|
2018-10-17 16:33:20 +00:00
|
|
|
|
2019-07-29 14:28:48 +00:00
|
|
|
# deployment steps must match expected number of steps
|
2019-08-02 23:06:32 +00:00
|
|
|
assert deployment_progress.num_steps == len(staking_escrow_deployer.deployment_steps) == len(deployment_receipts) == 4
|
2019-06-24 11:41:03 +00:00
|
|
|
|
2019-08-02 23:06:32 +00:00
|
|
|
for step in staking_escrow_deployer.deployment_steps:
|
|
|
|
assert deployment_receipts[step]['status'] == 1
|
2018-10-17 16:33:20 +00:00
|
|
|
|
2019-07-09 21:21:00 +00:00
|
|
|
|
2019-08-12 07:15:43 +00:00
|
|
|
def test_make_agent(staking_escrow_deployer, test_registry):
|
2019-07-09 21:21:00 +00:00
|
|
|
# Create a StakingEscrowAgent instance
|
|
|
|
staking_agent = staking_escrow_deployer.make_agent()
|
|
|
|
|
2019-06-24 11:41:03 +00:00
|
|
|
# Retrieve the StakingEscrowAgent singleton
|
2019-08-15 03:29:24 +00:00
|
|
|
same_staking_agent = ContractAgency.get_agent(StakingEscrowAgent, registry=test_registry)
|
2019-06-24 11:41:03 +00:00
|
|
|
assert staking_agent == same_staking_agent
|
2018-10-17 16:33:20 +00:00
|
|
|
|
2019-08-21 12:11:39 +00:00
|
|
|
# Compare the contract address for equality
|
2019-05-29 14:15:18 +00:00
|
|
|
assert staking_agent.contract_address == same_staking_agent.contract_address
|
2018-10-17 16:33:20 +00:00
|
|
|
|
2019-07-09 21:21:00 +00:00
|
|
|
|
2019-08-12 07:15:43 +00:00
|
|
|
def test_deployment_parameters(staking_escrow_deployer,
|
|
|
|
token_deployer,
|
|
|
|
token_economics,
|
|
|
|
test_registry):
|
2019-07-10 12:00:52 +00:00
|
|
|
|
|
|
|
token_address = staking_escrow_deployer.contract.functions.token().call()
|
|
|
|
assert token_deployer.contract_address == token_address
|
|
|
|
|
2019-08-15 03:29:24 +00:00
|
|
|
staking_agent = ContractAgency.get_agent(StakingEscrowAgent, registry=test_registry)
|
2019-07-10 12:00:52 +00:00
|
|
|
params = staking_agent.staking_parameters()
|
|
|
|
assert token_economics.staking_deployment_parameters[1:] == params[1:]
|
|
|
|
assert token_economics.staking_deployment_parameters[0]*60*60 == params[0] # FIXME: Do we really want this?
|
|
|
|
|
|
|
|
|
2019-08-12 07:15:43 +00:00
|
|
|
def test_staking_escrow_has_dispatcher(staking_escrow_deployer, testerchain, test_registry):
|
2019-07-09 21:21:00 +00:00
|
|
|
|
|
|
|
# Let's get the "bare" StakingEscrow contract (i.e., unwrapped, no dispatcher)
|
2019-08-12 07:15:43 +00:00
|
|
|
existing_bare_contract = testerchain.get_contract_by_name(registry=test_registry,
|
|
|
|
name=staking_escrow_deployer.contract_name,
|
|
|
|
proxy_name=DispatcherDeployer.contract_name,
|
|
|
|
use_proxy_address=False)
|
2019-07-09 21:21:00 +00:00
|
|
|
|
|
|
|
# This contract shouldn't be accessible directly through the deployer or the agent
|
|
|
|
assert staking_escrow_deployer.contract_address != existing_bare_contract.address
|
2019-08-15 03:29:24 +00:00
|
|
|
staking_agent = ContractAgency.get_agent(StakingEscrowAgent, registry=test_registry)
|
2019-07-09 21:21:00 +00:00
|
|
|
assert staking_agent.contract_address != existing_bare_contract
|
|
|
|
|
|
|
|
# The wrapped contract, on the other hand, points to the bare one.
|
|
|
|
target = staking_escrow_deployer.contract.functions.target().call()
|
|
|
|
assert target == existing_bare_contract.address
|
|
|
|
|
|
|
|
|
2019-08-12 07:15:43 +00:00
|
|
|
def test_upgrade(testerchain, test_registry):
|
2019-07-09 21:21:00 +00:00
|
|
|
wrong_secret = b"on second thoughts..."
|
|
|
|
old_secret = bytes(STAKING_ESCROW_DEPLOYMENT_SECRET, encoding='utf-8')
|
2019-07-10 09:41:14 +00:00
|
|
|
new_secret_hash = keccak(b'new'+old_secret)
|
2019-07-09 21:21:00 +00:00
|
|
|
|
2019-08-12 07:15:43 +00:00
|
|
|
deployer = StakingEscrowDeployer(registry=test_registry,
|
|
|
|
deployer_address=testerchain.etherbase_account)
|
2019-07-09 21:21:00 +00:00
|
|
|
|
|
|
|
with pytest.raises(deployer.ContractDeploymentError):
|
|
|
|
deployer.upgrade(existing_secret_plaintext=wrong_secret,
|
|
|
|
new_secret_hash=new_secret_hash)
|
|
|
|
|
|
|
|
receipts = deployer.upgrade(existing_secret_plaintext=old_secret,
|
|
|
|
new_secret_hash=new_secret_hash)
|
|
|
|
|
2019-07-16 11:20:49 +00:00
|
|
|
for title, receipt in receipts.items():
|
|
|
|
assert receipt['status'] == 1
|
2019-07-09 21:21:00 +00:00
|
|
|
|
|
|
|
|
2019-08-12 07:15:43 +00:00
|
|
|
def test_rollback(testerchain, test_registry):
|
2019-07-09 21:21:00 +00:00
|
|
|
old_secret = bytes('new'+STAKING_ESCROW_DEPLOYMENT_SECRET, encoding='utf-8')
|
2019-07-10 09:41:14 +00:00
|
|
|
new_secret_hash = keccak(text="third time's the charm")
|
2019-07-09 21:21:00 +00:00
|
|
|
|
2019-08-12 07:15:43 +00:00
|
|
|
deployer = StakingEscrowDeployer(registry=test_registry,
|
|
|
|
deployer_address=testerchain.etherbase_account)
|
2019-07-09 21:21:00 +00:00
|
|
|
|
2019-08-15 03:29:24 +00:00
|
|
|
staking_agent = ContractAgency.get_agent(StakingEscrowAgent, registry=test_registry)
|
2019-07-09 21:21:00 +00:00
|
|
|
current_target = staking_agent.contract.functions.target().call()
|
|
|
|
|
|
|
|
# Let's do one more upgrade
|
2019-09-19 20:13:43 +00:00
|
|
|
receipts = deployer.upgrade(existing_secret_plaintext=old_secret, new_secret_hash=new_secret_hash)
|
|
|
|
|
2019-07-16 11:20:49 +00:00
|
|
|
for title, receipt in receipts.items():
|
|
|
|
assert receipt['status'] == 1
|
2019-07-09 21:21:00 +00:00
|
|
|
|
|
|
|
old_target = current_target
|
|
|
|
current_target = staking_agent.contract.functions.target().call()
|
|
|
|
assert current_target != old_target
|
|
|
|
|
|
|
|
# It's time to rollback. But first...
|
|
|
|
wrong_secret = b"WRONG!!"
|
|
|
|
with pytest.raises(deployer.ContractDeploymentError):
|
|
|
|
deployer.rollback(existing_secret_plaintext=wrong_secret,
|
|
|
|
new_secret_hash=new_secret_hash)
|
|
|
|
|
|
|
|
# OK, *now* is time for rollback
|
|
|
|
old_secret = b"third time's the charm"
|
2019-07-10 09:41:14 +00:00
|
|
|
new_secret_hash = keccak(text="...maybe not.")
|
2019-07-16 11:20:49 +00:00
|
|
|
receipt = deployer.rollback(existing_secret_plaintext=old_secret,
|
|
|
|
new_secret_hash=new_secret_hash)
|
2019-07-09 21:21:00 +00:00
|
|
|
|
2019-07-16 11:20:49 +00:00
|
|
|
assert receipt['status'] == 1
|
2019-07-09 21:21:00 +00:00
|
|
|
|
|
|
|
new_target = staking_agent.contract.functions.target().call()
|
|
|
|
assert new_target != current_target
|
|
|
|
assert new_target == old_target
|
2019-09-19 20:13:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_deploy_bare_upgradeable_contract_deployment(testerchain, test_registry, token_economics):
|
|
|
|
deployer = StakingEscrowDeployer(registry=test_registry,
|
|
|
|
deployer_address=testerchain.etherbase_account,
|
|
|
|
economics=token_economics)
|
|
|
|
|
|
|
|
enrolled_names = list(test_registry.enrolled_names)
|
|
|
|
old_number_of_enrollments = enrolled_names.count(StakingEscrowDeployer.contract_name)
|
|
|
|
old_number_of_proxy_enrollments = enrolled_names.count(StakingEscrowDeployer._proxy_deployer.contract_name)
|
|
|
|
|
|
|
|
receipts = deployer.deploy(initial_deployment=False)
|
|
|
|
for title, receipt in receipts.items():
|
|
|
|
assert receipt['status'] == 1
|
|
|
|
|
|
|
|
enrolled_names = list(test_registry.enrolled_names)
|
|
|
|
new_number_of_enrollments = enrolled_names.count(StakingEscrowDeployer.contract_name)
|
|
|
|
new_number_of_proxy_enrollments = enrolled_names.count(StakingEscrowDeployer._proxy_deployer.contract_name)
|
|
|
|
|
|
|
|
# The prinicipal contract was deployed.
|
|
|
|
assert new_number_of_enrollments == (old_number_of_enrollments + 1)
|
|
|
|
|
|
|
|
# The Dispatcher was not deployed.
|
|
|
|
assert new_number_of_proxy_enrollments == old_number_of_proxy_enrollments
|
|
|
|
|
|
|
|
|
|
|
|
def test_manual_proxy_retargeting(testerchain, test_registry, token_economics):
|
|
|
|
|
|
|
|
deployer = StakingEscrowDeployer(registry=test_registry,
|
|
|
|
deployer_address=testerchain.etherbase_account,
|
|
|
|
economics=token_economics)
|
|
|
|
|
|
|
|
# Get Proxy-Direct
|
|
|
|
existing_bare_contract = deployer.get_latest_version(registry=test_registry, provider_uri=TEST_PROVIDER_URI)
|
|
|
|
proxy_deployer = StakingEscrowDeployer._proxy_deployer(registry=test_registry,
|
|
|
|
target_contract=existing_bare_contract,
|
|
|
|
deployer_address=testerchain.etherbase_account,
|
|
|
|
bare=True) # acquire agency for the proxy itself.
|
|
|
|
|
|
|
|
# Re-Deploy Staking Escrow
|
|
|
|
staking_agent = ContractAgency.get_agent(StakingEscrowAgent, registry=test_registry)
|
|
|
|
old_target = proxy_deployer.contract.functions.target().call()
|
|
|
|
|
|
|
|
old_secret = bytes("...maybe not.", encoding='utf-8')
|
|
|
|
new_secret = keccak_digest(bytes('thistimeforsure', encoding='utf-8'))
|
|
|
|
receipt = deployer.retarget(target_address=staking_agent.contract_address,
|
|
|
|
existing_secret_plaintext=old_secret,
|
|
|
|
new_secret_hash=new_secret)
|
|
|
|
|
|
|
|
assert receipt['status'] == 1
|
|
|
|
|
|
|
|
#
|
|
|
|
# Post-Retargeting
|
|
|
|
#
|
|
|
|
|
|
|
|
staking_agent = ContractAgency.get_agent(StakingEscrowAgent, registry=test_registry)
|
|
|
|
new_target = proxy_deployer.contract.functions.target().call()
|
|
|
|
|
|
|
|
assert old_target != new_target
|
|
|
|
assert new_target == staking_agent.contract_address
|