Repository: MystenLabs/sui
File: crates/sui-core/src/consensus_handler.rs (Rust)

Code before:
                .expect("db error")
            {
                self.metrics.skipped_consensus_txns.inc();
                continue;
            }

            first_commit_keys.insert(key.clone());

            state.output.record_consensus_message_processed(key);

            all_transactions.push(verified_transaction);
        }

        for key in first_commit_keys {
            if let Some(&count) = occurrence_counts.get(&key)
                && count > 1
            {
                self.metrics
                    .consensus_handler_duplicate_tx_count
                    .observe(count as f64);
            }
        }

        // Copy user transaction occurrence counts to state for unpaid amplification detection.
        state.occurrence_counts.reserve(occurrence_counts.len());
        state.occurrence_counts.extend(
            occurrence_counts
                .into_iter()
                .filter_map(|(key, count)| key.user_transaction_digest().map(|d| (d, count))),
        );

        all_transactions
    }

    fn build_commit_handler_input(
        &self,
        transactions: Vec<VerifiedSequencedConsensusTransaction>,
    ) -> CommitHandlerInput {
        let epoch = self.epoch_store.epoch();
        let mut commit_handler_input = CommitHandlerInput::default();

        for VerifiedSequencedConsensusTransaction(transaction) in transactions.into_iter() {
            match transaction.transaction {
                SequencedConsensusTransactionKind::External(consensus_transaction) => {
                    match consensus_transaction.kind {
                        // === User transactions ===
                        ConsensusTransactionKind::CertifiedTransaction(cert) => {
                            // Safe because signatures are verified when consensus called into SuiTxValidator::validate_batch.
                            let cert = VerifiedCertificate::new_unchecked(*cert);
                            let transaction =
                                VerifiedExecutableTransaction::new_from_certificate(cert);

Review comment (suggestion):
Could we assert that `state.occurrence_counts` is still empty here? Want to make sure we don't accidentally modify it beforehand.

Code after:
                continue;
            }

            first_commit_keys.insert(key.clone());

            state.output.record_consensus_message_processed(key);

            all_transactions.push(verified_transaction);
        }

        for key in first_commit_keys {
            if let Some(&count) = occurrence_counts.get(&key)
                && count > 1
            {
                self.metrics
                    .consensus_handler_duplicate_tx_count
                    .observe(count as f64);
            }
        }

        // Copy user transaction occurrence counts to state for unpaid amplification detection.
        assert!(
            state.occurrence_counts.is_empty(),
            "occurrence_counts should be empty before populating"
        );
        state.occurrence_counts.reserve(occurrence_counts.len());
        state.occurrence_counts.extend(
            occurrence_counts
                .into_iter()
                .filter_map(|(key, count)| key.user_transaction_digest().map(|d| (d, count))),
        );

        all_transactions
    }

    fn build_commit_handler_input(
        &self,
        transactions: Vec<VerifiedSequencedConsensusTransaction>,
    ) -> CommitHandlerInput {
        let epoch = self.epoch_store.epoch();
        let mut commit_handler_input = CommitHandlerInput::default();

        for VerifiedSequencedConsensusTransaction(transaction) in transactions.into_iter() {
            match transaction.transaction {
                SequencedConsensusTransactionKind::External(consensus_transaction) => {
                    match consensus_transaction.kind {
                        // === User transactions ===
                        ConsensusTransactionKind::CertifiedTransaction(cert) => {
                            // Safe because signatures are verified when consensus called into SuiTxValidator::validate_batch.
                            let cert = VerifiedCertificate::new_unchecked(*cert);
                            let transaction =