1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use fuel_core_interfaces::{
    common::{
        fuel_types::Word,
        prelude::Chargeable,
    },
    model::ArcPoolTx,
};
use std::cmp::Reverse;

// TODO: This logic should be housed in the txpool,
//  as it will have the most efficient way to select txs.

// transaction selection could use a plugin based approach in the
// future for block producers to customize block building (e.g. alternative priorities besides gas fees)

pub fn select_transactions(
    mut includable_txs: Vec<ArcPoolTx>,
    max_gas: u64,
) -> Vec<ArcPoolTx> {
    // Select all txs that fit into the block, preferring ones with higher gas price.
    //
    // Future improvements to this algorithm may take into account the parallel nature of
    // transactions to maximize throughput.
    let mut used_block_space: Word = 0;

    // Sort transactions by gas price, highest first
    includable_txs.sort_by_key(|a| Reverse(a.price()));

    // Pick as many transactions as we can fit into the block (greedy)
    includable_txs
        .into_iter()
        .filter(|tx| {
            let tx_block_space = tx.max_gas();
            if let Some(new_used_space) = used_block_space.checked_add(tx_block_space) {
                if new_used_space <= max_gas {
                    used_block_space = new_used_space;
                    true
                } else {
                    false
                }
            } else {
                false
            }
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use fuel_core_interfaces::common::{
        fuel_asm::Opcode,
        fuel_crypto::rand::{
            thread_rng,
            Rng,
        },
        fuel_tx::{
            ConsensusParameters,
            Output,
            TransactionBuilder,
        },
        fuel_vm::consts::REG_ONE,
    };
    use itertools::Itertools;
    use std::sync::Arc;

    use super::*;

    #[derive(Debug, Clone, Copy, PartialEq)]
    struct TxGas {
        pub price: u64,
        pub limit: u64,
    }

    /// A test helper that generates set of txs with given gas prices and limits and runs
    /// `select_transactions` against that, returning the list of selected gas price, limit pairs
    fn make_txs_and_select(txs: &[TxGas], block_gas_limit: Word) -> Vec<TxGas> {
        let mut rng = thread_rng();

        let txs = txs
            .iter()
            .map(|tx_gas| {
                TransactionBuilder::script(
                    vec![Opcode::RET(REG_ONE)].into_iter().collect(),
                    vec![],
                )
                .gas_price(tx_gas.price)
                .gas_limit(tx_gas.limit)
                .add_unsigned_coin_input(
                    rng.gen(),
                    rng.gen(),
                    1_000_000,
                    Default::default(),
                    Default::default(),
                    0,
                )
                .add_output(Output::Change {
                    to: Default::default(),
                    amount: 0,
                    asset_id: Default::default(),
                })
                // The block producer assumes transactions are already checked
                // so it doesn't need to compute valid sigs for tests
                .finalize_checked_basic(
                    0,
                    &ConsensusParameters {
                        gas_price_factor: 1,
                        ..ConsensusParameters::default()
                    },
                ).into()
            })
            .map(Arc::new)
            .collect();

        select_transactions(txs, block_gas_limit)
            .into_iter()
            .map(|tx| TxGas {
                limit: tx.limit(),
                price: tx.price(),
            })
            .collect()
    }

    #[test]
    fn selector_works_with_empty_input() {
        let selected = make_txs_and_select(&[], 1_000_000);
        assert!(selected.is_empty());
    }

    #[rstest::rstest]
    #[test]
    #[case(1000, vec![])]
    #[case(2500, vec![TxGas { price: 5, limit: 1000 }])]
    #[case(5000, vec![
        TxGas { price: 5, limit: 1000 },
        TxGas { price: 2, limit: 1000 }])
    ]
    #[case(7500, vec![
        TxGas { price: 5, limit: 1000 },
        TxGas { price: 4, limit: 3000 }
    ])]
    #[case(10_000, vec![
        TxGas { price: 5, limit: 1000 },
        TxGas { price: 4, limit: 3000 },
        TxGas { price: 2, limit: 1000 }
    ])]
    #[case(12_500, vec![
        TxGas { price: 5, limit: 1000 },
        TxGas { price: 4, limit: 3000 },
        TxGas { price: 3, limit: 2000 }
    ])]
    #[case(15_000, vec![
        TxGas { price: 5, limit: 1000 },
        TxGas { price: 4, limit: 3000 },
        TxGas { price: 3, limit: 2000 },
        TxGas { price: 2, limit: 1000 }
    ])]
    #[case(17_500, vec![
        TxGas { price: 5, limit: 1000 },
        TxGas { price: 4, limit: 3000 },
        TxGas { price: 3, limit: 2000 },
        TxGas { price: 2, limit: 1000 },
        TxGas { price: 1, limit: 1000 }
    ])]
    fn selector_prefers_highest_gas_txs_and_sorts(
        #[case] selection_limit: u64,
        #[case] expected_txs: Vec<TxGas>,
    ) {
        #[rustfmt::skip]
        let original = [
            TxGas { price: 3, limit: 2000 },
            TxGas { price: 1, limit: 1000 },
            TxGas { price: 4, limit: 3000 },
            TxGas { price: 5, limit: 1000 },
            TxGas { price: 2, limit: 1000 },
        ];

        let selected = make_txs_and_select(&original, selection_limit);
        assert_eq!(
            expected_txs, selected,
            "Wrong txs selected for max_gas: {}",
            selection_limit
        );
    }

    #[test]
    fn selector_doesnt_exceed_max_gas_per_block() {
        #[rustfmt::skip]
        let original = [
            TxGas { price: 3, limit: 2000 },
            TxGas { price: 1, limit: 1000 },
            TxGas { price: 4, limit: 3000 },
            TxGas { price: 5, limit: 1000 },
            TxGas { price: 2, limit: 1000 },
        ];

        for k in 0..original.len() {
            for perm in original.into_iter().permutations(k) {
                for gas_limit in [999, 1000, 2000, 2500, 3000, 5000, 6000, 10_000] {
                    let selected = make_txs_and_select(perm.as_slice(), gas_limit);
                    let total_gas: Word = selected.iter().map(|g| g.limit).sum();
                    assert!(total_gas <= gas_limit);
                }
            }
        }
    }
}