Skip to content

Commit

Permalink
Merge pull request #163 from alpenlabs/feature/timely-l2-genesis
Browse files Browse the repository at this point in the history
Refactoring CSM startup
  • Loading branch information
delbonis authored Jul 26, 2024
2 parents 89d7117 + 3ab5fb0 commit 0ebed36
Show file tree
Hide file tree
Showing 23 changed files with 895 additions and 276 deletions.
59 changes: 49 additions & 10 deletions crates/consensus-logic/src/client_transition.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
//! Core state transition function.
#![allow(unused)] // still under development

use tracing::*;

use alpen_vertex_db::traits::{Database, L1DataProvider, L2DataProvider};
use alpen_vertex_primitives::prelude::*;
Expand All @@ -11,10 +14,10 @@ use crate::errors::*;
/// Processes the event given the current consensus state, producing some
/// output. This can return database errors.
pub fn process_event<D: Database>(
_state: &ClientState,
state: &ClientState,
ev: &SyncEvent,
database: &D,
_params: &Params,
params: &Params,
) -> Result<ClientUpdateOutput, Error> {
let mut writes = Vec::new();
let mut actions = Vec::new();
Expand All @@ -32,26 +35,62 @@ pub fn process_event<D: Database>(

// TODO if we have some number of L1 blocks finalized, also emit an
// `UpdateBuried` write

let l1v = state.l1_view();

if let Some(ss) = state.sync() {
// TODO figure out what to do here
} else {
let horizon_ht = params.rollup.horizon_l1_height;
let genesis_ht = params.rollup.genesis_l1_height;

// TODO make params configurable
let genesis_threshold = genesis_ht + 3;

// If necessary, activeate the chain!
if !state.is_chain_active() && *height >= genesis_threshold {
debug!("emitting chain activation");
writes.push(ClientStateWrite::ActivateChain);
}
}
}

SyncEvent::L1Revert(_to_height) => {
// TODO
}

SyncEvent::L1DABatch(blkids) => {
// TODO load it up and figure out what's there, see if we have to
// load the state updates from L1 or something
let l2prov = database.l2_provider();
if blkids.is_empty() {
warn!("empty L1DABatch");
}

if let Some(ss) = state.sync() {
// TODO load it up and figure out what's there, see if we have to
// load the state updates from L1 or something
let l2prov = database.l2_provider();

for id in blkids {
let _block = l2prov
.get_block_data(*id)?
.ok_or(Error::MissingL2Block(*id))?;
for id in blkids {
let _block = l2prov
.get_block_data(*id)?
.ok_or(Error::MissingL2Block(*id))?;

// TODO do whatever changes we have to to accept the new block
// TODO do whatever changes we have to to accept the new block
}

let last = blkids.last().unwrap();
writes.push(ClientStateWrite::UpdateFinalized(*last));
} else {
// TODO we can expand this later to make more sense
return Err(Error::MissingClientSyncState);
}
}

SyncEvent::ComputedGenesis(gblkid) => {
// Just construct the sync state for the genesis.
let ss = SyncState::from_genesis_blkid(*gblkid);
writes.push(ClientStateWrite::ReplaceSync(Box::new(ss)));
}

SyncEvent::NewTipBlock(blkid) => {
let l2prov = database.l2_provider();
let _block = l2prov
Expand Down
13 changes: 11 additions & 2 deletions crates/consensus-logic/src/duty_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,15 @@ fn update_tracker<D: Database>(
ident: &Identity,
database: &D,
) -> Result<(), Error> {
let Some(ss) = state.sync() else {
return Ok(());
};

let new_duties = duty_extractor::extract_duties(state, ident, database)?;

// Figure out the block slot from the tip blockid.
// TODO include the block slot in the consensus state
let tip_blkid = *state.chain_tip_blkid();
let tip_blkid = *ss.chain_tip_blkid();
let l2prov = database.l2_provider();
let block = l2prov
.get_block_data(tip_blkid)?
Expand Down Expand Up @@ -246,7 +250,12 @@ fn sign_and_store_block<D: Database, E: ExecEngineCtl>(
let last_cstate = cs_prov
.get_state_checkpoint(ckpt_idx)?
.expect("dutyexec: get state checkpoint");
let prev_block_id = *last_cstate.chain_tip_blkid();

let Some(last_ss) = last_cstate.sync() else {
return Ok(None);
};

let prev_block_id = *last_ss.chain_tip_blkid();

// Start preparing the EL payload.
let ts = now_millis();
Expand Down
8 changes: 7 additions & 1 deletion crates/consensus-logic/src/duty_extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ pub fn extract_duties<D: Database>(
_ident: &duties::Identity,
database: &D,
) -> Result<Vec<duties::Duty>, Error> {
let tip_blkid = *state.chain_tip_blkid();
// If a sync state isn't present then we probably don't have anything we
// want to do. We might change this later.
let Some(ss) = state.sync() else {
return Ok(Vec::new());
};

let tip_blkid = *ss.chain_tip_blkid();

// Figure out the block slot from the tip blockid.
// TODO include the block slot in the consensus state
Expand Down
9 changes: 9 additions & 0 deletions crates/consensus-logic/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub enum Error {
#[error("L1 blkid {0:?} missing from database")]
MissingL1Block(L1BlockId),

#[error("L1 block {0} missing from database")]
MissingL1BlockHeight(u64),

#[error("missing expected consensus writes at {0}")]
MissingConsensusWrites(u64),

Expand All @@ -38,6 +41,12 @@ pub enum Error {
#[error("invalid state transition on block {0:?}: {1}")]
InvalidStateTsn(L2BlockId, TsnError),

#[error("client sync state unset")]
MissingClientSyncState,

#[error("csm dropped")]
CsmDropped,

#[error("chaintip: {0}")]
ChainTip(#[from] ChainTipError),

Expand Down
Loading

0 comments on commit 0ebed36

Please sign in to comment.