eth-address-index

signed metadata declarations for ethereum addresses
Info | Log | Files | Refs

test_tokenendorser.py (2166B)


      1 import logging
      2 import os
      3 import json
      4 import hashlib
      5 
      6 import web3
      7 import eth_tester
      8 
      9 logging.basicConfig(level=logging.DEBUG)
     10 logg = logging.getLogger()
     11 
     12 
     13 eth_params = eth_tester.backends.pyevm.main.get_default_genesis_params({
     14     'gas_limit': 9000000,
     15     })
     16 backend = eth_tester.PyEVMBackend(eth_params)
     17 instance = eth_tester.EthereumTester(backend)
     18 provider = web3.Web3.EthereumTesterProvider(instance)
     19 w3 = web3.Web3(provider)
     20 
     21 
     22 f = open('TokenEndorser.bin', 'r')
     23 bytecode = f.read()
     24 f.close()
     25 
     26 f = open('TokenEndorser.json', 'r')
     27 abi = json.load(f)
     28 f.close()
     29 
     30 token_address = web3.Web3.toChecksumAddress('0x' + os.urandom(20).hex())
     31 
     32 c = w3.eth.contract(abi=abi, bytecode=bytecode)
     33 tx_hash = c.constructor().transact({'from': w3.eth.accounts[0]})
     34 r = w3.eth.getTransactionReceipt(tx_hash)
     35 logg.debug('contract {} initial token {}'.format(r.contractAddress, token_address))
     36 
     37 c = w3.eth.contract(abi=abi, address=r.contractAddress)
     38 d = '0x' + os.urandom(32).hex()
     39 
     40 # Initial token will fail in any case
     41 c.functions.add(token_address, d).transact({'from':w3.eth.accounts[0]})
     42 
     43 fail = False
     44 try:
     45     c.functions.add(token_address, d).transact({'from':w3.eth.accounts[0]})
     46 except:
     47     fail = True
     48     pass
     49 
     50 if not fail:
     51     raise RuntimeError('expected fail on register same token to same address')
     52 
     53 
     54 c.functions.add(token_address, d).transact({'from':w3.eth.accounts[1]})
     55 
     56 
     57 h = hashlib.new('sha256')
     58 h.update(bytes.fromhex(token_address[2:]))
     59 h.update(bytes.fromhex(w3.eth.accounts[0][2:]))
     60 z = h.digest()
     61 
     62 assert d[2:] == c.functions.endorsement(z.hex()).call().hex()
     63 
     64 
     65 another_token_address = web3.Web3.toChecksumAddress('0x' + os.urandom(20).hex())
     66 c.functions.add(another_token_address, d).transact({'from':w3.eth.accounts[0]})
     67 
     68 assert c.functions.endorsers(w3.eth.accounts[0], 0).call() == 1
     69 assert c.functions.endorsers(w3.eth.accounts[1], 0).call() == 1
     70 assert c.functions.endorsers(w3.eth.accounts[0], 1).call() == 2
     71 
     72 assert c.functions.tokens(1).call() == token_address
     73 assert c.functions.tokens(2).call() == another_token_address
     74 
     75 assert c.functions.tokenIndex(token_address).call() == 1
     76 assert c.functions.tokenIndex(another_token_address).call() == 2
     77 
     78