From 4cce47373f673b08372d2907647481ec0dd75df5 Mon Sep 17 00:00:00 2001 From: Ian Costanzo Date: Thu, 26 May 2022 09:22:04 -0700 Subject: [PATCH] Fix tails server upload multi-ledger mode Signed-off-by: Ian Costanzo --- aries_cloudagent/ledger/indy.py | 28 +++++++++++++++++++++ aries_cloudagent/tails/indy_tails_server.py | 16 +++++++++--- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/aries_cloudagent/ledger/indy.py b/aries_cloudagent/ledger/indy.py index 13f27abac2..96ac9e0e3a 100644 --- a/aries_cloudagent/ledger/indy.py +++ b/aries_cloudagent/ledger/indy.py @@ -5,6 +5,7 @@ import logging import tempfile from datetime import date, datetime +from io import StringIO from os import path from time import time from typing import Sequence, Tuple, Optional @@ -42,6 +43,17 @@ GENESIS_TRANSACTION_FILE = "indy_genesis_transactions.txt" +def _normalize_txns(txns: str) -> str: + """Normalize a set of genesis transactions.""" + lines = StringIO() + for line in txns.splitlines(): + line = line.strip() + if line: + lines.write(line) + lines.write("\n") + return lines.getvalue() + + class IndySdkLedgerPoolProvider(BaseProvider): """Indy ledger pool provider which keys off the selected pool name.""" @@ -107,12 +119,28 @@ def __init__( self.cache = cache self.cache_duration = cache_duration self.genesis_transactions = genesis_transactions + self.genesis_txns_cache = genesis_transactions self.handle = None self.name = name self.taa_cache = None self.read_only = read_only self.socks_proxy = socks_proxy + @property + def genesis_txns(self) -> str: + """Get the configured genesis transactions.""" + if not self.genesis_txns_cache: + try: + txn_path = path.join( + tempfile.gettempdir(), f"{self.name}_{GENESIS_TRANSACTION_FILE}" + ) + self.genesis_txns_cache = _normalize_txns(open(txn_path).read()) + except FileNotFoundError: + raise LedgerConfigError( + "Pool config '%s' not found", self.name + ) from None + return self.genesis_txns_cache + async def create_pool_config( self, genesis_transactions: str, recreate: bool = False ): diff --git a/aries_cloudagent/tails/indy_tails_server.py b/aries_cloudagent/tails/indy_tails_server.py index a7aee90a58..9f07970a42 100644 --- a/aries_cloudagent/tails/indy_tails_server.py +++ b/aries_cloudagent/tails/indy_tails_server.py @@ -1,6 +1,7 @@ """Indy tails server interface class.""" from typing import Tuple +import logging from ..config.injection_context import InjectionContext from ..ledger.multiple_ledger.base_manager import BaseMultipleLedgerManager @@ -10,6 +11,9 @@ from .error import TailsServerNotConfiguredError +LOGGER = logging.getLogger(__name__) + + class IndyTailsServer(BaseTailsServer): """Indy tails server interface.""" @@ -38,12 +42,16 @@ async def upload_tails_file( if not genesis_transactions: ledger_manager = context.injector.inject(BaseMultipleLedgerManager) write_ledgers = await ledger_manager.get_write_ledger() + LOGGER.debug(f"write_ledgers = {write_ledgers}") pool = write_ledgers[1].pool + LOGGER.debug(f"write_ledger pool = {pool}") + + genesis_transactions = pool.genesis_txns - try: - genesis_transactions = pool.genesis_transactions - except AttributeError: - genesis_transactions = pool.genesis_txns_cache + if not genesis_transactions: + raise TailsServerNotConfiguredError( + "no genesis_transactions for writable ledger" + ) if not tails_server_upload_url: raise TailsServerNotConfiguredError(