Skip to content

Commit

Permalink
Restore input/output (#1547)
Browse files Browse the repository at this point in the history
  • Loading branch information
rakanalh authored Dec 3, 2024
1 parent 77bf340 commit 5f6aa70
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 127 deletions.
39 changes: 31 additions & 8 deletions crates/batch-prover/src/proving.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use sov_db::ledger_db::BatchProverLedgerOps;
use sov_db::schema::types::{BatchNumber, StoredBatchProof, StoredBatchProofOutput};
use sov_modules_api::{BatchProofCircuitOutputV2, BlobReaderTrait, SlotData, SpecId, Zkvm};
use sov_modules_api::{BatchProofCircuitOutput, BlobReaderTrait, SlotData, SpecId, Zkvm};
use sov_rollup_interface::da::{BlockHeaderTrait, DaNamespace, DaSpec, SequencerCommitment};
use sov_rollup_interface::fork::fork_from_block_number;
use sov_rollup_interface::rpc::SoftConfirmationStatus;
use sov_rollup_interface::services::da::DaService;
use sov_rollup_interface::zk::{BatchProofCircuitInputV2, Proof, ZkvmHost};
use sov_rollup_interface::zk::{BatchProofCircuitInput, Proof, ZkvmHost};
use sov_stf_runner::ProverService;
use tokio::sync::Mutex;
use tracing::{debug, info};
Expand Down Expand Up @@ -51,7 +51,7 @@ pub(crate) async fn data_to_prove<'txs, Da, DB, StateRoot, Witness, Tx>(
) -> Result<
(
Vec<SequencerCommitment>,
Vec<BatchProofCircuitInputV2<'txs, StateRoot, Witness, Da::Spec, Tx>>,
Vec<BatchProofCircuitInput<'txs, StateRoot, Witness, Da::Spec, Tx>>,
),
L1ProcessingError,
>
Expand Down Expand Up @@ -134,6 +134,8 @@ where
for sequencer_commitments_range in ranges {
let first_l2_height_of_l1 =
sequencer_commitments[*sequencer_commitments_range.start()].l2_start_block_number;
let last_l2_height_of_l1 =
sequencer_commitments[*sequencer_commitments_range.end()].l2_end_block_number;
let (
state_transition_witnesses,
soft_confirmations,
Expand All @@ -158,8 +160,26 @@ where
})?
.expect("There should be a state root");

let input: BatchProofCircuitInputV2<StateRoot, Witness, Da::Spec, Tx> =
BatchProofCircuitInputV2 {
let final_state_root = ledger
.get_l2_state_root::<StateRoot>(last_l2_height_of_l1)
.map_err(|e| {
L1ProcessingError::Other(format!("Error getting final state root: {:?}", e))
})?
.expect("There should be a state root");

let initial_batch_hash = ledger
.get_soft_confirmation_by_number(&BatchNumber(first_l2_height_of_l1))
.map_err(|e| {
L1ProcessingError::Other(format!("Error getting initial batch hash: {:?}", e))
})?
.ok_or(L1ProcessingError::Other(format!(
"Could not find soft batch at height {}",
first_l2_height_of_l1
)))?
.prev_hash;

let input: BatchProofCircuitInput<StateRoot, Witness, Da::Spec, Tx> =
BatchProofCircuitInput {
initial_state_root,
da_data: da_data.clone(),
da_block_header_of_commitments: da_block_header_of_commitments.clone(),
Expand All @@ -175,6 +195,8 @@ where
),
sequencer_public_key: sequencer_pub_key.clone(),
sequencer_da_public_key: sequencer_da_pub_key.clone(),
final_state_root,
prev_soft_confirmation_hash: initial_batch_hash,
};

batch_proof_circuit_inputs.push(input);
Expand All @@ -190,7 +212,7 @@ pub(crate) async fn prove_l1<Da, Ps, Vm, DB, StateRoot, Witness, Tx>(
elfs_by_spec: HashMap<SpecId, Vec<u8>>,
l1_block: Da::FilteredBlock,
sequencer_commitments: Vec<SequencerCommitment>,
inputs: Vec<BatchProofCircuitInputV2<'_, StateRoot, Witness, Da::Spec, Tx>>,
inputs: Vec<BatchProofCircuitInput<'_, StateRoot, Witness, Da::Spec, Tx>>,
) -> anyhow::Result<()>
where
Da: DaService,
Expand Down Expand Up @@ -255,7 +277,7 @@ where
}

pub(crate) fn state_transition_already_proven<StateRoot, Witness, Da, Tx>(
input: &BatchProofCircuitInputV2<StateRoot, Witness, Da::Spec, Tx>,
input: &BatchProofCircuitInput<StateRoot, Witness, Da::Spec, Tx>,
proofs: &Vec<StoredBatchProof>,
) -> bool
where
Expand Down Expand Up @@ -305,7 +327,7 @@ where
// TODO: select output version based on spec
let circuit_output = Vm::extract_output::<
<Da as DaService>::Spec,
BatchProofCircuitOutputV2<<Da as DaService>::Spec, StateRoot>,
BatchProofCircuitOutput<<Da as DaService>::Spec, StateRoot>,
>(&proof)
.expect("Proof should be deserializable");

Expand Down Expand Up @@ -334,6 +356,7 @@ where
sequencer_public_key: circuit_output.sequencer_public_key,
sequencer_da_public_key: circuit_output.sequencer_da_public_key,
preproven_commitments: circuit_output.preproven_commitments,
validity_condition: vec![],
};
let l1_height = ledger_db
.get_l1_height_of_l1_hash(slot_hash)?
Expand Down
8 changes: 5 additions & 3 deletions crates/batch-prover/tests/prover_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use sov_db::rocks_db_config::RocksdbConfig;
use sov_mock_da::{MockAddress, MockBlockHeader, MockDaService, MockDaSpec, MockHash};
use sov_mock_zkvm::MockZkvm;
use sov_rollup_interface::da::Time;
use sov_rollup_interface::zk::BatchProofCircuitInputV2;
use sov_rollup_interface::zk::BatchProofCircuitInput;
use sov_stf_runner::mock::MockStf;
use sov_stf_runner::ProverService;

Expand Down Expand Up @@ -99,10 +99,11 @@ fn make_new_prover(thread_pool_size: usize, da_service: Arc<MockDaService>) -> T

fn make_transition_data(
header_hash: MockHash,
) -> BatchProofCircuitInputV2<'static, [u8; 0], Vec<u8>, MockDaSpec, ()> {
BatchProofCircuitInputV2 {
) -> BatchProofCircuitInput<'static, [u8; 0], Vec<u8>, MockDaSpec, ()> {
BatchProofCircuitInput {
initial_state_root: [],
inclusion_proof: [0; 32],
prev_soft_confirmation_hash: [0; 32],
completeness_proof: (),
da_data: vec![],
sequencer_commitments_range: (0, 0),
Expand All @@ -120,5 +121,6 @@ fn make_transition_data(
sequencer_public_key: vec![],
sequencer_da_public_key: vec![],
preproven_commitments: vec![],
final_state_root: [],
}
}
9 changes: 4 additions & 5 deletions crates/citrea-stf/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ use std::marker::PhantomData;

use sov_rollup_interface::da::{BlockHeaderTrait, DaNamespace, DaVerifier};
use sov_rollup_interface::stf::{ApplySequencerCommitmentsOutput, StateTransitionFunction};
use sov_rollup_interface::zk::{
BatchProofCircuitInputV2, BatchProofCircuitOutputV2, Zkvm, ZkvmGuest,
};
use sov_rollup_interface::zk::{BatchProofCircuitInput, BatchProofCircuitOutput, Zkvm, ZkvmGuest};

/// Verifies a state transition
pub struct StateTransitionVerifier<ST, Da, Zk>
Expand Down Expand Up @@ -40,7 +38,7 @@ where
pre_state: Stf::PreState,
) -> Result<(), Da::Error> {
println!("Running sequencer commitments in DA slot");
let data: BatchProofCircuitInputV2<Stf::StateRoot, _, Da::Spec, Stf::Transaction> =
let data: BatchProofCircuitInput<Stf::StateRoot, _, Da::Spec, Stf::Transaction> =
zkvm.read_from_host();

if !data.da_block_header_of_commitments.verify_hash() {
Expand Down Expand Up @@ -90,11 +88,12 @@ where

println!("out of apply_soft_confirmations_from_sequencer_commitments");

let out: BatchProofCircuitOutputV2<Da::Spec, _> = BatchProofCircuitOutputV2 {
let out: BatchProofCircuitOutput<Da::Spec, _> = BatchProofCircuitOutput {
initial_state_root: data.initial_state_root,
final_state_root,
final_soft_confirmation_hash,
state_diff,
prev_soft_confirmation_hash: data.prev_soft_confirmation_hash,
da_slot_hash: data.da_block_header_of_commitments.hash(),
sequencer_public_key: data.sequencer_public_key,
sequencer_da_public_key: data.sequencer_da_public_key,
Expand Down
5 changes: 3 additions & 2 deletions crates/fullnode/src/da_block_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use sov_rollup_interface::fork::fork_from_block_number;
use sov_rollup_interface::rpc::SoftConfirmationStatus;
use sov_rollup_interface::services::da::{DaService, SlotData};
use sov_rollup_interface::spec::SpecId;
use sov_rollup_interface::zk::{BatchProofCircuitOutputV2, Proof, ZkvmHost};
use sov_rollup_interface::zk::{BatchProofCircuitOutput, Proof, ZkvmHost};
use tokio::select;
use tokio::sync::{mpsc, Mutex};
use tokio::time::{sleep, Duration};
Expand Down Expand Up @@ -301,7 +301,7 @@ where
// TODO: select output version based on spec
let batch_proof_output = Vm::extract_output::<
<Da as DaService>::Spec,
BatchProofCircuitOutputV2<<Da as DaService>::Spec, StateRoot>,
BatchProofCircuitOutput<<Da as DaService>::Spec, StateRoot>,
>(&proof)
.expect("Proof should be deserializable");
if batch_proof_output.sequencer_da_public_key != self.sequencer_da_pub_key
Expand Down Expand Up @@ -330,6 +330,7 @@ where
sequencer_public_key: batch_proof_output.sequencer_public_key,
sequencer_da_public_key: batch_proof_output.sequencer_da_public_key,
preproven_commitments: batch_proof_output.preproven_commitments.clone(),
validity_condition: vec![],
};

let l1_hash = batch_proof_output.da_slot_hash.into();
Expand Down
4 changes: 2 additions & 2 deletions crates/light-client-prover/src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use borsh::BorshDeserialize;
use sov_modules_api::BlobReaderTrait;
use sov_rollup_interface::da::{DaDataLightClient, DaNamespace, DaVerifier};
use sov_rollup_interface::zk::{
BatchProofCircuitOutputV2, BatchProofInfo, LightClientCircuitInput, LightClientCircuitOutput,
BatchProofCircuitOutput, BatchProofInfo, LightClientCircuitInput, LightClientCircuitOutput,
ZkvmGuest,
};

Expand Down Expand Up @@ -102,7 +102,7 @@ pub fn run_circuit<DaV: DaVerifier, G: ZkvmGuest>(
let journal =
G::extract_raw_output(&proof).expect("DaData proofs must be valid");
// TODO: select output version based on the spec
let batch_proof_output: BatchProofCircuitOutputV2<DaV::Spec, [u8; 32]> =
let batch_proof_output: BatchProofCircuitOutput<DaV::Spec, [u8; 32]> =
match G::verify_and_extract_output(
&journal,
&batch_proof_method_id.into(),
Expand Down
4 changes: 2 additions & 2 deletions crates/light-client-prover/src/da_block_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use sequencer_client::SequencerClient;
use sov_db::ledger_db::{LightClientProverLedgerOps, SharedLedgerOps};
use sov_db::schema::types::{SlotNumber, StoredLightClientProofOutput};
use sov_modules_api::fork::fork_from_block_number;
use sov_modules_api::{BatchProofCircuitOutputV2, BlobReaderTrait, DaSpec, Zkvm};
use sov_modules_api::{BatchProofCircuitOutput, BlobReaderTrait, DaSpec, Zkvm};
use sov_rollup_interface::da::{BlockHeaderTrait, DaDataLightClient, DaNamespace};
use sov_rollup_interface::services::da::{DaService, SlotData};
use sov_rollup_interface::spec::SpecId;
Expand Down Expand Up @@ -161,7 +161,7 @@ where
if let DaDataLightClient::Complete(proof) = batch_proof {
let batch_proof_output = Vm::extract_output::<
<Da as DaService>::Spec,
BatchProofCircuitOutputV2<<Da as DaService>::Spec, [u8; 32]>,
BatchProofCircuitOutput<<Da as DaService>::Spec, [u8; 32]>,
>(&proof)
.map_err(|_| anyhow!("Proof should be deserializable"))?;
let last_l2_height = batch_proof_output.last_l2_height;
Expand Down
5 changes: 3 additions & 2 deletions crates/light-client-prover/src/tests/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::BTreeMap;
use sov_mock_da::{MockAddress, MockBlob, MockDaSpec, MockHash};
use sov_mock_zkvm::{MockCodeCommitment, MockJournal, MockProof};
use sov_rollup_interface::da::{BlobReaderTrait, DaDataLightClient};
use sov_rollup_interface::zk::{BatchProofCircuitOutputV2, LightClientCircuitOutput};
use sov_rollup_interface::zk::{BatchProofCircuitOutput, LightClientCircuitOutput};

pub(crate) fn create_mock_blob(
initial_state_root: [u8; 32],
Expand All @@ -13,9 +13,10 @@ pub(crate) fn create_mock_blob(
) -> MockBlob {
let batch_proof_method_id = MockCodeCommitment([2u8; 32]);

let bp = BatchProofCircuitOutputV2::<MockDaSpec, [u8; 32]> {
let bp = BatchProofCircuitOutput::<MockDaSpec, [u8; 32]> {
initial_state_root,
final_state_root,
prev_soft_confirmation_hash: [3; 32],
final_soft_confirmation_hash: [4; 32],
state_diff: BTreeMap::new(),
da_slot_hash: MockHash([5; 32]),
Expand Down
6 changes: 3 additions & 3 deletions crates/sovereign-sdk/full-node/db/sov-db/src/schema/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ use sov_schema_db::{CodecError, SeekKeyEncoder};

use super::types::{
AccessoryKey, AccessoryStateValue, BatchNumber, DbHash, JmtValue, L2HeightRange, SlotNumber,
StateKey, StoredBatch, StoredBatchProof, StoredBatchProofV1, StoredLightClientProof,
StoredSlot, StoredSoftConfirmation, StoredVerifiedProof,
StateKey, StoredBatch, StoredBatchProof, StoredLightClientProof, StoredSlot,
StoredSoftConfirmation, StoredVerifiedProof,
};

/// A list of all tables used by the StateDB. These tables store rollup state - meaning
Expand Down Expand Up @@ -316,7 +316,7 @@ define_table_with_default_codec!(

define_table_with_default_codec!(
/// Old version of ProofsBySlotNumber
(ProofsBySlotNumber) SlotNumber => Vec<StoredBatchProofV1>
(ProofsBySlotNumber) SlotNumber => Vec<StoredBatchProof>
);

define_table_with_default_codec!(
Expand Down
36 changes: 2 additions & 34 deletions crates/sovereign-sdk/full-node/db/sov-db/src/schema/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ impl From<StoredLightClientProof> for LightClientProofResponse {
}
}

/// Old version of StoredBatchProofOutput
/// The on-disk format for a state transition.
#[derive(Debug, PartialEq, BorshDeserialize, BorshSerialize, Clone)]
pub struct StoredBatchProofOutputV1 {
pub struct StoredBatchProofOutput {
/// The state of the rollup before the transition
pub initial_state_root: Vec<u8>,
/// The state of the rollup after the transition
Expand All @@ -165,16 +165,6 @@ pub struct StoredBatchProofOutputV1 {
/// Validity condition. Removed in the newer version.
pub validity_condition: Vec<u8>,
}
/// Old version of StoredBatchProof
#[derive(Debug, PartialEq, BorshDeserialize, BorshSerialize)]
pub struct StoredBatchProofV1 {
/// Tx id
pub l1_tx_id: [u8; 32],
/// Proof
pub proof: Proof,
/// Output
pub proof_output: StoredBatchProofOutputV1,
}

/// The on-disk format for a proof. Stores the tx id of the proof sent to da, proof data and state transition
#[derive(Debug, PartialEq, BorshDeserialize, BorshSerialize)]
Expand Down Expand Up @@ -215,28 +205,6 @@ impl From<StoredVerifiedProof> for VerifiedBatchProofResponse {
}
}

/// The on-disk format for a state transition.
#[derive(Debug, PartialEq, BorshDeserialize, BorshSerialize, Clone)]
pub struct StoredBatchProofOutput {
/// The state of the rollup before the transition
pub initial_state_root: Vec<u8>,
/// The state of the rollup after the transition
pub final_state_root: Vec<u8>,
/// State diff of L2 blocks in the processed sequencer commitments.
pub state_diff: CumulativeStateDiff,
/// The DA slot hash that the sequencer commitments causing this state transition were found in.
pub da_slot_hash: [u8; 32],
/// The range of sequencer commitments in the DA slot that were processed.
/// The range is inclusive.
pub sequencer_commitments_range: (u32, u32),
/// Sequencer public key.
pub sequencer_public_key: Vec<u8>,
/// Sequencer DA public key.
pub sequencer_da_public_key: Vec<u8>,
/// Pre-proven commitments L2 ranges which also exist in the current L1 `da_data`.
pub preproven_commitments: Vec<usize>,
}

impl From<StoredBatchProofOutput> for BatchProofOutputRpcResponse {
fn from(value: StoredBatchProofOutput) -> Self {
Self {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ pub use sov_rollup_interface::soft_confirmation::{
SignedSoftConfirmation, UnsignedSoftConfirmation,
};
pub use sov_rollup_interface::stf::{Event, StateDiff};
pub use sov_rollup_interface::zk::{BatchProofCircuitOutputV2, Zkvm};
pub use sov_rollup_interface::zk::{BatchProofCircuitOutput, Zkvm};
pub use sov_rollup_interface::{digest, BasicAddress, RollupAddress};

pub mod prelude {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use borsh::{BorshDeserialize, BorshSerialize};
use serde::{Deserialize, Serialize};

use crate::da::DaSpec;
use crate::zk::BatchProofCircuitOutputV2;
use crate::zk::BatchProofCircuitOutput;

/// A proof that the attester was bonded at the transition num `transition_num`.
/// For rollups using the `jmt`, this will be a `jmt::SparseMerkleProof`
Expand Down Expand Up @@ -39,7 +39,7 @@ pub struct ChallengeContents<Address, Da: DaSpec, Root> {
/// The rollup address of the originator of this challenge
pub challenger_address: Address,
/// The state transition that was proven
pub state_transition: BatchProofCircuitOutputV2<Da, Root>,
pub state_transition: BatchProofCircuitOutput<Da, Root>,
}

#[derive(Debug, Clone, PartialEq, Eq, BorshSerialize, Serialize, Deserialize)]
Expand Down
Loading

0 comments on commit 5f6aa70

Please sign in to comment.