cic-cli/tests/eth/base_eth.py

115 lines
3.9 KiB
Python
Raw Normal View History

2021-10-10 21:30:18 +02:00
# standard imports import unittestimport logging
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
2021-10-10 12:50:18 +02:00
# local imports
from cic.ext.eth import CICEth
2021-10-10 21:30:18 +02:00
from cic import Proof
from cic.attachment import Attachment
from cic.output import KVWriter
from cic.processor import Processor
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-10-11 08:57:03 +02:00
self.initial_description = add_0x(random.randbytes(32).hex())
2021-10-10 12:50:18 +02:00
self.token_address = add_0x(random.randbytes(20).hex())
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))
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
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 = Processor(outputs_writer=self.outputs_writer, extensions=[self.adapter])