Skip to content

Commit

Permalink
refactor(starknet_l1_provider): extract transaction_manager into file
Browse files Browse the repository at this point in the history
Only extract + visibility change
  • Loading branch information
Gilad Chase committed Jan 9, 2025
1 parent f272b05 commit be0cec0
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 50 deletions.
4 changes: 2 additions & 2 deletions crates/starknet_l1_provider/src/l1_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use starknet_l1_provider_types::errors::L1ProviderError;
use starknet_l1_provider_types::{Event, L1ProviderResult, ValidationStatus};
use starknet_sequencer_infra::component_definitions::ComponentStarter;

use crate::{L1ProviderConfig, ProviderState, TransactionManager};

use crate::transaction_manager::TransactionManager;
use crate::{L1ProviderConfig, ProviderState};
// TODO: optimistic proposer support, will add later to keep things simple, but the design here
// is compatible with it.
#[derive(Debug, Default)]
Expand Down
50 changes: 3 additions & 47 deletions crates/starknet_l1_provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ pub mod communication;

pub mod l1_provider;
pub mod l1_scraper;
pub(crate) mod transaction_manager;

#[cfg(test)]
pub mod test_utils;

use std::collections::BTreeMap;
use std::time::Duration;

use indexmap::{IndexMap, IndexSet};
use papyrus_base_layer::constants::{
EventIdentifier,
CONSUMED_MESSAGE_TO_L1_EVENT_IDENTIFIER,
Expand All @@ -20,59 +21,14 @@ use papyrus_config::converters::deserialize_milliseconds_to_duration;
use papyrus_config::dumping::{ser_param, SerializeConfig};
use papyrus_config::{ParamPath, ParamPrivacyInput, SerializedParam};
use serde::{Deserialize, Serialize};
use starknet_api::executable_transaction::L1HandlerTransaction;
use starknet_api::transaction::TransactionHash;
use starknet_l1_provider_types::errors::L1ProviderError;
use starknet_l1_provider_types::{L1ProviderResult, ValidationStatus};
use starknet_l1_provider_types::L1ProviderResult;
use validator::Validate;

#[cfg(test)]
#[path = "l1_provider_tests.rs"]
pub mod l1_provider_tests;

#[derive(Debug, Default)]
struct TransactionManager {
txs: IndexMap<TransactionHash, L1HandlerTransaction>,
proposed_txs: IndexSet<TransactionHash>,
on_l2_awaiting_l1_consumption: IndexSet<TransactionHash>,
}

impl TransactionManager {
pub fn get_txs(&mut self, n_txs: usize) -> Vec<L1HandlerTransaction> {
let (tx_hashes, txs): (Vec<_>, Vec<_>) = self
.txs
.iter()
.skip(self.proposed_txs.len()) // Transactions are proposed FIFO.
.take(n_txs)
.map(|(&hash, tx)| (hash, tx.clone()))
.unzip();

self.proposed_txs.extend(tx_hashes);
txs
}

pub fn tx_status(&self, tx_hash: TransactionHash) -> ValidationStatus {
if self.txs.contains_key(&tx_hash) {
ValidationStatus::Validated
} else if self.on_l2_awaiting_l1_consumption.contains(&tx_hash) {
ValidationStatus::AlreadyIncludedOnL2
} else {
ValidationStatus::ConsumedOnL1OrUnknown
}
}

pub fn _add_unconsumed_l1_not_in_l2_block_tx(&mut self, _tx: L1HandlerTransaction) {
todo!(
"Check if tx is in L2, if it isn't on L2 add it to the txs buffer, otherwise print
debug and do nothing."
)
}

pub fn _mark_tx_included_on_l2(&mut self, _tx_hash: &TransactionHash) {
todo!("Adds the tx hash to l2 buffer; remove tx from the txs storage if it's there.")
}
}

/// Current state of the provider, where pending means: idle, between proposal/validation cycles.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum ProviderState {
Expand Down
3 changes: 2 additions & 1 deletion crates/starknet_l1_provider/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use starknet_api::executable_transaction::L1HandlerTransaction;
use starknet_api::transaction::TransactionHash;

use crate::l1_provider::L1Provider;
use crate::{ProviderState, TransactionManager};
use crate::transaction_manager::TransactionManager;
use crate::ProviderState;
// Represents the internal content of the L1 provider for testing.
// Enables customized (and potentially inconsistent) creation for unit testing.
#[derive(Debug, Default)]
Expand Down
47 changes: 47 additions & 0 deletions crates/starknet_l1_provider/src/transaction_manager.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use indexmap::{IndexMap, IndexSet};
use starknet_api::executable_transaction::L1HandlerTransaction;
use starknet_api::transaction::TransactionHash;
use starknet_l1_provider_types::ValidationStatus;

#[derive(Debug, Default)]
pub struct TransactionManager {
pub txs: IndexMap<TransactionHash, L1HandlerTransaction>,
pub proposed_txs: IndexSet<TransactionHash>,
pub on_l2_awaiting_l1_consumption: IndexSet<TransactionHash>,
}

impl TransactionManager {
pub fn get_txs(&mut self, n_txs: usize) -> Vec<L1HandlerTransaction> {
let (tx_hashes, txs): (Vec<_>, Vec<_>) = self
.txs
.iter()
.skip(self.proposed_txs.len()) // Transactions are proposed FIFO.
.take(n_txs)
.map(|(&hash, tx)| (hash, tx.clone()))
.unzip();

self.proposed_txs.extend(tx_hashes);
txs
}

pub fn tx_status(&self, tx_hash: TransactionHash) -> ValidationStatus {
if self.txs.contains_key(&tx_hash) {
ValidationStatus::Validated
} else if self.on_l2_awaiting_l1_consumption.contains(&tx_hash) {
ValidationStatus::AlreadyIncludedOnL2
} else {
ValidationStatus::ConsumedOnL1OrUnknown
}
}

pub fn _add_unconsumed_l1_not_in_l2_block_tx(&mut self, _tx: L1HandlerTransaction) {
todo!(
"Check if tx is in L2, if it isn't on L2 add it to the txs buffer, otherwise print
debug and do nothing."
)
}

pub fn _mark_tx_included_on_l2(&mut self, _tx_hash: &TransactionHash) {
todo!("Adds the tx hash to l2 buffer; remove tx from the txs storage if it's there.")
}
}

0 comments on commit be0cec0

Please sign in to comment.