Ejemplo n.º 1
0
def decode_kv_rw_set(kv_bytes):
    """Decodes Key Value Read Write Set from KV Bytes

    :param kv_bytes: Buffer of key value bytes
    :type kv_bytes: str
    :return: deserialized key value read write set of reads, writes
             and range queries information.
    """
    proto_kv_rw_set = kv_rwset_pb2.KVRWSet()
    proto_kv_rw_set.ParseFromString(kv_bytes)
    kv_rw_set = {}
    # KV readwrite set has 3 arrays
    kv_rw_set['reads'] = []
    kv_rw_set['range_queries_info'] = []
    kv_rw_set['writes'] = []
    # Build reads
    reads = kv_rw_set['reads']
    proto_reads = proto_kv_rw_set.reads
    for read in proto_reads:
        reads.append(decode_kv_read(read))
    # Build range_queries_info
    range_queries_info = kv_rw_set['range_queries_info']
    proto_range_queries_info = proto_kv_rw_set.range_queries_info
    for range_query in proto_range_queries_info:
        range_queries_info.append(decode_range_query_info(range_query))
    # Build writes
    writes = kv_rw_set['writes']
    proto_writes = proto_kv_rw_set.writes
    for write in proto_writes:
        writes.append(decode_kv_write(write))

    kv_rw_set['reads'] = reads
    kv_rw_set['range_queries_info'] = range_queries_info
    kv_rw_set['writes'] = writes
    return kv_rw_set
Ejemplo 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'])