2021-02-01 18:12:51 +01:00
|
|
|
# standard imports
|
|
|
|
import logging
|
|
|
|
|
2021-03-29 15:27:53 +02:00
|
|
|
# external imports
|
2021-02-01 18:12:51 +01:00
|
|
|
import celery
|
2021-05-02 19:02:45 +02:00
|
|
|
from erc20_faucet import Faucet
|
2021-03-29 15:27:53 +02:00
|
|
|
from hexathon import (
|
|
|
|
strip_0x,
|
|
|
|
)
|
2021-04-24 19:53:45 +02:00
|
|
|
from chainlib.eth.constant import ZERO_ADDRESS
|
2021-03-29 15:27:53 +02:00
|
|
|
from chainlib.connection import RPCConnection
|
|
|
|
from chainlib.eth.sign import (
|
|
|
|
new_account,
|
|
|
|
sign_message,
|
|
|
|
)
|
2021-10-07 17:12:35 +02:00
|
|
|
from chainlib.eth.address import to_checksum_address, is_address
|
2021-08-28 13:10:18 +02:00
|
|
|
from chainlib.eth.tx import TxFormat
|
2021-03-29 15:27:53 +02:00
|
|
|
from chainlib.chain import ChainSpec
|
2021-04-24 19:53:45 +02:00
|
|
|
from chainlib.error import JSONRPCException
|
2021-05-05 18:25:21 +02:00
|
|
|
from eth_accounts_index.registry import AccountRegistry
|
|
|
|
from eth_accounts_index import AccountsIndex
|
2021-05-02 19:02:45 +02:00
|
|
|
from sarafu_faucet import MinterFaucet
|
2021-07-21 19:34:51 +02:00
|
|
|
from chainqueue.sql.tx import cache_tx_dict
|
2021-02-01 18:12:51 +01:00
|
|
|
|
|
|
|
# local import
|
2021-03-29 15:27:53 +02:00
|
|
|
from cic_eth_registry import CICRegistry
|
|
|
|
from cic_eth.eth.gas import (
|
|
|
|
create_check_gas_task,
|
|
|
|
)
|
|
|
|
#from cic_eth.eth.factory import TxFactory
|
2021-02-01 18:12:51 +01:00
|
|
|
from cic_eth.db.models.nonce import Nonce
|
|
|
|
from cic_eth.db.models.base import SessionBase
|
|
|
|
from cic_eth.db.models.role import AccountRole
|
2021-10-07 17:12:35 +02:00
|
|
|
from cic_eth.encode import tx_normalize
|
2021-03-07 14:51:59 +01:00
|
|
|
from cic_eth.error import (
|
|
|
|
RoleMissingError,
|
|
|
|
SignerError,
|
|
|
|
)
|
|
|
|
from cic_eth.task import (
|
|
|
|
CriticalSQLAlchemyTask,
|
|
|
|
CriticalSQLAlchemyAndSignerTask,
|
2021-03-29 15:27:53 +02:00
|
|
|
BaseTask,
|
|
|
|
)
|
|
|
|
from cic_eth.eth.nonce import (
|
|
|
|
CustodialTaskNonceOracle,
|
|
|
|
)
|
|
|
|
from cic_eth.queue.tx import (
|
|
|
|
register_tx,
|
2021-03-07 14:51:59 +01:00
|
|
|
)
|
2021-08-28 13:10:18 +02:00
|
|
|
from cic_eth.encode import (
|
|
|
|
unpack_normal,
|
|
|
|
ZERO_ADDRESS_NORMAL,
|
2021-10-07 17:12:35 +02:00
|
|
|
tx_normalize,
|
2021-08-28 13:10:18 +02:00
|
|
|
)
|
2021-02-01 18:12:51 +01:00
|
|
|
|
|
|
|
logg = logging.getLogger()
|
|
|
|
celery_app = celery.current_app
|
|
|
|
|
|
|
|
|
2021-03-01 21:15:17 +01:00
|
|
|
# TODO: Separate out nonce initialization task
|
2021-03-29 15:27:53 +02:00
|
|
|
@celery_app.task(bind=True, base=CriticalSQLAlchemyAndSignerTask)
|
|
|
|
def create(self, password, chain_spec_dict):
|
2021-02-01 18:12:51 +01:00
|
|
|
"""Creates and stores a new ethereum account in the keystore.
|
|
|
|
|
|
|
|
The password is passed on to the wallet backend, no encryption is performed in the task worker.
|
|
|
|
|
|
|
|
:param password: Password to encrypt private key with
|
|
|
|
:type password: str
|
|
|
|
:param chain_str: Chain spec string representation
|
|
|
|
:type chain_str: str
|
|
|
|
:returns: Ethereum address of newly created account
|
|
|
|
:rtype: str, 0x-hex
|
|
|
|
"""
|
2021-03-29 15:27:53 +02:00
|
|
|
chain_spec = ChainSpec.from_dict(chain_spec_dict)
|
2021-03-07 14:51:59 +01:00
|
|
|
a = None
|
2021-03-29 15:27:53 +02:00
|
|
|
conn = RPCConnection.connect(chain_spec, 'signer')
|
|
|
|
o = new_account()
|
2021-04-24 19:53:45 +02:00
|
|
|
try:
|
|
|
|
a = conn.do(o)
|
|
|
|
except ConnectionError as e:
|
|
|
|
raise SignerError(e)
|
|
|
|
except FileNotFoundError as e:
|
|
|
|
raise SignerError(e)
|
2021-03-29 15:27:53 +02:00
|
|
|
conn.disconnect()
|
|
|
|
|
2021-04-24 19:53:45 +02:00
|
|
|
# TODO: It seems infeasible that a can be None in any case, verify
|
2021-03-07 14:51:59 +01:00
|
|
|
if a == None:
|
|
|
|
raise SignerError('create account')
|
2021-10-07 17:12:35 +02:00
|
|
|
a = tx_normalize.wallet_address(a)
|
2021-02-01 18:12:51 +01:00
|
|
|
logg.debug('created account {}'.format(a))
|
|
|
|
|
|
|
|
# Initialize nonce provider record for account
|
2021-03-29 15:27:53 +02:00
|
|
|
session = self.create_session()
|
2021-03-06 18:55:51 +01:00
|
|
|
Nonce.init(a, session=session)
|
|
|
|
session.commit()
|
2021-02-01 18:12:51 +01:00
|
|
|
session.close()
|
|
|
|
return a
|
|
|
|
|
|
|
|
|
2021-03-07 14:51:59 +01:00
|
|
|
@celery_app.task(bind=True, throws=(RoleMissingError,), base=CriticalSQLAlchemyAndSignerTask)
|
2021-03-29 15:27:53 +02:00
|
|
|
def register(self, account_address, chain_spec_dict, writer_address=None):
|
2021-02-01 18:12:51 +01:00
|
|
|
"""Creates a transaction to add the given address to the accounts index.
|
|
|
|
|
|
|
|
:param account_address: Ethereum address to add
|
|
|
|
:type account_address: str, 0x-hex
|
|
|
|
:param chain_str: Chain spec string representation
|
|
|
|
:type chain_str: str
|
|
|
|
:param writer_address: Specify address in keystore to sign transaction. Overrides local accounts role setting.
|
|
|
|
:type writer_address: str, 0x-hex
|
|
|
|
:raises RoleMissingError: Writer address not set and writer role not found.
|
|
|
|
:returns: The account_address input param
|
|
|
|
:rtype: str, 0x-hex
|
|
|
|
"""
|
2021-03-29 15:27:53 +02:00
|
|
|
chain_spec = ChainSpec.from_dict(chain_spec_dict)
|
2021-02-01 18:12:51 +01:00
|
|
|
|
2021-03-29 15:27:53 +02:00
|
|
|
session = self.create_session()
|
2021-02-01 18:12:51 +01:00
|
|
|
if writer_address == None:
|
2021-03-29 15:27:53 +02:00
|
|
|
writer_address = AccountRole.get_address('ACCOUNT_REGISTRY_WRITER', session=session)
|
2021-02-01 18:12:51 +01:00
|
|
|
|
2021-03-29 15:27:53 +02:00
|
|
|
if writer_address == ZERO_ADDRESS:
|
2021-03-01 21:15:17 +01:00
|
|
|
session.close()
|
2021-03-29 15:27:53 +02:00
|
|
|
raise RoleMissingError('writer address for regsistering {}'.format(account_address))
|
2021-02-01 18:12:51 +01:00
|
|
|
|
|
|
|
logg.debug('adding account address {} to index; writer {}'.format(account_address, writer_address))
|
2021-03-29 15:27:53 +02:00
|
|
|
queue = self.request.delivery_info.get('routing_key')
|
2021-02-01 18:12:51 +01:00
|
|
|
|
2021-03-29 15:27:53 +02:00
|
|
|
# Retrieve account index address
|
|
|
|
rpc = RPCConnection.connect(chain_spec, 'default')
|
|
|
|
registry = CICRegistry(chain_spec, rpc)
|
|
|
|
call_address = AccountRole.get_address('DEFAULT', session=session)
|
|
|
|
if writer_address == ZERO_ADDRESS:
|
|
|
|
session.close()
|
|
|
|
raise RoleMissingError('call address for resgistering {}'.format(account_address))
|
2021-05-05 18:25:21 +02:00
|
|
|
account_registry_address = registry.by_name('AccountRegistry', sender_address=call_address)
|
2021-03-29 15:27:53 +02:00
|
|
|
|
|
|
|
# Generate and sign transaction
|
|
|
|
rpc_signer = RPCConnection.connect(chain_spec, 'signer')
|
|
|
|
nonce_oracle = CustodialTaskNonceOracle(writer_address, self.request.root_id, session=session) #, default_nonce)
|
2021-10-29 08:45:42 +02:00
|
|
|
gas_oracle = self.create_gas_oracle(rpc, code_callback=AccountRegistry.gas)
|
2021-05-02 19:11:03 +02:00
|
|
|
account_registry = AccountsIndex(chain_spec, signer=rpc_signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle)
|
2021-03-29 15:27:53 +02:00
|
|
|
(tx_hash_hex, tx_signed_raw_hex) = account_registry.add(account_registry_address, writer_address, account_address, tx_format=TxFormat.RLP_SIGNED)
|
|
|
|
rpc_signer.disconnect()
|
|
|
|
|
|
|
|
# add transaction to queue
|
|
|
|
cache_task = 'cic_eth.eth.account.cache_account_data'
|
|
|
|
register_tx(tx_hash_hex, tx_signed_raw_hex, chain_spec, queue, cache_task=cache_task, session=session)
|
|
|
|
session.commit()
|
2021-03-01 21:15:17 +01:00
|
|
|
session.close()
|
2021-02-01 18:12:51 +01:00
|
|
|
|
2021-03-29 15:27:53 +02:00
|
|
|
gas_pair = gas_oracle.get_gas(tx_signed_raw_hex)
|
|
|
|
gas_budget = gas_pair[0] * gas_pair[1]
|
|
|
|
logg.debug('register user tx {} {} {}'.format(tx_hash_hex, queue, gas_budget))
|
|
|
|
rpc.disconnect()
|
2021-02-01 18:12:51 +01:00
|
|
|
|
2021-03-29 15:27:53 +02:00
|
|
|
s = create_check_gas_task(
|
2021-02-01 18:12:51 +01:00
|
|
|
[tx_signed_raw_hex],
|
2021-03-29 15:27:53 +02:00
|
|
|
chain_spec,
|
2021-02-01 18:12:51 +01:00
|
|
|
writer_address,
|
2021-03-29 15:27:53 +02:00
|
|
|
gas=gas_budget,
|
2021-02-01 18:12:51 +01:00
|
|
|
tx_hashes_hex=[tx_hash_hex],
|
|
|
|
queue=queue,
|
|
|
|
)
|
|
|
|
s.apply_async()
|
|
|
|
return account_address
|
|
|
|
|
|
|
|
|
2021-03-07 14:51:59 +01:00
|
|
|
@celery_app.task(bind=True, base=CriticalSQLAlchemyAndSignerTask)
|
2021-03-29 15:27:53 +02:00
|
|
|
def gift(self, account_address, chain_spec_dict):
|
2021-02-01 18:12:51 +01:00
|
|
|
"""Creates a transaction to invoke the faucet contract for the given address.
|
|
|
|
|
|
|
|
:param account_address: Ethereum address to give to
|
|
|
|
:type account_address: str, 0x-hex
|
|
|
|
:param chain_str: Chain spec string representation
|
|
|
|
:type chain_str: str
|
|
|
|
:returns: Raw signed transaction
|
|
|
|
:rtype: list with transaction as only element
|
|
|
|
"""
|
2021-03-29 15:27:53 +02:00
|
|
|
chain_spec = ChainSpec.from_dict(chain_spec_dict)
|
2021-02-01 18:12:51 +01:00
|
|
|
|
2021-10-07 17:12:35 +02:00
|
|
|
if is_address(account_address):
|
|
|
|
account_address = tx_normalize.wallet_address(account_address)
|
|
|
|
|
2021-02-01 18:12:51 +01:00
|
|
|
logg.debug('gift account address {} to index'.format(account_address))
|
2021-03-29 15:27:53 +02:00
|
|
|
queue = self.request.delivery_info.get('routing_key')
|
|
|
|
|
|
|
|
# Retrieve account index address
|
|
|
|
session = self.create_session()
|
|
|
|
rpc = RPCConnection.connect(chain_spec, 'default')
|
|
|
|
registry = CICRegistry(chain_spec, rpc)
|
|
|
|
faucet_address = registry.by_name('Faucet', sender_address=self.call_address)
|
|
|
|
|
|
|
|
# Generate and sign transaction
|
|
|
|
rpc_signer = RPCConnection.connect(chain_spec, 'signer')
|
|
|
|
nonce_oracle = CustodialTaskNonceOracle(account_address, self.request.root_id, session=session) #, default_nonce)
|
2021-10-29 08:45:42 +02:00
|
|
|
gas_oracle = self.create_gas_oracle(rpc, code_callback=MinterFaucet.gas)
|
2021-04-04 14:40:59 +02:00
|
|
|
faucet = Faucet(chain_spec, signer=rpc_signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle)
|
2021-03-29 15:27:53 +02:00
|
|
|
(tx_hash_hex, tx_signed_raw_hex) = faucet.give_to(faucet_address, account_address, account_address, tx_format=TxFormat.RLP_SIGNED)
|
|
|
|
rpc_signer.disconnect()
|
|
|
|
|
|
|
|
# add transaction to queue
|
|
|
|
cache_task = 'cic_eth.eth.account.cache_gift_data'
|
|
|
|
register_tx(tx_hash_hex, tx_signed_raw_hex, chain_spec, queue, cache_task, session=session)
|
|
|
|
session.commit()
|
2021-03-03 08:37:26 +01:00
|
|
|
session.close()
|
2021-02-01 18:12:51 +01:00
|
|
|
|
2021-03-29 15:27:53 +02:00
|
|
|
gas_pair = gas_oracle.get_gas(tx_signed_raw_hex)
|
|
|
|
gas_budget = gas_pair[0] * gas_pair[1]
|
|
|
|
logg.debug('register user tx {} {} {}'.format(tx_hash_hex, queue, gas_budget))
|
|
|
|
rpc.disconnect()
|
2021-02-01 18:12:51 +01:00
|
|
|
|
2021-03-29 15:27:53 +02:00
|
|
|
s = create_check_gas_task(
|
2021-02-01 18:12:51 +01:00
|
|
|
[tx_signed_raw_hex],
|
2021-03-29 15:27:53 +02:00
|
|
|
chain_spec,
|
2021-02-01 18:12:51 +01:00
|
|
|
account_address,
|
|
|
|
gas_budget,
|
|
|
|
[tx_hash_hex],
|
|
|
|
queue=queue,
|
|
|
|
)
|
|
|
|
s.apply_async()
|
2021-03-29 15:27:53 +02:00
|
|
|
|
2021-02-01 18:12:51 +01:00
|
|
|
return [tx_signed_raw_hex]
|
|
|
|
|
|
|
|
|
|
|
|
@celery_app.task(bind=True)
|
2021-03-29 15:27:53 +02:00
|
|
|
def have(self, account, chain_spec_dict):
|
2021-02-01 18:12:51 +01:00
|
|
|
"""Check whether the given account exists in keystore
|
|
|
|
|
|
|
|
:param account: Account to check
|
|
|
|
:type account: str, 0x-hex
|
|
|
|
:param chain_str: Chain spec string representation
|
|
|
|
:type chain_str: str
|
|
|
|
:returns: Account, or None if not exists
|
|
|
|
:rtype: Varies
|
|
|
|
"""
|
2021-03-29 15:27:53 +02:00
|
|
|
chain_spec = ChainSpec.from_dict(chain_spec_dict)
|
|
|
|
o = sign_message(account, '0x2a')
|
2021-04-24 19:53:45 +02:00
|
|
|
conn = RPCConnection.connect(chain_spec, 'signer')
|
2021-03-29 15:27:53 +02:00
|
|
|
|
2021-02-01 18:12:51 +01:00
|
|
|
try:
|
2021-03-29 15:27:53 +02:00
|
|
|
conn.do(o)
|
2021-04-24 19:53:45 +02:00
|
|
|
except ConnectionError as e:
|
|
|
|
raise SignerError(e)
|
|
|
|
except FileNotFoundError as e:
|
|
|
|
raise SignerError(e)
|
|
|
|
except JSONRPCException as e:
|
2021-02-01 18:12:51 +01:00
|
|
|
logg.debug('cannot sign with {}: {}'.format(account, e))
|
2021-03-29 15:27:53 +02:00
|
|
|
conn.disconnect()
|
2021-02-01 18:12:51 +01:00
|
|
|
return None
|
2021-03-29 15:27:53 +02:00
|
|
|
|
2021-04-24 19:53:45 +02:00
|
|
|
conn.disconnect()
|
|
|
|
return account
|
|
|
|
|
2021-02-01 18:12:51 +01:00
|
|
|
|
2021-03-29 15:27:53 +02:00
|
|
|
@celery_app.task(bind=True, base=CriticalSQLAlchemyTask)
|
|
|
|
def set_role(self, tag, address, chain_spec_dict):
|
2021-10-07 17:12:35 +02:00
|
|
|
if not is_address(address):
|
|
|
|
raise ValueError('invalid address {}'.format(address))
|
|
|
|
address = tx_normalize.wallet_address(address)
|
2021-03-29 15:27:53 +02:00
|
|
|
session = SessionBase.create_session()
|
|
|
|
role = AccountRole.set(tag, address, session=session)
|
|
|
|
session.add(role)
|
|
|
|
session.commit()
|
|
|
|
session.close()
|
|
|
|
return tag
|
2021-02-01 18:12:51 +01:00
|
|
|
|
2021-03-29 15:27:53 +02:00
|
|
|
|
|
|
|
@celery_app.task(bind=True, base=BaseTask)
|
|
|
|
def role(self, address, chain_spec_dict):
|
2021-10-30 13:19:31 +02:00
|
|
|
"""Return account role for address and/or role
|
2021-02-01 18:12:51 +01:00
|
|
|
|
|
|
|
:param account: Account to check
|
|
|
|
:type account: str, 0x-hex
|
2021-10-30 13:19:31 +02:00
|
|
|
:param chain_spec_dict: Chain spec dict representation
|
|
|
|
:type chain_spec_dict: dict
|
2021-02-01 18:12:51 +01:00
|
|
|
:returns: Account, or None if not exists
|
|
|
|
:rtype: Varies
|
|
|
|
"""
|
2021-03-29 15:27:53 +02:00
|
|
|
session = self.create_session()
|
|
|
|
role_tag = AccountRole.role_for(address, session=session)
|
|
|
|
session.close()
|
2021-10-30 13:19:31 +02:00
|
|
|
return [(address, role_tag,)]
|
|
|
|
|
|
|
|
|
|
|
|
@celery_app.task(bind=True, base=BaseTask)
|
|
|
|
def role_account(self, role_tag, chain_spec_dict):
|
|
|
|
"""Return address for role.
|
|
|
|
|
|
|
|
If the role parameter is None, will return addresses for all roles.
|
|
|
|
|
|
|
|
:param role_tag: Role to match
|
|
|
|
:type role_tag: str
|
|
|
|
:param chain_spec_dict: Chain spec dict representation
|
|
|
|
:type chain_spec_dict: dict
|
|
|
|
:returns: List with a single account/tag pair for a single tag, or a list of account and tag pairs for all tags
|
|
|
|
:rtype: list
|
|
|
|
"""
|
|
|
|
session = self.create_session()
|
|
|
|
|
|
|
|
pairs = None
|
|
|
|
if role_tag != None:
|
|
|
|
addr = AccountRole.get_address(role_tag, session=session)
|
|
|
|
pairs = [(addr, role_tag,)]
|
|
|
|
else:
|
|
|
|
pairs = AccountRole.all(session=session)
|
|
|
|
|
|
|
|
session.close()
|
|
|
|
|
|
|
|
return pairs
|
2021-02-01 18:12:51 +01:00
|
|
|
|
|
|
|
|
2021-03-29 15:27:53 +02:00
|
|
|
@celery_app.task(bind=True, base=CriticalSQLAlchemyTask)
|
2021-02-01 18:12:51 +01:00
|
|
|
def cache_gift_data(
|
2021-03-29 15:27:53 +02:00
|
|
|
self,
|
2021-02-01 18:12:51 +01:00
|
|
|
tx_hash_hex,
|
|
|
|
tx_signed_raw_hex,
|
2021-03-29 15:27:53 +02:00
|
|
|
chain_spec_dict,
|
2021-02-01 18:12:51 +01:00
|
|
|
):
|
|
|
|
"""Generates and commits transaction cache metadata for a Faucet.giveTo transaction
|
|
|
|
|
|
|
|
:param tx_hash_hex: Transaction hash
|
|
|
|
:type tx_hash_hex: str, 0x-hex
|
|
|
|
:param tx_signed_raw_hex: Raw signed transaction
|
|
|
|
:type tx_signed_raw_hex: str, 0x-hex
|
|
|
|
:param chain_str: Chain spec string representation
|
|
|
|
:type chain_str: str
|
|
|
|
:returns: Transaction hash and id of cache element in storage backend, respectively
|
|
|
|
:rtype: tuple
|
|
|
|
"""
|
2021-03-29 15:27:53 +02:00
|
|
|
chain_spec = ChainSpec.from_dict(chain_spec_dict)
|
2021-02-01 18:12:51 +01:00
|
|
|
|
2021-03-29 15:27:53 +02:00
|
|
|
tx_signed_raw_bytes = bytes.fromhex(strip_0x(tx_signed_raw_hex))
|
2021-08-28 13:10:18 +02:00
|
|
|
tx = unpack_normal(tx_signed_raw_bytes, chain_spec)
|
2021-03-29 15:27:53 +02:00
|
|
|
tx_data = Faucet.parse_give_to_request(tx['data'])
|
2021-10-07 17:12:35 +02:00
|
|
|
sender_address = tx_normalize.wallet_address(tx['from'])
|
|
|
|
recipient_address = tx_normalize.wallet_address(tx['to'])
|
2021-02-01 18:12:51 +01:00
|
|
|
|
2021-03-29 15:27:53 +02:00
|
|
|
session = self.create_session()
|
2021-02-19 08:06:05 +01:00
|
|
|
|
2021-07-21 19:34:51 +02:00
|
|
|
tx_dict = {
|
2021-08-28 13:10:18 +02:00
|
|
|
'hash': tx['hash'],
|
2021-10-07 17:12:35 +02:00
|
|
|
'from': sender_address,
|
|
|
|
'to': recipient_address,
|
2021-08-28 13:10:18 +02:00
|
|
|
'source_token': ZERO_ADDRESS_NORMAL,
|
|
|
|
'destination_token': ZERO_ADDRESS_NORMAL,
|
2021-07-21 19:34:51 +02:00
|
|
|
'from_value': 0,
|
|
|
|
'to_value': 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
(tx_dict, cache_id) = cache_tx_dict(tx_dict, session=session)
|
2021-02-01 18:12:51 +01:00
|
|
|
session.close()
|
|
|
|
return (tx_hash_hex, cache_id)
|
|
|
|
|
|
|
|
|
2021-03-29 15:27:53 +02:00
|
|
|
@celery_app.task(bind=True, base=CriticalSQLAlchemyTask)
|
2021-02-01 18:12:51 +01:00
|
|
|
def cache_account_data(
|
2021-03-29 15:27:53 +02:00
|
|
|
self,
|
2021-02-01 18:12:51 +01:00
|
|
|
tx_hash_hex,
|
|
|
|
tx_signed_raw_hex,
|
2021-03-29 15:27:53 +02:00
|
|
|
chain_spec_dict,
|
2021-02-01 18:12:51 +01:00
|
|
|
):
|
|
|
|
"""Generates and commits transaction cache metadata for an AccountsIndex.add transaction
|
|
|
|
|
|
|
|
:param tx_hash_hex: Transaction hash
|
|
|
|
:type tx_hash_hex: str, 0x-hex
|
|
|
|
:param tx_signed_raw_hex: Raw signed transaction
|
|
|
|
:type tx_signed_raw_hex: str, 0x-hex
|
|
|
|
:param chain_str: Chain spec string representation
|
|
|
|
:type chain_str: str
|
|
|
|
:returns: Transaction hash and id of cache element in storage backend, respectively
|
|
|
|
:rtype: tuple
|
|
|
|
"""
|
2021-03-29 15:27:53 +02:00
|
|
|
chain_spec = ChainSpec.from_dict(chain_spec_dict)
|
2021-08-28 13:10:18 +02:00
|
|
|
tx_signed_raw_bytes = bytes.fromhex(strip_0x(tx_signed_raw_hex))
|
|
|
|
tx = unpack_normal(tx_signed_raw_bytes, chain_spec)
|
2021-05-02 19:11:03 +02:00
|
|
|
tx_data = AccountsIndex.parse_add_request(tx['data'])
|
2021-10-07 17:12:35 +02:00
|
|
|
sender_address = tx_normalize.wallet_address(tx['from'])
|
|
|
|
recipient_address = tx_normalize.wallet_address(tx['to'])
|
2021-02-01 18:12:51 +01:00
|
|
|
|
2021-02-19 08:06:05 +01:00
|
|
|
session = SessionBase.create_session()
|
2021-07-21 19:34:51 +02:00
|
|
|
tx_dict = {
|
2021-08-28 13:10:18 +02:00
|
|
|
'hash': tx['hash'],
|
2021-10-07 17:12:35 +02:00
|
|
|
'from': sender_address,
|
|
|
|
'to': recipient_address,
|
2021-08-28 13:10:18 +02:00
|
|
|
'source_token': ZERO_ADDRESS_NORMAL,
|
|
|
|
'destination_token': ZERO_ADDRESS_NORMAL,
|
2021-07-21 19:34:51 +02:00
|
|
|
'from_value': 0,
|
|
|
|
'to_value': 0,
|
|
|
|
}
|
|
|
|
(tx_dict, cache_id) = cache_tx_dict(tx_dict, session=session)
|
2021-02-01 18:12:51 +01:00
|
|
|
session.close()
|
|
|
|
return (tx_hash_hex, cache_id)
|