Example #1
0
 def add_txn_to_ledger(txn_time: Optional[int]) -> dict:
     nonlocal node
     req = sdk_random_request_objects(1, CURRENT_PROTOCOL_VERSION, identifier='someidentifier')[0]
     txn = reqToTxn(req)
     node.domainLedger.append_txns_metadata([txn], txn_time=txn_time)
     node.domainLedger.appendTxns([txn])
     return txn
Example #2
0
def create_valid_batch_committed():
    reqs = [
        req.as_dict for req in sdk_random_request_objects(
            10, identifier="1" * 16, protocol_version=CURRENT_PROTOCOL_VERSION)
    ]
    return BatchCommitted(reqs, DOMAIN_LEDGER_ID, get_utc_epoch(),
                          generate_state_root(), generate_state_root(), 1, 2)
Example #3
0
def fake_msg_batch_committed():
    reqs = [
        req.as_dict for req in sdk_random_request_objects(
            10, identifier="1" * 16, protocol_version=CURRENT_PROTOCOL_VERSION)
    ]
    return BatchCommitted(reqs, DOMAIN_LEDGER_ID, 0, 0, 1, get_utc_epoch(),
                          generate_state_root(), generate_state_root(), 1, 10,
                          generate_state_root(), ['Alpha', 'Beta'], 0)
def create_valid_batch_committed():
    reqs = [req.as_dict for req in
            sdk_random_request_objects(10, identifier="1" * 16, protocol_version=CURRENT_PROTOCOL_VERSION)]
    return BatchCommitted(reqs,
                          DOMAIN_LEDGER_ID,
                          get_utc_epoch(),
                          generate_state_root(),
                          generate_state_root(),
                          1,
                          2)
Example #5
0
def test_old_txn_metadata_multisig_digest_fallback(looper, sdk_wallet_client, sdk_wallet_client2):
    # Create signed request and convert to legacy txn
    req_str = json.dumps(sdk_random_request_objects(1, CURRENT_PROTOCOL_VERSION, sdk_wallet_client[1])[0].as_dict)
    req_str = sdk_multisign_request_object(looper, sdk_wallet_client, req_str)
    req_str = sdk_multisign_request_object(looper, sdk_wallet_client2, req_str)
    req = deserialize_req(req_str)
    txn = req_to_legacy_txn(req_str)

    # Check that digests still can be extracted correctly
    assert get_payload_digest(txn) == req.payload_digest
    assert get_digest(txn) == None
Example #6
0
def create_preprepare(replica, sdk_wallet_steward, req_count):
    _, did = sdk_wallet_steward
    reqs = sdk_random_request_objects(
        req_count, identifier=did, protocol_version=CURRENT_PROTOCOL_VERSION)
    for req in reqs:
        replica.requestQueues[DOMAIN_LEDGER_ID].add(req.key)
        replica.requests.add(req)
        replica.requests.set_finalised(req)
    replica.last_accepted_pre_prepare_time = int(time.time())
    pp = replica.create_3pc_batch(DOMAIN_LEDGER_ID)
    return reqs, pp
Example #7
0
def create_preprepare(replica, sdk_wallet_steward, req_count):
    _, did = sdk_wallet_steward
    reqs = sdk_random_request_objects(req_count, identifier=did,
                                      protocol_version=CURRENT_PROTOCOL_VERSION)
    for req in reqs:
        replica.requestQueues[DOMAIN_LEDGER_ID].add(req.key)
        replica.requests.add(req)
        replica.requests.set_finalised(req)
    replica.last_accepted_pre_prepare_time = int(time.time())
    pp = replica.create3PCBatch(DOMAIN_LEDGER_ID)
    return reqs, pp
Example #8
0
def test_request_with_correct_version(looper, txnPoolNodeSet, sdk_pool_handle,
                                      sdk_wallet_client, request_num):
    _, did = sdk_wallet_client
    reqs_obj = sdk_random_request_objects(
        request_num, identifier=did, protocol_version=CURRENT_PROTOCOL_VERSION)
    for req_obj in reqs_obj:
        assert req_obj.protocolVersion == CURRENT_PROTOCOL_VERSION

    signed_reqs = sdk_sign_request_objects(looper, sdk_wallet_client, reqs_obj)
    reqs = sdk_send_signed_requests(sdk_pool_handle, signed_reqs)
    sdk_get_and_check_replies(looper, reqs)
Example #9
0
def test_request_no_protocol_version(looper, txnPoolNodeSet, sdk_pool_handle,
                                     sdk_wallet_client, request_num):
    _, did = sdk_wallet_client
    reqs_obj = sdk_random_request_objects(request_num,
                                          identifier=did,
                                          protocol_version=None)
    for req_obj in reqs_obj:
        assert req_obj.protocolVersion == None

    signed_reqs = sdk_sign_request_objects(looper, sdk_wallet_client, reqs_obj)
    reqs = sdk_send_signed_requests(sdk_pool_handle, signed_reqs)
    sdk_get_and_check_replies(looper, reqs)
def replica_with_requests(replica):
    requests = {ledger_id: sdk_random_request_objects(1, identifier="did",
                                                      protocol_version=CURRENT_PROTOCOL_VERSION)[0]
                for ledger_id in LEDGER_IDS}

    def patched_consume_req_queue_for_pre_prepare(ledger_id, tm, view_no, pp_seq_no):
        reqs = [requests[ledger_id]] if len(replica.requestQueues[ledger_id]) > 0 else []
        return [reqs, [], []]

    replica.consume_req_queue_for_pre_prepare = patched_consume_req_queue_for_pre_prepare

    return replica, requests
Example #11
0
def create_observed_data(seq_no_start=1, seq_no_end=5):
    req_num = seq_no_end - seq_no_start + 1
    reqs = [
        req.as_dict for req in sdk_random_request_objects(
            req_num,
            identifier="1" * 16,
            protocol_version=CURRENT_PROTOCOL_VERSION)
    ]
    msg = BatchCommitted(reqs, DOMAIN_LEDGER_ID, 0, 0, 1, get_utc_epoch(),
                         generate_state_root(), generate_state_root(),
                         seq_no_start, seq_no_end, generate_state_root())
    return ObservedData(BATCH, msg)
def test_request_none_protocol_version(looper, txnPoolNodeSet, sdk_pool_handle,
                                       sdk_wallet_client, request_num):
    _, did = sdk_wallet_client
    req_objs = sdk_random_request_objects(request_num,
                                          identifier=did,
                                          protocol_version=None)
    for req_obj in req_objs:
        assert req_obj.protocolVersion == None

    signed_reqs = sdk_sign_request_objects(looper, sdk_wallet_client, req_objs)
    reqs = sdk_send_signed_requests(sdk_pool_handle, signed_reqs)
    sdk_get_bad_response(looper, reqs, RequestNackedException,
                         'missed fields - protocolVersion. ' + error_msg)
Example #13
0
def test_seq_no_db_signed_request(looper, node, sdk_wallet_client):
    # Create signed request and write it to ledger
    req = sdk_random_request_objects(
        1,
        identifier=sdk_wallet_client[1],
        protocol_version=CURRENT_PROTOCOL_VERSION)[0]
    req = sdk_sign_request_objects(looper, sdk_wallet_client, [req])[0]
    req = deserialize_req(req)
    write_request(node, req)

    # Make sure sending request again will return REPLY
    rep = node.getReplyFromLedgerForRequest(req)
    assert isinstance(rep, Reply)
Example #14
0
def test_request_with_invalid_version(looper, txnPoolNodeSet, sdk_pool_handle,
                                      sdk_wallet_client, request_num):
    _, did = sdk_wallet_client
    reqs_obj = sdk_random_request_objects(request_num,
                                          identifier=did,
                                          protocol_version=-1)
    for req_obj in reqs_obj:
        assert req_obj.protocolVersion == -1

    signed_reqs = sdk_sign_request_objects(looper, sdk_wallet_client, reqs_obj)
    reqs = sdk_send_signed_requests(sdk_pool_handle, signed_reqs)
    sdk_get_bad_response(looper, reqs, RequestNackedException,
                         'Unknown protocol version value. ' + error_msg)
def test_request_with_invalid_version(looper, txnPoolNodeSet, sdk_pool_handle,
                                      sdk_wallet_client, request_num):
    _, did = sdk_wallet_client
    reqs_obj = sdk_random_request_objects(request_num,
                                          identifier=did,
                                          protocol_version=-1)
    for req_obj in reqs_obj:
        assert req_obj.protocolVersion == -1

    signed_reqs = sdk_sign_request_objects(looper, sdk_wallet_client, reqs_obj)
    reqs = sdk_send_signed_requests(sdk_pool_handle, signed_reqs)
    sdk_get_bad_response(looper, reqs, CommonSdkIOException,
                         'Got an error with code 113')
def test_second_digest_is_written(
        looper, txnPoolNodeSet, sdk_pool_handle, sdk_wallet_stewards):
    req = json.dumps(sdk_random_request_objects(1, CURRENT_PROTOCOL_VERSION, sdk_wallet_stewards[0][1])[0].as_dict)
    req = sdk_multisign_request_object(looper, sdk_wallet_stewards[0], req)
    req = sdk_multisign_request_object(looper, sdk_wallet_stewards[1], req)
    sdk_get_and_check_replies(looper, sdk_send_signed_requests(sdk_pool_handle, [req]))

    req = Request(**json.loads(req))

    ledger_id, _ = txnPoolNodeSet[0].seqNoDB.get_by_payload_digest(req.payload_digest)
    assert ledger_id == DOMAIN_LEDGER_ID

    payload_digest = txnPoolNodeSet[0].seqNoDB.get_by_full_digest(req.digest)
    assert payload_digest == req.payload_digest
Example #17
0
def test_request_with_invalid_version(looper, txnPoolNodeSet, sdk_pool_handle,
                                      sdk_wallet_client, request_num):
    _, did = sdk_wallet_client
    reqs_obj = sdk_random_request_objects(request_num,
                                          identifier=did,
                                          protocol_version=-1)
    for req_obj in reqs_obj:
        assert req_obj.protocolVersion == -1

    signed_reqs = sdk_sign_request_objects(looper, sdk_wallet_client, reqs_obj)
    reqs = sdk_send_signed_requests(sdk_pool_handle, signed_reqs)
    with pytest.raises(RequestNackedException) as e:
        sdk_get_and_check_replies(looper, reqs)
    assert 'Unknown protocol version value -1' in e._excinfo[1].args[0]
def test_request_with_correct_version(looper,
                                      txnPoolNodeSet,
                                      sdk_pool_handle,
                                      sdk_wallet_client,
                                      request_num):
    _, did = sdk_wallet_client
    reqs_obj = sdk_random_request_objects(request_num, identifier=did,
                                          protocol_version=CURRENT_PROTOCOL_VERSION)
    for req_obj in reqs_obj:
        assert req_obj.protocolVersion == CURRENT_PROTOCOL_VERSION

    signed_reqs = sdk_sign_request_objects(looper, sdk_wallet_client, reqs_obj)
    reqs = sdk_send_signed_requests(sdk_pool_handle, signed_reqs)
    sdk_get_and_check_replies(looper, reqs)
def test_request_none_protocol_version(looper, txnPoolNodeSet,
                                       sdk_pool_handle,
                                       sdk_wallet_client,
                                       request_num):
    _, did = sdk_wallet_client
    req_objs = sdk_random_request_objects(request_num, identifier=did,
                                          protocol_version=None)
    for req_obj in req_objs:
        assert req_obj.protocolVersion == None

    signed_reqs = sdk_sign_request_objects(looper, sdk_wallet_client, req_objs)
    reqs = sdk_send_signed_requests(sdk_pool_handle, signed_reqs)
    sdk_get_bad_response(looper, reqs, RequestNackedException,
                         'missed fields - protocolVersion. ' + error_msg)
def test_request_with_invalid_version(looper,
                                      txnPoolNodeSet,
                                      sdk_pool_handle,
                                      sdk_wallet_client,
                                      request_num):
    _, did = sdk_wallet_client
    reqs_obj = sdk_random_request_objects(request_num, identifier=did,
                                          protocol_version=-1)
    for req_obj in reqs_obj:
        assert req_obj.protocolVersion == -1

    signed_reqs = sdk_sign_request_objects(looper, sdk_wallet_client, reqs_obj)
    reqs = sdk_send_signed_requests(sdk_pool_handle, signed_reqs)
    sdk_get_bad_response(looper, reqs, RequestNackedException,
                         'Unknown protocol version value. ' + error_msg)
def test_request_with_outdated_version(looper, txnPoolNodeSet, sdk_pool_handle,
                                       sdk_wallet_client, request_num):
    _, did = sdk_wallet_client
    reqs_obj = sdk_random_request_objects(
        request_num,
        identifier=did,
        protocol_version=CURRENT_PROTOCOL_VERSION - 1)
    for req_obj in reqs_obj:
        assert req_obj.protocolVersion == CURRENT_PROTOCOL_VERSION - 1

    signed_reqs = sdk_sign_request_objects(looper, sdk_wallet_client, reqs_obj)
    reqs = sdk_send_signed_requests(sdk_pool_handle, signed_reqs)
    sdk_get_bad_response(
        looper, reqs, RequestNackedException,
        'differs from current protocol version. '.format(
            CURRENT_PROTOCOL_VERSION) + error_msg)
def test_request_with_outdated_version(looper,
                                       txnPoolNodeSet,
                                       sdk_pool_handle,
                                       sdk_wallet_client,
                                       request_num):
    _, did = sdk_wallet_client
    reqs_obj = sdk_random_request_objects(request_num, identifier=did,
                                          protocol_version=CURRENT_PROTOCOL_VERSION - 1)
    for req_obj in reqs_obj:
        assert req_obj.protocolVersion == CURRENT_PROTOCOL_VERSION - 1

    signed_reqs = sdk_sign_request_objects(looper, sdk_wallet_client, reqs_obj)
    reqs = sdk_send_signed_requests(sdk_pool_handle, signed_reqs)
    sdk_get_bad_response(looper, reqs, RequestNackedException,
                         'differs from current protocol version. '
                         .format(CURRENT_PROTOCOL_VERSION) + error_msg)
Example #23
0
def test_request_with_incorrect_multisig_signatures(looper, sdk_pool_handle, sdk_wallet_client, sdk_wallet_client2):
    req = sdk_random_request_objects(1, identifier=sdk_wallet_client[1], protocol_version=CURRENT_PROTOCOL_VERSION)[0]

    req = sdk_multisign_request_object(looper, sdk_wallet_client, json.dumps(req.as_dict))
    req = deserialize_req(req)
    req.signatures[req.identifier] = 'garbage'

    multisig_req = sdk_multisign_request_object(looper, sdk_wallet_client2, json.dumps(req.as_dict))

    rep1 = sdk_send_signed_requests(sdk_pool_handle, [multisig_req])

    invalid_signatures = 'did={}, signature={}'.format(req.identifier, req.signatures[req.identifier])
    expected_error_message = 'Reason: client request invalid: {}'.\
        format(InsufficientCorrectSignatures.reason.format(2, 1, 1, invalid_signatures))

    with pytest.raises(RequestNackedException, match=expected_error_message):
        sdk_get_and_check_replies(looper, rep1)
Example #24
0
def test_seq_no_db_multisigned_request(looper, node, sdk_wallet_client,
                                       sdk_wallet_client2):
    # Create signed request and write it to ledger
    req = sdk_random_request_objects(
        1,
        identifier=sdk_wallet_client[1],
        protocol_version=CURRENT_PROTOCOL_VERSION)[0]
    req = sdk_multisign_request_object(looper, sdk_wallet_client,
                                       json.dumps(req.as_dict))
    req = deserialize_req(req)
    write_request(node, req)

    # Make sure sending request again will return REPLY
    rep = node.getReplyFromLedgerForRequest(req)
    assert isinstance(rep, Reply)

    # Make sure sending request with additional signature will return NACK
    multisig_req = sdk_multisign_request_object(looper, sdk_wallet_client2,
                                                json.dumps(req.as_dict))
    multisig_req = deserialize_req(multisig_req)
    rep = node.getReplyFromLedgerForRequest(multisig_req)
    assert isinstance(rep, RequestNack)
Example #25
0
def test_create_3pc_batch(replica):
    root_hash = [
        "EuDgqga9DNr4bjH57Rdq6BRtvCN1PV9UX5Mpnm9gbMAZ",
        "QuDgqga9DNr4bjH57Rdq6BRtvCN1PV9UX5Mpnm9gbMAZ"
    ]
    requests = sdk_random_request_objects(
        2, identifier="did", protocol_version=CURRENT_PROTOCOL_VERSION)
    ledger_id = POOL_LEDGER_ID
    replica.consume_req_queue_for_pre_prepare = \
        lambda ledger, tm, view_no, pp_seq_no: (requests, [], [])
    replica.stateRootHash = lambda ledger, to_str=False: root_hash[ledger]

    pre_prepare_msg = replica.create_3pc_batch(ledger_id)

    assert pre_prepare_msg.poolStateRootHash == root_hash[POOL_LEDGER_ID]
    assert pre_prepare_msg.stateRootHash == root_hash[ledger_id]
    assert pre_prepare_msg.ppSeqNo == 1
    assert pre_prepare_msg.ledgerId == ledger_id
    assert pre_prepare_msg.viewNo == replica.viewNo
    assert pre_prepare_msg.instId == replica.instId
    assert pre_prepare_msg.reqIdr == [req.digest for req in requests]
    assert f.BLS_MULTI_SIG.nm not in pre_prepare_msg
Example #26
0
def testDoNotBlacklistClient(looper, txnPoolNodeSet, sdk_wallet_client,
                             sdk_pool_handle, poolTxnClientNames):
    """
    Client should be not be blacklisted by node on sending an unsigned request
    """
    client_name = poolTxnClientNames[0]
    _, did = sdk_wallet_client
    # No node should blacklist the client
    req_obj = sdk_random_request_objects(
        1, identifier=did, protocol_version=CURRENT_PROTOCOL_VERSION)[0]

    reqs = sdk_send_signed_requests(sdk_pool_handle,
                                    [json.dumps(req_obj.as_dict)])

    with pytest.raises(RequestNackedException, match='MissingSignature'):
        sdk_get_and_check_replies(looper, reqs)

    def chk():
        for node in txnPoolNodeSet:
            assert not node.isClientBlacklisted(client_name)

    timeout = waits.expectedClientToPoolConnectionTimeout(len(txnPoolNodeSet))
    looper.run(eventually(chk, retryWait=1, timeout=timeout))
def testDoNotBlacklistClient(looper, txnPoolNodeSet,
                             sdk_wallet_client, sdk_pool_handle,
                             poolTxnClientNames):
    """
    Client should be not be blacklisted by node on sending an unsigned request
    """
    client_name = poolTxnClientNames[0]
    _, did = sdk_wallet_client
    # No node should blacklist the client
    req_obj = sdk_random_request_objects(1, identifier=did,
                                         protocol_version=CURRENT_PROTOCOL_VERSION)[0]

    reqs = sdk_send_signed_requests(sdk_pool_handle, [json.dumps(req_obj.as_dict)])

    with pytest.raises(RequestNackedException) as e:
        sdk_get_and_check_replies(looper, reqs)
    assert 'MissingSignature' in e._excinfo[1].args[0]

    def chk():
        for node in txnPoolNodeSet:
            assert not node.isClientBlacklisted(client_name)

    timeout = waits.expectedClientToPoolConnectionTimeout(len(txnPoolNodeSet))
    looper.run(eventually(chk, retryWait=1, timeout=timeout))
Example #28
0
def create_requests(count):
    return sdk_random_request_objects(count, CURRENT_PROTOCOL_VERSION)
Example #29
0
def fake_requests():
    return sdk_random_request_objects(
        10, identifier="fake_did", protocol_version=CURRENT_PROTOCOL_VERSION)