Skip to content

Commit

Permalink
feat(katana-provider): implement a DB provider (#1299)
Browse files Browse the repository at this point in the history
* wip

* wip

* add test for de/compress contact class

* wip

* update

* update

* update test

* impl state update provider + tests

* update

* increase code coverage
  • Loading branch information
kariy authored Dec 20, 2023
1 parent cb2e3be commit 490dab0
Show file tree
Hide file tree
Showing 23 changed files with 2,225 additions and 559 deletions.
55 changes: 23 additions & 32 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/katana/primitives/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub struct Block {
}

/// A block with only the transaction hashes.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BlockWithTxHashes {
pub header: Header,
pub body: Vec<TxHash>,
Expand Down
2 changes: 1 addition & 1 deletion crates/katana/primitives/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::contract::{
/// State updates.
///
/// Represents all the state updates after performing some executions on a state.
#[derive(Debug, Default, Clone)]
#[derive(Debug, Default, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct StateUpdates {
/// A mapping of contract addresses to their updated nonces.
Expand Down
7 changes: 5 additions & 2 deletions crates/katana/storage/db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ version.workspace = true
katana-primitives = { path = "../../primitives" }

anyhow.workspace = true
bincode = "1.3.3"
page_size = "0.6.0"
parking_lot.workspace = true
serde.workspace = true
Expand All @@ -23,7 +22,11 @@ cairo-vm.workspace = true
starknet_api.workspace = true

# codecs
postcard = { version = "1.0.8", optional = true, default-features = false, features = [ "use-std" ] }
[dependencies.postcard]
default-features = false
features = [ "use-std" ]
optional = true
version = "1.0.8"

[dependencies.libmdbx]
git = "https://github.com/paradigmxyz/reth.git"
Expand Down
2 changes: 1 addition & 1 deletion crates/katana/storage/db/benches/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use blockifier::execution::contract_class::ContractClassV1;
use cairo_lang_starknet::casm_contract_class::CasmContractClass;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use katana_db::codecs::{Compress, Decompress};
use katana_db::models::contract::StoredContractClass;
use katana_db::models::class::StoredContractClass;
use katana_primitives::contract::CompiledContractClass;

fn compress_contract(contract: CompiledContractClass) -> Vec<u8> {
Expand Down
11 changes: 7 additions & 4 deletions crates/katana/storage/db/src/codecs/postcard.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use katana_primitives::block::Header;
use katana_primitives::block::{BlockNumber, Header};
use katana_primitives::contract::{ContractAddress, GenericContractInfo, SierraClass};
use katana_primitives::receipt::Receipt;
use katana_primitives::transaction::Tx;
Expand All @@ -7,7 +7,8 @@ use katana_primitives::FieldElement;
use super::{Compress, Decompress};
use crate::error::CodecError;
use crate::models::block::StoredBlockBodyIndices;
use crate::models::contract::StoredContractClass;
use crate::models::class::StoredContractClass;
use crate::models::contract::ContractInfoChangeList;

macro_rules! impl_compress_and_decompress_for_table_values {
($($name:ty),*) => {
Expand All @@ -21,7 +22,7 @@ macro_rules! impl_compress_and_decompress_for_table_values {

impl Decompress for $name {
fn decompress<B: AsRef<[u8]>>(bytes: B) -> Result<Self, crate::error::CodecError> {
postcard::from_bytes(bytes.as_ref()).map_err(|e| CodecError::Decode(e.to_string()))
postcard::from_bytes(bytes.as_ref()).map_err(|e| CodecError::Decompress(e.to_string()))
}
}
)*
Expand All @@ -36,7 +37,9 @@ impl_compress_and_decompress_for_table_values!(
SierraClass,
FieldElement,
ContractAddress,
Vec<BlockNumber>,
StoredContractClass,
GenericContractInfo,
StoredBlockBodyIndices
StoredBlockBodyIndices,
ContractInfoChangeList
);
13 changes: 10 additions & 3 deletions crates/katana/storage/db/src/mdbx/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl<K: TransactionKind, T: DupSort> Cursor<K, T> {
&mut self,
key: Option<T::Key>,
subkey: Option<T::SubKey>,
) -> Result<DupWalker<'_, K, T>, DatabaseError> {
) -> Result<Option<DupWalker<'_, K, T>>, DatabaseError> {
let start = match (key, subkey) {
(Some(key), Some(subkey)) => {
// encode key and decode it after.
Expand All @@ -154,10 +154,17 @@ impl<K: TransactionKind, T: DupSort> Cursor<K, T> {

(Some(key), None) => {
let key: Vec<u8> = key.encode().into();
self.inner

let Some(start) = self
.inner
.set(key.as_ref())
.map_err(DatabaseError::Read)?
.map(|val| decoder::<T>((Cow::Owned(key), val)))
else {
return Ok(None);
};

Some(start)
}

(None, Some(subkey)) => {
Expand All @@ -175,7 +182,7 @@ impl<K: TransactionKind, T: DupSort> Cursor<K, T> {
(None, None) => self.first().transpose(),
};

Ok(DupWalker::new(self, start))
Ok(Some(DupWalker::new(self, start)))
}
}

Expand Down
9 changes: 4 additions & 5 deletions crates/katana/storage/db/src/mdbx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,17 @@ impl DbEnv {
#[cfg(any(test, feature = "test-utils"))]
pub mod test_utils {
use std::path::Path;
use std::sync::Arc;

use super::{DbEnv, DbEnvKind};

const ERROR_DB_CREATION: &str = "Not able to create the mdbx file.";

/// Create database for testing
pub fn create_test_db(kind: DbEnvKind) -> Arc<DbEnv> {
Arc::new(create_test_db_with_path(
pub fn create_test_db(kind: DbEnvKind) -> DbEnv {
create_test_db_with_path(
kind,
&tempfile::TempDir::new().expect("Failed to create temp dir.").into_path(),
))
)
}

/// Create database for testing with specified path
Expand Down Expand Up @@ -392,7 +391,7 @@ mod tests {
{
let tx = env.tx().expect(ERROR_INIT_TX);
let mut cursor = tx.cursor::<ContractStorage>().unwrap();
let mut walker = cursor.walk_dup(Some(key), Some(felt!("1"))).unwrap();
let mut walker = cursor.walk_dup(Some(key), Some(felt!("1"))).unwrap().unwrap();

assert_eq!(
(key, value11),
Expand Down
Loading

0 comments on commit 490dab0

Please sign in to comment.