Skip to content

Commit

Permalink
Merge pull request #672 from sklump/fix-provisioning-on-read-only-ledger
Browse files Browse the repository at this point in the history
Fix provisioning on read only ledger
  • Loading branch information
andrewwhitehead committed Aug 20, 2020
2 parents 5fc6394 + f25f676 commit f75a467
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
25 changes: 15 additions & 10 deletions aries_cloudagent/config/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from prompt_toolkit.formatted_text import HTML

from ..ledger.base import BaseLedger
from ..ledger.error import LedgerError
from ..utils.http import fetch, FetchError
from ..wallet.base import BaseWallet

Expand Down Expand Up @@ -63,23 +64,27 @@ async def ledger_config(

async with ledger:
# Check transaction author agreement acceptance
taa_info = await ledger.get_txn_author_agreement()
if taa_info["taa_required"] and public_did:
taa_accepted = await ledger.get_latest_txn_author_acceptance()
if (
not taa_accepted
or taa_info["taa_record"]["digest"] != taa_accepted["digest"]
):
if not await accept_taa(ledger, taa_info, provision):
return False
if not context.settings.get("read_only_ledger"):
taa_info = await ledger.get_txn_author_agreement()
if taa_info["taa_required"] and public_did:
taa_accepted = await ledger.get_latest_txn_author_acceptance()
if (
not taa_accepted
or taa_info["taa_record"]["digest"] != taa_accepted["digest"]
):
if not await accept_taa(ledger, taa_info, provision):
return False

# Publish endpoint if necessary - skipped if TAA is required but not accepted
endpoint = context.settings.get("default_endpoint")
if public_did:
wallet: BaseWallet = await context.inject(BaseWallet)
if wallet.type != "indy":
raise ConfigError("Cannot provision a non-Indy wallet type")
await wallet.set_did_endpoint(public_did, endpoint, ledger)
try:
await wallet.set_did_endpoint(public_did, endpoint, ledger)
except LedgerError as x_ledger:
raise ConfigError(x_ledger.message) from x_ledger # e.g., read-only

return True

Expand Down
35 changes: 35 additions & 0 deletions aries_cloudagent/config/tests/test_ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from asynctest import TestCase as AsyncTestCase, mock as async_mock

from ...ledger.base import BaseLedger
from ...ledger.error import LedgerError
from ...wallet.base import BaseWallet

from .. import ledger as test_module
Expand Down Expand Up @@ -208,6 +209,40 @@ async def test_ledger_config_genesis_url_no_taa_accept(self):
context, TEST_DID, provision=True
)

async def test_ledger_config_read_only_skip_taa_accept(self):
settings = {
"ledger.genesis_url": "00000000000000000000000000000000",
"read_only_ledger": True,
}
mock_ledger = async_mock.MagicMock(
type="indy",
get_txn_author_agreement=async_mock.CoroutineMock(),
get_latest_txn_author_acceptance=async_mock.CoroutineMock(),
)
mock_wallet = async_mock.MagicMock(
type="indy",
set_did_endpoint=async_mock.CoroutineMock(
side_effect=LedgerError(
"Error cannot update endpoint when ledger is in read only mode"
)
),
)

context = InjectionContext(settings=settings, enforce_typing=False)
context.injector.bind_instance(BaseLedger, mock_ledger)
context.injector.bind_instance(BaseWallet, mock_wallet)

with async_mock.patch.object(
test_module, "fetch_genesis_transactions", async_mock.CoroutineMock()
) as mock_fetch, async_mock.patch.object(
test_module, "accept_taa", async_mock.CoroutineMock()
) as mock_accept_taa:
with self.assertRaises(test_module.ConfigError) as x_context:
await test_module.ledger_config(context, TEST_DID, provision=True)
assert "ledger is in read only mode" in str(x_context.exception)
mock_ledger.get_txn_author_agreement.assert_not_called()
mock_ledger.get_latest_txn_author_acceptance.assert_not_called()

async def test_ledger_config_genesis_file_non_indy_wallet(self):
settings = {
"ledger.genesis_file": "/tmp/genesis/path",
Expand Down

0 comments on commit f75a467

Please sign in to comment.