Beispiel #1
0
def test_update_state(handler_and_request):
    handler, request = handler_and_request
    seq_no = 1
    txn_time = 1560241033
    txn = reqToTxn(request)
    append_txn_metadata(txn, seq_no, txn_time)
    op = request.operation

    handler.update_state(txn, None, context_request)

    value = {
        'id': op[RS_ID],
        'rsType': op[RS_TYPE],
        'rsName': op[RS_NAME],
        'rsVersion': op[RS_VERSION],
        'content': op[RS_CONTENT],
        'from': request.identifier,
        'endorser': request.endorser,
        'ver': op[OP_VER],
    }
    primary_key = op[RS_ID]
    secondary_key = "{RS_TYPE}:{RS_NAME}:{RS_VERSION}".format(
        RS_TYPE=op['rsType'], RS_NAME=op['rsName'],
        RS_VERSION=op['rsVersion']).encode()

    value_from_state = handler.get_from_state(primary_key)
    assert SortedDict(value_from_state[0]) == SortedDict(value)
    assert value_from_state[1] == seq_no
    assert value_from_state[2] == txn_time
    assert handler.state.get(secondary_key,
                             isCommitted=False) == op[RS_ID].encode()
Beispiel #2
0
 def node_txn(steward_nym,
              node_name,
              nym,
              ip,
              node_port,
              client_port,
              client_ip=None,
              blskey=None,
              services=None,
              txn_id=None,
              seq_no=None):
     txn = init_empty_txn(txn_type=NODE)
     txn = append_payload_metadata(txn, frm=steward_nym)
     txn_data = {
         TARGET_NYM: nym,
         DATA: {
             CLIENT_IP: client_ip or ip,
             ALIAS: node_name,
             CLIENT_PORT: client_port,
             NODE_IP: ip,
             NODE_PORT: node_port,
             SERVICES: services or [VALIDATOR],
             BLS_KEY: blskey
         },
     }
     txn = set_payload_data(txn, txn_data)
     txn_id = txn_id or sha256(node_name.encode()).hexdigest()
     txn = append_txn_metadata(txn, txn_id=txn_id)
     if seq_no:
         txn = append_txn_metadata(txn, seq_no=seq_no)
     return txn
Beispiel #3
0
def test_append_txn_metadata():
    txn = init_empty_txn(txn_type=NODE, protocol_version="3")
    set_payload_data(txn, {"somekey": "somevalue"})
    append_payload_metadata(txn, frm="DID1", req_id=12345)
    append_txn_metadata(txn, seq_no=144, txn_time=12345678, txn_id="dddd")
    expected = SortedDict({
        "reqSignature": {},
        "txn": {
            "data": {
                "somekey": "somevalue"
            },
            "metadata": {
                "from": "DID1",
                "reqId": 12345,
            },
            "protocolVersion": "3",
            "type": NODE,
        },
        "txnMetadata": {
            "seqNo": 144,
            "txnId": "dddd",
            "txnTime": 12345678,
        },
        "ver": "1"
    })
    assert SortedDict(expected) == SortedDict(txn)
Beispiel #4
0
 def _append_seq_no(self, txns, start_seq_no):
     # TODO: Fix name `start_seq_no`, it is misleading. The seq no start from `start_seq_no`+1
     seq_no = start_seq_no
     for txn in txns:
         seq_no += 1
         append_txn_metadata(txn, seq_no=seq_no)
     return txns
Beispiel #5
0
 def append_txns_metadata(self, txns: List, txn_time=None):
     if txn_time is not None:
         # All transactions have the same time since all these
         # transactions belong to the same 3PC batch
         for txn in txns:
             append_txn_metadata(txn, txn_time=txn_time)
     self._append_seq_no(txns, self.seqNo + len(self.uncommittedTxns))
Beispiel #6
0
 def _append_seq_no(self, txns, start_seq_no):
     # TODO: Fix name `start_seq_no`, it is misleading. The seq no start from `start_seq_no`+1
     seq_no = start_seq_no
     for txn in txns:
         seq_no += 1
         append_txn_metadata(txn, seq_no=seq_no)
     return txns
Beispiel #7
0
 def append_txns_metadata(self, txns: List, txn_time=None):
     if txn_time is not None:
         # All transactions have the same time since all these
         # transactions belong to the same 3PC batch
         for txn in txns:
             append_txn_metadata(txn, txn_time=txn_time)
     self._append_seq_no(txns, self.seqNo + len(self.uncommittedTxns))
def test_update_state(txn_author_agreement_handler, taa_request):
    seq_no = 1
    txn_time = 1560241033
    txn_id = "id"
    txn = reqToTxn(taa_request)
    payload = get_payload_data(txn)
    text = payload[TXN_AUTHOR_AGREEMENT_TEXT]
    version = payload[TXN_AUTHOR_AGREEMENT_VERSION]
    digest = StaticTAAHelper.taa_digest(text, version)
    append_txn_metadata(txn, seq_no, txn_time, txn_id)

    state_value = {
        TXN_AUTHOR_AGREEMENT_TEXT: text,
        TXN_AUTHOR_AGREEMENT_VERSION: version
    }

    txn_author_agreement_handler.update_state(txn, None, taa_request)

    assert txn_author_agreement_handler.get_from_state(
        StaticTAAHelper.state_path_taa_digest(digest)) == (state_value, seq_no,
                                                           txn_time)
    assert txn_author_agreement_handler.state.get(
        StaticTAAHelper.state_path_taa_latest()) == digest
    assert txn_author_agreement_handler.state.get(
        StaticTAAHelper.state_path_taa_version(version)) == digest
Beispiel #9
0
def test_update_state(revoc_reg_entry_handler, revoc_reg_entry_request,
                      revoc_reg_def_handler, revoc_reg_def_request):
    # create revoc_req_def
    seq_no = 1
    txn_time = 1560241030
    revoc_reg_def_request.operation[ISSUANCE_TYPE] = ISSUANCE_BY_DEFAULT
    txn = reqToTxn(revoc_reg_def_request)
    append_txn_metadata(txn, seq_no, txn_time)
    path = RevocRegDefHandler.prepare_revoc_def_for_state(txn, path_only=True)
    revoc_reg_def_handler.update_state(txn, None, revoc_reg_def_request)

    # create revoc_req_entry
    seq_no = 2
    txn_time = 1560241033
    revoc_reg_entry_request.operation[REVOC_REG_DEF_ID] = path.decode()
    txn = reqToTxn(revoc_reg_entry_request)
    append_txn_metadata(txn, seq_no, txn_time)
    revoc_reg_entry_handler.update_state(txn, None, revoc_reg_entry_request)

    # check state for revoc_reg_entry
    txn_data = get_payload_data(txn)
    txn_data[f.SEQ_NO.nm] = seq_no
    txn_data[TXN_TIME] = txn_time
    assert revoc_reg_entry_handler.get_from_state(
        RevocRegEntryHandler.prepare_revoc_reg_entry_for_state(
            txn, path_only=True)) == (txn_data, seq_no, txn_time)
    # check state for revoc_reg_entry
    txn_data[VALUE] = {ACCUM: txn_data[VALUE][ACCUM]}
    path, _ = RevocRegEntryHandler.prepare_revoc_reg_entry_accum_for_state(txn)
    assert revoc_reg_entry_handler.get_from_state(path) == (txn_data, seq_no,
                                                            txn_time)
Beispiel #10
0
    def nym_txn(nym,
                name=None,
                verkey=None,
                role=None,
                creator=None,
                txn_id=None,
                seq_no=None):
        txn = init_empty_txn(NYM)

        txn_data = {
            TARGET_NYM: nym,
        }
        if verkey is not None:
            txn_data[VERKEY] = verkey
        if role is not None:
            txn_data[ROLE] = role
        if name is not None:
            txn_data[ALIAS] = name
        set_payload_data(txn, txn_data)

        txn = append_payload_metadata(txn, frm=creator)
        if txn_id:
            txn = append_txn_metadata(txn, txn_id=txn_id)
        if seq_no:
            txn = append_txn_metadata(txn, seq_no=seq_no)

        return txn
Beispiel #11
0
 def node_txn(steward_nym, node_name, nym, ip, node_port, client_port,
              client_ip=None, blskey=None, services=None, txn_id=None,
              seq_no=None,
              protocol_version=CURRENT_PROTOCOL_VERSION, bls_key_proof=None):
     txn = init_empty_txn(txn_type=NODE, protocol_version=protocol_version)
     txn = append_payload_metadata(txn, frm=steward_nym)
     txn_data = {
         TARGET_NYM: nym,
         DATA: {
             CLIENT_IP: client_ip or ip,
             ALIAS: node_name,
             CLIENT_PORT: client_port,
             NODE_IP: ip,
             NODE_PORT: node_port,
             SERVICES: services or [VALIDATOR],
             BLS_KEY: blskey,
             BLS_KEY_PROOF: bls_key_proof
         },
     }
     txn = set_payload_data(txn, txn_data)
     txn_id = txn_id or sha256(node_name.encode()).hexdigest()
     txn = append_txn_metadata(txn, txn_id=txn_id)
     if seq_no:
         txn = append_txn_metadata(txn, seq_no=seq_no)
     return txn
Beispiel #12
0
def make_rich_schema_object_exist(handler, request, commit=False):
    seq_no = 1
    txn_time = 1560241033
    txn = reqToTxn(request)
    append_txn_metadata(txn, seq_no, txn_time)
    handler.update_state(txn, None, context_request)
    if commit:
        handler.state.commit()
Beispiel #13
0
def test_update_state(revoc_reg_entry_handler, revoc_reg_entry_request):
    seq_no = 1
    txn_time = 1560241033
    txn = reqToTxn(revoc_reg_entry_request)
    append_txn_metadata(txn, seq_no, txn_time)
    with pytest.raises(InvalidClientRequest,
                       match="There is no any REVOC_REG_DEF by path"):
        revoc_reg_entry_handler.update_state(txn, None,
                                             revoc_reg_entry_request)
Beispiel #14
0
def test_update_state(schema_request, schema_handler):
    seq_no = 1
    txn_time = 1560241033
    txn = reqToTxn(schema_request)
    append_txn_metadata(txn, seq_no, txn_time)
    path, value_bytes = SchemaHandler.prepare_schema_for_state(txn)
    value = {SCHEMA_ATTR_NAMES: get_txn_schema_attr_names(txn)}

    schema_handler.update_state(txn, None, schema_request)
    assert schema_handler.get_from_state(path) == (value, seq_no, txn_time)
Beispiel #15
0
def test_update_state(context_request, context_handler):
    seq_no = 1
    txn_time = 1560241033
    txn = reqToTxn(context_request)
    append_txn_metadata(txn, seq_no, txn_time)
    path, value_bytes = ContextHandler.prepare_context_for_state(txn)
    value = {META: get_txn_context_meta(txn), DATA: get_txn_context_data(txn)}

    context_handler.update_state(txn, None, context_request)
    assert context_handler.get_from_state(path) == (value, seq_no, txn_time)
Beispiel #16
0
def test_update_state(revoc_reg_def_handler, revoc_reg_def_request):
    seq_no = 1
    txn_time = 1560241033
    txn = reqToTxn(revoc_reg_def_request)
    append_txn_metadata(txn, seq_no, txn_time)
    value = get_payload_data(txn)
    path = RevocRegDefHandler.prepare_revoc_def_for_state(txn, path_only=True)

    revoc_reg_def_handler.update_state(txn, None, revoc_reg_def_request)
    assert revoc_reg_def_handler.get_from_state(path) == (value, seq_no,
                                                          txn_time)
Beispiel #17
0
def test_update_state(attrib_handler, attrib_request):
    seq_no = 1
    txn_time = 1560241033
    txn_id = "id"
    txn = reqToTxn(attrib_request)
    append_txn_metadata(txn, seq_no, txn_time, txn_id)
    attr_type, path, value, hashed_value, value_bytes = AttributeHandler.prepare_attr_for_state(
        txn)

    attrib_handler.update_state(txn, None, attrib_request)
    assert attrib_handler.get_from_state(path) == (hashed_value, seq_no,
                                                   txn_time)
Beispiel #18
0
def test_update_state_with_incorrect_schema_seq_no(
        claim_def_request, claim_def_handler: ClaimDefHandler, schema_handler,
        schema_request):
    claim_def_seq_no = 1
    claim_def_txn_time = 1560241033
    claim_def_txn = reqToTxn(claim_def_request)
    append_txn_metadata(claim_def_txn, claim_def_seq_no, claim_def_txn_time)
    with pytest.raises(
            ValueError,
            match="'{}' field is absent, "
            "but it must contain schema seq no".format(CLAIM_DEF_SCHEMA_REF)):
        claim_def_handler.update_state(claim_def_txn, None, claim_def_request)
Beispiel #19
0
def test_update_state(node_handler, node_request):
    seq_no = 1
    txn_time = 1560241033
    txn_id = "id"
    txn = reqToTxn(node_request)
    append_txn_metadata(txn, seq_no, txn_time, txn_id)
    expected_data = node_request.operation[DATA]
    expected_data.update({IDENTIFIER: node_request.identifier})

    node_handler.update_state(txn, None, node_request)
    tmp = node_handler.get_from_state(node_handler.gen_state_key(txn))
    assert set(tmp.items()) == set(expected_data.items())
def test_update_state(rs_schema_request, rs_schema_handler):
    seq_no = 1
    txn_time = 1560241033
    txn = reqToTxn(rs_schema_request)
    append_txn_metadata(txn, seq_no, txn_time)
    path, value_bytes = RsSchemaHandler.prepare_state(txn)
    payload = txn[TXN_PAYLOAD][RS_DATA]
    meta, data = payload[RS_META], payload[RS_DATA]
    value = {
        RS_META: meta,
        RS_DATA: data
    }
    rs_schema_handler.update_state(txn, None, rs_schema_request)
    assert rs_schema_handler.get_from_state(path) == (value, seq_no, txn_time)
def test_update_state(txn_author_agreement_disable_handler,
                      taa_disable_request, txn_author_agreement_handler, tconf,
                      domain_state, taa_pp_time):
    # create TAAs
    taa_txns = []
    taa_digests = []
    taa_state_datas = []
    for _ in list(range(5)):
        txn, digest, state_data = create_taa_txn(
            taa_request(tconf, domain_state, taa_pp_time), taa_pp_time)
        taa_txns.append(txn)
        taa_digests.append(digest)
        taa_state_datas.append(state_data)
    assert taa_txns

    # create a disable txn
    disable_seq_no = 1
    disable_txn_time = get_utc_epoch()
    taa_disable_txn = reqToTxn(taa_disable_request)
    append_txn_metadata(taa_disable_txn, disable_seq_no, disable_txn_time)

    # set a TAAs
    for index, taa_txn in enumerate(taa_txns):
        txn_author_agreement_handler.update_state(taa_txn, None, None)

        check_taa_in_state(
            handler=txn_author_agreement_handler,
            digest=taa_digests[index],
            version=taa_state_datas[index][0][TXN_AUTHOR_AGREEMENT_VERSION],
            state_data=taa_state_datas[index])
        assert txn_author_agreement_disable_handler.state.get(
            StaticTAAHelper.state_path_taa_latest(),
            isCommitted=False) == taa_digests[index].encode()

    # disable TAAs
    txn_author_agreement_disable_handler.update_state(taa_disable_txn, None,
                                                      None)

    assert txn_author_agreement_disable_handler.state.get(
        StaticTAAHelper.state_path_taa_latest(), isCommitted=False) is None

    # set a TAAs
    for index, state_data in enumerate(taa_state_datas):
        state_value = state_data[0]
        state_value[TXN_AUTHOR_AGREEMENT_RETIREMENT_TS] = disable_txn_time
        check_taa_in_state(handler=txn_author_agreement_handler,
                           digest=taa_digests[index],
                           version=state_value[TXN_AUTHOR_AGREEMENT_VERSION],
                           state_data=(state_data[0], disable_seq_no,
                                       disable_txn_time))
Beispiel #22
0
def test_update_state(nym_request: Request, nym_handler: NymHandler):
    seq_no = 1
    txn_time = 1560241033
    nym = nym_request.operation.get(TARGET_NYM)
    txn = reqToTxn(nym_request)
    append_txn_metadata(txn, seq_no, txn_time)
    path = nym_to_state_key(nym)

    result = nym_handler.update_state(txn, None, nym_request)
    state_value = nym_handler.get_from_state(path)
    assert state_value == result
    assert state_value[IDENTIFIER] == nym_request.identifier
    assert state_value[F.seqNo.name] == seq_no
    assert state_value[ROLE] == nym_request.operation.get(ROLE)
    assert state_value[VERKEY] == nym_request.operation.get(VERKEY)
def _add_txns_to_ledger(node, looper, sdk_wallet_client, num_txns_in_reply, reply_count):
    '''
    Add txn_count transactions to node's ledger and return
    ConsistencyProof for all new transactions and list of CatchupReplies
    :return: ConsistencyProof, list of CatchupReplies
    '''
    txn_count = num_txns_in_reply * reply_count
    ledger_manager = node.ledgerManager
    ledger = ledger_manager.ledgerRegistry[ledger_id].ledger
    ledger_info = ledger_manager.getLedgerInfoByType(ledger_id)
    reqs = sdk_signed_random_requests(looper, sdk_wallet_client, txn_count)
    # add transactions to ledger
    for req in reqs:
        txn = append_txn_metadata(reqToTxn(req), txn_time=12345678)
        ledger_manager._add_txn(
            ledger_id, ledger, ledger_info, txn)
    # generate CatchupReps
    replies = []
    for i in range(ledger.seqNo - txn_count + 1, ledger.seqNo + 1, num_txns_in_reply):
        start = i
        end = i + num_txns_in_reply - 1
        cons_proof = ledger_manager._make_consistency_proof(ledger, end, ledger.size)
        txns = {}
        for seq_no, txn in ledger.getAllTxn(start, end):
            txns[str(seq_no)] = ledger_manager.owner.update_txn_with_extra_data(txn)
        replies.append(CatchupRep(ledger_id,
                                  SortedDict(txns),
                                  cons_proof))
    return ledger_manager._buildConsistencyProof(ledger_id,
                                                 ledger.seqNo - txn_count,
                                                 ledger.seqNo), replies
    def apply_request(self, request: Request, batch_ts, prev_result):

        txn = self._req_to_txn(request)
        txn = append_txn_metadata(txn,
                                  seq_no=prev_result.get(f.SEQ_NO.nm),
                                  txn_time=prev_result.get(TXN_TIME))
        return self.update_state(txn, prev_result, request)
Beispiel #25
0
def test_no_state_proofs_if_protocol_version_less(request_handler):
    nym = 'Gw6pDLhcBcoQesN72qfotTgFa7cbuqZpkX3Xo6pLhPhv'
    role = "2"
    verkey = "~7TYfekw4GUagBnBVCqPjiC"
    seq_no = 0
    txn_time = int(time.time())
    # Adding nym
    data = {
        f.IDENTIFIER.nm: nym,
        ROLE: role,
        VERKEY: verkey,
        f.SEQ_NO.nm: seq_no,
        TXN_TIME: txn_time,
    }
    txn = append_txn_metadata(reqToTxn(Request(operation=data)),
                              seq_no=seq_no,
                              txn_time=txn_time)
    txn = append_payload_metadata(txn, frm=nym)
    request_handler.updateNym(nym, txn)
    request_handler.state.commit()
    multi_sig = save_multi_sig(request_handler)

    # Getting nym
    request = Request(operation={TARGET_NYM: nym}, signatures={})
    result = request_handler.handleGetNymReq(request)
    assert STATE_PROOF not in result
def test_get_delta_with_other_reg_def_in_state(looper,
                                               create_node_and_not_start,
                                               reg_entry_with_other_reg_id,
                                               build_txn_for_revoc_def_entry_by_default,
                                               build_get_revoc_reg_delta):
    entry_second_id = build_txn_for_revoc_def_entry_by_default
    delta_req = build_get_revoc_reg_delta
    node = create_node_and_not_start
    # need for different txnTime
    looper.runFor(2)
    req_handler = node.getDomainReqHandler()
    txn = append_txn_metadata(reqToTxn(entry_second_id),
                              txn_time=SECOND_TS_ID,
                              seq_no=node.domainLedger.seqNo + 1)
    req_handler._addRevocRegEntry(txn)
    req_handler.ts_store.set(get_txn_time(txn), req_handler.state.headHash)

    # timestamp beetween FIRST_ID_TS and SECOND_ID_TS
    delta_req['operation'][FROM] = FIRST_ID_TS + 10
    path_to_reg_entry = domain.make_state_path_for_revoc_reg_entry(
        revoc_reg_def_id=entry_second_id['operation'][REVOC_REG_DEF_ID])
    reg_entry = req_handler._get_reg_entry_by_timestamp(
        delta_req['operation'][FROM],
        path_to_reg_entry)
    # we found root_hash in txRevoc storage but there is not corresponded reg_entry by path
    assert reg_entry.root_hash is not None
    assert reg_entry.value is None

    path_to_reg_entry_accum = domain.make_state_path_for_revoc_reg_entry(
        revoc_reg_def_id=entry_second_id['operation'][REVOC_REG_DEF_ID])
    reg_entry_accum = req_handler._get_reg_entry_accum_by_timestamp(
        delta_req['operation'][FROM],
        path_to_reg_entry_accum)
    assert reg_entry_accum.root_hash is not None
    assert reg_entry_accum.value is None
Beispiel #27
0
def test_get_delta_with_other_reg_def_in_state(
        looper, create_node_and_not_start, reg_entry_with_other_reg_id,
        build_txn_for_revoc_def_entry_by_default, build_get_revoc_reg_delta):
    entry_second_id = build_txn_for_revoc_def_entry_by_default
    delta_req = build_get_revoc_reg_delta
    node = create_node_and_not_start
    # need for different txnTime
    looper.runFor(2)
    req_handler = node.getDomainReqHandler()
    txn = append_txn_metadata(reqToTxn(entry_second_id),
                              txn_time=SECOND_TS_ID,
                              seq_no=node.domainLedger.seqNo + 1)
    req_handler._addRevocRegEntry(txn)
    req_handler.ts_store.set(get_txn_time(txn), req_handler.state.headHash)

    # timestamp beetween FIRST_ID_TS and SECOND_ID_TS
    delta_req['operation'][FROM] = FIRST_ID_TS + 10
    path_to_reg_entry = domain.make_state_path_for_revoc_reg_entry(
        revoc_reg_def_id=entry_second_id['operation'][REVOC_REG_DEF_ID])
    reg_entry = req_handler._get_reg_entry_by_timestamp(
        delta_req['operation'][FROM], path_to_reg_entry)
    # we found root_hash in txRevoc storage but there is not corresponded reg_entry by path
    assert reg_entry.root_hash is not None
    assert reg_entry.value is None

    path_to_reg_entry_accum = domain.make_state_path_for_revoc_reg_entry(
        revoc_reg_def_id=entry_second_id['operation'][REVOC_REG_DEF_ID])
    reg_entry_accum = req_handler._get_reg_entry_accum_by_timestamp(
        delta_req['operation'][FROM], path_to_reg_entry_accum)
    assert reg_entry_accum.root_hash is not None
    assert reg_entry_accum.value is None
Beispiel #28
0
def add_revoc_def_by_default(create_node_and_not_start,
                  looper,
                  sdk_wallet_steward):
    node = create_node_and_not_start
    data = {
        ID: randomString(50),
        TXN_TYPE: REVOC_REG_DEF,
        REVOC_TYPE: "CL_ACCUM",
        TAG: randomString(5),
        CRED_DEF_ID: ":".join(4*[randomString(10)]),
        VALUE:{
            ISSUANCE_TYPE: ISSUANCE_BY_DEFAULT,
            MAX_CRED_NUM: 1000000,
            TAILS_HASH: randomString(50),
            TAILS_LOCATION: 'http://tails.location.com',
            PUBLIC_KEYS: {},
        }
    }
    req = sdk_sign_request_from_dict(looper, sdk_wallet_steward, data)

    req_handler = node.getDomainReqHandler()
    txn = append_txn_metadata(reqToTxn(Request(**req)),
                              txn_time=int(time.time()),
                              seq_no=node.domainLedger.seqNo + 1)
    req_handler._addRevocDef(txn)
    return req
Beispiel #29
0
def db_manager(tconf):
    _db_manager = DatabaseManager()
    storage = initKeyValueStorage(
        KeyValueStorageType.Memory,
        None,
        "tokenInMemoryStore",
        txn_serializer=serialization.multi_sig_store_serializer)
    ledger = get_fake_ledger()

    def commit_txns(count):
        ledger.committed_root_hash = ledger.uncommitted_root_hash
        return None, [1]

    ledger.commitTxns = commit_txns
    ledger.root_hash = txn_root_serializer.serialize("1")
    ledger.uncommitted_root_hash = "1"
    ledger.uncommitted_size = 1
    ledger.size = 0
    ledger.discardTxns = lambda x: None
    ledger.committed_root_hash = "-1"
    ledger.append_txns_metadata = lambda txns, txn_time: [
        append_txn_metadata(txn, 2, txn_time, 2) for txn in txns
    ]
    ledger.appendTxns = lambda x: (None, x)
    _db_manager.register_new_database(TOKEN_LEDGER_ID, ledger,
                                      PruningState(storage))
    return _db_manager
Beispiel #30
0
def _add_txns_to_ledger(node, looper, sdk_wallet_client, num_txns_in_reply,
                        reply_count):
    '''
    Add txn_count transactions to node's ledger and return
    ConsistencyProof for all new transactions and list of CatchupReplies
    :return: ConsistencyProof, list of CatchupReplies
    '''
    txn_count = num_txns_in_reply * reply_count
    ledger_manager = node.ledgerManager
    ledger = ledger_manager.ledgerRegistry[ledger_id].ledger
    ledger_info = ledger_manager.getLedgerInfoByType(ledger_id)
    reqs = sdk_signed_random_requests(looper, sdk_wallet_client, txn_count)
    # add transactions to ledger
    for req in reqs:
        txn = append_txn_metadata(reqToTxn(req), txn_time=12345678)
        ledger_manager._add_txn(ledger_id, ledger, ledger_info, txn)
    # generate CatchupReps
    replies = []
    for i in range(ledger.seqNo - txn_count + 1, ledger.seqNo + 1,
                   num_txns_in_reply):
        start = i
        end = i + num_txns_in_reply - 1
        cons_proof = ledger_manager._make_consistency_proof(
            ledger, end, ledger.size)
        txns = {}
        for seq_no, txn in ledger.getAllTxn(start, end):
            txns[str(
                seq_no)] = ledger_manager.owner.update_txn_with_extra_data(txn)
        replies.append(CatchupRep(ledger_id, SortedDict(txns), cons_proof))
    return ledger_manager._buildConsistencyProof(ledger_id,
                                                 ledger.seqNo - txn_count,
                                                 ledger.seqNo), replies
def _add_txns_to_ledger(node, looper, sdk_wallet_client, num_txns_in_reply, reply_count):
    '''
    Add txn_count transactions to node's ledger and return
    ConsistencyProof for all new transactions and list of CatchupReplies
    :return: ConsistencyProof, list of CatchupReplies
    '''
    txn_count = num_txns_in_reply * reply_count
    ledger_manager = node.ledgerManager
    ledger = ledger_manager.ledgerRegistry[ledger_id].ledger
    catchup_rep_service = ledger_manager._node_leecher._leechers[ledger_id]._catchup_rep_service
    reqs = sdk_signed_random_requests(looper, sdk_wallet_client, txn_count)
    # add transactions to ledger
    for req in reqs:
        txn = append_txn_metadata(reqToTxn(req), txn_time=12345678)
        catchup_rep_service._add_txn(txn)
    # generate CatchupReps
    replies = []
    for i in range(ledger.seqNo - txn_count + 1, ledger.seqNo + 1, num_txns_in_reply):
        start = i
        end = i + num_txns_in_reply - 1
        cons_proof = ledger_manager._node_seeder._make_consistency_proof(ledger, end, ledger.size)
        txns = {}
        for seq_no, txn in ledger.getAllTxn(start, end):
            txns[str(seq_no)] = ledger_manager.owner.update_txn_with_extra_data(txn)
        replies.append(CatchupRep(ledger_id,
                                  SortedDict(txns),
                                  cons_proof))

    three_pc_key = node.three_phase_key_for_txn_seq_no(ledger_id, ledger.seqNo)
    view_no, pp_seq_no = three_pc_key if three_pc_key else (0, 0)
    return CatchupTill(start_size=ledger.seqNo - txn_count,
                       final_size=ledger.seqNo,
                       final_hash=Ledger.hashToStr(ledger.tree.merkle_tree_hash(0, ledger.seqNo)),
                       view_no=view_no,
                       pp_seq_no=pp_seq_no), replies
Beispiel #32
0
def add_revoc_def_by_demand(create_node_and_not_start, looper,
                            sdk_wallet_steward):
    node = create_node_and_not_start
    data = {
        ID: randomString(50),
        TXN_TYPE: REVOC_REG_DEF,
        REVOC_TYPE: "CL_ACCUM",
        TAG: randomString(5),
        CRED_DEF_ID: ":".join(4 * [randomString(10)]),
        VALUE: {
            ISSUANCE_TYPE: ISSUANCE_ON_DEMAND,
            MAX_CRED_NUM: 1000000,
            TAILS_HASH: randomString(50),
            TAILS_LOCATION: 'http://tails.location.com',
            PUBLIC_KEYS: {},
        }
    }
    req = sdk_sign_request_from_dict(looper, sdk_wallet_steward, data)

    req_handler = node.getDomainReqHandler()
    txn = append_txn_metadata(reqToTxn(Request(**req)),
                              txn_time=int(time.time()),
                              seq_no=node.domainLedger.seqNo + 1)
    req_handler._addRevocDef(txn)
    return req
 def apply(self, req: Request, cons_time):
     txn = append_txn_metadata(reqToTxn(req),
                               txn_time=cons_time)
     self.ledger.append_txns_metadata([txn])
     (start, _), _ = self.ledger.appendTxns([txn])
     self.updateState([txn], isCommitted=False)
     return start, txn
Beispiel #34
0
def test_no_state_proofs_if_protocol_version_less(write_manager, read_manager,
                                                  db_manager):
    nym = 'Gw6pDLhcBcoQesN72qfotTgFa7cbuqZpkX3Xo6pLhPhv'
    role = "2"
    verkey = "~7TYfekw4GUagBnBVCqPjiC"
    identifier = "6ouriXMZkLeHsuXrN1X1fd"

    seq_no = 0
    txn_time = int(time.time())
    # Adding nym
    data = {
        TARGET_NYM: nym,
        ROLE: role,
        VERKEY: verkey,
    }
    txn = append_txn_metadata(reqToTxn(
        Request(operation=data,
                protocolVersion=CURRENT_PROTOCOL_VERSION,
                identifier=identifier)),
                              seq_no=seq_no,
                              txn_time=txn_time)
    txn = set_type(txn, NYM)
    txn = append_payload_metadata(txn, frm=nym)
    write_manager.update_state(txn)
    db_manager.get_state(DOMAIN_LEDGER_ID).commit()
    multi_sig = save_multi_sig(db_manager)

    # Getting nym
    request = Request(operation={
        TARGET_NYM: nym,
        TXN_TYPE: GET_NYM,
    },
                      signatures={})
    result = read_manager.get_result(request)
    assert STATE_PROOF not in result
Beispiel #35
0
def test_req_to_txn_with_seq_no(req_and_expected):
    req, new_expected = req_and_expected
    new = SortedDict(
        append_txn_metadata(reqToTxn(req), seq_no=143, txn_time=2613945121))
    new_expected["txnMetadata"]["txnTime"] = 2613945121
    new_expected["txnMetadata"]["seqNo"] = 143
    assert new == new_expected
Beispiel #36
0
    def tryOrderAndAddTxns(self, commit):
        canOrder, _ = self.canOrder(commit)
        node = replica.node
        if not replica.added and canOrder:
            pp = self.getPrePrepare(commit.viewNo, commit.ppSeqNo)
            ledger_manager = node.ledgerManager
            ledger_id = DOMAIN_LEDGER_ID
            catchup_rep_service = ledger_manager._leechers[
                ledger_id].catchup_rep_service

            # simulate audit ledger catchup
            three_pc_batch = ThreePcBatch.from_pre_prepare(
                pre_prepare=pp,
                valid_txn_count=len(reqs),
                state_root=pp.stateRootHash,
                txn_root=pp.txnRootHash)
            node.audit_handler.post_batch_applied(three_pc_batch)
            node.audit_handler.commit_batch(ledger_id, len(reqs),
                                            pp.stateRootHash, pp.txnRootHash,
                                            pp.ppTime)

            ledger_manager.preCatchupClbk(ledger_id)
            pp = self.getPrePrepare(commit.viewNo, commit.ppSeqNo)
            for req in reqs:
                txn = append_txn_metadata(reqToTxn(req), txn_time=pp.ppTime)
                catchup_rep_service._add_txn(txn)
            ledger_manager.catchupCompleted(DOMAIN_LEDGER_ID,
                                            (node.viewNo, commit.ppSeqNo))

            replica.added = True

        return origMethod(commit)
def test_process_invalid_catchup_reply(txnPoolNodeSet, looper, sdk_wallet_client):
    '''
    Test correct work of method processCatchupRep and that sending replies
    in reverse order will call a few iterations of cycle in _processCatchupRep
    '''
    ledger_manager = txnPoolNodeSet[0].ledgerManager
    ledger_info = ledger_manager.getLedgerInfoByType(ledger_id)
    ledger = ledger_manager.ledgerRegistry[ledger_id].ledger
    ledger_size = ledger.size
    num_txns_in_reply = 3
    reply_count = 2

    cons_proof, catchup_reps = _add_txns_to_ledger(txnPoolNodeSet[1],
                                                   looper,
                                                   sdk_wallet_client,
                                                   num_txns_in_reply,
                                                   reply_count)
    ledger_info.catchUpTill = cons_proof
    ledger_info.state = LedgerState.syncing

    # make invalid catchup reply by dint of adding new transaction in it
    reply2 = catchup_reps[1]
    txns = OrderedDict(getattr(reply2, f.TXNS.nm))
    req = sdk_signed_random_requests(looper, sdk_wallet_client, 1)[0]
    txns[str(ledger_size + 4)] = append_txn_metadata(reqToTxn(req), txn_time=12345678)
    invalid_reply2 = CatchupRep(ledger_id,
                                txns,
                                getattr(reply2, f.CONS_PROOF.nm))
    # process 2nd interval with invalid catchup reply
    ledger_manager.processCatchupRep(invalid_reply2,
                                     sdk_wallet_client[1])
    # check that invalid transaction was not added to ledger, but add to ledger_info.receivedCatchUpReplies
    check_reply_not_applied(ledger_size, ledger, ledger_info, sdk_wallet_client[1], invalid_reply2)

    # process valid reply from 1st interval
    reply1 = catchup_reps[0]
    ledger_manager.processCatchupRep(reply1, sdk_wallet_client[1])
    # check that only valid reply added to ledger
    ledger_size = check_replies_applied(ledger_size,
                          ledger,
                          ledger_info,
                          sdk_wallet_client[1],
                          [reply1])
    # check that invalid reply was removed from ledger_info.receivedCatchUpReplies
    received_replies = {str(seq_no) for seq_no, _ in ledger_info.receivedCatchUpReplies}
    assert not set(getattr(reply2, f.TXNS.nm).keys()).issubset(received_replies)
    assert not ledger_info.recvdCatchupRepliesFrm[sdk_wallet_client[1]]

    # check that valid reply for 2nd interval was added to ledger
    reply2 = catchup_reps[1]
    ledger_manager.processCatchupRep(reply2, sdk_wallet_client[1])
    ledger_size = check_replies_applied(ledger_size,
                          ledger,
                          ledger_info,
                          sdk_wallet_client[1],
                          [reply2])
    assert not ledger_info.receivedCatchUpReplies
    assert not ledger_info.recvdCatchupRepliesFrm
    def apply_request(self, request: Request, batch_ts):
        txn = self._reqToTxn(request)
        txn = append_txn_metadata(txn, txn_id=self.gen_txn_path(txn))

        db = self.get_db_by_request(request)
        db.ledger.append_txns_metadata([txn], batch_ts)
        (start, end), _ = db.ledger.appendTxns(
            [self.transform_txn_for_ledger(txn)])
        self.updateState([txn])
        return start, txn
    def apply(self, req: Request, cons_time: int):
        txn = self._reqToTxn(req)

        txn = append_txn_metadata(txn, txn_id=self.gen_txn_path(txn))

        self.ledger.append_txns_metadata([txn], cons_time)
        (start, end), _ = self.ledger.appendTxns(
            [self.transform_txn_for_ledger(txn)])
        self.updateState([txn])
        return start, txn
def test_req_to_txn_with_seq_no(req_and_expected):
    req, new_expected = req_and_expected
    new = SortedDict(
        append_txn_metadata(
            reqToTxn(req),
            seq_no=143,
            txn_time=2613945121)
    )
    new_expected["txnMetadata"]["txnTime"] = 2613945121
    new_expected["txnMetadata"]["seqNo"] = 143
    assert new == new_expected
Beispiel #41
0
    def nym_txn(nym, name=None, verkey=None, role=None, creator=None, txn_id=None, seq_no=None,
                protocol_version=CURRENT_PROTOCOL_VERSION):
        txn = init_empty_txn(NYM, protocol_version=protocol_version)

        txn_data = {
            TARGET_NYM: nym,
        }
        if verkey is not None:
            txn_data[VERKEY] = verkey
        if role is not None:
            txn_data[ROLE] = role
        if name is not None:
            txn_data[ALIAS] = name
        set_payload_data(txn, txn_data)

        txn = append_payload_metadata(txn,
                                      frm=creator)
        if txn_id:
            txn = append_txn_metadata(txn, txn_id=txn_id)
        if seq_no:
            txn = append_txn_metadata(txn, seq_no=seq_no)

        return txn
def test_append_txn_metadata():
    txn = init_empty_txn(txn_type=NODE, protocol_version="3")
    set_payload_data(txn, {"somekey": "somevalue"})
    append_payload_metadata(txn, frm="DID1", req_id=12345)
    append_txn_metadata(txn, seq_no=144, txn_time=12345678, txn_id="dddd")
    expected = SortedDict({
        "reqSignature": {},
        "txn": {
            "data": {"somekey": "somevalue"},
            "metadata": {
                "from": "DID1",
                "reqId": 12345,
            },
            "protocolVersion": "3",
            "type": NODE,
        },
        "txnMetadata": {
            "seqNo": 144,
            "txnId": "dddd",
            "txnTime": 12345678,
        },
        "ver": "1"
    })
    assert SortedDict(expected) == SortedDict(txn)
def reg_entry_with_other_reg_id(looper,
                                sdk_wallet_steward,
                                add_another_reg_id,
                                create_node_and_not_start):
    node = create_node_and_not_start
    revoc_def_txn = add_another_reg_id
    data = build_revoc_reg_entry_for_given_revoc_reg_def(revoc_def_txn)
    req = sdk_sign_request_from_dict(looper, sdk_wallet_steward, data)
    looper.runFor(2)
    req_handler = node.getDomainReqHandler()
    txn = append_txn_metadata(reqToTxn(Request(**req)),
                              txn_time=FIRST_ID_TS,
                              seq_no=node.domainLedger.seqNo + 1)
    req_handler._addRevocRegEntry(txn)
    req_handler.ts_store.set(get_txn_time(txn), req_handler.state.headHash)
    return txn
Beispiel #44
0
    def tryOrderAndAddTxns(self, commit):
        canOrder, _ = self.canOrder(commit)
        node = replica.node
        if not replica.added and canOrder:

            ledger_manager = node.ledgerManager
            ledger_id = DOMAIN_LEDGER_ID
            ledger = ledger_manager.ledgerRegistry[ledger_id].ledger
            ledgerInfo = ledger_manager.getLedgerInfoByType(ledger_id)

            ledger_manager.preCatchupClbk(ledger_id)
            pp = self.getPrePrepare(commit.viewNo, commit.ppSeqNo)
            for req in reqs:
                txn = append_txn_metadata(reqToTxn(req), txn_time=pp.ppTime)
                ledger_manager._add_txn(
                    ledger_id, ledger, ledgerInfo, txn)
            ledger_manager.catchupCompleted(
                DOMAIN_LEDGER_ID, (node.viewNo, commit.ppSeqNo))

            replica.added = True

        return origMethod(commit)
 def apply(self, req: Request, cons_time):
     txn = append_txn_metadata(reqToTxn(req),
                               txn_time=cons_time)
     self.ledger.append_txns_metadata([txn])
     (start, _), _ = self.ledger.appendTxns([txn])
     return start, txn
    wallet_h, did = sdk_wallet
    reqs_str = [json.dumps(req.as_dict) for req in reqs]
    resp = [looper.loop.run_until_complete(sign_request(wallet_h, did, req)) for req in reqs_str]
    return resp


def sdk_signed_random_requests(looper, sdk_wallet, count):
    _, did = sdk_wallet
    reqs_obj = sdk_random_request_objects(count, identifier=did, protocol_version=CURRENT_PROTOCOL_VERSION)
    return sdk_sign_request_objects(looper, sdk_wallet, reqs_obj)


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('count', help="Count of generated txns", type=int)
    parser.add_argument('outfpath', help="Path to save generated txns", type=str, default='/tmp/generated_txns')
    args = parser.parse_args()
    path_to_save = os.path.realpath(args.outfpath)

    with ExitStack() as exit_stack:
        with Looper() as looper:
            sdk_wallet, DID = looper.loop.run_until_complete(get_wallet_and_pool())
            with open(path_to_save, 'w') as outpath:
                for i in range(args.count):
                    req = sdk_signed_random_requests(looper, (sdk_wallet, DID), 1)[0]
                    txn = reqToTxn(req)
                    append_txn_metadata(txn, txn_time=int(time.time()))
                    outpath.write(json.dumps(txn))
                    outpath.write(os.linesep)
            looper.stopall()
def test_req_to_txn(req_and_expected):
    req, new_expected = req_and_expected
    txn = append_txn_metadata(reqToTxn(req), txn_time=1513945121)
    new = SortedDict(txn)
    assert new == new_expected