Reimplement tests for stdlib unittest

This commit is contained in:
lash 2023-03-26 06:45:20 +01:00
parent 5a81927fa1
commit faa9e71bac
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
4 changed files with 68 additions and 52 deletions

View File

@ -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

View File

@ -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)

View File

@ -1,3 +1,4 @@
pytest==6.0.1
eth-tester==0.5.0b3
py-evm==0.3.0a20
eth-erc20~=0.7.2

View File

@ -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):
nonce_oracle = RPCNonceOracle(roles['CONTRACT_DEPLOYER'], eth_rpc)
builder = ContractRegistry(default_chain_spec, signer=eth_signer, nonce_oracle=nonce_oracle)
def setUp(self):
super(TestContractRegistry, self).setUp()
self.registry_ids = ['FOo', 'Bar', 'baz', 'XYZZY']
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)
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)
(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
(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
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)
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)
(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)
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))
def test_identifiers(self):
c = Registry(self.chain_spec)
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])
if __name__ == '__main__':
unittest.main()