sui_replay/fuzz_mutations/
shuffle_types.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::{Command, TransactionKind};
7use tracing::info;
8
9pub struct ShuffleTypes {
10    pub rng: rand::rngs::StdRng,
11    pub num_mutations_per_base_left: u64,
12}
13
14impl ShuffleTypes {
15    fn shuffle_command(&mut self, command: &mut Command) {
16        if let Command::MoveCall(pt) = command {
17            pt.type_arguments.shuffle(&mut self.rng)
18        }
19    }
20}
21
22impl TransactionKindMutator for ShuffleTypes {
23    fn mutate(&mut self, transaction_kind: &TransactionKind) -> Option<TransactionKind> {
24        if self.num_mutations_per_base_left == 0 {
25            // Nothing else to do
26            return None;
27        }
28
29        self.num_mutations_per_base_left -= 1;
30        if let TransactionKind::ProgrammableTransaction(mut p) = transaction_kind.clone() {
31            for command in &mut p.commands {
32                self.shuffle_command(command);
33            }
34            info!("Mutation: Shuffling types");
35            Some(TransactionKind::ProgrammableTransaction(p))
36        } else {
37            // Other types not supported yet
38            None
39        }
40    }
41
42    fn reset(&mut self, mutations_per_base: u64) {
43        self.num_mutations_per_base_left = mutations_per_base;
44    }
45}