2021-02-01 18:12:51 +01:00
|
|
|
# standard imports
|
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
import time
|
|
|
|
|
|
|
|
# third-party imports
|
|
|
|
import pytest
|
|
|
|
import celery
|
2021-03-29 15:27:53 +02:00
|
|
|
from chainlib.connection import RPCConnection
|
|
|
|
from chainlib.eth.nonce import RPCNonceOracle
|
|
|
|
from chainlib.eth.tx import receipt
|
2021-05-19 17:11:08 +02:00
|
|
|
from eth_accounts_index.registry import AccountRegistry
|
2021-03-29 15:27:53 +02:00
|
|
|
from hexathon import strip_0x
|
2021-04-04 14:40:59 +02:00
|
|
|
from chainqueue.db.enum import StatusEnum
|
|
|
|
from chainqueue.db.models.otx import Otx
|
2021-02-01 18:12:51 +01:00
|
|
|
|
|
|
|
# local imports
|
|
|
|
from cic_eth.error import OutOfGasError
|
|
|
|
from cic_eth.db.models.base import SessionBase
|
|
|
|
from cic_eth.db.models.nonce import Nonce
|
|
|
|
from cic_eth.db.models.role import AccountRole
|
|
|
|
|
2021-03-29 15:27:53 +02:00
|
|
|
logg = logging.getLogger()
|
2021-02-01 18:12:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_create_account(
|
|
|
|
default_chain_spec,
|
2021-03-29 15:27:53 +02:00
|
|
|
eth_rpc,
|
2021-02-01 18:12:51 +01:00
|
|
|
init_database,
|
|
|
|
celery_session_worker,
|
2021-03-29 15:27:53 +02:00
|
|
|
caplog,
|
2021-02-01 18:12:51 +01:00
|
|
|
):
|
|
|
|
s = celery.signature(
|
|
|
|
'cic_eth.eth.account.create',
|
|
|
|
[
|
|
|
|
'foo',
|
2021-03-29 15:27:53 +02:00
|
|
|
default_chain_spec.asdict(),
|
2021-02-01 18:12:51 +01:00
|
|
|
],
|
|
|
|
)
|
|
|
|
t = s.apply_async()
|
|
|
|
r = t.get()
|
|
|
|
|
|
|
|
session = SessionBase.create_session()
|
|
|
|
q = session.query(Nonce).filter(Nonce.address_hex==r)
|
|
|
|
o = q.first()
|
|
|
|
session.close()
|
|
|
|
assert o != None
|
|
|
|
assert o.nonce == 0
|
|
|
|
|
|
|
|
s = celery.signature(
|
|
|
|
'cic_eth.eth.account.have',
|
|
|
|
[
|
|
|
|
r,
|
2021-03-29 15:27:53 +02:00
|
|
|
default_chain_spec.asdict(),
|
2021-02-01 18:12:51 +01:00
|
|
|
],
|
|
|
|
)
|
|
|
|
t = s.apply_async()
|
|
|
|
assert r == t.get()
|
|
|
|
|
|
|
|
|
|
|
|
def test_register_account(
|
|
|
|
default_chain_spec,
|
2021-03-29 15:27:53 +02:00
|
|
|
account_registry,
|
2021-02-01 18:12:51 +01:00
|
|
|
init_database,
|
|
|
|
init_eth_tester,
|
2021-03-29 15:27:53 +02:00
|
|
|
eth_accounts,
|
|
|
|
eth_rpc,
|
2021-02-01 18:12:51 +01:00
|
|
|
cic_registry,
|
|
|
|
eth_empty_accounts,
|
2021-03-29 15:27:53 +02:00
|
|
|
custodial_roles,
|
|
|
|
call_sender,
|
|
|
|
celery_session_worker,
|
2021-02-01 18:12:51 +01:00
|
|
|
):
|
|
|
|
|
2021-03-06 18:55:51 +01:00
|
|
|
s_nonce = celery.signature(
|
2021-04-04 14:40:59 +02:00
|
|
|
'cic_eth.eth.nonce.reserve_nonce',
|
2021-02-01 18:12:51 +01:00
|
|
|
[
|
|
|
|
eth_empty_accounts[0],
|
2021-04-04 14:40:59 +02:00
|
|
|
default_chain_spec.asdict(),
|
2021-03-29 15:27:53 +02:00
|
|
|
custodial_roles['ACCOUNT_REGISTRY_WRITER'],
|
2021-03-06 18:55:51 +01:00
|
|
|
],
|
|
|
|
queue=None,
|
|
|
|
)
|
|
|
|
s_register = celery.signature(
|
|
|
|
'cic_eth.eth.account.register',
|
|
|
|
[
|
2021-03-29 15:27:53 +02:00
|
|
|
default_chain_spec.asdict(),
|
|
|
|
custodial_roles['ACCOUNT_REGISTRY_WRITER'],
|
2021-02-01 18:12:51 +01:00
|
|
|
],
|
2021-03-29 15:27:53 +02:00
|
|
|
queue=None,
|
2021-02-01 18:12:51 +01:00
|
|
|
)
|
2021-03-06 18:55:51 +01:00
|
|
|
s_nonce.link(s_register)
|
|
|
|
t = s_nonce.apply_async()
|
2021-02-01 18:12:51 +01:00
|
|
|
address = t.get()
|
2021-03-06 18:55:51 +01:00
|
|
|
for r in t.collect():
|
2021-03-29 15:27:53 +02:00
|
|
|
logg.debug('r {}'.format(r))
|
2021-03-06 18:55:51 +01:00
|
|
|
assert t.successful()
|
2021-02-01 18:12:51 +01:00
|
|
|
|
|
|
|
session = SessionBase.create_session()
|
|
|
|
o = session.query(Otx).first()
|
|
|
|
tx_signed_hex = o.signed_tx
|
|
|
|
session.close()
|
2021-03-29 15:27:53 +02:00
|
|
|
|
2021-02-01 18:12:51 +01:00
|
|
|
s_send = celery.signature(
|
|
|
|
'cic_eth.eth.tx.send',
|
|
|
|
[
|
|
|
|
[tx_signed_hex],
|
2021-03-29 15:27:53 +02:00
|
|
|
default_chain_spec.asdict(),
|
2021-02-01 18:12:51 +01:00
|
|
|
],
|
2021-03-29 15:27:53 +02:00
|
|
|
queue=None,
|
2021-02-01 18:12:51 +01:00
|
|
|
)
|
|
|
|
t = s_send.apply_async()
|
|
|
|
address = t.get()
|
|
|
|
r = t.collect()
|
|
|
|
t.successful()
|
|
|
|
|
|
|
|
init_eth_tester.mine_block()
|
|
|
|
|
2021-04-06 17:14:04 +02:00
|
|
|
c = AccountRegistry(default_chain_spec)
|
2021-03-29 15:27:53 +02:00
|
|
|
o = c.have(account_registry, eth_empty_accounts[0], sender_address=call_sender)
|
|
|
|
r = eth_rpc.do(o)
|
|
|
|
assert int(strip_0x(r), 16) == 1
|
2021-02-01 18:12:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_role_task(
|
2021-03-29 15:27:53 +02:00
|
|
|
default_chain_spec,
|
2021-02-01 18:12:51 +01:00
|
|
|
init_database,
|
|
|
|
celery_session_worker,
|
|
|
|
):
|
|
|
|
|
|
|
|
address = '0x' + os.urandom(20).hex()
|
|
|
|
role = AccountRole.set('foo', address)
|
|
|
|
init_database.add(role)
|
|
|
|
init_database.commit()
|
|
|
|
s = celery.signature(
|
|
|
|
'cic_eth.eth.account.role',
|
|
|
|
[
|
|
|
|
address,
|
2021-03-29 15:27:53 +02:00
|
|
|
default_chain_spec.asdict(),
|
2021-02-01 18:12:51 +01:00
|
|
|
],
|
|
|
|
)
|
|
|
|
t = s.apply_async()
|
|
|
|
r = t.get()
|
2021-12-22 19:24:05 +01:00
|
|
|
assert r[0][0] == address
|
|
|
|
assert r[0][1] == 'foo'
|
2021-03-29 15:27:53 +02:00
|
|
|
|
|
|
|
|
2021-12-22 19:24:05 +01:00
|
|
|
def test_get_role_task(
|
|
|
|
init_database,
|
|
|
|
celery_session_worker,
|
|
|
|
default_chain_spec,
|
|
|
|
):
|
|
|
|
address_foo = '0x' + os.urandom(20).hex()
|
|
|
|
role_foo = AccountRole.set('foo', address_foo)
|
|
|
|
init_database.add(role_foo)
|
|
|
|
|
|
|
|
address_bar = '0x' + os.urandom(20).hex()
|
|
|
|
role_bar = AccountRole.set('bar', address_bar)
|
|
|
|
init_database.add(role_bar)
|
|
|
|
|
|
|
|
init_database.commit()
|
|
|
|
|
|
|
|
s = celery.signature(
|
|
|
|
'cic_eth.eth.account.role_account',
|
|
|
|
[
|
|
|
|
'bar',
|
|
|
|
default_chain_spec.asdict(),
|
|
|
|
],
|
|
|
|
queue=None,
|
|
|
|
)
|
|
|
|
t = s.apply_async()
|
|
|
|
r = t.get()
|
|
|
|
assert r[0][0] == address_bar
|
|
|
|
assert r[0][1] == 'bar'
|
|
|
|
|
|
|
|
s = celery.signature(
|
|
|
|
'cic_eth.eth.account.role_account',
|
|
|
|
[
|
|
|
|
None,
|
|
|
|
default_chain_spec.asdict(),
|
|
|
|
],
|
|
|
|
queue=None,
|
|
|
|
)
|
|
|
|
t = s.apply_async()
|
|
|
|
r = t.get()
|
|
|
|
x_tags = ['foo', 'bar']
|
|
|
|
x_addrs = [address_foo, address_bar]
|
|
|
|
|
|
|
|
for v in r:
|
|
|
|
x_addrs.remove(v[0])
|
|
|
|
x_tags.remove(v[1])
|
|
|
|
|
|
|
|
assert len(x_tags) == 0
|
|
|
|
assert len(x_addrs) == 0
|
2021-03-29 15:27:53 +02:00
|
|
|
|
|
|
|
def test_gift(
|
|
|
|
init_database,
|
|
|
|
default_chain_spec,
|
|
|
|
contract_roles,
|
|
|
|
agent_roles,
|
|
|
|
account_registry,
|
|
|
|
faucet,
|
|
|
|
eth_rpc,
|
|
|
|
eth_signer,
|
|
|
|
init_celery_tasks,
|
|
|
|
cic_registry,
|
2021-05-31 17:34:16 +02:00
|
|
|
register_lookups,
|
2021-03-29 15:27:53 +02:00
|
|
|
celery_session_worker,
|
|
|
|
):
|
|
|
|
|
|
|
|
nonce_oracle = RPCNonceOracle(contract_roles['ACCOUNT_REGISTRY_WRITER'], eth_rpc)
|
2021-04-04 14:40:59 +02:00
|
|
|
c = AccountRegistry(default_chain_spec, signer=eth_signer, nonce_oracle=nonce_oracle)
|
2021-03-29 15:27:53 +02:00
|
|
|
(tx_hash_hex, o) = c.add(account_registry, contract_roles['ACCOUNT_REGISTRY_WRITER'], agent_roles['ALICE'])
|
|
|
|
eth_rpc.do(o)
|
|
|
|
o = receipt(tx_hash_hex)
|
|
|
|
r = eth_rpc.do(o)
|
|
|
|
assert r['status'] == 1
|
|
|
|
|
|
|
|
s_nonce = celery.signature(
|
2021-04-04 14:40:59 +02:00
|
|
|
'cic_eth.eth.nonce.reserve_nonce',
|
2021-03-29 15:27:53 +02:00
|
|
|
[
|
|
|
|
agent_roles['ALICE'],
|
2021-04-04 14:40:59 +02:00
|
|
|
default_chain_spec.asdict(),
|
2021-03-29 15:27:53 +02:00
|
|
|
],
|
|
|
|
queue=None,
|
|
|
|
)
|
|
|
|
|
|
|
|
s_gift = celery.signature(
|
|
|
|
'cic_eth.eth.account.gift',
|
|
|
|
[
|
|
|
|
default_chain_spec.asdict(),
|
|
|
|
],
|
|
|
|
queue=None,
|
|
|
|
)
|
|
|
|
s_nonce.link(s_gift)
|
|
|
|
t = s_nonce.apply_async()
|
|
|
|
r = t.get_leaf()
|
|
|
|
assert t.successful()
|