From 6822e09066e31a91ec834e8c567fcfb02867ffea Mon Sep 17 00:00:00 2001 From: nolash Date: Mon, 11 Oct 2021 12:23:08 +0200 Subject: [PATCH] Add agnostic processing unit, outputter --- cic/ext/eth/__init__.py | 40 ++++++++++++++++++++++++++--------- cic/meta.py | 17 +++++++++++++++ cic/output.py | 16 ++++++++++++++ cic/processor.py | 30 ++++++++++++++++++++++++++ requirements.txt | 1 + tests/eth/base_eth.py | 9 +++++++- tests/eth/test_eth_full.py | 27 ++++++++++++++++++++--- tests/eth/test_eth_offline.py | 7 +++++- tests/eth/test_eth_rpc.py | 5 +++-- tests/eth/test_eth_sign.py | 2 +- tests/eth/test_eth_token.py | 3 +-- 11 files changed, 137 insertions(+), 20 deletions(-) create mode 100644 cic/output.py create mode 100644 cic/processor.py diff --git a/cic/ext/eth/__init__.py b/cic/ext/eth/__init__.py index 04d3c74..c2cbd8d 100644 --- a/cic/ext/eth/__init__.py +++ b/cic/ext/eth/__init__.py @@ -27,7 +27,7 @@ logg = logging.getLogger(__name__) class CICEth: - def __init__(self, chain_spec, resources, proof, signer=None, rpc=None, fee_oracle=None): + def __init__(self, core_processor, chain_spec, resources, proof, signer=None, metadata=None, rpc=None, fee_oracle=None): """resources will be modified """ self.resources = resources @@ -35,7 +35,9 @@ class CICEth: self.chain_spec = chain_spec self.signer = signer self.rpc = rpc + self.core_processor = core_processor self.fee_oracle = fee_oracle + self.metadata = metadata self.token_details = None self.token_address = None self.token_code = None @@ -120,7 +122,10 @@ class CICEth: return (args, args_types, positions) - def process_token(self): + def process_token(self, writer=None): + if writer == None: + writer = self.core_processor.writer() + (args, args_types, positions) = self.__order_args() enc = ABIContractEncoder() @@ -131,7 +136,9 @@ class CICEth: code = self.token_details['code'] + enc.get() signer_address = self.resources['token']['key_address'] - nonce_oracle = RPCNonceOracle(signer_address, conn=self.rpc) + nonce_oracle = None + if self.rpc != None: + nonce_oracle = RPCNonceOracle(signer_address, conn=self.rpc) c = TxFactory(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=self.fee_oracle) tx = c.template(signer_address, None, use_nonce=True) @@ -155,12 +162,17 @@ class CICEth: return r - def process_token_index(self): + def process_token_index(self, writer=None): + if writer == None: + writer = self.core_processor.writer() + signer_address = self.resources['token_index']['key_address'] contract_address = self.resources['token_index']['reference'] gas_oracle = OverrideGasOracle(limit=TokenUniqueSymbolIndex.gas(), conn=self.rpc) - nonce_oracle = RPCNonceOracle(signer_address, conn=self.rpc) + nonce_oracle = None + if self.rpc != None: + nonce_oracle = RPCNonceOracle(signer_address, conn=self.rpc) c = TokenUniqueSymbolIndex(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle) o = c.register(contract_address, signer_address, self.token_address, tx_format=self.tx_format) @@ -176,12 +188,17 @@ class CICEth: return r - def process_address_declarator(self): + def process_address_declarator(self, writer=None): + if writer == None: + writer = self.core_processor.writer() + signer_address = self.resources['address_declarator']['key_address'] contract_address = self.resources['address_declarator']['reference'] gas_oracle = OverrideGasOracle(limit=AddressDeclarator.gas(), conn=self.rpc) - nonce_oracle = RPCNonceOracle(signer_address, conn=self.rpc) + nonce_oracle = None + if self.rpc != None: + nonce_oracle = RPCNonceOracle(signer_address, conn=self.rpc) c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle) r = [] @@ -212,10 +229,13 @@ class CICEth: tasks.append(k) for task in tasks: - r = getattr(self, 'process_' + task)() + logg.debug('ciceth adapter process {}'.format(task)) + r = getattr(self, 'process_' + task)(writer=self.core_processor.writer()) + + self.core_processor.process() return self.outputs -def new(resources, proof, signer_hint=None): - return CICEth(resources, proof, signer=None) +def new(core_processor, resources, proof, signer_hint=None): + return CICEth(core_processor, resources, proof, signer=None) diff --git a/cic/meta.py b/cic/meta.py index 64e918b..37f17bf 100644 --- a/cic/meta.py +++ b/cic/meta.py @@ -2,6 +2,11 @@ import os import json +# external imports +from cic_types import MetadataPointer +from cic_types.processor import generate_metadata_pointer +from hexathon import strip_0x + # local imports from .base import ( Data, @@ -46,6 +51,18 @@ class Meta(Data): f.close() + def reference(self, token_address): + token_address_bytes = bytes.fromhex(strip_0x(token_address)) + return generate_metadata_pointer(token_address_bytes, MetadataPointer.TOKEN) + + + def asdict(self): + return { + 'name': self.name, + 'contact': self.contact, + } + + def __str__(self): s = "contact.name = {}\n".format(self.name) diff --git a/cic/output.py b/cic/output.py new file mode 100644 index 0000000..7334aa3 --- /dev/null +++ b/cic/output.py @@ -0,0 +1,16 @@ +# standard imports +import os + + +class KVWriter: + + def __init__(self, path): + os.stat(path) + self.path = path + + + def write(self, k, v): + fp = open(self.path, str(k)) + f = open(fp, 'w') + f.write(v) + f.close() diff --git a/cic/processor.py b/cic/processor.py new file mode 100644 index 0000000..636dcd4 --- /dev/null +++ b/cic/processor.py @@ -0,0 +1,30 @@ +class Processor: + + def __init__(self, token=None, metadata=None, outputs_writer=None): + self.token = token + self.token_address = None + self.metadata = metadata + self.__outputs_writer = outputs_writer + + + def set_token_address(address): + self.token_address = address + + + def writer(self): + return self.__outputs_writer + + + def process_metadata(self, writer=None): + pass + + + def process(self): + tasks = [ + 'metadata', + ] + + for task in tasks: + getattr(self, 'process_' + task)(writer=self.__outputs_writer) + + diff --git a/requirements.txt b/requirements.txt index 963b4b3..80a70b8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ funga>=0.5.0a1,<0.6.0 +cic-types>=0.2.0a1,<=0.2.0 diff --git a/tests/eth/base_eth.py b/tests/eth/base_eth.py index 0b8d69e..d6f2e74 100644 --- a/tests/eth/base_eth.py +++ b/tests/eth/base_eth.py @@ -2,6 +2,7 @@ import random import os import logging +import tempfile # external imports from chainlib.chain import ChainSpec @@ -24,6 +25,9 @@ from okota.token_index.index import TokenUniqueSymbolIndexAddressDeclarator from cic.ext.eth import CICEth from cic import Proof from cic.attachment import Attachment +from cic.output import KVWriter +from cic.processor import Processor + # test imports from tests.base_cic import test_data_dir @@ -38,6 +42,9 @@ class TestCICEthBase(EthTesterCase): random.seed(42) self.initial_description = add_0x(random.randbytes(32).hex()) self.token_address = add_0x(random.randbytes(20).hex()) + d = tempfile.mkdtemp() + self.outputs_writer = KVWriter(d) + self.core_processor = Processor(self.token_address, outputs_writer=self.outputs_writer) addresses = self.keystore.list() @@ -91,7 +98,7 @@ class TestCICEthTokenBase(TestCICEthBase): def setUp(self): super(TestCICEthTokenBase, self).setUp() self.resources['token']['reference'] = None - self.adapter = CICEth(self.chain_spec, self.resources, self.proofs) + self.adapter = CICEth(self.core_processor, self.chain_spec, self.resources, self.proofs) self.token_name = 'FOotoken' self.token_symbol = 'FOO' self.token_precision = 8 diff --git a/tests/eth/test_eth_full.py b/tests/eth/test_eth_full.py index e171a5b..0b34c1c 100644 --- a/tests/eth/test_eth_full.py +++ b/tests/eth/test_eth_full.py @@ -1,6 +1,7 @@ # standard imports import unittest import logging +import random # external imports from chainlib.eth.nonce import ( @@ -15,10 +16,14 @@ from chainlib.eth.tx import ( TxFormat, unpack, receipt, + Tx, + ) +from hexathon import ( + strip_0x, + add_0x, ) -from hexathon import strip_0x -from giftable_erc20_token import GiftableToken from eth_token_index import TokenUniqueSymbolIndex +from giftable_erc20_token import GiftableToken # local imports from cic.ext.eth import CICEth @@ -37,10 +42,26 @@ class TestCICEthRPC(TestCICEthTokenBase): nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) gas_oracle = RPCGasOracle(self.rpc) - self.adapter = CICEth(self.chain_spec, self.resources, self.proofs, signer=self.signer, rpc=self.rpc, fee_oracle=gas_oracle) + self.adapter = CICEth(self.core_processor, self.chain_spec, self.resources, self.proofs, signer=self.signer, rpc=self.rpc, fee_oracle=gas_oracle) def test_rpc_process_notoken(self): + addresses = self.keystore.list() + nonce_oracle = RPCNonceOracle(addresses[0], self.rpc) + gas_oracle = OverrideGasOracle(limit=GiftableToken.gas(), conn=self.rpc) + c = GiftableToken(self.chain_spec, self.signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle) + (tx_hash_hex, o) = c.constructor(addresses[0], self.token_name, self.token_symbol, self.token_precision) + r = self.rpc.do(o) + o = receipt(r) + r = self.rpc.do(o) + Tx.src_normalize(r) + self.assertEqual(r['status'], 1) + token_address = r['contract_address'] + self.adapter.resources['token']['reference'] = token_address + + self.token_index_address = r['contract_address'] + logg.debug('token index deployed at {}'.format(self.token_index_address)) + results = self.adapter.process() for hsh in results: logg.debug('hsh {}'.format(hsh)) diff --git a/tests/eth/test_eth_offline.py b/tests/eth/test_eth_offline.py index b8d6a46..1c16108 100644 --- a/tests/eth/test_eth_offline.py +++ b/tests/eth/test_eth_offline.py @@ -1,5 +1,6 @@ # standard imports import unittest +import logging # local imports from cic.ext.eth import CICEth @@ -7,11 +8,15 @@ from cic.ext.eth import CICEth # tests imports from tests.eth.base_eth import TestCICEthBase +logging.basicConfig(level=logging.DEBUG) +logg = logging.getLogger() + + class TestCICEthOffline(TestCICEthBase): def setUp(self): super(TestCICEthOffline, self).setUp() - self.adapter = CICEth(self.chain_spec, self.resources, self.proofs) + self.adapter = CICEth(self.core_processor, self.chain_spec, self.resources, self.proofs) def test_offline_token_index(self): diff --git a/tests/eth/test_eth_rpc.py b/tests/eth/test_eth_rpc.py index e5358fe..0bef019 100644 --- a/tests/eth/test_eth_rpc.py +++ b/tests/eth/test_eth_rpc.py @@ -27,14 +27,15 @@ class TestCICEthRPC(TestCICEthBase): super(TestCICEthRPC, self).setUp() nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) gas_oracle = RPCGasOracle(self.rpc) - self.adapter = CICEth(self.chain_spec, self.resources, self.proofs, signer=self.signer, rpc=self.rpc, nonce_oracle=nonce_oracle, fee_oracle=gas_oracle) + self.adapter = CICEth(self.core_processor, self.chain_spec, self.resources, self.proofs, signer=self.signer, rpc=self.rpc, fee_oracle=gas_oracle) + @unittest.skip('test does not make sense anymore because nonces will be different') def test_rpc_token_index(self): self.adapter.token_address = self.token_address v = self.adapter.process_token_index() - adapter_norpc = CICEth(self.chain_spec, self.resources, self.proofs, signer=self.signer) + adapter_norpc = CICEth(self.core_processor, self.chain_spec, self.resources, self.proofs, signer=self.signer) adapter_norpc.token_address = self.token_address vv = adapter_norpc.process_token_index() diff --git a/tests/eth/test_eth_sign.py b/tests/eth/test_eth_sign.py index 8065ec3..d080636 100644 --- a/tests/eth/test_eth_sign.py +++ b/tests/eth/test_eth_sign.py @@ -23,7 +23,7 @@ class TestCICEthSign(TestCICEthBase): def setUp(self): super(TestCICEthSign, self).setUp() - self.adapter = CICEth(self.chain_spec, self.resources, self.proofs, signer=self.signer) + self.adapter = CICEth(self.core_processor, self.chain_spec, self.resources, self.proofs, signer=self.signer) def test_sign_token_index(self): diff --git a/tests/eth/test_eth_token.py b/tests/eth/test_eth_token.py index cc01b51..68a3f60 100644 --- a/tests/eth/test_eth_token.py +++ b/tests/eth/test_eth_token.py @@ -22,7 +22,7 @@ from eth_erc20 import ERC20 from cic.ext.eth import CICEth # tests imports -from tests.eth.base_eth import TestCICEthBase +from tests.eth.base_eth import TestCICEthTokenBase from tests.base_cic import test_data_dir logging.basicConfig(level=logging.DEBUG) @@ -31,7 +31,6 @@ logg = logging.getLogger() class TestCICEthToken(TestCICEthTokenBase): - def test_token_nobackend(self): self.adapter.prepare_token(self.token_name, self.token_symbol, self.token_precision, GiftableToken.bytecode()) v = self.adapter.process_token()