Add agnostic processing unit, outputter

This commit is contained in:
nolash 2021-10-11 12:23:08 +02:00
parent 3da923481f
commit 6822e09066
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
11 changed files with 137 additions and 20 deletions

View File

@ -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)

View File

@ -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)

16
cic/output.py Normal file
View File

@ -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()

30
cic/processor.py Normal file
View File

@ -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)

View File

@ -1 +1,2 @@
funga>=0.5.0a1,<0.6.0
cic-types>=0.2.0a1,<=0.2.0

View File

@ -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

View File

@ -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))

View File

@ -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):

View File

@ -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()

View File

@ -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):

View File

@ -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()