iota_core/
post_consensus_tx_reorder.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use iota_metrics::monitored_scope;
6use iota_protocol_config::ConsensusTransactionOrdering;
7use iota_types::messages_consensus::{ConsensusTransaction, ConsensusTransactionKind};
8
9use crate::consensus_handler::{
10    SequencedConsensusTransactionKind, VerifiedSequencedConsensusTransaction,
11};
12
13pub struct PostConsensusTxReorder {}
14
15impl PostConsensusTxReorder {
16    pub fn reorder(
17        transactions: &mut [VerifiedSequencedConsensusTransaction],
18        kind: ConsensusTransactionOrdering,
19    ) {
20        // TODO: make the reordering algorithm richer and depend on object hotness as
21        // well. Order transactions based on their gas prices. System
22        // transactions without gas price are put to the beginning of the
23        // sequenced_transactions vector.
24        match kind {
25            ConsensusTransactionOrdering::ByGasPrice => Self::order_by_gas_price(transactions),
26            ConsensusTransactionOrdering::None => (),
27        }
28    }
29
30    fn order_by_gas_price(transactions: &mut [VerifiedSequencedConsensusTransaction]) {
31        let _scope = monitored_scope("HandleConsensusOutput::order_by_gas_price");
32        transactions.sort_by_key(|txn| {
33            // Reverse order, so that transactions with higher gas price are put to the
34            // beginning.
35            std::cmp::Reverse({
36                match &txn.0.transaction {
37                    SequencedConsensusTransactionKind::External(ConsensusTransaction {
38                        tracking_id: _,
39                        kind: ConsensusTransactionKind::UserTransaction(cert),
40                    }) => cert.gas_price(),
41                    // Non-user transactions are considered to have gas price of MAX u64 and are
42                    // put to the beginning.
43                    _ => u64::MAX,
44                }
45            })
46        })
47    }
48}