Skip to content

Commit

Permalink
feat(l1_provider): implement proposal_start (#2058)
Browse files Browse the repository at this point in the history
Co-Authored-By: Gilad Chase <gilad@starkware.com>
  • Loading branch information
giladchase and Gilad Chase authored Nov 17, 2024
1 parent a29daf1 commit 3ff0dcf
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 7 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.

3 changes: 3 additions & 0 deletions crates/starknet_l1_provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ papyrus_base_layer.workspace = true
starknet_api.workspace = true
thiserror.workspace = true

[dev-dependencies]
assert_matches.workspace = true

[lints]
workspace = true
6 changes: 4 additions & 2 deletions crates/starknet_l1_provider/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use papyrus_base_layer::ethereum_base_layer_contract::EthereumBaseLayerError;
use thiserror::Error;

use crate::ProviderState;

#[derive(Error, Debug)]
pub enum L1ProviderError {
#[error(transparent)]
Expand All @@ -11,8 +13,8 @@ pub enum L1ProviderError {
GetTransactionsInPendingState,
#[error("`get_txs` while in validate state")]
GetTransactionConsensusBug,
#[error("Can not set state: {0}")]
UnexpectedState(String),
#[error("Cannot transition from {from} to {to}")]
UnexpectedProviderStateTransition { from: ProviderState, to: ProviderState },
#[error("`validate_tx` called while in proposal state")]
ValidateTransactionConsensusBug,
}
20 changes: 20 additions & 0 deletions crates/starknet_l1_provider/src/l1_provider_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use assert_matches::assert_matches;

use crate::errors::L1ProviderError::UnexpectedProviderStateTransition;
use crate::{L1Provider, ProviderState};

#[test]
fn proposal_start_errors() {
// Setup.
let mut l1_provider = L1Provider::default();

// Test.
l1_provider.proposal_start().unwrap();
assert_matches!(
l1_provider.proposal_start().unwrap_err(),
UnexpectedProviderStateTransition {
from: ProviderState::Propose,
to: ProviderState::Propose
}
);
}
38 changes: 33 additions & 5 deletions crates/starknet_l1_provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ use crate::errors::L1ProviderError;

type L1ProviderResult<T> = Result<T, L1ProviderError>;

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

// TODO: optimistic proposer support, will add later to keep things simple, but the design here
// is compatible with it.
#[derive(Debug, Default)]
pub struct L1Provider {
unconsumed_l1_not_in_l2_block_txs: PendingMessagesFromL1,
state: ProviderState,
Expand Down Expand Up @@ -53,8 +58,9 @@ impl L1Provider {
todo!("Sets internal state as validate, returns error if state is Pending.")
}

pub fn proposal_start(&mut self) {
todo!("Similar to validation_start.")
pub fn proposal_start(&mut self) -> L1ProviderResult<()> {
self.state = self.state.transition_to_propose()?;
Ok(())
}

/// Simple recovery from L1 and L2 reorgs by reseting the service, which rewinds L1 and L2
Expand All @@ -81,6 +87,7 @@ impl L1Provider {
}
}

#[derive(Debug, Default)]
struct PendingMessagesFromL1;

impl PendingMessagesFromL1 {
Expand All @@ -90,7 +97,7 @@ impl PendingMessagesFromL1 {
}

/// Current state of the provider, where pending means: idle, between proposal/validation cycles.
#[derive(Debug, Default)]
#[derive(Clone, Copy, Debug, Default)]
pub enum ProviderState {
#[default]
Pending,
Expand All @@ -99,8 +106,14 @@ pub enum ProviderState {
}

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

fn _transition_to_validate(self) -> L1ProviderResult<Self> {
Expand All @@ -110,6 +123,21 @@ impl ProviderState {
fn _transition_to_pending(self) -> L1ProviderResult<Self> {
todo!()
}

pub fn as_str(&self) -> &str {
match self {
ProviderState::Pending => "Pending",
ProviderState::Propose => "Propose",
ProviderState::Validate => "Validate",
}
}
}

impl std::fmt::Display for ProviderState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}

#[derive(Debug)]
pub struct L1ProviderConfig;

0 comments on commit 3ff0dcf

Please sign in to comment.