2021-03-31 16:07:23 +02:00
|
|
|
# standard imports
|
2020-11-13 23:19:48 +01:00
|
|
|
import os
|
|
|
|
import unittest
|
|
|
|
import json
|
2020-11-16 19:19:13 +01:00
|
|
|
import logging
|
2020-11-13 23:19:48 +01:00
|
|
|
|
2021-03-31 16:07:23 +02:00
|
|
|
# external imports
|
|
|
|
from chainlib.eth.unittest.ethtester import EthTesterCase
|
|
|
|
from chainlib.connection import RPCConnection
|
2021-04-30 12:46:53 +02:00
|
|
|
from chainlib.eth.nonce import RPCNonceOracle
|
2021-03-31 16:07:23 +02:00
|
|
|
from chainlib.eth.address import to_checksum_address
|
|
|
|
from chainlib.eth.tx import (
|
|
|
|
receipt,
|
|
|
|
transaction,
|
|
|
|
TxFormat,
|
|
|
|
)
|
|
|
|
from chainlib.eth.contract import (
|
|
|
|
abi_decode_single,
|
|
|
|
ABIContractType,
|
|
|
|
)
|
2023-02-12 08:51:58 +01:00
|
|
|
from chainlib.eth.block import (
|
|
|
|
block_latest,
|
|
|
|
block_by_number,
|
|
|
|
)
|
2021-03-31 16:07:23 +02:00
|
|
|
|
|
|
|
# local imports
|
2021-04-30 12:46:53 +02:00
|
|
|
from eth_accounts_index.registry import AccountRegistry
|
|
|
|
from eth_accounts_index import AccountsIndex
|
2020-11-13 23:19:48 +01:00
|
|
|
|
2020-11-16 19:19:13 +01:00
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
logg = logging.getLogger()
|
|
|
|
|
2020-11-13 23:19:48 +01:00
|
|
|
testdir = os.path.dirname(__file__)
|
|
|
|
|
|
|
|
|
2021-03-31 16:07:23 +02:00
|
|
|
class TestNonceOracle:
|
2020-11-13 23:19:48 +01:00
|
|
|
|
2021-03-31 16:07:23 +02:00
|
|
|
def __init__(self, address, default_value=0):
|
|
|
|
self.nonce = default_value
|
2020-11-13 23:19:48 +01:00
|
|
|
|
|
|
|
|
2021-03-31 16:07:23 +02:00
|
|
|
def next_nonce(self):
|
|
|
|
nonce = self.nonce
|
|
|
|
self.nonce += 1
|
|
|
|
return nonce
|
2020-11-13 23:19:48 +01:00
|
|
|
|
|
|
|
|
2021-03-31 16:07:23 +02:00
|
|
|
class Test(EthTesterCase):
|
2020-11-13 23:19:48 +01:00
|
|
|
|
2021-03-31 16:07:23 +02:00
|
|
|
def setUp(self):
|
|
|
|
super(Test, self).setUp()
|
|
|
|
nonce_oracle = TestNonceOracle(self.accounts[0])
|
2021-04-30 12:46:53 +02:00
|
|
|
c = AccountRegistry(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
|
|
|
(tx_hash, o) = c.constructor(self.accounts[0])
|
2021-03-31 16:07:23 +02:00
|
|
|
self.conn = RPCConnection.connect(self.chain_spec, 'default')
|
|
|
|
r = self.conn.do(o)
|
2023-02-05 05:54:50 +01:00
|
|
|
logg.debug(f'deployed with hash {r}')
|
|
|
|
|
2021-03-31 16:07:23 +02:00
|
|
|
o = receipt(r)
|
|
|
|
r = self.conn.do(o)
|
|
|
|
self.address = to_checksum_address(r['contract_address'])
|
|
|
|
|
2021-04-30 12:46:53 +02:00
|
|
|
(tx_hash, o) = c.add_writer(self.address, self.accounts[0], self.accounts[0])
|
2021-03-31 16:07:23 +02:00
|
|
|
r = self.conn.do(o)
|
|
|
|
|
|
|
|
o = receipt(r)
|
|
|
|
r = self.conn.do(o)
|
|
|
|
self.assertEqual(r['status'], 1)
|
|
|
|
|
|
|
|
|
|
|
|
def test_1_count(self):
|
2021-04-30 12:46:53 +02:00
|
|
|
#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])
|
2021-03-31 16:07:23 +02:00
|
|
|
r = self.conn.do(o)
|
|
|
|
r = abi_decode_single(ABIContractType.UINT256, r)
|
2021-04-30 12:46:53 +02:00
|
|
|
self.assertEqual(r, 0)
|
2021-03-31 16:07:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_2_add(self):
|
|
|
|
b = os.urandom(20)
|
|
|
|
a = to_checksum_address(b.hex())
|
|
|
|
|
2021-04-30 12:46:53 +02:00
|
|
|
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)
|
2021-03-31 16:07:23 +02:00
|
|
|
r = self.conn.do(o)
|
|
|
|
self.assertEqual(tx_hash, r)
|
|
|
|
|
|
|
|
o = receipt(tx_hash)
|
|
|
|
rcpt = self.conn.do(o)
|
|
|
|
|
|
|
|
self.helper.mine_block()
|
2021-04-30 12:46:53 +02:00
|
|
|
o = c.have(self.address, a, sender_address=self.accounts[0])
|
2021-03-31 16:07:23 +02:00
|
|
|
r = self.conn.do(o)
|
|
|
|
|
|
|
|
|
|
|
|
def test_3_add_rlpsigned(self):
|
|
|
|
b = os.urandom(20)
|
|
|
|
a = to_checksum_address(b.hex())
|
|
|
|
|
2021-04-30 12:46:53 +02:00
|
|
|
nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
|
|
|
|
c = AccountsIndex(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
2021-05-13 17:55:05 +02:00
|
|
|
(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)
|
2021-03-31 16:07:23 +02:00
|
|
|
logg.debug('o {}'.format(o))
|
|
|
|
|
|
|
|
|
2021-05-13 17:55:05 +02:00
|
|
|
def test_4_indices(self):
|
|
|
|
nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
|
|
|
|
c = AccountsIndex(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
|
|
|
|
|
|
|
o = c.entry_count(self.address, sender_address=self.accounts[0])
|
|
|
|
r = self.conn.do(o)
|
|
|
|
count = c.parse_entry_count(r)
|
|
|
|
self.assertEqual(count, 0)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
b = os.urandom(20)
|
|
|
|
aa = to_checksum_address(b.hex())
|
|
|
|
(tx_hash, o) = c.add(self.address, self.accounts[0], aa)
|
|
|
|
r = self.conn.do(o)
|
|
|
|
|
|
|
|
o = c.entry_count(self.address, sender_address=self.accounts[0])
|
|
|
|
r = self.conn.do(o)
|
|
|
|
count = c.parse_entry_count(r)
|
|
|
|
self.assertEqual(count, 2)
|
|
|
|
|
|
|
|
|
2023-02-12 07:07:14 +01:00
|
|
|
def test_5_deactivate(self):
|
2023-02-05 05:54:50 +01:00
|
|
|
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)
|
|
|
|
count = c.parse_entry_count(r)
|
|
|
|
self.assertEqual(count, 1)
|
|
|
|
|
|
|
|
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)
|
|
|
|
count = c.parse_entry_count(r)
|
|
|
|
self.assertEqual(count, 2)
|
|
|
|
|
2023-02-12 07:07:14 +01:00
|
|
|
(tx_hash, o) = c.deactivate(self.address, self.accounts[0], a)
|
|
|
|
r = self.conn.do(o)
|
|
|
|
o = receipt(r)
|
2023-02-05 05:54:50 +01:00
|
|
|
r = self.conn.do(o)
|
2023-02-12 07:07:14 +01:00
|
|
|
self.assertEqual(r['status'], 1)
|
2023-02-05 05:54:50 +01:00
|
|
|
|
|
|
|
o = c.entry_count(self.address, sender_address=self.accounts[0])
|
|
|
|
r = self.conn.do(o)
|
|
|
|
count = c.parse_entry_count(r)
|
2023-02-12 07:07:14 +01:00
|
|
|
self.assertEqual(count, 2)
|
2021-05-13 17:55:05 +02:00
|
|
|
|
2023-02-12 07:07:14 +01:00
|
|
|
o = c.have(self.address, a, sender_address=self.accounts[0])
|
|
|
|
r = self.conn.do(o)
|
|
|
|
self.assertEqual(int(r, 16), 1)
|
|
|
|
|
|
|
|
o = c.is_active(self.address, a, sender_address=self.accounts[0])
|
|
|
|
r = self.conn.do(o)
|
|
|
|
self.assertEqual(int(r, 16), 0)
|
|
|
|
|
|
|
|
(tx_hash, o) = c.activate(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.have(self.address, a, sender_address=self.accounts[0])
|
|
|
|
r = self.conn.do(o)
|
|
|
|
self.assertEqual(int(r, 16), 1)
|
|
|
|
|
|
|
|
o = c.is_active(self.address, a, sender_address=self.accounts[0])
|
|
|
|
r = self.conn.do(o)
|
|
|
|
self.assertEqual(int(r, 16), 1)
|
2021-05-13 17:55:05 +02:00
|
|
|
|
2020-11-13 23:19:48 +01:00
|
|
|
|
2023-02-12 08:51:58 +01:00
|
|
|
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))
|
|
|
|
|
|
|
|
|
2020-11-13 23:19:48 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|