Register gas cache only for registered tokens

This commit is contained in:
nolash 2021-10-29 07:00:25 +02:00
parent fb953d0318
commit d35e144723
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
2 changed files with 41 additions and 5 deletions

View File

@ -1,20 +1,32 @@
# standard imports
import logging
# external imports # external imports
from eth_erc20 import ERC20 from eth_erc20 import ERC20
from chainlib.eth.contract import ( from chainlib.eth.contract import (
ABIContractEncoder, ABIContractEncoder,
ABIContractType, ABIContractType,
) )
from chainlib.eth.constant import ZERO_ADDRESS
from chainlib.eth.address import is_same_address
from cic_eth_registry import CICRegistry
from cic_eth_registry.erc20 import ERC20Token
from eth_token_index import TokenUniqueSymbolIndex
import celery import celery
# local imports # local imports
from .base import SyncFilter from .base import SyncFilter
#logg = logging.getLogger(__name__)
logg = logging.getLogger()
class TokenFilter(SyncFilter): class TokenFilter(SyncFilter):
def __init__(self, chain_spec, queue): def __init__(self, chain_spec, queue, call_address=ZERO_ADDRESS):
self.queue = queue self.queue = queue
self.chain_spec = chain_spec self.chain_spec = chain_spec
self.caller_address = call_address
def filter(self, conn, block, tx, db_session=None): def filter(self, conn, block, tx, db_session=None):
@ -25,7 +37,15 @@ class TokenFilter(SyncFilter):
r = ERC20.parse_transfer_request(tx.payload) r = ERC20.parse_transfer_request(tx.payload)
except RequestMismatchException: except RequestMismatchException:
return (None, None) return (None, None)
token_address = tx.inputs[0]
token = ERC20Token(self.chain_spec, conn, token_address)
registry = CICRegistry(self.chain_spec, conn)
r = registry.by_name(token.symbol, sender_address=self.caller_address)
if is_same_address(r, ZERO_ADDRESS):
return None
enc = ABIContractEncoder() enc = ABIContractEncoder()
enc.method('transfer') enc.method('transfer')
method = enc.get() method = enc.get()
@ -33,7 +53,7 @@ class TokenFilter(SyncFilter):
s = celery.signature( s = celery.signature(
'cic_eth.eth.gas.apply_gas_value_cache', 'cic_eth.eth.gas.apply_gas_value_cache',
[ [
tx.inputs[0], token_address,
method, method,
tx.gas_used, tx.gas_used,
tx.hash, tx.hash,

View File

@ -20,6 +20,7 @@ from chainlib.eth.block import (
) )
from chainlib.eth.contract import ABIContractEncoder from chainlib.eth.contract import ABIContractEncoder
from hexathon import strip_0x from hexathon import strip_0x
from eth_token_index import TokenUniqueSymbolIndex
# local imports # local imports
from cic_eth.runnable.daemons.filters.token import TokenFilter from cic_eth.runnable.daemons.filters.token import TokenFilter
@ -32,10 +33,14 @@ def test_filter_gas(
init_database, init_database,
eth_rpc, eth_rpc,
eth_signer, eth_signer,
contract_roles,
agent_roles, agent_roles,
token_roles, token_roles,
foo_token, foo_token,
celery_session_worker, token_registry,
register_lookups,
celery_worker,
cic_registry,
): ):
rpc = RPCConnection.connect(default_chain_spec, 'default') rpc = RPCConnection.connect(default_chain_spec, 'default')
@ -49,7 +54,7 @@ def test_filter_gas(
rcpt = eth_rpc.do(o) rcpt = eth_rpc.do(o)
assert rcpt['status'] == 1 assert rcpt['status'] == 1
fltr = TokenFilter(default_chain_spec, queue=None) fltr = TokenFilter(default_chain_spec, queue=None, call_address=agent_roles['ALICE'])
o = block_latest() o = block_latest()
r = eth_rpc.do(o) r = eth_rpc.do(o)
@ -62,6 +67,17 @@ def test_filter_gas(
tx_src = unpack(tx_signed_raw_bytes, default_chain_spec) tx_src = unpack(tx_signed_raw_bytes, default_chain_spec)
tx = Tx(tx_src, block=block) tx = Tx(tx_src, block=block)
tx.apply_receipt(rcpt) tx.apply_receipt(rcpt)
t = fltr.filter(eth_rpc, block, tx, db_session=init_database)
assert t == None
nonce_oracle = RPCNonceOracle(contract_roles['CONTRACT_DEPLOYER'], eth_rpc)
c = TokenUniqueSymbolIndex(default_chain_spec, signer=eth_signer, nonce_oracle=nonce_oracle)
(tx_hash_hex_register, o) = c.register(token_registry, contract_roles['CONTRACT_DEPLOYER'], foo_token)
eth_rpc.do(o)
o = receipt(tx_hash_hex)
r = eth_rpc.do(o)
assert r['status'] == 1
t = fltr.filter(eth_rpc, block, tx, db_session=init_database) t = fltr.filter(eth_rpc, block, tx, db_session=init_database)
r = t.get_leaf() r = t.get_leaf()
assert t.successful() assert t.successful()