Beispiel #1
0
def add_new_nym(looper,
                sdk_pool_handle,
                creators_wallets,
                alias=None,
                role=None,
                seed=None,
                dest=None,
                verkey=None,
                skipverkey=False,
                no_wait=False):
    seed = seed or randomString(32)
    alias = alias or randomString(5)
    wh, _ = creators_wallets[0]

    # filling nym request and getting steward did
    # if role == None, we are adding client
    nym_request, new_did = looper.loop.run_until_complete(
        prepare_nym_request(creators_wallets[0], seed, alias, role, dest,
                            verkey, skipverkey))

    # sending request using 'sdk_' functions
    signed_reqs = sdk_multi_sign_request_objects(
        looper, creators_wallets,
        [sdk_json_to_request_object(json.loads(nym_request))])
    request_couple = sdk_send_signed_requests(sdk_pool_handle, signed_reqs)[0]
    if no_wait:
        return request_couple
    # waitng for replies
    sdk_get_and_check_replies(looper, [request_couple])
Beispiel #2
0
def testLoggingTxnStateForInvalidRequest(looper, txnPoolNodeSet,
                                         sdk_pool_handle, sdk_wallet_client,
                                         logsearch):
    logsPropagate, _ = logsearch(files=['propagator.py'],
                                 funcs=['propagate'],
                                 msgs=['propagating.*request.*from client'])
    logsReject, _ = logsearch(
        files=['ordering_service.py'],
        funcs=['_consume_req_queue_for_pre_prepare'],
        msgs=['encountered exception.*while processing.*will reject'])

    seed = randomString(32)
    wh, _ = sdk_wallet_client

    nym_request, _ = looper.loop.run_until_complete(
        prepare_nym_request(sdk_wallet_client, seed, "name", STEWARD_STRING))

    request_couple = sdk_sign_and_send_prepared_request(
        looper, sdk_wallet_client, sdk_pool_handle, nym_request)

    with pytest.raises(RequestRejectedException) as e:
        sdk_get_and_check_replies(looper, [request_couple])

    assert 'Only Steward is allowed to do these transactions' in e._excinfo[
        1].args[0]
    request = request_couple[0]
    req_id = str(request[f.REQ_ID.nm])
    digest = get_key_from_req(request)
    assert any(digest in record.getMessage() for record in logsPropagate)
    assert any(req_id in record.getMessage() for record in logsReject)
Beispiel #3
0
def testLoggingTxnStateForInvalidRequest(looper, txnPoolNodeSet,
                                         sdk_pool_handle, sdk_wallet_client,
                                         logsearch):
    logsPropagate, _ = logsearch(levels=['INFO'],
                                 files=['propagator.py'],
                                 funcs=['propagate'],
                                 msgs=['propagating.*request.*from client'])

    logsReject, _ = logsearch(
        levels=['WARNING'],
        files=['replica.py'],
        funcs=['processReqDuringBatch'],
        msgs=['encountered exception.*while processing.*will reject'])

    seed = randomString(32)
    wh, _ = sdk_wallet_client

    nym_request, _ = looper.loop.run_until_complete(
        prepare_nym_request(sdk_wallet_client, seed, "name", STEWARD_STRING))

    request_couple = sdk_sign_and_send_prepared_request(
        looper, sdk_wallet_client, sdk_pool_handle, nym_request)

    with pytest.raises(RequestRejectedException) as e:
        sdk_get_and_check_replies(looper, [request_couple])

    assert 'Only Steward is allowed to do these transactions' in e._excinfo[
        1].args[0]

    reqId = str(json.loads(nym_request)['reqId'])
    assert any(reqId in record.getMessage() for record in logsPropagate)
    assert any(reqId in record.getMessage() for record in logsReject)
def testLoggingTxnStateForInvalidRequest(
        looper, txnPoolNodeSet, sdk_pool_handle, sdk_wallet_client, logsearch):
    logsPropagate, _ = logsearch(files=['propagator.py'], funcs=['propagate'],
                                 msgs=['propagating.*request.*from client'])
    logsReject, _ = logsearch(files=['replica.py'], funcs=['consume_req_queue_for_pre_prepare'],
                              msgs=['encountered exception.*while processing.*will reject'])

    seed = randomString(32)
    wh, _ = sdk_wallet_client

    nym_request, _ = looper.loop.run_until_complete(
        prepare_nym_request(sdk_wallet_client, seed,
                            "name", STEWARD_STRING))

    request_couple = sdk_sign_and_send_prepared_request(looper, sdk_wallet_client,
                                                        sdk_pool_handle, nym_request)

    with pytest.raises(RequestRejectedException) as e:
        sdk_get_and_check_replies(looper, [request_couple])

    assert 'Only Steward is allowed to do these transactions' in e._excinfo[1].args[0]
    request = json.loads(nym_request)
    req_id = str(request[f.REQ_ID.nm])
    digest = get_key_from_req(request)
    assert any(digest in record.getMessage() for record in logsPropagate)
    assert any(req_id in record.getMessage() for record in logsReject)
Beispiel #5
0
def test_get_txn_response_as_expected(looper, txnPoolNodeSet, sdk_pool_handle,
                                      sdk_wallet_steward):
    seed = randomString(32)
    wh, _ = sdk_wallet_steward

    # filling nym request and getting steward did
    # if role == None, we are adding client
    nym_request, new_did = looper.loop.run_until_complete(
        prepare_nym_request(sdk_wallet_steward, seed, None, None))

    # sending request using 'sdk_' functions
    request_couple = sdk_sign_and_send_prepared_request(
        looper, sdk_wallet_steward, sdk_pool_handle, nym_request)

    result1 = sdk_get_and_check_replies(looper,
                                        [request_couple])[0][1]['result']
    seqNo = get_seq_no(result1)

    _, steward_did = sdk_wallet_steward
    request = sdk_build_get_txn_request(looper, steward_did, seqNo)

    request_couple = \
        sdk_sign_and_send_prepared_request(looper,
                                           sdk_wallet_steward,
                                           sdk_pool_handle,
                                           request)
    result2 = sdk_get_and_check_replies(looper,
                                        [request_couple])[0][1]['result']

    assert result1['reqSignature'] == result2['data']['reqSignature']
    assert result1['txn'] == result2['data']['txn']
    assert result1['txnMetadata'] == result2['data']['txnMetadata']
    assert result1['rootHash'] == result2['data']['rootHash']
    assert result1['ver'] == result2['data']['ver']
    assert result1['auditPath'] == result2['data']['auditPath']
def test_get_txn_response_as_expected(looper, txnPoolNodeSet,
                                      sdk_pool_handle,
                                      sdk_wallet_steward):
    seed = randomString(32)
    wh, _ = sdk_wallet_steward

    # filling nym request and getting steward did
    # if role == None, we are adding client
    nym_request, new_did = looper.loop.run_until_complete(
        prepare_nym_request(sdk_wallet_steward, seed,
                            None, None))

    # sending request using 'sdk_' functions
    request_couple = sdk_sign_and_send_prepared_request(
        looper, sdk_wallet_steward,
        sdk_pool_handle, nym_request)

    result1 = sdk_get_and_check_replies(looper,
                                        [request_couple])[0][1]['result']
    seqNo = get_seq_no(result1)

    _, steward_did = sdk_wallet_steward
    request = sdk_build_get_txn_request(looper, steward_did, seqNo)

    request_couple = \
        sdk_sign_and_send_prepared_request(looper,
                                           sdk_wallet_steward,
                                           sdk_pool_handle,
                                           request)
    result2 = sdk_get_and_check_replies(looper,
                                        [request_couple])[0][1]['result']
    assert result1 == result2['data']
def two_requests(looper, helpers,
                 nodeSetWithIntegratedTokenPlugin,
                 sdk_pool_handle,
                 fees_set, address_main, mint_tokens, sdk_wallet_steward):
    amount = get_amount_from_token_txn(mint_tokens)
    init_seq_no = 1

    seed = randomString(32)
    alias = randomString(5)
    wh, _ = sdk_wallet_steward
    nym_request, new_did = looper.loop.run_until_complete(
        prepare_nym_request(sdk_wallet_steward, seed,
                            alias, None))
    nym_request = \
        sdk_sign_request_objects(looper, sdk_wallet_steward, [sdk_json_to_request_object(json.loads(nym_request))])[0]
    req_obj = sdk_json_to_request_object(json.loads(nym_request))
    helpers.request.nym = lambda: copy.deepcopy(req_obj)

    req1, req2 = nyms_with_fees(2,
                                helpers,
                                fees_set,
                                address_main,
                                amount,
                                init_seq_no=init_seq_no)

    assert req1.payload_digest == req2.payload_digest
    assert req1.digest != req2.digest
    return req1, req2
Beispiel #8
0
def set_verkey(looper, sdk_pool_handle, sdk_wallet_sender, dest, verkey):
    wh, _ = sdk_wallet_sender
    nym_request, new_did = looper.loop.run_until_complete(
        prepare_nym_request(sdk_wallet_sender, None,
                            None, TRUST_ANCHOR_STRING, dest, verkey, False if verkey else True))
    request_couple = sdk_sign_and_send_prepared_request(looper, sdk_wallet_sender,
                                                        sdk_pool_handle, nym_request)
    sdk_get_and_check_replies(looper, [request_couple])
    return wh, new_did
Beispiel #9
0
def set_verkey(looper, sdk_pool_handle, sdk_wallet_sender, dest, verkey):
    wh, _ = sdk_wallet_sender
    nym_request, new_did = looper.loop.run_until_complete(
        prepare_nym_request(sdk_wallet_sender, None, None, TRUST_ANCHOR_STRING,
                            dest, verkey, False if verkey else True))
    request_couple = sdk_sign_and_send_prepared_request(
        looper, sdk_wallet_sender, sdk_pool_handle, nym_request)
    sdk_get_and_check_replies(looper, [request_couple])
    return wh, new_did
Beispiel #10
0
def nym_request(looper, sdk_wallet_trustee):
    seed = randomString(32)
    alias = randomString(5)
    dest = None
    role = None
    verkey = None
    nym_request, _ = looper.loop.run_until_complete(
        prepare_nym_request(sdk_wallet_trustee, seed,
                            alias, role, dest, verkey, True))
    return json.loads(nym_request)
Beispiel #11
0
 def _build_nym(self, creator_wallet, role_string, did, verkey=None, skipverkey=True):
     seed = randomString(32)
     alias = randomString(5)
     nym_request, new_did = self.looper.loop.run_until_complete(
         prepare_nym_request(creator_wallet,
                             seed,
                             alias,
                             role_string,
                             dest=did,
                             verkey=verkey,
                             skipverkey=skipverkey))
     return sdk_json_to_request_object(json.loads(nym_request))
Beispiel #12
0
def sdk_add_new_nym_without_waiting(looper, sdk_pool_handle, creators_wallet,
                                    alias=None, role=None, seed=None,
                                    dest=None, verkey=None, skipverkey=False):
    seed = seed or randomString(32)
    alias = alias or randomString(5)
    wh, _ = creators_wallet

    nym_request, new_did = looper.loop.run_until_complete(
        prepare_nym_request(creators_wallet, seed,
                            alias, role, dest, verkey, skipverkey))
    sdk_sign_and_send_prepared_request(looper, creators_wallet,
                                       sdk_pool_handle, nym_request)
Beispiel #13
0
def test_requests_collection_debugging(looper,
                                       nodeSet,
                                       sdk_wallet_trustee):
    primary = nodeSet[0]

    seed = randomString(32)
    alias = randomString(5)
    wh, _ = sdk_wallet_trustee
    nym_request, new_did = looper.loop.run_until_complete(
        prepare_nym_request(sdk_wallet_trustee, seed,
                            alias, STEWARD_STRING))

    nym_request = json.loads(nym_request)
    a = sys.getsizeof(primary.requests)

    mas = []
    for _ in range(50000):
        req = sdk_json_to_request_object(nym_request)
        req.reqId = randomString(32)
        mas.append(req)
        primary.requests.add_propagate(req, 'asd')
        primary.requests.mark_as_forwarded(req, 2)
        primary.requests.set_finalised(req)

    b = sys.getsizeof(primary.requests)
    lb = len(primary.requests)

    for req in mas:
        primary.requests.mark_as_executed(req)
        primary.requests.free(req.key)
        primary.requests.free(req.key)

    c = sys.getsizeof(primary.requests)
    lc = len(primary.requests)

    for _ in range(100000):
        req = sdk_json_to_request_object(nym_request)
        req.reqId = randomString(32)
        mas.append(req)
        primary.requests.add_propagate(req, 'asd')
        primary.requests.mark_as_forwarded(req, 2)
        primary.requests.set_finalised(req)

    d = sys.getsizeof(primary.requests)
    ld = len(primary.requests)

    print(a)
    print(b, lb)
    print(c, lc)
    print(d, ld)
def test_forced_request_validation(looper, txnPoolNodeSet, sdk_wallet_client,
                                   sdk_pool_handle, sdk_wallet_steward):
    nym_request, new_did = looper.loop.run_until_complete(
        prepare_nym_request(sdk_wallet_client, randomString(32), None, None))

    request_json = json.loads(nym_request)
    request_json['operation'][FORCE] = True
    node_request = json.dumps(request_json)

    request_couple = sdk_sign_and_send_prepared_request(
        looper, sdk_wallet_client, sdk_pool_handle, node_request)

    with pytest.raises(RequestNackedException):
        sdk_get_and_check_replies(looper, [request_couple])

    sdk_add_new_nym(looper, sdk_pool_handle, sdk_wallet_steward)
Beispiel #15
0
 def _build_nym(self,
                creator_wallet,
                role_string,
                did,
                verkey=None,
                skipverkey=True):
     seed = randomString(32)
     alias = randomString(5)
     nym_request, _ = self.looper.loop.run_until_complete(
         prepare_nym_request(creator_wallet,
                             seed,
                             alias,
                             role_string,
                             dest=did,
                             verkey=verkey,
                             skipverkey=skipverkey))
     return nym_request
def test_txn_with_different_signature_and_idr(looper, txnPoolNodeSet,
                                              sdk_pool_handle,
                                              sdk_wallet_trustee,
                                              sdk_wallet_client):
    # filling nym request and getting steward did
    nym_request, new_did = looper.loop.run_until_complete(
        prepare_nym_request(sdk_wallet_trustee, randomString(32),
                            'newSteward1', STEWARD_STRING))

    # sending request using 'sdk_' functions
    signed_reqs = sdk_multi_sign_request_objects(
        looper, [sdk_wallet_client],
        [sdk_json_to_request_object(json.loads(nym_request))])
    request_couple = sdk_send_signed_requests(sdk_pool_handle, signed_reqs)[0]

    with pytest.raises(RequestNackedException,
                       match="The identifier is not contained in signatures"):
        # waiting for replies
        sdk_get_and_check_replies(looper, [request_couple])
Beispiel #17
0
def testOnlyAStewardCanAddAnotherSteward(looper,
                                         txnPoolNodeSet,
                                         sdk_pool_handle,
                                         sdk_wallet_steward,
                                         sdk_wallet_client):
    sdk_add_new_nym(looper, sdk_pool_handle, sdk_wallet_steward,
                    alias='testSteward' + randomString(3), role=STEWARD_STRING)

    seed = randomString(32)
    wh, _ = sdk_wallet_client

    nym_request, steward_did = looper.loop.run_until_complete(
        prepare_nym_request(sdk_wallet_client, seed,
                            'testSteward2', 'STEWARD'))

    request_couple = sdk_sign_and_send_prepared_request(looper, sdk_wallet_client,
                                                        sdk_pool_handle, nym_request)
    total_timeout = sdk_eval_timeout(1, len(txnPoolNodeSet))
    request_couple = sdk_get_replies(looper, [request_couple], total_timeout)[0]
    with pytest.raises(RequestRejectedException) as e:
        sdk_check_reply(request_couple)
    assert 'Only Steward is allowed to do these transactions' in e._excinfo[1].args[0]
def testLoggingTxnStateWhenCommitFails(
        looper, txnPoolNodeSet, sdk_pool_handle, sdk_wallet_steward, logsearch):
    logsPropagate, _ = logsearch(files=['propagator.py'], funcs=['propagate'],
                                 msgs=['propagating.*request.*from client'])
    logsOrdered, _ = logsearch(files=['replica.py'], funcs=['order_3pc_key'], msgs=['ordered batch request'])
    logsCommitFail, _ = logsearch(files=['node.py'], funcs=['executeBatch'],
                                  msgs=['commit failed for batch request'])

    seed = randomString(32)
    wh, _ = sdk_wallet_steward

    nym_request, _ = looper.loop.run_until_complete(
        prepare_nym_request(sdk_wallet_steward, seed,
                            "name", None))

    sdk_sign_and_send_prepared_request(looper, sdk_wallet_steward,
                                       sdk_pool_handle, nym_request)

    class SomeError(Exception):
        pass

    def commitPatched(node, commitOrig, *args, **kwargs):
        req_handler = node.get_req_handler(ledger_id=DOMAIN_LEDGER_ID)
        req_handler.commit = commitOrig
        raise SomeError(ERORR_MSG)

    excCounter = 0

    def executeBatchPatched(node, executeBatchOrig, *args, **kwargs):
        nonlocal excCounter
        try:
            executeBatchOrig(*args, **kwargs)
        except SomeError:
            excCounter += 1
            node.executeBatch = executeBatchOrig
            pass

    def checkSufficientExceptionsHappend():
        assert excCounter == len(txnPoolNodeSet)
        return

    for node in txnPoolNodeSet:
        req_handler = node.get_req_handler(ledger_id=DOMAIN_LEDGER_ID)
        req_handler.commit = functools.partial(
            commitPatched, node, req_handler.commit
        )
        node.executeBatch = functools.partial(
            executeBatchPatched, node, node.executeBatch
        )

    timeout = waits.expectedTransactionExecutionTime(len(txnPoolNodeSet))
    looper.run(
        eventually(checkSufficientExceptionsHappend,
                   retryWait=1, timeout=timeout))

    request = json.loads(nym_request)
    digest = get_key_from_req(request)
    assert any(digest in record.getMessage() for record in logsPropagate)
    assert any(digest in record.getMessage() for record in logsOrdered)
    assert any(digest in record.getMessage() for record in logsCommitFail)
    assert any(ERORR_MSG in record.getMessage() for record in logsCommitFail)
Beispiel #19
0
def test_successive_batch_do_no_change_state(looper, tconf, nodeSet,
                                             sdk_pool_handle,
                                             sdk_wallet_trustee, monkeypatch):
    """
    Send 2 NYM txns in different batches such that the second batch does not
    change state so that state root remains same, but keep the identifier
    and reqId different. Make sure the first request is not ordered by the
    primary before PRE-PREPARE for the second is sent.
    Also check reject and commit
    :return:
    """

    # Disable view change during this test
    for n in nodeSet:
        n.nodeIbStasher.delay(icDelay())

    # Delay only first PRE-PREPARE
    delay_cm_duration = 10

    def delay_commits(wrappedMsg):
        msg, sender = wrappedMsg
        if isinstance(msg, Commit) and msg.instId == 0:
            return delay_cm_duration

    def check_verkey(i, vk):
        for node in nodeSet:
            data = node.idrCache.getNym(i, isCommitted=True)
            assert data[VERKEY] == vk

    def check_uncommitted(count):
        for node in nodeSet:
            assert len(node.idrCache.un_committed) == count

    for node in nodeSet:
        for rpl in node.replicas:
            monkeypatch.setattr(rpl, '_request_missing_three_phase_messages',
                                lambda *x, **y: None)

    wh, did = sdk_wallet_trustee
    seed = randomString(32)
    (new_did, verkey) = looper.loop.run_until_complete(
        create_and_store_my_did(wh, json.dumps({'seed': seed})))

    sdk_add_new_nym(looper,
                    sdk_pool_handle,
                    sdk_wallet_trustee,
                    dest=new_did,
                    verkey=verkey)

    for node in nodeSet:
        node.nodeIbStasher.delay(delay_commits)

    # Setting the same verkey thrice but in different batches with different
    # request ids
    for _ in range(3):
        verkey = sdk_rotate_verkey(looper, sdk_pool_handle, wh, new_did,
                                   new_did, seed)
        logger.debug('{} rotates his key to {}'.format(new_did, verkey))

    # Number of uncommitted entries is 0
    looper.run(eventually(check_uncommitted, 0))

    check_verkey(new_did, verkey)

    # Setting the verkey to `x`, then `y` and then back to `x` but in different
    # batches with different request ids. The idea is to change
    # state root to `t` then `t'` and then back to `t` and observe that no
    # errors are encountered

    seed = randomString(32)
    (new_client_did, verkey) = looper.loop.run_until_complete(
        create_and_store_my_did(wh, json.dumps({'seed': seed})))
    sdk_add_new_nym(looper,
                    sdk_pool_handle,
                    sdk_wallet_trustee,
                    dest=new_client_did,
                    verkey=verkey)

    x_seed = randomString(32)
    verkey = sdk_rotate_verkey(looper, sdk_pool_handle, wh, new_client_did,
                               new_client_did, x_seed)
    logger.debug('{} rotates his key to {}'.format(new_client_did, verkey))

    y_seed = randomString(32)
    sdk_rotate_verkey(looper, sdk_pool_handle, wh, new_client_did,
                      new_client_did, y_seed)
    logger.debug('{} rotates his key to {}'.format(new_client_did, verkey))

    sdk_rotate_verkey(looper, sdk_pool_handle, wh, new_client_did,
                      new_client_did, x_seed)
    logger.debug('{} rotates his key to {}'.format(new_client_did, verkey))

    # Number of uncommitted entries is 0
    looper.run(eventually(check_uncommitted, 0))

    check_verkey(new_client_did, verkey)
    monkeypatch.undo()

    # Delay COMMITs so that IdrCache can be checked for correct
    # number of entries

    uncommitteds = {}
    methods = {}
    for node in nodeSet:
        cache = node.idrCache  # type: IdrCache
        uncommitteds[cache._name] = []
        # Since the post batch creation handler is registered (added to a list),
        # find it and patch it
        dh = node.get_req_handler(DOMAIN_LEDGER_ID)
        for i, handler in enumerate(dh.post_batch_creation_handlers):
            # Find the cache's post create handler, not hardcoding names of
            # class or functions as they can change with refactoring.
            if handler.__func__.__qualname__ == '{}.{}'.format(
                    cache.__class__.__name__,
                    cache.currentBatchCreated.__name__):
                cre = dh.post_batch_creation_handlers[i]
                break
        com = cache.onBatchCommitted
        methods[cache._name] = (cre, com)

        # Patch methods to record and check roots after commit

        def patched_cre(self, stateRoot):
            uncommitteds[self._name].append(stateRoot)
            return methods[self._name][0](stateRoot)

        def patched_com(self, stateRoot):
            assert uncommitteds[self._name][0] == stateRoot
            rv = methods[self._name][1](stateRoot)
            uncommitteds[self._name] = uncommitteds[self._name][1:]
            return rv

        dh.post_batch_creation_handlers[i] = types.MethodType(
            patched_cre, cache)
        cache.onBatchCommitted = types.MethodType(patched_com, cache)

    # Set verkey of multiple identities
    more = 5
    keys = {}
    reqs = []
    for _ in range(more):
        seed = randomString(32)
        nym_request, new_did = looper.loop.run_until_complete(
            prepare_nym_request(sdk_wallet_trustee, seed, None, None))
        keys[new_did] = json.loads(nym_request)['operation']['verkey']
        reqs.append(
            sdk_sign_and_send_prepared_request(looper, sdk_wallet_trustee,
                                               sdk_pool_handle, nym_request))
        looper.runFor(.01)

    # Correct number of uncommitted entries
    looper.run(eventually(check_uncommitted, more, retryWait=1))

    sdk_get_and_check_replies(looper, reqs)

    # Number of uncommitted entries is 0
    looper.run(eventually(check_uncommitted, 0))

    # The verkeys are correct
    for i, v in keys.items():
        check_verkey(i, v)

    waitNodeDataEquality(looper, nodeSet[0], *nodeSet[1:])

    for _ in range(3):
        seed = randomString(32)
        nym_request, new_did = looper.loop.run_until_complete(
            prepare_nym_request(sdk_wallet_trustee, seed, None, None))
        reqs.append(
            sdk_sign_and_send_prepared_request(looper, sdk_wallet_trustee,
                                               sdk_pool_handle, nym_request))
        looper.runFor(.01)

    # Correct number of uncommitted entries
    looper.run(eventually(check_uncommitted, 3, retryWait=1))

    # Check batch reject
    for node in nodeSet:
        cache = node.idrCache
        initial = cache.un_committed
        cache.batchRejected()
        # After reject, last entry is removed
        assert cache.un_committed == initial[:-1]
        root = cache.un_committed[0][0]
        cache.onBatchCommitted(root)
        # Calling commit with same root results in Assertion error
        with pytest.raises(AssertionError):
            cache.onBatchCommitted(root)
Beispiel #20
0
def testLoggingTxnStateWhenCommitFails(looper, txnPoolNodeSet, sdk_pool_handle,
                                       sdk_wallet_steward, logsearch):
    logsPropagate, _ = logsearch(levels=['INFO'],
                                 files=['propagator.py'],
                                 funcs=['propagate'],
                                 msgs=['propagating.*request.*from client'])

    logsOrdered, _ = logsearch(levels=['INFO'],
                               files=['replica.py'],
                               funcs=['order_3pc_key'],
                               msgs=['ordered batch request'])

    logsCommitFail, _ = logsearch(levels=['WARNING'],
                                  files=['node.py'],
                                  funcs=['executeBatch'],
                                  msgs=['commit failed for batch request'])

    seed = randomString(32)
    wh, _ = sdk_wallet_steward

    nym_request, _ = looper.loop.run_until_complete(
        prepare_nym_request(sdk_wallet_steward, seed, "name", None))

    sdk_sign_and_send_prepared_request(looper, sdk_wallet_steward,
                                       sdk_pool_handle, nym_request)

    class SomeError(Exception):
        pass

    def commitPatched(node, commitOrig, *args, **kwargs):
        req_handler = node.get_req_handler(ledger_id=DOMAIN_LEDGER_ID)
        req_handler.commit = commitOrig
        raise SomeError(ERORR_MSG)

    excCounter = 0

    def executeBatchPatched(node, executeBatchOrig, *args, **kwargs):
        nonlocal excCounter
        try:
            executeBatchOrig(*args, **kwargs)
        except SomeError:
            excCounter += 1
            node.executeBatch = executeBatchOrig
            pass

    def checkSufficientExceptionsHappend():
        assert excCounter == len(txnPoolNodeSet)
        return

    for node in txnPoolNodeSet:
        req_handler = node.get_req_handler(ledger_id=DOMAIN_LEDGER_ID)
        req_handler.commit = functools.partial(commitPatched, node,
                                               req_handler.commit)
        node.executeBatch = functools.partial(executeBatchPatched, node,
                                              node.executeBatch)

    timeout = waits.expectedTransactionExecutionTime(len(txnPoolNodeSet))
    looper.run(
        eventually(checkSufficientExceptionsHappend,
                   retryWait=1,
                   timeout=timeout))

    reqId = str(json.loads(nym_request)['reqId'])
    assert any(reqId in record.getMessage() for record in logsPropagate)
    assert any(reqId in record.getMessage() for record in logsOrdered)
    assert any(reqId in record.getMessage() for record in logsCommitFail)
    assert any(ERORR_MSG in record.getMessage() for record in logsCommitFail)
Beispiel #21
0
def build_nym_request(looper, sdk_wallet):
    return looper.loop.run_until_complete(
        prepare_nym_request(sdk_wallet,
                            named_seed=randomString(32),
                            alias=randomString(5),
                            role=None))[0]