Example #1
0
 def prepare_buy_for_state(txn):
     from common.serializers.serialization import domain_state_serializer
     identifier = get_from(txn)
     req_id = get_req_id(txn)
     value = domain_state_serializer.serialize({"amount": get_payload_data(txn)['amount']})
     key = TestDomainRequestHandler.prepare_buy_key(identifier, req_id)
     return key, value
 def prepare_buy_for_state(txn):
     identifier = get_from(txn)
     req_id = get_req_id(txn)
     value = domain_state_serializer.serialize(
         {"amount": get_payload_data(txn)['amount']})
     key = BuyHandler.prepare_buy_key(identifier, req_id)
     return key, value
Example #3
0
 def prepare_buy_for_state(txn):
     from common.serializers.serialization import domain_state_serializer
     identifier = txn.get(f.IDENTIFIER.nm)
     req_id = txn.get(f.REQ_ID.nm)
     value = domain_state_serializer.serialize({"amount": txn['amount']})
     key = TestDomainRequestHandler.prepare_buy_key(identifier, req_id)
     return key, value
Example #4
0
def prepare_for_state_read(req: Request):
    if req.txn_type == "buy":
        txn = reqToTxn(req)
        key = BuyHandler.prepare_buy_key(req.identifier, req.reqId)
        value = domain_state_serializer.serialize(
            {"amount": get_payload_data(txn)['amount']})
        return key, value
Example #5
0
    def get_result(self, request: Request):
        self._validate_request_type(request)
        nym = request.operation[TARGET_NYM]
        path = domain.make_state_path_for_nym(nym)
        nym_data, proof = self._get_value_from_state(path, with_proof=True)
        if nym_data:
            nym_data = domain_state_serializer.deserialize(nym_data)
            nym_data[TARGET_NYM] = nym
            data = domain_state_serializer.serialize(nym_data)
            seq_no = nym_data[f.SEQ_NO.nm]
            update_time = nym_data[TXN_TIME]
        else:
            data = None
            seq_no = None
            update_time = None

        # TODO: add update time here!
        result = self.make_result(request=request,
                                  data=data,
                                  last_seq_no=seq_no,
                                  update_time=update_time,
                                  proof=proof)

        result.update(request.operation)
        return result
Example #6
0
 def update_state(self, txn, prev_result, request, is_committed=False):
     self._validate_txn_type(txn)
     key = self.gen_state_key(txn)
     value = domain_state_serializer.serialize({"amount": get_payload_data(txn)['amount']})
     self.state.set(key, value)
     logger.trace('{} after adding to state, headhash is {}'.
                  format(self, self.state.headHash))
Example #7
0
 def prepare_buy_for_state(txn):
     from common.serializers.serialization import domain_state_serializer
     identifier = get_from(txn)
     req_id = get_req_id(txn)
     value = domain_state_serializer.serialize({"amount": get_payload_data(txn)['amount']})
     key = TestDomainRequestHandler.prepare_buy_key(identifier, req_id)
     return key, value
Example #8
0
def prepare_nym_for_state(txn):
    # TODO: this is semi-duplicated in plenum.DomainRequestHandler
    data = txn.get(DATA)
    parsed = domain_state_serializer.deserialize(data)
    parsed.pop(TARGET_NYM, None)
    value = domain_state_serializer.serialize(parsed)
    nym = txn[TARGET_NYM]
    key = make_state_path_for_nym(nym)
    return key, value
Example #9
0
def prepare_get_nym_for_state(txn):
    data = txn.get(DATA)
    value = None
    if data is not None:
        parsed = domain_state_serializer.deserialize(data)
        parsed.pop(TARGET_NYM, None)
        value = domain_state_serializer.serialize(parsed)
    nym = txn[TARGET_NYM]
    key = make_state_path_for_nym(nym)
    return key, value
Example #10
0
def prepare_get_nym_for_state(reply):
    data = reply.get(DATA)
    value = None
    if data is not None:
        parsed = domain_state_serializer.deserialize(data)
        parsed.pop(TARGET_NYM, None)
        value = domain_state_serializer.serialize(parsed)
    nym = reply[TARGET_NYM]
    key = make_state_path_for_nym(nym)
    return key, value
Example #11
0
def add_txns(db_manager, ledger_id, count, txn_time):
    ledger = db_manager.get_ledger(ledger_id)
    state = db_manager.get_state(ledger_id)

    txns = [do_req_to_txn({}, {"ledger_id": ledger_id, "num": i}) for i in range(count)]
    ledger.append_txns_metadata(txns, txn_time)

    ledger.appendTxns(txns)
    for i, txn in enumerate(txns):
        state.set(bytes(ledger_id + i),
                  domain_state_serializer.serialize(txn))
Example #12
0
def get_nym_verify_proof(read_manager, db_manager, nym, data, multi_sig):
    request = Request(operation={
        TARGET_NYM: nym,
        TXN_TYPE: GET_NYM,
    },
                      signatures={},
                      protocolVersion=CURRENT_PROTOCOL_VERSION)
    result = read_manager.get_result(request)
    proof = extract_proof(result, multi_sig)
    result_data = None

    assert proof
    if data:
        assert result[DATA]
        result_data = domain_state_serializer.deserialize(result[DATA])
        data_from_req = copy.copy(result_data)
        data_from_req.pop(f.IDENTIFIER.nm, None)
        data_from_req.pop(f.SEQ_NO.nm, None)
        data_from_req.pop(TXN_TIME, None)
        assert data_from_req == data

    # Verifying signed state proof
    path = nym_to_state_key(nym)
    # If the value does not exist, serialization should be null and
    # verify_state_proof needs to be given null (None). This is done to
    # differentiate between absence of value and presence of empty string value
    if result_data:
        data.pop(TARGET_NYM, None)
        data.update({
            f.IDENTIFIER.nm: result_data[f.IDENTIFIER.nm],
            f.SEQ_NO.nm: result_data[f.SEQ_NO.nm],
            TXN_TIME: result_data[TXN_TIME]
        })
    serialized_value = domain_state_serializer.serialize(
        data) if data else None
    proof_nodes = base64.b64decode(proof[PROOF_NODES])
    root_hash = base58.b58decode(proof[ROOT_HASH])
    return db_manager.get_state(DOMAIN_LEDGER_ID).verify_state_proof(
        root_hash, path, serialized_value, proof_nodes, serialized=True)
Example #13
0
def hash_of(text) -> str:
    if not isinstance(text, (str, bytes)):
        text = domain_state_serializer.serialize(text)
    if not isinstance(text, bytes):
        text = text.encode()
    return sha256(text).hexdigest()
Example #14
0
def encode_state_value(value, seqNo, txnTime):
    return domain_state_serializer.serialize({
        LAST_SEQ_NO: seqNo,
        LAST_UPDATE_TIME: txnTime,
        VALUE: value
    })
Example #15
0
 def _reset_fees(self, node):
     empty_fees = domain_state_serializer.serialize({})
     node.db_manager.get_state(CONFIG_LEDGER_ID).set(build_path_for_set_fees().encode(), empty_fees)
Example #16
0
def encode_state_value(value, seqNo, txnTime):
    return domain_state_serializer.serialize({
        LAST_SEQ_NO: seqNo,
        LAST_UPDATE_TIME: txnTime,
        VALUE: value
    })
Example #17
0
def update_nym(state, identifier, role):
    state.set(
        nym_to_state_key(identifier),
        domain_state_serializer.serialize(
            create_nym_txn(identifier, role)['txn']['data']))
Example #18
0
def hash_of(text) -> str:
    if not isinstance(text, (str, bytes)):
        text = domain_state_serializer.serialize(text)
    if not isinstance(text, bytes):
        text = text.encode()
    return sha256(text).hexdigest()