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

Refactoring CSM startup #163

Merged
merged 18 commits into from
Jul 26, 2024
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
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 @@
/// 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,

Check warning on line 17 in crates/consensus-logic/src/client_transition.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/client_transition.rs#L17

Added line #L17 was not covered by tests
ev: &SyncEvent,
database: &D,
_params: &Params,
params: &Params,

Check warning on line 20 in crates/consensus-logic/src/client_transition.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/client_transition.rs#L20

Added line #L20 was not covered by tests
) -> Result<ClientUpdateOutput, Error> {
let mut writes = Vec::new();
let mut actions = Vec::new();
Expand All @@ -32,26 +35,62 @@

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

let l1v = state.l1_view();

Check warning on line 39 in crates/consensus-logic/src/client_transition.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/client_transition.rs#L38-L39

Added lines #L38 - L39 were not covered by tests

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);
}

Check warning on line 54 in crates/consensus-logic/src/client_transition.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/client_transition.rs#L41-L54

Added lines #L41 - L54 were not covered by tests
}
}

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");
}

Check warning on line 65 in crates/consensus-logic/src/client_transition.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/client_transition.rs#L63-L65

Added lines #L63 - L65 were not covered by tests

if let Some(ss) = state.sync() {

Check warning on line 67 in crates/consensus-logic/src/client_transition.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/client_transition.rs#L67

Added line #L67 was not covered by tests
// 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();

Check warning on line 70 in crates/consensus-logic/src/client_transition.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/client_transition.rs#L70

Added line #L70 was not covered by tests

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))?;

Check warning on line 75 in crates/consensus-logic/src/client_transition.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/client_transition.rs#L72-L75

Added lines #L72 - L75 were not covered by tests

// 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));

Check warning on line 81 in crates/consensus-logic/src/client_transition.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/client_transition.rs#L80-L81

Added lines #L80 - L81 were not covered by tests
} else {
// TODO we can expand this later to make more sense
return Err(Error::MissingClientSyncState);

Check warning on line 84 in crates/consensus-logic/src/client_transition.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/client_transition.rs#L84

Added line #L84 was not covered by tests
}
}

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)));
}

Check warning on line 92 in crates/consensus-logic/src/client_transition.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/client_transition.rs#L88-L92

Added lines #L88 - L92 were not covered by tests

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 @@
ident: &Identity,
database: &D,
) -> Result<(), Error> {
let Some(ss) = state.sync() else {
return Ok(());

Check warning on line 96 in crates/consensus-logic/src/duty_executor.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/duty_executor.rs#L95-L96

Added lines #L95 - L96 were not covered by tests
};

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();

Check warning on line 103 in crates/consensus-logic/src/duty_executor.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/duty_executor.rs#L103

Added line #L103 was not covered by tests
let l2prov = database.l2_provider();
let block = l2prov
.get_block_data(tip_blkid)?
Expand Down Expand Up @@ -246,7 +250,12 @@
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);

Check warning on line 255 in crates/consensus-logic/src/duty_executor.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/duty_executor.rs#L254-L255

Added lines #L254 - L255 were not covered by tests
};

let prev_block_id = *last_ss.chain_tip_blkid();

Check warning on line 258 in crates/consensus-logic/src/duty_executor.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/duty_executor.rs#L258

Added line #L258 was not covered by tests

// 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 @@
_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());

Check warning on line 18 in crates/consensus-logic/src/duty_extractor.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/duty_extractor.rs#L17-L18

Added lines #L17 - L18 were not covered by tests
};

let tip_blkid = *ss.chain_tip_blkid();

Check warning on line 21 in crates/consensus-logic/src/duty_extractor.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/duty_extractor.rs#L21

Added line #L21 was not covered by tests

// 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
Loading