[KMS-ETH]- Blockchain remains open; deprecates context manager usage for blockchain instances.

pull/195/head^2
Kieran R Prasch 2018-02-06 23:13:29 -08:00 committed by Kieran Prasch
parent 32660a6108
commit b2ce78ecf4
10 changed files with 606 additions and 635 deletions

View File

@ -18,25 +18,25 @@ class Blockchain:
"""
network = 'mainnetrpc'
project_name = 'nucypher-kms'
python_project_name = 'nucypher-kms'
_project = threading.local()
registrar_path = join(appdirs.user_data_dir(project_name), 'registrar.json') # Persistent; In user's .local dir
registrar_path = join(appdirs.user_data_dir(python_project_name), 'registrar.json') # Persistent; In user's .local dir
class NotEnoughUrsulas(Exception):
pass
def __init__(self, project_name='nucypher-kms', timeout=60):
self.project_name = project_name
self.timeout = timeout
# Populus project config
project_dir = join(dirname(abspath(nkms_eth.__file__)), 'project')
project = populus.Project(project_dir)
project.config['chains.mainnetrpc.contracts.backends.JSONFile.settings.file_path'] = self.registrar_path
self.project_name = project_name
self.timeout = timeout
self.project_dir = project_dir
self._project.project = project
self._project.chain = project.get_chain(self.network)
self._project.chain = self._project.project.get_chain(self.network).__enter__()
def __repr__(self):
class_name = self.__class__.__name__
@ -44,27 +44,27 @@ class Blockchain:
return r.format(class_name, self.network, self.project_name, self.timeout)
def __str__(self):
return f"{self.__class__.__name__} {self.network}:{self.project_name}"
def __enter__(self):
return self._project.chain.__enter__()
def __exit__(self, exc_type, exc_val, exc_tb):
return self._project.chain.__exit__(None, None, None)
class_name = self.__class__.__name__
return "{} {}:{}".format(class_name, self.network, self.project_name)
def __del__(self):
self._project.chain.__exit__(None, None, None)
for attr in ('project', 'chain', 'w3'):
if hasattr(self._project, attr):
delattr(self._project, attr)
@property
def chain(self):
return self._project.chain
@property
def web3(self):
return self._project.chain.web3
def get_contract(self, name):
""" Gets an existing contract or returns an error """
with self as chain:
return chain.provider.get_contract(name)
return self._project.chain.provider.get_contract(name)
class TesterBlockchain(Blockchain):
network = 'tester'
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

View File

@ -1,7 +1,9 @@
"""Deploy contracts in tester.
"""
Deploy contracts in tester.
A simple Python script to deploy contracts and then estimate gas for different methods.
"""
from nkms_eth.blockchain import project

View File

@ -1,84 +1,33 @@
#!/usr/bin/env python3
"""
Deploy contracts in tester.
A simple Python script to deploy contracts and then estimate gas for different methods.
"""
import random
from nkms_eth.blockchain import Blockchain
TIMEOUT = 10
MINING_COEFF = [10 ** 5, 10 ** 7]
M = int(1e6)
NULL_ADDR = '0x' + '0' * 40
from nkms_eth.blockchain import TesterBlockchain
from nkms_eth.escrow import Escrow
from nkms_eth.token import NuCypherKMSToken
from nkms_eth.miner import Miner
def main():
proj = project()
blockchain = TesterBlockchain()
with blockchain as chain:
print("Web3 providers -> ", chain.web3.providers)
creator, *addresses = chain.web3.eth.accounts
# ursula, alice, *everyone_else = addresses
chain_name = "tester"
print("Make sure {} chain is running, you can connect to it, or you'll get timeout".format(chain_name))
# Create NC ERC20 token
token = NuCypherKMSToken(blockchain=blockchain)
with proj.get_chain(chain_name) as chain:
web3 = chain.web3
print("Web3 providers are", web3.providers)
creator = web3.eth.accounts[0]
# ursula = web3.eth.accounts[1]
# alice = web3.eth.accounts[2]
# Prepare escrow and miner
escrow = Escrow(blockchain=blockchain, token=token)
miner = Miner(blockchain=blockchain, token=token, escrow=escrow)
# Create an ERC20 token
token, tx = chain.provider.get_or_deploy_contract(
'HumanStandardToken', deploy_args=[
int(1e9) * M, int(1e10) * M, 'NuCypher KMS', 6, 'KMS'],deploy_transaction={'from': creator})
chain.wait.for_receipt(tx, timeout=TIMEOUT)
print("Deployed HumanStandardToken, tx hash is", tx)
# Airdropping
token.airdrop()
escrow, tx = chain.provider.get_or_deploy_contract(
'Escrow', deploy_args=[token.address] + MINING_COEFF,
deploy_transaction={'from': creator})
chain.wait.for_receipt(tx, timeout=TIMEOUT)
print("Deployed escrow, tx hash is", tx)
# Locking
for address in addresses:
miner.lock(address=address, amount=1000*NuCypherKMSToken.M, locktime=100)
# debug airdrop
txs = [
token.transact({'from': creator}).transfer(account, 10000 * M)
for account in web3.eth.accounts[1:]]
for tx in txs:
chain.wait.for_receipt(tx, timeout=TIMEOUT)
print('Airdrop done')
# Test locking
for addr in web3.eth.accounts[1:]:
tx = token.transact({'from': addr}).approve(
escrow.address, 1000 * M)
chain.wait.for_receipt(tx, timeout=TIMEOUT)
tx = escrow.transact({'from': addr}).deposit(1000 * M, 100)
chain.wait.for_receipt(tx, timeout=TIMEOUT)
n_tokens = escrow.call().getAllLockedTokens()
print('Locked', n_tokens)
print(web3.eth.accounts[1])
print(web3.eth.accounts[-1])
address_stop, shift = escrow.call().findCumSum(NULL_ADDR, n_tokens // 3)
print(address_stop, shift)
# Experimenting with distributions of random points
n_ursulas = 5
n_select = int(n_ursulas * 1.7) # Select more ursulas
points = [0] + sorted(random.randrange(n_tokens) for _ in
range(n_select))
deltas = [i - j for i, j in zip(points[1:], points[:-1])]
addrs = set()
addr = NULL_ADDR
shift = 0
for delta in deltas:
addr, shift = escrow.call().findCumSum(addr, delta + shift)
addrs.add(addr)
addrs = random.sample(addrs, n_ursulas)
print(addrs)
# Select random miners
miners = escrow.sample()
print(miners)
if __name__ == "__main__":

View File

@ -3,435 +3,438 @@ from ethereum.tester import TransactionFailed
# TODO extract method
def wait_time(testerchain, wait_hours):
with testerchain as chain:
web3 = chain.web3
step = 50
end_timestamp = web3.eth.getBlock(web3.eth.blockNumber).timestamp + wait_hours * 60 * 60
while web3.eth.getBlock(web3.eth.blockNumber).timestamp < end_timestamp:
chain.wait.for_block(web3.eth.blockNumber + step)
def wait_time(testerchain, wait_hours, step=50):
end_timestamp = testerchain.web3.eth.getBlock(testerchain.web3.eth.blockNumber).timestamp + wait_hours * 60 * 60
while testerchain.web3.eth.getBlock(testerchain.web3.eth.blockNumber).timestamp<end_timestamp:
testerchain.chain.wait.for_block(testerchain.web3.eth.blockNumber+step)
def test_escrow(web3, testerchain, token, escrow):
with testerchain as chain:
creator = web3.eth.accounts[0]
ursula = web3.eth.accounts[1]
alice = web3.eth.accounts[2]
def test_escrow(testerchain, token, escrow):
web3 = testerchain.web3
chain = testerchain.chain
# Give Ursula and Alice some coins
tx = token.transact({'from': creator}).transfer(ursula, 10000)
chain.wait.for_receipt(tx)
tx = token.transact({'from': creator}).transfer(alice, 10000)
chain.wait.for_receipt(tx)
assert 10000 == token.call().balanceOf(ursula)
assert 10000 == token.call().balanceOf(alice)
creator = web3.eth.accounts[0]
ursula = web3.eth.accounts[1]
alice = web3.eth.accounts[2]
# Ursula and Alice give Escrow rights to transfer
tx = token.transact({'from': ursula}).approve(escrow.address, 2500)
chain.wait.for_receipt(tx)
assert 2500 == token.call().allowance(ursula, escrow.address)
tx = token.transact({'from': alice}).approve(escrow.address, 1100)
chain.wait.for_receipt(tx)
assert 1100 == token.call().allowance(alice, escrow.address)
# Give Ursula and Alice some coins
tx = token.transact({'from': creator}).transfer(ursula, 10000)
testerchain.chain.wait.for_receipt(tx)
tx = token.transact({'from': creator}).transfer(alice, 10000)
testerchain.chain.wait.for_receipt(tx)
assert 10000 == token().balanceOf(ursula)
assert 10000 == token().balanceOf(alice)
# Ursula's withdrawal attempt won't succeed because nothing to withdraw
with pytest.raises(TransactionFailed):
tx = escrow.transact({'from': ursula}).withdraw(100)
chain.wait.for_receipt(tx)
# Ursula and Alice give Escrow rights to transfer
tx = token.transact({'from': ursula}).approve(escrow.contract.address, 2500)
testerchain.chain.wait.for_receipt(tx)
assert 2500 == token().allowance(ursula, escrow.contract.address)
tx = token.transact({'from': alice}).approve(escrow.contract.address, 1100)
testerchain.chain.wait.for_receipt(tx)
assert 1100 == token().allowance(alice, escrow.contract.address)
# And can't lock because nothing to lock
with pytest.raises(TransactionFailed):
tx = escrow.transact({'from': ursula}).lock(500, 2)
chain.wait.for_receipt(tx)
# Ursula's withdrawal attempt won't succeed because nothing to withdraw
with pytest.raises(TransactionFailed):
tx = escrow.transact({'from': ursula}).withdraw(100)
testerchain.chain.wait.for_receipt(tx)
# Check that nothing is locked
assert 0 == escrow.call().getLockedTokens(ursula)
assert 0 == escrow.call().getLockedTokens(alice)
# And can't lock because nothing to lock
with pytest.raises(TransactionFailed):
tx = escrow.transact({'from': ursula}).lock(500, 2)
testerchain.chain.wait.for_receipt(tx)
# Ursula can't lock too low value
# TODO uncomment after completing logic
# with pytest.raises(TransactionFailed):
# tx = escrow.transact({'from': ursula}).deposit(1000, 10)
# chain.wait.for_receipt(tx)
# Check that nothing is locked
assert 0 == escrow().getLockedTokens(ursula)
assert 0 == escrow().getLockedTokens(alice)
# Ursula and Alice transfer some tokens to the escrow and lock them
tx = escrow.transact({'from': ursula}).deposit(1000, 1)
chain.wait.for_receipt(tx)
assert 1000 == token.call().balanceOf(escrow.address)
assert 9000 == token.call().balanceOf(ursula)
assert 1000 == escrow.call().getLockedTokens(ursula)
assert 1000 == escrow.call().calculateLockedTokens(ursula, 1)
assert 1000 == escrow.call().calculateLockedTokens(ursula, 2)
tx = escrow.transact({'from': ursula}).switchLock()
chain.wait.for_receipt(tx)
assert 500 == escrow.call().calculateLockedTokens(ursula, 2)
tx = escrow.transact({'from': ursula}).switchLock()
chain.wait.for_receipt(tx)
assert 1000 == escrow.call().calculateLockedTokens(ursula, 2)
tx = escrow.transact({'from': alice}).deposit(500, 2)
chain.wait.for_receipt(tx)
assert 1500 == token.call().balanceOf(escrow.address)
assert 9500 == token.call().balanceOf(alice)
assert 500 == escrow.call().getLockedTokens(alice)
assert 500 == escrow.call().calculateLockedTokens(alice, 1)
# Ursula can't lock too low value
# TODO uncomment after completing logic
# with pytest.raises(TransactionFailed):
# tx = escrow.transact({'from': ursula}).deposit(1000, 10)
# chain.wait.for_receipt(tx)
# Checks locked tokens in next period
wait_time(chain, 1)
assert 1000 == escrow.call().getLockedTokens(ursula)
assert 500 == escrow.call().getLockedTokens(alice)
assert 1500 == escrow.call().getAllLockedTokens()
# Ursula and Alice transfer some tokens to the escrow and lock them
tx = escrow.transact({'from': ursula}).deposit(1000, 1)
chain.wait.for_receipt(tx)
assert 1000 == token().balanceOf(escrow.contract.address)
assert 9000 == token().balanceOf(ursula)
assert 1000 == escrow().getLockedTokens(ursula)
assert 1000 == escrow().calculateLockedTokens(ursula, 1)
assert 1000 == escrow().calculateLockedTokens(ursula, 2)
tx = escrow.transact({'from': ursula}).switchLock()
chain.wait.for_receipt(tx)
assert 500 == escrow().calculateLockedTokens(ursula, 2)
tx = escrow.transact({'from': ursula}).switchLock()
chain.wait.for_receipt(tx)
assert 1000 == escrow().calculateLockedTokens(ursula, 2)
tx = escrow.transact({'from': alice}).deposit(500, 2)
chain.wait.for_receipt(tx)
assert 1500 == token().balanceOf(escrow.contract.address)
assert 9500 == token().balanceOf(alice)
assert 500 == escrow().getLockedTokens(alice)
assert 500 == escrow().calculateLockedTokens(alice, 1)
# Ursula's withdrawal attempt won't succeed
with pytest.raises(TransactionFailed):
tx = escrow.transact({'from': ursula}).withdraw(100)
chain.wait.for_receipt(tx)
assert 1500 == token.call().balanceOf(escrow.address)
assert 9000 == token.call().balanceOf(ursula)
# Checks locked tokens in next period
wait_time(chain, 1)
assert 1000 == escrow().getLockedTokens(ursula)
assert 500 == escrow().getLockedTokens(alice)
assert 1500 == escrow().getAllLockedTokens()
# Ursula can deposit more tokens
tx = escrow.transact({'from': ursula}).confirmActivity()
chain.wait.for_receipt(tx)
tx = escrow.transact({'from': ursula}).deposit(500, 0)
chain.wait.for_receipt(tx)
assert 2000 == token.call().balanceOf(escrow.address)
assert 8500 == token.call().balanceOf(ursula)
# Ursula starts unlocking
tx = escrow.transact({'from': ursula}).switchLock()
chain.wait.for_receipt(tx)
assert 750 == escrow.call().calculateLockedTokens(ursula, 2)
# Wait 1 period and checks locking
wait_time(chain, 1)
assert 1500 == escrow.call().getLockedTokens(ursula)
# Confirm activity and wait 1 period
tx = escrow.transact({'from': ursula}).confirmActivity()
chain.wait.for_receipt(tx)
wait_time(chain, 1)
assert 750 == escrow.call().getLockedTokens(ursula)
assert 0 == escrow.call().calculateLockedTokens(ursula, 1)
# And Ursula can withdraw some tokens
# Ursula's withdrawal attempt won't succeed
with pytest.raises(TransactionFailed):
tx = escrow.transact({'from': ursula}).withdraw(100)
chain.wait.for_receipt(tx)
assert 1900 == token.call().balanceOf(escrow.address)
assert 8600 == token.call().balanceOf(ursula)
assert 1500 == token().balanceOf(escrow.contract.address)
assert 9000 == token().balanceOf(ursula)
# But Ursula can't withdraw all without mining for locked value
with pytest.raises(TransactionFailed):
tx = escrow.transact({'from': ursula}).withdrawAll()
chain.wait.for_receipt(tx)
# Ursula can deposit more tokens
tx = escrow.transact({'from': ursula}).confirmActivity()
chain.wait.for_receipt(tx)
tx = escrow.transact({'from': ursula}).deposit(500, 0)
chain.wait.for_receipt(tx)
assert 2000 == token().balanceOf(escrow.contract.address)
assert 8500 == token().balanceOf(ursula)
# Ursula can deposit and lock more tokens
tx = escrow.transact({'from': ursula}).deposit(500, 0)
chain.wait.for_receipt(tx)
tx = escrow.transact({'from': ursula}).lock(100, 0)
# Ursula starts unlocking
tx = escrow.transact({'from': ursula}).switchLock()
chain.wait.for_receipt(tx)
assert 750 == escrow().calculateLockedTokens(ursula, 2)
# Wait 1 period and checks locking
wait_time(chain, 1)
assert 1500 == escrow().getLockedTokens(ursula)
# Confirm activity and wait 1 period
tx = escrow.transact({'from': ursula}).confirmActivity()
chain.wait.for_receipt(tx)
wait_time(chain, 1)
assert 750 == escrow().getLockedTokens(ursula)
assert 0 == escrow().calculateLockedTokens(ursula, 1)
# And Ursula can withdraw some tokens
tx = escrow.transact({'from': ursula}).withdraw(100)
chain.wait.for_receipt(tx)
assert 1900 == token().balanceOf(escrow.contract.address)
assert 8600 == token().balanceOf(ursula)
# But Ursula can't withdraw all without mining for locked value
with pytest.raises(TransactionFailed):
tx = escrow.transact({'from': ursula}).withdrawAll()
chain.wait.for_receipt(tx)
# Locked tokens will be updated in next period
# Release rate will be updated too because of end of previous locking
assert 750 == escrow.call().getLockedTokens(ursula)
assert 600 == escrow.call().calculateLockedTokens(ursula, 1)
assert 600 == escrow.call().calculateLockedTokens(ursula, 2)
tx = escrow.transact({'from': ursula}).switchLock()
# Ursula can deposit and lock more tokens
tx = escrow.transact({'from': ursula}).deposit(500, 0)
chain.wait.for_receipt(tx)
tx = escrow.transact({'from': ursula}).lock(100, 0)
chain.wait.for_receipt(tx)
# Locked tokens will be updated in next period
# Release rate will be updated too because of end of previous locking
assert 750 == escrow().getLockedTokens(ursula)
assert 600 == escrow().calculateLockedTokens(ursula, 1)
assert 600 == escrow().calculateLockedTokens(ursula, 2)
tx = escrow.transact({'from': ursula}).switchLock()
chain.wait.for_receipt(tx)
assert 300 == escrow().calculateLockedTokens(ursula, 2)
assert 0 == escrow().calculateLockedTokens(ursula, 3)
wait_time(chain, 1)
assert 600 == escrow().getLockedTokens(ursula)
assert 300 == escrow().calculateLockedTokens(ursula, 1)
assert 0 == escrow().calculateLockedTokens(ursula, 2)
# Ursula can increase lock
tx = escrow.transact({'from': ursula}).lock(500, 2)
chain.wait.for_receipt(tx)
assert 600 == escrow().getLockedTokens(ursula)
assert 800 == escrow().calculateLockedTokens(ursula, 1)
assert 500 == escrow().calculateLockedTokens(ursula, 2)
assert 200 == escrow().calculateLockedTokens(ursula, 3)
assert 0 == escrow().calculateLockedTokens(ursula, 4)
wait_time(chain, 1)
assert 800 == escrow().getLockedTokens(ursula)
# Alice can't deposit too low value (less then rate)
# TODO uncomment after completing logic
# with pytest.raises(TransactionFailed):
# tx = escrow.transact({'from': ursula}).deposit(100, 100)
# chain.wait.for_receipt(tx)
# Alice starts unlocking and increases lock by deposit more tokens
tx = escrow.transact({'from': alice}).deposit(500, 0)
chain.wait.for_receipt(tx)
tx = escrow.transact({'from': alice}).switchLock()
chain.wait.for_receipt(tx)
assert 500 == escrow().getLockedTokens(alice)
assert 1000 == escrow().calculateLockedTokens(alice, 1)
assert 500 == escrow().calculateLockedTokens(alice, 2)
assert 0 == escrow().calculateLockedTokens(alice, 3)
wait_time(chain, 1)
assert 1000 == escrow().getLockedTokens(alice)
# And increases locked time
tx = escrow.transact({'from': alice}).lock(0, 2)
chain.wait.for_receipt(tx)
assert 1000 == escrow().getLockedTokens(alice)
assert 500 == escrow().calculateLockedTokens(alice, 1)
assert 0 == escrow().calculateLockedTokens(alice, 2)
# Alice increases lock by small amount of tokens
tx = escrow.transact({'from': alice}).deposit(100, 0)
chain.wait.for_receipt(tx)
assert 600 == escrow().calculateLockedTokens(alice, 1)
assert 100 == escrow().calculateLockedTokens(alice, 2)
assert 0 == escrow().calculateLockedTokens(alice, 3)
# # Ursula can't destroy contract
# with pytest.raises(TransactionFailed):
# tx = escrow.transact({'from': ursula}).destroy()
# chain.wait.for_receipt(tx)
#
# # Destroy contract from creator and refund all to Ursula and Alice
# tx = escrow.transact({'from': creator}).destroy()
# chain.wait.for_receipt(tx)
# assert 0 == token().balanceOf(escrow.contract.address)
# assert 10000 == token().balanceOf(ursula)
# assert 10000 == token().balanceOf(alice)
def test_locked_distribution(testerchain, token, escrow):
chain = testerchain.chain
web3 = testerchain.web3
NULL_ADDR = '0x' + '0' * 40
creator = web3.eth.accounts[0]
miners = web3.eth.accounts[1:]
amount = token().balanceOf(creator) // 2
largest_locked = amount
# Airdrop
for miner in miners:
tx = token.transact({'from': creator}).transfer(miner, amount)
chain.wait.for_receipt(tx)
assert 300 == escrow.call().calculateLockedTokens(ursula, 2)
assert 0 == escrow.call().calculateLockedTokens(ursula, 3)
wait_time(chain, 1)
assert 600 == escrow.call().getLockedTokens(ursula)
assert 300 == escrow.call().calculateLockedTokens(ursula, 1)
assert 0 == escrow.call().calculateLockedTokens(ursula, 2)
amount = amount // 2
# Ursula can increase lock
tx = escrow.transact({'from': ursula}).lock(500, 2)
# Lock
for index, miner in enumerate(miners[::-1]):
balance = token().balanceOf(miner)
tx = token.transact({'from': miner}).approve(escrow.contract.address, balance)
chain.wait.for_receipt(tx)
assert 600 == escrow.call().getLockedTokens(ursula)
assert 800 == escrow.call().calculateLockedTokens(ursula, 1)
assert 500 == escrow.call().calculateLockedTokens(ursula, 2)
assert 200 == escrow.call().calculateLockedTokens(ursula, 3)
assert 0 == escrow.call().calculateLockedTokens(ursula, 4)
wait_time(chain, 1)
assert 800 == escrow.call().getLockedTokens(ursula)
# Alice can't deposit too low value (less then rate)
# TODO uncomment after completing logic
# with pytest.raises(TransactionFailed):
# tx = escrow.transact({'from': ursula}).deposit(100, 100)
# chain.wait.for_receipt(tx)
# Alice starts unlocking and increases lock by deposit more tokens
tx = escrow.transact({'from': alice}).deposit(500, 0)
tx = escrow.transact({'from': miner}).deposit(balance, len(miners) - index + 1)
chain.wait.for_receipt(tx)
tx = escrow.transact({'from': alice}).switchLock()
# Check current period
address_stop, shift = escrow().findCumSum(NULL_ADDR, 1, 1)
assert NULL_ADDR == address_stop.lower()
assert 0 == shift
# Wait next period
wait_time(chain, 1)
n_locked = escrow().getAllLockedTokens()
assert n_locked > 0
# And confirm activity
for miner in miners:
tx = escrow.transact({'from': miner}).confirmActivity()
chain.wait.for_receipt(tx)
assert 500 == escrow.call().getLockedTokens(alice)
assert 1000 == escrow.call().calculateLockedTokens(alice, 1)
assert 500 == escrow.call().calculateLockedTokens(alice, 2)
assert 0 == escrow.call().calculateLockedTokens(alice, 3)
wait_time(chain, 1)
assert 1000 == escrow.call().getLockedTokens(alice)
# And increases locked time
tx = escrow.transact({'from': alice}).lock(0, 2)
chain.wait.for_receipt(tx)
assert 1000 == escrow.call().getLockedTokens(alice)
assert 500 == escrow.call().calculateLockedTokens(alice, 1)
assert 0 == escrow.call().calculateLockedTokens(alice, 2)
address_stop, shift = escrow().findCumSum(NULL_ADDR, n_locked // 3, 1)
assert miners[0].lower() == address_stop.lower()
assert n_locked // 3 == shift
# Alice increases lock by small amount of tokens
tx = escrow.transact({'from': alice}).deposit(100, 0)
chain.wait.for_receipt(tx)
assert 600 == escrow.call().calculateLockedTokens(alice, 1)
assert 100 == escrow.call().calculateLockedTokens(alice, 2)
assert 0 == escrow.call().calculateLockedTokens(alice, 3)
address_stop, shift = escrow().findCumSum(NULL_ADDR, largest_locked, 1)
assert miners[1].lower() == address_stop.lower()
assert 0 == shift
# # Ursula can't destroy contract
# with pytest.raises(TransactionFailed):
# tx = escrow.transact({'from': ursula}).destroy()
# chain.wait.for_receipt(tx)
#
# # Destroy contract from creator and refund all to Ursula and Alice
# tx = escrow.transact({'from': creator}).destroy()
# chain.wait.for_receipt(tx)
# assert 0 == token.call().balanceOf(escrow.address)
# assert 10000 == token.call().balanceOf(ursula)
# assert 10000 == token.call().balanceOf(alice)
address_stop, shift = escrow().findCumSum(
miners[1], largest_locked // 2 + 1, 1)
assert miners[2].lower() == address_stop.lower()
assert 1 == shift
address_stop, shift = escrow().findCumSum(NULL_ADDR, 1, 10)
assert NULL_ADDR != address_stop.lower()
assert 0 != shift
address_stop, shift = escrow().findCumSum(NULL_ADDR, 1, 11)
assert NULL_ADDR == address_stop.lower()
assert 0 == shift
def test_locked_distribution(web3, testerchain, token, escrow):
with testerchain as chain:
NULL_ADDR = '0x' + '0' * 40
creator = web3.eth.accounts[0]
miners = web3.eth.accounts[1:]
amount = token.call().balanceOf(creator) // 2
largest_locked = amount
# Airdrop
for miner in miners:
tx = token.transact({'from': creator}).transfer(miner, amount)
chain.wait.for_receipt(tx)
amount = amount // 2
# Lock
for index, miner in enumerate(miners[::-1]):
balance = token.call().balanceOf(miner)
tx = token.transact({'from': miner}).approve(escrow.address, balance)
chain.wait.for_receipt(tx)
tx = escrow.transact({'from': miner}).deposit(balance, len(miners) - index + 1)
chain.wait.for_receipt(tx)
# Check current period
address_stop, shift = escrow.call().findCumSum(NULL_ADDR, 1, 1)
assert NULL_ADDR == address_stop.lower()
assert 0 == shift
# Wait next period
wait_time(chain, 1)
n_locked = escrow.call().getAllLockedTokens()
assert n_locked > 0
# And confirm activity
for miner in miners:
tx = escrow.transact({'from': miner}).confirmActivity()
chain.wait.for_receipt(tx)
address_stop, shift = escrow.call().findCumSum(NULL_ADDR, n_locked // 3, 1)
assert miners[0].lower() == address_stop.lower()
assert n_locked // 3 == shift
address_stop, shift = escrow.call().findCumSum(NULL_ADDR, largest_locked, 1)
assert miners[1].lower() == address_stop.lower()
assert 0 == shift
address_stop, shift = escrow.call().findCumSum(
miners[1], largest_locked // 2 + 1, 1)
assert miners[2].lower() == address_stop.lower()
for index, _ in enumerate(miners[:-1]):
address_stop, shift = escrow().findCumSum(NULL_ADDR, 1, index + 3)
assert miners[index + 1].lower() == address_stop.lower()
assert 1 == shift
address_stop, shift = escrow.call().findCumSum(NULL_ADDR, 1, 10)
assert NULL_ADDR != address_stop.lower()
assert 0 != shift
address_stop, shift = escrow.call().findCumSum(NULL_ADDR, 1, 11)
assert NULL_ADDR == address_stop.lower()
assert 0 == shift
for index, _ in enumerate(miners[:-1]):
address_stop, shift = escrow.call().findCumSum(NULL_ADDR, 1, index + 3)
assert miners[index + 1].lower() == address_stop.lower()
assert 1 == shift
def test_mining(testerchain, token, escrow):
web3 = testerchain.web3
chain = testerchain.chain
creator = web3.eth.accounts[0]
ursula = web3.eth.accounts[1]
alice = web3.eth.accounts[2]
def test_mining(web3, testerchain, token, escrow):
with testerchain as chain:
creator = web3.eth.accounts[0]
ursula = web3.eth.accounts[1]
alice = web3.eth.accounts[2]
policy_manager, _ = chain.provider.get_or_deploy_contract(
'PolicyManagerTest', deploy_args=[token.contract.address, escrow.contract.address],
deploy_transaction={'from': creator})
tx = escrow.transact({'from': creator}).setPolicyManager(policy_manager.address)
chain.wait.for_receipt(tx)
policy_manager, _ = chain.provider.get_or_deploy_contract(
'PolicyManagerTest', deploy_args=[token.address, escrow.address],
deploy_transaction={'from': creator})
tx = escrow.transact({'from': creator}).setPolicyManager(policy_manager.address)
chain.wait.for_receipt(tx)
# Give Ursula and Alice some coins
tx = token.transact({'from': creator}).transfer(ursula, 10000)
chain.wait.for_receipt(tx)
tx = token.transact({'from': creator}).transfer(alice, 10000)
chain.wait.for_receipt(tx)
# Give Ursula and Alice some coins
tx = token.transact({'from': creator}).transfer(ursula, 10000)
chain.wait.for_receipt(tx)
tx = token.transact({'from': creator}).transfer(alice, 10000)
chain.wait.for_receipt(tx)
# Ursula can't confirm and mint because no locked tokens
with pytest.raises(TransactionFailed):
tx = escrow.transact({'from': ursula}).mint()
chain.wait.for_receipt(tx)
with pytest.raises(TransactionFailed):
tx = escrow.transact({'from': ursula}).confirmActivity()
chain.wait.for_receipt(tx)
# Ursula and Alice give Escrow rights to transfer
tx = token.transact({'from': ursula}).approve(escrow.address, 2000)
chain.wait.for_receipt(tx)
tx = token.transact({'from': alice}).approve(escrow.address, 500)
chain.wait.for_receipt(tx)
# Ursula and Alice transfer some tokens to the escrow and lock them
tx = escrow.transact({'from': ursula}).deposit(1000, 1)
chain.wait.for_receipt(tx)
tx = escrow.transact({'from': alice}).deposit(500, 2)
chain.wait.for_receipt(tx)
# Using locked tokens starts from next period
assert 0 == escrow.call().getAllLockedTokens()
# Give rights for mining
tx = token.transact({'from': creator}).addMiner(escrow.address)
chain.wait.for_receipt(tx)
assert token.call().isMiner(escrow.address)
# Ursula can't use method from Miner contract
with pytest.raises(TypeError):
tx = escrow.transact({'from': ursula}).mint(ursula, 1, 1, 1, 1, 1)
chain.wait.for_receipt(tx)
# Only Ursula confirm next period
wait_time(chain, 1)
assert 1500 == escrow.call().getAllLockedTokens()
tx = escrow.transact({'from': ursula}).confirmActivity()
chain.wait.for_receipt(tx)
# Checks that no error
tx = escrow.transact({'from': ursula}).confirmActivity()
chain.wait.for_receipt(tx)
# Ursula and Alice mint tokens for last periods
wait_time(chain, 1)
assert 1000 == escrow.call().getAllLockedTokens()
# Ursula can't confirm and mint because no locked tokens
with pytest.raises(TransactionFailed):
tx = escrow.transact({'from': ursula}).mint()
chain.wait.for_receipt(tx)
tx = escrow.transact({'from': alice}).mint()
chain.wait.for_receipt(tx)
assert 9050 == token.call().balanceOf(ursula)
assert 9521 == token.call().balanceOf(alice)
assert 1 == policy_manager.call().getPeriodsLength(ursula)
assert 1 == policy_manager.call().getPeriodsLength(alice)
period = escrow.call().getCurrentPeriod() - 1
assert period == policy_manager.call().getPeriod(ursula, 0)
assert period == policy_manager.call().getPeriod(alice, 0)
# Only Ursula confirm activity for next period
tx = escrow.transact({'from': ursula}).switchLock()
chain.wait.for_receipt(tx)
with pytest.raises(TransactionFailed):
tx = escrow.transact({'from': ursula}).confirmActivity()
chain.wait.for_receipt(tx)
# Ursula can't confirm next period because end of locking
wait_time(chain, 1)
assert 500 == escrow.call().getAllLockedTokens()
with pytest.raises(TransactionFailed):
tx = escrow.transact({'from': ursula}).confirmActivity()
chain.wait.for_receipt(tx)
# Ursula and Alice give Escrow rights to transfer
tx = token.transact({'from': ursula}).approve(escrow.contract.address, 2000)
chain.wait.for_receipt(tx)
tx = token.transact({'from': alice}).approve(escrow.contract.address, 500)
chain.wait.for_receipt(tx)
# But Alice can
tx = escrow.transact({'from': alice}).confirmActivity()
# Ursula and Alice transfer some tokens to the escrow and lock them
tx = escrow.transact({'from': ursula}).deposit(1000, 1)
chain.wait.for_receipt(tx)
tx = escrow.transact({'from': alice}).deposit(500, 2)
chain.wait.for_receipt(tx)
# Using locked tokens starts from next period
assert 0 == escrow().getAllLockedTokens()
# Give rights for mining
tx = token.transact({'from': creator}).addMiner(escrow.contract.address)
chain.wait.for_receipt(tx)
assert token().isMiner(escrow.contract.address)
# Ursula can't use method from Miner contract
with pytest.raises(TypeError):
tx = escrow.transact({'from': ursula}).mint(ursula, 1, 1, 1, 1, 1)
chain.wait.for_receipt(tx)
# Ursula mint tokens for next period
wait_time(chain, 1)
assert 500 == escrow.call().getAllLockedTokens()
tx = escrow.transact({'from': ursula}).mint()
chain.wait.for_receipt(tx)
# But Alice can't mining because she did not confirmed activity
with pytest.raises(TransactionFailed):
tx = escrow.transact({'from': alice}).mint()
chain.wait.for_receipt(tx)
assert 9163 == token.call().balanceOf(ursula)
assert 9521 == token.call().balanceOf(alice)
# Only Ursula confirm next period
wait_time(chain, 1)
assert 1500 == escrow().getAllLockedTokens()
tx = escrow.transact({'from': ursula}).confirmActivity()
chain.wait.for_receipt(tx)
assert 3 == policy_manager.call().getPeriodsLength(ursula)
assert 1 == policy_manager.call().getPeriodsLength(alice)
assert period + 1 == policy_manager.call().getPeriod(ursula, 1)
assert period + 2 == policy_manager.call().getPeriod(ursula, 2)
# Checks that no error
tx = escrow.transact({'from': ursula}).confirmActivity()
chain.wait.for_receipt(tx)
# Alice confirm next period and mint tokens
tx = escrow.transact({'from': alice}).switchLock()
# Ursula and Alice mint tokens for last periods
wait_time(chain, 1)
assert 1000 == escrow().getAllLockedTokens()
tx = escrow.transact({'from': ursula}).mint()
chain.wait.for_receipt(tx)
tx = escrow.transact({'from': alice}).mint()
chain.wait.for_receipt(tx)
assert 9050 == token().balanceOf(ursula)
assert 9521 == token().balanceOf(alice)
assert 1 == policy_manager.call().getPeriodsLength(ursula)
assert 1 == policy_manager.call().getPeriodsLength(alice)
period = escrow().getCurrentPeriod() - 1
assert period == policy_manager.call().getPeriod(ursula, 0)
assert period == policy_manager.call().getPeriod(alice, 0)
# Only Ursula confirm activity for next period
tx = escrow.transact({'from': ursula}).switchLock()
chain.wait.for_receipt(tx)
tx = escrow.transact({'from': ursula}).confirmActivity()
chain.wait.for_receipt(tx)
# Ursula can't confirm next period because end of locking
wait_time(chain, 1)
assert 500 == escrow().getAllLockedTokens()
with pytest.raises(TransactionFailed):
tx = escrow.transact({'from': ursula}).confirmActivity()
chain.wait.for_receipt(tx)
tx = escrow.transact({'from': alice}).confirmActivity()
chain.wait.for_receipt(tx)
wait_time(chain, 2)
assert 0 == escrow.call().getAllLockedTokens()
# But Alice can
tx = escrow.transact({'from': alice}).confirmActivity()
chain.wait.for_receipt(tx)
# Ursula mint tokens for next period
wait_time(chain, 1)
assert 500 == escrow().getAllLockedTokens()
tx = escrow.transact({'from': ursula}).mint()
chain.wait.for_receipt(tx)
# But Alice can't mining because she did not confirmed activity
with pytest.raises(TransactionFailed):
tx = escrow.transact({'from': alice}).mint()
chain.wait.for_receipt(tx)
assert 9163 == token.call().balanceOf(ursula)
assert 9634 == token.call().balanceOf(alice)
assert 9163 == token().balanceOf(ursula)
assert 9521 == token().balanceOf(alice)
assert 3 == policy_manager.call().getPeriodsLength(ursula)
assert 3 == policy_manager.call().getPeriodsLength(alice)
assert period + 3 == policy_manager.call().getPeriod(alice, 1)
assert period + 4 == policy_manager.call().getPeriod(alice, 2)
assert 3 == policy_manager.call().getPeriodsLength(ursula)
assert 1 == policy_manager.call().getPeriodsLength(alice)
assert period + 1 == policy_manager.call().getPeriod(ursula, 1)
assert period + 2 == policy_manager.call().getPeriod(ursula, 2)
# Ursula can't confirm and mint because no locked tokens
with pytest.raises(TransactionFailed):
tx = escrow.transact({'from': ursula}).mint()
chain.wait.for_receipt(tx)
with pytest.raises(TransactionFailed):
tx = escrow.transact({'from': ursula}).confirmActivity()
chain.wait.for_receipt(tx)
# Alice confirm next period and mint tokens
tx = escrow.transact({'from': alice}).switchLock()
chain.wait.for_receipt(tx)
tx = escrow.transact({'from': alice}).confirmActivity()
chain.wait.for_receipt(tx)
wait_time(chain, 2)
assert 0 == escrow().getAllLockedTokens()
tx = escrow.transact({'from': alice}).mint()
chain.wait.for_receipt(tx)
assert 9163 == token().balanceOf(ursula)
assert 9634 == token().balanceOf(alice)
# Ursula can lock some tokens again
tx = escrow.transact({'from': ursula}).lock(500, 4)
chain.wait.for_receipt(tx)
tx = escrow.transact({'from': ursula}).switchLock()
chain.wait.for_receipt(tx)
assert 500 == escrow.call().getLockedTokens(ursula)
assert 500 == escrow.call().calculateLockedTokens(ursula, 1)
assert 375 == escrow.call().calculateLockedTokens(ursula, 2)
assert 250 == escrow.call().calculateLockedTokens(ursula, 3)
assert 0 == escrow.call().calculateLockedTokens(ursula, 5)
# And can increase lock
tx = escrow.transact({'from': ursula}).lock(100, 0)
chain.wait.for_receipt(tx)
assert 600 == escrow.call().getLockedTokens(ursula)
assert 600 == escrow.call().calculateLockedTokens(ursula, 1)
assert 450 == escrow.call().calculateLockedTokens(ursula, 2)
assert 0 == escrow.call().calculateLockedTokens(ursula, 5)
tx = escrow.transact({'from': ursula}).lock(0, 2)
chain.wait.for_receipt(tx)
assert 600 == escrow.call().getLockedTokens(ursula)
assert 600 == escrow.call().calculateLockedTokens(ursula, 1)
assert 450 == escrow.call().calculateLockedTokens(ursula, 2)
assert 0 == escrow.call().calculateLockedTokens(ursula, 5)
tx = escrow.transact({'from': ursula}).deposit(800, 1)
chain.wait.for_receipt(tx)
assert 1400 == escrow.call().getLockedTokens(ursula)
assert 1400 == escrow.call().calculateLockedTokens(ursula, 1)
assert 1000 == escrow.call().calculateLockedTokens(ursula, 3)
assert 400 == escrow.call().calculateLockedTokens(ursula, 6)
assert 0 == escrow.call().calculateLockedTokens(ursula, 8)
assert 3 == policy_manager.call().getPeriodsLength(ursula)
assert 3 == policy_manager.call().getPeriodsLength(alice)
assert period + 3 == policy_manager.call().getPeriod(alice, 1)
assert period + 4 == policy_manager.call().getPeriod(alice, 2)
# Alice can withdraw all
tx = escrow.transact({'from': alice}).withdrawAll()
# Ursula can't confirm and mint because no locked tokens
with pytest.raises(TransactionFailed):
tx = escrow.transact({'from': ursula}).mint()
chain.wait.for_receipt(tx)
with pytest.raises(TransactionFailed):
tx = escrow.transact({'from': ursula}).confirmActivity()
chain.wait.for_receipt(tx)
assert 10134 == token.call().balanceOf(alice)
# TODO test max confirmed periods and miners
# Ursula can lock some tokens again
tx = escrow.transact({'from': ursula}).lock(500, 4)
chain.wait.for_receipt(tx)
tx = escrow.transact({'from': ursula}).switchLock()
chain.wait.for_receipt(tx)
assert 500 == escrow().getLockedTokens(ursula)
assert 500 == escrow().calculateLockedTokens(ursula, 1)
assert 375 == escrow().calculateLockedTokens(ursula, 2)
assert 250 == escrow().calculateLockedTokens(ursula, 3)
assert 0 == escrow().calculateLockedTokens(ursula, 5)
# And can increase lock
tx = escrow.transact({'from': ursula}).lock(100, 0)
chain.wait.for_receipt(tx)
assert 600 == escrow().getLockedTokens(ursula)
assert 600 == escrow().calculateLockedTokens(ursula, 1)
assert 450 == escrow().calculateLockedTokens(ursula, 2)
assert 0 == escrow().calculateLockedTokens(ursula, 5)
tx = escrow.transact({'from': ursula}).lock(0, 2)
chain.wait.for_receipt(tx)
assert 600 == escrow().getLockedTokens(ursula)
assert 600 == escrow().calculateLockedTokens(ursula, 1)
assert 450 == escrow().calculateLockedTokens(ursula, 2)
assert 0 == escrow().calculateLockedTokens(ursula, 5)
tx = escrow.transact({'from': ursula}).deposit(800, 1)
chain.wait.for_receipt(tx)
assert 1400 == escrow().getLockedTokens(ursula)
assert 1400 == escrow().calculateLockedTokens(ursula, 1)
assert 1000 == escrow().calculateLockedTokens(ursula, 3)
assert 400 == escrow().calculateLockedTokens(ursula, 6)
assert 0 == escrow().calculateLockedTokens(ursula, 8)
# Alice can withdraw all
tx = escrow.transact({'from': alice}).withdrawAll()
chain.wait.for_receipt(tx)
assert 10134 == token().balanceOf(alice)
# TODO test max confirmed periods and miners

View File

@ -62,60 +62,60 @@ rate = 20
number_of_periods = 10
def test_create_revoke(web3, testerchain, token, escrow, policy_manager):
with testerchain as chain:
def test_create_revoke(testerchain, token, escrow, policy_manager):
chain, web3 = testerchain.chain, testerchain.web3
creator = web3.eth.accounts[0]
node = web3.eth.accounts[1]
client = web3.eth.accounts[2]
bad_node = web3.eth.accounts[3]
creator = web3.eth.accounts[0]
node = web3.eth.accounts[1]
client = web3.eth.accounts[2]
bad_node = web3.eth.accounts[3]
# Try create policy for bad node
with pytest.raises(TransactionFailed):
tx = policy_manager.transact({'from': client}).createPolicy(policy_id, bad_node, 1, 1)
chain.wait.for_receipt(tx)
# Try create policy for bad node
with pytest.raises(TransactionFailed):
tx = policy_manager.transact({'from': client}).createPolicy(policy_id, bad_node, 1, 1)
chain.wait.for_receipt(tx)
# Create policy
period = escrow.call().getCurrentPeriod()
# Create policy
period = escrow().getCurrentPeriod()
tx = policy_manager.transact({'from': client}).createPolicy(policy_id, node, rate, number_of_periods)
chain.wait.for_receipt(tx)
policy = policy_manager.call().policies(policy_id)
assert 200 == token().balanceOf(policy_manager.address)
assert 9800 == token().balanceOf(client)
assert client == policy[0]
assert node == policy[1]
assert rate == policy[2]
assert period + 1 == policy[3]
assert period + 10 == policy[4]
# Try to create policy again
with pytest.raises(TransactionFailed):
tx = policy_manager.transact({'from': client}).createPolicy(policy_id, node, rate, number_of_periods)
chain.wait.for_receipt(tx)
policy = policy_manager.call().policies(policy_id)
assert 200 == token.call().balanceOf(policy_manager.address)
assert 9800 == token.call().balanceOf(client)
assert client == policy[0]
assert node == policy[1]
assert rate == policy[2]
assert period + 1 == policy[3]
assert period + 10 == policy[4]
# Try to create policy again
with pytest.raises(TransactionFailed):
tx = policy_manager.transact({'from': client}).createPolicy(policy_id, node, rate, number_of_periods)
chain.wait.for_receipt(tx)
# Not client try to revoke policy
with pytest.raises(TransactionFailed):
tx = policy_manager.transact({'from': creator}).revokePolicy(policy_id)
chain.wait.for_receipt(tx)
# Client try to revoke policy
tx = policy_manager.transact({'from': client}).revokePolicy(policy_id)
# Not client try to revoke policy
with pytest.raises(TransactionFailed):
tx = policy_manager.transact({'from': creator}).revokePolicy(policy_id)
chain.wait.for_receipt(tx)
policy = policy_manager.call().policies(policy_id)
assert '0x' + '0' * 40 == policy[0]
# Create another policy
period = escrow.call().getCurrentPeriod()
tx = policy_manager.transact({'from': client}).createPolicy(policy_id_2, node, rate, number_of_periods)
chain.wait.for_receipt(tx)
policy = policy_manager.call().policies(policy_id_2)
assert 200 == token.call().balanceOf(policy_manager.address)
assert 9800 == token.call().balanceOf(client)
assert client == policy[0]
assert node == policy[1]
assert rate == policy[2]
assert period + 1 == policy[3]
assert period + 10 == policy[4]
# Client try to revoke policy
tx = policy_manager.transact({'from': client}).revokePolicy(policy_id)
chain.wait.for_receipt(tx)
policy = policy_manager.call().policies(policy_id)
assert '0x' + '0' * 40 == policy[0]
# Create another policy
period = escrow().getCurrentPeriod()
tx = policy_manager.transact({'from': client}).createPolicy(policy_id_2, node, rate, number_of_periods)
chain.wait.for_receipt(tx)
policy = policy_manager.call().policies(policy_id_2)
assert 200 == token().balanceOf(policy_manager.address)
assert 9800 == token().balanceOf(client)
assert client == policy[0]
assert node == policy[1]
assert rate == policy[2]
assert period + 1 == policy[3]
assert period + 10 == policy[4]
def test_reward(web3, chain, token, escrow, policy_manager):
@ -124,7 +124,7 @@ def test_reward(web3, chain, token, escrow, policy_manager):
bad_node = web3.eth.accounts[3]
# Create policy
period = escrow.call().getCurrentPeriod()
period = escrow().getCurrentPeriod()
tx = policy_manager.transact({'from': client}).createPolicy(policy_id, node, rate, number_of_periods)
chain.wait.for_receipt(tx)
@ -148,8 +148,8 @@ def test_reward(web3, chain, token, escrow, policy_manager):
# Withdraw
tx = policy_manager.transact({'from': node}).withdraw()
chain.wait.for_receipt(tx)
assert 80 == token.call().balanceOf(node)
assert 120 == token.call().balanceOf(policy_manager.address)
assert 80 == token().balanceOf(node)
assert 120 == token().balanceOf(policy_manager.address)
# Mint more periods
for x in range(20):
@ -161,8 +161,8 @@ def test_reward(web3, chain, token, escrow, policy_manager):
# Withdraw
tx = policy_manager.transact({'from': node}).withdraw()
chain.wait.for_receipt(tx)
assert 200 == token.call().balanceOf(node)
assert 0 == token.call().balanceOf(policy_manager.address)
assert 200 == token().balanceOf(node)
assert 0 == token().balanceOf(policy_manager.address)
def test_refund(web3, chain, token, escrow, policy_manager):
@ -172,33 +172,33 @@ def test_refund(web3, chain, token, escrow, policy_manager):
# Create policy
tx = policy_manager.transact({'from': client}).createPolicy(policy_id, node, rate, number_of_periods)
chain.wait.for_receipt(tx)
tx = escrow.transact().setLastActivePeriod(escrow.call().getCurrentPeriod())
tx = escrow.transact().setLastActivePeriod(escrow().getCurrentPeriod())
chain.wait.for_receipt(tx)
# Wait and refund all
wait_time(chain, 9)
tx = policy_manager.transact({'from': client}).refund(policy_id)
chain.wait.for_receipt(tx)
assert 20 == token.call().balanceOf(policy_manager.address)
assert 9980 == token.call().balanceOf(client)
assert 20 == token().balanceOf(policy_manager.address)
assert 9980 == token().balanceOf(client)
assert client == policy_manager.call().policies(policy_id)[0]
wait_time(chain, 1)
tx = policy_manager.transact({'from': client}).refund(policy_id)
chain.wait.for_receipt(tx)
assert 0 == token.call().balanceOf(policy_manager.address)
assert 10000 == token.call().balanceOf(client)
assert 0 == token().balanceOf(policy_manager.address)
assert 10000 == token().balanceOf(client)
assert '0x' + '0' * 40 == policy_manager.call().policies(policy_id)[0]
# Create policy again
period = escrow.call().getCurrentPeriod()
period = escrow().getCurrentPeriod()
tx = policy_manager.transact({'from': client}).createPolicy(policy_id, node, rate, number_of_periods)
chain.wait.for_receipt(tx)
# Nothing to refund
tx = policy_manager.transact({'from': client}).refund(policy_id)
chain.wait.for_receipt(tx)
assert 200 == token.call().balanceOf(policy_manager.address)
assert 9800 == token.call().balanceOf(client)
assert 200 == token().balanceOf(policy_manager.address)
assert 9800 == token().balanceOf(client)
# Try to refund nonexistent policy
with pytest.raises(TransactionFailed):
@ -232,12 +232,12 @@ def test_refund(web3, chain, token, escrow, policy_manager):
wait_time(chain, 10)
tx = policy_manager.transact({'from': client}).refund(policy_id)
chain.wait.for_receipt(tx)
assert 80 == token.call().balanceOf(policy_manager.address)
assert 9920 == token.call().balanceOf(client)
assert 80 == token().balanceOf(policy_manager.address)
assert 9920 == token().balanceOf(client)
assert '0x' + '0' * 40 == policy_manager.call().policies(policy_id)[0]
# Create policy again
period = escrow.call().getCurrentPeriod()
period = escrow().getCurrentPeriod()
tx = policy_manager.transact({'from': client}).createPolicy(policy_id, node, rate, number_of_periods)
chain.wait.for_receipt(tx)
@ -258,8 +258,8 @@ def test_refund(web3, chain, token, escrow, policy_manager):
tx = policy_manager.transact({'from': client}).revokePolicy(policy_id)
chain.wait.for_receipt(tx)
policy = policy_manager.call().policies(policy_id)
assert 140 == token.call().balanceOf(policy_manager.address)
assert 9860 == token.call().balanceOf(client)
assert 140 == token().balanceOf(policy_manager.address)
assert 9860 == token().balanceOf(client)
assert '0x' + '0' * 40 == policy[0]
# Minting is useless after revoke

View File

@ -8,78 +8,78 @@ def test_create_token(testerchain):
https://github.com/ConsenSys/Tokens/
but some of the tests are converted from javascript to python
"""
with testerchain as chain:
creator = chain.web3.eth.accounts[1]
account1 = chain.web3.eth.accounts[0]
account2 = chain.web3.eth.accounts[2]
chain = testerchain.chain
# Create an ERC20 token
token, txhash = chain.provider.get_or_deploy_contract(
'NuCypherKMSToken', deploy_args=[10 ** 9, 10 ** 10],
deploy_transaction={'from': creator})
assert txhash is not None
creator = chain.web3.eth.accounts[1]
account1 = chain.web3.eth.accounts[0]
account2 = chain.web3.eth.accounts[2]
# Account balances
assert token.call().balanceOf(creator) == 10 ** 9
assert token.call().balanceOf(account1) == 0
# Create an ERC20 token
token, txhash = chain.provider.get_or_deploy_contract(
'NuCypherKMSToken', deploy_args=[10 ** 9, 10 ** 10],
deploy_transaction={'from': creator})
assert txhash is not None
# Basic properties
assert token.call().name() == 'NuCypher KMS'
assert token.call().decimals() == 18
assert token.call().symbol() == 'KMS'
# Account balances
assert token.call().balanceOf(creator) == 10 ** 9
assert token.call().balanceOf(account1) == 0
# Cannot send ethers to the contract
with pytest.raises(TransactionFailed):
tx = chain.web3.eth.sendTransaction({
'from': account1, 'to': token.address, 'value': 10 ** 9})
chain.wait.for_receipt(tx)
# Basic properties
assert token.call().name() == 'NuCypher KMS'
assert token.call().decimals() == 18
assert token.call().symbol() == 'KMS'
# Can transfer tokens
tx = token.transact({'from': creator}).transfer(account1, 10000)
# Cannot send ethers to the contract
with pytest.raises(TransactionFailed):
tx = chain.web3.eth.sendTransaction({'from': account1, 'to': token.address, 'value': 10 ** 9})
chain.wait.for_receipt(tx)
assert token.call().balanceOf(account1) == 10000
assert token.call().balanceOf(creator) == 10 ** 9 - 10000
tx = token.transact({'from': account1}).transfer(account2, 10)
chain.wait.for_receipt(tx)
assert token.call().balanceOf(account1) == 10000 - 10
assert token.call().balanceOf(account2) == 10
# Can transfer tokens
tx = token.transact({'from': creator}).transfer(account1, 10000)
chain.wait.for_receipt(tx)
assert token.call().balanceOf(account1) == 10000
assert token.call().balanceOf(creator) == 10 ** 9 - 10000
tx = token.transact({'from': account1}).transfer(token.address, 10)
chain.wait.for_receipt(tx)
assert token.call().balanceOf(token.address) == 10
tx = token.transact({'from': account1}).transfer(account2, 10)
chain.wait.for_receipt(tx)
assert token.call().balanceOf(account1) == 10000 - 10
assert token.call().balanceOf(account2) == 10
# Can't mint tokens without rights
with pytest.raises(TransactionFailed):
tx = token.transact({'from': account1}).mint(account2, 10000)
chain.wait.for_receipt(tx)
tx = token.transact({'from': account1}).transfer(token.address, 10)
chain.wait.for_receipt(tx)
assert token.call().balanceOf(token.address) == 10
# Can't change rights not from owner
with pytest.raises(TransactionFailed):
tx = token.transact({'from': account1}).addMiner(account1)
chain.wait.for_receipt(tx)
with pytest.raises(TransactionFailed):
tx = token.transact({'from': account1}).removeMiner(account1)
chain.wait.for_receipt(tx)
# Give rights for mining
tx = token.transact({'from': creator}).addMiner(account1)
chain.wait.for_receipt(tx)
assert token.call().isMiner(account1)
# And try again
# Can't mint tokens without rights
with pytest.raises(TransactionFailed):
tx = token.transact({'from': account1}).mint(account2, 10000)
chain.wait.for_receipt(tx)
assert token.call().balanceOf(account2) == 10010
assert token.call().totalSupply() == 10 ** 9 + 10000
# Remove rights for mining
tx = token.transact({'from': creator}).removeMiner(account1)
# Can't change rights not from owner
with pytest.raises(TransactionFailed):
tx = token.transact({'from': account1}).addMiner(account1)
chain.wait.for_receipt(tx)
with pytest.raises(TransactionFailed):
tx = token.transact({'from': account1}).removeMiner(account1)
chain.wait.for_receipt(tx)
assert not token.call().isMiner(account1)
# Can burn own tokens
tx = token.transact({'from': account2}).burn(10000)
chain.wait.for_receipt(tx)
assert token.call().balanceOf(account2) == 10
assert token.call().totalSupply() == 10 ** 9
# Give rights for mining
tx = token.transact({'from': creator}).addMiner(account1)
chain.wait.for_receipt(tx)
assert token.call().isMiner(account1)
# And try again
tx = token.transact({'from': account1}).mint(account2, 10000)
chain.wait.for_receipt(tx)
assert token.call().balanceOf(account2) == 10010
assert token.call().totalSupply() == 10 ** 9 + 10000
# Remove rights for mining
tx = token.transact({'from': creator}).removeMiner(account1)
chain.wait.for_receipt(tx)
assert not token.call().isMiner(account1)
# Can burn own tokens
tx = token.transact({'from': account2}).burn(10000)
chain.wait.for_receipt(tx)
assert token.call().balanceOf(account2) == 10
assert token.call().totalSupply() == 10 ** 9

View File

@ -1,4 +1,3 @@
def test_testerchain_create(testerchain):
with testerchain as chain:
assert chain.web3.eth.blockNumber >= 0
assert testerchain.chain.web3.eth.blockNumber >= 0

View File

@ -5,14 +5,19 @@ from nkms_eth.escrow import Escrow
from nkms_eth.token import NuCypherKMSToken
def test_create_escrow(testerchain, token):
# token = NuCypherKMSToken(blockchain=testerchain)
def test_create_escrow(testerchain):
with raises(NoKnownAddress):
NuCypherKMSToken.get(blockchain=testerchain)
token = NuCypherKMSToken(blockchain=testerchain)
same_token = NuCypherKMSToken.get(blockchain=testerchain)
assert len(token.contract.address) == 42
assert token.contract.address == same_token.contract.address
with raises(NoKnownAddress):
Escrow.get(blockchain=testerchain, token=token)
escrow = Escrow(blockchain=testerchain, token=token)
same_escrow = Escrow.get(blockchain=testerchain, token=token)
e1 = Escrow(blockchain=testerchain, token=token)
e2 = Escrow.get(blockchain=testerchain, token=token)
assert len(e1.contract.address) == 42
assert e1.contract.address == e2.contract.addresst.address == e2.contract.address
assert len(escrow.contract.address) == 42
assert escrow.contract.address == same_escrow.contract.address

View File

@ -1,65 +1,50 @@
import random
import pytest
from nkms_eth.escrow import Escrow
from nkms_eth.miner import Miner
from nkms_eth.token import NuCypherKMSToken
M = 10 ** 6
def airdrop(blockchain, token) -> None:
"""
Airdrops from accounts[0] to others
"""
web3 = blockchain.web3
# token = Token.get(blockchain=blockchain)
def txs():
for account in web3.eth.accounts[1:]:
tx = token.contract.transact({'from': web3.eth.accounts[0]}).transfer(account, 10000*M)
yield tx
for tx in txs():
blockchain.chain.wait.for_receipt(tx, timeout=10)
def test_deposit(testerchain, miner, token):
airdrop(testerchain, token)
ursula.lock(amount=1000*M,
locktime=100,
address=testerchain.web3.eth.accounts[1])
token.airdrop()
miner.lock(amount=1000*M, locktime=100, address=testerchain.web3.eth.accounts[1])
def test_select_ursulas(testerchain, miner, escrow, token):
airdrop(testerchain, token)
def test_select_ursulas(testerchain):
token = NuCypherKMSToken(blockchain=testerchain)
escrow = Escrow(blockchain=testerchain, token=token)
miner = Miner(blockchain=testerchain)
token.airdrop()
# Create a random set of miners (we have 9 in total)
for u in testerchain.web3.eth.accounts[1:]:
miner.lock((10 + random.randrange(9000)) * M, 100, u)
testerchain.wait.for_block(testerchain.web3.eth.blockNumber + escrow.BLOCKS_PER_PERIOD)
miner.lock((10 + random.randrange(9000))*M, 100, u)
testerchain.chain.wait.for_block(testerchain.web3.eth.blockNumber+escrow.hours_per_period)
miners = escrow.sample(3)
miners = escrow.sample(quantity=3)
assert len(miners) == 3
assert len(set(miners)) == 3
with pytest.raises(Exception):
escrow.sample(100) # Waay more than we have deployed
escrow.sample(quantity=100) # Waay more than we have deployed
def test_mine_withdraw(testerchain, miner, token, escrow):
airdrop(testerchain, token)
token.airdrop()
addr = testerchain.web3.eth.accounts[1]
initial_balance = token.balance(addr)
# Create a random set of miners (we have 9 in total)
for u in testerchain.web3.eth.accounts[1:]:
miner.lock(amount=(10 + random.randrange(9000))*M,
locktime=1,
address=u)
testerchain.chain.wait.for_block(testerchain.web3.eth.blockNumber + 2 * escrow.BLOCKS_PER_PERIOD)
miner.lock(amount=(10 + random.randrange(9000))*M, locktime=1, address=u)
testerchain.chain.wait.for_block(testerchain.web3.eth.blockNumber + 2 * escrow.hours_per_period)
miner.mine(addr)
miner.withdraw(addr)
final_balance = token.balance(addr)
assert final_balance > initial_balance
assert final_balance > initial_balance

View File

@ -1,5 +1,7 @@
from pytest import raises
from populus.contracts.exceptions import NoKnownAddress
from nkms_eth.blockchain import TesterBlockchain
from nkms_eth.token import NuCypherKMSToken
@ -7,3 +9,29 @@ def test_get_token_before_creation(testerchain):
with raises(NoKnownAddress):
NuCypherKMSToken.get(blockchain=testerchain)
def test_create_nucypher_kms_token(testerchain):
token = NuCypherKMSToken(blockchain=testerchain)
assert len(token.contract.address) == 42
assert token.contract.call().totalSupply() != 0
assert token.contract.call().totalSupply() == 1000000000000000000000000000
def test_create_then_get_nucypher_kms_token(testerchain):
with raises(NoKnownAddress):
NuCypherKMSToken.get(blockchain=testerchain)
token = NuCypherKMSToken(blockchain=testerchain)
assert len(token.contract.address) == 42
assert token.contract.call().totalSupply() != 0
assert token.contract.call().totalSupply() == 1000000000000000000000000000
same_token = NuCypherKMSToken.get(blockchain=testerchain)
assert token.contract.address == same_token.contract.address
assert token == same_token