From 9363ed3bfb166994fab02a06c8b8ea37548bc49f Mon Sep 17 00:00:00 2001 From: nolash Date: Fri, 30 Apr 2021 12:46:53 +0200 Subject: [PATCH] Split index interface and registry implementation --- python/eth_accounts_index/__init__.py | 2 +- .../eth_accounts_index/data/AccountsIndex.bin | 2 +- .../data/AccountsIndex.json | 2 +- python/eth_accounts_index/index.py | 121 ++++++++++++++++++ python/eth_accounts_index/registry.py | 103 +-------------- python/tests/test_app.py | 26 ++-- solidity/AccountsIndex.sol | 30 +++-- 7 files changed, 168 insertions(+), 118 deletions(-) create mode 100644 python/eth_accounts_index/index.py diff --git a/python/eth_accounts_index/__init__.py b/python/eth_accounts_index/__init__.py index bb7364b..f42a3d9 100644 --- a/python/eth_accounts_index/__init__.py +++ b/python/eth_accounts_index/__init__.py @@ -1 +1 @@ -from .registry import AccountRegistry +from .index import AccountsIndex diff --git a/python/eth_accounts_index/data/AccountsIndex.bin b/python/eth_accounts_index/data/AccountsIndex.bin index 31e80c3..c5a049c 100644 --- a/python/eth_accounts_index/data/AccountsIndex.bin +++ b/python/eth_accounts_index/data/AccountsIndex.bin @@ -1 +1 @@ -608060405234801561001057600080fd5b5033600460006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555060008080600181540180825580915050600190039060005260206000200160009091909190916101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506001600281905550610d4b806100cb6000396000f3fe608060405234801561001057600080fd5b50600436106100d1576000357c01000000000000000000000000000000000000000000000000000000009004806379ba50971161008e57806379ba5097146101d25780638da5cb5b146101f0578063da2824a81461020e578063e0db665b1461023e578063e2095c071461026e578063f2fde38b1461029e576100d1565b806301ffc9a7146100d657806306661abd146101065780630a3b0a4f146101245780630cbb0f83146101545780633ef25013146101725780635ae06f7e146101a2575b600080fd5b6100f060048036038101906100eb9190610ae0565b6102ce565b6040516100fd9190610b7a565b60405180910390f35b61010e610426565b60405161011b9190610b95565b60405180910390f35b61013e60048036038101906101399190610ab7565b61042c565b60405161014b9190610b7a565b60405180910390f35b61015c6105ec565b6040516101699190610b95565b60405180910390f35b61018c60048036038101906101879190610ab7565b6105f6565b6040516101999190610b7a565b60405180910390f35b6101bc60048036038101906101b79190610ab7565b610641565b6040516101c99190610b7a565b60405180910390f35b6101da6106f5565b6040516101e79190610b7a565b60405180910390f35b6101f861089c565b6040516102059190610b5f565b60405180910390f35b61022860048036038101906102239190610ab7565b6108c2565b6040516102359190610b7a565b60405180910390f35b61025860048036038101906102539190610ab7565b61097f565b6040516102659190610b95565b60405180910390f35b61028860048036038101906102839190610b09565b610997565b6040516102959190610b5f565b60405180910390f35b6102b860048036038101906102b39190610ab7565b6109d6565b6040516102c59190610b7a565b60405180910390f35b600063cbdb05c77c010000000000000000000000000000000000000000000000000000000002827bffffffffffffffffffffffffffffffffffffffffffffffffffffffff191614156103235760019050610421565b6301ffc9a77c010000000000000000000000000000000000000000000000000000000002827bffffffffffffffffffffffffffffffffffffffffffffffffffffffff191614156103765760019050610421565b639493f8b27c010000000000000000000000000000000000000000000000000000000002827bffffffffffffffffffffffffffffffffffffffffffffffffffffffff191614156103c95760019050610421565b6337a47be47c010000000000000000000000000000000000000000000000000000000002827bffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916141561041c5760019050610421565b600090505b919050565b60025481565b6000600360003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060009054906101000a900460ff1661048457600080fd5b6000600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054146104d057600080fd5b6000829080600181540180825580915050600190039060005260206000200160009091909190916101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550600254600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055506002600081548092919061058c90610c58565b919050555060016002546105a09190610bb0565b8273ffffffffffffffffffffffffffffffffffffffff167f5ed3bdd47b9af629827a8d129aa39c870b10c03f0153fe9ddb8e84b665061acd60405160405180910390a360019050919050565b6000600254905090565b600080600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054119050919050565b60003373ffffffffffffffffffffffffffffffffffffffff16600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff161461069d57600080fd5b600360008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060006101000a81549060ff021916905560019050919050565b600080600560009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161461075257600080fd5b600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050600560009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16600460006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506000600560006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e060405160405180910390a35090565b600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60003373ffffffffffffffffffffffffffffffffffffffff16600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff161461091e57600080fd5b6001600360008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060006101000a81548160ff02191690831515021790555060019050919050565b60016020528060005260406000206000915090505481565b600081815481106109a757600080fd5b906000526020600020016000915054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6000600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614610a3257600080fd5b81600560006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550919050565b600081359050610a8781610cd0565b92915050565b600081359050610a9c81610ce7565b92915050565b600081359050610ab181610cfe565b92915050565b600060208284031215610ac957600080fd5b6000610ad784828501610a78565b91505092915050565b600060208284031215610af257600080fd5b6000610b0084828501610a8d565b91505092915050565b600060208284031215610b1b57600080fd5b6000610b2984828501610aa2565b91505092915050565b610b3b81610be4565b82525050565b610b4a81610bf6565b82525050565b610b5981610c4e565b82525050565b6000602082019050610b746000830184610b32565b92915050565b6000602082019050610b8f6000830184610b41565b92915050565b6000602082019050610baa6000830184610b50565b92915050565b6000610bbb82610c4e565b9150610bc683610c4e565b925082821015610bd957610bd8610ca1565b5b828203905092915050565b6000610bef82610c2e565b9050919050565b60008115159050919050565b60007fffffffff0000000000000000000000000000000000000000000000000000000082169050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b6000610c6382610c4e565b91507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff821415610c9657610c95610ca1565b5b600182019050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b610cd981610be4565b8114610ce457600080fd5b50565b610cf081610c02565b8114610cfb57600080fd5b50565b610d0781610c4e565b8114610d1257600080fd5b5056fea2646970667358221220c56f0a6472d523df4121387f28d200b09cbfd10338652250ac97b823e729ed2c64736f6c63430008030033 \ No newline at end of file +608060405234801561001057600080fd5b5033600460006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555060008080600181540180825580915050600190039060005260206000200160009091909190916101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506001600281905550610d0a806100cb6000396000f3fe608060405234801561001057600080fd5b50600436106100bb576000357c01000000000000000000000000000000000000000000000000000000009004806379ba50971161008357806379ba50971461019e5780638da5cb5b146101bc578063da2824a8146101da578063e2095c071461020a578063f2fde38b1461023a576100bb565b806301ffc9a7146100c05780630a3b0a4f146100f05780630cbb0f83146101205780633ef250131461013e5780635ae06f7e1461016e575b600080fd5b6100da60048036038101906100d59190610a92565b61026a565b6040516100e79190610b2c565b60405180910390f35b61010a60048036038101906101059190610a69565b6103c2565b6040516101179190610b2c565b60405180910390f35b610128610570565b6040516101359190610b47565b60405180910390f35b61015860048036038101906101539190610a69565b610586565b6040516101659190610b2c565b60405180910390f35b61018860048036038101906101839190610a69565b6105d1565b6040516101959190610b2c565b60405180910390f35b6101a6610685565b6040516101b39190610b2c565b60405180910390f35b6101c461082c565b6040516101d19190610b11565b60405180910390f35b6101f460048036038101906101ef9190610a69565b610852565b6040516102019190610b2c565b60405180910390f35b610224600480360381019061021f9190610abb565b61090f565b6040516102319190610b11565b60405180910390f35b610254600480360381019061024f9190610a69565b610988565b6040516102619190610b2c565b60405180910390f35b600063cbdb05c77c010000000000000000000000000000000000000000000000000000000002827bffffffffffffffffffffffffffffffffffffffffffffffffffffffff191614156102bf57600190506103bd565b6301ffc9a77c010000000000000000000000000000000000000000000000000000000002827bffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916141561031257600190506103bd565b639493f8b27c010000000000000000000000000000000000000000000000000000000002827bffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916141561036557600190506103bd565b6337a47be47c010000000000000000000000000000000000000000000000000000000002827bffffffffffffffffffffffffffffffffffffffffffffffffffffffff191614156103b857600190506103bd565b600090505b919050565b6000600360003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060009054906101000a900460ff1661041a57600080fd5b6000600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541461046657600080fd5b600080549050600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055506000829080600181540180825580915050600190039060005260206000200160009091909190916101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555060016000805490506105249190610bb8565b8273ffffffffffffffffffffffffffffffffffffffff167f5ed3bdd47b9af629827a8d129aa39c870b10c03f0153fe9ddb8e84b665061acd60405160405180910390a360019050919050565b600060016002546105819190610bb8565b905090565b600080600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054119050919050565b60003373ffffffffffffffffffffffffffffffffffffffff16600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff161461062d57600080fd5b600360008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060006101000a81549060ff021916905560019050919050565b600080600560009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16146106e257600080fd5b600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050600560009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16600460006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506000600560006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e060405160405180910390a35090565b600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60003373ffffffffffffffffffffffffffffffffffffffff16600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16146108ae57600080fd5b6001600360008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060006101000a81548160ff02191690831515021790555060019050919050565b60008060018361091f9190610b62565b81548110610956577f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b9060005260206000200160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050919050565b6000600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16146109e457600080fd5b81600560006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550919050565b600081359050610a3981610c8f565b92915050565b600081359050610a4e81610ca6565b92915050565b600081359050610a6381610cbd565b92915050565b600060208284031215610a7b57600080fd5b6000610a8984828501610a2a565b91505092915050565b600060208284031215610aa457600080fd5b6000610ab284828501610a3f565b91505092915050565b600060208284031215610acd57600080fd5b6000610adb84828501610a54565b91505092915050565b610aed81610bec565b82525050565b610afc81610bfe565b82525050565b610b0b81610c56565b82525050565b6000602082019050610b266000830184610ae4565b92915050565b6000602082019050610b416000830184610af3565b92915050565b6000602082019050610b5c6000830184610b02565b92915050565b6000610b6d82610c56565b9150610b7883610c56565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff03821115610bad57610bac610c60565b5b828201905092915050565b6000610bc382610c56565b9150610bce83610c56565b925082821015610be157610be0610c60565b5b828203905092915050565b6000610bf782610c36565b9050919050565b60008115159050919050565b60007fffffffff0000000000000000000000000000000000000000000000000000000082169050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b610c9881610bec565b8114610ca357600080fd5b50565b610caf81610c0a565b8114610cba57600080fd5b50565b610cc681610c56565b8114610cd157600080fd5b5056fea264697066735822122098d54d1b537a089f2dfb228d51384fe0c6a1680d1f6248ae10f64e3e5610c44564736f6c63430008030033 \ No newline at end of file diff --git a/python/eth_accounts_index/data/AccountsIndex.json b/python/eth_accounts_index/data/AccountsIndex.json index e8dfc61..d6c414a 100644 --- a/python/eth_accounts_index/data/AccountsIndex.json +++ b/python/eth_accounts_index/data/AccountsIndex.json @@ -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"}] diff --git a/python/eth_accounts_index/index.py b/python/eth_accounts_index/index.py new file mode 100644 index 0000000..7d5f30a --- /dev/null +++ b/python/eth_accounts_index/index.py @@ -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 diff --git a/python/eth_accounts_index/registry.py b/python/eth_accounts_index/registry.py index 0f38083..2b66c0f 100644 --- a/python/eth_accounts_index/registry.py +++ b/python/eth_accounts_index/registry.py @@ -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) diff --git a/python/tests/test_app.py b/python/tests/test_app.py index e070260..aec6d92 100644 --- a/python/tests/test_app.py +++ b/python/tests/test_app.py @@ -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)) diff --git a/solidity/AccountsIndex.sol b/solidity/AccountsIndex.sol index 066d334..3664dc3 100644 --- a/solidity/AccountsIndex.sol +++ b/solidity/AccountsIndex.sol @@ -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;