Compare commits

...

2 Commits

Author SHA1 Message Date
nolash
1c022e9853 Added changes to wrong branch 2021-10-29 07:33:38 +02:00
nolash
d35e144723 Register gas cache only for registered tokens 2021-10-29 07:00:25 +02:00
5 changed files with 53 additions and 17 deletions

View File

@@ -10,6 +10,9 @@ from chainlib.eth.tx import (
TxFormat,
unpack,
)
from chainlib.eth.contract import (
ABIContractEncoder,
)
from cic_eth_registry import CICRegistry
from cic_eth_registry.erc20 import ERC20Token
from hexathon import (
@@ -155,7 +158,7 @@ def transfer_from(self, tokens, holder_address, receiver_address, value, chain_s
session = self.create_session()
nonce_oracle = CustodialTaskNonceOracle(holder_address, self.request.root_id, session=session)
gas_oracle = self.create_gas_oracle(rpc, MaxGasOracle.gas)
gas_oracle = self.create_gas_oracle(rpc, t['address'], MaxGasOracle.gas)
c = ERC20(chain_spec, signer=rpc_signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle)
try:
(tx_hash_hex, tx_signed_raw_hex) = c.transfer_from(t['address'], spender_address, holder_address, receiver_address, value, tx_format=TxFormat.RLP_SIGNED)
@@ -226,6 +229,11 @@ def transfer(self, tokens, holder_address, receiver_address, value, chain_spec_d
session = self.create_session()
nonce_oracle = CustodialTaskNonceOracle(holder_address, self.request.root_id, session=session)
enc = ABIContractEncoder()
enc.method('transferFrom')
method = enc.get()
gas_oracle = self.create_gas_oracle(rpc, MaxGasOracle.gas)
c = ERC20(chain_spec, signer=rpc_signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle)
try:

View File

@@ -71,13 +71,6 @@ celery_app = celery.current_app
logg = logging.getLogger()
MAXIMUM_FEE_UNITS = 8000000
class MaxGasOracle:
def gas(code=None):
return MAXIMUM_FEE_UNITS
@celery_app.task(base=CriticalSQLAlchemyTask)
def apply_gas_value_cache(address, method, value, tx_hash):
@@ -85,8 +78,8 @@ def apply_gas_value_cache(address, method, value, tx_hash):
def apply_gas_value_cache_local(address, method, value, tx_hash, session=None):
address = strip_0x(address)
tx_hash = strip_0x(tx_hash)
address = tx_normalize.executable_address(address)
tx_hash = tx_normalize.tx_hash(tx_hash)
value = int(value)
session = SessionBase.bind_session(session)

View File

@@ -1,20 +1,32 @@
# standard imports
import logging
# external imports
from eth_erc20 import ERC20
from chainlib.eth.contract import (
ABIContractEncoder,
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
# local imports
from .base import SyncFilter
#logg = logging.getLogger(__name__)
logg = logging.getLogger()
class TokenFilter(SyncFilter):
def __init__(self, chain_spec, queue):
def __init__(self, chain_spec, queue, call_address=ZERO_ADDRESS):
self.queue = queue
self.chain_spec = chain_spec
self.caller_address = call_address
def filter(self, conn, block, tx, db_session=None):
@@ -25,7 +37,15 @@ class TokenFilter(SyncFilter):
r = ERC20.parse_transfer_request(tx.payload)
except RequestMismatchException:
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.method('transfer')
method = enc.get()
@@ -33,7 +53,7 @@ class TokenFilter(SyncFilter):
s = celery.signature(
'cic_eth.eth.gas.apply_gas_value_cache',
[
tx.inputs[0],
token_address,
method,
tx.gas_used,
tx.hash,

View File

@@ -10,7 +10,6 @@ from chainlib.chain import ChainSpec
from chainlib.connection import RPCConnection
from chainlib.eth.constant import ZERO_ADDRESS
from chainlib.eth.nonce import RPCNonceOracle
from chainlib.eth.gas import RPCGasOracle
from cic_eth_registry import CICRegistry
from cic_eth_registry.error import UnknownContractError
@@ -30,7 +29,7 @@ class BaseTask(celery.Task):
call_address = ZERO_ADDRESS
trusted_addresses = []
create_nonce_oracle = RPCNonceOracle
create_gas_oracle = RPCGasOracle
create_gas_oracle = CacheGasOracle
default_token_address = None
default_token_symbol = None
default_token_name = None

View File

@@ -20,6 +20,7 @@ from chainlib.eth.block import (
)
from chainlib.eth.contract import ABIContractEncoder
from hexathon import strip_0x
from eth_token_index import TokenUniqueSymbolIndex
# local imports
from cic_eth.runnable.daemons.filters.token import TokenFilter
@@ -32,10 +33,14 @@ def test_filter_gas(
init_database,
eth_rpc,
eth_signer,
contract_roles,
agent_roles,
token_roles,
foo_token,
celery_session_worker,
token_registry,
register_lookups,
celery_worker,
cic_registry,
):
rpc = RPCConnection.connect(default_chain_spec, 'default')
@@ -49,7 +54,7 @@ def test_filter_gas(
rcpt = eth_rpc.do(o)
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()
r = eth_rpc.do(o)
@@ -62,6 +67,17 @@ def test_filter_gas(
tx_src = unpack(tx_signed_raw_bytes, default_chain_spec)
tx = Tx(tx_src, block=block)
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)
r = t.get_leaf()
assert t.successful()