Skip to content

Commit

Permalink
[diemdb] add transaction_by_hash cf
Browse files Browse the repository at this point in the history
  • Loading branch information
lightmark authored and bors-libra committed Sep 16, 2021
1 parent 45efde8 commit f13aa94
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions storage/diemdb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ impl DiemDB {
TRANSACTION_CF_NAME,
TRANSACTION_ACCUMULATOR_CF_NAME,
TRANSACTION_BY_ACCOUNT_CF_NAME,
TRANSACTION_BY_HASH_CF_NAME,
TRANSACTION_INFO_CF_NAME,
]
}
Expand Down
3 changes: 3 additions & 0 deletions storage/diemdb/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub(crate) mod stale_node_index;
pub(crate) mod transaction;
pub(crate) mod transaction_accumulator;
pub(crate) mod transaction_by_account;
pub(crate) mod transaction_by_hash;
pub(crate) mod transaction_info;

use anyhow::{ensure, Result};
Expand All @@ -34,6 +35,7 @@ pub const STALE_NODE_INDEX_CF_NAME: ColumnFamilyName = "stale_node_index";
pub const TRANSACTION_CF_NAME: ColumnFamilyName = "transaction";
pub const TRANSACTION_ACCUMULATOR_CF_NAME: ColumnFamilyName = "transaction_accumulator";
pub const TRANSACTION_BY_ACCOUNT_CF_NAME: ColumnFamilyName = "transaction_by_account";
pub const TRANSACTION_BY_HASH_CF_NAME: ColumnFamilyName = "transaction_by_hash";
pub const TRANSACTION_INFO_CF_NAME: ColumnFamilyName = "transaction_info";

fn ensure_slice_len_eq(data: &[u8], len: usize) -> Result<()> {
Expand Down Expand Up @@ -91,6 +93,7 @@ pub mod fuzzing {
super::transaction_by_account::TransactionByAccountSchema,
data
);
decode_key_value!(super::transaction_by_hash::TransactionByHashSchema, data);
decode_key_value!(super::transaction_info::TransactionInfoSchema, data);
}
}
Expand Down
54 changes: 54 additions & 0 deletions storage/diemdb/src/schema/transaction_by_hash/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

//! This module defines physical storage schema mapping transaction hash to its version.
//! With the version one can resort to `TransactionSchema` for the transaction content.
//!
//! ```text
//! |<--key-->|<-value->|
//! | hash | txn_ver |
//! ```
use crate::schema::{ensure_slice_len_eq, TRANSACTION_BY_HASH_CF_NAME};
use anyhow::Result;
use byteorder::{BigEndian, ReadBytesExt};
use diem_crypto::HashValue;
use diem_types::transaction::Version;
use schemadb::{
define_schema,
schema::{KeyCodec, ValueCodec},
};
use std::mem::size_of;

define_schema!(
TransactionByHashSchema,
HashValue,
Version,
TRANSACTION_BY_HASH_CF_NAME
);

impl KeyCodec<TransactionByHashSchema> for HashValue {
fn encode_key(&self) -> Result<Vec<u8>> {
Ok(self.to_vec())
}

fn decode_key(data: &[u8]) -> Result<Self> {
ensure_slice_len_eq(data, size_of::<Self>())?;
Ok(HashValue::from_slice(data)?)
}
}

impl ValueCodec<TransactionByHashSchema> for Version {
fn encode_value(&self) -> Result<Vec<u8>> {
Ok(self.to_be_bytes().to_vec())
}

fn decode_value(mut data: &[u8]) -> Result<Self> {
ensure_slice_len_eq(data, size_of::<Self>())?;

Ok(data.read_u64::<BigEndian>()?)
}
}

#[cfg(test)]
mod test;
16 changes: 16 additions & 0 deletions storage/diemdb/src/schema/transaction_by_hash/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use super::*;
use proptest::prelude::*;
use schemadb::schema::assert_encode_decode;

proptest! {
#[test]
fn test_encode_decode(
hash in any::<HashValue>(),
version in any::<Version>(),
) {
assert_encode_decode::<TransactionByHashSchema>(&hash, &version);
}
}

0 comments on commit f13aa94

Please sign in to comment.