Skip to content

Commit

Permalink
feat(l1_provider): implement proposal_start
Browse files Browse the repository at this point in the history
  • Loading branch information
Gilad Chase committed Nov 14, 2024
1 parent 77404ff commit 6c0a505
Show file tree
Hide file tree
Showing 5 changed files with 60 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}")]
UnexpectedProviderState { from: ProviderState, to: ProviderState },
#[error("`validate_tx` called while in proposal state")]
ValidateTransactionConsensusBug,
}
17 changes: 17 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,17 @@
use assert_matches::assert_matches;

use crate::errors::L1ProviderError::UnexpectedProviderState;
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(),
UnexpectedProviderState { from: ProviderState::Propose, to: ProviderState::Propose }
);
}
40 changes: 35 additions & 5 deletions crates/starknet_l1_provider/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
pub mod errors;

use std::fmt;

use starknet_api::executable_transaction::L1HandlerTransaction;
use starknet_api::transaction::TransactionHash;

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 +60,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 +89,7 @@ impl L1Provider {
}
}

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

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

/// Current state of the provider, where pending means: idle, between proposal/validation cycles.
#[derive(Debug, Default)]
#[derive(Debug, Default, Clone, Copy)]
pub enum ProviderState {
#[default]
Pending,
Expand All @@ -99,8 +108,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::UnexpectedProviderState {
from: self,
to: ProviderState::Propose,
}),
}
}

fn _transition_to_validate(self) -> L1ProviderResult<Self> {
Expand All @@ -110,6 +125,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 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 6c0a505

Please sign in to comment.