iota_replay/fuzz_mutations/
shuffle_commands.rs

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