Skip to content

Commit

Permalink
Print usage in break_sequencer_commitments_into_groups
Browse files Browse the repository at this point in the history
  • Loading branch information
kpp committed Feb 28, 2025
1 parent 77b1a43 commit d316f39
Show file tree
Hide file tree
Showing 17 changed files with 86 additions and 45 deletions.
23 changes: 20 additions & 3 deletions crates/batch-prover/src/da_block_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use sov_prover_storage_manager::ProverStorageManager;
use sov_rollup_interface::da::{BlockHeaderTrait, SequencerCommitment};
use sov_rollup_interface::services::da::{DaService, SlotData};
use sov_rollup_interface::spec::SpecId;
use sov_rollup_interface::stateful_statediff::StatefulStateDiff;
use sov_rollup_interface::zk::ZkvmHost;
use tokio::select;
use tokio::sync::Mutex;
Expand Down Expand Up @@ -306,8 +307,10 @@ pub(crate) fn break_sequencer_commitments_into_groups<DB: BatchProverLedgerOps>(

let mut range = 0usize..=0usize;
let mut cumulative_state_diff = StateDiff::new();
let mut cumulative_st_statediff = StatefulStateDiff::default();
for (index, sequencer_commitment) in sequencer_commitments.iter().enumerate() {
let mut sequencer_commitment_state_diff = StateDiff::new();
let mut sequencer_st_statediff = StatefulStateDiff::default();
for l2_height in
sequencer_commitment.l2_start_block_number..=sequencer_commitment.l2_end_block_number
{
Expand All @@ -319,14 +322,27 @@ pub(crate) fn break_sequencer_commitments_into_groups<DB: BatchProverLedgerOps>(
sequencer_commitment.l2_end_block_number
))?;
sequencer_commitment_state_diff =
merge_state_diffs(sequencer_commitment_state_diff, state_diff);
merge_state_diffs(sequencer_commitment_state_diff, state_diff.0);
sequencer_st_statediff = sequencer_st_statediff.merge(state_diff.1);
}
cumulative_state_diff = merge_state_diffs(
cumulative_state_diff,
sequencer_commitment_state_diff.clone(),
);

let compressed_state_diff = compress_blob(&borsh::to_vec(&cumulative_state_diff)?);
cumulative_st_statediff = cumulative_st_statediff.merge(sequencer_st_statediff.clone());

let uncompressed_state_diff = borsh::to_vec(&cumulative_state_diff)?;
let compressed_state_diff = compress_blob(&uncompressed_state_diff);
dbg!(uncompressed_state_diff.len(), compressed_state_diff.len());

let uncompressed_st_state_diff = borsh::to_vec(&cumulative_st_statediff)?;
let hex_state_diff = hex::encode(&uncompressed_st_state_diff);
let compressed_st_state_diff = compress_blob(&uncompressed_st_state_diff);
dbg!(
uncompressed_st_state_diff.len(),
compressed_st_state_diff.len(),
hex_state_diff
);

// Threshold is checked by comparing compressed state diff size as the data will be compressed before it is written on DA
let state_diff_threshold_reached = compressed_state_diff.len() > MAX_TXBODY_SIZE;
Expand All @@ -344,6 +360,7 @@ pub(crate) fn break_sequencer_commitments_into_groups<DB: BatchProverLedgerOps>(
result_range.push(range);
// Reset the cumulative state diff to be equal to the current commitment state diff
cumulative_state_diff = sequencer_commitment_state_diff;
cumulative_st_statediff = sequencer_st_statediff;
range = index..=index;
current_spec = commitment_spec
} else {
Expand Down
7 changes: 4 additions & 3 deletions crates/sequencer/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ use sov_rollup_interface::da::{BlockHeaderTrait, DaSpec};
use sov_rollup_interface::fork::ForkManager;
use sov_rollup_interface::services::da::DaService;
use sov_rollup_interface::soft_confirmation::{L2Header, SignedL2Header};
use sov_rollup_interface::stateful_statediff::StatefulStateDiff;
use sov_rollup_interface::zk::StorageRootHash;
use sov_state::storage::NativeStorage;
use sov_state::ProverStorage;
Expand Down Expand Up @@ -384,7 +385,7 @@ where
da_block: <Da as DaService>::FilteredBlock,
l1_fee_rate: u128,
l2_block_mode: L2BlockMode,
) -> anyhow::Result<(u64, u64, StateDiff)> {
) -> anyhow::Result<(u64, u64, (StateDiff, StatefulStateDiff))> {
let start = Instant::now();
let da_height = da_block.header().height();
let (l2_height, l1_height) = match self
Expand Down Expand Up @@ -769,7 +770,7 @@ where
// Only errors when there are no receivers
let _ = self.soft_confirmation_tx.send(l2_height);

let _ = da_commitment_tx.send((l2_height, state_diff));
let _ = da_commitment_tx.send((l2_height, state_diff.0));
},
Err(e) => {
error!("Sequencer error: {}", e);
Expand Down Expand Up @@ -801,7 +802,7 @@ where
// Only errors when there are no receivers
let _ = self.soft_confirmation_tx.send(l2_height);

let _ = da_commitment_tx.send((l2_height, state_diff));
let _ = da_commitment_tx.send((l2_height, state_diff.0));
},
Err(e) => {
error!("Sequencer error: {}", e);
Expand Down
5 changes: 3 additions & 2 deletions crates/sovereign-sdk/full-node/db/sov-db/src/ledger_db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use rocksdb::WriteBatch;
use sov_rollup_interface::da::SequencerCommitment;
use sov_rollup_interface::fork::{Fork, ForkMigration};
use sov_rollup_interface::soft_confirmation::L2Block;
use sov_rollup_interface::stateful_statediff::StatefulStateDiff;
use sov_rollup_interface::stf::StateDiff;
use sov_rollup_interface::zk::{Proof, StorageRootHash};
use sov_schema_db::{Schema, SchemaBatch, SeekKeyEncoder, DB};
Expand Down Expand Up @@ -574,7 +575,7 @@ impl BatchProverLedgerOps for LedgerDB {
fn set_l2_state_diff(
&self,
l2_height: SoftConfirmationNumber,
state_diff: StateDiff,
state_diff: (StateDiff, StatefulStateDiff),
) -> anyhow::Result<()> {
let mut schema_batch = SchemaBatch::new();
schema_batch.put::<ProverStateDiffs>(&l2_height, &state_diff)?;
Expand All @@ -587,7 +588,7 @@ impl BatchProverLedgerOps for LedgerDB {
fn get_l2_state_diff(
&self,
l2_height: SoftConfirmationNumber,
) -> anyhow::Result<Option<StateDiff>> {
) -> anyhow::Result<Option<(StateDiff, StatefulStateDiff)>> {
self.db.get::<ProverStateDiffs>(&l2_height)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use anyhow::Result;
use borsh::BorshSerialize;
use sov_rollup_interface::da::SequencerCommitment;
use sov_rollup_interface::soft_confirmation::L2Block;
use sov_rollup_interface::stateful_statediff::StatefulStateDiff;
use sov_rollup_interface::stf::StateDiff;
use sov_rollup_interface::zk::{Proof, StorageRootHash};
use sov_schema_db::SchemaBatch;
Expand Down Expand Up @@ -186,11 +187,14 @@ pub trait BatchProverLedgerOps: SharedLedgerOps + Send + Sync {
fn set_l2_state_diff(
&self,
l2_height: SoftConfirmationNumber,
state_diff: StateDiff,
state_diff: (StateDiff, StatefulStateDiff),
) -> Result<()>;

/// Returns an L2 state diff
fn get_l2_state_diff(&self, l2_height: SoftConfirmationNumber) -> Result<Option<StateDiff>>;
fn get_l2_state_diff(
&self,
l2_height: SoftConfirmationNumber,
) -> Result<Option<(StateDiff, StatefulStateDiff)>>;

/// Clears all pending proving sessions
fn clear_pending_proving_sessions(&self) -> Result<()>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use jmt::storage::{NibblePath, Node, NodeKey, StaleNodeIndex};
use jmt::Version;
use sov_rollup_interface::da::SequencerCommitment;
use sov_rollup_interface::mmr::{MMRChunk, MMRNodeHash, Wtxid};
use sov_rollup_interface::stateful_statediff::StatefulStateDiff;
use sov_rollup_interface::stf::StateDiff;
use sov_schema_db::schema::{KeyDecoder, KeyEncoder, ValueCodec};
use sov_schema_db::{CodecError, SeekKeyEncoder};
Expand Down Expand Up @@ -436,7 +437,7 @@ define_table_with_default_codec!(

define_table_with_default_codec!(
/// L2 height to state diff for prover
(ProverStateDiffs) SoftConfirmationNumber => StateDiff
(ProverStateDiffs) SoftConfirmationNumber => (StateDiff, StatefulStateDiff)
);

define_table_with_seek_key_codec!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl From<StoredBatchProofOutput> for BatchProofOutputRpcResponse {
StoredBatchProofOutput::V3(value) => Self {
initial_state_root: value.initial_state_root.to_vec(),
final_state_root: value.final_state_root.to_vec(),
state_diff: value.state_diff,
state_diff: value.state_diff.0,
da_slot_hash: None,
sequencer_da_public_key: vec![],
sequencer_public_key: vec![],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use core::fmt;
use borsh::{BorshDeserialize, BorshSerialize};
#[cfg(feature = "sync")]
use serde::Serialize;
use sov_rollup_interface::stateful_statediff::StatefulStateDiff;
use sov_rollup_interface::stf::{StateDiff, StateRootTransition};
use sov_rollup_interface::witness::Witness;
use sov_rollup_interface::zk::{SparseMerkleProofSha2, StorageRootHash};
Expand Down Expand Up @@ -209,7 +210,7 @@ pub trait Storage: Clone {
(
StateRootTransition,
Self::StateUpdate,
StateDiff, // computed in Zk mode
(StateDiff, StatefulStateDiff), // computed in Zk mode
),
anyhow::Error,
>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use sov_rollup_interface::da::SequencerCommitment;
use sov_rollup_interface::fork::ForkManager;
use sov_rollup_interface::soft_confirmation::{L2Block, SignedL2Header};
use sov_rollup_interface::spec::SpecId;
use sov_rollup_interface::stateful_statediff::StatefulStateDiff;
use sov_rollup_interface::stf::{
SoftConfirmationError, SoftConfirmationResult, StateTransitionError,
};
Expand Down Expand Up @@ -87,7 +88,7 @@ pub struct ApplySequencerCommitmentsOutput {
/// Final state root after all sequencer commitments were applied
pub final_state_root: StorageRootHash,
/// State diff generated after applying
pub state_diff: CumulativeStateDiff,
pub state_diff: (CumulativeStateDiff, StatefulStateDiff),
/// Last processed L2 block height
pub last_l2_height: u64,
/// Last soft confirmation hash
Expand Down Expand Up @@ -424,6 +425,7 @@ where
forks: &[Fork],
) -> ApplySequencerCommitmentsOutput {
let mut state_diff = CumulativeStateDiff::default();
let mut st_statediff = StatefulStateDiff::default();

let sequencer_commitment_merkle_roots = sequencer_commitments
.iter()
Expand Down Expand Up @@ -570,7 +572,8 @@ where

assert_eq!(current_state_root, result.state_root_transition.init_root);
current_state_root = result.state_root_transition.final_root;
state_diff.extend(result.state_diff);
state_diff.extend(result.state_diff.0);
st_statediff = st_statediff.merge(result.state_diff.1);

l2_height += 1;

Expand Down Expand Up @@ -619,7 +622,7 @@ where

ApplySequencerCommitmentsOutput {
final_state_root: current_state_root,
state_diff,
state_diff: (state_diff, st_statediff),
// There has to be a height
last_l2_height: last_commitment_end_height.unwrap(),
final_soft_confirmation_hash: prev_soft_confirmation_hash.unwrap(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,14 @@ impl Storage for ProverStorage {
&self,
state_log: &ReadWriteLog,
witness: &mut Witness,
) -> Result<(StateRootTransition, Self::StateUpdate, StateDiff), anyhow::Error> {
) -> Result<
(
StateRootTransition,
Self::StateUpdate,
(StateDiff, StatefulStateDiff),
),
anyhow::Error,
> {
let version = self.version();
let jmt = JellyfishMerkleTree::<_, DefaultHasher>::new(&self.db);

Expand Down Expand Up @@ -242,7 +249,7 @@ impl Storage for ProverStorage {
final_root: new_root.into(),
},
state_update,
diff,
(diff, st_statediff),
))
}

Expand Down
14 changes: 10 additions & 4 deletions crates/sovereign-sdk/module-system/sov-state/src/zk_storage.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use jmt::KeyHash;
use sov_modules_core::{OrderedWrites, ReadWriteLog, Storage, StorageKey, StorageValue};
use sov_rollup_interface::stateful_statediff;
use sov_rollup_interface::stateful_statediff::{self, StatefulStateDiff};
use sov_rollup_interface::stf::{StateDiff, StateRootTransition};
use sov_rollup_interface::witness::Witness;
use sov_rollup_interface::zk::StorageRootHash;
Expand Down Expand Up @@ -55,7 +55,14 @@ impl Storage for ZkStorage {
&self,
state_log: &ReadWriteLog,
witness: &mut Witness,
) -> Result<(StateRootTransition, Self::StateUpdate, StateDiff), anyhow::Error> {
) -> Result<
(
StateRootTransition,
Self::StateUpdate,
(StateDiff, StatefulStateDiff),
),
anyhow::Error,
> {
let prev_state_root = witness.get_hint();

// For each value that's been read from the tree, verify the provided jmt proof
Expand Down Expand Up @@ -116,7 +123,6 @@ impl Storage for ZkStorage {
.sum();
let ststdiff = borsh::to_vec(&st_statediff).unwrap();
let prevdiff = borsh::to_vec(&diff).unwrap();
let _ = st_statediff;

println!(
"zk: ststdiff: {} bytes, diff: {} bytes, ststdiff unparsed: {} bytes \n",
Expand All @@ -131,7 +137,7 @@ impl Storage for ZkStorage {
final_root: new_root,
},
(),
diff,
(diff, st_statediff),
))
}

Expand Down
3 changes: 1 addition & 2 deletions crates/sovereign-sdk/rollup-interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ readme = "README.md"
resolver = "2"

[dependencies]
alloy-primitives = { workspace = true, features = ["serde"], optional = true }
alloy-primitives = { workspace = true, features = ["serde"] }
anyhow = { workspace = true, features = ["default"] }
async-trait = { workspace = true, optional = true }
bcs = { workspace = true }
Expand All @@ -40,7 +40,6 @@ serde_json = { workspace = true }
[features]
default = []
native = [
"alloy-primitives",
"tokio",
"futures",
"tracing",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! main event loop of the rollup.
use super::zk::StorageRootHash;
use crate::stateful_statediff::StatefulStateDiff;
use crate::RefCount;

/// The configuration of a full node of the rollup which creates zk proofs.
Expand Down Expand Up @@ -64,7 +65,7 @@ pub struct SoftConfirmationResult<Cs, W, SL> {
/// Witness after applying the whole block
pub offchain_witness: W,
/// State diff after applying the whole block
pub state_diff: StateDiff,
pub state_diff: (StateDiff, StatefulStateDiff),
}

#[derive(Debug, PartialEq)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use borsh::{BorshDeserialize, BorshSerialize};
use serde::{Deserialize, Serialize};

use super::CumulativeStateDiff;
use crate::stateful_statediff::StatefulStateDiff;
use crate::zk::StorageRootHash;

/// The public output of a SNARK batch proof in Sovereign, this struct makes a claim that
Expand All @@ -24,7 +25,7 @@ pub struct BatchProofCircuitOutputV3 {
/// This will be [0; 32] for pre fork 1 proofs
pub final_soft_confirmation_hash: [u8; 32],
/// State diff of L2 blocks in the processed sequencer commitments.
pub state_diff: CumulativeStateDiff,
pub state_diff: (CumulativeStateDiff, StatefulStateDiff),
/// The last processed l2 height in the processed sequencer commitments.
/// This will be 0 for pre fork 1 proofs
pub last_l2_height: u64,
Expand Down
Loading

0 comments on commit d316f39

Please sign in to comment.