cic-cli/tests/eth/base_eth.py

131 lines
4.6 KiB
Python
Raw Permalink Normal View History

# standard imports
2021-10-10 12:50:18 +02:00
import random
2021-10-10 21:30:18 +02:00
import os
import logging
import tempfile
2021-10-10 12:50:18 +02:00
# external imports
from chainlib.chain import ChainSpec
2021-10-10 13:20:00 +02:00
from chainlib.eth.unittest.ethtester import EthTesterCase
2021-10-11 08:57:03 +02:00
from chainlib.eth.tx import (
Tx,
receipt,
)
from chainlib.eth.gas import OverrideGasOracle
from chainlib.eth.nonce import RPCNonceOracle
2021-10-10 12:50:18 +02:00
from hexathon import (
add_0x,
strip_0x,
)
2021-10-10 13:20:00 +02:00
from funga.eth.keystore.dict import DictKeystore
2021-10-11 08:57:03 +02:00
from eth_address_declarator.declarator import AddressDeclarator
from okota.token_index.index import TokenUniqueSymbolIndexAddressDeclarator
from cic_contracts.writer import CICWriter
2021-10-10 12:50:18 +02:00
# local imports
from cic.ext.eth import CICEth
from cic.writers import KVWriter
from cic.contract.processor import ContractProcessor
from cic.contract.components.proof import Proof
from cic.contract.components.attachment import Attachment
2021-10-10 21:30:18 +02:00
# test imports
from tests.base_cic import test_data_dir
2021-10-10 12:50:18 +02:00
logg = logging.getLogger(__name__)
2021-10-10 13:20:00 +02:00
class TestCICEthBase(EthTesterCase):
2021-10-10 12:50:18 +02:00
def setUp(self):
2021-10-10 13:20:00 +02:00
super(TestCICEthBase, self).setUp()
2021-10-10 12:50:18 +02:00
random.seed(42)
2021-11-29 15:44:09 +01:00
f = open('/dev/urandom', 'rb')
initial_description_bytes = f.read(32)
self.initial_description = add_0x(initial_description_bytes.hex())
token_address_bytes = f.read(20)
self.token_address = add_0x(token_address_bytes.hex())
f.close()
2021-10-11 08:57:03 +02:00
2021-10-10 13:20:00 +02:00
addresses = self.keystore.list()
2021-10-11 08:57:03 +02:00
nonce_oracle = RPCNonceOracle(addresses[2], self.rpc)
gas_oracle = OverrideGasOracle(limit=AddressDeclarator.gas(), conn=self.rpc)
c = AddressDeclarator(self.chain_spec, self.signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle)
(tx_hash_hex, o) = c.constructor(addresses[2], self.initial_description)
r = self.rpc.do(o)
o = receipt(r)
r = self.rpc.do(o)
Tx.src_normalize(r)
self.assertEqual(r['status'], 1)
self.address_declarator_address = r['contract_address']
logg.debug('address declarator deployed at {}'.format(self.address_declarator_address))
nonce_oracle = RPCNonceOracle(addresses[1], self.rpc)
gas_oracle = OverrideGasOracle(limit=TokenUniqueSymbolIndexAddressDeclarator.gas(), conn=self.rpc)
c = TokenUniqueSymbolIndexAddressDeclarator(self.chain_spec, self.signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle)
(tx_hash_hex, o) = c.constructor(addresses[1], self.address_declarator_address)
r = self.rpc.do(o)
o = receipt(r)
r = self.rpc.do(o)
Tx.src_normalize(r)
self.assertEqual(r['status'], 1)
self.token_index_address = r['contract_address']
logg.debug('token index deployed at {}'.format(self.token_index_address))
c = CICWriter(self.chain_spec, self.signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle)
(tx_hash_hex, o) = c.add_writer(self.token_index_address, addresses[1], addresses[1])
r = self.rpc.do(o)
o = receipt(r)
r = self.rpc.do(o)
Tx.src_normalize(r)
self.assertEqual(r['status'], 1)
2021-10-10 12:50:18 +02:00
self.resources = {
'token': {
'reference': self.token_address,
'key_account': addresses[0],
2021-10-10 12:50:18 +02:00
},
'token_index': {
'reference': self.token_index_address,
'key_account': addresses[1],
2021-10-10 12:50:18 +02:00
},
2021-10-10 14:06:40 +02:00
'address_declarator': {
2021-10-11 08:57:03 +02:00
'reference': self.address_declarator_address,
'key_account': addresses[2],
2021-10-10 14:06:40 +02:00
},
2021-10-10 12:50:18 +02:00
}
self.chain_spec_dict = {
'arch': 'evm',
'fork': 'byzantine',
'network_id': 42,
'common_name': 'foo',
}
2021-10-10 21:30:18 +02:00
proof_dir = os.path.join(test_data_dir, 'proof')
attach = Attachment(path=proof_dir)
attach.load()
self.proofs = Proof(proof_dir, attachments=attach)
self.proofs.load()
2021-10-11 08:57:03 +02:00
2021-11-29 13:18:28 +01:00
logg.debug('proofs {}'.format(self.proofs.attachments))
self.outputs_dir = tempfile.mkdtemp()
self.outputs_writer = KVWriter(self.outputs_dir)
2021-10-11 08:57:03 +02:00
class TestCICEthTokenBase(TestCICEthBase):
def setUp(self):
super(TestCICEthTokenBase, self).setUp()
2021-10-11 08:57:03 +02:00
self.resources['token']['reference'] = None
self.adapter = CICEth(self.chain_spec, self.resources, self.proofs, outputs_writer=self.outputs_writer)
2021-10-11 08:57:03 +02:00
self.token_name = 'FOotoken'
self.token_symbol = 'FOO'
self.token_precision = 8
2021-10-15 12:48:14 +02:00
self.token_supply = 1073741824
self.core_processor = ContractProcessor(outputs_writer=self.outputs_writer, extensions=[self.adapter])