contract-registry

Ethereum Smart Contract key-value registry
Log | Files | Refs

commit faa9e71bac788e5d83049cb62f98d96cddaf6cc1
parent 5a81927fa1d974fa5cbfbd1b72d80310224c824a
Author: lash <dev@holbrook.no>
Date:   Sun, 26 Mar 2023 06:45:20 +0100

Reimplement tests for stdlib unittest

Diffstat:
Mpython/eth_contract_registry/interface.py | 4++--
Mpython/eth_contract_registry/registry.py | 2++
Mpython/test_requirements.txt | 1+
Mpython/tests/test_basic.py | 113++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
4 files changed, 68 insertions(+), 52 deletions(-)

diff --git a/python/eth_contract_registry/interface.py b/python/eth_contract_registry/interface.py @@ -50,7 +50,7 @@ class Registry(TxFactory): enc = ABIContractEncoder() enc.method('identifier') enc.typ(ABIContractType.UINT256) - enc.bytes32(idx) + enc.uint256(idx) data = add_0x(enc.encode()) tx = self.template(sender_address, contract_address) tx = self.set_code(tx, data) @@ -60,7 +60,7 @@ class Registry(TxFactory): def identifier_count(self, contract_address, sender_address=ZERO_ADDRESS, id_generator=None): - return self.call_noarg('identifierCount', contract_address, sender_address=ZERO_ADDRESS, id_generator=None) + return self.call_noarg('identifierCount', contract_address, sender_address=sender_address, id_generator=id_generator) @classmethod diff --git a/python/eth_contract_registry/registry.py b/python/eth_contract_registry/registry.py @@ -94,6 +94,8 @@ class ContractRegistry(Registry): def set(self, contract_address, sender_address, identifier_string, address): + if len(identifier_string) > 32: + raise ValueError('String too long') enc = ABIContractEncoder() enc.method('set') enc.typ(ABIContractType.BYTES32) diff --git a/python/test_requirements.txt b/python/test_requirements.txt @@ -1,3 +1,4 @@ pytest==6.0.1 eth-tester==0.5.0b3 py-evm==0.3.0a20 +eth-erc20~=0.7.2 diff --git a/python/tests/test_basic.py b/python/tests/test_basic.py @@ -1,73 +1,86 @@ # standard imports import os +import unittest +import json +import logging +import hashlib # external imports -import logging -import pytest -from chainlib.eth.tx import ( - receipt, - transaction, - ) -from chainlib.connection import RPCConnection +from chainlib.eth.unittest.ethtester import EthTesterCase from chainlib.eth.contract import ( ABIContractEncoder, ABIContractType, - abi_decode_single, ) from chainlib.eth.nonce import RPCNonceOracle -from chainlib.eth.address import to_checksum_address +from chainlib.eth.tx import receipt +from giftable_erc20_token import GiftableToken from hexathon import ( - add_0x, - strip_0x, - ) + add_0x, + strip_0x, + same as same_hex, + ) # local imports -from eth_contract_registry import Registry from eth_contract_registry.registry import ContractRegistry -from eth_contract_registry.encoding import from_identifier_hex -from eth_contract_registry.pytest.fixtures_registry import valid_identifiers +from eth_contract_registry import Registry logging.basicConfig(level=logging.DEBUG) logg = logging.getLogger() -valid_identifiers += [ - 'FooContract', - ] -def test_set( - default_chain_spec, - registry, - eth_accounts, - eth_rpc, - eth_signer, - roles, - ): - - addr_registry = to_checksum_address(os.urandom(20).hex()) - addr_foo = to_checksum_address(os.urandom(20).hex()) - bogus_hash = add_0x(os.urandom(32).hex()) +class TestContractRegistry(EthTesterCase): + + def setUp(self): + super(TestContractRegistry, self).setUp() + self.registry_ids = ['FOo', 'Bar', 'baz', 'XYZZY'] + + nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) + c = ContractRegistry(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + (tx_hash_hex, o) = c.constructor(self.accounts[0], self.registry_ids) + self.rpc.do(o) + o = receipt(tx_hash_hex) + rcpt = self.rpc.do(o) + self.assertEqual(rcpt['status'], 1) + self.address = rcpt['contract_address'] + logg.info('registry published to ' + self.address) + + + def test_retrieve(self): + nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) + c = ContractRegistry(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + (tx_hash_hex, o) = c.set(self.address, self.accounts[0], 'FOO', self.address) + r = self.rpc.do(o) + o = receipt(tx_hash_hex) + r = self.rpc.do(o) + self.assertEqual(r['status'], 0) + + (tx_hash_hex, o) = c.set(self.address, self.accounts[0], 'FOo', self.address) + r = self.rpc.do(o) + o = receipt(tx_hash_hex) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) - nonce_oracle = RPCNonceOracle(roles['CONTRACT_DEPLOYER'], eth_rpc) - builder = ContractRegistry(default_chain_spec, signer=eth_signer, nonce_oracle=nonce_oracle) + c = ContractRegistry(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + o = c.address_of(self.address, 'FOo', sender_address=self.accounts[0]) + r = self.rpc.do(o) + self.assertTrue(same_hex(strip_0x(r)[24:], self.address)) - o = builder.address_of(registry, 'ContractRegistry', sender_address=eth_accounts[0]) - r = eth_rpc.do(o) - r = abi_decode_single(ABIContractType.ADDRESS, r) - assert r == strip_0x(registry) - (tx_hash_hex, o) = builder.set(registry, roles['CONTRACT_DEPLOYER'], 'ContractRegistry', addr_registry) - r = eth_rpc.do(o) - o = receipt(r) - rcpt = eth_rpc.do(o) - assert rcpt['status'] == 0 + def test_identifiers(self): + c = Registry(self.chain_spec) - (tx_hash_hex, o) = builder.set(registry, roles['CONTRACT_DEPLOYER'], 'FooContract', addr_foo) - r = eth_rpc.do(o) - o = receipt(r) - rcpt = eth_rpc.do(o) - assert rcpt['status'] == 1 + o = c.identifier_count(self.address, sender_address=self.accounts[0]) + r = self.rpc.do(o) + self.assertEqual(int(r, 16), 4) + + for i in range(4): + o = c.identifier(self.address, i, sender_address=self.accounts[0]) + r = self.rpc.do(o) + r = bytes.fromhex(strip_0x(r)) + r = r.strip(b'\x00') + s = r.decode('utf-8') + self.assertEqual(s, self.registry_ids[i]) + - builder = Registry(default_chain_spec) - o = builder.address_of(registry, 'FooContract', sender_address=eth_accounts[0]) - r = eth_rpc.do(o) - r = abi_decode_single(ABIContractType.ADDRESS, r) +if __name__ == '__main__': + unittest.main()