mirror of https://github.com/nucypher/nucypher.git
Massage Token/Issuer contract tests into running again. Removes .lower calls, update web3 events API to use event Filters.
parent
b9d460a719
commit
8b00b0a1cd
|
@ -1,4 +1,5 @@
|
|||
import random
|
||||
import time
|
||||
from abc import ABC
|
||||
|
||||
from nkms.blockchain.eth.interfaces import Provider
|
||||
|
@ -76,10 +77,16 @@ class TesterBlockchain(TheBlockchain):
|
|||
|
||||
_network = 'tester'
|
||||
|
||||
def wait_time(self, wait_hours):
|
||||
def wait_time(self, hours=None, seconds=None):
|
||||
"""Wait the specified number of wait_hours by comparing block timestamps."""
|
||||
if hours:
|
||||
duration = hours * 60 * 60
|
||||
elif seconds:
|
||||
duration = seconds
|
||||
else:
|
||||
raise Exception("Invalid time")
|
||||
|
||||
end_timestamp = self.provider.web3.eth.getBlock('latest').timestamp + wait_hours * 60 * 60
|
||||
end_timestamp = self.provider.web3.eth.getBlock('latest').timestamp + duration
|
||||
self.provider.web3.eth.web3.testing.timeTravel(end_timestamp)
|
||||
self.provider.web3.eth.web3.testing.mine(1)
|
||||
|
||||
|
|
|
@ -37,7 +37,7 @@ class ContractDeployer:
|
|||
return address
|
||||
|
||||
@property
|
||||
def displatcher(self):
|
||||
def dispatcher(self):
|
||||
return self.__dispatcher
|
||||
|
||||
@property
|
||||
|
@ -235,12 +235,12 @@ class MinerEscrowDeployer(ContractDeployer, NuCypherMinerConfig):
|
|||
assert is_ready
|
||||
|
||||
# Build deployment arguments
|
||||
deploy_args = [self.token_agent.contract_address] + self.mining_coefficient # config
|
||||
origin_args = {'from': self.token_agent.origin}
|
||||
|
||||
# 1 - Deploy #
|
||||
the_escrow_contract, deploy_txhash = self.blockchain.provider.\
|
||||
deploy_contract(self._contract_name, *deploy_args)
|
||||
the_escrow_contract, deploy_txhash = self.blockchain.provider.deploy_contract(self._contract_name,
|
||||
self.token_agent.contract_address,
|
||||
*self.mining_coefficient)
|
||||
_deploy_receipt = self.blockchain.wait_for_receipt(deploy_txhash)
|
||||
|
||||
# 2 - Deploy the dispatcher used for updating this contract #
|
||||
|
|
|
@ -44,37 +44,6 @@ def _read_registrar_file(registrar_filepath: str) -> dict:
|
|||
return registrar_data
|
||||
|
||||
|
||||
def _write_registrar_file(registrar_data: dict, registrar_filepath: str) -> None:
|
||||
"""
|
||||
Writes the registrar data dict as JSON to the registrar file. If no
|
||||
file exists, it will create it and write the data. If a file does exist
|
||||
and contains JSON data, it will _overwrite_ everything in it.
|
||||
"""
|
||||
with open(registrar_filepath, 'a+') as registrar_file:
|
||||
registrar_file.seek(0)
|
||||
registrar_file.write(json.dumps(registrar_data))
|
||||
registrar_file.truncate()
|
||||
|
||||
|
||||
def _read_registrar_file(registrar_filepath: str) -> dict:
|
||||
"""
|
||||
Reads the registrar file and parses the JSON and returns a dict.
|
||||
If the file is empty or the JSON is corrupt, it will return an empty
|
||||
dict.
|
||||
If you are modifying or updating the registrar file, you _must_ call
|
||||
this function first to get the current state to append to the dict or
|
||||
modify it because _write_registrar_file overwrites the file.
|
||||
"""
|
||||
try:
|
||||
with open(registrar_filepath, 'r') as registrar_file:
|
||||
registrar_data = json.loads(registrar_file.read())
|
||||
except json.decoder.JSONDecodeError:
|
||||
registrar_data = dict()
|
||||
except FileNotFoundError:
|
||||
raise RegistrarDoesNotExist("No Registrar exists at this filepath.")
|
||||
return registrar_data
|
||||
|
||||
|
||||
class Registrar:
|
||||
"""
|
||||
Records known contracts on the disk for future access and utility.
|
||||
|
@ -237,14 +206,16 @@ class Provider:
|
|||
try:
|
||||
contract = self.__contract_cache[contract_name]
|
||||
except KeyError:
|
||||
raise self.UnknownContract('{} is not a known contract.'.format(contract_name))
|
||||
raise self.UnknownContract('{} is not a compiled contract.'.format(contract_name))
|
||||
else:
|
||||
return contract
|
||||
|
||||
def deploy_contract(self, contract_name: str, *args, **kwargs) -> Tuple[str, str]:
|
||||
contract = self.__get_cached_contract(contract_name)
|
||||
|
||||
deploy_bytecode = contract.constructor(*args, **kwargs).buildTransaction()
|
||||
transaction = {'from': self.web3.eth.coinbase}
|
||||
|
||||
deploy_bytecode = contract.constructor(*args, **kwargs).buildTransaction(transaction)
|
||||
|
||||
txhash = self.web3.eth.sendTransaction(deploy_bytecode) # deploy!
|
||||
receipt = self.web3.eth.waitForTransactionReceipt(txhash)
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import pytest
|
||||
from ethereum.tester import TransactionFailed
|
||||
from eth_tester.exceptions import TransactionFailed
|
||||
from web3.contract import Contract
|
||||
|
||||
|
||||
|
@ -21,30 +21,28 @@ def test_dispatcher(web3, chain):
|
|||
contract2_lib, _ = chain.provider.get_or_deploy_contract('ContractV2', deploy_args=[1])
|
||||
contract3_lib, _ = chain.provider.get_or_deploy_contract('ContractV3', deploy_args=[2])
|
||||
contract2_bad_lib, _ = chain.provider.get_or_deploy_contract('ContractV2Bad')
|
||||
dispatcher, _ = chain.provider.get_or_deploy_contract(
|
||||
'Dispatcher', deploy_args=[contract1_lib.address],
|
||||
deploy_transaction={'from': creator})
|
||||
assert dispatcher.call().target().lower() == contract1_lib.address
|
||||
dispatcher, _ = chain.provider.get_or_deploy_contract('Dispatcher', contract1_lib.address)
|
||||
assert dispatcher.call().target() == contract1_lib.address
|
||||
|
||||
events = dispatcher.pastEvents('Upgraded').get()
|
||||
assert 1 == len(events)
|
||||
event_args = events[0]['args']
|
||||
assert '0x' + '0' * 40 == event_args['from']
|
||||
assert contract1_lib.address.lower() == event_args['to'].lower()
|
||||
assert contract1_lib.address == event_args['to']
|
||||
assert creator == event_args['owner']
|
||||
|
||||
# Assign dispatcher address as contract.
|
||||
# In addition to the interface can be used ContractV1, ContractV2 or ContractV3 ABI
|
||||
contract_instance = web3.eth.contract(
|
||||
contract_interface.abi,
|
||||
dispatcher.address,
|
||||
abi=contract_interface.abi,
|
||||
address=dispatcher.address,
|
||||
ContractFactoryClass=Contract)
|
||||
|
||||
# Only owner can change target address for dispatcher
|
||||
with pytest.raises(TransactionFailed):
|
||||
tx = dispatcher.transact({'from': account}).upgrade(contract2_lib.address)
|
||||
chain.wait.for_receipt(tx)
|
||||
assert dispatcher.call().target().lower() == contract1_lib.address.lower()
|
||||
assert dispatcher.call().target() == contract1_lib.address
|
||||
|
||||
# Check values before upgrade
|
||||
assert contract_instance.call().getStorageValue() == 1
|
||||
|
@ -80,18 +78,18 @@ def test_dispatcher(web3, chain):
|
|||
with pytest.raises(TransactionFailed):
|
||||
tx = dispatcher.transact({'from': creator}).upgrade(contract2_bad_lib.address)
|
||||
chain.wait.for_receipt(tx)
|
||||
assert dispatcher.call().target().lower() == contract1_lib.address
|
||||
assert dispatcher.call().target() == contract1_lib.address
|
||||
|
||||
# Upgrade contract
|
||||
tx = dispatcher.transact({'from': creator}).upgrade(contract2_lib.address)
|
||||
chain.wait.for_receipt(tx)
|
||||
assert dispatcher.call().target().lower() == contract2_lib.address.lower()
|
||||
assert dispatcher.call().target() == contract2_lib.address
|
||||
|
||||
events = dispatcher.pastEvents('Upgraded').get()
|
||||
assert 2 == len(events)
|
||||
event_args = events[1]['args']
|
||||
assert contract1_lib.address.lower() == event_args['from'].lower()
|
||||
assert contract2_lib.address.lower() == event_args['to'].lower()
|
||||
assert contract1_lib.address == event_args['from']
|
||||
assert contract2_lib.address == event_args['to']
|
||||
assert creator == event_args['owner']
|
||||
|
||||
# Check values after upgrade
|
||||
|
@ -126,8 +124,8 @@ def test_dispatcher(web3, chain):
|
|||
|
||||
# Changes ABI to ContractV2 for using additional methods
|
||||
contract_instance = web3.eth.contract(
|
||||
contract2_lib.abi,
|
||||
dispatcher.address,
|
||||
abi=contract2_lib.abi,
|
||||
address=dispatcher.address,
|
||||
ContractFactoryClass=Contract)
|
||||
|
||||
# Check new method and finish upgrade method
|
||||
|
@ -145,12 +143,12 @@ def test_dispatcher(web3, chain):
|
|||
with pytest.raises(TransactionFailed):
|
||||
tx = dispatcher.transact({'from': creator}).upgrade(contract2_bad_lib.address)
|
||||
chain.wait.for_receipt(tx)
|
||||
assert dispatcher.call().target().lower() == contract2_lib.address.lower()
|
||||
assert dispatcher.call().target() == contract2_lib.address
|
||||
|
||||
# But can rollback
|
||||
tx = dispatcher.transact({'from': creator}).rollback()
|
||||
chain.wait.for_receipt(tx)
|
||||
assert dispatcher.call().target().lower() == contract1_lib.address
|
||||
assert dispatcher.call().target() == contract1_lib.address
|
||||
assert contract_instance.call().getArrayValueLength() == 2
|
||||
assert contract_instance.call().getArrayValue(0) == 12
|
||||
assert contract_instance.call().getArrayValue(1) == 232
|
||||
|
@ -162,15 +160,15 @@ def test_dispatcher(web3, chain):
|
|||
events = dispatcher.pastEvents('RolledBack').get()
|
||||
assert 1 == len(events)
|
||||
event_args = events[0]['args']
|
||||
assert contract2_lib.address.lower() == event_args['from'].lower()
|
||||
assert contract1_lib.address.lower() == event_args['to'].lower()
|
||||
assert contract2_lib.address == event_args['from']
|
||||
assert contract1_lib.address == event_args['to']
|
||||
assert creator == event_args['owner']
|
||||
|
||||
# Can't upgrade to the bad version
|
||||
with pytest.raises(TransactionFailed):
|
||||
tx = dispatcher.transact({'from': creator}).upgrade(contract2_bad_lib.address)
|
||||
chain.wait.for_receipt(tx)
|
||||
assert dispatcher.call().target().lower() == contract1_lib.address.lower()
|
||||
assert dispatcher.call().target() == contract1_lib.address
|
||||
|
||||
# Check dynamically sized value
|
||||
# TODO uncomment after fixing dispatcher
|
||||
|
@ -180,8 +178,8 @@ def test_dispatcher(web3, chain):
|
|||
|
||||
# Create Event
|
||||
contract_instance = web3.eth.contract(
|
||||
contract1_lib.abi,
|
||||
dispatcher.address,
|
||||
abi=contract1_lib.abi,
|
||||
address=dispatcher.address,
|
||||
ContractFactoryClass=Contract)
|
||||
tx = contract_instance.transact().createEvent(33)
|
||||
chain.wait.for_receipt(tx)
|
||||
|
@ -195,10 +193,10 @@ def test_dispatcher(web3, chain):
|
|||
tx = dispatcher.transact({'from': creator}).upgrade(contract3_lib.address)
|
||||
chain.wait.for_receipt(tx)
|
||||
contract_instance = web3.eth.contract(
|
||||
contract2_lib.abi,
|
||||
dispatcher.address,
|
||||
abi=contract2_lib.abi,
|
||||
address=dispatcher.address,
|
||||
ContractFactoryClass=Contract)
|
||||
assert dispatcher.call().target().lower() == contract3_lib.address.lower()
|
||||
assert dispatcher.call().target() == contract3_lib.address
|
||||
assert contract_instance.call().returnValue() == 20
|
||||
assert contract_instance.call().getStorageValue() == 5
|
||||
assert contract_instance.call().getArrayValueLength() == 2
|
||||
|
@ -220,12 +218,12 @@ def test_dispatcher(web3, chain):
|
|||
events = dispatcher.pastEvents('Upgraded').get()
|
||||
assert 4 == len(events)
|
||||
event_args = events[2]['args']
|
||||
assert contract1_lib.address.lower() == event_args['from'].lower()
|
||||
assert contract2_lib.address.lower() == event_args['to'].lower()
|
||||
assert contract1_lib.address == event_args['from']
|
||||
assert contract2_lib.address == event_args['to']
|
||||
assert creator == event_args['owner']
|
||||
event_args = events[3]['args']
|
||||
assert contract2_lib.address.lower() == event_args['from'].lower()
|
||||
assert contract3_lib.address.lower() == event_args['to'].lower()
|
||||
assert contract2_lib.address == event_args['from']
|
||||
assert contract3_lib.address == event_args['to']
|
||||
assert creator == event_args['owner']
|
||||
|
||||
# Create and check events
|
||||
|
@ -235,8 +233,8 @@ def test_dispatcher(web3, chain):
|
|||
assert 1 == len(events)
|
||||
assert 22 == events[0]['args']['value']
|
||||
contract_instance = web3.eth.contract(
|
||||
contract1_lib.abi,
|
||||
dispatcher.address,
|
||||
abi=contract1_lib.abi,
|
||||
address=dispatcher.address,
|
||||
ContractFactoryClass=Contract)
|
||||
events = contract_instance.pastEvents('EventV1').get()
|
||||
assert 1 == len(events)
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import pytest
|
||||
from ethereum.tester import TransactionFailed
|
||||
from eth_tester.exceptions import TransactionFailed
|
||||
import os
|
||||
|
||||
from web3.contract import Contract
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import pytest
|
||||
from ethereum.tester import TransactionFailed
|
||||
from web3.contract import Contract
|
||||
from eth_tester.exceptions import TransactionFailed
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
|
@ -24,9 +24,12 @@ def test_issuer(web3, chain, token):
|
|||
reserved_reward = 2 * 10 ** 40 - 10 ** 30
|
||||
tx = token.transact({'from': creator}).transfer(issuer.address, reserved_reward)
|
||||
chain.wait_for_receipt(tx)
|
||||
|
||||
events = issuer.eventFilter('Initialized')
|
||||
tx = issuer.transact().initialize()
|
||||
chain.wait_for_receipt(tx)
|
||||
events = issuer.pastEvents('Initialized').get()
|
||||
events = events.get_all_entries()
|
||||
|
||||
assert 1 == len(events)
|
||||
assert reserved_reward == events[0]['args']['reservedReward']
|
||||
balance = token.call().balanceOf(issuer.address)
|
||||
|
@ -114,8 +117,8 @@ def test_verifying_state(web3, chain, token):
|
|||
# Deploy second version of the contract
|
||||
contract_library_v2, _ = chain.provider.deploy_contract('IssuerV2Mock', token.address, 2, 2, 2, 2)
|
||||
contract = web3.eth.contract(
|
||||
contract_library_v2.abi,
|
||||
dispatcher.address,
|
||||
abi=contract_library_v2.abi,
|
||||
address=dispatcher.address,
|
||||
ContractFactoryClass=Contract)
|
||||
|
||||
# Give Miner tokens for reward and initialize contract
|
||||
|
|
|
@ -25,16 +25,16 @@ def test_linked_list(web3, chain):
|
|||
|
||||
# Insert new value
|
||||
tx = instance.transact().insert(HEAD, address2, NEXT)
|
||||
chain.wait.for_receipt(tx)
|
||||
chain.wait_for_receipt(tx)
|
||||
assert instance.call().exists()
|
||||
assert instance.call().sizeOf() == 1
|
||||
assert instance.call().valueExists(address2)
|
||||
|
||||
# Insert more values
|
||||
tx = instance.transact().insert(address2, address1, PREV)
|
||||
chain.wait.for_receipt(tx)
|
||||
chain.wait_for_receipt(tx)
|
||||
tx = instance.transact().insert(address2, address3, NEXT)
|
||||
chain.wait.for_receipt(tx)
|
||||
chain.wait_for_receipt(tx)
|
||||
assert instance.call().sizeOf() == 3
|
||||
|
||||
# Try to remove non-existent value
|
||||
|
@ -43,7 +43,7 @@ def test_linked_list(web3, chain):
|
|||
# Remove middle value
|
||||
assert instance.call().remove(address2) == address2
|
||||
tx = instance.transact().remove(address2)
|
||||
chain.wait.for_receipt(tx)
|
||||
chain.wait_for_receipt(tx)
|
||||
assert instance.call().sizeOf() == 2
|
||||
|
||||
# Check node
|
||||
|
@ -54,7 +54,7 @@ def test_linked_list(web3, chain):
|
|||
# Remove another value
|
||||
assert instance.call().remove(address3) == address3
|
||||
tx = instance.transact().remove(address3)
|
||||
chain.wait.for_receipt(tx)
|
||||
chain.wait_for_receipt(tx)
|
||||
assert instance.call().sizeOf() == 1
|
||||
|
||||
# Check node
|
||||
|
@ -65,7 +65,7 @@ def test_linked_list(web3, chain):
|
|||
# Remove last value
|
||||
assert instance.call().remove(address1).lower() == address1.lower()
|
||||
tx = instance.transact().remove(address1)
|
||||
chain.wait.for_receipt(tx)
|
||||
chain.wait_for_receipt(tx)
|
||||
assert instance.call().sizeOf() == 0
|
||||
|
||||
# Check head node
|
||||
|
@ -75,11 +75,11 @@ def test_linked_list(web3, chain):
|
|||
|
||||
# Push values
|
||||
tx = instance.transact().push(address2, NEXT)
|
||||
chain.wait.for_receipt(tx)
|
||||
chain.wait_for_receipt(tx)
|
||||
tx = instance.transact().push(address3, PREV)
|
||||
chain.wait.for_receipt(tx)
|
||||
chain.wait_for_receipt(tx)
|
||||
tx = instance.transact().push(address1, NEXT)
|
||||
chain.wait.for_receipt(tx)
|
||||
chain.wait_for_receipt(tx)
|
||||
assert instance.call().sizeOf() == 3
|
||||
|
||||
# Check nodes
|
||||
|
@ -94,9 +94,9 @@ def test_linked_list(web3, chain):
|
|||
assert instance.call().pop(NEXT).lower() == address1.lower()
|
||||
assert instance.call().pop(PREV).lower() == address3.lower()
|
||||
tx = instance.transact().pop(NEXT)
|
||||
chain.wait.for_receipt(tx)
|
||||
chain.wait_for_receipt(tx)
|
||||
tx = instance.transact().pop(PREV)
|
||||
chain.wait.for_receipt(tx)
|
||||
chain.wait_for_receipt(tx)
|
||||
assert instance.call().sizeOf() == 1
|
||||
|
||||
# Check last node
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import pytest
|
||||
from ethereum.tester import TransactionFailed
|
||||
from eth_tester.exceptions import TransactionFailed
|
||||
import os
|
||||
from web3.contract import Contract
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import pytest
|
||||
from ethereum.tester import TransactionFailed
|
||||
from eth_tester.exceptions import TransactionFailed
|
||||
import os
|
||||
from web3.contract import Contract
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import pytest
|
||||
from ethereum.tester import TransactionFailed
|
||||
from eth_tester.exceptions import TransactionFailed
|
||||
|
||||
|
||||
def test_create_token(web3, chain):
|
||||
|
@ -9,8 +9,8 @@ def test_create_token(web3, chain):
|
|||
but some of the tests are converted from javascript to python
|
||||
"""
|
||||
|
||||
creator = web3.eth.accounts[1]
|
||||
account1 = web3.eth.accounts[0]
|
||||
creator = web3.eth.accounts[0]
|
||||
account1 = web3.eth.accounts[1]
|
||||
account2 = web3.eth.accounts[2]
|
||||
|
||||
# Create an ERC20 token
|
||||
|
@ -28,27 +28,26 @@ def test_create_token(web3, chain):
|
|||
|
||||
# Cannot send ethers to the contract
|
||||
with pytest.raises(TransactionFailed):
|
||||
tx = web3.eth.sendTransaction({
|
||||
'from': account1, 'to': token.address, 'value': 10 ** 9})
|
||||
chain.wait.for_receipt(tx)
|
||||
tx = web3.eth.sendTransaction({'from': account1, 'to': token.address, 'value': 10 ** 9})
|
||||
chain.wait_for_receipt(tx)
|
||||
|
||||
# Can transfer tokens
|
||||
tx = token.transact({'from': creator}).transfer(account1, 10000)
|
||||
chain.wait.for_receipt(tx)
|
||||
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)
|
||||
chain.wait_for_receipt(tx)
|
||||
assert token.call().balanceOf(account1) == 10000 - 10
|
||||
assert token.call().balanceOf(account2) == 10
|
||||
|
||||
tx = token.transact({'from': account1}).transfer(token.address, 10)
|
||||
chain.wait.for_receipt(tx)
|
||||
chain.wait_for_receipt(tx)
|
||||
assert token.call().balanceOf(token.address) == 10
|
||||
|
||||
# Can burn own tokens
|
||||
tx = token.transact({'from': account2}).burn(1)
|
||||
chain.wait.for_receipt(tx)
|
||||
chain.wait_for_receipt(tx)
|
||||
assert token.call().balanceOf(account2) == 9
|
||||
assert token.call().totalSupply() == 10 ** 9 - 1
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import pytest
|
||||
from ethereum.tester import TransactionFailed
|
||||
from eth_tester.exceptions import TransactionFailed
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
|
@ -43,17 +43,10 @@ def user_escrow(web3, chain, token, escrow, policy_manager):
|
|||
return contract
|
||||
|
||||
|
||||
def wait_time(chain, wait_seconds):
|
||||
web3 = chain.web3
|
||||
step = 1
|
||||
end_timestamp = web3.eth.getBlock(web3.eth.blockNumber).timestamp + wait_seconds
|
||||
while web3.eth.getBlock(web3.eth.blockNumber).timestamp < end_timestamp:
|
||||
chain.wait.for_block(web3.eth.blockNumber + step)
|
||||
|
||||
|
||||
def test_escrow(web3, chain, token, user_escrow):
|
||||
creator = web3.eth.accounts[0]
|
||||
user = web3.eth.accounts[1]
|
||||
deposits = user_escrow.eventFilter('Deposited')
|
||||
|
||||
# Deposit some tokens to the user escrow and lock them
|
||||
tx = token.transact({'from': creator}).approve(user_escrow.address, 2000)
|
||||
|
@ -65,10 +58,10 @@ def test_escrow(web3, chain, token, user_escrow):
|
|||
assert 1000 >= user_escrow.call().getLockedTokens()
|
||||
assert 950 <= user_escrow.call().getLockedTokens()
|
||||
|
||||
events = user_escrow.pastEvents('Deposited').get()
|
||||
events = deposits.get_all_entries()
|
||||
assert 1 == len(events)
|
||||
event_args = events[0]['args']
|
||||
assert creator.lower() == event_args['sender'].lower()
|
||||
assert creator == event_args['sender']
|
||||
assert 1000 == event_args['value']
|
||||
assert 1000 == event_args['duration']
|
||||
|
||||
|
@ -87,6 +80,8 @@ def test_escrow(web3, chain, token, user_escrow):
|
|||
chain.wait_for_receipt(tx)
|
||||
assert 2000 == token.call().balanceOf(user_escrow.address)
|
||||
|
||||
withdraws = user_escrow.eventFilter('Withdrawn')
|
||||
|
||||
# Only user can withdraw available tokens
|
||||
with pytest.raises(TransactionFailed):
|
||||
tx = user_escrow.transact({'from': creator}).withdraw(100)
|
||||
|
@ -96,14 +91,15 @@ def test_escrow(web3, chain, token, user_escrow):
|
|||
assert 1000 == token.call().balanceOf(user)
|
||||
assert 1000 == token.call().balanceOf(user_escrow.address)
|
||||
|
||||
events = user_escrow.pastEvents('Withdrawn').get()
|
||||
events = withdraws.get_all_entries()
|
||||
|
||||
assert 1 == len(events)
|
||||
event_args = events[0]['args']
|
||||
assert user.lower() == event_args['owner'].lower()
|
||||
assert user == event_args['owner']
|
||||
assert 1000 == event_args['value']
|
||||
|
||||
# Wait some time
|
||||
wait_time(chain, 500)
|
||||
chain.wait_time(seconds=500)
|
||||
assert 500 >= user_escrow.call().getLockedTokens()
|
||||
assert 450 <= user_escrow.call().getLockedTokens()
|
||||
|
||||
|
@ -112,24 +108,28 @@ def test_escrow(web3, chain, token, user_escrow):
|
|||
chain.wait_for_receipt(tx)
|
||||
assert 1500 == token.call().balanceOf(user)
|
||||
|
||||
events = user_escrow.pastEvents('Withdrawn').get()
|
||||
# events = user_escrow.pastEvents('Withdrawn').get()
|
||||
events = withdraws.get_all_entries()
|
||||
|
||||
assert 2 == len(events)
|
||||
event_args = events[1]['args']
|
||||
assert user.lower() == event_args['owner'].lower()
|
||||
assert user == event_args['owner']
|
||||
assert 500 == event_args['value']
|
||||
|
||||
# Wait more time and withdraw all
|
||||
wait_time(chain, 500)
|
||||
chain.wait_time(seconds=500)
|
||||
assert 0 == user_escrow.call().getLockedTokens()
|
||||
tx = user_escrow.transact({'from': user}).withdraw(500)
|
||||
chain.wait_for_receipt(tx)
|
||||
assert 0 == token.call().balanceOf(user_escrow.address)
|
||||
assert 2000 == token.call().balanceOf(user)
|
||||
|
||||
events = user_escrow.pastEvents('Withdrawn').get()
|
||||
# events = user_escrow.pastEvents('Withdrawn').get()
|
||||
events = withdraws.get_all_entries()
|
||||
|
||||
assert 3 == len(events)
|
||||
event_args = events[2]['args']
|
||||
assert user.lower() == event_args['owner'].lower()
|
||||
assert user == event_args['owner']
|
||||
assert 500 == event_args['value']
|
||||
|
||||
|
||||
|
@ -137,6 +137,8 @@ def test_miner(web3, chain, token, escrow, user_escrow):
|
|||
creator = web3.eth.accounts[0]
|
||||
user = web3.eth.accounts[1]
|
||||
|
||||
deposits = user_escrow.eventFilter('Deposited')
|
||||
|
||||
# Deposit some tokens to the user escrow and lock them
|
||||
tx = token.transact({'from': creator}).approve(user_escrow.address, 1000)
|
||||
chain.wait_for_receipt(tx)
|
||||
|
@ -145,7 +147,9 @@ def test_miner(web3, chain, token, escrow, user_escrow):
|
|||
tx = token.transact({'from': creator}).transfer(user_escrow.address, 1000)
|
||||
chain.wait_for_receipt(tx)
|
||||
assert 2000 == token.call().balanceOf(user_escrow.address)
|
||||
assert 1 == len(user_escrow.pastEvents('Deposited').get())
|
||||
|
||||
events = deposits.get_all_entries()
|
||||
assert 1 == len(events)
|
||||
|
||||
# Only user can deposit tokens to the miner escrow
|
||||
with pytest.raises(TransactionFailed):
|
||||
|
@ -156,10 +160,12 @@ def test_miner(web3, chain, token, escrow, user_escrow):
|
|||
tx = user_escrow.transact({'from': user}).minerDeposit(10000, 5)
|
||||
chain.wait_for_receipt(tx)
|
||||
|
||||
miner_deposits = user_escrow.eventFilter('DepositedAsMiner')
|
||||
|
||||
# Deposit some tokens to the miners escrow
|
||||
tx = user_escrow.transact({'from': user}).minerDeposit(1500, 5)
|
||||
chain.wait_for_receipt(tx)
|
||||
assert user_escrow.address.lower() == escrow.call().node().lower()
|
||||
assert user_escrow.address == escrow.call().node()
|
||||
assert 1500 == escrow.call().value()
|
||||
assert 1500 == escrow.call().lockedValue()
|
||||
assert 5 == escrow.call().periods()
|
||||
|
@ -167,10 +173,11 @@ def test_miner(web3, chain, token, escrow, user_escrow):
|
|||
assert 11500 == token.call().balanceOf(escrow.address)
|
||||
assert 500 == token.call().balanceOf(user_escrow.address)
|
||||
|
||||
events = user_escrow.pastEvents('DepositedAsMiner').get()
|
||||
events = miner_deposits.get_all_entries()
|
||||
|
||||
assert 1 == len(events)
|
||||
event_args = events[0]['args']
|
||||
assert user.lower() == event_args['owner'].lower()
|
||||
assert user == event_args['owner']
|
||||
assert 1500 == event_args['value']
|
||||
assert 5 == event_args['periods']
|
||||
|
||||
|
@ -199,6 +206,14 @@ def test_miner(web3, chain, token, escrow, user_escrow):
|
|||
tx = escrow.transact({'from': user}).withdrawAll()
|
||||
chain.wait_for_receipt(tx)
|
||||
|
||||
locks = user_escrow.eventFilter('Locked')
|
||||
switches = user_escrow.eventFilter('LockSwitched')
|
||||
confirms = user_escrow.eventFilter('ActivityConfirmed')
|
||||
mints = user_escrow.eventFilter('Mined')
|
||||
miner_withdraws = user_escrow.eventFilter('WithdrawnAsMiner')
|
||||
withdraws = user_escrow.eventFilter('Withdrawn')
|
||||
|
||||
|
||||
# Use methods through the user escrow
|
||||
tx = user_escrow.transact({'from': user}).lock(100, 1)
|
||||
chain.wait_for_receipt(tx)
|
||||
|
@ -225,31 +240,35 @@ def test_miner(web3, chain, token, escrow, user_escrow):
|
|||
assert 9000 == token.call().balanceOf(escrow.address)
|
||||
assert 3000 == token.call().balanceOf(user_escrow.address)
|
||||
|
||||
events = user_escrow.pastEvents('Locked').get()
|
||||
events = locks.get_all_entries()
|
||||
assert 1 == len(events)
|
||||
event_args = events[0]['args']
|
||||
assert user.lower() == event_args['owner'].lower()
|
||||
assert user == event_args['owner']
|
||||
assert 100 == event_args['value']
|
||||
assert 1 == event_args['periods']
|
||||
events = user_escrow.pastEvents('LockSwitched').get()
|
||||
|
||||
events = switches.get_all_entries()
|
||||
assert 1 == len(events)
|
||||
event_args = events[0]['args']
|
||||
assert user.lower() == event_args['owner'].lower()
|
||||
events = user_escrow.pastEvents('ActivityConfirmed').get()
|
||||
assert user == event_args['owner']
|
||||
|
||||
events = confirms.get_all_entries()
|
||||
assert 1 == len(events)
|
||||
event_args = events[0]['args']
|
||||
assert user.lower() == event_args['owner'].lower()
|
||||
events = user_escrow.pastEvents('Mined').get()
|
||||
assert user == event_args['owner']
|
||||
|
||||
events = mints.get_all_entries()
|
||||
assert 1 == len(events)
|
||||
event_args = events[0]['args']
|
||||
assert user.lower() == event_args['owner'].lower()
|
||||
events = user_escrow.pastEvents('WithdrawnAsMiner').get()
|
||||
assert user == event_args['owner']
|
||||
|
||||
events = miner_withdraws.get_all_entries()
|
||||
assert 2 == len(events)
|
||||
event_args = events[0]['args']
|
||||
assert user.lower() == event_args['owner'].lower()
|
||||
assert user == event_args['owner']
|
||||
assert 1500 == event_args['value']
|
||||
event_args = events[1]['args']
|
||||
assert user.lower() == event_args['owner'].lower()
|
||||
assert user == event_args['owner']
|
||||
assert 1000 == event_args['value']
|
||||
|
||||
# User can withdraw reward for mining but no more than locked
|
||||
|
@ -261,10 +280,10 @@ def test_miner(web3, chain, token, escrow, user_escrow):
|
|||
assert 2000 == token.call().balanceOf(user_escrow.address)
|
||||
assert 1000 == token.call().balanceOf(user)
|
||||
|
||||
events = user_escrow.pastEvents('Withdrawn').get()
|
||||
events = withdraws.get_all_entries()
|
||||
assert 1 == len(events)
|
||||
event_args = events[0]['args']
|
||||
assert user.lower() == event_args['owner'].lower()
|
||||
assert user == event_args['owner']
|
||||
assert 1000 == event_args['value']
|
||||
|
||||
|
||||
|
@ -295,6 +314,9 @@ def test_policy(web3, chain, policy_manager, user_escrow):
|
|||
tx = web3.eth.sendTransaction({'from': web3.eth.coinbase, 'to': policy_manager.address, 'value': 10000})
|
||||
chain.wait_for_receipt(tx)
|
||||
|
||||
miner_collections = user_escrow.eventFilter('RewardWithdrawnAsMiner')
|
||||
rewards = user_escrow.eventFilter('RewardWithdrawn')
|
||||
|
||||
# Withdraw reward reward
|
||||
tx = user_escrow.transact({'from': user, 'gas_price': 0}).policyRewardWithdraw()
|
||||
chain.wait_for_receipt(tx)
|
||||
|
@ -305,13 +327,15 @@ def test_policy(web3, chain, policy_manager, user_escrow):
|
|||
assert user_balance + 10000 == web3.eth.getBalance(user)
|
||||
assert 0 == web3.eth.getBalance(user_escrow.address)
|
||||
|
||||
events = user_escrow.pastEvents('RewardWithdrawnAsMiner').get()
|
||||
events = miner_collections.get_all_entries()
|
||||
|
||||
assert 1 == len(events)
|
||||
event_args = events[0]['args']
|
||||
assert user.lower() == event_args['owner'].lower()
|
||||
assert user == event_args['owner']
|
||||
assert 10000 == event_args['value']
|
||||
events = user_escrow.pastEvents('RewardWithdrawn').get()
|
||||
|
||||
events = rewards.get_all_entries()
|
||||
assert 1 == len(events)
|
||||
event_args = events[0]['args']
|
||||
assert user.lower() == event_args['owner'].lower()
|
||||
assert user == event_args['owner']
|
||||
assert 10000 == event_args['value']
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
from cryptography.hazmat.primitives.asymmetric import ec
|
||||
from umbral.config import set_default_curve
|
||||
from .eth_fixtures import *
|
||||
from .fixtures import *
|
||||
|
||||
|
||||
set_default_curve(ec.SECP256K1())
|
||||
|
|
|
@ -2,6 +2,8 @@ import os
|
|||
import pytest
|
||||
import tempfile
|
||||
|
||||
from web3.contract import Contract
|
||||
|
||||
from nkms.blockchain.eth.agents import NuCypherKMSTokenAgent, MinerAgent
|
||||
from nkms.blockchain.eth.agents import PolicyAgent
|
||||
from nkms.blockchain.eth.chains import TheBlockchain, TesterBlockchain
|
||||
|
@ -10,7 +12,7 @@ from nkms.blockchain.eth.utilities import MockMinerEscrowDeployer
|
|||
from nkms.blockchain.eth.interfaces import Registrar, Provider
|
||||
|
||||
|
||||
@pytest.fixture(scope='session')
|
||||
@pytest.fixture(scope='module')
|
||||
def tester_registrar():
|
||||
_, filepath = tempfile.mkstemp()
|
||||
tester_registrar = Registrar(chain_name='tester', registrar_filepath=filepath)
|
||||
|
@ -18,13 +20,13 @@ def tester_registrar():
|
|||
os.remove(filepath)
|
||||
|
||||
|
||||
@pytest.fixture(scope='session')
|
||||
@pytest.fixture(scope='module')
|
||||
def tester_provider(tester_registrar):
|
||||
tester_provider = Provider(registrar=tester_registrar)
|
||||
yield tester_provider
|
||||
|
||||
|
||||
@pytest.fixture(scope='session')
|
||||
@pytest.fixture(scope='module')
|
||||
def chain(tester_provider):
|
||||
|
||||
chain = TesterBlockchain(provider=tester_provider)
|
||||
|
@ -34,7 +36,7 @@ def chain(tester_provider):
|
|||
TheBlockchain._TheBlockchain__instance = None
|
||||
|
||||
|
||||
@pytest.fixture(scope='session')
|
||||
@pytest.fixture(scope='module')
|
||||
def web3(chain):
|
||||
yield chain.provider.web3
|
||||
|
||||
|
@ -99,7 +101,7 @@ def escrow_contract(web3, chain, token):
|
|||
# Creator deploys the escrow
|
||||
|
||||
contract, _ = chain.provider.get_or_deploy_contract(
|
||||
'MinersEscrow', token.address, 1, int(8e7), 4, 4, 2, 100, 1e9
|
||||
'MinersEscrow', token.address, 1, int(8e7), 4, 4, 2, 100, int(1e9)
|
||||
)
|
||||
|
||||
dispatcher, _ = chain.provider.deploy_contract('Dispatcher', contract.address)
|
||||
|
|
Loading…
Reference in New Issue