Add time added

This commit is contained in:
lash 2023-02-12 07:51:58 +00:00
parent a30614efed
commit 9a8fb62113
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
8 changed files with 155 additions and 27 deletions

View File

@ -1,6 +1,8 @@
* 0.3.3
- Rename cli script from deploy to publish
- Refactor cli scripts to use current chainlib setup
- Add time added entry
- Add activate and deactivate option
* 0.3.2
- Add remove option
* 0.3.0

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":"_executor","type":"address"},{"indexed":false,"internalType":"address","name":"_account","type":"address"}],"name":"AddressActive","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_executor","type":"address"},{"indexed":true,"internalType":"uint256","name":"_accountIndex","type":"uint256"},{"indexed":false,"internalType":"address","name":"_account","type":"address"}],"name":"AddressAdded","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_executor","type":"address"},{"indexed":false,"internalType":"address","name":"_account","type":"address"}],"name":"AddressInactive","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_executor","type":"address"},{"indexed":false,"internalType":"address","name":"_account","type":"address"}],"name":"AddressRemoved","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":[{"internalType":"address","name":"_account","type":"address"}],"name":"activate","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":"_account","type":"address"}],"name":"deactivate","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":"","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":[{"internalType":"address","name":"_account","type":"address"}],"name":"isActive","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":"_executor","type":"address"},{"indexed":false,"internalType":"address","name":"_account","type":"address"}],"name":"AddressActive","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_executor","type":"address"},{"indexed":false,"internalType":"address","name":"_account","type":"address"}],"name":"AddressAdded","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_executor","type":"address"},{"indexed":false,"internalType":"address","name":"_account","type":"address"}],"name":"AddressInactive","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_executor","type":"address"},{"indexed":false,"internalType":"address","name":"_account","type":"address"}],"name":"AddressRemoved","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":[{"internalType":"address","name":"_account","type":"address"}],"name":"activate","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":"_account","type":"address"}],"name":"deactivate","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":"_i","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":"","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":[{"internalType":"address","name":"_account","type":"address"}],"name":"isActive","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":"address","name":"_account","type":"address"}],"name":"remove","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":"_account","type":"address"}],"name":"time","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_newOwner","type":"address"}],"name":"transferOwnership","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}]

View File

@ -41,12 +41,32 @@ class AccountsIndex(TxFactory):
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 remove(self, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC):
return self.__single_address_method('remove', contract_address, sender_address, address, tx_format)
def activate(self, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC):
return self.__single_address_method('activate', contract_address, sender_address, address, tx_format)
def deactivate(self, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC):
return self.__single_address_method('deactivate', contract_address, sender_address, address, tx_format)
def time(self, contract_address, address, sender_address=ZERO_ADDRESS, id_generator=None):
j = JSONRPCRequest(id_generator)
o = j.template()
o['method'] = 'eth_call'
enc = ABIContractEncoder()
enc.method('time')
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 have(self, contract_address, address, sender_address=ZERO_ADDRESS, id_generator=None):
j = JSONRPCRequest(id_generator)
o = j.template()
@ -90,7 +110,7 @@ class AccountsIndex(TxFactory):
def count(self, contract_address, sender_address=ZERO_ADDRESS, id_generator=None):
return self.entry_count(contract_address, sender_address=sender_address, id_generator=id_generator)
def entry(self, contract_address, idx, sender_address=ZERO_ADDRESS, id_generator=None):
j = JSONRPCRequest(id_generator)
o = j.template()

View File

@ -4,7 +4,7 @@ version = 0.3.3
description = Accounts index evm contract tooling with permissioned writes
author = Louis Holbrook
author_email = dev@holbrook.no
url = https://gitlab.com/cicnet/eth-accounts-index
url = https://holbrook.no/src/eth-accounts-index
keywords =
ethereum
classifiers =

View File

@ -20,13 +20,6 @@ f.close()
setup(
package_data={
'': [
'data/*.abi.json',
'data/*.bin',
],
},
include_package_data=True,
install_requires=requirements,
tests_require=test_requirements,
)

View File

@ -18,6 +18,10 @@ from chainlib.eth.contract import (
abi_decode_single,
ABIContractType,
)
from chainlib.eth.block import (
block_latest,
block_by_number,
)
# local imports
from eth_accounts_index.registry import AccountRegistry
@ -186,5 +190,77 @@ class Test(EthTesterCase):
self.assertEqual(int(r, 16), 1)
def test_6_remove(self):
nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
c = AccountsIndex(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
b = os.urandom(20)
a = to_checksum_address(b.hex())
(tx_hash, o) = c.add(self.address, self.accounts[0], a)
r = self.conn.do(o)
o = c.entry_count(self.address, sender_address=self.accounts[0])
r = self.conn.do(o)
self.assertEqual(int(r, 16), 1)
(tx_hash, o) = c.remove(self.address, self.accounts[0], a)
r = self.conn.do(o)
o = receipt(r)
r = self.conn.do(o)
self.assertEqual(r['status'], 1)
o = c.entry_count(self.address, sender_address=self.accounts[0])
r = self.conn.do(o)
self.assertEqual(int(r, 16), 0)
addrs = []
for i in range(8):
b = os.urandom(20)
addrs.append(to_checksum_address(b.hex()))
(tx_hash, o) = c.add(self.address, self.accounts[0], addrs[i])
r = self.conn.do(o)
o = receipt(tx_hash)
r = self.conn.do(o)
self.assertEqual(r['status'], 1)
o = c.entry_count(self.address, sender_address=self.accounts[0])
r = self.conn.do(o)
self.assertEqual(int(r, 16), 8)
(tx_hash, o) = c.remove(self.address, self.accounts[0], addrs[4])
r = self.conn.do(o)
o = receipt(r)
r = self.conn.do(o)
self.assertEqual(r['status'], 1)
r = []
for i in range(7):
o = c.entry(self.address, i, sender_address=self.accounts[0])
r.append(self.conn.do(o))
self.assertEqual(len(r), 7)
self.assertNotIn(addrs[4], r)
def test_7_time(self):
nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
c = AccountsIndex(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
b = os.urandom(20)
a = to_checksum_address(b.hex())
(tx_hash, o) = c.add(self.address, self.accounts[0], a)
self.conn.do(o)
o = block_latest()
r = self.conn.do(o)
o = block_by_number(r)
r = self.conn.do(o)
t = r['timestamp']
o = c.time(self.address, a, sender_address=self.accounts[0])
r = self.conn.do(o)
self.assertEqual(t, int(r, 16))
if __name__ == '__main__':
unittest.main()

View File

@ -5,14 +5,14 @@ pragma solidity >0.6.12;
contract CustodialAccountIndex {
uint256 constant blockedField = 1 << 64;
address[] public entry;
uint256 constant blockedField = 1 << 128;
address[] entryList;
mapping(address => uint256) public entryIndex;
mapping(address => bool) writers;
address public owner;
address newOwner;
event AddressAdded(address indexed _executor, uint256 indexed _accountIndex, address _account); // AccountsIndex
event AddressAdded(address indexed _executor, address _account); // AccountsIndex
event AddressActive(address indexed _executor, address _account);
event AddressInactive(address indexed _executor, address _account);
event AddressRemoved(address indexed _executor, address _account);
@ -20,10 +20,11 @@ contract CustodialAccountIndex {
constructor() public {
owner = msg.sender;
entryList.push(address(0));
}
function entryCount() external returns (uint256) {
return entry.length;
return entryList.length - 1;
}
function addWriter(address _writer) public returns (bool) {
@ -41,25 +42,38 @@ contract CustodialAccountIndex {
// Implements AccountsIndex
function add(address _account) external returns (bool) {
uint256 i;
uint256 _entry;
require(writers[msg.sender]);
require(entryIndex[_account] == 0);
require(entry.length < (1 << 128));
i = entry.length;
entry.push(_account);
entryIndex[_account] = i;
emit AddressAdded(msg.sender, i, _account);
require(entryList.length < (1 << 64));
i = entryList.length;
entryList.push(_account);
_entry = uint64(i);
_entry |= block.timestamp << 64;
entryIndex[_account] = _entry;
emit AddressAdded(msg.sender, _account);
return true;
}
// Implements AccountsIndex
// Deactivate account, without removing the entry. The entry will still be part of the entry count.
function deactivate(address _account) external returns (bool) {
function remove(address _account) external returns (bool) {
uint256 i;
uint256 l;
require(writers[msg.sender]);
require(entryIndex[_account] > 0 && entryIndex[_account] & blockedField == 0);
entryIndex[_account] <<= 65;
entryIndex[_account] |= blockedField;
emit AddressInactive(msg.sender, _account);
require(this.have(_account));
l = entryList.length - 1;
i = entryIndex[_account];
if (i < l) {
entryList[i] = entryList[l];
}
entryList.pop();
entryIndex[_account] = 0;
emit AddressRemoved(msg.sender, _account);
return true;
}
// Implements AccountsIndex
@ -67,10 +81,33 @@ contract CustodialAccountIndex {
function activate(address _account) external returns (bool) {
require(writers[msg.sender]);
require(entryIndex[_account] > 0 && entryIndex[_account] & blockedField == blockedField);
entryIndex[_account] >>= 65;
entryIndex[_account] >>= 129;
emit AddressActive(msg.sender, _account);
}
// Implements AccountsIndex
// Deactivate account, without removing the entry. The entry will still be part of the entry count.
function deactivate(address _account) external returns (bool) {
require(writers[msg.sender]);
require(entryIndex[_account] > 0 && entryIndex[_account] & blockedField == 0);
entryIndex[_account] <<= 129;
entryIndex[_account] |= blockedField;
emit AddressInactive(msg.sender, _account);
return true;
}
// Implements AccountsIndex
function entry(uint256 _i) external returns (address) {
return entryList[_i + 1];
}
// Implements AccountsIndex
function time(address _account) external returns (uint256) {
require(entryIndex[_account] > 0);
return entryIndex[_account] >> 64;
}
// Implements AccountsIndex
function have(address _account) external view returns (bool) {
return entryIndex[_account] > 0;