From 6a6b6b59d8d5af73b458c0415c557554372789f6 Mon Sep 17 00:00:00 2001 From: nolash Date: Tue, 16 Mar 2021 15:35:22 +0100 Subject: [PATCH] Assimilate agnostic rpc in base tests --- apps/cic-eth/tests/conftest.py | 3 +- apps/cic-eth/tests/fixtures_contracts.py | 48 ++++++++++++++++++++---- apps/cic-eth/tests/fixtures_registry.py | 29 -------------- apps/cic-eth/tests/test_chainlib.py | 24 ++++++------ apps/cic-eth/tests/test_registry.py | 37 +++++++++++++++++- 5 files changed, 91 insertions(+), 50 deletions(-) delete mode 100644 apps/cic-eth/tests/fixtures_registry.py diff --git a/apps/cic-eth/tests/conftest.py b/apps/cic-eth/tests/conftest.py index 22eda579..c6d1260a 100644 --- a/apps/cic-eth/tests/conftest.py +++ b/apps/cic-eth/tests/conftest.py @@ -11,5 +11,6 @@ from tests.fixtures_config import * from tests.fixtures_celery import * from tests.fixtures_database import * from tests.fixtures_contracts import * +from tests.fixtures_role import * from chainlib.eth.pytest import * -from cic_registry.pytest import * +from contract_registry.pytest import * diff --git a/apps/cic-eth/tests/fixtures_contracts.py b/apps/cic-eth/tests/fixtures_contracts.py index 66e147f2..6cce2fee 100644 --- a/apps/cic-eth/tests/fixtures_contracts.py +++ b/apps/cic-eth/tests/fixtures_contracts.py @@ -1,4 +1,5 @@ # standard imports +import logging import json import hashlib @@ -7,23 +8,48 @@ import pytest from eth_accounts_index import AccountRegistry from chainlib.eth.nonce import NodeNonceOracle from hexathon import add_0x +from chainlib.eth.tx import receipt # local imports -from cic_registry.registry import Registry -from cic_registry.encoding import to_identifier +from contract_registry.registry import Registry +from contract_registry.encoding import to_identifier +from contract_registry.pytest.fixtures_registry import valid_identifiers + +logg = logging.getLogger(__name__) + +valid_identifiers += [ + 'AccountRegistry', + ] + @pytest.fixture(scope='function') -def accounts_registry( +def account_registry( registry, eth_signer, + eth_accounts, eth_rpc, - accounts, default_chain_spec, default_chain_config, + roles, ): - nonce_oracle = NodeNonceOracle(accounts[0], eth_rpc) - c = Registry(signer=eth_rpc.signer, nonce_oracle=nonce_oracle) + nonce_oracle = NodeNonceOracle(roles['CONTRACT_DEPLOYER'], eth_rpc) + + c = AccountRegistry(signer=eth_signer, nonce_oracle=nonce_oracle) + (tx_hash_hex, o) = c.constructor(roles['CONTRACT_DEPLOYER']) + r = eth_rpc.do(o) + o = receipt(tx_hash_hex) + r = eth_rpc.do(o) + assert r['status'] == 1 + account_registry_address = r['contract_address'] + + (tx_hash_hex, o) = c.add_writer(account_registry_address, roles['CONTRACT_DEPLOYER'], roles['ACCOUNT_REGISTRY_WRITER']) + r = eth_rpc.do(o) + o = receipt(tx_hash_hex) + r = eth_rpc.do(o) + assert r['status'] == 1 + + c = Registry(signer=eth_signer, nonce_oracle=nonce_oracle) chain_spec_identifier = to_identifier(str(default_chain_spec)) @@ -32,4 +58,12 @@ def accounts_registry( h.update(j.encode('utf-8')) z = h.digest() chain_config_digest = add_0x(z.hex()) - o = c.set(registry, accounts[0], 'AccountsRegistry', accounts[1], chain_spec_identifier, chain_config_digest) + (tx_hash_hex, o) = c.set(registry, roles['CONTRACT_DEPLOYER'], 'AccountRegistry', account_registry_address, chain_spec_identifier, chain_config_digest) + r = eth_rpc.do(o) + o = receipt(tx_hash_hex) + r = eth_rpc.do(o) + assert r['status'] == 1 + + logg.info('accounts registry deployed: {}'.format(account_registry_address)) + return account_registry_address + diff --git a/apps/cic-eth/tests/fixtures_registry.py b/apps/cic-eth/tests/fixtures_registry.py deleted file mode 100644 index 7f0feece..00000000 --- a/apps/cic-eth/tests/fixtures_registry.py +++ /dev/null @@ -1,29 +0,0 @@ -# external imports -import pytest -from cic_registry import CICRegistry -from chainlib.chain import ChainSpec -from eth_accounts_index import AccountRegistry - -@pytest.fixture(scope='session') -def default_chain_spec(): - return ChainSpec('evm', 'bloxberg', 8996) - - -@pytest.fixture(scope='session') -def accounts_registry( - default_chain_spec, - cic_registry, - ): - #abi = AccountRegistry.abi() - #constructor = w3.eth.contract(abi=abi, bytecode=AccountRegistry.bytecode()) - #tx_hash = constructor.constructor().transact() - r = w3.eth.getTransactionReceipt(tx_hash) - logg.debug('accounts registry deployed {}'.format(r.contractAddress)) - account_registry = AccountRegistry(w3, r.contractAddress) - - c = w3.eth.contract(abi=abi, address=r.contractAddress) - c.functions.addWriter(w3.eth.accounts[0]).transact() - - CICRegistry.add_contract(default_chain_spec, c, 'AccountRegistry') - - return account_registry diff --git a/apps/cic-eth/tests/test_chainlib.py b/apps/cic-eth/tests/test_chainlib.py index 5d862049..e08490e8 100644 --- a/apps/cic-eth/tests/test_chainlib.py +++ b/apps/cic-eth/tests/test_chainlib.py @@ -2,7 +2,7 @@ import logging # external imports -from chainlib.eth.connection import RPCConnection +from chainlib.connection import RPCConnection from chainlib.eth.gas import ( balance, price, @@ -19,31 +19,33 @@ logg = logging.getLogger(__name__) def test_init_eth_tester( - accounts, + default_chain_spec, + eth_accounts, init_eth_tester, - init_rpc, + eth_rpc, ): - conn = RPCConnection.connect() - o = balance(accounts[0]) + conn = RPCConnection.connect(default_chain_spec, 'default') + o = balance(eth_accounts[0]) conn.do(o) o = price() conn.do(o) - o = count_pending(accounts[0]) + o = count_pending(eth_accounts[0]) conn.do(o) - o = count_confirmed(accounts[0]) + o = count_confirmed(eth_accounts[0]) conn.do(o) def test_signer( + default_chain_spec, init_eth_tester, - init_rpc, - accounts, + eth_rpc, + eth_accounts, ): - o = sign_message(accounts[0], '0x2a') - conn = RPCConnection.connect('signer') + o = sign_message(eth_accounts[0], '0x2a') + conn = RPCConnection.connect(default_chain_spec, 'signer') r = conn.do(o) diff --git a/apps/cic-eth/tests/test_registry.py b/apps/cic-eth/tests/test_registry.py index c7c35797..eb21da08 100644 --- a/apps/cic-eth/tests/test_registry.py +++ b/apps/cic-eth/tests/test_registry.py @@ -1,4 +1,37 @@ +# standard imports +import logging + +# external imports +from chainlib.eth.nonce import NodeNonceOracle +from chainlib.eth.tx import receipt +from eth_accounts_index import AccountRegistry +from contract_registry.registry import ( + Registry, + ) + +logg = logging.getLogger() + + def test_registry( - accounts_registry, + eth_rpc, + eth_signer, + registry, + account_registry, + roles, + eth_empty_accounts, ): - pass + + nonce_oracle = NodeNonceOracle(roles['DEFAULT'], eth_rpc) + c = Registry(signer=eth_signer, nonce_oracle=nonce_oracle) + o = c.address_of(registry, 'AccountRegistry', roles['DEFAULT']) + r = eth_rpc.do(o) + r = c.parse_address_of(r) + assert account_registry == r + + nonce_oracle = NodeNonceOracle(roles['ACCOUNT_REGISTRY_WRITER'], eth_rpc) + c = AccountRegistry(signer=eth_signer, nonce_oracle=nonce_oracle) + (tx_hash_hex, o) = c.add(account_registry, roles['ACCOUNT_REGISTRY_WRITER'], eth_empty_accounts[0]) + r = eth_rpc.do(o) + o = receipt(tx_hash_hex) + r = eth_rpc.do(o) + assert r['status'] == 1