Merge "origin/master" into "cic-cli"

This commit is contained in:
PhilipWafula 2021-10-15 14:14:05 +03:00
commit 1c3905fc39
Signed by: mango-habanero
GPG Key ID: B00CE9034DA19FB7
15 changed files with 66 additions and 31 deletions

View File

@ -49,7 +49,8 @@ class Attachment(Data):
writer = self.writer
for k in self.contents.keys():
f = open(self.contents[k], 'rb')
fp = os.path.join(self.attachment_path, self.contents[k])
f = open(fp, 'rb')
v = f.read()
f.close()
logg.debug('writing attachment {}'.format(k))

View File

@ -1,6 +1,7 @@
# standard imports
import logging
import copy
import json
# external imports
from chainlib.chain import ChainSpec
@ -42,7 +43,6 @@ class CICEth(Extension):
super(CICEth, self).__init__(chain_spec, resources, proof, signer=signer, rpc=rpc, outputs_writer=outputs_writer)
self.fee_oracle = fee_oracle
self.tx_format = TxFormat.RAW_ARGS
self.outputs_writer = outputs_writer
if self.rpc != None:
self.tx_format = TxFormat.JSONRPC
elif self.signer != None:
@ -145,20 +145,23 @@ class CICEth(Extension):
tx = c.set_code(tx, code)
o = c.finalize(tx, self.tx_format)
token_address_tx = None
r = None
if self.rpc != None:
r = self.rpc.do(o[1])
token_address_tx = r
o = self.rpc.wait(r)
o = Tx.src_normalize(o)
self.token_address = o['contract_address']
elif self.signer != None:
r = o[1]
token_address_tx = r
if r == None:
r = code
writer.write('token', r.encode('utf-8'))
writer.write('token_address', self.token_address.encode('utf-8'))
self.add_outputs('token', r.encode('utf-8'))
self.add_outputs('token', r)
if self.token_details['supply'] > 0:
c = GiftableToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=self.fee_oracle)
@ -167,12 +170,15 @@ class CICEth(Extension):
if self.rpc != None:
r = self.rpc.do(o[1])
self.rpc.wait(r)
writer.write('token_supply', r.encode('utf-8'))
elif self.signer != None:
r = o[1]
writer.write('token_supply', json.dumps(r).encode('utf-8'))
else:
r = o
writer.write('token_supply', r.encode('utf-8'))
return r
writer.write('token_supply', r.encode('utf-8'))
return token_address_tx
def process_token_index(self, writer=None):

View File

@ -4,12 +4,15 @@ import logging
# external imports
from hexathon import valid as valid_hex
# local imports
from cic.output import StdoutWriter
logg = logging.getLogger(__name__)
class Extension:
def __init__(self, chain_spec, resources, proof, signer=None, rpc=None, outputs_writer=None):
def __init__(self, chain_spec, resources, proof, signer=None, rpc=None, outputs_writer=StdoutWriter()):
self.resources = resources
self.proof = proof
self.chain_spec = chain_spec
@ -19,6 +22,7 @@ class Extension:
self.token_address = None
self.token_code = None
self.outputs = []
self.outputs_writer = outputs_writer
# TODO: apply / prepare token can be factored out

View File

@ -1,6 +1,9 @@
# standard imports
import os
import sys
import logging
logg = logging.getLogger(__name__)
class OutputWriter:
@ -27,6 +30,7 @@ class KVWriter(OutputWriter):
def write(self, k, v):
fp = os.path.join(self.path, str(k))
logg.debug('path write {} {}'.format(fp, str(v)))
f = open(fp, 'wb')
f.write(v)
f.close()

View File

@ -33,14 +33,13 @@ class Processor:
def process(self, writer=None):
tasks = [
#'attachment',
#'proof',
'attachment',
'proof',
'metadata',
]
for ext in self.extensions:
# token_address = ext.process()
token_address = '0x1222afdeadbeef'
token_address = ext.process()
for task in tasks:
a = self.cores.get(task)

View File

@ -1,4 +1,4 @@
funga>=0.5.0a1,<0.6.0
funga-eth>=0.5.0a1,<0.6.0
cic-types>=0.2.0a4,<=0.2.0
confini>=0.4.2rc3,<0.5.0
chainlib>=0.0.10a1,<0.1.0

View File

@ -3,3 +3,5 @@ eth_tester==0.5.0b3
py-evm==0.3.0a20
rlp==2.0.1
chainlib-eth>=0.0.10a2,<0.1.0
eth-address-index>=0.2.4a1,<0.3.0
okota>=0.2.4a6,<0.3.0

View File

@ -22,8 +22,8 @@ test_data_dir = os.path.join(test_base_dir, 'testdata')
proof_hash = '0f6fc017f29caf512c0feaaf83bc10614b488311cace2973dc248dc24b01e04f'
foo_hash = '2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae'
bar_hash = 'fcde2b2edba56bf408601fb721fe9b5c338d10ee429ea04fae5511b68fbf8fb9'
root_merged_hash = '1b38b0e9a2cc0895c72e5f460a613bb0f3d88c4a87c1ee2b39a41c64f1dbcd1e'
root_unmerged_hash = '86d0091b98635a45da472388a1204104d7d10ea1199c1bd9c1d235de73a2c285'
root_merged_hash = 'ba135f8518d36af5fa65c59317ea9602c4b654d998ea9097ecf81e0638a03441'
root_unmerged_hash = '68ccfe99fd905be439b09dcd780993865598605b8492462a6fc4f127b688fef6'
class TestCICBase(unittest.TestCase):

View File

@ -109,5 +109,6 @@ class TestCICEthTokenBase(TestCICEthBase):
self.token_name = 'FOotoken'
self.token_symbol = 'FOO'
self.token_precision = 8
self.token_supply = 1073741824
self.core_processor = Processor(outputs_writer=self.outputs_writer, extensions=[self.adapter])

View File

@ -34,6 +34,7 @@ from tests.eth.base_eth import TestCICEthTokenBase
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
logg.setLevel(level=logging.DEBUG)
class TestCICEthRPC(TestCICEthTokenBase):
@ -43,7 +44,7 @@ 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.chain_spec, self.resources, self.proofs, signer=self.signer, rpc=self.rpc, fee_oracle=gas_oracle, outputs_writer=self.outputs_writer)
self.core_processor = Processor(outputs_writer=self.outputs_writer, extensions=[self.adapter])
@ -74,7 +75,7 @@ class TestCICEthRPC(TestCICEthTokenBase):
def test_rpc_process_withtoken(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.adapter.prepare_token(self.token_name, self.token_symbol, self.token_precision, GiftableToken.bytecode(), self.token_supply)
self.adapter.process()
results = self.adapter.get_outputs()
@ -96,7 +97,7 @@ class TestCICEthRPC(TestCICEthTokenBase):
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.adapter.prepare_token(self.token_name, self.token_symbol, self.token_precision, GiftableToken.bytecode(), self.token_supply)
self.core_processor.process()
results = self.adapter.get_outputs()

View File

@ -19,13 +19,17 @@ class TestCICEthOffline(TestCICEthBase):
def setUp(self):
super(TestCICEthOffline, self).setUp()
self.adapter = CICEth(self.chain_spec, self.resources, self.proofs)
self.first_proof = self.proofs.get()[0]
proofs = []
for proof in self.proofs.get():
proofs.append(proof[0])
proofs.sort()
self.first_proof = proofs[0]
#self.core_processor = Processor(outputs_writer=self.outputs_writer, extensions=[self.adapter])
def test_offline_token_index(self):
self.adapter.token_address = self.token_address
self.adapter.process_token_index()
self.adapter.process_token_index(writer=self.outputs_writer)
self.assertEqual(self.adapter.outputs[0][0], 'token_index')
self.assertEqual(self.adapter.outputs[0][1][:8], '4420e486')
@ -43,6 +47,8 @@ class TestCICEthOffline(TestCICEthBase):
self.adapter.outputs_writer = self.outputs_writer
self.adapter.token_address = self.token_address
self.adapter.process_address_declarator()
logg.debug('proocs {}'.format(self.proofs))
logg.debug('outputs {}'.format(self.adapter.outputs))
self.assertEqual(self.adapter.outputs[0][0], 'address_declarator_' + self.first_proof)
self.assertEqual(self.adapter.outputs[0][1][:8], 'ae47ece0')
self.assertEqual(len(self.adapter.outputs), 3)

View File

@ -30,7 +30,7 @@ class TestCICEthSign(TestCICEthBase):
def test_sign_token_index(self):
self.adapter.token_address = self.token_address
v = self.adapter.process_token_index()
v = self.adapter.process_token_index(writer=self.outputs_writer)
tx_raw = bytes.fromhex(strip_0x(self.adapter.outputs[0][1]))
tx = unpack(tx_raw, self.chain_spec)
self.assertEqual(strip_0x(tx['data'])[:8], '4420e486')

View File

@ -7,6 +7,7 @@ import logging
from chainlib.eth.tx import (
TxFormat,
receipt,
Tx,
)
from chainlib.eth.contract import (
abi_decode_single,
@ -27,28 +28,31 @@ from tests.base_cic import test_data_dir
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
logg.setLevel(logging.DEBUG)
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()
self.adapter.prepare_token(self.token_name, self.token_symbol, self.token_precision, GiftableToken.bytecode(), 0)
self.adapter.token_address = self.token_address
v = self.adapter.process_token(writer=self.outputs_writer)
def test_token_sign(self):
self.adapter.token_address = self.token_address
self.adapter.signer = self.signer
self.adapter.tx_format = TxFormat.RLP_SIGNED
self.adapter.prepare_token(self.token_name, self.token_symbol, self.token_precision, GiftableToken.bytecode())
v = self.adapter.process_token()
self.adapter.prepare_token(self.token_name, self.token_symbol, self.token_precision, GiftableToken.bytecode(), self.token_supply)
v = self.adapter.process_token(writer=self.outputs_writer)
def test_token_rpc(self):
self.adapter.signer = self.signer
self.adapter.rpc = self.rpc
self.adapter.tx_format = TxFormat.JSONRPC
self.adapter.prepare_token(self.token_name, self.token_symbol, self.token_precision, GiftableToken.bytecode())
v = self.adapter.process_token()
self.adapter.prepare_token(self.token_name, self.token_symbol, self.token_precision, GiftableToken.bytecode(), self.token_supply)
v = self.adapter.process_token(writer=self.outputs_writer)
o = receipt(v)
r = self.rpc.do(o)
self.assertEqual(r['status'], 1)
@ -60,10 +64,11 @@ class TestCICEthToken(TestCICEthTokenBase):
weird_bytecode = f.read()
f.close()
try:
self.adapter.prepare_token(self.token_name, self.token_symbol, self.token_precision, weird_bytecode, extra=['0xdeadbeef'], extra_types=['bytes32'])
self.adapter.prepare_token(self.token_name, self.token_symbol, self.token_precision, weird_bytecode, self.token_supply, extra=['0xdeadbeef'], extra_types=['bytes32'])
except ValueError:
v = self.adapter.process_token()
self.adapter.prepare_token(self.token_name, self.token_symbol, self.token_precision, weird_bytecode, extra=['0xdeadbeef'], extra_types=['bytes32'], positions=[1, 2, 3, 0])
v = self.adapter.process_token(writer=self.outputs_writer)
self.adapter.prepare_token(self.token_name, self.token_symbol, self.token_precision, weird_bytecode, self.token_supply, extra=['0xdeadbeef'], extra_types=['bytes32'], positions=[1, 2, 3, 0])
self.adapter.load_code()
self.adapter.signer = self.signer
self.adapter.rpc = self.rpc
self.adapter.tx_format = TxFormat.JSONRPC
@ -72,10 +77,11 @@ class TestCICEthToken(TestCICEthTokenBase):
o = receipt(v)
r = self.rpc.do(o)
self.assertEqual(r['status'], 1)
Tx.src_normalize(r)
contract_address = r['contract_address']
logg.debug('contract address {}'.format(contract_address))
c = ERC20(self.chain_spec)
o = c.name(contract_address, sender_address=self.accounts[0])
r = self.rpc.do(o)
self.assertEqual(c.parse_name(r), self.token_name)

View File

@ -3,6 +3,7 @@ import unittest
import logging
import os
import json
import sys
# external imports
from hexathon import strip_0x
@ -12,6 +13,7 @@ from cic import Proof
from cic.processor import Processor
from cic.attachment import Attachment
from cic.meta import Meta
from cic.output import KVWriter
# test imports
from tests.base_cic import (
@ -21,8 +23,9 @@ from tests.base_cic import (
root_unmerged_hash,
)
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
logg.setLevel(logging.DEBUG)
class MockExt:
@ -42,6 +45,7 @@ class TestCICProcessor(TestCICBase):
mock_ext = MockExt(self.token_address)
p = Processor(metadata=m, outputs_writer=self.outputs_writer, extensions=[mock_ext])
p.token_address = self.token_address
p.process()
meta_reference = m.reference(self.token_address)
@ -62,8 +66,8 @@ class TestCICProcessor(TestCICBase):
p = Processor(attachment=m, outputs_writer=self.outputs_writer, extensions=[mock_ext])
p.process()
for k in list(m.contents.keys()):
fp = os.path.join(self.outputs_dir, k)
os.stat(fp)

View File

@ -1,5 +1,6 @@
{
"version": 0,
"namespace": "ge",
"issuer": "the man",
"description": "foo bar baz"
}