mirror of https://github.com/nucypher/nucypher.git
Fixed finishUpgrade methods in the MinerEscrow and Issuer, added vote method to the UserEscrow
parent
fce814f8d9
commit
41d24f9ad9
|
@ -64,9 +64,6 @@ contract Issuer is Upgradeable {
|
|||
secondsPerPeriod = _hoursPerPeriod.mul(1 hours);
|
||||
lockedPeriodsCoefficient = _lockedPeriodsCoefficient;
|
||||
awardedPeriods = _awardedPeriods;
|
||||
|
||||
lastMintedPeriod = getCurrentPeriod();
|
||||
futureSupply = token.totalSupply();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -90,6 +87,8 @@ contract Issuer is Upgradeable {
|
|||
**/
|
||||
function initialize() public {
|
||||
require(currentSupply1 == 0);
|
||||
lastMintedPeriod = getCurrentPeriod();
|
||||
futureSupply = token.totalSupply();
|
||||
uint256 reservedReward = token.balanceOf(address(this));
|
||||
uint256 currentTotalSupply = futureSupply.sub(reservedReward);
|
||||
currentSupply1 = currentTotalSupply;
|
||||
|
@ -173,8 +172,5 @@ contract Issuer is Upgradeable {
|
|||
secondsPerPeriod = issuer.secondsPerPeriod();
|
||||
lockedPeriodsCoefficient = issuer.lockedPeriodsCoefficient();
|
||||
awardedPeriods = issuer.awardedPeriods();
|
||||
|
||||
lastMintedPeriod = issuer.lastMintedPeriod();
|
||||
futureSupply = issuer.futureSupply();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -116,7 +116,7 @@ contract MinersEscrow is Issuer {
|
|||
_awardedPeriods
|
||||
)
|
||||
{
|
||||
require(_minLockedPeriods != 0);
|
||||
require(_minLockedPeriods != 0 && _maxAllowableLockedTokens != 0);
|
||||
minLockedPeriods = _minLockedPeriods;
|
||||
minAllowableLockedTokens = _minAllowableLockedTokens;
|
||||
maxAllowableLockedTokens = _maxAllowableLockedTokens;
|
||||
|
@ -194,34 +194,34 @@ contract MinersEscrow is Issuer {
|
|||
|
||||
/**
|
||||
* @notice Pre-deposit tokens
|
||||
* @param _owners Tokens owners
|
||||
* @param _miners Tokens owners
|
||||
* @param _values Amount of token to deposit for each owner
|
||||
* @param _periods Amount of periods during which tokens will be unlocked for each owner
|
||||
**/
|
||||
function preDeposit(address[] _owners, uint256[] _values, uint256[] _periods)
|
||||
public isInitialized onlyOwner
|
||||
function preDeposit(address[] _miners, uint256[] _values, uint256[] _periods)
|
||||
public isInitialized
|
||||
{
|
||||
require(_owners.length != 0 &&
|
||||
_owners.length == _values.length &&
|
||||
_owners.length == _periods.length);
|
||||
require(_miners.length != 0 &&
|
||||
_miners.length == _values.length &&
|
||||
_miners.length == _periods.length);
|
||||
uint256 currentPeriod = getCurrentPeriod();
|
||||
uint256 allValue = 0;
|
||||
|
||||
for (uint256 i = 0; i < _owners.length; i++) {
|
||||
address owner = _owners[i];
|
||||
for (uint256 i = 0; i < _miners.length; i++) {
|
||||
address miner = _miners[i];
|
||||
uint256 value = _values[i];
|
||||
uint256 periods = _periods[i];
|
||||
MinerInfo storage info = minerInfo[owner];
|
||||
MinerInfo storage info = minerInfo[miner];
|
||||
require(info.value == 0 &&
|
||||
value >= minAllowableLockedTokens &&
|
||||
value <= maxAllowableLockedTokens &&
|
||||
periods >= minLockedPeriods);
|
||||
miners.push(owner);
|
||||
miners.push(miner);
|
||||
info.lastActivePeriod = currentPeriod;
|
||||
info.value = value;
|
||||
info.stakes.push(StakeInfo(currentPeriod.add(uint256(1)), 0, periods, value));
|
||||
allValue = allValue.add(value);
|
||||
emit Deposited(owner, value, periods);
|
||||
emit Deposited(miner, value, periods);
|
||||
}
|
||||
|
||||
token.safeTransferFrom(msg.sender, address(this), allValue);
|
||||
|
@ -752,7 +752,6 @@ contract MinersEscrow is Issuer {
|
|||
function finishUpgrade(address _target) public onlyOwner {
|
||||
super.finishUpgrade(_target);
|
||||
MinersEscrow escrow = MinersEscrow(_target);
|
||||
policyManager = escrow.policyManager();
|
||||
minLockedPeriods = escrow.minLockedPeriods();
|
||||
minAllowableLockedTokens = escrow.minAllowableLockedTokens();
|
||||
maxAllowableLockedTokens = escrow.maxAllowableLockedTokens();
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
pragma solidity ^0.4.23;
|
||||
|
||||
|
||||
import "zeppelin/token/ERC20/SafeERC20.sol";
|
||||
import "zeppelin/ownership/Ownable.sol";
|
||||
import "zeppelin/math/SafeMath.sol";
|
||||
import "contracts/NuCypherToken.sol";
|
||||
import "contracts/MinersEscrow.sol";
|
||||
import "contracts/PolicyManager.sol";
|
||||
|
||||
import "proxy/Government.sol";
|
||||
|
||||
|
||||
/**
|
||||
|
@ -34,10 +35,12 @@ contract UserEscrow is Ownable {
|
|||
event RewardWithdrawnAsMiner(address indexed owner, uint256 value);
|
||||
event RewardWithdrawn(address indexed owner, uint256 value);
|
||||
event MinRewardRateSet(address indexed owner, uint256 value);
|
||||
event Voted(address indexed owner, bool voteFor);
|
||||
|
||||
NuCypherToken public token;
|
||||
MinersEscrow public escrow;
|
||||
PolicyManager public policyManager;
|
||||
Government public government;
|
||||
uint256 public lockedValue;
|
||||
uint256 public endLockTimestamp;
|
||||
uint256 public lockDuration;
|
||||
|
@ -47,20 +50,24 @@ contract UserEscrow is Ownable {
|
|||
* @param _token Token contract
|
||||
* @param _escrow Escrow contract
|
||||
* @param _policyManager PolicyManager contract
|
||||
* @param _government Government contract
|
||||
**/
|
||||
constructor(
|
||||
NuCypherToken _token,
|
||||
MinersEscrow _escrow,
|
||||
PolicyManager _policyManager
|
||||
PolicyManager _policyManager,
|
||||
Government _government
|
||||
)
|
||||
public
|
||||
{
|
||||
require(address(_token) != 0x0 &&
|
||||
address(_escrow) != 0x0 &&
|
||||
address(_policyManager) != 0x0);
|
||||
address(_policyManager) != 0x0 &&
|
||||
address(_government) != 0x0);
|
||||
token = _token;
|
||||
escrow = _escrow;
|
||||
policyManager = _policyManager;
|
||||
government = _government;
|
||||
}
|
||||
|
||||
function () public payable {}
|
||||
|
@ -193,4 +200,12 @@ contract UserEscrow is Ownable {
|
|||
emit MinRewardRateSet(owner, _minRewardRate);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notice Vote for the upgrade in the government contract
|
||||
**/
|
||||
function vote(bool _voteFor) public onlyOwner {
|
||||
government.vote(_voteFor);
|
||||
emit Voted(owner, _voteFor);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -126,13 +126,13 @@ contract Government is Upgradeable {
|
|||
}
|
||||
|
||||
/**
|
||||
* @notice Vote
|
||||
* @notice Vote for the upgrade
|
||||
**/
|
||||
function vote(bool voteFor) public {
|
||||
function vote(bool _voteFor) public {
|
||||
require(getVotingState() == VotingState.Active && lastVote[msg.sender] < votingNumber);
|
||||
uint256 lockedTokens = MinersEscrowInterface(escrow).getLockedTokens(msg.sender);
|
||||
require(lockedTokens > 0);
|
||||
if (voteFor) {
|
||||
if (_voteFor) {
|
||||
votesFor = votesFor.add(lockedTokens);
|
||||
} else {
|
||||
votesAgainst = votesAgainst.add(lockedTokens);
|
||||
|
|
|
@ -5,6 +5,9 @@ from eth_tester.exceptions import TransactionFailed
|
|||
from web3.contract import Contract
|
||||
|
||||
|
||||
|
||||
NULL_ADDR = '0x' + '0' * 40
|
||||
|
||||
VALUE_FIELD = 0
|
||||
DECIMALS_FIELD = 1
|
||||
CONFIRMED_PERIOD_1_FIELD = 2
|
||||
|
@ -22,6 +25,17 @@ REWARD_FIELD = 0
|
|||
REWARD_RATE_FIELD = 1
|
||||
LAST_MINED_PERIOD_FIELD = 2
|
||||
|
||||
ACTIVE_STATE = 0
|
||||
UPGRADE_WAITING_STATE = 1
|
||||
FINISHED_STATE = 2
|
||||
|
||||
UPGRADE_GOVERNMENT = 0
|
||||
UPGRADE_ESCROW = 1
|
||||
UPGRADE_POLICY_MANAGER = 2
|
||||
ROLLBACK_GOVERNMENT = 3
|
||||
ROLLBACK_ESCROW = 4
|
||||
ROLLBACK_POLICY_MANAGER = 5
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def token(chain):
|
||||
|
@ -68,8 +82,30 @@ def policy_manager(web3, chain, escrow):
|
|||
return contract
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def government(web3, chain, escrow, policy_manager):
|
||||
creator = web3.eth.accounts[0]
|
||||
|
||||
# Creator deploys the government
|
||||
contract, _ = chain.provider.deploy_contract('Government', escrow.address, policy_manager.address, 1)
|
||||
dispatcher, _ = chain.provider.deploy_contract('Dispatcher', contract.address)
|
||||
|
||||
# Wrap dispatcher contract
|
||||
contract = web3.eth.contract(abi=contract.abi, address=dispatcher.address, ContractFactoryClass=Contract)
|
||||
|
||||
# Transfer ownership
|
||||
tx = contract.functions.transferOwnership(contract.address).transact({'from': creator})
|
||||
chain.wait_for_receipt(tx)
|
||||
tx = escrow.functions.transferOwnership(contract.address).transact({'from': creator})
|
||||
chain.wait_for_receipt(tx)
|
||||
tx = policy_manager.functions.transferOwnership(contract.address).transact({'from': creator})
|
||||
chain.wait_for_receipt(tx)
|
||||
|
||||
return contract
|
||||
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_all(web3, chain, token, escrow, policy_manager):
|
||||
def test_all(web3, chain, token, escrow, policy_manager, government):
|
||||
creator, ursula1, ursula2, ursula3, ursula4, alice1, alice2, *everyone_else = web3.eth.accounts
|
||||
|
||||
# Give clients some ether
|
||||
|
@ -108,14 +144,14 @@ def test_all(web3, chain, token, escrow, policy_manager):
|
|||
chain.wait_for_receipt(tx)
|
||||
|
||||
# Deposit some tokens to the user escrow and lock them
|
||||
user_escrow_1, _ = chain.provider.deploy_contract('UserEscrow', token.address, escrow.address, policy_manager.address)
|
||||
user_escrow_1, _ = chain.provider.deploy_contract('UserEscrow', token.address, escrow.address, policy_manager.address, government.address)
|
||||
tx = user_escrow_1.functions.transferOwnership(ursula3).transact({'from': creator})
|
||||
chain.wait_for_receipt(tx)
|
||||
tx = token.functions.approve(user_escrow_1.address, 10000).transact({'from': creator})
|
||||
chain.wait_for_receipt(tx)
|
||||
tx = user_escrow_1.functions.initialDeposit(10000, 20 * 60 * 60).transact({'from': creator})
|
||||
chain.wait_for_receipt(tx)
|
||||
user_escrow_2, _ = chain.provider.deploy_contract('UserEscrow', token.address, escrow.address, policy_manager.address)
|
||||
user_escrow_2, _ = chain.provider.deploy_contract('UserEscrow', token.address, escrow.address, policy_manager.address, government.address)
|
||||
tx = user_escrow_2.functions.transferOwnership(ursula4).transact({'from': creator})
|
||||
chain.wait_for_receipt(tx)
|
||||
tx = token.functions.approve(user_escrow_2.address, 10000).transact({'from': creator})
|
||||
|
@ -362,8 +398,184 @@ def test_all(web3, chain, token, escrow, policy_manager):
|
|||
chain.wait_for_receipt(tx)
|
||||
assert alice2_balance < web3.eth.getBalance(alice2)
|
||||
|
||||
# Voting for upgrade
|
||||
escrow_v1 = escrow.functions.target().call()
|
||||
policy_manager_v1 = policy_manager.functions.target().call()
|
||||
government_v1 = government.functions.target().call()
|
||||
# Creator deploys the contracts as the second versions
|
||||
escrow_v2, _ = chain.provider.deploy_contract(
|
||||
'MinersEscrow',
|
||||
token.address,
|
||||
1,
|
||||
4 * 2 * 10 ** 7,
|
||||
4,
|
||||
4,
|
||||
2,
|
||||
100,
|
||||
2000)
|
||||
policy_manager_v2, _ = chain.provider.deploy_contract('PolicyManager', escrow.address)
|
||||
government_v2, _ = chain.provider.deploy_contract('Government', escrow.address, policy_manager.address, 1)
|
||||
assert FINISHED_STATE == government.functions.getVotingState().call()
|
||||
|
||||
# Alice can't create voting
|
||||
with pytest.raises((TransactionFailed, ValueError)):
|
||||
tx = government.functions.createVoting(UPGRADE_GOVERNMENT, government_v2.address).transact({'from': alice1})
|
||||
chain.wait_for_receipt(tx)
|
||||
with pytest.raises((TransactionFailed, ValueError)):
|
||||
tx = government.functions.createVoting(UPGRADE_GOVERNMENT, government_v2.address).transact({'from': alice2})
|
||||
chain.wait_for_receipt(tx)
|
||||
|
||||
# Vote and upgrade government contract
|
||||
tx = government.functions.createVoting(UPGRADE_GOVERNMENT, government_v2.address).transact({'from': ursula1})
|
||||
chain.wait_for_receipt(tx)
|
||||
assert ACTIVE_STATE == government.functions.getVotingState().call()
|
||||
# Alice can't vote
|
||||
with pytest.raises((TransactionFailed, ValueError)):
|
||||
tx = government.functions.vote(False).transact({'from': alice1})
|
||||
chain.wait_for_receipt(tx)
|
||||
with pytest.raises((TransactionFailed, ValueError)):
|
||||
tx = government.functions.vote(False).transact({'from': alice2})
|
||||
chain.wait_for_receipt(tx)
|
||||
|
||||
tx = government.functions.vote(True).transact({'from': ursula1})
|
||||
chain.wait_for_receipt(tx)
|
||||
tx = government.functions.vote(False).transact({'from': ursula2})
|
||||
chain.wait_for_receipt(tx)
|
||||
tx = user_escrow_1.functions.vote(True).transact({'from': ursula3})
|
||||
chain.wait_for_receipt(tx)
|
||||
|
||||
# Can't vote again
|
||||
with pytest.raises((TransactionFailed, ValueError)):
|
||||
tx = government.functions.vote(False).transact({'from': ursula1})
|
||||
chain.wait_for_receipt(tx)
|
||||
|
||||
chain.time_travel(1)
|
||||
assert UPGRADE_WAITING_STATE == government.functions.getVotingState().call()
|
||||
tx = government.functions.commitUpgrade().transact({'from': ursula1})
|
||||
chain.wait_for_receipt(tx)
|
||||
assert FINISHED_STATE == government.functions.getVotingState().call()
|
||||
assert government_v2.address == government.functions.target().call()
|
||||
|
||||
# Vote and upgrade escrow contract
|
||||
tx = government.functions.createVoting(UPGRADE_ESCROW, escrow_v2.address).transact({'from': ursula2})
|
||||
chain.wait_for_receipt(tx)
|
||||
assert ACTIVE_STATE == government.functions.getVotingState().call()
|
||||
tx = government.functions.vote(False).transact({'from': ursula1})
|
||||
chain.wait_for_receipt(tx)
|
||||
tx = government.functions.vote(True).transact({'from': ursula2})
|
||||
chain.wait_for_receipt(tx)
|
||||
tx = user_escrow_1.functions.vote(True).transact({'from': ursula3})
|
||||
chain.wait_for_receipt(tx)
|
||||
chain.time_travel(1)
|
||||
assert UPGRADE_WAITING_STATE == government.functions.getVotingState().call()
|
||||
tx = government.functions.commitUpgrade().transact({'from': ursula2})
|
||||
chain.wait_for_receipt(tx)
|
||||
assert FINISHED_STATE == government.functions.getVotingState().call()
|
||||
assert escrow_v2.address == escrow.functions.target().call()
|
||||
|
||||
# Vote and upgrade policy manager contract
|
||||
tx = government.functions.createVoting(UPGRADE_POLICY_MANAGER, policy_manager_v2.address).transact({'from': ursula2})
|
||||
chain.wait_for_receipt(tx)
|
||||
assert ACTIVE_STATE == government.functions.getVotingState().call()
|
||||
tx = government.functions.vote(False).transact({'from': ursula1})
|
||||
chain.wait_for_receipt(tx)
|
||||
tx = government.functions.vote(True).transact({'from': ursula2})
|
||||
chain.wait_for_receipt(tx)
|
||||
tx = user_escrow_1.functions.vote(True).transact({'from': ursula3})
|
||||
chain.wait_for_receipt(tx)
|
||||
chain.time_travel(1)
|
||||
assert UPGRADE_WAITING_STATE == government.functions.getVotingState().call()
|
||||
tx = government.functions.commitUpgrade().transact({'from': ursula3})
|
||||
chain.wait_for_receipt(tx)
|
||||
assert FINISHED_STATE == government.functions.getVotingState().call()
|
||||
assert policy_manager_v2.address == policy_manager.functions.target().call()
|
||||
|
||||
# Voting against rollback
|
||||
tx = government.functions.createVoting(ROLLBACK_GOVERNMENT, NULL_ADDR).transact({'from': ursula1})
|
||||
chain.wait_for_receipt(tx)
|
||||
assert ACTIVE_STATE == government.functions.getVotingState().call()
|
||||
tx = government.functions.vote(True).transact({'from': ursula1})
|
||||
chain.wait_for_receipt(tx)
|
||||
tx = user_escrow_1.functions.vote(False).transact({'from': ursula3})
|
||||
chain.wait_for_receipt(tx)
|
||||
chain.time_travel(1)
|
||||
assert FINISHED_STATE == government.functions.getVotingState().call()
|
||||
assert government_v2.address == government.functions.target().call()
|
||||
|
||||
tx = government.functions.createVoting(ROLLBACK_ESCROW, NULL_ADDR).transact({'from': ursula2})
|
||||
chain.wait_for_receipt(tx)
|
||||
assert ACTIVE_STATE == government.functions.getVotingState().call()
|
||||
tx = government.functions.vote(True).transact({'from': ursula2})
|
||||
chain.wait_for_receipt(tx)
|
||||
tx = user_escrow_1.functions.vote(False).transact({'from': ursula3})
|
||||
chain.wait_for_receipt(tx)
|
||||
chain.time_travel(1)
|
||||
assert FINISHED_STATE == government.functions.getVotingState().call()
|
||||
assert escrow_v2.address == escrow.functions.target().call()
|
||||
|
||||
tx = government.functions.createVoting(ROLLBACK_ESCROW, NULL_ADDR).transact({'from': ursula2})
|
||||
chain.wait_for_receipt(tx)
|
||||
assert ACTIVE_STATE == government.functions.getVotingState().call()
|
||||
tx = government.functions.vote(True).transact({'from': ursula1})
|
||||
chain.wait_for_receipt(tx)
|
||||
tx = government.functions.vote(False).transact({'from': ursula2})
|
||||
chain.wait_for_receipt(tx)
|
||||
chain.time_travel(1)
|
||||
assert FINISHED_STATE == government.functions.getVotingState().call()
|
||||
assert policy_manager_v2.address == policy_manager.functions.target().call()
|
||||
|
||||
# Voting for upgrade with errors
|
||||
tx = government.functions.createVoting(UPGRADE_GOVERNMENT, escrow_v2.address).transact({'from': ursula1})
|
||||
chain.wait_for_receipt(tx)
|
||||
tx = government.functions.vote(True).transact({'from': ursula1})
|
||||
chain.wait_for_receipt(tx)
|
||||
chain.time_travel(1)
|
||||
assert UPGRADE_WAITING_STATE == government.functions.getVotingState().call()
|
||||
tx = government.functions.commitUpgrade().transact({'from': ursula1})
|
||||
chain.wait_for_receipt(tx)
|
||||
assert FINISHED_STATE == government.functions.getVotingState().call()
|
||||
assert government_v2.address == government.functions.target().call()
|
||||
|
||||
# Some activity
|
||||
for index in range(5):
|
||||
tx = escrow.functions.confirmActivity().transact({'from': ursula1})
|
||||
chain.wait_for_receipt(tx)
|
||||
tx = escrow.functions.confirmActivity().transact({'from': ursula2})
|
||||
chain.wait_for_receipt(tx)
|
||||
tx = user_escrow_1.functions.confirmActivity().transact({'from': ursula3})
|
||||
chain.wait_for_receipt(tx)
|
||||
chain.time_travel(hours=1)
|
||||
|
||||
# Vote and rollback all contracts
|
||||
tx = government.functions.createVoting(ROLLBACK_GOVERNMENT, NULL_ADDR).transact({'from': ursula1})
|
||||
chain.wait_for_receipt(tx)
|
||||
tx = government.functions.vote(True).transact({'from': ursula1})
|
||||
chain.wait_for_receipt(tx)
|
||||
chain.time_travel(hours=1)
|
||||
tx = government.functions.commitUpgrade().transact({'from': ursula1})
|
||||
chain.wait_for_receipt(tx)
|
||||
assert government_v1 == government.functions.target().call()
|
||||
|
||||
tx = government.functions.createVoting(ROLLBACK_ESCROW, NULL_ADDR).transact({'from': ursula1})
|
||||
chain.wait_for_receipt(tx)
|
||||
tx = government.functions.vote(True).transact({'from': ursula1})
|
||||
chain.wait_for_receipt(tx)
|
||||
chain.time_travel(hours=1)
|
||||
tx = government.functions.commitUpgrade().transact({'from': ursula1})
|
||||
chain.wait_for_receipt(tx)
|
||||
assert escrow_v1 == escrow.functions.target().call()
|
||||
|
||||
tx = government.functions.createVoting(ROLLBACK_POLICY_MANAGER, NULL_ADDR).transact({'from': ursula1})
|
||||
chain.wait_for_receipt(tx)
|
||||
tx = government.functions.vote(True).transact({'from': ursula1})
|
||||
chain.wait_for_receipt(tx)
|
||||
chain.time_travel(hours=1)
|
||||
tx = government.functions.commitUpgrade().transact({'from': ursula1})
|
||||
chain.wait_for_receipt(tx)
|
||||
assert policy_manager_v1 == policy_manager.functions.target().call()
|
||||
|
||||
# Unlock and withdraw all tokens in MinersEscrow
|
||||
for index in range(11):
|
||||
for index in range(6):
|
||||
tx = escrow.functions.confirmActivity().transact({'from': ursula1})
|
||||
chain.wait_for_receipt(tx)
|
||||
tx = escrow.functions.confirmActivity().transact({'from': ursula2})
|
||||
|
|
|
@ -4,8 +4,7 @@ from eth_tester.exceptions import TransactionFailed
|
|||
|
||||
|
||||
@pytest.fixture()
|
||||
def token(web3, chain):
|
||||
creator = web3.eth.accounts[0]
|
||||
def token(chain):
|
||||
# Create an ERC20 token
|
||||
token, _ = chain.provider.deploy_contract('NuCypherToken', 2 * 10 ** 40)
|
||||
return token
|
||||
|
@ -24,7 +23,7 @@ def test_issuer(web3, chain, token):
|
|||
|
||||
# Give Miner tokens for reward and initialize contract
|
||||
reserved_reward = 2 * 10 ** 40 - 10 ** 30
|
||||
tx = token.functions.transfer(issuer.address, reserved_reward).transact({'from': creator})
|
||||
tx = token.functions.transfer(issuer.address, reserved_reward).transact({'from': creator})
|
||||
chain.wait_for_receipt(tx)
|
||||
|
||||
tx = issuer.functions.initialize().transact({'from': creator})
|
||||
|
@ -41,23 +40,23 @@ def test_issuer(web3, chain, token):
|
|||
chain.wait_for_receipt(tx)
|
||||
|
||||
# Mint some tokens
|
||||
tx = issuer.functions.testMint(0, 1000, 2000, 0, 0).transact({'from': ursula})
|
||||
tx = issuer.functions.testMint(0, 1000, 2000, 0, 0).transact({'from': ursula})
|
||||
chain.wait_for_receipt(tx)
|
||||
assert 10 == token.functions.balanceOf(ursula).call()
|
||||
assert balance - 10 == token.functions.balanceOf(issuer.address).call()
|
||||
|
||||
# Mint more tokens
|
||||
tx = issuer.functions.testMint(0, 500, 500, 0, 0).transact({'from': ursula})
|
||||
tx = issuer.functions.testMint(0, 500, 500, 0, 0).transact({'from': ursula})
|
||||
chain.wait_for_receipt(tx)
|
||||
assert 30 == token.functions.balanceOf(ursula).call()
|
||||
assert balance - 30 == token.functions.balanceOf(issuer.address).call()
|
||||
|
||||
tx = issuer.functions.testMint(0, 500, 500, 10 ** 7, 0).transact({'from': ursula})
|
||||
tx = issuer.functions.testMint(0, 500, 500, 10 ** 7, 0).transact({'from': ursula})
|
||||
chain.wait_for_receipt(tx)
|
||||
assert 70 == token.functions.balanceOf(ursula).call()
|
||||
assert balance - 70 == token.functions.balanceOf(issuer.address).call()
|
||||
|
||||
tx = issuer.functions.testMint(0, 500, 500, 2 * 10 ** 7, 0).transact({'from': ursula})
|
||||
tx = issuer.functions.testMint(0, 500, 500, 2 * 10 ** 7, 0).transact({'from': ursula})
|
||||
chain.wait_for_receipt(tx)
|
||||
assert 110 == token.functions.balanceOf(ursula).call()
|
||||
assert balance - 110 == token.functions.balanceOf(issuer.address).call()
|
||||
|
@ -74,35 +73,35 @@ def test_inflation_rate(web3, chain, token):
|
|||
)
|
||||
|
||||
# Give Miner tokens for reward and initialize contract
|
||||
tx = token.functions.transfer(issuer.address, 2 * 10 ** 40 - 10 ** 30).transact({'from': creator})
|
||||
tx = token.functions.transfer(issuer.address, 2 * 10 ** 40 - 10 ** 30).transact({'from': creator})
|
||||
chain.wait_for_receipt(tx)
|
||||
tx = issuer.functions.initialize().transact({'from': creator})
|
||||
chain.wait_for_receipt(tx)
|
||||
|
||||
# Mint some tokens
|
||||
period = issuer.functions.getCurrentPeriod().call()
|
||||
tx = issuer.functions.testMint(period + 1, 1, 1, 0, 0).transact({'from': ursula})
|
||||
tx = issuer.functions.testMint(period + 1, 1, 1, 0, 0).transact({'from': ursula})
|
||||
chain.wait_for_receipt(tx)
|
||||
one_period = token.functions.balanceOf(ursula).call()
|
||||
|
||||
# Mint more tokens in the same period
|
||||
tx = issuer.functions.testMint(period + 1, 1, 1, 0, 0).transact({'from': ursula})
|
||||
tx = issuer.functions.testMint(period + 1, 1, 1, 0, 0).transact({'from': ursula})
|
||||
chain.wait_for_receipt(tx)
|
||||
assert 2 * one_period == token.functions.balanceOf(ursula).call()
|
||||
|
||||
# Mint tokens in the next period
|
||||
tx = issuer.functions.testMint(period + 2, 1, 1, 0, 0).transact({'from': ursula})
|
||||
tx = issuer.functions.testMint(period + 2, 1, 1, 0, 0).transact({'from': ursula})
|
||||
chain.wait_for_receipt(tx)
|
||||
assert 3 * one_period > token.functions.balanceOf(ursula).call()
|
||||
minted_amount = token.functions.balanceOf(ursula).call() - 2 * one_period
|
||||
|
||||
# Mint tokens in the next period
|
||||
tx = issuer.functions.testMint(period + 1, 1, 1, 0, 0).transact({'from': ursula})
|
||||
tx = issuer.functions.testMint(period + 1, 1, 1, 0, 0).transact({'from': ursula})
|
||||
chain.wait_for_receipt(tx)
|
||||
assert 2 * one_period + 2 * minted_amount == token.functions.balanceOf(ursula).call()
|
||||
|
||||
# Mint tokens in the next period
|
||||
tx = issuer.functions.testMint(period + 3, 1, 1, 0, 0).transact({'from': ursula})
|
||||
tx = issuer.functions.testMint(period + 3, 1, 1, 0, 0).transact({'from': ursula})
|
||||
chain.wait_for_receipt(tx)
|
||||
assert 2 * one_period + 3 * minted_amount > token.functions.balanceOf(ursula).call()
|
||||
|
||||
|
@ -125,31 +124,34 @@ def test_verifying_state(web3, chain, token):
|
|||
ContractFactoryClass=Contract)
|
||||
|
||||
# Give Miner tokens for reward and initialize contract
|
||||
tx = token.functions.transfer(contract.address, 10000).transact({'from': creator})
|
||||
tx = token.functions.transfer(contract.address, 10000).transact({'from': creator})
|
||||
chain.wait_for_receipt(tx)
|
||||
tx = contract.functions.initialize().transact({'from': creator})
|
||||
chain.wait_for_receipt(tx)
|
||||
|
||||
# Upgrade to the second version
|
||||
period = contract.functions.lastMintedPeriod().call()
|
||||
assert 1 == contract.functions.miningCoefficient().call()
|
||||
tx = dispatcher.functions.upgrade(contract_library_v2.address).transact({'from': creator})
|
||||
tx = dispatcher.functions.upgrade(contract_library_v2.address).transact({'from': creator})
|
||||
chain.wait_for_receipt(tx)
|
||||
assert contract_library_v2.address == dispatcher.functions.target().call()
|
||||
assert 2 == contract.functions.miningCoefficient().call()
|
||||
assert 2 * 3600 == contract.functions.secondsPerPeriod().call()
|
||||
assert 2 == contract.functions.lockedPeriodsCoefficient().call()
|
||||
assert 2 == contract.functions.awardedPeriods().call()
|
||||
tx = contract.functions.setValueToCheck(3).transact({'from': creator})
|
||||
assert period == contract.functions.lastMintedPeriod().call()
|
||||
assert 2 * 10 ** 40 == contract.functions.futureSupply().call()
|
||||
tx = contract.functions.setValueToCheck(3).transact({'from': creator})
|
||||
chain.wait_for_receipt(tx)
|
||||
assert 3 == contract.functions.valueToCheck().call()
|
||||
|
||||
# Can't upgrade to the previous version or to the bad version
|
||||
contract_library_bad, _ = chain.provider.deploy_contract('IssuerBad', token.address, 2, 2, 2, 2)
|
||||
with pytest.raises((TransactionFailed, ValueError)):
|
||||
tx = dispatcher.functions.upgrade(contract_library_v1.address).transact({'from': creator})
|
||||
tx = dispatcher.functions.upgrade(contract_library_v1.address).transact({'from': creator})
|
||||
chain.wait_for_receipt(tx)
|
||||
with pytest.raises((TransactionFailed, ValueError)):
|
||||
tx = dispatcher.functions.upgrade(contract_library_bad.address).transact({'from': creator})
|
||||
tx = dispatcher.functions.upgrade(contract_library_bad.address).transact({'from': creator})
|
||||
chain.wait_for_receipt(tx)
|
||||
|
||||
# But can rollback
|
||||
|
@ -160,11 +162,13 @@ def test_verifying_state(web3, chain, token):
|
|||
assert 3600 == contract.functions.secondsPerPeriod().call()
|
||||
assert 1 == contract.functions.lockedPeriodsCoefficient().call()
|
||||
assert 1 == contract.functions.awardedPeriods().call()
|
||||
assert period == contract.functions.lastMintedPeriod().call()
|
||||
assert 2 * 10 ** 40 == contract.functions.futureSupply().call()
|
||||
with pytest.raises((TransactionFailed, ValueError)):
|
||||
tx = contract.functions.setValueToCheck(2).transact({'from': creator})
|
||||
tx = contract.functions.setValueToCheck(2).transact({'from': creator})
|
||||
chain.wait_for_receipt(tx)
|
||||
|
||||
# Try to upgrade to the bad version
|
||||
with pytest.raises((TransactionFailed, ValueError)):
|
||||
tx = dispatcher.functions.upgrade(contract_library_bad.address).transact({'from': creator})
|
||||
tx = dispatcher.functions.upgrade(contract_library_bad.address).transact({'from': creator})
|
||||
chain.wait_for_receipt(tx)
|
||||
|
|
|
@ -865,6 +865,7 @@ def test_verifying_state(web3, chain, token):
|
|||
chain.wait_for_receipt(tx)
|
||||
assert contract_library_v2.address == dispatcher.functions.target().call()
|
||||
assert 1500 == contract.functions.maxAllowableLockedTokens().call()
|
||||
assert policy_manager.address == contract.functions.policyManager().call()
|
||||
assert 2 == contract.functions.valueToCheck().call()
|
||||
tx = contract.functions.setValueToCheck(3).transact({'from': creator})
|
||||
chain.wait_for_receipt(tx)
|
||||
|
@ -884,9 +885,10 @@ def test_verifying_state(web3, chain, token):
|
|||
|
||||
# But can rollback
|
||||
tx = dispatcher.functions.rollback().transact({'from': creator})
|
||||
|
||||
chain.wait_for_receipt(tx)
|
||||
assert contract_library_v1.address == dispatcher.functions.target().call()
|
||||
assert policy_manager.address == contract.functions.policyManager().call()
|
||||
|
||||
with pytest.raises((TransactionFailed, ValueError)):
|
||||
tx = contract.functions.setValueToCheck(2).transact({'from': creator})
|
||||
chain.wait_for_receipt(tx)
|
||||
|
|
|
@ -3,7 +3,7 @@ from eth_tester.exceptions import TransactionFailed
|
|||
|
||||
|
||||
@pytest.fixture()
|
||||
def token(web3, chain):
|
||||
def token(chain):
|
||||
# Create an ERC20 token
|
||||
token, _ = chain.provider.deploy_contract('NuCypherToken', int(2e9))
|
||||
return token
|
||||
|
@ -16,7 +16,7 @@ def escrow(web3, chain, token):
|
|||
contract, _ = chain.provider.deploy_contract('MinersEscrowForUserEscrowMock', token.address)
|
||||
|
||||
# Give escrow some coins
|
||||
tx = token.functions.transfer(contract.address, 10000).transact({'from': creator})
|
||||
tx = token.functions.transfer(contract.address, 10000).transact({'from': creator})
|
||||
chain.wait_for_receipt(tx)
|
||||
|
||||
return contract
|
||||
|
@ -29,12 +29,19 @@ def policy_manager(chain):
|
|||
|
||||
|
||||
@pytest.fixture()
|
||||
def user_escrow(web3, chain, token, escrow, policy_manager):
|
||||
def government(chain):
|
||||
contract, _ = chain.provider.deploy_contract('GovernmentForUserEscrowMock')
|
||||
return contract
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def user_escrow(web3, chain, token, escrow, policy_manager, government):
|
||||
creator = web3.eth.accounts[0]
|
||||
user = web3.eth.accounts[1]
|
||||
|
||||
# Creator deploys the user escrow
|
||||
contract, _ = chain.provider.deploy_contract('UserEscrow', token.address, escrow.address, policy_manager.address)
|
||||
contract, _ = chain.provider.deploy_contract(
|
||||
'UserEscrow', token.address, escrow.address, policy_manager.address, government.address)
|
||||
|
||||
# Transfer ownership
|
||||
tx = contract.functions.transferOwnership(user).transact({'from': creator})
|
||||
|
@ -360,3 +367,34 @@ def test_policy(web3, chain, policy_manager, user_escrow):
|
|||
event_args = events[0]['args']
|
||||
assert user == event_args['owner']
|
||||
assert 222 == event_args['value']
|
||||
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_government(web3, chain, government, user_escrow):
|
||||
creator = web3.eth.accounts[0]
|
||||
user = web3.eth.accounts[1]
|
||||
votes = user_escrow.events.Voted.createFilter(fromBlock='latest')
|
||||
|
||||
# Only user can vote
|
||||
with pytest.raises((TransactionFailed, ValueError)):
|
||||
tx = user_escrow.functions.vote(True).transact({'from': creator})
|
||||
chain.wait_for_receipt(tx)
|
||||
with pytest.raises((TransactionFailed, ValueError)):
|
||||
tx = user_escrow.functions.vote(False).transact({'from': creator})
|
||||
chain.wait_for_receipt(tx)
|
||||
|
||||
tx = user_escrow.functions.vote(True).transact({'from': user})
|
||||
chain.wait_for_receipt(tx)
|
||||
assert government.functions.voteFor().call()
|
||||
tx = user_escrow.functions.vote(False).transact({'from': user})
|
||||
chain.wait_for_receipt(tx)
|
||||
assert not government.functions.voteFor().call()
|
||||
|
||||
events = votes.get_all_entries()
|
||||
assert 2 == len(events)
|
||||
event_args = events[0]['args']
|
||||
assert user == event_args['owner']
|
||||
assert event_args['voteFor']
|
||||
event_args = events[1]['args']
|
||||
assert user == event_args['owner']
|
||||
assert not event_args['voteFor']
|
||||
|
|
Loading…
Reference in New Issue