From 1edacf70cf7842a054ecab1344bc595b9477fa86 Mon Sep 17 00:00:00 2001 From: nolash Date: Thu, 25 Feb 2021 08:30:24 +0100 Subject: [PATCH] Rehabilitate faucet and gifter callback --- apps/cic-eth/cic_eth/api/api_admin.py | 1 + apps/cic-eth/cic_eth/eth/token.py | 13 +++- .../runnable/daemons/filters/callback.py | 60 +++++++++++-------- apps/cic-eth/cic_eth/runnable/view.py | 19 +++--- apps/contract-migration/docker/Dockerfile | 2 +- apps/requirements.txt | 2 +- 6 files changed, 60 insertions(+), 37 deletions(-) diff --git a/apps/cic-eth/cic_eth/api/api_admin.py b/apps/cic-eth/cic_eth/api/api_admin.py index 75a54bdc..ca43d037 100644 --- a/apps/cic-eth/cic_eth/api/api_admin.py +++ b/apps/cic-eth/cic_eth/api/api_admin.py @@ -457,6 +457,7 @@ class AdminApi: tx_unpacked = unpack_signed_raw_tx(bytes.fromhex(tx['signed_tx'][2:]), chain_spec.chain_id()) tx['gas_price'] = tx_unpacked['gasPrice'] tx['gas_limit'] = tx_unpacked['gas'] + tx['data'] = tx_unpacked['data'] s = celery.signature( 'cic_eth.queue.tx.get_state_log', diff --git a/apps/cic-eth/cic_eth/eth/token.py b/apps/cic-eth/cic_eth/eth/token.py index 8f3e8c6e..98dd1ec8 100644 --- a/apps/cic-eth/cic_eth/eth/token.py +++ b/apps/cic-eth/cic_eth/eth/token.py @@ -9,6 +9,7 @@ from cic_registry import CICRegistry from cic_registry import zero_address from cic_registry.chain import ChainSpec from hexathon import strip_0x +from chainlib.status import Status as TxStatus # platform imports from cic_eth.db.models.tx import TxCache @@ -478,6 +479,8 @@ class ExtendedTx: self.destination_token_symbol = '' self.source_token_decimals = ExtendedTx._default_decimals self.destination_token_decimals = ExtendedTx._default_decimals + self.status = TxStatus.PENDING.name + self.status_code = TxStatus.PENDING.value def set_actors(self, sender, recipient, trusted_declarator_addresses=None): @@ -505,10 +508,18 @@ class ExtendedTx: self.destination_token_value = destination_value + def set_status(self, n): + if n: + self.status = TxStatus.ERROR.name + else: + self.status = TxStatus.SUCCESS.name + self.status_code = n + + def to_dict(self): o = {} for attr in dir(self): - if attr[0] == '_' or attr in ['set_actors', 'set_tokens', 'to_dict']: + if attr[0] == '_' or attr in ['set_actors', 'set_tokens', 'set_status', 'to_dict']: continue o[attr] = getattr(self, attr) return o diff --git a/apps/cic-eth/cic_eth/runnable/daemons/filters/callback.py b/apps/cic-eth/cic_eth/runnable/daemons/filters/callback.py index f0426b5d..19faadfa 100644 --- a/apps/cic-eth/cic_eth/runnable/daemons/filters/callback.py +++ b/apps/cic-eth/cic_eth/runnable/daemons/filters/callback.py @@ -6,6 +6,8 @@ import web3 import celery from cic_registry.error import UnknownContractError from chainlib.status import Status as TxStatus +from chainlib.eth.address import to_checksum +from hexathon import strip_0x # local imports from .base import SyncFilter @@ -35,12 +37,13 @@ class CallbackFilter(SyncFilter): def call_back(self, transfer_type, result): + logg.debug('result {}'.format(result)) s = celery.signature( self.method, [ result, transfer_type, - int(rcpt.status == 0), + int(result['status_code'] == 0), ], queue=self.queue, ) @@ -60,39 +63,40 @@ class CallbackFilter(SyncFilter): def parse_data(self, tx): - #transfer_type = 'transfer' transfer_type = None transfer_data = None logg.debug('have payload {}'.format(tx.payload)) method_signature = tx.payload[:8] - if tx.status == TxStatus.ERROR: - logg.error('tx {} has failed, no callbacks will be called'.format(tx.hash)) + logg.debug('tx status {}'.format(tx.status)) + if method_signature == transfer_method_signature: + transfer_data = unpack_transfer(tx.payload) + transfer_data['from'] = tx['from'] + transfer_data['token_address'] = tx['to'] - else: - logg.debug('tx status {}'.format(tx.status)) - if method_signature == transfer_method_signature: - transfer_data = unpack_transfer(tx.payload) - transfer_data['from'] = tx['from'] - transfer_data['token_address'] = tx['to'] + elif method_signature == transferfrom_method_signature: + transfer_type = 'transferfrom' + transfer_data = unpack_transferfrom(tx.payload) + transfer_data['token_address'] = tx['to'] - elif method_signature == transferfrom_method_signature: - transfer_type = 'transferfrom' - transfer_data = unpack_transferfrom(tx.payload) - transfer_data['token_address'] = tx['to'] + # TODO: do not rely on logs here + elif method_signature == giveto_method_signature: + transfer_type = 'tokengift' + transfer_data = unpack_gift(tx.payload) + for l in tx.logs: + topics = l['topics'] + logg.debug('topixx {}'.format(topics)) + if strip_0x(topics[0]) == '45c201a59ac545000ead84f30b2db67da23353aa1d58ac522c48505412143ffa': + transfer_data['value'] = web3.Web3.toInt(hexstr=strip_0x(l['data'])) + #token_address_bytes = topics[2][32-20:] + token_address = strip_0x(topics[2])[64-40:] + transfer_data['token_address'] = to_checksum(token_address) + transfer_data['from'] = tx.inputs[0] - # TODO: do not rely on logs here - elif method_signature == giveto_method_signature: - transfer_type = 'tokengift' - transfer_data = unpack_gift(tx.payload) - for l in tx.logs: - if l.topics[0].hex() == '0x45c201a59ac545000ead84f30b2db67da23353aa1d58ac522c48505412143ffa': - transfer_data['value'] = web3.Web3.toInt(hexstr=l.data) - token_address_bytes = l.topics[2][32-20:] - transfer_data['token_address'] = web3.Web3.toChecksumAddress(token_address_bytes.hex()) - transfer_data['from'] = tx.to + logg.debug('resolved method {}'.format(transfer_type)) - logg.debug('resolved method {}'.format(transfer_type)) + if transfer_data != None: + transfer_data['status'] = tx.status return (transfer_type, transfer_data) @@ -122,7 +126,11 @@ class CallbackFilter(SyncFilter): tokentx = ExtendedTx(tx.hash, self.chain_spec) tokentx.set_actors(transfer_data['from'], transfer_data['to'], self.trusted_addresses) tokentx.set_tokens(transfer_data['token_address'], transfer_data['value']) - t = self.call_back(tokentx.to_dict()) + if transfer_data['status'] == 0: + tokentx.set_status(1) + else: + tokentx.set_status(0) + t = self.call_back(transfer_type, tokentx.to_dict()) logg.info('callback success task id {} tx {}'.format(t, tx.hash)) except UnknownContractError: logg.debug('callback filter {}:{} skipping "transfer" method on unknown contract {} tx {}'.format(tc.queue, tc.method, transfer_data['to'], tx.hash)) diff --git a/apps/cic-eth/cic_eth/runnable/view.py b/apps/cic-eth/cic_eth/runnable/view.py index 3402ece5..a9d964c0 100644 --- a/apps/cic-eth/cic_eth/runnable/view.py +++ b/apps/cic-eth/cic_eth/runnable/view.py @@ -18,6 +18,7 @@ import web3 from cic_registry import CICRegistry from cic_registry.chain import ChainSpec from cic_registry.chain import ChainRegistry +from hexathon import add_0x # local imports from cic_eth.api import AdminApi @@ -69,6 +70,8 @@ config.censor('PASSWORD', 'DATABASE') config.censor('PASSWORD', 'SSL') logg.debug('config loaded from {}:\n{}'.format(config_dir, config)) +config.add(add_0x(args.query), '_QUERY', True) + re_websocket = re.compile('^wss?://') re_http = re.compile('^https?://') blockchain_provider = config.get('ETH_PROVIDER') @@ -152,18 +155,18 @@ def render_lock(o, **kwargs): def main(): txs = [] renderer = render_tx - if len(args.query) > 66: - txs = [admin_api.tx(chain_spec, tx_raw=args.query)] - elif len(args.query) > 42: - txs = [admin_api.tx(chain_spec, tx_hash=args.query)] - elif len(args.query) == 42: - txs = admin_api.account(chain_spec, args.query, include_recipient=False) + if len(config.get('_QUERY')) > 66: + txs = [admin_api.tx(chain_spec, tx_raw=config.get('_QUERY'))] + elif len(config.get('_QUERY')) > 42: + txs = [admin_api.tx(chain_spec, tx_hash=config.get('_QUERY'))] + elif len(config.get('_QUERY')) == 42: + txs = admin_api.account(chain_spec, config.get('_QUERY'), include_recipient=False) renderer = render_account - elif len(args.query) >= 4 and args.query[:4] == 'lock': + elif len(config.get('_QUERY')) >= 4 and config.get('_QUERY')[:4] == 'lock': txs = admin_api.get_lock() renderer = render_lock else: - raise ValueError('cannot parse argument {}'.format(args.query)) + raise ValueError('cannot parse argument {}'.format(config.get('_QUERY'))) if len(txs) == 0: logg.info('no matches found') diff --git a/apps/contract-migration/docker/Dockerfile b/apps/contract-migration/docker/Dockerfile index d05f546d..69d876f2 100644 --- a/apps/contract-migration/docker/Dockerfile +++ b/apps/contract-migration/docker/Dockerfile @@ -107,7 +107,7 @@ RUN cd cic-bancor/python && \ RUN apt-get install -y cargo -ARG cic_base_version=0.1.1a4 +ARG cic_base_version=0.1.1a7 RUN pip install --extra-index-url $pip_extra_index_url cic-base[full_graph]==$cic_base_version ARG cic_registry_version=0.5.3a21 diff --git a/apps/requirements.txt b/apps/requirements.txt index ee777f19..e70f9c2a 100644 --- a/apps/requirements.txt +++ b/apps/requirements.txt @@ -42,6 +42,6 @@ rlp==2.0.1 cryptocurrency-cli-tools==0.0.4 giftable-erc20-token==0.0.7b12 hexathon==0.0.1a3 -chainlib==0.0.1a18 +chainlib==0.0.1a19 chainsyncer==0.0.1a18 cic-registry==0.5.3.a21