mirror of
git://holbrook.no/eth-contract-registry
synced 2025-04-20 02:51:02 +02:00
Separate out interface
This commit is contained in:
parent
657137efb0
commit
f939189f8e
@ -1 +1 @@
|
|||||||
from .registry import Registry
|
from .interface import Registry
|
||||||
|
File diff suppressed because one or more lines are too long
@ -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"}]
|
||||||
|
43
python/eth_contract_registry/interface.py
Normal file
43
python/eth_contract_registry/interface.py
Normal 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)
|
@ -11,7 +11,7 @@ from chainlib.eth.tx import receipt
|
|||||||
from chainlib.eth.nonce import RPCNonceOracle
|
from chainlib.eth.nonce import RPCNonceOracle
|
||||||
|
|
||||||
# local imports
|
# local imports
|
||||||
from eth_contract_registry.registry import Registry
|
from eth_contract_registry.registry import ContractRegistry
|
||||||
from eth_contract_registry.encoding import to_identifier
|
from eth_contract_registry.encoding import to_identifier
|
||||||
|
|
||||||
#logg = logging.getLogger(__name__)
|
#logg = logging.getLogger(__name__)
|
||||||
@ -45,7 +45,7 @@ def registry(
|
|||||||
|
|
||||||
nonce_oracle = RPCNonceOracle(roles['CONTRACT_DEPLOYER'], eth_rpc)
|
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))
|
logg.info('registering identifiers {} in contract registry'.format(valid_identifiers))
|
||||||
(tx_hash_hex, o) = builder.constructor(roles['CONTRACT_DEPLOYER'], valid_identifiers)
|
(tx_hash_hex, o) = builder.constructor(roles['CONTRACT_DEPLOYER'], valid_identifiers)
|
||||||
r = eth_rpc.do(o)
|
r = eth_rpc.do(o)
|
||||||
@ -56,7 +56,7 @@ def registry(
|
|||||||
|
|
||||||
registry_address = rcpt['contract_address']
|
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))
|
chain_spec_identifier = to_identifier(str(default_chain_spec))
|
||||||
|
|
||||||
|
@ -13,8 +13,6 @@ from chainlib.eth.contract import (
|
|||||||
from chainlib.chain import ChainSpec
|
from chainlib.chain import ChainSpec
|
||||||
from chainlib.eth.constant import (
|
from chainlib.eth.constant import (
|
||||||
ZERO_ADDRESS,
|
ZERO_ADDRESS,
|
||||||
ZERO_CONTENT,
|
|
||||||
MAX_UINT,
|
|
||||||
)
|
)
|
||||||
from chainlib.jsonrpc import (
|
from chainlib.jsonrpc import (
|
||||||
jsonrpc_template,
|
jsonrpc_template,
|
||||||
@ -30,6 +28,7 @@ from .encoding import (
|
|||||||
to_identifier,
|
to_identifier,
|
||||||
from_identifier_hex,
|
from_identifier_hex,
|
||||||
)
|
)
|
||||||
|
from .interface import Registry
|
||||||
|
|
||||||
logg = logging.getLogger(__name__)
|
logg = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -37,7 +36,7 @@ moddir = os.path.dirname(__file__)
|
|||||||
datadir = os.path.join(moddir, 'data')
|
datadir = os.path.join(moddir, 'data')
|
||||||
|
|
||||||
|
|
||||||
class Registry(TxFactory):
|
class ContractRegistry(Registry):
|
||||||
|
|
||||||
default_chain_spec = None
|
default_chain_spec = None
|
||||||
__chains_registry = {}
|
__chains_registry = {}
|
||||||
@ -45,30 +44,29 @@ class Registry(TxFactory):
|
|||||||
__abi = None
|
__abi = None
|
||||||
__bytecode = None
|
__bytecode = None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def abi():
|
def abi():
|
||||||
if Registry.__abi == None:
|
if ContractRegistry.__abi == None:
|
||||||
f = open(os.path.join(datadir, 'Registry.json'), 'r')
|
f = open(os.path.join(datadir, 'Registry.json'), 'r')
|
||||||
Registry.__abi = json.load(f)
|
ContractRegistry.__abi = json.load(f)
|
||||||
f.close()
|
f.close()
|
||||||
return Registry.__abi
|
return ContractRegistry.__abi
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def bytecode():
|
def bytecode():
|
||||||
if Registry.__bytecode == None:
|
if ContractRegistry.__bytecode == None:
|
||||||
f = open(os.path.join(datadir, 'Registry.bin'))
|
f = open(os.path.join(datadir, 'Registry.bin'))
|
||||||
Registry.__bytecode = f.read()
|
ContractRegistry.__bytecode = f.read()
|
||||||
f.close()
|
f.close()
|
||||||
return Registry.__bytecode
|
return ContractRegistry.__bytecode
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def gas(code=None):
|
def gas(code=None):
|
||||||
return 1500000
|
return 1500000
|
||||||
|
|
||||||
|
|
||||||
def constructor(self, sender_address, identifier_strings=[]):
|
def constructor(self, sender_address, identifier_strings=[]):
|
||||||
# TODO: handle arrays in chainlib encode instead
|
# TODO: handle arrays in chainlib encode instead
|
||||||
enc = ABIContractEncoder()
|
enc = ABIContractEncoder()
|
||||||
@ -79,15 +77,15 @@ class Registry(TxFactory):
|
|||||||
data = enc.get_contents()
|
data = enc.get_contents()
|
||||||
|
|
||||||
tx = self.template(sender_address, None, use_nonce=True)
|
tx = self.template(sender_address, None, use_nonce=True)
|
||||||
tx = self.set_code(tx, Registry.bytecode() + data)
|
tx = self.set_code(tx, ContractRegistry.bytecode() + data)
|
||||||
logg.debug('bytecode {}\ndata {}\ntx {}'.format(Registry.bytecode(), data, tx))
|
logg.debug('bytecode {}\ndata {}\ntx {}'.format(ContractRegistry.bytecode(), data, tx))
|
||||||
return self.build(tx)
|
return self.build(tx)
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def address(address=None):
|
def address(address=None):
|
||||||
if address != None:
|
if address != None:
|
||||||
Registry.__address = address
|
ContractRegistry.__address = address
|
||||||
return Registry.__address
|
return Registry.__address
|
||||||
|
|
||||||
|
|
||||||
@ -97,26 +95,6 @@ class Registry(TxFactory):
|
|||||||
raise NotImplementedError()
|
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):
|
def set(self, contract_address, sender_address, identifier_string, address, chain_spec, chain_config_hash):
|
||||||
enc = ABIContractEncoder()
|
enc = ABIContractEncoder()
|
||||||
enc.method('set')
|
enc.method('set')
|
||||||
|
BIN
python/gmon.out
BIN
python/gmon.out
Binary file not shown.
@ -22,7 +22,8 @@ from hexathon import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
# local imports
|
# 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.encoding import from_identifier_hex
|
||||||
from eth_contract_registry.pytest.fixtures_registry import valid_identifiers
|
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())
|
bogus_hash = add_0x(os.urandom(32).hex())
|
||||||
|
|
||||||
nonce_oracle = RPCNonceOracle(roles['CONTRACT_DEPLOYER'], eth_rpc)
|
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])
|
o = builder.address_of(registry, 'ContractRegistry', sender_address=eth_accounts[0])
|
||||||
r = eth_rpc.do(o)
|
r = eth_rpc.do(o)
|
||||||
@ -65,6 +66,7 @@ def test_set(
|
|||||||
rcpt = eth_rpc.do(o)
|
rcpt = eth_rpc.do(o)
|
||||||
assert rcpt['status'] == 1
|
assert rcpt['status'] == 1
|
||||||
|
|
||||||
|
builder = Registry(default_chain_spec)
|
||||||
o = builder.address_of(registry, 'FooContract', sender_address=eth_accounts[0])
|
o = builder.address_of(registry, 'FooContract', sender_address=eth_accounts[0])
|
||||||
r = eth_rpc.do(o)
|
r = eth_rpc.do(o)
|
||||||
r = abi_decode_single(ABIContractType.ADDRESS, r)
|
r = abi_decode_single(ABIContractType.ADDRESS, r)
|
||||||
|
@ -58,4 +58,18 @@ contract CICRegistry {
|
|||||||
function configSumOf(bytes32 _chain) public view returns (bytes32) {
|
function configSumOf(bytes32 _chain) public view returns (bytes32) {
|
||||||
return chainConfigs[_chain];
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user