contract-registry

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

test_basic.py (2781B)


      1 # standard imports
      2 import os
      3 import unittest
      4 import json
      5 import logging
      6 import hashlib
      7 
      8 # external imports
      9 from chainlib.eth.unittest.ethtester import EthTesterCase
     10 from chainlib.eth.contract import (
     11         ABIContractEncoder,
     12         ABIContractType,
     13         )
     14 from chainlib.eth.nonce import RPCNonceOracle
     15 from chainlib.eth.tx import receipt
     16 from giftable_erc20_token import GiftableToken
     17 from hexathon import (
     18     add_0x,
     19     strip_0x,
     20     same as same_hex,
     21     )
     22 
     23 # local imports
     24 from eth_contract_registry.registry import ContractRegistry
     25 from eth_contract_registry import Registry
     26 
     27 logging.basicConfig(level=logging.DEBUG)
     28 logg = logging.getLogger()
     29 
     30 
     31 class TestContractRegistry(EthTesterCase):
     32 
     33     def setUp(self):
     34         super(TestContractRegistry, self).setUp()
     35         self.registry_ids = ['FOo', 'Bar', 'baz', 'XYZZY']
     36 
     37         nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
     38         c = ContractRegistry(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     39         (tx_hash_hex, o) = c.constructor(self.accounts[0], self.registry_ids)
     40         self.rpc.do(o)
     41         o = receipt(tx_hash_hex)
     42         rcpt = self.rpc.do(o)
     43         self.assertEqual(rcpt['status'], 1)
     44         self.address = rcpt['contract_address'] 
     45         logg.info('registry published to ' + self.address)
     46 
     47 
     48     def test_retrieve(self):
     49         nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
     50         c = ContractRegistry(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     51         (tx_hash_hex, o) = c.set(self.address, self.accounts[0], 'FOO', self.address)
     52         r = self.rpc.do(o)
     53         o = receipt(tx_hash_hex)
     54         r = self.rpc.do(o)
     55         self.assertEqual(r['status'], 0)
     56 
     57         (tx_hash_hex, o) = c.set(self.address, self.accounts[0], 'FOo', self.address)
     58         r = self.rpc.do(o)
     59         o = receipt(tx_hash_hex)
     60         r = self.rpc.do(o)
     61         self.assertEqual(r['status'], 1)
     62 
     63         c = ContractRegistry(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     64         o = c.address_of(self.address, 'FOo', sender_address=self.accounts[0])
     65         r = self.rpc.do(o)
     66         self.assertTrue(same_hex(strip_0x(r)[24:], self.address))
     67 
     68 
     69     def test_identifiers(self):
     70         c = Registry(self.chain_spec)
     71 
     72         o = c.identifier_count(self.address, sender_address=self.accounts[0])
     73         r = self.rpc.do(o)
     74         self.assertEqual(int(r, 16), 4)
     75             
     76         for i in range(4):
     77             o = c.identifier(self.address, i, sender_address=self.accounts[0])
     78             r = self.rpc.do(o)
     79             r = bytes.fromhex(strip_0x(r))
     80             r = r.strip(b'\x00')
     81             s = r.decode('utf-8')
     82             self.assertEqual(s, self.registry_ids[i])
     83     
     84 
     85 if __name__ == '__main__':
     86     unittest.main()