iota_replay/fuzz_mutations/
drop_random_command_suffix.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::Rng;
7use tracing::info;
8
9use crate::fuzz::TransactionKindMutator;
10
11pub struct DropCommandSuffix {
12    pub rng: rand::rngs::StdRng,
13    pub num_mutations_per_base_left: u64,
14}
15
16impl TransactionKindMutator for DropCommandSuffix {
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            if p.commands.is_empty() {
26                return None;
27            }
28            let slice_index = self.rng.gen_range(0..p.commands.len());
29            p.commands.truncate(slice_index);
30            info!("Mutation: Dropping command suffix");
31            Some(TransactionKind::ProgrammableTransaction(p))
32        } else {
33            // Other types not supported yet
34            None
35        }
36    }
37
38    fn reset(&mut self, mutations_per_base: u64) {
39        self.num_mutations_per_base_left = mutations_per_base;
40    }
41}