refactor: switch to poetry, add interactive deployment

This commit is contained in:
2022-03-01 10:01:56 +03:00
parent 45a6e5e79f
commit a2dfdbedb5
56 changed files with 4921 additions and 972 deletions

0
tests/__init__.py Normal file
View File

View File

@@ -1,43 +1,42 @@
# standard imports
import os
import tempfile
import logging
import unittest
import random
import tempfile
import unittest
from cic.contract.components.attachment import Attachment
from cic.contract.components.proof import Proof
from cic.contract.processor import ContractProcessor
# local imports
from cic.writers import KVWriter
# external imports
from hexathon import add_0x
# local imports
from cic.output import KVWriter
from cic.processor import Processor
from cic.attachment import Attachment
from cic import Proof
test_base_dir = os.path.dirname(os.path.realpath(__file__))
test_data_dir = os.path.join(test_base_dir, 'testdata')
test_data_dir = os.path.join(test_base_dir, "testdata")
proof_hash = '0f6fc017f29caf512c0feaaf83bc10614b488311cace2973dc248dc24b01e04f'
foo_hash = '2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae'
bar_hash = 'fcde2b2edba56bf408601fb721fe9b5c338d10ee429ea04fae5511b68fbf8fb9'
root_merged_hash = '795fed550ada0ec1eea4309a282f5910bc3bdb3a9762c7d9cc25d6de71c45096'
root_unmerged_hash = '5dc81e51703e624f498663e7d5d70429b824e9ff60f92b61fe47eb6862a971b4'
proof_hash = "0f6fc017f29caf512c0feaaf83bc10614b488311cace2973dc248dc24b01e04f"
foo_hash = "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"
bar_hash = "fcde2b2edba56bf408601fb721fe9b5c338d10ee429ea04fae5511b68fbf8fb9"
root_merged_hash = "2a27a488377c753fffea58ad535cfdacc2fcb5cf0ae495ec71d88e31757ec0c3"
root_unmerged_hash = "14dc271290eca763e99c2e7c21c541bded86fb803c6b01bac28cd367db34399c"
class TestCICBase(unittest.TestCase):
def setUp(self):
super(TestCICBase, self).setUp()
random.seed(42)
f = open('/dev/urandom', 'rb')
f = open("/dev/urandom", "rb")
addresses = []
for i in range(3):
for _i in range(3):
address_bytes = f.read(32)
addresses.append(add_0x(address_bytes.hex()))
self.token_symbol = 'FOO'
self.token_symbol = "FOO"
token_address_bytes = f.read(20)
token_index_address_bytes = f.read(20)
address_declarator_address_bytes = f.read(20)
@@ -50,23 +49,23 @@ class TestCICBase(unittest.TestCase):
self.outputs_dir = tempfile.mkdtemp()
self.outputs_writer = KVWriter(self.outputs_dir)
self.core_processor = Processor(outputs_writer=self.outputs_writer)
self.core_processor = ContractProcessor(outputs_writer=self.outputs_writer)
self.resources = {
'token': {
'reference': self.token_address,
'key_address': addresses[0],
},
'token_index': {
'reference': self.token_index_address,
'key_address': addresses[1],
},
'address_declarator': {
'reference': self.address_declarator_address,
'key_address': addresses[2],
},
}
proof_dir = os.path.join(test_data_dir, 'proof')
"token": {
"reference": self.token_address,
"key_address": addresses[0],
},
"token_index": {
"reference": self.token_index_address,
"key_address": addresses[1],
},
"address_declarator": {
"reference": self.address_declarator_address,
"key_address": addresses[2],
},
}
proof_dir = os.path.join(test_data_dir, "proof")
attach = Attachment(path=proof_dir)
attach.load()
self.proofs = Proof(proof_dir, attachments=attach)

View File

@@ -1,4 +1,4 @@
# standard imports import unittestimport logging
# standard imports
import random
import os
import logging
@@ -24,10 +24,10 @@ from cic_contracts.writer import CICWriter
# local imports
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
from cic.writers import KVWriter
from cic.contract.processor import ContractProcessor
from cic.contract.components.proof import Proof
from cic.contract.components.attachment import Attachment
# test imports
@@ -127,4 +127,4 @@ class TestCICEthTokenBase(TestCICEthBase):
self.token_precision = 8
self.token_supply = 1073741824
self.core_processor = Processor(outputs_writer=self.outputs_writer, extensions=[self.adapter])
self.core_processor = ContractProcessor(outputs_writer=self.outputs_writer, extensions=[self.adapter])

View File

@@ -27,8 +27,8 @@ from giftable_erc20_token import GiftableToken
# local imports
from cic.ext.eth import CICEth
from cic.processor import Processor
from cic.token import Token
from cic.contract.processor import ContractProcessor
from cic.contract.components.token import Token
# test imports
from tests.eth.base_eth import TestCICEthTokenBase
@@ -46,7 +46,7 @@ class TestCICEthRPC(TestCICEthTokenBase):
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, outputs_writer=self.outputs_writer)
self.core_processor = Processor(outputs_writer=self.outputs_writer, extensions=[self.adapter])
self.core_processor = ContractProcessor(outputs_writer=self.outputs_writer, extensions=[self.adapter])
def test_rpc_process_notoken(self):

View File

@@ -5,7 +5,7 @@ import os
# local imports
from cic.ext.eth import CICEth
from cic.processor import Processor
from cic.contract.processor import ContractProcessor
# tests imports
from tests.eth.base_eth import TestCICEthBase

View File

@@ -11,7 +11,7 @@ from hexathon import (
# local imports
from cic.ext.eth import CICEth
from cic.processor import Processor
from cic.contract.processor import ContractProcessor
# tests imports
from tests.eth.base_eth import TestCICEthBase
@@ -25,7 +25,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.core_processor = Processor(outputs_writer=self.outputs_writer, extensions=[self.adapter])
self.core_processor = ContractProcessor(outputs_writer=self.outputs_writer, extensions=[self.adapter])
def test_sign_token_index(self):

View File

@@ -1,14 +1,16 @@
# standard imports
import logging
import os
import unittest
import logging
# local imports
from cic.keystore import KeystoreDirectory
from funga.eth.keystore.dict import DictKeystore
from funga.error import DecryptError
from funga.eth.keystore.dict import DictKeystore
from hexathon import uniform as hex_uniform
# external imports
from cic.keystore import KeystoreDirectory
# test imports
from tests.base_cic import test_base_dir
@@ -16,8 +18,9 @@ logging = logging.getLogger()
script_dir = test_base_dir
def pass_getter():
return 'test'
return "test"
class EthKeystoreDirectory(DictKeystore, KeystoreDirectory):
@@ -25,25 +28,25 @@ class EthKeystoreDirectory(DictKeystore, KeystoreDirectory):
class TestKeyfile(unittest.TestCase):
def setUp(self):
self.path = os.path.join(script_dir, 'testdata', 'keystore')
self.path = os.path.join(script_dir, "testdata", "keystore")
self.keystore = EthKeystoreDirectory()
def test_keystore_bogus(self):
bogus_path = os.path.join(self.path, 'bogus')
bogus_path = os.path.join(self.path, "bogus")
self.keystore.process_dir(bogus_path)
def test_keystore_ok(self):
ok_path = os.path.join(self.path, 'ok')
ok_path = os.path.join(self.path, "ok")
with self.assertRaises(DecryptError):
self.keystore.process_dir(ok_path) # wrong password
self.keystore.process_dir(ok_path, default_password='test')
self.keystore.process_dir(ok_path) # wrong password
self.keystore.process_dir(ok_path, default_password="test")
self.keystore.process_dir(ok_path, password_retriever=pass_getter)
self.assertTrue(hex_uniform('cc4f82F5DacDE395E1E0CFc4d62827C8B8B5688C') in self.keystore.list())
self.assertTrue(
hex_uniform("cc4f82F5DacDE395E1E0CFc4d62827C8B8B5688C")
in self.keystore.list()
)
if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()

View File

@@ -5,27 +5,51 @@ import os
# external imports
from hexathon import strip_0x
# local imports
from cic.meta import Meta
from cic.contract.components.meta import Meta
# test imports
from tests.base_cic import (
TestCICBase,
test_data_dir,
)
from tests.base_cic import TestCICBase, test_data_dir
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
class TestCICMeta(TestCICBase):
def test_meta(self):
fp = os.path.join(test_data_dir, 'proof')
fp = os.path.join(test_data_dir, "proof")
m = Meta(fp)
m.load()
self.assertEquals(
str(m),
"""name = Test
contact.phone = 0700-123456
country_code = KE
location = Kilifi
""",
)
def test_meta_with_initial_values(self):
fp = os.path.join(test_data_dir, "proof")
m = Meta(
fp,
name="TestName",
location="TestLocation",
country_code="TestCC",
contact={
"phone": "0723578455158",
},
)
self.assertEquals(
str(m),
"""name = TestName
contact.phone = 0723578455158
country_code = TestCC
location = TestLocation
""",
)
if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()

View File

@@ -3,17 +3,12 @@ import unittest
import logging
import os
import json
import sys
# external imports
from hexathon import strip_0x
# local imports
from cic import Proof
from cic.processor import Processor
from cic.attachment import Attachment
from cic.meta import Meta
from cic.output import KVWriter
from cic.contract.processor import ContractProcessor
from cic.contract.components.proof import Proof
from cic.contract.components.attachment import Attachment
from cic.contract.components.meta import Meta
# test imports
from tests.base_cic import (
@@ -28,66 +23,65 @@ logg.setLevel(logging.DEBUG)
class MockExt:
def __init__(self, address):
self.address = address
def process(self):
return (self.address, 'foo')
return (self.address, "foo")
class TestCICProcessor(TestCICBase):
def test_processor_meta(self):
fp = os.path.join(test_data_dir, 'proof')
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 = ContractProcessor(
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)
fp = os.path.join(self.outputs_dir, meta_reference)
f = open(fp, 'r')
o = json.load(f)
f.close()
with open(fp, "r", encoding="utf-8") as f:
o = json.load(f)
self.assertEqual(m.asdict(), o)
def test_processor_attachment(self):
fp = os.path.join(test_data_dir, 'proof')
fp = os.path.join(test_data_dir, "proof")
m = Attachment(fp)
m.load()
mock_ext = MockExt(self.token_address)
p = Processor(attachment=m, outputs_writer=self.outputs_writer, extensions=[mock_ext])
p = ContractProcessor(
attachment=m, outputs_writer=self.outputs_writer, extensions=[mock_ext]
)
p.process()
for k in list(m.contents.keys()):
for _k in list(m.contents.keys()):
os.stat(fp)
def test_processor_proof_noattachment(self):
fp = os.path.join(test_data_dir, 'proof')
fp = os.path.join(test_data_dir, "proof")
m = Proof(fp)
ap = os.path.join(test_data_dir, 'proof_empty')
ap = os.path.join(test_data_dir, "proof_empty")
m.extra_attachments = Attachment(ap)
m.load()
mock_ext = MockExt(self.token_address)
p = Processor(proof=m, outputs_writer=self.outputs_writer, extensions=[mock_ext])
p = ContractProcessor(
proof=m, outputs_writer=self.outputs_writer, extensions=[mock_ext]
)
p.process()
self.assertEqual(p.outputs[0], root_unmerged_hash)
self.assertEqual(p.outputs[0], root_unmerged_hash)
def test_processor_proof_attachment(self):
fp = os.path.join(test_data_dir, 'proof')
fp = os.path.join(test_data_dir, "proof")
ma = Attachment(fp)
ma.load()
@@ -96,11 +90,13 @@ class TestCICProcessor(TestCICBase):
mp.load()
mock_ext = MockExt(self.token_address)
p = Processor(proof=mp, outputs_writer=self.outputs_writer, extensions=[mock_ext])
p = ContractProcessor(
proof=mp, outputs_writer=self.outputs_writer, extensions=[mock_ext]
)
p.process()
self.assertEqual(p.outputs[0], root_merged_hash)
self.assertEqual(p.outputs[0], root_merged_hash)
if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()

View File

@@ -4,23 +4,35 @@ import unittest
import logging
# local imports
from cic import Proof
from cic.attachment import Attachment
from cic.contract.components.proof import Proof
from cic.contract.components.attachment import Attachment
# test imports
from tests.base_cic import (
test_data_dir,
TestCICBase,
root_merged_hash,
)
from tests.base_cic import test_data_dir, TestCICBase, root_merged_hash
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
class TestProof(TestCICBase):
def test_proof_load(self):
proof_path = os.path.join(test_data_dir, "proof")
attach = Attachment(proof_path, writer=self.outputs_writer)
attach.load()
c = Proof(path=proof_path, attachments=attach)
c.load()
self.assertEquals(
str(c),
"""description = foo bar baz
issuer = the man
namespace = ge
version = 0
proofs[0] = 2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae
proofs[1] = fcde2b2edba56bf408601fb721fe9b5c338d10ee429ea04fae5511b68fbf8fb9
""",
)
def test_proof_serialize_merge(self):
proof_path = os.path.join(test_data_dir, 'proof')
proof_path = os.path.join(test_data_dir, "proof")
attach = Attachment(proof_path, writer=self.outputs_writer)
attach.load()
@@ -31,5 +43,5 @@ class TestProof(TestCICBase):
self.assertEqual(v, root_merged_hash)
if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()

View File

@@ -7,7 +7,7 @@ import logging
from hexathon import strip_0x
# local imports
from cic.output import KVWriter
from cic.writers import KVWriter
# test imports
from tests.base_cic import TestCICBase

View File

@@ -1,7 +1,8 @@
{
"name": "",
"location": "",
"country_code": "",
"name": "Test",
"location": "Kilifi",
"country_code": "KE",
"contact": {
"phone": "0700-123456"
}
}