From 37aba6a64f6f412ca024b20fe8ce59e2007cb3eb Mon Sep 17 00:00:00 2001 From: Roman Date: Tue, 5 Nov 2024 17:30:34 -0300 Subject: [PATCH] Don't clone SignedSoftConfirmation::txs (#1433) --- crates/batch-prover/src/da_block_handler.rs | 10 +++--- crates/batch-prover/src/proving.rs | 4 +-- crates/batch-prover/tests/prover_tests.rs | 2 +- crates/sequencer-client/src/lib.rs | 2 +- crates/sequencer/src/runner.rs | 12 +++---- .../full-node/db/sov-db/src/schema/types.rs | 4 +-- .../sov-modules-api/src/hooks.rs | 2 +- .../sov-modules-stf-blueprint/src/lib.rs | 4 +-- .../src/stf_blueprint.rs | 2 +- .../src/state_machine/soft_confirmation.rs | 32 ++++++++----------- .../src/state_machine/zk/mod.rs | 4 +-- 11 files changed, 37 insertions(+), 41 deletions(-) diff --git a/crates/batch-prover/src/da_block_handler.rs b/crates/batch-prover/src/da_block_handler.rs index 422e89e9d..74b30e3c8 100644 --- a/crates/batch-prover/src/da_block_handler.rs +++ b/crates/batch-prover/src/da_block_handler.rs @@ -33,9 +33,9 @@ use tracing::{error, info, warn}; use crate::errors::L1ProcessingError; use crate::proving::{data_to_prove, extract_and_store_proof, prove_l1}; -type CommitmentStateTransitionData = ( +type CommitmentStateTransitionData<'txs, Witness, Da> = ( VecDeque>, - VecDeque>, + VecDeque>>, VecDeque::Spec as DaSpec>::BlockHeader>>, ); @@ -341,7 +341,7 @@ pub(crate) async fn get_batch_proof_circuit_input_from_commitments< da_service: &Arc, ledger_db: &DB, l1_block_cache: &Arc>>, -) -> Result, anyhow::Error> { +) -> Result, anyhow::Error> { let mut state_transition_witnesses: VecDeque> = VecDeque::new(); let mut soft_confirmations: VecDeque> = VecDeque::new(); let mut da_block_headers_of_soft_confirmations: VecDeque< @@ -388,8 +388,8 @@ pub(crate) async fn get_batch_proof_circuit_input_from_commitments< }; da_block_headers_to_push.push(filtered_block.header().clone()); } - let signed_soft_confirmation: SignedSoftConfirmation = soft_confirmation.clone().into(); - commitment_soft_confirmations.push(signed_soft_confirmation.clone()); + let signed_soft_confirmation: SignedSoftConfirmation = soft_confirmation.into(); + commitment_soft_confirmations.push(signed_soft_confirmation); } soft_confirmations.push_back(commitment_soft_confirmations); diff --git a/crates/batch-prover/src/proving.rs b/crates/batch-prover/src/proving.rs index e413c0a61..7db205b35 100644 --- a/crates/batch-prover/src/proving.rs +++ b/crates/batch-prover/src/proving.rs @@ -36,7 +36,7 @@ pub(crate) async fn data_to_prove( ) -> Result< ( Vec, - Vec>, + Vec>, ), L1ProcessingError, > @@ -192,7 +192,7 @@ pub(crate) async fn prove_l1( code_commitments_by_spec: HashMap, l1_block: Da::FilteredBlock, sequencer_commitments: Vec, - inputs: Vec>, + inputs: Vec>, ) -> anyhow::Result<()> where Da: DaService, diff --git a/crates/batch-prover/tests/prover_tests.rs b/crates/batch-prover/tests/prover_tests.rs index 7767549e6..fffba87a1 100644 --- a/crates/batch-prover/tests/prover_tests.rs +++ b/crates/batch-prover/tests/prover_tests.rs @@ -102,7 +102,7 @@ fn make_new_prover(thread_pool_size: usize, da_service: Arc) -> T fn make_transition_data( header_hash: MockHash, -) -> BatchProofCircuitInput<[u8; 0], Vec, MockDaSpec> { +) -> BatchProofCircuitInput<'static, [u8; 0], Vec, MockDaSpec> { BatchProofCircuitInput { initial_state_root: [], final_state_root: [], diff --git a/crates/sequencer-client/src/lib.rs b/crates/sequencer-client/src/lib.rs index f65317647..6b4db0d74 100644 --- a/crates/sequencer-client/src/lib.rs +++ b/crates/sequencer-client/src/lib.rs @@ -127,7 +127,7 @@ pub struct GetSoftConfirmationResponse { pub timestamp: u64, } -impl From for SignedSoftConfirmation { +impl From for SignedSoftConfirmation<'static> { fn from(val: GetSoftConfirmationResponse) -> Self { SignedSoftConfirmation::new( val.l2_height, diff --git a/crates/sequencer/src/runner.rs b/crates/sequencer/src/runner.rs index 8235ecd2e..51ea81214 100644 --- a/crates/sequencer/src/runner.rs +++ b/crates/sequencer/src/runner.rs @@ -464,14 +464,14 @@ where da_block.header().height(), da_block.header().hash().into(), da_block.header().txs_commitment().into(), - txs, + &txs, deposit_data.clone(), l1_fee_rate, timestamp, ); let mut signed_soft_confirmation = - self.sign_soft_confirmation_batch(unsigned_batch, self.batch_hash)?; + self.sign_soft_confirmation_batch(&unsigned_batch, self.batch_hash)?; let (soft_confirmation_receipt, checkpoint) = self.stf.end_soft_confirmation( active_fork_spec, @@ -777,11 +777,11 @@ where } /// Signs necessary info and returns a BlockTemplate - fn sign_soft_confirmation_batch( + fn sign_soft_confirmation_batch<'txs>( &mut self, - soft_confirmation: UnsignedSoftConfirmation, + soft_confirmation: &'txs UnsignedSoftConfirmation<'_>, prev_soft_confirmation_hash: [u8; 32], - ) -> anyhow::Result { + ) -> anyhow::Result> { let raw = borsh::to_vec(&soft_confirmation).map_err(|e| anyhow!(e))?; let hash = ::Hasher::digest(raw.as_slice()).into(); @@ -796,7 +796,7 @@ where soft_confirmation.da_slot_hash(), soft_confirmation.da_slot_txs_commitment(), soft_confirmation.l1_fee_rate(), - soft_confirmation.txs(), + soft_confirmation.txs().into(), soft_confirmation.deposit_data(), borsh::to_vec(&signature).map_err(|e| anyhow!(e))?, borsh::to_vec(&pub_key).map_err(|e| anyhow!(e))?, diff --git a/crates/sovereign-sdk/full-node/db/sov-db/src/schema/types.rs b/crates/sovereign-sdk/full-node/db/sov-db/src/schema/types.rs index 13f6dbcd6..db006a1ec 100644 --- a/crates/sovereign-sdk/full-node/db/sov-db/src/schema/types.rs +++ b/crates/sovereign-sdk/full-node/db/sov-db/src/schema/types.rs @@ -164,7 +164,7 @@ impl From for BatchProofOutputRpcResponse { /// The on-disk format for a batch. Stores the hash and identifies the range of transactions /// included in the batch. -#[derive(Clone, Debug, PartialEq, BorshDeserialize, BorshSerialize)] +#[derive(Debug, PartialEq, BorshDeserialize, BorshSerialize)] pub struct StoredSoftConfirmation { /// The l2 height of the soft confirmation pub l2_height: u64, @@ -194,7 +194,7 @@ pub struct StoredSoftConfirmation { pub timestamp: u64, } -impl From for SignedSoftConfirmation { +impl From for SignedSoftConfirmation<'static> { fn from(value: StoredSoftConfirmation) -> Self { SignedSoftConfirmation::new( value.l2_height, diff --git a/crates/sovereign-sdk/module-system/sov-modules-api/src/hooks.rs b/crates/sovereign-sdk/module-system/sov-modules-api/src/hooks.rs index 79d63e93d..465aefde8 100644 --- a/crates/sovereign-sdk/module-system/sov-modules-api/src/hooks.rs +++ b/crates/sovereign-sdk/module-system/sov-modules-api/src/hooks.rs @@ -106,7 +106,7 @@ pub struct HookSoftConfirmationInfo { impl HookSoftConfirmationInfo { pub fn new( - signed_soft_confirmation: SignedSoftConfirmation, + signed_soft_confirmation: &SignedSoftConfirmation, pre_state_root: Vec, current_spec: SpecId, ) -> Self { diff --git a/crates/sovereign-sdk/module-system/sov-modules-stf-blueprint/src/lib.rs b/crates/sovereign-sdk/module-system/sov-modules-stf-blueprint/src/lib.rs index 181936fd4..bbca6d57d 100644 --- a/crates/sovereign-sdk/module-system/sov-modules-stf-blueprint/src/lib.rs +++ b/crates/sovereign-sdk/module-system/sov-modules-stf-blueprint/src/lib.rs @@ -252,7 +252,7 @@ where soft_confirmation.da_slot_height(), soft_confirmation.da_slot_hash(), soft_confirmation.da_slot_txs_commitment(), - soft_confirmation.txs().to_vec(), + soft_confirmation.txs(), soft_confirmation.deposit_data().to_vec(), soft_confirmation.l1_fee_rate(), soft_confirmation.timestamp(), @@ -457,7 +457,7 @@ where SoftConfirmationError, > { let soft_confirmation_info = HookSoftConfirmationInfo::new( - soft_confirmation.clone(), + soft_confirmation, pre_state_root.as_ref().to_vec(), current_spec, ); diff --git a/crates/sovereign-sdk/module-system/sov-modules-stf-blueprint/src/stf_blueprint.rs b/crates/sovereign-sdk/module-system/sov-modules-stf-blueprint/src/stf_blueprint.rs index 6ed4cb145..a94b9297f 100644 --- a/crates/sovereign-sdk/module-system/sov-modules-stf-blueprint/src/stf_blueprint.rs +++ b/crates/sovereign-sdk/module-system/sov-modules-stf-blueprint/src/stf_blueprint.rs @@ -209,7 +209,7 @@ where mut batch_workspace: WorkingSet, ) -> (ApplySoftConfirmationResult, StateCheckpoint) { let hook_soft_confirmation_info = - HookSoftConfirmationInfo::new(soft_confirmation.clone(), pre_state_root, current_spec); + HookSoftConfirmationInfo::new(soft_confirmation, pre_state_root, current_spec); if let Err(e) = self .runtime diff --git a/crates/sovereign-sdk/rollup-interface/src/state_machine/soft_confirmation.rs b/crates/sovereign-sdk/rollup-interface/src/state_machine/soft_confirmation.rs index 3e787483e..dbf95238a 100644 --- a/crates/sovereign-sdk/rollup-interface/src/state_machine/soft_confirmation.rs +++ b/crates/sovereign-sdk/rollup-interface/src/state_machine/soft_confirmation.rs @@ -3,6 +3,7 @@ extern crate alloc; +use alloc::borrow::Cow; use alloc::vec::Vec; use core::fmt::Debug; @@ -10,19 +11,19 @@ use borsh::{BorshDeserialize, BorshSerialize}; use serde::{Deserialize, Serialize}; /// Contains raw transactions and information about the soft confirmation block -#[derive(Debug, PartialEq, Clone, BorshDeserialize, BorshSerialize, Serialize, Deserialize)] -pub struct UnsignedSoftConfirmation { +#[derive(Debug, PartialEq, BorshSerialize)] +pub struct UnsignedSoftConfirmation<'txs> { l2_height: u64, da_slot_height: u64, da_slot_hash: [u8; 32], da_slot_txs_commitment: [u8; 32], - txs: Vec>, + txs: &'txs [Vec], deposit_data: Vec>, l1_fee_rate: u128, timestamp: u64, } -impl UnsignedSoftConfirmation { +impl<'txs> UnsignedSoftConfirmation<'txs> { #[allow(clippy::too_many_arguments)] /// Creates a new unsigned soft confirmation batch pub fn new( @@ -30,7 +31,7 @@ impl UnsignedSoftConfirmation { da_slot_height: u64, da_slot_hash: [u8; 32], da_slot_txs_commitment: [u8; 32], - txs: Vec>, + txs: &'txs [Vec], deposit_data: Vec>, l1_fee_rate: u128, timestamp: u64, @@ -63,8 +64,8 @@ impl UnsignedSoftConfirmation { self.da_slot_txs_commitment } /// Raw transactions. - pub fn txs(&self) -> Vec> { - self.txs.clone() + pub fn txs(&self) -> &[Vec] { + self.txs } /// Deposit data from L1 chain pub fn deposit_data(&self) -> Vec> { @@ -82,8 +83,8 @@ impl UnsignedSoftConfirmation { /// Signed version of the `UnsignedSoftConfirmation` /// Contains the signature and public key of the sequencer -#[derive(Debug, PartialEq, Clone, BorshDeserialize, BorshSerialize, Serialize, Deserialize, Eq)] -pub struct SignedSoftConfirmation { +#[derive(Debug, PartialEq, BorshDeserialize, BorshSerialize, Serialize, Deserialize, Eq)] +pub struct SignedSoftConfirmation<'txs> { l2_height: u64, hash: [u8; 32], prev_hash: [u8; 32], @@ -91,14 +92,14 @@ pub struct SignedSoftConfirmation { da_slot_hash: [u8; 32], da_slot_txs_commitment: [u8; 32], l1_fee_rate: u128, - txs: Vec>, + txs: Cow<'txs, [Vec]>, signature: Vec, deposit_data: Vec>, pub_key: Vec, timestamp: u64, } -impl SignedSoftConfirmation { +impl<'txs> SignedSoftConfirmation<'txs> { /// Creates a signed soft confirmation batch #[allow(clippy::too_many_arguments)] pub fn new( @@ -109,7 +110,7 @@ impl SignedSoftConfirmation { da_slot_hash: [u8; 32], da_slot_txs_commitment: [u8; 32], l1_fee_rate: u128, - txs: Vec>, + txs: Cow<'txs, [Vec]>, deposit_data: Vec>, signature: Vec, pub_key: Vec, @@ -168,7 +169,7 @@ impl SignedSoftConfirmation { /// Txs of signed batch pub fn txs(&self) -> &[Vec] { - self.txs.as_slice() + &self.txs } /// Deposit data @@ -181,11 +182,6 @@ impl SignedSoftConfirmation { self.signature.as_slice() } - /// Borsh serialized data - pub fn full_data(&mut self) -> Vec { - borsh::to_vec(self).unwrap() - } - /// L1 fee rate pub fn l1_fee_rate(&self) -> u128 { self.l1_fee_rate diff --git a/crates/sovereign-sdk/rollup-interface/src/state_machine/zk/mod.rs b/crates/sovereign-sdk/rollup-interface/src/state_machine/zk/mod.rs index 3b8c3316d..5bf7d0e45 100644 --- a/crates/sovereign-sdk/rollup-interface/src/state_machine/zk/mod.rs +++ b/crates/sovereign-sdk/rollup-interface/src/state_machine/zk/mod.rs @@ -181,7 +181,7 @@ pub trait Matches { // StateTransitionFunction, DA, and Zkvm traits. #[serde(bound = "StateRoot: Serialize + DeserializeOwned, Witness: Serialize + DeserializeOwned")] /// Data required to verify a state transition. -pub struct BatchProofCircuitInput { +pub struct BatchProofCircuitInput<'txs, StateRoot, Witness, Da: DaSpec> { /// The state root before the state transition pub initial_state_root: StateRoot, /// The state root after the state transition @@ -199,7 +199,7 @@ pub struct BatchProofCircuitInput { /// Pre-proven commitments L2 ranges which also exist in the current L1 `da_data`. pub preproven_commitments: Vec, /// The soft confirmations that are inside the sequencer commitments. - pub soft_confirmations: VecDeque>, + pub soft_confirmations: VecDeque>>, /// Corresponding witness for the soft confirmations. pub state_transition_witnesses: VecDeque>, /// DA block headers the soft confirmations was constructed on.