Run extensions processing from core processor

This commit is contained in:
nolash 2021-10-11 15:28:09 +02:00
parent dd9640b383
commit 23567905a1
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
13 changed files with 233 additions and 60 deletions

View File

@ -27,7 +27,7 @@ logg = logging.getLogger(__name__)
class CICEth: class CICEth:
def __init__(self, core_processor, chain_spec, resources, proof, signer=None, metadata=None, rpc=None, fee_oracle=None): def __init__(self, chain_spec, resources, proof, signer=None, rpc=None, fee_oracle=None, outputs_writer=None):
"""resources will be modified """resources will be modified
""" """
self.resources = resources self.resources = resources
@ -35,14 +35,13 @@ class CICEth:
self.chain_spec = chain_spec self.chain_spec = chain_spec
self.signer = signer self.signer = signer
self.rpc = rpc self.rpc = rpc
self.core_processor = core_processor
self.fee_oracle = fee_oracle self.fee_oracle = fee_oracle
self.metadata = metadata
self.token_details = None self.token_details = None
self.token_address = None self.token_address = None
self.token_code = None self.token_code = None
self.outputs = [] self.outputs = []
self.tx_format = TxFormat.RAW_ARGS self.tx_format = TxFormat.RAW_ARGS
self.outputs_writer = outputs_writer
if self.rpc != None: if self.rpc != None:
self.tx_format = TxFormat.JSONRPC self.tx_format = TxFormat.JSONRPC
elif self.signer != None: elif self.signer != None:
@ -122,10 +121,20 @@ class CICEth:
return (args, args_types, positions) return (args, args_types, positions)
def add_outputs(self, k, v):
logg.debug('adding outputs {} {}'.format(k, v))
self.outputs.append((k, v))
def get_outputs(self):
return self.outputs
def process_token(self, writer=None): def process_token(self, writer=None):
if writer == None: if writer == None:
writer = self.core_processor.writer() writer = self.outputs_writer
logg.debug('ZZZZZZZZ token details {}'.format(self.token_details))
(args, args_types, positions) = self.__order_args() (args, args_types, positions) = self.__order_args()
enc = ABIContractEncoder() enc = ABIContractEncoder()
@ -158,13 +167,13 @@ class CICEth:
if r == None: if r == None:
r = code r = code
self.outputs.append(r) self.add_outputs('token', r)
return r return r
def process_token_index(self, writer=None): def process_token_index(self, writer=None):
if writer == None: if writer == None:
writer = self.core_processor.writer() writer = self.outputs_writer
signer_address = self.resources['token_index']['key_address'] signer_address = self.resources['token_index']['key_address']
contract_address = self.resources['token_index']['reference'] contract_address = self.resources['token_index']['reference']
@ -184,13 +193,13 @@ class CICEth:
else: else:
r = o r = o
self.outputs.append(r) self.add_outputs('token_index', r)
return r return r
def process_address_declarator(self, writer=None): def process_address_declarator(self, writer=None):
if writer == None: if writer == None:
writer = self.core_processor.writer() writer = self.outputs_writer
signer_address = self.resources['address_declarator']['key_address'] signer_address = self.resources['address_declarator']['key_address']
contract_address = self.resources['address_declarator']['reference'] contract_address = self.resources['address_declarator']['reference']
@ -201,21 +210,26 @@ class CICEth:
nonce_oracle = RPCNonceOracle(signer_address, conn=self.rpc) nonce_oracle = RPCNonceOracle(signer_address, conn=self.rpc)
c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle) c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle)
r = [] results = []
for proof in self.proof.get(): for proof in self.proof.get():
o = c.add_declaration(contract_address, signer_address, self.token_address, proof, tx_format=self.tx_format) o = c.add_declaration(contract_address, signer_address, self.token_address, proof, tx_format=self.tx_format)
r = None
if self.rpc != None: if self.rpc != None:
r.append(self.rpc.do(o[1])) r = self.rpc.do(o[1])
elif self.signer != None: elif self.signer != None:
r.append(o[1]) r = o[1]
else: else:
r.append(o) r = o
self.add_outputs('address_declarator', r)
results.append(r)
self.outputs += r return results
return r
def process(self): def process(self, writer=None):
if writer == None:
writer = self.outputs_writer
tasks = [] tasks = []
self.token_address = self.resources['token']['reference'] self.token_address = self.resources['token']['reference']
@ -230,12 +244,10 @@ class CICEth:
for task in tasks: for task in tasks:
logg.debug('ciceth adapter process {}'.format(task)) logg.debug('ciceth adapter process {}'.format(task))
r = getattr(self, 'process_' + task)(writer=self.core_processor.writer()) r = getattr(self, 'process_' + task)(writer=writer)
self.core_processor.process() return self.token_address
return self.outputs
def new(core_processor, resources, proof, signer_hint=None): def new(resources, proof, signer_hint=None):
return CICEth(core_processor, resources, proof, signer=None) return CICEth(resources, proof, signer=None)

View File

@ -62,6 +62,14 @@ class Meta(Data):
'contact': self.contact, 'contact': self.contact,
} }
def process(self, token_address=None, writer=None):
k = self.reference(token_address)
v = json.dumps(self.asdict())
writer.write(k, v)
return (k, v)
def __str__(self): def __str__(self):
s = "contact.name = {}\n".format(self.name) s = "contact.name = {}\n".format(self.name)

View File

@ -10,7 +10,7 @@ class KVWriter:
def write(self, k, v): def write(self, k, v):
fp = open(self.path, str(k)) fp = os.path.join(self.path, str(k))
f = open(fp, 'w') f = open(fp, 'w')
f.write(v) f.write(v)
f.close() f.close()

View File

@ -1,22 +1,38 @@
# standard imports
import logging
logg = logging.getLogger(__name__)
class Processor: class Processor:
def __init__(self, token=None, metadata=None, outputs_writer=None): def __init__(self, metadata=None, outputs_writer=None, extensions=[]):
self.token = token
self.token_address = None self.token_address = None
self.metadata = metadata self.metadata = metadata
self.extensions = extensions
self.outputs = []
self.__outputs_writer = outputs_writer self.__outputs_writer = outputs_writer
def set_token_address(address):
self.token_address = address
def writer(self): def writer(self):
return self.__outputs_writer return self.__outputs_writer
def can_process(self):
return self.token_address != None
def process_metadata(self, writer=None): def process_metadata(self, writer=None):
pass if not self.can_process():
raise RuntimeError('incomplete processing state for metadata')
def get_outputs(self):
outputs = []
for ext in self.extensions:
outputs += ext.outputs
outputs += self.outputs
return outputs
def process(self): def process(self):
@ -24,7 +40,13 @@ class Processor:
'metadata', 'metadata',
] ]
for task in tasks: for ext in self.extensions:
getattr(self, 'process_' + task)(writer=self.__outputs_writer) token_address = ext.process()
for task in tasks:
a = getattr(self, task)
if a == None:
logg.debug('skipping missing task receiver "{}"'.format(task))
continue
v = a.process(token_address=token_address, writer=self.__outputs_writer)
self.outputs.append(v)

View File

@ -36,8 +36,8 @@ class TestCICBase(unittest.TestCase):
self.token_index_address = add_0x(random.randbytes(32).hex()) self.token_index_address = add_0x(random.randbytes(32).hex())
self.address_declarator_address = add_0x(random.randbytes(32).hex()) self.address_declarator_address = add_0x(random.randbytes(32).hex())
d = tempfile.mkdtemp() self.outputs_dir = tempfile.mkdtemp()
self.outputs_writer = KVWriter(d) self.outputs_writer = KVWriter(self.outputs_dir)
self.core_processor = Processor(self.token_address, outputs_writer=self.outputs_writer) self.core_processor = Processor(self.token_address, outputs_writer=self.outputs_writer)
self.resources = { self.resources = {

View File

@ -42,9 +42,6 @@ class TestCICEthBase(EthTesterCase):
random.seed(42) random.seed(42)
self.initial_description = add_0x(random.randbytes(32).hex()) self.initial_description = add_0x(random.randbytes(32).hex())
self.token_address = add_0x(random.randbytes(20).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() addresses = self.keystore.list()
@ -92,13 +89,19 @@ class TestCICEthBase(EthTesterCase):
self.proofs = Proof(proof_dir, attachments=attach) self.proofs = Proof(proof_dir, attachments=attach)
self.proofs.load() self.proofs.load()
d = tempfile.mkdtemp()
self.outputs_writer = KVWriter(d)
class TestCICEthTokenBase(TestCICEthBase): class TestCICEthTokenBase(TestCICEthBase):
def setUp(self): def setUp(self):
super(TestCICEthTokenBase, self).setUp() super(TestCICEthTokenBase, self).setUp()
self.resources['token']['reference'] = None self.resources['token']['reference'] = None
self.adapter = CICEth(self.core_processor, self.chain_spec, self.resources, self.proofs) self.adapter = CICEth(self.chain_spec, self.resources, self.proofs, outputs_writer=self.outputs_writer)
self.token_name = 'FOotoken' self.token_name = 'FOotoken'
self.token_symbol = 'FOO' self.token_symbol = 'FOO'
self.token_precision = 8 self.token_precision = 8
self.core_processor = Processor(outputs_writer=self.outputs_writer, extensions=[self.adapter])

View File

@ -27,6 +27,7 @@ from giftable_erc20_token import GiftableToken
# local imports # local imports
from cic.ext.eth import CICEth from cic.ext.eth import CICEth
from cic.processor import Processor
# test imports # test imports
from tests.eth.base_eth import TestCICEthTokenBase from tests.eth.base_eth import TestCICEthTokenBase
@ -42,7 +43,8 @@ class TestCICEthRPC(TestCICEthTokenBase):
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
gas_oracle = RPCGasOracle(self.rpc) gas_oracle = RPCGasOracle(self.rpc)
self.adapter = CICEth(self.core_processor, self.chain_spec, self.resources, self.proofs, signer=self.signer, rpc=self.rpc, fee_oracle=gas_oracle) self.adapter = CICEth(self.chain_spec, self.resources, self.proofs, signer=self.signer, rpc=self.rpc, fee_oracle=gas_oracle)
self.core_processor = Processor(outputs_writer=self.outputs_writer, extensions=[self.adapter])
def test_rpc_process_notoken(self): def test_rpc_process_notoken(self):
@ -62,10 +64,10 @@ class TestCICEthRPC(TestCICEthTokenBase):
self.token_index_address = r['contract_address'] self.token_index_address = r['contract_address']
logg.debug('token index deployed at {}'.format(self.token_index_address)) logg.debug('token index deployed at {}'.format(self.token_index_address))
results = self.adapter.process() self.adapter.process()
for hsh in results: results = self.adapter.get_outputs()
logg.debug('hsh {}'.format(hsh)) for v in results:
o = receipt(hsh) o = receipt(v[1])
r = self.rpc.do(o) r = self.rpc.do(o)
self.assertEqual(r['status'], 1) self.assertEqual(r['status'], 1)
@ -73,14 +75,38 @@ class TestCICEthRPC(TestCICEthTokenBase):
def test_rpc_process_withtoken(self): def test_rpc_process_withtoken(self):
self.adapter.fee_oracle = OverrideGasOracle(limit=GiftableToken.gas(), conn=self.rpc) self.adapter.fee_oracle = OverrideGasOracle(limit=GiftableToken.gas(), conn=self.rpc)
self.adapter.prepare_token(self.token_name, self.token_symbol, self.token_precision, GiftableToken.bytecode()) self.adapter.prepare_token(self.token_name, self.token_symbol, self.token_precision, GiftableToken.bytecode())
results = self.adapter.process() self.adapter.process()
for hsh in results:
logg.debug('hsh {}'.format(hsh)) results = self.adapter.get_outputs()
o = receipt(hsh) for v in results:
o = receipt(v[1])
r = self.rpc.do(o) r = self.rpc.do(o)
self.assertEqual(r['status'], 1) self.assertEqual(r['status'], 1)
o = receipt(results[0]) o = receipt(results[0][1])
r = self.rpc.do(o)
token_contract_address = r['contract_address']
c = TokenUniqueSymbolIndex(self.chain_spec)
o = c.address_of(self.token_index_address, 'FOO', sender_address=self.accounts[0])
r = self.rpc.do(o)
token_contract_address_lookup = c.parse_address_of(r)
self.assertEqual(strip_0x(token_contract_address), strip_0x(token_contract_address_lookup))
def test_rpc_process_top(self):
self.adapter.fee_oracle = OverrideGasOracle(limit=GiftableToken.gas(), conn=self.rpc)
self.adapter.prepare_token(self.token_name, self.token_symbol, self.token_precision, GiftableToken.bytecode())
self.core_processor.process()
results = self.adapter.get_outputs()
for v in results:
logg.debug('results {}'.format(v))
o = receipt(v[1])
r = self.rpc.do(o)
self.assertEqual(r['status'], 1)
o = receipt(results[0][1])
r = self.rpc.do(o) r = self.rpc.do(o)
token_contract_address = r['contract_address'] token_contract_address = r['contract_address']

View File

@ -4,6 +4,7 @@ import logging
# local imports # local imports
from cic.ext.eth import CICEth from cic.ext.eth import CICEth
from cic.processor import Processor
# tests imports # tests imports
from tests.eth.base_eth import TestCICEthBase from tests.eth.base_eth import TestCICEthBase
@ -16,21 +17,23 @@ class TestCICEthOffline(TestCICEthBase):
def setUp(self): def setUp(self):
super(TestCICEthOffline, self).setUp() super(TestCICEthOffline, self).setUp()
self.adapter = CICEth(self.core_processor, self.chain_spec, self.resources, self.proofs) self.adapter = CICEth(self.chain_spec, self.resources, self.proofs)
self.core_processor = Processor(outputs_writer=self.outputs_writer, extensions=[self.adapter])
def test_offline_token_index(self): def test_offline_token_index(self):
self.adapter.token_address = self.token_address self.adapter.token_address = self.token_address
v = self.adapter.process_token_index() self.adapter.process_token_index()
self.assertEqual(self.adapter.outputs[0], v) self.assertEqual(self.adapter.outputs[0][0], 'token_index')
self.assertEqual(self.adapter.outputs[0][:8], '4420e486') self.assertEqual(self.adapter.outputs[0][1][:8], '4420e486')
def test_offline_address_declarator(self): def test_offline_address_declarator(self):
self.adapter.token_address = self.token_address self.adapter.token_address = self.token_address
v = self.adapter.process_address_declarator() self.adapter.process_address_declarator()
self.assertEqual(v[0][:8], 'ae47ece0') self.assertEqual(self.adapter.outputs[0][0], 'address_declarator')
self.assertEqual(len(v), 3) self.assertEqual(self.adapter.outputs[0][1][:8], 'ae47ece0')
self.assertEqual(len(self.adapter.outputs), 3)

View File

@ -11,6 +11,7 @@ from hexathon import (
# local imports # local imports
from cic.ext.eth import CICEth from cic.ext.eth import CICEth
from cic.processor import Processor
# tests imports # tests imports
from tests.eth.base_eth import TestCICEthBase from tests.eth.base_eth import TestCICEthBase
@ -23,13 +24,14 @@ class TestCICEthSign(TestCICEthBase):
def setUp(self): def setUp(self):
super(TestCICEthSign, self).setUp() super(TestCICEthSign, self).setUp()
self.adapter = CICEth(self.core_processor, self.chain_spec, self.resources, self.proofs, signer=self.signer) self.adapter = CICEth(self.chain_spec, self.resources, self.proofs, signer=self.signer)
self.core_processor = Processor(outputs_writer=self.outputs_writer, extensions=[self.adapter])
def test_sign_token_index(self): def test_sign_token_index(self):
self.adapter.token_address = self.token_address self.adapter.token_address = self.token_address
v = self.adapter.process_token_index() v = self.adapter.process_token_index()
tx_raw = bytes.fromhex(strip_0x(self.adapter.outputs[0])) tx_raw = bytes.fromhex(strip_0x(self.adapter.outputs[0][1]))
tx = unpack(tx_raw, self.chain_spec) tx = unpack(tx_raw, self.chain_spec)
self.assertEqual(strip_0x(tx['data'])[:8], '4420e486') self.assertEqual(strip_0x(tx['data'])[:8], '4420e486')

View File

@ -1,15 +1,19 @@
# standard imports # standard imports
import unittest import unittest
import logging import logging
import os
# external imports # external imports
from hexathon import strip_0x from hexathon import strip_0x
# local imports # local imports
from cic.output import KVWriter from cic.meta import Meta
# test imports # test imports
from tests.base_cic import TestCICBase from tests.base_cic import (
TestCICBase,
test_data_dir,
)
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger() logg = logging.getLogger()
@ -18,7 +22,9 @@ logg = logging.getLogger()
class TestCICMeta(TestCICBase): class TestCICMeta(TestCICBase):
def test_meta(self): def test_meta(self):
pass fp = os.path.join(test_data_dir, 'proof')
m = Meta(fp)
m.load()
if __name__ == '__main__': if __name__ == '__main__':

31
tests/test_output.py Normal file
View File

@ -0,0 +1,31 @@
# standard imports
import os
import unittest
import logging
# external imports
from hexathon import strip_0x
# local imports
from cic.output import KVWriter
# test imports
from tests.base_cic import TestCICBase
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
class TestCICOutput(TestCICBase):
def test_output_file(self):
self.outputs_writer.write('foo', 'bar')
fp = os.path.join(self.outputs_dir, 'foo')
f = open(fp, 'r')
v = f.read()
f.close()
self.assertEqual(v, 'bar')
if __name__ == '__main__':
unittest.main()

53
tests/test_processor.py Normal file
View File

@ -0,0 +1,53 @@
# standard imports
import unittest
import logging
import os
import json
# external imports
from hexathon import strip_0x
# local imports
from cic.processor import Processor
from cic.meta import Meta
# test imports
from tests.base_cic import (
TestCICBase,
test_data_dir,
)
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
class MockExt:
def __init__(self, address):
self.address = address
def process(self):
return self.address
class TestCICProcessor(TestCICBase):
def test_processor(self):
fp = os.path.join(test_data_dir, 'proof')
m = Meta(fp)
m.load()
mock_ext = MockExt(self.token_address)
p = Processor(metadata=m, outputs_writer=self.outputs_writer, extensions=[mock_ext])
p.process()
meta_reference = m.reference(self.token_address)
fp = os.path.join(self.outputs_dir, meta_reference)
f = open(fp, 'r')
o = json.load(f)
f.close()
self.assertEqual(m.asdict(), o)
if __name__ == '__main__':
unittest.main()

7
tests/testdata/proof/meta.json vendored Normal file
View File

@ -0,0 +1,7 @@
{
"name": "",
"location": "",
"country_code": "",
"contact": {
}
}