eth-address-index

signed metadata declarations for ethereum addresses
Log | Files | Refs

test.py (3867B)


      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('AddressDeclarator.bin', 'r')
     23 bytecode = f.read()
     24 f.close()
     25 
     26 f = open('AddressDeclarator.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 
     35 declarations = [
     36     ['0x' + os.urandom(32).hex(), '0x' + os.urandom(32).hex()],
     37     ['0x' + os.urandom(32).hex(), '0x' + os.urandom(32).hex()],
     38     ['0x' + os.urandom(32).hex(), '0x' + os.urandom(32).hex()],
     39     ['0x' + os.urandom(32).hex(), '0x' + os.urandom(32).hex()],
     40         ]
     41 
     42 # Deployment is a self-signed declaration
     43 tx_hash = c.constructor(declarations[0][0]).transact({'from': w3.eth.accounts[0]})
     44 r = w3.eth.getTransactionReceipt(tx_hash)
     45 logg.debug('contract {}'.format(r.contractAddress))
     46 
     47 c = w3.eth.contract(abi=abi, address=r.contractAddress)
     48 
     49 r = c.functions.declaratorCount(w3.eth.accounts[0]).call()
     50 assert r == 1
     51 
     52 r = c.functions.declaratorAddressAt(w3.eth.accounts[0], 0).call()
     53 assert r == w3.eth.accounts[0]
     54 
     55 r = c.functions.declaration(w3.eth.accounts[0], w3.eth.accounts[0]).call()
     56 assert r[0].hex() == declarations[0][0][2:]
     57 
     58 
     59 # Add first declaration for 0 by 2
     60 c.functions.addDeclaration(w3.eth.accounts[0], declarations[1][0]).transact({'from': w3.eth.accounts[2]})
     61 
     62 r = c.functions.declaratorCount(w3.eth.accounts[0]).call()
     63 assert r == 2
     64 
     65 r = c.functions.declaratorAddressAt(w3.eth.accounts[0], 1).call()
     66 assert r == w3.eth.accounts[2]
     67 
     68 r = c.functions.declaration(w3.eth.accounts[2], w3.eth.accounts[0]).call()
     69 assert r[0].hex() == declarations[1][0][2:]
     70 
     71 
     72 # Add second declaration for 0 by 2
     73 c.functions.addDeclaration(w3.eth.accounts[0], declarations[1][1]).transact({'from': w3.eth.accounts[2]})
     74 
     75 r = c.functions.declaratorCount(w3.eth.accounts[0]).call()
     76 assert r == 2
     77 
     78 
     79 r = c.functions.declaration(w3.eth.accounts[2], w3.eth.accounts[0]).call()
     80 assert r[0].hex() == declarations[1][0][2:]
     81 assert r[1].hex() == declarations[1][1][2:]
     82 
     83 
     84 # Add first declaration for 1 by 2
     85 c.functions.addDeclaration(w3.eth.accounts[1], declarations[2][0]).transact({'from': w3.eth.accounts[2]})
     86 
     87 r = c.functions.declaratorCount(w3.eth.accounts[0]).call()
     88 assert r == 2
     89 
     90 r = c.functions.declaratorCount(w3.eth.accounts[1]).call()
     91 assert r == 1
     92 
     93 r = c.functions.declaratorAddressAt(w3.eth.accounts[1], 0).call()
     94 assert r == w3.eth.accounts[2]
     95 
     96 r = c.functions.declaration(w3.eth.accounts[2], w3.eth.accounts[1]).call()
     97 assert r[0].hex() == declarations[2][0][2:]
     98 
     99 
    100 # Add declaration for 0 by 3
    101 c.functions.addDeclaration(w3.eth.accounts[0], declarations[3][0]).transact({'from': w3.eth.accounts[3]})
    102 
    103 
    104 # 0 declared itself and 1
    105 r = c.functions.declarationCount(w3.eth.accounts[0]).call()
    106 assert r == 1
    107 
    108 # 0 was declared by itself, 1 and 3
    109 r = c.functions.declaratorCount(w3.eth.accounts[0]).call()
    110 assert r == 3
    111 
    112 # 1 declared noone
    113 r = c.functions.declarationCount(w3.eth.accounts[1]).call()
    114 assert r == 0
    115 
    116 # 1 was declared by 2
    117 r = c.functions.declaratorCount(w3.eth.accounts[1]).call()
    118 assert r == 1
    119 
    120 # 2 declared 0 and 1
    121 r = c.functions.declarationCount(w3.eth.accounts[2]).call()
    122 assert r == 2
    123 
    124 # 2 was declared by noone
    125 r = c.functions.declaratorCount(w3.eth.accounts[2]).call()
    126 assert r == 0
    127 
    128 # 3 declared 0
    129 r = c.functions.declarationCount(w3.eth.accounts[3]).call()
    130 assert r == 1
    131 
    132 # 3 was declared by noone
    133 r = c.functions.declaratorCount(w3.eth.accounts[3]).call()
    134 assert r == 0