mirror of
git://holbrook.no/eth-accounts-index
synced 2026-04-28 04:21:04 +02:00
Lash/chainlib
This commit is contained in:
@@ -1,11 +1,24 @@
|
||||
# standard imports
|
||||
import os
|
||||
import unittest
|
||||
import json
|
||||
import logging
|
||||
|
||||
import web3
|
||||
import eth_tester
|
||||
# external imports
|
||||
from chainlib.eth.unittest.ethtester import EthTesterCase
|
||||
from chainlib.connection import RPCConnection
|
||||
from chainlib.eth.address import to_checksum_address
|
||||
from chainlib.eth.tx import (
|
||||
receipt,
|
||||
transaction,
|
||||
TxFormat,
|
||||
)
|
||||
from chainlib.eth.contract import (
|
||||
abi_decode_single,
|
||||
ABIContractType,
|
||||
)
|
||||
|
||||
# local imports
|
||||
from eth_accounts_index import AccountRegistry
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
@@ -14,106 +27,138 @@ logg = logging.getLogger()
|
||||
testdir = os.path.dirname(__file__)
|
||||
|
||||
|
||||
class Test(unittest.TestCase):
|
||||
class TestNonceOracle:
|
||||
|
||||
def __init__(self, address, default_value=0):
|
||||
self.nonce = default_value
|
||||
|
||||
|
||||
def next_nonce(self):
|
||||
nonce = self.nonce
|
||||
self.nonce += 1
|
||||
return nonce
|
||||
|
||||
|
||||
class Test(EthTesterCase):
|
||||
|
||||
def setUp(self):
|
||||
eth_params = eth_tester.backends.pyevm.main.get_default_genesis_params({
|
||||
'gas_limit': 9000000,
|
||||
})
|
||||
super(Test, self).setUp()
|
||||
nonce_oracle = TestNonceOracle(self.accounts[0])
|
||||
self.o = AccountRegistry(signer=self.signer, nonce_oracle=nonce_oracle)
|
||||
(tx_hash, o) = self.o.constructor(self.accounts[0])
|
||||
self.conn = RPCConnection.connect(self.chain_spec, 'default')
|
||||
r = self.conn.do(o)
|
||||
logg.debug('deployed with hash {}'.format(r))
|
||||
|
||||
o = receipt(r)
|
||||
r = self.conn.do(o)
|
||||
self.address = to_checksum_address(r['contract_address'])
|
||||
|
||||
f = open(os.path.join(testdir, '../eth_accounts_index/data/AccountsIndex.bin'), 'r')
|
||||
bytecode = f.read()
|
||||
f.close()
|
||||
(tx_hash, o) = self.o.add_writer(self.address, self.accounts[0], self.accounts[0])
|
||||
r = self.conn.do(o)
|
||||
|
||||
f = open(os.path.join(testdir, '../eth_accounts_index/data/AccountsIndex.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()
|
||||
o = receipt(r)
|
||||
r = self.conn.do(o)
|
||||
self.assertEqual(r['status'], 1)
|
||||
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
def test_1_count(self):
|
||||
o = self.o.count(self.address, sender_address=self.accounts[0])
|
||||
r = self.conn.do(o)
|
||||
r = abi_decode_single(ABIContractType.UINT256, r)
|
||||
self.assertEqual(r, 1)
|
||||
|
||||
|
||||
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_2_add(self):
|
||||
b = os.urandom(20)
|
||||
a = to_checksum_address(b.hex())
|
||||
|
||||
(tx_hash, o) = self.o.add(self.address, self.accounts[0], a)
|
||||
r = self.conn.do(o)
|
||||
self.assertEqual(tx_hash, r)
|
||||
|
||||
o = receipt(tx_hash)
|
||||
rcpt = self.conn.do(o)
|
||||
|
||||
self.helper.mine_block()
|
||||
o = self.o.have(self.address, a, sender_address=self.accounts[0])
|
||||
r = self.conn.do(o)
|
||||
|
||||
|
||||
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)
|
||||
def test_3_add_rlpsigned(self):
|
||||
b = os.urandom(20)
|
||||
a = to_checksum_address(b.hex())
|
||||
|
||||
# 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)
|
||||
(tx_hash, o) = self.o.add(self.address, self.accounts[0], a, tx_format=TxFormat.RLP_SIGNED)
|
||||
#r = self.conn.do(o)
|
||||
#self.assertEqual(tx_hash, r)
|
||||
logg.debug('o {}'.format(o))
|
||||
|
||||
|
||||
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])
|
||||
|
||||
# TODO: reinstate all tests
|
||||
# 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()
|
||||
|
||||
Reference in New Issue
Block a user