2017-11-25 23:03:20 +00:00
|
|
|
from nkms_eth import token
|
2017-11-26 07:38:07 +00:00
|
|
|
from nkms_eth import escrow
|
2017-11-25 23:03:20 +00:00
|
|
|
from nkms_eth import ursula
|
2017-11-27 00:59:25 +00:00
|
|
|
import random
|
|
|
|
import pytest
|
2017-11-25 23:03:20 +00:00
|
|
|
|
|
|
|
M = 10 ** 6
|
|
|
|
|
|
|
|
|
|
|
|
def airdrop(chain):
|
|
|
|
"""
|
|
|
|
Airdrops from accounts[0] to others
|
|
|
|
"""
|
|
|
|
web3 = chain.web3
|
|
|
|
tok = token.get()
|
|
|
|
txs = [
|
|
|
|
tok.transact({'from': web3.eth.accounts[0]}).transfer(account, 10000 * M)
|
|
|
|
for account in web3.eth.accounts[1:]]
|
|
|
|
for tx in txs:
|
|
|
|
chain.wait.for_receipt(tx, timeout=10)
|
|
|
|
|
|
|
|
|
|
|
|
def test_deposit(chain):
|
|
|
|
token.create()
|
2017-11-26 07:38:07 +00:00
|
|
|
escrow.create()
|
2017-11-25 23:03:20 +00:00
|
|
|
airdrop(chain)
|
|
|
|
ursula.lock(1000 * M, 100, chain.web3.eth.accounts[1])
|
2017-11-27 00:59:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_select_ursulas(chain):
|
|
|
|
token.create()
|
|
|
|
escrow.create()
|
|
|
|
airdrop(chain)
|
|
|
|
|
|
|
|
# Create a random set of miners (we have 9 in total)
|
|
|
|
for u in chain.web3.eth.accounts[1:]:
|
|
|
|
ursula.lock((10 + random.randrange(9000)) * M, 100, u)
|
2018-01-13 17:38:11 +00:00
|
|
|
chain.wait.for_block(chain.web3.eth.blockNumber + escrow.BLOCKS_PER_PERIOD)
|
2017-11-27 00:59:25 +00:00
|
|
|
|
|
|
|
miners = escrow.sample(3)
|
|
|
|
assert len(miners) == 3
|
|
|
|
assert len(set(miners)) == 3
|
|
|
|
|
|
|
|
with pytest.raises(Exception):
|
|
|
|
escrow.sample(100) # Waay more than we have deployed
|
2017-12-03 02:44:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_mine_withdraw(chain):
|
|
|
|
token.create()
|
|
|
|
escrow.create()
|
|
|
|
airdrop(chain)
|
|
|
|
|
|
|
|
addr = chain.web3.eth.accounts[1]
|
|
|
|
initial_balance = token.balance(addr)
|
|
|
|
|
|
|
|
# Create a random set of miners (we have 9 in total)
|
|
|
|
for u in chain.web3.eth.accounts[1:]:
|
2018-01-12 16:52:17 +00:00
|
|
|
ursula.lock((10 + random.randrange(9000)) * M, 1, u)
|
2017-12-03 02:44:48 +00:00
|
|
|
|
2018-01-13 17:38:11 +00:00
|
|
|
chain.wait.for_block(chain.web3.eth.blockNumber + 2 * escrow.BLOCKS_PER_PERIOD)
|
2017-12-03 02:44:48 +00:00
|
|
|
|
|
|
|
ursula.mine(addr)
|
|
|
|
ursula.withdraw(addr)
|
|
|
|
final_balance = token.balance(addr)
|
|
|
|
|
|
|
|
assert final_balance > initial_balance
|