forked from aptos-labs/aptos-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6979a60
commit 8edb3dc
Showing
5 changed files
with
106 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) Aptos | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//! This module defines the physical storage schema for state value index, which is used | ||
//! to access the state value directly without needing to walk through the JMT. | ||
//! | ||
//! An Index Key in this data set has 2 pieces of information: | ||
//! 1. The state key | ||
//! 2. The version associated with the key | ||
//! The value associated with the key is nibble length, which combined with the state key and | ||
//! version can give us access to the JMT leaf associated with corresponding key and version | ||
//! | ||
//! | ||
//! //! ```text | ||
//! |<---------key--------> |<-----value----->| | ||
//! | state_key, version | num of nibbles | | ||
//! ``` | ||
use crate::schema::{ensure_slice_len_eq, ensure_slice_len_gt, STATE_VALUE_INDEX_CF_NAME}; | ||
use anyhow::Result; | ||
use aptos_types::state_store::state_key::StateKey; | ||
use aptos_types::transaction::Version; | ||
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}; | ||
use schemadb::{ | ||
define_schema, | ||
schema::{KeyCodec, ValueCodec}, | ||
}; | ||
use std::{io::Write, mem::size_of}; | ||
|
||
type Key = (StateKey, Version); | ||
|
||
define_schema!(StateValueIndexSchema, Key, u8, STATE_VALUE_INDEX_CF_NAME); | ||
|
||
impl KeyCodec<StateValueIndexSchema> for Key { | ||
fn encode_key(&self) -> Result<Vec<u8>> { | ||
let mut encoded = vec![]; | ||
encoded.write_all(&bcs::to_bytes(&self.0)?)?; | ||
encoded.write_u64::<BigEndian>(self.1)?; | ||
Ok(encoded) | ||
} | ||
|
||
fn decode_key(data: &[u8]) -> Result<Self> { | ||
const VERSION_SIZE: usize = size_of::<Version>(); | ||
|
||
ensure_slice_len_gt(data, VERSION_SIZE)?; | ||
let state_key_len = data.len() - VERSION_SIZE; | ||
let state_key: StateKey = bcs::from_bytes(&data[..state_key_len])?; | ||
let version = (&data[state_key_len..]).read_u64::<BigEndian>()?; | ||
Ok((state_key, version)) | ||
} | ||
} | ||
|
||
impl ValueCodec<StateValueIndexSchema> for u8 { | ||
fn encode_value(&self) -> Result<Vec<u8>> { | ||
let mut encoded = vec![]; | ||
encoded.write_u8(*self)?; | ||
Ok(encoded) | ||
} | ||
|
||
fn decode_value(data: &[u8]) -> Result<Self> { | ||
ensure_slice_len_eq(data, 1)?; | ||
Ok(data[0]) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) Aptos | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use super::*; | ||
use proptest::prelude::*; | ||
use schemadb::{schema::fuzzing::assert_encode_decode, test_no_panic_decoding}; | ||
|
||
proptest! { | ||
#[test] | ||
fn test_encode_decode( | ||
state_key in any::<StateKey>(), | ||
version in any::<Version>(), | ||
num_nibbles in any::<u8>(), | ||
) { | ||
assert_encode_decode::<StateValueIndexSchema>(&(state_key, version), &num_nibbles); | ||
} | ||
} | ||
|
||
test_no_panic_decoding!(StateValueIndexSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters