Skip to content

Commit

Permalink
chore(starknet_l1_provider): get errors ready for infra (#2435)
Browse files Browse the repository at this point in the history
- don't expose internal module `ProviderState`
- add serde
- extract scraping error, it will be added to a separate interface.

Co-Authored-By: Gilad Chase <[email protected]>
  • Loading branch information
giladchase and Gilad Chase authored Dec 9, 2024
1 parent ef39c7f commit cbd8129
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 28 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/starknet_l1_provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ license.workspace = true
[dependencies]
indexmap.workspace = true
papyrus_base_layer.workspace = true
serde.workspace = true
starknet_api.workspace = true
thiserror.workspace = true

Expand Down
22 changes: 16 additions & 6 deletions crates/starknet_l1_provider/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
use papyrus_base_layer::ethereum_base_layer_contract::EthereumBaseLayerError;
use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::ProviderState;

#[derive(Error, Debug)]
#[derive(Clone, Debug, Error, PartialEq, Eq, Serialize, Deserialize)]
pub enum L1ProviderError {
#[error(transparent)]
BaseLayer(#[from] EthereumBaseLayerError),
#[error(
"`get_txs` called while in `Pending` state, likely due to a crash; restart block proposal"
)]
GetTransactionsInPendingState,
#[error("`get_txs` while in validate state")]
GetTransactionConsensusBug,
#[error("Cannot transition from {from} to {to}")]
UnexpectedProviderStateTransition { from: ProviderState, to: ProviderState },
UnexpectedProviderStateTransition { from: String, to: String },
#[error(
"`validate` called while in `Pending` state, likely due to a crash; restart block proposal"
)]
ValidateInPendingState,
#[error("`validate` called while in `Propose`")]
ValidateTransactionConsensusBug,
}

impl L1ProviderError {
pub fn unexpected_transition(from: impl ToString, to: impl ToString) -> Self {
Self::UnexpectedProviderStateTransition { from: from.to_string(), to: to.to_string() }
}
}

// TODO(Gilad): move to scraper module once it's created.
#[derive(Error, Debug)]
pub enum L1ScraperError {
#[error(transparent)]
BaseLayer(#[from] EthereumBaseLayerError),
}
19 changes: 10 additions & 9 deletions crates/starknet_l1_provider/src/l1_provider_tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use assert_matches::assert_matches;
use pretty_assertions::assert_eq;
use starknet_api::test_utils::l1_handler::executable_l1_handler_tx;
use starknet_api::transaction::TransactionHash;
use starknet_api::{l1_handler_tx_args, tx_hash};

use crate::errors::L1ProviderError;
Expand Down Expand Up @@ -86,7 +87,7 @@ fn uninitialized_validate() {
let uninitialized_l1_provider = L1Provider::default();
assert_eq!(uninitialized_l1_provider.state, Uninitialized);

uninitialized_l1_provider.validate(Default::default()).unwrap();
uninitialized_l1_provider.validate(TransactionHash::default()).unwrap();
}

#[test]
Expand All @@ -97,13 +98,13 @@ fn proposal_start_errors() {
// Test.
l1_provider.proposal_start().unwrap();

assert_matches!(
assert_eq!(
l1_provider.proposal_start().unwrap_err(),
L1ProviderError::UnexpectedProviderStateTransition { from: Propose, to: Propose }
L1ProviderError::unexpected_transition(Propose, Propose)
);
assert_matches!(
assert_eq!(
l1_provider.validation_start().unwrap_err(),
L1ProviderError::UnexpectedProviderStateTransition { from: Propose, to: Validate }
L1ProviderError::unexpected_transition(Propose, Validate)
);
}

Expand All @@ -116,12 +117,12 @@ fn validation_start_errors() {
// Test.
l1_provider.validation_start().unwrap();

assert_matches!(
assert_eq!(
l1_provider.validation_start().unwrap_err(),
L1ProviderError::UnexpectedProviderStateTransition { from: Validate, to: Validate }
L1ProviderError::unexpected_transition(Validate, Validate)
);
assert_matches!(
assert_eq!(
l1_provider.proposal_start().unwrap_err(),
L1ProviderError::UnexpectedProviderStateTransition { from: Validate, to: Propose }
L1ProviderError::unexpected_transition(Validate, Propose)
);
}
20 changes: 7 additions & 13 deletions crates/starknet_l1_provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,41 +142,35 @@ impl TransactionManager {
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ValidationStatus {
Validated,
AlreadyIncludedOnL2,
ConsumedOnL1OrUnknown,
}

/// Current state of the provider, where pending means: idle, between proposal/validation cycles.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum ProviderState {
#[default]
Uninitialized,
Pending,
Propose,
#[default]
Uninitialized,
Validate,
}

impl ProviderState {
fn transition_to_propose(self) -> L1ProviderResult<Self> {
match self {
ProviderState::Pending => Ok(ProviderState::Propose),
_ => Err(L1ProviderError::UnexpectedProviderStateTransition {
from: self,
to: ProviderState::Propose,
}),
_ => Err(L1ProviderError::unexpected_transition(self, ProviderState::Propose)),
}
}

fn transition_to_validate(self) -> L1ProviderResult<Self> {
match self {
ProviderState::Pending => Ok(ProviderState::Validate),
_ => Err(L1ProviderError::UnexpectedProviderStateTransition {
from: self,
to: ProviderState::Validate,
}),
_ => Err(L1ProviderError::unexpected_transition(self, ProviderState::Validate)),
}
}

Expand All @@ -188,8 +182,8 @@ impl ProviderState {
match self {
ProviderState::Pending => "Pending",
ProviderState::Propose => "Propose",
ProviderState::Uninitialized => "Uninitialized",
ProviderState::Validate => "Validate",
ProviderState::Uninitialized => "Validate",
}
}
}
Expand Down

0 comments on commit cbd8129

Please sign in to comment.