Skip to content

Commit

Permalink
add tests for key value traits
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy committed Apr 5, 2024
1 parent aa365ac commit e1e5ba6
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 5 deletions.
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
88 changes: 88 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,91 @@ mod tests {
assert_eq!(Tables::StorageChangeHistory.table_type(), TableType::DupSort);
assert_eq!(Tables::StorageChangeSet.table_type(), TableType::Table);
}

use crate::{
codecs::{Compress, Decode, Decompress, Encode},
models::{
block::StoredBlockBodyIndices,
contract::{ContractClassChange, ContractInfoChangeList, ContractNonceChange},
storage::{BlockList, ContractStorageEntry, ContractStorageKey, StorageEntry},
},
};
use katana_primitives::{
block::{BlockHash, BlockNumber, FinalityStatus, Header},
class::{ClassHash, CompiledClass, CompiledClassHash},
contract::{ContractAddress, GenericContractInfo},
receipt::Receipt,
trace::TxExecInfo,
transaction::{InvokeTx, Tx, TxHash, TxNumber},
};
use starknet::macros::felt;

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())
}
}
}

0 comments on commit e1e5ba6

Please sign in to comment.