Add cic package

This commit is contained in:
lash
2024-08-01 22:18:03 +01:00
parent 2836b314f7
commit 5135f54b74
193 changed files with 754 additions and 1351 deletions

View File

@@ -0,0 +1 @@
from .seal import EthSeal

View File

@@ -0,0 +1,32 @@
# external imports
from chainlib.eth.tx import TxFormat
from chainlib.eth.tx import TxFactory
from chainlib.jsonrpc import JSONRPCRequest
from chainlib.eth.constant import ZERO_ADDRESS
from chainlib.eth.contract import ABIContractEncoder
from chainlib.eth.contract import ABIContractType
from chainlib.eth.contract import abi_decode_single
from hexathon import add_0x
class EthSeal(TxFactory):
def __noarg(self, method, contract_address, sender_address=ZERO_ADDRESS, id_generator=None):
j = JSONRPCRequest(id_generator)
o = j.template()
o['method'] = 'eth_call'
enc = ABIContractEncoder()
enc.method(method)
data = add_0x(enc.get())
tx = self.template(sender_address, contract_address)
tx = self.set_code(tx, data)
o['params'].append(self.normalize(tx))
return o
def max_seal_state(self, contract_address, sender_address=ZERO_ADDRESS, id_generator=None):
return self.__noarg('maxSealState', contract_address, sender_address=sender_address, id_generator=id_generator)
def seal_state(self, contract_address, sender_address=ZERO_ADDRESS, id_generator=None):
return self.__noarg('sealState', contract_address, sender_address=sender_address, id_generator=id_generator)

View File

@@ -0,0 +1 @@
from .interface import TestEthSealInterface

View File

@@ -0,0 +1,61 @@
# standard imports
import os
import logging
# external imports
from chainlib.eth.unittest.ethtester import EthTesterCase
from chainlib.connection import RPCConnection
from chainlib.eth.nonce import RPCNonceOracle
from chainlib.eth.tx import receipt
from chainlib.eth.tx import TxFactory
from chainlib.eth.tx import TxFormat
from chainlib.eth.contract import ABIContractEncoder
from chainlib.eth.contract import ABIContractType
from cic_contracts.unittest import bytecode
from cic_contracts import Name
script_dir = os.path.dirname(os.path.realpath(__file__))
contract_dir = os.path.join(script_dir, '..', '..', 'solidity')
logg = logging.getLogger(__name__)
class TestEthSeal(EthTesterCase):
def setUp(self):
super(TestEthSeal, self).setUp()
self.set_method = None
self.conn = RPCConnection.connect(self.chain_spec, 'default')
nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
code = bytecode(Name.SEAL)
txf = TxFactory(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
tx = txf.template(self.accounts[0], None, use_nonce=True)
self.max_seal_state = 7
tx = txf.set_code(tx, code)
(tx_hash_hex, o) = txf.build(tx)
self.conn.do(o)
o = receipt(tx_hash_hex)
r = self.conn.do(o)
self.assertEqual(r['status'], 1)
self.address = r['contract_address']
self.contracts['seal'] = self.address
logg.debug('published seal test contract with hash {}'.format(r))
def seal(self, contract_address, sender_address, v, tx_format=TxFormat.JSONRPC):
nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
txf = TxFactory(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
enc = ABIContractEncoder()
enc.method('seal')
enc.typ(ABIContractType.UINT256)
enc.uint256(v)
data = enc.get()
tx = txf.template(sender_address, contract_address, use_nonce=True)
tx = txf.set_code(tx, data)
tx = txf.finalize(tx, tx_format)
return tx

View File

@@ -0,0 +1,39 @@
# external imports
from chainlib.eth.tx import receipt
# local imports
from eth_seal import EthSeal
class TestEthSealInterface:
def __init__(self):
self.set_method = None
self.max_seal_state = 0
def test_default_seal(self):
if self.max_seal_state == 0:
return
c = EthSeal(self.chain_spec)
o = c.max_seal_state(self.address, sender_address=self.accounts[0])
r = self.rpc.do(o)
self.assertEqual(self.max_seal_state, int(r, 16))
def test_seal_set(self):
if self.set_method == None:
return
if self.max_seal_state == 0:
return
(tx_hash, o) = self.set_method(self.contracts['seal'], self.accounts[0], self.max_seal_state)
self.rpc.do(o)
o = receipt(tx_hash)
r = self.rpc.do(o)
self.assertEqual(r['status'], 1)
c = EthSeal(self.chain_spec)
o = c.max_seal_state(self.address, sender_address=self.accounts[0])
r = self.rpc.do(o)
self.assertEqual(self.max_seal_state, int(r, 16))