import os import unittest import json import logging import web3 import eth_tester from eth_accounts_index import AccountRegistry logging.basicConfig(level=logging.DEBUG) logg = logging.getLogger() testdir = os.path.dirname(__file__) class Test(unittest.TestCase): def setUp(self): eth_params = eth_tester.backends.pyevm.main.get_default_genesis_params({ 'gas_limit': 9000000, }) f = open(os.path.join(testdir, '../eth_accounts_index/data/registry.bin'), 'r') bytecode = f.read() f.close() f = open(os.path.join(testdir, '../eth_accounts_index/data/registry.abi.json'), 'r') abi = json.load(f) f.close() backend = eth_tester.PyEVMBackend(eth_params) self.eth_tester = eth_tester.EthereumTester(backend) provider = web3.Web3.EthereumTesterProvider(self.eth_tester) self.w3 = web3.Web3(provider) c = self.w3.eth.contract(abi=abi, bytecode=bytecode) tx_hash = c.constructor().transact({'from': self.w3.eth.accounts[0]}) r = self.w3.eth.getTransactionReceipt(tx_hash) self.address = r.contractAddress c = self.w3.eth.contract(abi=abi, address=self.address) c.functions.addWriter(self.w3.eth.accounts[1]).transact() def tearDown(self): pass def test_basic(self): registry = AccountRegistry(self.w3, self.address) self.assertEqual(registry.count(), 1); # count starts at 1, first addess is always 0x0 def test_access(self): registry = AccountRegistry(self.w3, self.address, self.w3.eth.accounts[1]) registry.add(self.w3.eth.accounts[2]) self.eth_tester.mine_block() self.assertEqual(registry.count(), 2) # account 2 does not have access registry = AccountRegistry(self.w3, self.address, self.w3.eth.accounts[2]) registry.add(self.w3.eth.accounts[2]) self.eth_tester.mine_block() self.assertEqual(registry.count(), 2) # after this account 2 has access registry.contract.functions.addWriter(self.w3.eth.accounts[2]).transact() registry.add(self.w3.eth.accounts[3]) self.eth_tester.mine_block() self.assertEqual(registry.count(), 3) # after this account 2 no longer has access registry.contract.functions.deleteWriter(self.w3.eth.accounts[2]).transact() registry.add(self.w3.eth.accounts[3]) self.eth_tester.mine_block() self.assertEqual(registry.count(), 3) def test_indices(self): registry = AccountRegistry(self.w3, self.address, self.w3.eth.accounts[1]) registry.add(self.w3.eth.accounts[2]) self.assertTrue(registry.have(self.w3.eth.accounts[2])) self.assertFalse(registry.have(self.w3.eth.accounts[3])) def test_no_duplicates(self): registry = AccountRegistry(self.w3, self.address, self.w3.eth.accounts[1]) tx_hash = registry.add(self.w3.eth.accounts[2]) self.eth_tester.mine_block() tx_hash = registry.add(self.w3.eth.accounts[3]) self.eth_tester.mine_block() # BUG: eth_tester does not detect the duplicate here, but does in the test.py file in the solidity folder #self.assertRaises(Exception): tx_hash = registry.add(self.w3.eth.accounts[2]) self.eth_tester.mine_block() def test_list(self): registry = AccountRegistry(self.w3, self.address, self.w3.eth.accounts[1]) for i in range(2, 10): registry.add(self.w3.eth.accounts[i]) self.assertEqual(registry.count(), 9) accounts_reverse = [] for i in range(9, 1, -1): accounts_reverse.append(self.w3.eth.accounts[i]) accounts_list = registry.last(8) for i in range(8): self.assertEqual(accounts_list[i], accounts_reverse[i]) if __name__ == '__main__': unittest.main()