Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(katana-db): ensure key/value types implement their respective traits #1776

Merged
merged 2 commits into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/katana/storage/db/src/models/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::ops::Range;
use katana_primitives::transaction::TxNumber;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
pub struct StoredBlockBodyIndices {
/// The offset in database of the first transaction in the block.
///
Expand Down
6 changes: 3 additions & 3 deletions crates/katana/storage/db/src/models/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ use serde::{Deserialize, Serialize};
use super::storage::BlockList;
use crate::codecs::{Compress, Decode, Decompress, Encode};

#[derive(Debug, Default, Serialize, Deserialize)]
#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct ContractInfoChangeList {
pub class_change_list: BlockList,
pub nonce_change_list: BlockList,
}

#[derive(Debug)]
#[derive(Debug, Default, PartialEq, Eq)]
pub struct ContractClassChange {
pub contract_address: ContractAddress,
/// The updated class hash of `contract_address`.
Expand All @@ -37,7 +37,7 @@ impl Decompress for ContractClassChange {
}
}

#[derive(Debug)]
#[derive(Debug, Default, PartialEq, Eq)]
pub struct ContractNonceChange {
pub contract_address: ContractAddress,
/// The updated nonce value of `contract_address`.
Expand Down
2 changes: 1 addition & 1 deletion crates/katana/storage/db/src/models/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Decompress for StorageEntry {
}
}

#[derive(Debug, Default, Serialize, Deserialize)]
#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct BlockList(pub Vec<BlockNumber>);

#[derive(Debug, Clone, Default, PartialEq, Eq)]
Expand Down
87 changes: 87 additions & 0 deletions crates/katana/storage/db/src/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ tables! {

#[cfg(test)]
mod tests {

#[test]
fn test_tables() {
use super::*;
Expand Down Expand Up @@ -278,4 +279,90 @@ mod tests {
assert_eq!(Tables::StorageChangeHistory.table_type(), TableType::DupSort);
assert_eq!(Tables::StorageChangeSet.table_type(), TableType::Table);
}

use katana_primitives::block::{BlockHash, BlockNumber, FinalityStatus, Header};
use katana_primitives::class::{ClassHash, CompiledClass, CompiledClassHash};
use katana_primitives::contract::{ContractAddress, GenericContractInfo};
use katana_primitives::receipt::Receipt;
use katana_primitives::trace::TxExecInfo;
use katana_primitives::transaction::{InvokeTx, Tx, TxHash, TxNumber};
use starknet::macros::felt;

use crate::codecs::{Compress, Decode, Decompress, Encode};
use crate::models::block::StoredBlockBodyIndices;
use crate::models::contract::{
ContractClassChange, ContractInfoChangeList, ContractNonceChange,
};
use crate::models::storage::{
BlockList, ContractStorageEntry, ContractStorageKey, StorageEntry,
};

macro_rules! assert_key_encode_decode {
{ $( ($name:ty, $key:expr) ),* } => {
$(
{
let key: $name = $key;
let encoded = key.encode();
let decoded = <$name as Decode>::decode(encoded.as_slice()).expect("decode failed");
assert_eq!($key, decoded);
}
)*
};
}

macro_rules! assert_value_compress_decompress {
{ $( ($name:ty, $value:expr) ),* } => {
$(
{
let value: $name = $value;
let compressed = value.compress();
let decompressed = <$name as Decompress>::decompress(compressed.as_slice()).expect("decode failed");
assert_eq!($value, decompressed);
}
)*
};
}

// Test that all key/subkey types can be encoded and decoded
// through the Encode and Decode traits
#[test]
fn test_key_encode_decode() {
assert_key_encode_decode! {
(BlockNumber, 100),
(BlockHash, felt!("0x123456789")),
(TxHash, felt!("0x123456789")),
(TxNumber, 100),
(ClassHash, felt!("0x123456789")),
(ContractAddress, ContractAddress(felt!("0x123456789"))),
(ContractStorageKey, ContractStorageKey { contract_address : ContractAddress(felt!("0x123456789")), key : felt!("0x123456789")})
}
}

// Test that all value types can be compressed and decompressed
// through the Compress and Decompress traits
#[test]
fn test_value_compress_decompress() {
assert_value_compress_decompress! {
(Header, Header::default()),
(BlockHash, BlockHash::default()),
(BlockNumber, BlockNumber::default()),
(FinalityStatus, FinalityStatus::AcceptedOnL1),
(StoredBlockBodyIndices, StoredBlockBodyIndices::default()),
(TxNumber, 77),
(TxHash, felt!("0x123456789")),
(Tx, Tx::Invoke(InvokeTx::V1(Default::default()))),
(BlockNumber, 99),
(TxExecInfo, TxExecInfo::default()),
(Receipt, Receipt::Invoke(Default::default())),
(CompiledClassHash, felt!("211")),
(CompiledClass, CompiledClass::Deprecated(Default::default())),
(GenericContractInfo, GenericContractInfo::default()),
(StorageEntry, StorageEntry::default()),
(ContractInfoChangeList, ContractInfoChangeList::default()),
(ContractNonceChange, ContractNonceChange::default()),
(ContractClassChange, ContractClassChange::default()),
(BlockList, BlockList::default()),
(ContractStorageEntry, ContractStorageEntry::default())
}
}
}
Loading