Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(l1_provider): implement proposal_start #2058

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
);
}
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::Display;

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(Clone, Copy, Debug, Default)]
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::UnexpectedProviderStateTransition {
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 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;
3 changes: 2 additions & 1 deletion crates/starknet_mempool/src/mempool.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::fmt::Display;

use starknet_api::block::GasPrice;
use starknet_api::core::{ContractAddress, Nonce};
Expand Down Expand Up @@ -355,7 +356,7 @@ impl TransactionReference {
}
}

impl std::fmt::Display for TransactionReference {
impl Display for TransactionReference {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let TransactionReference { address, nonce, tx_hash, tip, max_l2_gas_price } = self;
write!(
Expand Down
Loading