-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
09ce976
commit 80c5021
Showing
12 changed files
with
309 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
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
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
66 changes: 66 additions & 0 deletions
66
storage/indexer_schemas/src/schema/translated_v1_event/mod.rs
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,66 @@ | ||
// Copyright © Aptos Foundation | ||
// Parts of the project are originally copyright © Meta Platforms, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//! This module defines physical storage schema for the contract events. | ||
//! | ||
//! A translated v1 event is keyed by the version of the transaction it belongs to and the index of | ||
//! the original v2 event among all events yielded by the same transaction. | ||
//! ```text | ||
//! |<-------key----->|<---value--->| | ||
//! | version | index | event bytes | | ||
//! ``` | ||
|
||
use crate::schema::{ensure_slice_len_eq, TRANSLATED_V1_EVENT_CF_NAME}; | ||
use anyhow::Result; | ||
use aptos_schemadb::{ | ||
define_pub_schema, | ||
schema::{KeyCodec, ValueCodec}, | ||
}; | ||
use aptos_types::{contract_event::ContractEventV1, transaction::Version}; | ||
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}; | ||
use std::mem::size_of; | ||
|
||
define_pub_schema!( | ||
TranslatedV1EventSchema, | ||
Key, | ||
ContractEventV1, | ||
TRANSLATED_V1_EVENT_CF_NAME | ||
); | ||
|
||
type Index = u64; | ||
type Key = (Version, Index); | ||
|
||
impl KeyCodec<TranslatedV1EventSchema> for Key { | ||
fn encode_key(&self) -> Result<Vec<u8>> { | ||
let (version, index) = *self; | ||
|
||
let mut encoded_key = Vec::with_capacity(size_of::<Version>() + size_of::<Index>()); | ||
encoded_key.write_u64::<BigEndian>(version)?; | ||
encoded_key.write_u64::<BigEndian>(index)?; | ||
Ok(encoded_key) | ||
} | ||
|
||
fn decode_key(data: &[u8]) -> Result<Self> { | ||
ensure_slice_len_eq(data, size_of::<Self>())?; | ||
|
||
let version_size = size_of::<Version>(); | ||
|
||
let version = (&data[..version_size]).read_u64::<BigEndian>()?; | ||
let index = (&data[version_size..]).read_u64::<BigEndian>()?; | ||
Ok((version, index)) | ||
} | ||
} | ||
|
||
impl ValueCodec<TranslatedV1EventSchema> for ContractEventV1 { | ||
fn encode_value(&self) -> Result<Vec<u8>> { | ||
bcs::to_bytes(self).map_err(Into::into) | ||
} | ||
|
||
fn decode_value(data: &[u8]) -> Result<Self> { | ||
bcs::from_bytes(data).map_err(Into::into) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test; |
20 changes: 20 additions & 0 deletions
20
storage/indexer_schemas/src/schema/translated_v1_event/test.rs
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,20 @@ | ||
// Copyright © Aptos Foundation | ||
// Parts of the project are originally copyright © Meta Platforms, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use super::*; | ||
use aptos_schemadb::{schema::fuzzing::assert_encode_decode, test_no_panic_decoding}; | ||
use proptest::prelude::*; | ||
|
||
proptest! { | ||
#[test] | ||
fn test_encode_decode( | ||
version in any::<Version>(), | ||
index in any::<u64>(), | ||
event in any::<ContractEventV1>(), | ||
) { | ||
assert_encode_decode::<TranslatedV1EventSchema>(&(version, index), &event); | ||
} | ||
} | ||
|
||
test_no_panic_decoding!(TranslatedV1EventSchema); |
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
Oops, something went wrong.