eth-address-index/solidity/test.py

135 lines
3.8 KiB
Python
Raw Normal View History

2020-12-21 23:00:45 +01:00
import logging
import os
import json
import hashlib
import web3
import eth_tester
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
eth_params = eth_tester.backends.pyevm.main.get_default_genesis_params({
'gas_limit': 9000000,
})
backend = eth_tester.PyEVMBackend(eth_params)
instance = eth_tester.EthereumTester(backend)
provider = web3.Web3.EthereumTesterProvider(instance)
w3 = web3.Web3(provider)
2020-12-29 16:08:49 +01:00
f = open('AddressDeclarator.bin', 'r')
2020-12-21 23:00:45 +01:00
bytecode = f.read()
f.close()
2020-12-29 16:08:49 +01:00
f = open('AddressDeclarator.json', 'r')
2020-12-21 23:00:45 +01:00
abi = json.load(f)
f.close()
2020-12-29 16:08:49 +01:00
#token_address = web3.Web3.toChecksumAddress('0x' + os.urandom(20).hex())
2020-12-21 23:00:45 +01:00
c = w3.eth.contract(abi=abi, bytecode=bytecode)
2020-12-29 16:08:49 +01:00
#tx_hash = c.constructor().transact({'from': w3.eth.accounts[0]})
declarations = [
['0x' + os.urandom(32).hex(), '0x' + os.urandom(32).hex()],
['0x' + os.urandom(32).hex(), '0x' + os.urandom(32).hex()],
2020-12-29 19:44:14 +01:00
['0x' + os.urandom(32).hex(), '0x' + os.urandom(32).hex()],
2020-12-29 16:08:49 +01:00
['0x' + os.urandom(32).hex(), '0x' + os.urandom(32).hex()],
]
# Deployment is a self-signed declaration
2020-12-29 17:33:00 +01:00
tx_hash = c.constructor(declarations[0][0]).transact({'from': w3.eth.accounts[0]})
2020-12-21 23:00:45 +01:00
r = w3.eth.getTransactionReceipt(tx_hash)
2020-12-29 16:08:49 +01:00
logg.debug('contract {}'.format(r.contractAddress))
2020-12-21 23:00:45 +01:00
c = w3.eth.contract(abi=abi, address=r.contractAddress)
2020-12-29 16:08:49 +01:00
r = c.functions.declaratorCount(w3.eth.accounts[0]).call()
assert r == 1
2020-12-29 19:44:14 +01:00
r = c.functions.declaratorAddressAt(w3.eth.accounts[0], 0).call()
2020-12-29 16:08:49 +01:00
assert r == w3.eth.accounts[0]
r = c.functions.declaration(w3.eth.accounts[0], w3.eth.accounts[0]).call()
assert r[0].hex() == declarations[0][0][2:]
# Add first declaration for 0 by 2
c.functions.addDeclaration(w3.eth.accounts[0], declarations[1][0]).transact({'from': w3.eth.accounts[2]})
r = c.functions.declaratorCount(w3.eth.accounts[0]).call()
assert r == 2
2020-12-21 23:00:45 +01:00
2020-12-29 19:44:14 +01:00
r = c.functions.declaratorAddressAt(w3.eth.accounts[0], 1).call()
2020-12-29 16:08:49 +01:00
assert r == w3.eth.accounts[2]
2020-12-21 23:00:45 +01:00
2020-12-29 16:08:49 +01:00
r = c.functions.declaration(w3.eth.accounts[2], w3.eth.accounts[0]).call()
assert r[0].hex() == declarations[1][0][2:]
2020-12-21 23:00:45 +01:00
2020-12-29 16:08:49 +01:00
# Add second declaration for 0 by 2
c.functions.addDeclaration(w3.eth.accounts[0], declarations[1][1]).transact({'from': w3.eth.accounts[2]})
2020-12-21 23:00:45 +01:00
2020-12-29 16:08:49 +01:00
r = c.functions.declaratorCount(w3.eth.accounts[0]).call()
assert r == 2
2020-12-21 23:00:45 +01:00
2020-12-29 16:08:49 +01:00
r = c.functions.declaration(w3.eth.accounts[2], w3.eth.accounts[0]).call()
assert r[0].hex() == declarations[1][0][2:]
assert r[1].hex() == declarations[1][1][2:]
2020-12-21 23:00:45 +01:00
2020-12-29 16:08:49 +01:00
# Add first declaration for 1 by 2
c.functions.addDeclaration(w3.eth.accounts[1], declarations[2][0]).transact({'from': w3.eth.accounts[2]})
2020-12-21 23:00:45 +01:00
2020-12-29 16:08:49 +01:00
r = c.functions.declaratorCount(w3.eth.accounts[0]).call()
assert r == 2
2020-12-21 23:00:45 +01:00
2020-12-29 16:08:49 +01:00
r = c.functions.declaratorCount(w3.eth.accounts[1]).call()
assert r == 1
2020-12-21 23:00:45 +01:00
2020-12-29 19:44:14 +01:00
r = c.functions.declaratorAddressAt(w3.eth.accounts[1], 0).call()
2020-12-29 16:08:49 +01:00
assert r == w3.eth.accounts[2]
2020-12-21 23:00:45 +01:00
2020-12-29 16:08:49 +01:00
r = c.functions.declaration(w3.eth.accounts[2], w3.eth.accounts[1]).call()
assert r[0].hex() == declarations[2][0][2:]
2020-12-21 23:00:45 +01:00
2020-12-29 19:44:14 +01:00
# Add declaration for 0 by 3
c.functions.addDeclaration(w3.eth.accounts[0], declarations[3][0]).transact({'from': w3.eth.accounts[3]})
# 0 declared itself and 1
r = c.functions.declarationCount(w3.eth.accounts[0]).call()
assert r == 1
# 0 was declared by itself, 1 and 3
r = c.functions.declaratorCount(w3.eth.accounts[0]).call()
assert r == 3
# 1 declared noone
r = c.functions.declarationCount(w3.eth.accounts[1]).call()
assert r == 0
# 1 was declared by 2
r = c.functions.declaratorCount(w3.eth.accounts[1]).call()
assert r == 1
# 2 declared 0 and 1
r = c.functions.declarationCount(w3.eth.accounts[2]).call()
assert r == 2
# 2 was declared by noone
r = c.functions.declaratorCount(w3.eth.accounts[2]).call()
assert r == 0
# 3 declared 0
r = c.functions.declarationCount(w3.eth.accounts[3]).call()
assert r == 1
# 3 was declared by noone
r = c.functions.declaratorCount(w3.eth.accounts[3]).call()
assert r == 0