WIP Add role fixture, rehabilitate account register task, test

This commit is contained in:
nolash 2021-03-17 11:24:55 +01:00
parent 318615751c
commit 41731b5e96
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
10 changed files with 128 additions and 31 deletions

View File

@ -10,16 +10,22 @@ from cic_eth.db.models.base import SessionBase
from cic_eth.db.models.otx import Otx
from cic_eth.db.models.tx import TxCache
from cic_eth.db.models.nonce import Nonce
from cic_eth.admin.ctrl import lock_send
from cic_eth.admin.ctrl import unlock_send
from cic_eth.admin.ctrl import lock_queue
from cic_eth.admin.ctrl import unlock_queue
from cic_eth.queue.tx import get_tx
from cic_eth.queue.tx import set_cancel
from cic_eth.admin.ctrl import (
lock_send,
unlock_send,
lock_queue,
unlock_queue,
)
from cic_eth.queue.tx import (
get_tx,
set_cancel,
)
from cic_eth.queue.tx import create as queue_create
from cic_eth.eth.util import unpack_signed_raw_tx
from cic_eth.eth.task import sign_tx
from cic_eth.eth.task import create_check_gas_and_send_task
from cic_eth.eth.task import (
sign_tx,
create_check_gas_task,
)
celery_app = celery.current_app
logg = logging.getLogger()

View File

@ -10,6 +10,7 @@ from cic_registry import zero_content
from cic_registry import CICRegistry
from crypto_dev_signer.eth.web3ext import Web3 as Web3Ext
from cic_registry.error import UnknownContractError
from chainlib.eth.address import to_checksum_address
# local imports
from cic_eth.db.models.base import SessionBase
@ -93,7 +94,8 @@ class AdminApi:
:type address_hex: str, 0x-hex
:raises ValueError: Invalid checksum address
"""
if not web3.Web3.isChecksumAddress(address_hex):
#if not web3.Web3.isChecksumAddress(address_hex):
if not to_checksum_address(address_hex):
raise ValueError('invalid address')
session = SessionBase.create_session()
role = AccountRole.set(tag, address_hex)

View File

@ -13,13 +13,18 @@ from chainlib.eth.sign import (
sign_message,
)
from chainlib.eth.address import to_checksum_address
from chainlib.eth.tx import TxFormat
from eth_accounts_index import AccountRegistry
# local import
from cic_eth.registry import safe_registry
from cic_eth.eth import RpcClient
#from cic_eth.registry import safe_registry
#from cic_eth.eth import RpcClient
from cic_eth_registry import CICRegistry
from cic_eth.eth import registry_extra_identifiers
from cic_eth.eth.task import sign_and_register_tx
from cic_eth.eth.task import create_check_gas_and_send_task
from cic_eth.eth.task import (
register_tx,
create_check_gas_task,
)
from cic_eth.eth.factory import TxFactory
from cic_eth.db.models.nonce import Nonce
from cic_eth.db.models.base import SessionBase
@ -39,6 +44,8 @@ from cic_eth.task import (
#logg = logging.getLogger(__name__)
logg = logging.getLogger()
celery_app = celery.current_app
#celery_app.log.setup_task_loggers(loglevel=logging.DEBUG)
#celery_app.log.redirect_stdouts_to_logger(logg, loglevel=logging.DEBUG)
class AccountTxFactory(TxFactory):
@ -214,7 +221,7 @@ def register(self, account_address, chain_str, writer_address=None):
raise RoleMissingError(account_address)
logg.debug('adding account address {} to index; writer {}'.format(account_address, writer_address))
queue = self.request.delivery_info['routing_key']
queue = self.request.delivery_info.get('routing_key')
# c = RpcClient(chain_spec, holder_address=writer_address)
# registry = safe_registry(c.w3)
@ -222,11 +229,29 @@ def register(self, account_address, chain_str, writer_address=None):
# tx_add = txf.add(account_address, chain_spec, self.request.root_id, session=session)
# (tx_hash_hex, tx_signed_raw_hex) = sign_and_register_tx(tx_add, chain_str, queue, 'cic_eth.eth.account.cache_account_data', session=session)
# Retrieve account index address
rpc = RPCConnection.connect(chain_spec, 'default')
reg = CICRegistry(chain_spec, rpc)
call_address = AccountRole.get_address('DEFAULT', session=session)
account_registry_address = reg.by_name('AccountRegistry', sender_address=call_address)
# Generate and sign transaction
rpc_signer = RPCConnection.connect(chain_spec, 'signer')
nonce_oracle = self.create_nonce_oracle(writer_address, rpc)
gas_oracle = self.create_gas_oracle(rpc)
account_registry = AccountRegistry(signer=rpc_signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle)
(tx_hash_hex, tx_signed_raw_hex) = account_registry.add(account_registry_address, writer_address, account_address, tx_format=TxFormat.RLP_SIGNED)
#cache_task = 'cic_eth.eth.account.cache_account_data'
cache_task = None
# add transaction to queue
register_tx(tx_hash_hex, tx_signed_raw_hex, chain_str, queue, cache_task=cache_task, session=session)
session.commit()
session.close()
gas_budget = tx_add['gas'] * tx_add['gasPrice']
return tx_hash_hex
#gas_budget = tx_add['gas'] * tx_add['gasPrice']
logg.debug('register user tx {}'.format(tx_hash_hex))
s = create_check_gas_and_send_task(

View File

@ -7,6 +7,7 @@ import celery
from chainlib.chain import ChainSpec
from chainlib.eth.sign import sign_transaction
from chainlib.connection import RPCConnection
from chainlib.eth.tx import unpack
from hexathon import (
strip_0x,
add_0x,
@ -52,7 +53,8 @@ def sign_tx(tx, chain_str):
return (tx_hash_hex, tx_transfer_signed['raw'],)
def sign_and_register_tx(tx, chain_str, queue, cache_task=None, session=None):
#def sign_and_register_tx(tx, chain_str, queue, cache_task=None, session=None):
def register_tx(tx_hash_hex, tx_signed_raw_hex, chain_str, queue, cache_task=None, session=None):
"""Signs the provided transaction, and adds it to the transaction queue cache (with status PENDING).
:param tx: Standard ethereum transaction data
@ -67,9 +69,11 @@ def sign_and_register_tx(tx, chain_str, queue, cache_task=None, session=None):
:returns: Tuple; Transaction hash, signed raw transaction data
:rtype: tuple
"""
(tx_hash_hex, tx_signed_raw_hex) = sign_tx(tx, chain_str)
#(tx_hash_hex, tx_signed_raw_hex) = sign_tx(tx, chain_str)
logg.debug('adding queue tx {}'.format(tx_hash_hex))
tx_signed_raw = bytes.fromhex(strip_0x(tx_signed_raw_hex))
tx = unpack(tx_signed_raw)
queue_create(
tx['nonce'],
@ -97,7 +101,7 @@ def sign_and_register_tx(tx, chain_str, queue, cache_task=None, session=None):
# TODO: rename as we will not be sending task in the chain, this is the responsibility of the dispatcher
def create_check_gas_and_send_task(tx_signed_raws_hex, chain_str, holder_address, gas, tx_hashes_hex=None, queue=None):
def create_check_gas_task(tx_signed_raws_hex, chain_str, holder_address, gas, tx_hashes_hex=None, queue=None):
"""Creates a celery task signature for a check_gas task that adds the task to the outgoing queue to be processed by the dispatcher.
If tx_hashes_hex is not spefified, a preceding task chained to check_gas must supply the transaction hashes as its return value.

View File

@ -16,8 +16,10 @@ from cic_eth.db.models.tx import TxCache
from cic_eth.db.models.base import SessionBase
from cic_eth.eth import RpcClient
from cic_eth.error import TokenCountError, PermanentTxError, OutOfGasError, NotLocalTxError
from cic_eth.eth.task import sign_and_register_tx
from cic_eth.eth.task import create_check_gas_and_send_task
from cic_eth.eth.task import (
register_tx,
create_check_gas_task,
)
from cic_eth.eth.factory import TxFactory
from cic_eth.eth.util import unpack_signed_raw_tx
from cic_eth.ext.address import translate_address

View File

@ -17,6 +17,7 @@ from chainlib.eth.tx import (
receipt,
)
from chainlib.hash import keccak256_hex_to_hex
from hexathon import add_0x
# local imports
@ -39,8 +40,11 @@ from cic_eth.queue.tx import get_nonce_tx
from cic_eth.error import OutOfGasError
from cic_eth.error import LockedError
from cic_eth.eth.util import unpack_signed_raw_tx
from cic_eth.eth.task import sign_and_register_tx, create_check_gas_and_send_task
from cic_eth.eth.task import sign_tx
from cic_eth.eth.task import (
register_tx,
create_check_gas_task,
sign_tx,
)
from cic_eth.eth.nonce import NonceOracle
from cic_eth.error import (
AlreadyFillingGasError,
@ -409,13 +413,11 @@ def send(self, txs, chain_str):
tx_hex = txs[0]
logg.debug('send transaction {}'.format(tx_hex))
tx_hash = add_0x(keccak256_hex_to_hex())
#tx_hash = web3.Web3.keccak(hexstr=tx_hex)
#tx_hash_hex = tx_hash.hex()
tx_hash_hex = add_0x(keccak256_hex_to_hex(tx_hex))
queue = self.request.delivery_info.get('routing_key', None)
queue = self.request.delivery_info.get('routing_key')
c = RpcClient(chain_spec)
#c = RpcClient(chain_spec)
r = None
s_set_sent = celery.signature(
'cic_eth.queue.tx.set_sent_status',
@ -425,6 +427,9 @@ def send(self, txs, chain_str):
],
queue=queue,
)
return txs[1:]
try:
#r = c.w3.eth.send_raw_transaction(tx_hex)
r = c.w3.eth.sendRawTransaction(tx_hex)

View File

@ -7,6 +7,9 @@ import uuid
# external imports
import celery
import sqlalchemy
from chainlib.eth.constant import ZERO_ADDRESS
from chainlib.eth.nonce import RPCNonceOracle
from chainlib.eth.gas import RPCGasOracle
# local imports
from cic_eth.error import (
@ -23,9 +26,11 @@ celery_app = celery.current_app
class BaseTask(celery.Task):
session_func = SessionBase.create_session
call_address = ZERO_ADDRESS
create_nonce_oracle = RPCNonceOracle
create_gas_oracle = RPCGasOracle
def create_session(self):
logg.warning('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> session from base {}'.format(id(self.session_func)))
return BaseTask.session_func()

View File

@ -11,7 +11,7 @@ logg = logging.getLogger(__name__)
@pytest.fixture(scope='session')
def celery_includes():
return [
'cic_eth.eth.bancor',
# 'cic_eth.eth.bancor',
'cic_eth.eth.token',
'cic_eth.eth.tx',
'cic_eth.ext.tx',

View File

@ -0,0 +1,38 @@
# standard imports
import logging
# external imports
import pytest
from hexathon import add_0x
from chainlib.eth.address import to_checksum_address
# local imports
from cic_eth.db.models.role import AccountRole
logg = logging.getLogger(__name__)
@pytest.fixture(scope='function')
def custodial_roles(
contract_roles,
eth_accounts,
init_database,
):
r = {}
r.update(contract_roles)
r.update({
'DEFAULT': eth_accounts[0],
})
for k in r.keys():
role = AccountRole.set(k, r[k])
init_database.add(role)
logg.info('adding role {} -> {}'.format(k, r[k]))
init_database.commit()
return r
@pytest.fixture(scope='function')
def whoever(
init_eth_tester,
):
return init_eth_tester.new_account()

View File

@ -7,6 +7,8 @@ import time
import pytest
import celery
from chainlib.connection import RPCConnection
from eth_accounts_index import AccountRegistry
from hexathon import strip_0x
# local imports
from cic_eth.error import OutOfGasError
@ -21,6 +23,7 @@ from cic_eth.eth.account import AccountTxFactory
logg = logging.getLogger()
@pytest.mark.skip()
def test_create_account(
default_chain_spec,
eth_rpc,
@ -60,11 +63,14 @@ def test_register_account(
default_chain_spec,
account_registry,
init_database,
init_eth_tester,
eth_accounts,
eth_rpc,
registry,
cic_registry,
celery_session_worker,
eth_empty_accounts,
custodial_roles,
call_sender,
):
logg.debug('chainspec {}'.format(str(default_chain_spec)))
@ -110,9 +116,13 @@ def test_register_account(
init_eth_tester.mine_block()
assert account_registry.have(eth_empty_accounts[0])
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
@pytest.mark.skip()
def test_role_task(
init_database,
celery_session_worker,