2020-11-13 23:19:48 +01:00
|
|
|
import os
|
|
|
|
import unittest
|
|
|
|
import json
|
2020-11-16 19:19:13 +01:00
|
|
|
import logging
|
2020-11-13 23:19:48 +01:00
|
|
|
|
|
|
|
import web3
|
|
|
|
import eth_tester
|
|
|
|
|
2020-11-14 00:09:43 +01:00
|
|
|
from eth_accounts_index import AccountRegistry
|
2020-11-13 23:19:48 +01:00
|
|
|
|
2020-11-16 19:19:13 +01:00
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
logg = logging.getLogger()
|
|
|
|
|
2020-11-13 23:19:48 +01:00
|
|
|
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,
|
|
|
|
})
|
|
|
|
|
2020-12-07 16:41:48 +01:00
|
|
|
f = open(os.path.join(testdir, '../eth_accounts_index/data/AccountsIndex.bin'), 'r')
|
2020-11-13 23:19:48 +01:00
|
|
|
bytecode = f.read()
|
|
|
|
f.close()
|
|
|
|
|
2020-12-07 16:41:48 +01:00
|
|
|
f = open(os.path.join(testdir, '../eth_accounts_index/data/AccountsIndex.abi.json'), 'r')
|
2020-11-13 23:19:48 +01:00
|
|
|
abi = json.load(f)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
backend = eth_tester.PyEVMBackend(eth_params)
|
2020-11-16 19:19:13 +01:00
|
|
|
self.eth_tester = eth_tester.EthereumTester(backend)
|
|
|
|
provider = web3.Web3.EthereumTesterProvider(self.eth_tester)
|
2020-11-13 23:19:48 +01:00
|
|
|
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):
|
2020-11-14 00:09:43 +01:00
|
|
|
registry = AccountRegistry(self.w3, self.address)
|
2020-11-13 23:19:48 +01:00
|
|
|
self.assertEqual(registry.count(), 1); # count starts at 1, first addess is always 0x0
|
|
|
|
|
|
|
|
|
|
|
|
def test_access(self):
|
2020-11-14 00:09:43 +01:00
|
|
|
registry = AccountRegistry(self.w3, self.address, self.w3.eth.accounts[1])
|
2020-11-13 23:19:48 +01:00
|
|
|
registry.add(self.w3.eth.accounts[2])
|
|
|
|
self.eth_tester.mine_block()
|
|
|
|
self.assertEqual(registry.count(), 2)
|
|
|
|
|
2020-11-14 00:25:15 +01:00
|
|
|
# account 2 does not have access
|
2020-11-14 00:09:43 +01:00
|
|
|
registry = AccountRegistry(self.w3, self.address, self.w3.eth.accounts[2])
|
2020-11-13 23:19:48 +01:00
|
|
|
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):
|
2020-11-14 00:09:43 +01:00
|
|
|
registry = AccountRegistry(self.w3, self.address, self.w3.eth.accounts[1])
|
2020-11-13 23:19:48 +01:00
|
|
|
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]))
|
|
|
|
|
|
|
|
|
2020-11-16 19:19:13 +01:00
|
|
|
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()
|
|
|
|
|
|
|
|
|
2020-11-13 23:19:48 +01:00
|
|
|
def test_list(self):
|
2020-11-14 00:09:43 +01:00
|
|
|
registry = AccountRegistry(self.w3, self.address, self.w3.eth.accounts[1])
|
2020-11-13 23:19:48 +01:00
|
|
|
|
|
|
|
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()
|