sui_replay/fuzz_mutations/
drop_random_commands.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::fuzz::TransactionKindMutator;
5use rand::seq::SliceRandom;
6use sui_types::transaction::TransactionKind;
7use tracing::info;
8
9pub struct DropRandomCommands {
10    pub rng: rand::rngs::StdRng,
11    pub num_mutations_per_base_left: u64,
12}
13
14impl TransactionKindMutator for DropRandomCommands {
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            p.commands = p
27                .commands
28                .choose_multiple(&mut self.rng, p.commands.len() - 1)
29                .cloned()
30                .collect();
31            info!("Mutation: Dropping random commands");
32            Some(TransactionKind::ProgrammableTransaction(p))
33        } else {
34            // Other types not supported yet
35            None
36        }
37    }
38
39    fn reset(&mut self, mutations_per_base: u64) {
40        self.num_mutations_per_base_left = mutations_per_base;
41    }
42}