Initial commit, ported from cic-registry

This commit is contained in:
nolash
2021-03-16 13:53:22 +01:00
commit 15cd758fae
17 changed files with 424 additions and 0 deletions

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1 @@
[{"inputs":[{"internalType":"bytes32[]","name":"_identifiers","type":"bytes32[]"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"bytes32","name":"_identifier","type":"bytes32"}],"name":"addressOf","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_identifier","type":"bytes32"}],"name":"chainOf","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_chain","type":"bytes32"}],"name":"configSumOf","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"identifiers","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_identifier","type":"bytes32"},{"internalType":"address","name":"_address","type":"address"},{"internalType":"bytes32","name":"_chainDescriptor","type":"bytes32"},{"internalType":"bytes32","name":"_chainConfig","type":"bytes32"}],"name":"set","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}]

View File

@@ -0,0 +1,32 @@
# standard imports
import logging
# external imports
from hexathon import strip_0x
logg = logging.getLogger(__name__)
def to_text(b):
b = b[:b.find(0)]
# TODO: why was this part of this method previously?
# if len(b) % 2 > 0:
# b = b'\x00' + b
return b.decode('utf-8')
def from_text(txt):
return '0x{:0<64s}'.format(txt.encode('utf-8').hex())
def from_identifier(b):
return to_text(b)
def from_identifier_hex(hx):
b = bytes.fromhex(strip_0x(hx))
return from_identifier(b)
def to_identifier(txt):
return from_text(txt)

View File

@@ -0,0 +1 @@
from .fixtures_registry import *

View File

@@ -0,0 +1,36 @@
# standard imports
import logging
# external imports
import pytest
from chainlib.eth.connection import RPCConnection
from chainlib.eth.tx import receipt
# local imports
from contract_registry.registry import Registry
logg = logging.getLogger(__name__)
valid_identifiers = [
'ContractRegistry',
]
@pytest.fixture(scope='function')
def registry(
init_eth_tester,
init_eth_rpc,
eth_accounts,
):
conn = RPCConnection.connect('default')
builder = Registry(signer=conn.signer)
(tx_hash_hex, o) = builder.constructor(eth_accounts[0], valid_identifiers)
r = conn.do(o)
logg.debug('r {}'.format(r))
o = receipt(r)
rcpt = conn.do(o)
assert rcpt['status'] == 1
return rcpt['contract_address']

View File

@@ -0,0 +1,140 @@
# standard imports
import os
import logging
import json
# third-party imports
from chainlib.eth.contract import (
ABIContractEncoder,
ABIContractType,
abi_decode_single,
)
from chainlib.chain import ChainSpec
from chainlib.eth.constant import (
ZERO_ADDRESS,
ZERO_CONTENT,
MAX_UINT,
)
from chainlib.eth.rpc import (
jsonrpc_template,
)
from hexathon import (
even,
add_0x,
)
from chainlib.eth.tx import TxFactory
# local imports
from .encoding import to_identifier
logg = logging.getLogger(__name__)
moddir = os.path.dirname(__file__)
datadir = os.path.join(moddir, 'data')
class Registry(TxFactory):
default_chain_spec = None
__chains_registry = {}
__abi = None
__bytecode = None
@staticmethod
def abi():
if Registry.__abi == None:
f = open(os.path.join(datadir, 'Registry.json'), 'r')
Registry.__abi = json.load(f)
f.close()
return Registry.__abi
@staticmethod
def bytecode():
if Registry.__bytecode == None:
f = open(os.path.join(datadir, 'Registry.bin'))
Registry.__bytecode = f.read()
f.close()
return Registry.__bytecode
def constructor(self, sender_address, identifier_strings=[]):
# TODO: handle arrays in chainlib encode instead
enc = ABIContractEncoder()
enc.uint256(32)
enc.uint256(len(identifier_strings))
for s in identifier_strings:
enc.bytes32(to_identifier(s))
data = enc.get_contents()
tx = self.template(sender_address, b'', use_nonce=True)
tx = self.set_code(tx, Registry.bytecode() + data)
logg.debug('bytecode {}\ndata {}\ntx {}'.format(Registry.bytecode(), data, tx))
return self.build(tx)
@staticmethod
def address(address=None):
if address != None:
Registry.__address = address
return Registry.__address
@staticmethod
def load_for(chain_spec):
chain_str = str(chain_spec)
raise NotImplementedError()
def address_of(self, contract_address, identifier_string, sender_address=ZERO_ADDRESS):
o = jsonrpc_template()
o['method'] = 'eth_call'
enc = ABIContractEncoder()
enc.method('addressOf')
enc.typ(ABIContractType.BYTES32)
identifier = to_identifier(identifier_string)
enc.bytes32(identifier)
data = add_0x(enc.encode())
tx = self.template(sender_address, contract_address)
tx = self.set_code(tx, data)
o['params'].append(self.normalize(tx))
return o
def parse_address_of(self, v):
return abi_decode_single(ABIContractType.ADDRESS, v)
def set(self, contract_address, sender_address, identifier_string, address, chain_descriptor_hash, chain_config_hash):
enc = ABIContractEncoder()
enc.method('set')
enc.typ(ABIContractType.BYTES32)
enc.typ(ABIContractType.ADDRESS)
enc.typ(ABIContractType.BYTES32)
enc.typ(ABIContractType.BYTES32)
identifier = to_identifier(identifier_string)
enc.bytes32(identifier)
enc.address(address)
enc.bytes32(chain_descriptor_hash)
enc.bytes32(chain_config_hash)
data = enc.encode()
tx = self.template(sender_address, contract_address, use_nonce=True)
tx = self.set_code(tx, data)
return self.build(tx)
def identifier(self, contract_address, idx, sender_address=ZERO_ADDRESS):
o = jsonrpc_template()
o['method'] = 'eth_call'
enc = ABIContractEncoder()
enc.method('identifiers')
enc.typ(ABIContractType.UINT256)
enc.uint256(idx)
data = add_0x(enc.encode())
tx = self.template(sender_address, contract_address)
tx = self.set_code(tx, data)
o['params'].append(self.normalize(tx))
return o