Provide thread safe registry solution

This commit is contained in:
nolash 2021-03-08 10:11:04 +01:00
parent abc9877726
commit b37c7f3cc2
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
12 changed files with 44 additions and 91 deletions

View File

@ -4,13 +4,13 @@ import logging
# third-party imports
import web3
import celery
from cic_registry import CICRegistry
from cic_registry.chain import ChainSpec
from erc20_single_shot_faucet import Faucet
from cic_registry import zero_address
from hexathon import strip_0x
# local import
from cic_eth.registry import safe_registry
from cic_eth.eth import RpcClient
from cic_eth.eth import registry_extra_identifiers
from cic_eth.eth.task import sign_and_register_tx
@ -55,7 +55,7 @@ class AccountTxFactory(TxFactory):
:rtype: dict
"""
c = CICRegistry.get_contract(chain_spec, 'AccountRegistry')
c = self.registry.get_contract(chain_spec, 'AccountRegistry')
f = c.function('add')
tx_add_buildable = f(
address,
@ -89,7 +89,7 @@ class AccountTxFactory(TxFactory):
:rtype: dict
"""
c = CICRegistry.get_contract(chain_spec, 'Faucet')
c = self.registry.get_contract(chain_spec, 'Faucet')
f = c.function('giveTo')
tx_add_buildable = f(address)
gas = c.gas('add')
@ -205,7 +205,8 @@ def register(self, account_address, chain_str, writer_address=None):
queue = self.request.delivery_info['routing_key']
c = RpcClient(chain_spec, holder_address=writer_address)
txf = AccountTxFactory(writer_address, c)
registry = safe_registry(c.w3)
txf = AccountTxFactory(writer_address, c, registry=registry)
tx_add = txf.add(account_address, chain_spec, self.request.root_id, session=session)
@ -244,7 +245,8 @@ def gift(self, account_address, chain_str):
queue = self.request.delivery_info['routing_key']
c = RpcClient(chain_spec, holder_address=account_address)
txf = AccountTxFactory(account_address, c)
registry = safe_registry(c.w3)
txf = AccountTxFactory(account_address, c, registry=registry)
session = SessionBase.create_session()
tx_add = txf.gift(account_address, chain_spec, self.request.root_id, session=session)

View File

@ -22,8 +22,9 @@ class TxFactory:
"""Gas price, updated between batches"""
def __init__(self, from_address, rpc_client):
def __init__(self, from_address, rpc_client, registry=CICRegistry):
self.address = from_address
self.registry = registry
self.default_nonce = rpc_client.w3.eth.getTransactionCount(from_address, 'pending')
self.nonce_oracle = NonceOracle(from_address, self.default_nonce)

View File

@ -5,13 +5,13 @@ import logging
import celery
import requests
import web3
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.registry import safe_registry
from cic_eth.db.models.tx import TxCache
from cic_eth.db.models.base import SessionBase
from cic_eth.eth import RpcClient
@ -63,7 +63,7 @@ class TokenTxFactory(TxFactory):
:returns: Unsigned "approve" transaction in standard Ethereum format
:rtype: dict
"""
source_token = CICRegistry.get_address(chain_spec, token_address)
source_token = self.registry.get_address(chain_spec, token_address)
source_token_contract = source_token.contract
tx_approve_buildable = source_token_contract.functions.approve(
spender_address,
@ -103,7 +103,7 @@ class TokenTxFactory(TxFactory):
:returns: Unsigned "transfer" transaction in standard Ethereum format
:rtype: dict
"""
source_token = CICRegistry.get_address(chain_spec, token_address)
source_token = self.registry.get_address(chain_spec, token_address)
source_token_contract = source_token.contract
transfer_buildable = source_token_contract.functions.transfer(
receiver_address,
@ -202,11 +202,9 @@ def balance(tokens, holder_address, chain_str):
#abi = ContractRegistry.abi('ERC20Token')
chain_spec = ChainSpec.from_chain_str(chain_str)
c = RpcClient(chain_spec)
registry = safe_registry(c.w3)
for t in tokens:
#token = CICRegistry.get_address(t['address'])
#abi = token.abi()
#o = c.w3.eth.contract(abi=abi, address=t['address'])
o = CICRegistry.get_address(chain_spec, t['address']).contract
o = registry.get_address(chain_spec, t['address']).contract
b = o.functions.balanceOf(holder_address).call()
t['balance_network'] = b
@ -247,8 +245,9 @@ def transfer(self, tokens, holder_address, receiver_address, value, chain_str):
t = tokens[0]
c = RpcClient(chain_spec, holder_address=holder_address)
registry = safe_registry(c.w3)
txf = TokenTxFactory(holder_address, c)
txf = TokenTxFactory(holder_address, c, registry=registry)
session = SessionBase.create_session()
tx_transfer = txf.transfer(t['address'], receiver_address, value, chain_spec, self.request.root_id, session=session)
@ -303,8 +302,9 @@ def approve(self, tokens, holder_address, spender_address, value, chain_str):
t = tokens[0]
c = RpcClient(chain_spec, holder_address=holder_address)
registry = safe_registry(c.w3)
txf = TokenTxFactory(holder_address, c)
txf = TokenTxFactory(holder_address, c, registry=registry)
session = SessionBase.create_session()
tx_transfer = txf.approve(t['address'], spender_address, value, chain_spec, self.request.root_id, session=session)
@ -339,8 +339,10 @@ def resolve_tokens_by_symbol(token_symbols, chain_str):
"""
tokens = []
chain_spec = ChainSpec.from_chain_str(chain_str)
c = RpcClient(chain_spec)
registry = safe_registry(c.w3)
for token_symbol in token_symbols:
token = CICRegistry.get_token(chain_spec, token_symbol)
token = registry.get_token(chain_spec, token_symbol)
tokens.append({
'address': token.address(),
'converters': [],
@ -502,12 +504,14 @@ class ExtendedTx:
def set_tokens(self, source, source_value, destination=None, destination_value=None):
c = RpcClient(self._chain_spec)
registry = safe_registry(c.w3)
if destination == None:
destination = source
if destination_value == None:
destination_value = source_value
st = CICRegistry.get_address(self._chain_spec, source)
dt = CICRegistry.get_address(self._chain_spec, destination)
st = registry.get_address(self._chain_spec, source)
dt = registry.get_address(self._chain_spec, destination)
self.source_token = source
self.source_token_symbol = st.symbol()
self.source_token_decimals = st.decimals()

View File

@ -4,7 +4,10 @@ import logging
# third-party imports
import celery
from cic_registry.chain import ChainSpec
from cic_registry import CICRegistry
# local imports
from cic_eth.eth import RpcClient
from cic_eth.registry import safe_registry
celery_app = celery.current_app
@ -12,8 +15,10 @@ logg = logging.getLogger()
def translate_address(address, trusted_addresses, chain_spec):
c = RpcClient(chain_spec)
registry = safe_registry(c.w3)
for trusted_address in trusted_addresses:
o = CICRegistry.get_contract(chain_spec, 'AddressDeclarator', 'Declarator')
o = registry.get_contract(chain_spec, 'AddressDeclarator', 'Declarator')
fn = o.function('declaration')
declaration_hex = fn(trusted_address, address).call()
declaration_bytes = declaration_hex[0].rstrip(b'\x00')

View File

@ -7,10 +7,10 @@ import web3
import celery
import moolb
from cic_registry.chain import ChainSpec
from cic_registry.registry import CICRegistry
from hexathon import strip_0x
# local imports
from cic_eth.registry import safe_registry
from cic_eth.eth.rpc import RpcClient
from cic_eth.db.models.otx import Otx
from cic_eth.eth.util import unpack_signed_raw_tx
@ -51,6 +51,7 @@ def list_tx_by_bloom(bloomspec, address, chain_str):
"""
chain_spec = ChainSpec.from_chain_str(chain_str)
c = RpcClient(chain_spec)
registry = safe_registry(c.w3)
block_filter_data = bytes.fromhex(bloomspec['block_filter'])
tx_filter_data = bytes.fromhex(bloomspec['blocktx_filter'])
databitlen = len(block_filter_data)*8
@ -97,7 +98,7 @@ def list_tx_by_bloom(bloomspec, address, chain_str):
tx_hash_hex = tx['hash'].hex()
token = CICRegistry.get_address(chain_spec, tx['to'])
token = registry.get_address(chain_spec, tx['to'])
token_symbol = token.symbol()
token_decimals = token.decimals()
times = tx_times(tx_hash_hex, chain_str)

View File

@ -12,8 +12,6 @@ from sqlalchemy import tuple_
from sqlalchemy import func
# local imports
from cic_registry import CICRegistry
from cic_registry.chain import ChainSpec
from cic_eth.db.models.otx import Otx
from cic_eth.db.models.otx import OtxStateLog
from cic_eth.db.models.tx import TxCache

View File

@ -1,11 +1,8 @@
# standard imports
import logging
import copy
# external imports
from cic_registry import CICRegistry
from eth_token_index import TokenUniqueSymbolIndex
from eth_accounts_index import AccountRegistry
from chainlib.chain import ChainSpec
from cic_registry.chain import ChainRegistry
from cic_registry.helper.declarator import DeclaratorOracleAdapter
@ -13,55 +10,11 @@ from cic_registry.helper.declarator import DeclaratorOracleAdapter
logg = logging.getLogger(__name__)
class TokenOracle:
def __init__(self, conn, chain_spec, registry):
self.tokens = []
self.chain_spec = chain_spec
self.registry = registry
token_registry_contract = CICRegistry.get_contract(chain_spec, 'TokenRegistry', 'Registry')
self.getter = TokenUniqueSymbolIndex(conn, token_registry_contract.address())
def get_tokens(self):
token_count = self.getter.count()
if token_count == len(self.tokens):
return self.tokens
for i in range(len(self.tokens), token_count):
token_address = self.getter.get_index(i)
t = self.registry.get_address(self.chain_spec, token_address)
token_symbol = t.symbol()
self.tokens.append(t)
logg.debug('adding token idx {} symbol {} address {}'.format(i, token_symbol, token_address))
return copy.copy(self.tokens)
class AccountsOracle:
def __init__(self, conn, chain_spec, registry):
self.accounts = []
self.chain_spec = chain_spec
self.registry = registry
accounts_registry_contract = CICRegistry.get_contract(chain_spec, 'AccountRegistry', 'Registry')
self.getter = AccountRegistry(conn, accounts_registry_contract.address())
def get_accounts(self):
accounts_count = self.getter.count()
if accounts_count == len(self.accounts):
return self.accounts
for i in range(len(self.accounts), accounts_count):
account = self.getter.get_index(i)
self.accounts.append(account)
logg.debug('adding account {}'.format(account))
return copy.copy(self.accounts)
def safe_registry(w3):
"""Temporary workaround for enabling thread-safe usage of the CICRegistry.
"""
CICRegistry.w3 = w3
return CICRegistry
def init_registry(config, w3):

View File

@ -39,20 +39,9 @@ from chainsyncer.db.models.base import SessionBase
# local imports
from cic_eth.registry import init_registry
from cic_eth.eth import RpcClient
from cic_eth.db import Otx
from cic_eth.db import TxConvertTransfer
from cic_eth.db.models.tx import TxCache
from cic_eth.db.enum import StatusEnum
from cic_eth.db import dsn_from_config
from cic_eth.queue.tx import get_paused_txs
#from cic_eth.sync import Syncer
#from cic_eth.sync.error import LoopDone
from cic_eth.db.error import UnknownConvertError
from cic_eth.eth.util import unpack_signed_raw_tx
from cic_eth.eth.task import create_check_gas_and_send_task
from cic_eth.eth.token import unpack_transfer
from cic_eth.eth.token import unpack_transferfrom
from cic_eth.eth.account import unpack_gift
from cic_eth.runnable.daemons.filters import (
CallbackFilter,
GasFilter,

View File

@ -1,4 +1,4 @@
cic-base~=0.1.1a20
cic-base~=0.1.1a24
web3==5.12.2
celery==4.4.7
crypto-dev-signer~=0.4.13rc4

View File

@ -1,4 +1,4 @@
cic_base[full-graph]~=0.1.1a19
cic_base[full_graph]~=0.1.1a24
alembic==1.4.2
bcrypt==3.2.0
celery==4.4.7

View File

@ -107,7 +107,7 @@ RUN cd cic-bancor/python && \
RUN apt-get install -y cargo
ARG cic_base_version=0.1.1a23
ARG cic_base_version=0.1.1a24
RUN pip install --extra-index-url $pip_extra_index_url cic-base[full_graph]==$cic_base_version
ARG cic_registry_version=0.5.3a24

View File

@ -1,3 +1,3 @@
cic-base[full_graph]==0.1.1a23
cic-base[full_graph]==0.1.1a24
cic-eth==0.10.0a41
cic-types==0.1.0a8