Skip to content

Commit

Permalink
Internal: split chain service (#487)
Browse files Browse the repository at this point in the history
Problem: the chain service is in charge of providing all chain-related
functionalities, namely: verify signature and read/write on chain. It
turns out that most use cases of the chain service only care about
signature verification, meaning that the dependencies required for the
chain service are not necessary most of the time. As we plan on adding
more dependencies for chain readers, the chain service is getting too
complex for its own good.

Solution: split the chain service into a signature verifier class and a
chain connector class. This way, the signature verifier can be
instantiated without dependencies in all the places where it is required
and the chain connector can get a bit more complex without impacting the
rest.

Other changes:
* Removed the conditional import of chain connectors as they must all be
  installed on each node.
  • Loading branch information
odesenfans committed Oct 20, 2023
1 parent eba0edd commit a6710e7
Show file tree
Hide file tree
Showing 33 changed files with 314 additions and 365 deletions.
11 changes: 4 additions & 7 deletions src/aleph/api_entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from configmanager import Config

import aleph.config
from aleph.chains.chain_service import ChainService
from aleph.chains.signature_verifier import SignatureVerifier
from aleph.db.connection import make_engine, make_session_factory
from aleph.services.cache.node_cache import NodeCache
from aleph.services.ipfs import IpfsService
Expand All @@ -25,7 +25,7 @@
APP_STATE_STORAGE_SERVICE,
APP_STATE_MQ_CHANNEL,
APP_STATE_MQ_WS_CHANNEL,
APP_STATE_CHAIN_SERVICE,
APP_STATE_SIGNATURE_VERIFIER,
)


Expand Down Expand Up @@ -53,9 +53,7 @@ async def configure_aiohttp_app(
ipfs_service=ipfs_service,
node_cache=node_cache,
)
chain_service = ChainService(
storage_service=storage_service, session_factory=session_factory
)
signature_verifier = SignatureVerifier()

app = create_aiohttp_app()

Expand All @@ -74,7 +72,7 @@ async def configure_aiohttp_app(
app[APP_STATE_NODE_CACHE] = node_cache
app[APP_STATE_STORAGE_SERVICE] = storage_service
app[APP_STATE_SESSION_FACTORY] = session_factory
app[APP_STATE_CHAIN_SERVICE] = chain_service
app[APP_STATE_SIGNATURE_VERIFIER] = signature_verifier

return app

Expand All @@ -95,5 +93,4 @@ async def create_app() -> web.Application:


if __name__ == "__main__":
import asyncio
web.run_app(create_app(), host="127.0.0.1", port=8000)
24 changes: 24 additions & 0 deletions src/aleph/chains/abc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import abc

from configmanager import Config

from aleph.schemas.pending_messages import BasePendingMessage
from aleph.types.chain_sync import ChainEventType


class Verifier(abc.ABC):
@abc.abstractmethod
async def verify_signature(self, message: BasePendingMessage) -> bool:
...


class ChainReader(abc.ABC):
@abc.abstractmethod
async def fetcher(self, config: Config):
...


class ChainWriter(ChainReader):
@abc.abstractmethod
async def packer(self, config: Config):
...
2 changes: 1 addition & 1 deletion src/aleph/chains/avalanche.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from aleph.chains.common import get_verification_buffer
from aleph.schemas.pending_messages import BasePendingMessage
from .connector import Verifier
from .abc import Verifier

LOGGER = logging.getLogger("chains.avalanche")
CHAIN_NAME = "AVAX"
Expand Down
2 changes: 1 addition & 1 deletion src/aleph/chains/bsc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from configmanager import Config

from aleph.chains.chaindata import ChainDataService
from aleph.chains.connector import ChainReader
from aleph.chains.abc import ChainReader
from aleph.chains.indexer_reader import AlephIndexerReader
from aleph.types.chain_sync import ChainEventType
from aleph.types.db_session import DbSessionFactory
Expand Down
189 changes: 0 additions & 189 deletions src/aleph/chains/chain_service.py

This file was deleted.

Loading

0 comments on commit a6710e7

Please sign in to comment.