Split index interface and registry implementation

This commit is contained in:
nolash 2021-04-30 12:46:53 +02:00
parent 6aa5da01bd
commit 9363ed3bfb
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
7 changed files with 168 additions and 118 deletions

View File

@ -1 +1 @@
from .registry import AccountRegistry
from .index import AccountsIndex

File diff suppressed because one or more lines are too long

View File

@ -1 +1 @@
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"addedAccount","type":"address"},{"indexed":true,"internalType":"uint256","name":"accountIndex","type":"uint256"}],"name":"AccountAdded","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"inputs":[],"name":"acceptOwnership","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"add","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_writer","type":"address"}],"name":"addWriter","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"count","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_writer","type":"address"}],"name":"deleteWriter","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"entry","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"entryCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"entryIndex","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"have","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","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"}]
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"addedAccount","type":"address"},{"indexed":true,"internalType":"uint256","name":"accountIndex","type":"uint256"}],"name":"AccountAdded","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"inputs":[],"name":"acceptOwnership","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"add","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_writer","type":"address"}],"name":"addWriter","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_writer","type":"address"}],"name":"deleteWriter","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_idx","type":"uint256"}],"name":"entry","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"entryCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"have","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","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,121 @@
# standard imports
import logging
import json
import os
# external imports
from chainlib.eth.tx import (
TxFactory,
TxFormat,
)
from chainlib.eth.contract import (
ABIContractEncoder,
ABIContractDecoder,
ABIContractType,
abi_decode_single,
)
from chainlib.eth.constant import ZERO_ADDRESS
from chainlib.jsonrpc import (
jsonrpc_template,
)
from chainlib.eth.error import RequestMismatchException
from hexathon import (
add_0x,
strip_0x,
)
logg = logging.getLogger()
class AccountsIndex(TxFactory):
def __single_address_method(self, method, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC):
enc = ABIContractEncoder()
enc.method(method)
enc.typ(ABIContractType.ADDRESS)
enc.address(address)
data = enc.get()
tx = self.template(sender_address, contract_address, use_nonce=True)
tx = self.set_code(tx, data)
tx = self.finalize(tx, tx_format)
return tx
def add(self, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC):
return self.__single_address_method('add', contract_address, sender_address, address, tx_format)
def have(self, contract_address, address, sender_address=ZERO_ADDRESS):
o = jsonrpc_template()
o['method'] = 'eth_call'
enc = ABIContractEncoder()
enc.method('have')
enc.typ(ABIContractType.ADDRESS)
enc.address(address)
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 entry_count(self, contract_address, sender_address=ZERO_ADDRESS):
o = jsonrpc_template()
o['method'] = 'eth_call'
enc = ABIContractEncoder()
enc.method('entryCount')
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 count(self, contract_address, sender_address=ZERO_ADDRESS):
return self.entry_count(contract_address, sender_address=sender_address)
def entry(self, contract_address, idx, sender_address=ZERO_ADDRESS):
o = jsonrpc_template()
o['method'] = 'eth_call'
enc = ABIContractEncoder()
enc.method('entry')
enc.typ(ABIContractType.UINT256)
enc.uint256(idx)
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
@classmethod
def parse_account(self, v):
return abi_decode_single(ABIContractType.ADDRESS, v)
@classmethod
def parse_have(self, v):
return abi_decode_single(ABIContractType.BOOLEAN, v)
@classmethod
def parse_add_request(self, v):
v = strip_0x(v)
cursor = 0
enc = ABIContractEncoder()
enc.method('add')
enc.typ(ABIContractType.ADDRESS)
r = enc.get()
l = len(r)
m = v[:l]
if m != r:
logg.error('method mismatch, expected {}, got {}'.format(r, m))
raise RequestMismatchException(v)
cursor += l
dec = ABIContractDecoder()
dec.typ(ABIContractType.ADDRESS)
dec.val(v[cursor:cursor+64])
r = dec.decode()
return r

View File

@ -1,36 +1,23 @@
# standard imports
import logging
import json
import os
# external imports
from chainlib.eth.tx import (
TxFactory,
TxFormat,
)
from chainlib.eth.contract import (
ABIContractEncoder,
ABIContractDecoder,
ABIContractType,
abi_decode_single,
)
from chainlib.eth.constant import ZERO_ADDRESS
from chainlib.jsonrpc import (
jsonrpc_template,
)
from chainlib.eth.error import RequestMismatchException
from hexathon import (
add_0x,
strip_0x,
)
logg = logging.getLogger()
# local imports
from .index import AccountsIndex
moddir = os.path.dirname(__file__)
datadir = os.path.join(moddir, 'data')
class AccountRegistry(TxFactory):
class AccountRegistry(AccountsIndex):
__abi = None
__bytecode = None
@ -65,18 +52,6 @@ class AccountRegistry(TxFactory):
return self.build(tx)
def add(self, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC):
return self.__single_address_method('add', contract_address, sender_address, address, tx_format)
def add_writer(self, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC):
return self.__single_address_method('addWriter', contract_address, sender_address, address, tx_format)
def delete_writer(self, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC):
return self.__single_address_method('deleteWriter', contract_address, sender_address, address, tx_format)
def __single_address_method(self, method, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC):
enc = ABIContractEncoder()
enc.method(method)
@ -89,73 +64,9 @@ class AccountRegistry(TxFactory):
return tx
def have(self, contract_address, address, sender_address=ZERO_ADDRESS):
o = jsonrpc_template()
o['method'] = 'eth_call'
enc = ABIContractEncoder()
enc.method('have')
enc.typ(ABIContractType.ADDRESS)
enc.address(address)
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 add_writer(self, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC):
return self.__single_address_method('addWriter', contract_address, sender_address, address, tx_format)
def count(self, contract_address, sender_address=ZERO_ADDRESS):
o = jsonrpc_template()
o['method'] = 'eth_call'
enc = ABIContractEncoder()
enc.method('count')
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 account(self, contract_address, idx, sender_address=ZERO_ADDRESS):
o = jsonrpc_template()
o['method'] = 'eth_call'
enc = ABIContractEncoder()
enc.method('accounts')
enc.typ(ABIContractType.UINT256)
enc.uint256(idx)
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
@classmethod
def parse_account(self, v):
return abi_decode_single(ABIContractType.ADDRESS, v)
@classmethod
def parse_have(self, v):
return abi_decode_single(ABIContractType.BOOLEAN, v)
@classmethod
def parse_add_request(self, v):
v = strip_0x(v)
cursor = 0
enc = ABIContractEncoder()
enc.method('add')
enc.typ(ABIContractType.ADDRESS)
r = enc.get()
l = len(r)
m = v[:l]
if m != r:
logg.error('method mismatch, expected {}, got {}'.format(r, m))
raise RequestMismatchException(v)
cursor += l
dec = ABIContractDecoder()
dec.typ(ABIContractType.ADDRESS)
dec.val(v[cursor:cursor+64])
r = dec.decode()
return r
def delete_writer(self, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC):
return self.__single_address_method('deleteWriter', contract_address, sender_address, address, tx_format)

View File

@ -7,6 +7,7 @@ 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.address import to_checksum_address
from chainlib.eth.tx import (
receipt,
@ -19,7 +20,8 @@ from chainlib.eth.contract import (
)
# local imports
from eth_accounts_index import AccountRegistry
from eth_accounts_index.registry import AccountRegistry
from eth_accounts_index import AccountsIndex
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
@ -44,8 +46,8 @@ class Test(EthTesterCase):
def setUp(self):
super(Test, self).setUp()
nonce_oracle = TestNonceOracle(self.accounts[0])
self.o = AccountRegistry(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
(tx_hash, o) = self.o.constructor(self.accounts[0])
c = AccountRegistry(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
(tx_hash, o) = c.constructor(self.accounts[0])
self.conn = RPCConnection.connect(self.chain_spec, 'default')
r = self.conn.do(o)
logg.debug('deployed with hash {}'.format(r))
@ -54,7 +56,7 @@ class Test(EthTesterCase):
r = self.conn.do(o)
self.address = to_checksum_address(r['contract_address'])
(tx_hash, o) = self.o.add_writer(self.address, self.accounts[0], self.accounts[0])
(tx_hash, o) = c.add_writer(self.address, self.accounts[0], self.accounts[0])
r = self.conn.do(o)
o = receipt(r)
@ -63,17 +65,21 @@ class Test(EthTesterCase):
def test_1_count(self):
o = self.o.count(self.address, sender_address=self.accounts[0])
#o = self.o.count(self.address, sender_address=self.accounts[0])
c = AccountsIndex(self.chain_spec)
o = c.entry_count(self.address, sender_address=self.accounts[0])
r = self.conn.do(o)
r = abi_decode_single(ABIContractType.UINT256, r)
self.assertEqual(r, 1)
self.assertEqual(r, 0)
def test_2_add(self):
b = os.urandom(20)
a = to_checksum_address(b.hex())
(tx_hash, o) = self.o.add(self.address, self.accounts[0], a)
nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
c = AccountsIndex(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
(tx_hash, o) = c.add(self.address, self.accounts[0], a)
r = self.conn.do(o)
self.assertEqual(tx_hash, r)
@ -81,7 +87,7 @@ class Test(EthTesterCase):
rcpt = self.conn.do(o)
self.helper.mine_block()
o = self.o.have(self.address, a, sender_address=self.accounts[0])
o = c.have(self.address, a, sender_address=self.accounts[0])
r = self.conn.do(o)
@ -89,7 +95,9 @@ class Test(EthTesterCase):
b = os.urandom(20)
a = to_checksum_address(b.hex())
(tx_hash, o) = self.o.add(self.address, self.accounts[0], a, tx_format=TxFormat.RLP_SIGNED)
nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
c = AccountsIndex(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
(tx_hash, o) = c.add(self.address, self.accounts[0], a, tx_format=TxFormat.RLP_SIGNED)
#r = self.conn.do(o)
#self.assertEqual(tx_hash, r)
logg.debug('o {}'.format(o))

View File

@ -4,9 +4,9 @@ pragma solidity >0.6.11;
contract CustodialAccountIndex {
address[] public entry;
mapping(address => uint256) public entryIndex;
uint256 public count;
address[] entries;
mapping(address => uint256) entryIndex;
uint256 count;
mapping(address => bool) writers;
address public owner;
address newOwner;
@ -16,7 +16,7 @@ contract CustodialAccountIndex {
constructor() public {
owner = msg.sender;
entry.push(address(0));
entries.push(address(0));
count = 1;
}
@ -32,29 +32,38 @@ contract CustodialAccountIndex {
return true;
}
// Implements AccountsIndex
function add(address _account) external returns (bool) {
require(writers[msg.sender]);
require(entryIndex[_account] == 0);
entry.push(_account);
entryIndex[_account] = count;
count++;
emit AccountAdded(_account, count-1);
entryIndex[_account] = entries.length;
entries.push(_account);
emit AccountAdded(_account, entries.length-1);
return true;
}
// Implements AccountsIndex
function have(address _account) external view returns (bool) {
return entryIndex[_account] > 0;
}
function entryCount() public returns (uint256) {
return count;
// Implements AccountsIndex
function entry(uint256 _idx) public returns (address) {
return entries[_idx+1];
}
// Implements AccountsIndex
function entryCount() public returns (uint256) {
return count - 1;
}
// Implements EIP173
function transferOwnership(address _newOwner) public returns (bool) {
require(msg.sender == owner);
newOwner = _newOwner;
}
// Implements OwnedAccepter
function acceptOwnership() public returns (bool) {
address oldOwner;
@ -65,6 +74,7 @@ contract CustodialAccountIndex {
emit OwnershipTransferred(oldOwner, owner);
}
// Implements EIP165
function supportsInterface(bytes4 _sum) public pure returns (bool) {
if (_sum == 0xcbdb05c7) { // AccountsIndex
return true;