Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Analyze if we can easily develop an AsyncEthereumClient #1269

Open
Uxio0 opened this issue Aug 6, 2024 · 1 comment
Open

Analyze if we can easily develop an AsyncEthereumClient #1269

Uxio0 opened this issue Aug 6, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@Uxio0
Copy link
Member

Uxio0 commented Aug 6, 2024

Context

  • Web3 supports async providers. It should be posible for us to do the same and allow people using async frameworks like FastAPI to use our EthereumClient
  • AsyncWeb3 must be used instead of Web3
  • Calls to self.http_session.post should be refactored to something similar to self.post_request or similar, so we don't expose what library are we using
  • Instead of requests, we aioHTTP must be used

Analyze if we can implement something similar with not much overhead. That might not be posible with our current implementation. If after the analysis we decide it takes more than one sprint to implement, we should meet and decide what to do

@Uxio0 Uxio0 added the enhancement New feature or request label Aug 6, 2024
@DavidRomanovizc
Copy link

Hi @Uxio0, I would like to assist with the analysis of this issue. What are your thoughts on adding an is_async flag to EthereumClient to initialize async/sync providers based on it? Existing methods will call their sync or async impl, which we will encapsulate. This approach minimizes overhead and allows for flexible switching between sync and async impl without significant code changes. Additionally, it simplifies the integration of the async client as it does not require reworking all methods and interfaces immediately, allowing for gradual impl and easier testing.

I think it could look like this:

class EthereumClient:
    def __init__(
            self, ..., is_async: bool = False):
        ...
        self.is_async = is_async
        self._initialize_providers()
        ...

    def _initialize_providers(self):
        if self.is_async:
            self.w3_provider = AsyncHTTPProvider(
                self.ethereum_node_url,
                request_kwargs={"timeout": self.provider_timeout},
            )
            self.w3_slow_provider = AsyncHTTPProvider(
                self.ethereum_node_url,
                request_kwargs={"timeout": self.slow_provider_timeout},
            )
            self.w3 = AsyncWeb3(self.w3_provider)
            self.slow_w3 = AsyncWeb3(self.w3_slow_provider)
        else:
            self.w3_provider = HTTPProvider(
                self.ethereum_node_url,
                request_kwargs={"timeout": self.provider_timeout},
            )
            self.w3_slow_provider = HTTPProvider(
                self.ethereum_node_url,
                request_kwargs={"timeout": self.slow_provider_timeout},
            )
            self.w3 = Web3(self.w3_provider)
            self.slow_w3 = Web3(self.w3_slow_provider)

    @property
    def current_block_number(self):
        if self.is_async:
            return self._async_current_block_number()
        else:
            return self._sync_current_block_number()

    def _sync_current_block_number(self):
        return self.w3.eth.block_number

    async def _async_current_block_number(self):
        return await self.w3.eth.block_number

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants