pos_ledger_db/schema/transaction/
mod.rs

1// Copyright (c) The Diem Core Contributors
2// SPDX-License-Identifier: Apache-2.0
3
4// Copyright 2021 Conflux Foundation. All rights reserved.
5// Conflux is free software and distributed under GNU General Public License.
6// See http://www.gnu.org/licenses/
7
8//! This module defines physical storage schema for signed transactions.
9//!
10//! Serialized signed transaction bytes identified by version.
11//! ```text
12//! |<--key-->|<--value-->|
13//! | version | txn bytes |
14//! ```
15//!
16//! `Version` is serialized in big endian so that records in RocksDB will be in
17//! order of it's numeric value.
18
19use crate::schema::{ensure_slice_len_eq, TRANSACTION_CF_NAME};
20use anyhow::Result;
21use byteorder::{BigEndian, ReadBytesExt};
22use diem_types::transaction::{Transaction, TransactionUnchecked, Version};
23use schemadb::{
24    define_schema,
25    schema::{KeyCodec, ValueCodec},
26};
27use std::mem::size_of;
28
29define_schema!(TransactionSchema, Version, Transaction, TRANSACTION_CF_NAME);
30
31impl KeyCodec<TransactionSchema> for Version {
32    fn encode_key(&self) -> Result<Vec<u8>> { Ok(self.to_be_bytes().to_vec()) }
33
34    fn decode_key(mut data: &[u8]) -> Result<Self> {
35        ensure_slice_len_eq(data, size_of::<Version>())?;
36        Ok(data.read_u64::<BigEndian>()?)
37    }
38}
39
40impl ValueCodec<TransactionSchema> for Transaction {
41    fn encode_value(&self) -> Result<Vec<u8>> {
42        bcs::to_bytes(self).map_err(Into::into)
43    }
44
45    fn decode_value(data: &[u8]) -> Result<Self> {
46        bcs::from_bytes::<TransactionUnchecked>(data)
47            .map(Into::into)
48            .map_err(Into::into)
49    }
50}
51
52#[cfg(test)]
53mod test;