Separate out interface

This commit is contained in:
nolash 2021-04-30 13:11:22 +02:00
parent 657137efb0
commit f939189f8e
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
9 changed files with 79 additions and 42 deletions

View File

@ -1 +1 @@
from .registry import Registry
from .interface import Registry

File diff suppressed because one or more lines are too long

View File

@ -1 +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"},{"inputs":[{"internalType":"address","name":"_newOwner","type":"address"}],"name":"transferOwnership","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}]
[{"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"},{"inputs":[{"internalType":"bytes4","name":"_sum","type":"bytes4"}],"name":"supportsInterface","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"address","name":"_newOwner","type":"address"}],"name":"transferOwnership","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}]

View File

@ -0,0 +1,43 @@
# external imports
from chainlib.jsonrpc import (
jsonrpc_template,
)
from chainlib.eth.contract import (
ABIContractEncoder,
ABIContractType,
abi_decode_single,
)
from chainlib.eth.tx import TxFactory
from hexathon import (
add_0x,
)
from chainlib.eth.constant import (
ZERO_ADDRESS,
)
# local imports
from .encoding import (
to_identifier,
)
class Registry(TxFactory):
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
@classmethod
def parse_address_of(self, v):
return abi_decode_single(ABIContractType.ADDRESS, v)

View File

@ -11,7 +11,7 @@ from chainlib.eth.tx import receipt
from chainlib.eth.nonce import RPCNonceOracle
# local imports
from eth_contract_registry.registry import Registry
from eth_contract_registry.registry import ContractRegistry
from eth_contract_registry.encoding import to_identifier
#logg = logging.getLogger(__name__)
@ -45,7 +45,7 @@ def registry(
nonce_oracle = RPCNonceOracle(roles['CONTRACT_DEPLOYER'], eth_rpc)
builder = Registry(default_chain_spec, signer=eth_signer, nonce_oracle=nonce_oracle)
builder = ContractRegistry(default_chain_spec, signer=eth_signer, nonce_oracle=nonce_oracle)
logg.info('registering identifiers {} in contract registry'.format(valid_identifiers))
(tx_hash_hex, o) = builder.constructor(roles['CONTRACT_DEPLOYER'], valid_identifiers)
r = eth_rpc.do(o)
@ -56,7 +56,7 @@ def registry(
registry_address = rcpt['contract_address']
c = Registry(default_chain_spec, signer=eth_signer, nonce_oracle=nonce_oracle)
c = ContractRegistry(default_chain_spec, signer=eth_signer, nonce_oracle=nonce_oracle)
chain_spec_identifier = to_identifier(str(default_chain_spec))

View File

@ -13,8 +13,6 @@ from chainlib.eth.contract import (
from chainlib.chain import ChainSpec
from chainlib.eth.constant import (
ZERO_ADDRESS,
ZERO_CONTENT,
MAX_UINT,
)
from chainlib.jsonrpc import (
jsonrpc_template,
@ -30,6 +28,7 @@ from .encoding import (
to_identifier,
from_identifier_hex,
)
from .interface import Registry
logg = logging.getLogger(__name__)
@ -37,7 +36,7 @@ moddir = os.path.dirname(__file__)
datadir = os.path.join(moddir, 'data')
class Registry(TxFactory):
class ContractRegistry(Registry):
default_chain_spec = None
__chains_registry = {}
@ -45,30 +44,29 @@ class Registry(TxFactory):
__abi = None
__bytecode = None
@staticmethod
def abi():
if Registry.__abi == None:
if ContractRegistry.__abi == None:
f = open(os.path.join(datadir, 'Registry.json'), 'r')
Registry.__abi = json.load(f)
ContractRegistry.__abi = json.load(f)
f.close()
return Registry.__abi
return ContractRegistry.__abi
@staticmethod
def bytecode():
if Registry.__bytecode == None:
if ContractRegistry.__bytecode == None:
f = open(os.path.join(datadir, 'Registry.bin'))
Registry.__bytecode = f.read()
ContractRegistry.__bytecode = f.read()
f.close()
return Registry.__bytecode
return ContractRegistry.__bytecode
@staticmethod
def gas(code=None):
return 1500000
def constructor(self, sender_address, identifier_strings=[]):
# TODO: handle arrays in chainlib encode instead
enc = ABIContractEncoder()
@ -79,15 +77,15 @@ class Registry(TxFactory):
data = enc.get_contents()
tx = self.template(sender_address, None, use_nonce=True)
tx = self.set_code(tx, Registry.bytecode() + data)
logg.debug('bytecode {}\ndata {}\ntx {}'.format(Registry.bytecode(), data, tx))
tx = self.set_code(tx, ContractRegistry.bytecode() + data)
logg.debug('bytecode {}\ndata {}\ntx {}'.format(ContractRegistry.bytecode(), data, tx))
return self.build(tx)
@staticmethod
def address(address=None):
if address != None:
Registry.__address = address
ContractRegistry.__address = address
return Registry.__address
@ -97,26 +95,6 @@ class Registry(TxFactory):
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
@classmethod
def parse_address_of(self, v):
return abi_decode_single(ABIContractType.ADDRESS, v)
def set(self, contract_address, sender_address, identifier_string, address, chain_spec, chain_config_hash):
enc = ABIContractEncoder()
enc.method('set')

Binary file not shown.

View File

@ -22,7 +22,8 @@ from hexathon import (
)
# local imports
from eth_contract_registry.registry import Registry
from eth_contract_registry import Registry
from eth_contract_registry.registry import ContractRegistry
from eth_contract_registry.encoding import from_identifier_hex
from eth_contract_registry.pytest.fixtures_registry import valid_identifiers
@ -46,7 +47,7 @@ def test_set(
bogus_hash = add_0x(os.urandom(32).hex())
nonce_oracle = RPCNonceOracle(roles['CONTRACT_DEPLOYER'], eth_rpc)
builder = Registry(default_chain_spec, signer=eth_signer, nonce_oracle=nonce_oracle)
builder = ContractRegistry(default_chain_spec, signer=eth_signer, nonce_oracle=nonce_oracle)
o = builder.address_of(registry, 'ContractRegistry', sender_address=eth_accounts[0])
r = eth_rpc.do(o)
@ -65,6 +66,7 @@ def test_set(
rcpt = eth_rpc.do(o)
assert rcpt['status'] == 1
builder = Registry(default_chain_spec)
o = builder.address_of(registry, 'FooContract', sender_address=eth_accounts[0])
r = eth_rpc.do(o)
r = abi_decode_single(ABIContractType.ADDRESS, r)

View File

@ -58,4 +58,18 @@ contract CICRegistry {
function configSumOf(bytes32 _chain) public view returns (bytes32) {
return chainConfigs[_chain];
}
// Implements EIP 165
function supportsInterface(bytes4 _sum) public pure returns (bool) {
if (_sum == 0xbb34534c) { // Registry
return true;
}
if (_sum == 0x01ffc9a7) { // EIP165
return true;
}
if (_sum == 0x9493f8b2) { // EIP173
return true;
}
return false;
}
}