Exemplo n.º 1
0
def decode_readwrite_sets(rw_sets_bytes):
    """Decodes read write sets from a given TxReadWriteSet

    :param rw_sets_bytes: Byte buffer of the TxReadWriteSet
    :type rw_sets_bytes: str
    :return: deserialized transaction read write set contents.
    """
    proto_tx_read_write_set = rwset_pb2.TxReadWriteSet()
    proto_tx_read_write_set.ParseFromString(rw_sets_bytes)
    tx_read_write_set = {}
    tx_read_write_set['data_model'] = proto_tx_read_write_set.data_model
    if (tx_read_write_set['data_model'] == rwset_pb2.TxReadWriteSet.KV):
        tx_read_write_set['ns_rwset'] = []
        proto_ns_rwset = proto_tx_read_write_set.ns_rwset
        for rw_set in proto_ns_rwset:
            kv_rw_set = {}
            proto_kv_rw_set = rw_set
            kv_rw_set['namespace'] = proto_kv_rw_set.namespace
            kv_rw_set['rwset'] = decode_kv_rw_set(proto_kv_rw_set.rwset)
            tx_read_write_set['ns_rwset'].append(kv_rw_set)
    else:
        tx_read_write_set['ns_rwset'] = proto_tx_read_write_set.ns_rwset
    return tx_read_write_set
Exemplo n.º 2
0
    def test_decode_read_write_sets(self):
        """
        checks if the decode read write sets have been decoded properly.
        """
        kv_read_proto = kv_rwset_pb2.KVRead()
        version_proto = kv_rwset_pb2.Version()
        version_proto.block_num = 12
        version_proto.tx_num = 21
        kv_read_proto.version.CopyFrom(version_proto)
        kv_read_proto.key = 'read key'
        reads_array = []
        reads_array.append(kv_read_proto)

        range_query_info_proto = kv_rwset_pb2.RangeQueryInfo()
        range_query_info_proto.start_key = 'start'
        range_query_info_proto.end_key = 'end'
        range_query_info_proto.itr_exhausted = False
        range_query_info_array = []
        range_query_info_array.append(range_query_info_proto)

        kv_write_proto = kv_rwset_pb2.KVWrite()
        kv_write_proto.key = 'write key'
        kv_write_proto.is_delete = False
        kv_write_proto.value = b'this is the value'
        writes_array = []
        writes_array.append(kv_write_proto)

        kvrwset_proto = kv_rwset_pb2.KVRWSet()
        kvrwset_proto.reads.extend(reads_array)
        kvrwset_proto.range_queries_info.extend(range_query_info_array)
        kvrwset_proto.writes.extend(writes_array)

        results_proto = rwset_pb2.TxReadWriteSet()
        results_proto.data_model = rwset_pb2.TxReadWriteSet.KV
        ns_rwset_array = []
        ns_rwset_proto = rwset_pb2.NsReadWriteSet()
        ns_rwset_proto.namespace = 'testnamespace'
        ns_rwset_proto.rwset = kvrwset_proto.SerializeToString()
        ns_rwset_array.append(ns_rwset_proto)
        results_proto.ns_rwset.extend(ns_rwset_array)

        results_json = decode_readwrite_sets(results_proto.SerializeToString())
        nsrwset_value = results_json['ns_rwset'][0]
        nsrwset_reads = nsrwset_value['rwset']['reads']
        nsrwset_range_queries = nsrwset_value['rwset']['range_queries_info'][0]

        self.assertEqual('testnamespace', nsrwset_value['namespace'])
        self.assertEqual('read key', nsrwset_reads[0]['key'])
        self.assertEqual('12', nsrwset_reads[0]['version']['block_num'])
        self.assertEqual('21', nsrwset_reads[0]['version']['tx_num'])

        self.assertIn('range_queries_info', nsrwset_value['rwset'])
        self.assertEqual('end', nsrwset_range_queries['end_key'])
        self.assertEqual('start', nsrwset_range_queries['start_key'])
        self.assertIn('reads_merkle_hashes', nsrwset_range_queries)
        self.assertIn('raw_reads', nsrwset_range_queries)

        self.assertIn('writes', nsrwset_value['rwset'])
        self.assertEqual('write key',
                         nsrwset_value['rwset']['writes'][0]['key'])
        self.assertEqual(b'this is the value',
                         nsrwset_value['rwset']['writes'][0]['value'])