Skip to content
Merged
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
11 changes: 11 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
members = [
"crates/btcio",
"crates/chaintsn",
"crates/common",
"crates/consensus-logic",
"crates/db",
Expand Down Expand Up @@ -45,6 +46,7 @@ alpen-express-rpc-api = { path = "crates/rpc/api" }
alpen-express-rpc-types = { path = "crates/rpc/types" }
alpen-express-state = { path = "crates/state" }
alpen-test-utils = { path = "crates/test-utils" }
express-chaintsn = { path = "crates/chaintsn" }
express-reth-db = { path = "crates/reth/db" }
express-reth-exex = { path = "crates/reth/exex" }
express-reth-rpc = { path = "crates/reth/rpc" }
Expand Down
15 changes: 15 additions & 0 deletions crates/chaintsn/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "express-chaintsn"
version = "0.1.0"
edition = "2021"

[dependencies]
alpen-express-primitives = { workspace = true }
alpen-express-state = { workspace = true }

thiserror = { workspace = true }
tracing = { workspace = true, optional = true }

[features]
default = ["fullstd"]
fullstd = ["dep:tracing"]
10 changes: 10 additions & 0 deletions crates/chaintsn/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# `express-chaintsn`

Contains core implementation of the rollup chain state transition.

This is meant to avoid deps on std that aren't available in risc0 or sp1 envs,
either directly or through crates.

## Features

* `fullstd` - enabled when we are building for running in a normal client with tracing
22 changes: 22 additions & 0 deletions crates/chaintsn/src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use alpen_express_state::prelude::*;

use thiserror::Error;

/// Errors for block state transition.
#[derive(Debug, Error)]
pub enum TsnError {
#[error("skipped a block")]
SkippedBlock,

#[error("mismatch parent (head {0:?}, parent {1:?}")]
MismatchParent(L2BlockId, L2BlockId),

#[error("attested mismatched ID for {0} (set {1}, computed {2})")]
L1BlockIdMismatch(u64, L1BlockId, L1BlockId),

#[error("parent link at L1 block {0} incorrect (set parent {1}, found block {2})")]
L1BlockParentMismatch(u64, L1BlockId, L1BlockId),

#[error("L1 segment block did not extend the chain tip")]
L1SegNotExtend,
}
6 changes: 6 additions & 0 deletions crates/chaintsn/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//! Rollup chain state transition.

pub mod errors;
pub mod transition;

pub mod macros;
1 change: 1 addition & 0 deletions crates/chaintsn/src/macros/macros_nostd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// TODO implement stubs of tracing macros
1 change: 1 addition & 0 deletions crates/chaintsn/src/macros/macros_std.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub use tracing::*;
11 changes: 11 additions & 0 deletions crates/chaintsn/src/macros/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#[cfg(feature = "std")]
mod macros_std;

#[cfg(feature = "std")]
pub use macros_std::*;

#[cfg(not(feature = "std"))]
mod macros_nostd;

#[cfg(not(feature = "std"))]
pub use macros_nostd::*;
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
//! we'll replace components with real implementations as we go along.
#![allow(unused)]

use tracing::*;

use alpen_express_primitives::params::RollupParams;
use alpen_express_state::{
block::L1Segment,
Expand All @@ -15,6 +13,7 @@ use alpen_express_state::{
};

use crate::errors::TsnError;
use crate::macros::*;

/// Processes a block, making writes into the provided state cache that will
/// then be written to disk. This does not check the block's credentials, it
Expand Down Expand Up @@ -77,7 +76,7 @@ fn process_l1_view_update(
// we need to do a reorg.
// FIXME this should actually check PoW, it just does it based on block heights
if !l1seg.new_payloads().is_empty() {
println!("new payloads {:?}", l1seg.new_payloads());
trace!("new payloads {:?}", l1seg.new_payloads());

// Validate the new blocks actually extend the tip. This is what we have to tweak to make
// more complicated to check the PoW.
Expand Down
1 change: 1 addition & 0 deletions crates/consensus-logic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ alpen-express-db = { workspace = true }
alpen-express-eectl = { workspace = true }
alpen-express-primitives = { workspace = true }
alpen-express-state = { workspace = true }
express-chaintsn = { workspace = true }

anyhow = { workspace = true }
bitcoin = { workspace = true }
Expand Down
5 changes: 2 additions & 3 deletions crates/consensus-logic/src/duty/block_assembly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ use alpen_express_state::header::L2BlockHeader;
use alpen_express_state::prelude::*;
use alpen_express_state::state_op::*;

use super::types::IdentityKey;
use crate::chain_transition;
use super::types::*;
use crate::credential::sign_schnorr_sig;
use crate::errors::Error;

Expand Down Expand Up @@ -349,7 +348,7 @@ fn compute_post_state(
params: &Arc<Params>,
) -> Result<(ChainState, WriteBatch), Error> {
let mut state_cache = StateCache::new(prev_chstate);
chain_transition::process_block(&mut state_cache, header, body, params.rollup())?;
express_chaintsn::transition::process_block(&mut state_cache, header, body, params.rollup())?;
let (post_state, wb) = state_cache.finalize();
Ok((post_state, wb))
}
Expand Down
20 changes: 1 addition & 19 deletions crates/consensus-logic/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use thiserror::Error;

use alpen_express_state::id::L2BlockId;
use alpen_express_state::l1::L1BlockId;
use express_chaintsn::errors::TsnError;

#[derive(Debug, Error)]
pub enum Error {
Expand Down Expand Up @@ -87,22 +88,3 @@ pub enum ChainTipError {
#[error("tried to finalize unknown block {0:?}")]
MissingBlock(L2BlockId),
}

/// Error with a block state transition.
#[derive(Debug, Error)]
pub enum TsnError {
#[error("skipped a block")]
SkippedBlock,

#[error("mismatch parent (head {0:?}, parent {1:?}")]
MismatchParent(L2BlockId, L2BlockId),

#[error("attested mismatched ID for {0} (set {1}, computed {2})")]
L1BlockIdMismatch(u64, L1BlockId, L1BlockId),

#[error("parent link at L1 block {0} incorrect (set parent {1}, found block {2})")]
L1BlockParentMismatch(u64, L1BlockId, L1BlockId),

#[error("L1 segment block did not extend the chain tip")]
L1SegNotExtend,
}
4 changes: 2 additions & 2 deletions crates/consensus-logic/src/fork_choice_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use alpen_express_state::sync_event::SyncEvent;
use crate::ctl::CsmController;
use crate::message::ForkChoiceMessage;
use crate::unfinalized_tracker::UnfinalizedBlockTracker;
use crate::{chain_transition, credential, errors::*, reorg, unfinalized_tracker};
use crate::{credential, errors::*, reorg, unfinalized_tracker};

/// Tracks the parts of the chain that haven't been finalized on-chain yet.
pub struct ForkChoiceManager<D: Database> {
Expand Down Expand Up @@ -510,7 +510,7 @@ fn apply_tip_update<D: Database>(
// locally and update our going state.
let rparams = state.params.rollup();
let mut prestate_cache = StateCache::new(pre_state);
chain_transition::process_block(&mut prestate_cache, header, body, rparams)
express_chaintsn::transition::process_block(&mut prestate_cache, header, body, rparams)
.map_err(|e| Error::InvalidStateTsn(*blkid, e))?;
let (post_state, wb) = prestate_cache.finalize();
pre_state = post_state;
Expand Down
1 change: 0 additions & 1 deletion crates/consensus-logic/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![allow(dead_code)] // TODO: remove this once `finalized_tip` fn is used in `ForkChoiceManager`.
//! Consensus validation logic and core state machine

pub mod chain_transition;
pub mod client_transition;
pub mod credential;
pub mod ctl;
Expand Down