Skip to content

Commit

Permalink
Don't clone SignedSoftConfirmation::txs (#1433)
Browse files Browse the repository at this point in the history
  • Loading branch information
kpp authored Nov 5, 2024
1 parent 0b07b0a commit 37aba6a
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 41 deletions.
10 changes: 5 additions & 5 deletions crates/batch-prover/src/da_block_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Witness, Da> = (
type CommitmentStateTransitionData<'txs, Witness, Da> = (
VecDeque<Vec<Witness>>,
VecDeque<Vec<SignedSoftConfirmation>>,
VecDeque<Vec<SignedSoftConfirmation<'txs>>>,
VecDeque<Vec<<<Da as DaService>::Spec as DaSpec>::BlockHeader>>,
);

Expand Down Expand Up @@ -341,7 +341,7 @@ pub(crate) async fn get_batch_proof_circuit_input_from_commitments<
da_service: &Arc<Da>,
ledger_db: &DB,
l1_block_cache: &Arc<Mutex<L1BlockCache<Da>>>,
) -> Result<CommitmentStateTransitionData<Witness, Da>, anyhow::Error> {
) -> Result<CommitmentStateTransitionData<'static, Witness, Da>, anyhow::Error> {
let mut state_transition_witnesses: VecDeque<Vec<Witness>> = VecDeque::new();
let mut soft_confirmations: VecDeque<Vec<SignedSoftConfirmation>> = VecDeque::new();
let mut da_block_headers_of_soft_confirmations: VecDeque<
Expand Down Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions crates/batch-prover/src/proving.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub(crate) async fn data_to_prove<Da, DB, StateRoot, Witness>(
) -> Result<
(
Vec<SequencerCommitment>,
Vec<BatchProofCircuitInput<StateRoot, Witness, Da::Spec>>,
Vec<BatchProofCircuitInput<'static, StateRoot, Witness, Da::Spec>>,
),
L1ProcessingError,
>
Expand Down Expand Up @@ -192,7 +192,7 @@ pub(crate) async fn prove_l1<Da, Ps, Vm, DB, StateRoot, Witness>(
code_commitments_by_spec: HashMap<SpecId, Vm::CodeCommitment>,
l1_block: Da::FilteredBlock,
sequencer_commitments: Vec<SequencerCommitment>,
inputs: Vec<BatchProofCircuitInput<StateRoot, Witness, Da::Spec>>,
inputs: Vec<BatchProofCircuitInput<'_, StateRoot, Witness, Da::Spec>>,
) -> anyhow::Result<()>
where
Da: DaService,
Expand Down
2 changes: 1 addition & 1 deletion crates/batch-prover/tests/prover_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn make_new_prover(thread_pool_size: usize, da_service: Arc<MockDaService>) -> T

fn make_transition_data(
header_hash: MockHash,
) -> BatchProofCircuitInput<[u8; 0], Vec<u8>, MockDaSpec> {
) -> BatchProofCircuitInput<'static, [u8; 0], Vec<u8>, MockDaSpec> {
BatchProofCircuitInput {
initial_state_root: [],
final_state_root: [],
Expand Down
2 changes: 1 addition & 1 deletion crates/sequencer-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ pub struct GetSoftConfirmationResponse {
pub timestamp: u64,
}

impl From<GetSoftConfirmationResponse> for SignedSoftConfirmation {
impl From<GetSoftConfirmationResponse> for SignedSoftConfirmation<'static> {
fn from(val: GetSoftConfirmationResponse) -> Self {
SignedSoftConfirmation::new(
val.l2_height,
Expand Down
12 changes: 6 additions & 6 deletions crates/sequencer/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<SignedSoftConfirmation> {
) -> anyhow::Result<SignedSoftConfirmation<'txs>> {
let raw = borsh::to_vec(&soft_confirmation).map_err(|e| anyhow!(e))?;

let hash = <C as sov_modules_api::Spec>::Hasher::digest(raw.as_slice()).into();
Expand All @@ -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))?,
Expand Down
4 changes: 2 additions & 2 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 @@ -164,7 +164,7 @@ impl From<StoredBatchProofOutput> 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,
Expand Down Expand Up @@ -194,7 +194,7 @@ pub struct StoredSoftConfirmation {
pub timestamp: u64,
}

impl From<StoredSoftConfirmation> for SignedSoftConfirmation {
impl From<StoredSoftConfirmation> for SignedSoftConfirmation<'static> {
fn from(value: StoredSoftConfirmation) -> Self {
SignedSoftConfirmation::new(
value.l2_height,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub struct HookSoftConfirmationInfo {

impl HookSoftConfirmationInfo {
pub fn new(
signed_soft_confirmation: SignedSoftConfirmation,
signed_soft_confirmation: &SignedSoftConfirmation,
pre_state_root: Vec<u8>,
current_spec: SpecId,
) -> Self {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ where
mut batch_workspace: WorkingSet<C>,
) -> (ApplySoftConfirmationResult<Da>, StateCheckpoint<C>) {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,35 @@
extern crate alloc;

use alloc::borrow::Cow;
use alloc::vec::Vec;
use core::fmt::Debug;

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<Vec<u8>>,
txs: &'txs [Vec<u8>],
deposit_data: Vec<Vec<u8>>,
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(
l2_height: u64,
da_slot_height: u64,
da_slot_hash: [u8; 32],
da_slot_txs_commitment: [u8; 32],
txs: Vec<Vec<u8>>,
txs: &'txs [Vec<u8>],
deposit_data: Vec<Vec<u8>>,
l1_fee_rate: u128,
timestamp: u64,
Expand Down Expand Up @@ -63,8 +64,8 @@ impl UnsignedSoftConfirmation {
self.da_slot_txs_commitment
}
/// Raw transactions.
pub fn txs(&self) -> Vec<Vec<u8>> {
self.txs.clone()
pub fn txs(&self) -> &[Vec<u8>] {
self.txs
}
/// Deposit data from L1 chain
pub fn deposit_data(&self) -> Vec<Vec<u8>> {
Expand All @@ -82,23 +83,23 @@ 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],
da_slot_height: u64,
da_slot_hash: [u8; 32],
da_slot_txs_commitment: [u8; 32],
l1_fee_rate: u128,
txs: Vec<Vec<u8>>,
txs: Cow<'txs, [Vec<u8>]>,
signature: Vec<u8>,
deposit_data: Vec<Vec<u8>>,
pub_key: Vec<u8>,
timestamp: u64,
}

impl SignedSoftConfirmation {
impl<'txs> SignedSoftConfirmation<'txs> {
/// Creates a signed soft confirmation batch
#[allow(clippy::too_many_arguments)]
pub fn new(
Expand All @@ -109,7 +110,7 @@ impl SignedSoftConfirmation {
da_slot_hash: [u8; 32],
da_slot_txs_commitment: [u8; 32],
l1_fee_rate: u128,
txs: Vec<Vec<u8>>,
txs: Cow<'txs, [Vec<u8>]>,
deposit_data: Vec<Vec<u8>>,
signature: Vec<u8>,
pub_key: Vec<u8>,
Expand Down Expand Up @@ -168,7 +169,7 @@ impl SignedSoftConfirmation {

/// Txs of signed batch
pub fn txs(&self) -> &[Vec<u8>] {
self.txs.as_slice()
&self.txs
}

/// Deposit data
Expand All @@ -181,11 +182,6 @@ impl SignedSoftConfirmation {
self.signature.as_slice()
}

/// Borsh serialized data
pub fn full_data(&mut self) -> Vec<u8> {
borsh::to_vec(self).unwrap()
}

/// L1 fee rate
pub fn l1_fee_rate(&self) -> u128 {
self.l1_fee_rate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ pub trait Matches<T> {
// StateTransitionFunction, DA, and Zkvm traits.
#[serde(bound = "StateRoot: Serialize + DeserializeOwned, Witness: Serialize + DeserializeOwned")]
/// Data required to verify a state transition.
pub struct BatchProofCircuitInput<StateRoot, Witness, Da: DaSpec> {
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
Expand All @@ -199,7 +199,7 @@ pub struct BatchProofCircuitInput<StateRoot, Witness, Da: DaSpec> {
/// Pre-proven commitments L2 ranges which also exist in the current L1 `da_data`.
pub preproven_commitments: Vec<usize>,
/// The soft confirmations that are inside the sequencer commitments.
pub soft_confirmations: VecDeque<Vec<SignedSoftConfirmation>>,
pub soft_confirmations: VecDeque<Vec<SignedSoftConfirmation<'txs>>>,
/// Corresponding witness for the soft confirmations.
pub state_transition_witnesses: VecDeque<Vec<Witness>>,
/// DA block headers the soft confirmations was constructed on.
Expand Down

0 comments on commit 37aba6a

Please sign in to comment.