sui_replay/fuzz_mutations/
drop_random_command_suffix.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::fuzz::TransactionKindMutator;
5use rand::Rng;
6use sui_types::transaction::TransactionKind;
7use tracing::info;
8
9pub struct DropCommandSuffix {
10    pub rng: rand::rngs::StdRng,
11    pub num_mutations_per_base_left: u64,
12}
13
14impl TransactionKindMutator for DropCommandSuffix {
15    fn mutate(&mut self, transaction_kind: &TransactionKind) -> Option<TransactionKind> {
16        if self.num_mutations_per_base_left == 0 {
17            // Nothing else to do
18            return None;
19        }
20
21        self.num_mutations_per_base_left -= 1;
22        if let TransactionKind::ProgrammableTransaction(mut p) = transaction_kind.clone() {
23            if p.commands.is_empty() {
24                return None;
25            }
26            let slice_index = self.rng.gen_range(0..p.commands.len());
27            p.commands.truncate(slice_index);
28            info!("Mutation: Dropping command suffix");
29            Some(TransactionKind::ProgrammableTransaction(p))
30        } else {
31            // Other types not supported yet
32            None
33        }
34    }
35
36    fn reset(&mut self, mutations_per_base: u64) {
37        self.num_mutations_per_base_left = mutations_per_base;
38    }
39}