2021-03-15 19:46:37 +01:00
|
|
|
# standard imports
|
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
import time
|
|
|
|
|
|
|
|
# third-party imports
|
|
|
|
import pytest
|
|
|
|
import celery
|
2021-03-16 17:16:06 +01:00
|
|
|
from chainlib.connection import RPCConnection
|
2021-03-17 11:24:55 +01:00
|
|
|
from eth_accounts_index import AccountRegistry
|
|
|
|
from hexathon import strip_0x
|
2021-03-15 19:46:37 +01:00
|
|
|
|
|
|
|
# local imports
|
|
|
|
from cic_eth.error import OutOfGasError
|
|
|
|
from cic_eth.db.models.otx import Otx
|
|
|
|
from cic_eth.db.models.base import SessionBase
|
|
|
|
from cic_eth.db.enum import StatusEnum
|
|
|
|
from cic_eth.db.enum import StatusEnum
|
|
|
|
from cic_eth.db.models.nonce import Nonce
|
|
|
|
from cic_eth.db.models.role import AccountRole
|
|
|
|
|
|
|
|
logg = logging.getLogger()
|
|
|
|
|
|
|
|
|
2021-03-17 11:24:55 +01:00
|
|
|
@pytest.mark.skip()
|
2021-03-15 19:46:37 +01:00
|
|
|
def test_create_account(
|
|
|
|
default_chain_spec,
|
2021-03-16 17:16:06 +01:00
|
|
|
eth_rpc,
|
2021-03-15 19:46:37 +01:00
|
|
|
init_database,
|
|
|
|
celery_session_worker,
|
|
|
|
caplog,
|
|
|
|
):
|
|
|
|
s = celery.signature(
|
|
|
|
'cic_eth.eth.account.create',
|
|
|
|
[
|
|
|
|
'foo',
|
2021-03-18 19:36:52 +01:00
|
|
|
default_chain_spec.asdict(),
|
2021-03-15 19:46:37 +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-18 19:36:52 +01:00
|
|
|
default_chain_spec.asdict(),
|
2021-03-15 19:46:37 +01:00
|
|
|
],
|
|
|
|
)
|
|
|
|
t = s.apply_async()
|
|
|
|
assert r == t.get()
|
|
|
|
|
|
|
|
|
|
|
|
def test_register_account(
|
|
|
|
default_chain_spec,
|
2021-03-16 17:16:06 +01:00
|
|
|
account_registry,
|
2021-03-15 19:46:37 +01:00
|
|
|
init_database,
|
2021-03-17 11:24:55 +01:00
|
|
|
init_eth_tester,
|
2021-03-16 17:16:06 +01:00
|
|
|
eth_accounts,
|
|
|
|
eth_rpc,
|
2021-03-17 11:24:55 +01:00
|
|
|
cic_registry,
|
2021-03-16 17:16:06 +01:00
|
|
|
eth_empty_accounts,
|
2021-03-17 11:24:55 +01:00
|
|
|
custodial_roles,
|
|
|
|
call_sender,
|
2021-03-18 11:57:26 +01:00
|
|
|
celery_worker,
|
2021-03-15 19:46:37 +01:00
|
|
|
):
|
|
|
|
|
|
|
|
s_nonce = celery.signature(
|
|
|
|
'cic_eth.eth.tx.reserve_nonce',
|
|
|
|
[
|
2021-03-16 17:16:06 +01:00
|
|
|
eth_empty_accounts[0],
|
2021-03-18 11:57:26 +01:00
|
|
|
custodial_roles['ACCOUNT_REGISTRY_WRITER'],
|
2021-03-15 19:46:37 +01:00
|
|
|
],
|
|
|
|
queue=None,
|
|
|
|
)
|
|
|
|
s_register = celery.signature(
|
|
|
|
'cic_eth.eth.account.register',
|
|
|
|
[
|
2021-03-18 11:57:26 +01:00
|
|
|
default_chain_spec.asdict(),
|
|
|
|
custodial_roles['ACCOUNT_REGISTRY_WRITER'],
|
2021-03-15 19:46:37 +01:00
|
|
|
],
|
2021-03-18 11:57:26 +01:00
|
|
|
queue=None,
|
2021-03-15 19:46:37 +01:00
|
|
|
)
|
|
|
|
s_nonce.link(s_register)
|
|
|
|
t = s_nonce.apply_async()
|
|
|
|
address = t.get()
|
|
|
|
for r in t.collect():
|
2021-03-18 19:36:52 +01:00
|
|
|
logg.debug('r {}'.format(r))
|
2021-03-15 19:46:37 +01:00
|
|
|
assert t.successful()
|
|
|
|
|
|
|
|
session = SessionBase.create_session()
|
|
|
|
o = session.query(Otx).first()
|
|
|
|
tx_signed_hex = o.signed_tx
|
|
|
|
session.close()
|
2021-03-18 11:57:26 +01:00
|
|
|
|
2021-03-15 19:46:37 +01:00
|
|
|
s_send = celery.signature(
|
|
|
|
'cic_eth.eth.tx.send',
|
|
|
|
[
|
|
|
|
[tx_signed_hex],
|
2021-03-18 11:57:26 +01:00
|
|
|
default_chain_spec.asdict(),
|
2021-03-15 19:46:37 +01:00
|
|
|
],
|
2021-03-18 11:57:26 +01:00
|
|
|
queue=None,
|
2021-03-15 19:46:37 +01:00
|
|
|
)
|
|
|
|
t = s_send.apply_async()
|
|
|
|
address = t.get()
|
|
|
|
r = t.collect()
|
|
|
|
t.successful()
|
|
|
|
|
|
|
|
init_eth_tester.mine_block()
|
|
|
|
|
2021-03-17 11:24:55 +01:00
|
|
|
c = AccountRegistry()
|
|
|
|
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-03-15 19:46:37 +01:00
|
|
|
|
|
|
|
|
2021-03-17 11:24:55 +01:00
|
|
|
@pytest.mark.skip()
|
2021-03-15 19:46:37 +01:00
|
|
|
def test_role_task(
|
|
|
|
init_database,
|
|
|
|
celery_session_worker,
|
|
|
|
default_chain_spec,
|
|
|
|
):
|
|
|
|
|
|
|
|
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,
|
|
|
|
str(default_chain_spec),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
t = s.apply_async()
|
|
|
|
r = t.get()
|
|
|
|
assert r == 'foo'
|