Skip to content

Commit

Permalink
feat(katana): implement struct for in memory provider
Browse files Browse the repository at this point in the history
  • Loading branch information
ybensacq committed Sep 19, 2024
1 parent 18a58d5 commit 4f19ab6
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 96 deletions.
6 changes: 3 additions & 3 deletions crates/katana/core/src/backend/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use katana_provider::providers::db::DbProvider;
use katana_provider::traits::block::{BlockProvider, BlockWriter};
use katana_provider::traits::contract::ContractClassWriter;
use katana_provider::traits::env::BlockEnvProvider;
use katana_provider::traits::messaging::MessagingProvider;
use katana_provider::traits::messaging::MessagingCheckpointProvider;
use katana_provider::traits::state::{StateFactoryProvider, StateRootProvider, StateWriter};
use katana_provider::traits::state_update::StateUpdateProvider;
use katana_provider::traits::transaction::{
Expand All @@ -32,7 +32,7 @@ pub trait Database:
+ ContractClassWriter
+ StateFactoryProvider
+ BlockEnvProvider
+ MessagingProvider
+ MessagingCheckpointProvider
+ 'static
+ Send
+ Sync
Expand All @@ -53,7 +53,7 @@ impl<T> Database for T where
+ ContractClassWriter
+ StateFactoryProvider
+ BlockEnvProvider
+ MessagingProvider
+ MessagingCheckpointProvider
+ 'static
+ Send
+ Sync
Expand Down
2 changes: 1 addition & 1 deletion crates/katana/core/src/service/block_producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use katana_primitives::transaction::{ExecutableTxWithHash, TxWithHash, Tx};
use katana_primitives::version::CURRENT_STARKNET_VERSION;
use katana_primitives::FieldElement;
use katana_provider::error::ProviderError;
use katana_provider::traits::messaging::MessagingProvider;
use katana_provider::traits::messaging::MessagingCheckpointProvider;
use katana_provider::traits::block::{BlockHashProvider, BlockNumberProvider};
use katana_provider::traits::env::BlockEnvProvider;
use katana_provider::traits::state::StateFactoryProvider;
Expand Down
7 changes: 5 additions & 2 deletions crates/katana/core/src/service/messaging/starknet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,15 @@ impl Messenger for StarknetMessaging {
};
}

self.send_hashes(hashes.clone()).await?;
for (index, hash) in hashes.iter().enumerate() {
let stored_index = self.provider.get_send_from_index();
self.send_hashes(std::slice::from_ref(hash)).await?;
self.provider.set_send_from_index(*hash, index as u64).await?;
self.provider.set_send_from_index((index as u64) + 1).await?;
}

// reset the index
self.provider.set_send_from_index(0).await?;

Ok(hashes)
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/katana/primitives/src/genesis/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ pub struct GenesisJson {
pub accounts: HashMap<ContractAddress, GenesisAccountJson>,
#[serde(default)]
pub contracts: HashMap<ContractAddress, GenesisContractJson>,
pub gather_from_block: BlockNumber,
pub settlement_block_number: BlockNumber,
}

impl GenesisJson {
Expand Down Expand Up @@ -473,7 +473,7 @@ impl TryFrom<GenesisJson> for Genesis {
gas_prices: value.gas_prices,
state_root: value.state_root,
parent_hash: value.parent_hash,
gather_from_block: value.gather_from_block,
settlement_block_number: value.settlement_block_number,
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/katana/primitives/src/genesis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub struct Genesis {
/// The genesis contract allocations.
pub allocations: BTreeMap<ContractAddress, GenesisAllocation>,
/// The block on settlement chain from where Katana will start fetching messages.
pub gather_from_block: BlockNumber,
pub settlement_block_number: BlockNumber,
}

impl Genesis {
Expand Down Expand Up @@ -297,7 +297,7 @@ impl Default for Genesis {
allocations: BTreeMap::new(),
fee_token,
universal_deployer: Some(universal_deployer),
gather_from_block: 0,
settlement_block_number: 0,
}
}
}
Expand Down
37 changes: 37 additions & 0 deletions crates/katana/storage/db/src/models/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,43 @@ impl Decompress for ContractStorageEntry {
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MessagingCheckpointId {
SendBlock,
SendIndex,
GatherBlock,
GatherNonce,
}


impl Encode for MessagingCheckpointId {
type Encoded = [u8; 1];
fn encode(self) -> Self::Encoded {
let mut buf = [0u8; 1];
buf[0] = match self {
MessagingCheckpointId::SendBlock => 1,
MessagingCheckpointId::SendIndex => 2,
MessagingCheckpointId::GatherBlock => 3,
MessagingCheckpointId::GatherNonce => 4,
};
buf
}
}

impl Decode for MessagingCheckpointId {
fn decode<B: AsRef<[u8]>>(bytes: B) -> Result<Self, CodecError> {
let bytes = bytes.as_ref();
match bytes[0] {
1 => Ok(MessagingCheckpointId::SendBlock),
2 => Ok(MessagingCheckpointId::SendIndex),
3 => Ok(MessagingCheckpointId::GatherBlock),
4 => Ok(MessagingCheckpointId::GatherNonce),
_ => Err(CodecError::Decode("Invalid MessagingCheckpointId".into())),
}
}
}


#[cfg(test)]
mod tests {
use starknet::macros::felt;
Expand Down
29 changes: 15 additions & 14 deletions crates/katana/storage/db/src/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::codecs::{Compress, Decode, Decompress, Encode};
use crate::models::block::StoredBlockBodyIndices;
use crate::models::contract::{ContractClassChange, ContractInfoChangeList, ContractNonceChange};
use crate::models::list::BlockList;
use crate::models::storage::{ContractStorageEntry, ContractStorageKey, StorageEntry};
use crate::models::storage::{ContractStorageEntry, ContractStorageKey, MessagingCheckpointId, StorageEntry};

pub trait Key: Encode + Decode + Clone + std::fmt::Debug {}
pub trait Value: Compress + Decompress + std::fmt::Debug {}
Expand Down Expand Up @@ -168,10 +168,10 @@ define_tables_enum! {[
(ClassChangeHistory, TableType::DupSort),
(StorageChangeHistory, TableType::DupSort),
(StorageChangeSet, TableType::Table),
(MessagingInfo, TableType::Table),
(MessagingNonceInfo, TableType::Table),
(MessagingCheckpointBlock, TableType::Table),
(MessagingCheckpointNonce, TableType::Table),
(MessagingMessageNonceMapping, TableType::Table),
(MessagingIndexInfo, TableType::Table),
(MessagingCheckpointIndex, TableType::Table)
]}

tables! {
Expand Down Expand Up @@ -230,16 +230,16 @@ tables! {
StorageChangeHistory: (BlockNumber, ContractStorageKey) => ContractStorageEntry,

/// Stores the block number related to messaging service
MessagingInfo: (u64) => BlockNumber,
MessagingCheckpointBlock: (MessagingCheckpointId) => BlockNumber,

/// Stores the nonce related to messaging service
MessagingNonceInfo: (u64) => Nonce,
MessagingCheckpointNonce: (MessagingCheckpointId) => Nonce,

/// Map a message hash to a message nonce
MessagingMessageNonceMapping: (TxHash) => Nonce,

/// Stores the index of the messaging service
MessagingIndexInfo: (u64) => u64,
MessagingCheckpointIndex: (MessagingCheckpointId) => u64

}

Expand Down Expand Up @@ -274,10 +274,10 @@ mod tests {
assert_eq!(Tables::ALL[20].name(), ClassChangeHistory::NAME);
assert_eq!(Tables::ALL[21].name(), StorageChangeHistory::NAME);
assert_eq!(Tables::ALL[22].name(), StorageChangeSet::NAME);
assert_eq!(Tables::ALL[23].name(), MessagingInfo::NAME);
assert_eq!(Tables::ALL[24].name(), MessagingNonceInfo::NAME);
assert_eq!(Tables::ALL[23].name(), MessagingCheckpointBlock::NAME);
assert_eq!(Tables::ALL[24].name(), MessagingCheckpointNonce::NAME);
assert_eq!(Tables::ALL[25].name(), MessagingMessageNonceMapping::NAME);
assert_eq!(Tables::ALL[26].name(), MessagingMessageNonceMapping::NAME);
assert_eq!(Tables::ALL[26].name(), MessagingCheckpointIndex::NAME);

assert_eq!(Tables::Headers.table_type(), TableType::Table);
assert_eq!(Tables::BlockHashes.table_type(), TableType::Table);
Expand All @@ -302,10 +302,10 @@ mod tests {
assert_eq!(Tables::ClassChangeHistory.table_type(), TableType::DupSort);
assert_eq!(Tables::StorageChangeHistory.table_type(), TableType::DupSort);
assert_eq!(Tables::StorageChangeSet.table_type(), TableType::Table);
assert_eq!(Tables::MessagingInfo.table_type(), TableType::Table);
assert_eq!(Tables::MessagingNonceInfo.table_type(), TableType::Table);
assert_eq!(Tables::MessagingMessageNonMapping.table_type(), TableType::Table);
assert_eq!(Tables::MessagingIndexInfo.table_type(), TableType::Table);
assert_eq!(Tables::MessagingCheckpointBlock.table_type(), TableType::Table);
assert_eq!(Tables::MessagingCheckpointNonce.table_type(), TableType::Table);
assert_eq!(Tables::MessagingMessageNonceMapping.table_type(), TableType::Table);
assert_eq!(Tables::MessagingCheckpointIndex.table_type(), TableType::Table);
}

use katana_primitives::block::{BlockHash, BlockNumber, FinalityStatus, Header};
Expand All @@ -323,6 +323,7 @@ mod tests {
};
use crate::models::list::BlockList;
use crate::models::storage::{ContractStorageEntry, ContractStorageKey, StorageEntry};
use crate::tables::Tables::{MessagingCheckpointBlock, MessagingCheckpointNonce};

macro_rules! assert_key_encode_decode {
{ $( ($name:ty, $key:expr) ),* } => {
Expand Down
10 changes: 5 additions & 5 deletions crates/katana/storage/provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use katana_primitives::FieldElement;
use traits::block::{BlockIdReader, BlockStatusProvider, BlockWriter};
use traits::contract::{ContractClassProvider, ContractClassWriter};
use traits::env::BlockEnvProvider;
use traits::messaging::MessagingProvider;
use traits::messaging::MessagingCheckpointProvider;
use traits::state::{StateRootProvider, StateWriter};
use traits::transaction::{TransactionStatusProvider, TransactionTraceProvider};

Expand Down Expand Up @@ -381,9 +381,9 @@ where
}
}

impl<Db> MessagingProvider for BlockchainProvider<Db>
impl<Db> MessagingCheckpointProvider for BlockchainProvider<Db>
where
Db: MessagingProvider,
Db: MessagingCheckpointProvider,
{
fn get_send_from_block(&self) -> ProviderResult<Option<BlockNumber>> {
self.provider.get_send_from_block()
Expand Down Expand Up @@ -421,7 +421,7 @@ where
self.provider.get_send_from_index()
}

fn set_send_from_index(&self, _send_from_index: u64) -> ProviderResult<()> {
self.provider.get_send_from_index(send_from_index)
fn set_send_from_index(&self, send_from_index: u64) -> ProviderResult<()> {
self.provider.set_send_from_index(send_from_index)
}
}
44 changes: 22 additions & 22 deletions crates/katana/storage/provider/src/providers/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use katana_db::models::contract::{
ContractClassChange, ContractInfoChangeList, ContractNonceChange,
};
use katana_db::models::list::BlockList;
use katana_db::models::storage::{ContractStorageEntry, ContractStorageKey, StorageEntry};
use katana_db::models::storage::{ContractStorageEntry, ContractStorageKey, MessagingCheckpointId, StorageEntry};
use katana_db::tables::{self, DupSort, Table};
use katana_db::utils::KeyValue;
use katana_primitives::block::{
Expand All @@ -35,7 +35,7 @@ use crate::traits::block::{
HeaderProvider,
};
use crate::traits::env::BlockEnvProvider;
use crate::traits::messaging::{MessagingProvider, GATHER_FROM_BLOCK_KEY, SEND_FROM_BLOCK_KEY, GATHER_FROM_NONCE_KEY, SEND_FROM_INDEX_KEY};
use crate::traits::messaging::MessagingCheckpointProvider;
use crate::traits::state::{StateFactoryProvider, StateProvider, StateRootProvider};
use crate::traits::state_update::StateUpdateProvider;
use crate::traits::transaction::{
Expand Down Expand Up @@ -747,45 +747,52 @@ impl BlockWriter for DbProvider {
}
}

impl MessagingProvider for DbProvider {
impl MessagingCheckpointProvider for DbProvider {
fn set_send_from_block(&self, send_from_block: BlockNumber) -> ProviderResult<()> {
self.0.update(|db_tx| {
db_tx.put::<tables::MessagingCheckpointBlock>(MessagingCheckpointId::SendBlock, send_from_block)?;
Ok(())
})?
}

fn get_send_from_block(&self) -> ProviderResult<Option<BlockNumber>> {
let db_tx = self.0.tx()?;
let block_num = db_tx.get::<tables::MessagingInfo>(SEND_FROM_BLOCK_KEY)?;
let block_num = db_tx.get::<tables::MessagingCheckpointBlock>(MessagingCheckpointId::SendBlock)?;
db_tx.commit()?;
Ok(block_num)
}

fn set_send_from_block(&self, send_from_block: BlockNumber) -> ProviderResult<()> {
fn set_gather_from_block(&self, gather_from_block: BlockNumber) -> ProviderResult<()> {
self.0.update(|db_tx| {
db_tx.put::<tables::MessagingInfo>(SEND_FROM_BLOCK_KEY, send_from_block)?;
db_tx.put::<tables::MessagingCheckpointBlock>(MessagingCheckpointId::GatherBlock, gather_from_block)?;
Ok(())
})?
}

fn get_gather_from_block(&self) -> ProviderResult<Option<BlockNumber>> {
let db_tx = self.0.tx()?;
let block_num = db_tx.get::<tables::MessagingInfo>(GATHER_FROM_BLOCK_KEY)?;
let block_num = db_tx.get::<tables::MessagingCheckpointBlock>(MessagingCheckpointId::GatherBlock)?;
db_tx.commit()?;
Ok(block_num)
}

fn set_gather_from_block(&self, gather_from_block: BlockNumber) -> ProviderResult<()> {
fn set_gather_message_nonce(&self, nonce: Nonce) -> ProviderResult<()> {
self.0.update(|db_tx| {
db_tx.put::<tables::MessagingInfo>(GATHER_FROM_BLOCK_KEY, gather_from_block)?;
db_tx.put::<tables::MessagingCheckpointNonce>(MessagingCheckpointId::GatherNonce, nonce)?;
Ok(())
})?
}

fn get_gather_message_nonce(&self) -> ProviderResult<Option<Nonce>> {
let db_tx = self.0.tx()?;
let nonce = db_tx.get::<tables::MessagingNonceInfo>(GATHER_FROM_NONCE_KEY)?;
let nonce = db_tx.get::<tables::MessagingCheckpointNonce>(MessagingCheckpointId::GatherNonce)?;
db_tx.commit()?;
Ok(nonce)
}

fn set_gather_message_nonce(&self, nonce: Nonce) -> ProviderResult<()> {
fn set_nonce_from_message_hash(&self, message_hash: MessageHash, nonce: Nonce) -> ProviderResult<()> {
self.0.update(|db_tx| {
db_tx.put::<tables::MessagingNonceInfo>(GATHER_FROM_NONCE_KEY, nonce)?;
db_tx.put::<tables::MessagingMessageNonceMapping>(message_hash, nonce)?;
Ok(())
})?
}
Expand All @@ -797,26 +804,19 @@ impl MessagingProvider for DbProvider {
Ok(nonce)
}

fn set_nonce_from_message_hash(&self, message_hash: MessageHash, nonce: Nonce) -> ProviderResult<()> {
fn set_send_from_index(&self, send_from_index: u64) -> ProviderResult<()> {
self.0.update(|db_tx| {
db_tx.put::<tables::MessagingMessageNonceMapping>(message_hash, nonce)?;
db_tx.put::<tables::MessagingCheckpointIndex>(MessagingCheckpointId::SendIndex, send_from_index)?;
Ok(())
})?
}

fn get_send_from_index(&self) -> ProviderResult<Option<u64>> {
let db_tx = self.0.tx()?;
let index = db_tx.get::<tables::MessagingIndexInfo>(SEND_FROM_INDEX_KEY)?;
let index = db_tx.get::<tables::MessagingCheckpointIndex>(MessagingCheckpointId::SendIndex)?;
db_tx.commit()?;
Ok(index)
}

fn set_send_from_index(&self, send_from_index: u64) -> ProviderResult<()> {
self.0.update(|db_tx| {
db_tx.put::<tables::MessagingIndexInfo>(SEND_FROM_INDEX_KEY, send_from_index)?;
Ok(())
})?
}
}

#[cfg(test)]
Expand Down
Loading

0 comments on commit 4f19ab6

Please sign in to comment.