transaction_fuzzer/
account_universe.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::executor::{ExecutionResult, Executor};
use once_cell::sync::Lazy;
use proptest::{prelude::*, strategy::Union};
use std::{fmt, sync::Arc};
use sui_types::{storage::ObjectStore, transaction::Transaction};

mod account;
mod helpers;
mod transfer_gen;
mod universe;
pub use account::*;
pub use transfer_gen::*;
pub use universe::*;

static UNIVERSE_SIZE: Lazy<usize> = Lazy::new(|| {
    use std::env;

    match env::var("UNIVERSE_SIZE") {
        Ok(s) => match s.parse::<usize>() {
            Ok(val) => val,
            Err(err) => {
                panic!("Could not parse universe size, aborting: {:?}", err);
            }
        },
        Err(env::VarError::NotPresent) => 30,
        Err(err) => {
            panic!(
                "Could not read universe size from the environment, aborting: {:?}",
                err
            );
        }
    }
});

pub fn default_num_accounts() -> usize {
    *UNIVERSE_SIZE
}

pub fn default_num_transactions() -> usize {
    *UNIVERSE_SIZE * 2
}

/// Represents any sort of transaction that can be done in an account universe.
pub trait AUTransactionGen: fmt::Debug {
    /// Applies this transaction onto the universe, updating balances within the universe as
    /// necessary. Returns a signed transaction that can be run on the VM and the execution status.
    fn apply(
        &self,
        universe: &mut AccountUniverse,
        exec: &mut Executor,
    ) -> (Transaction, ExecutionResult);

    /// Creates an arced version of this transaction, suitable for dynamic dispatch.
    fn arced(self) -> Arc<dyn AUTransactionGen>
    where
        Self: 'static + Sized,
    {
        Arc::new(self)
    }
}

impl AUTransactionGen for Arc<dyn AUTransactionGen> {
    fn apply(
        &self,
        universe: &mut AccountUniverse,
        exec: &mut Executor,
    ) -> (Transaction, ExecutionResult) {
        (**self).apply(universe, exec)
    }
}

/// Returns a [`Strategy`] that provides a variety of balances (or transfer amounts) over a roughly
/// logarithmic distribution.
pub fn log_balance_strategy(min_balance: u64, max_balance: u64) -> impl Strategy<Value = u64> {
    // The logarithmic distribution is modeled by uniformly picking from ranges of powers of 2.
    assert!(max_balance >= min_balance, "minimum to make sense");
    let mut strategies = vec![];
    // Balances below and around the minimum are interesting but don't cover *every* power of 2,
    // just those starting from the minimum.
    let mut lower_bound: u64 = 0;
    let mut upper_bound: u64 = min_balance;
    loop {
        strategies.push(lower_bound..upper_bound);
        if upper_bound >= max_balance {
            break;
        }
        lower_bound = upper_bound;
        upper_bound = (upper_bound * 2).min(max_balance);
    }
    Union::new(strategies)
}

/// Run these transactions and verify the expected output.
pub fn run_and_assert_universe(
    universe: AccountUniverseGen,
    transaction_gens: Vec<impl AUTransactionGen + Clone>,
    executor: &mut Executor,
) -> Result<(), TestCaseError> {
    let mut universe = universe.setup(executor);
    let (transactions, expected_values): (Vec<_>, Vec<_>) = transaction_gens
        .iter()
        .map(|transaction_gen| transaction_gen.clone().apply(&mut universe, executor))
        .unzip();
    let outputs = executor.execute_transactions(transactions);
    prop_assert_eq!(outputs.len(), expected_values.len());

    for (idx, (output, expected)) in outputs.iter().zip(&expected_values).enumerate() {
        prop_assert!(
            output == expected,
            "unexpected status for transaction {} expected {:#?} but got {:#?}",
            idx,
            expected,
            output
        );
    }
    assert_accounts_match(&universe, executor)
}

pub fn assert_accounts_match(
    universe: &AccountUniverse,
    executor: &Executor,
) -> Result<(), TestCaseError> {
    let state = executor.state.clone();
    let backing_package_store = state.get_backing_package_store();
    let object_store = state.get_object_store();
    let epoch_store = state.load_epoch_store_one_call_per_task();
    let mut layout_resolver = epoch_store
        .executor()
        .type_layout_resolver(Box::new(backing_package_store.as_ref()));
    for (idx, account) in universe.accounts().iter().enumerate() {
        for (balance_idx, acc_object) in account.current_coins.iter().enumerate() {
            let object = object_store.get_object(&acc_object.id()).unwrap();
            let total_sui_value =
                object.get_total_sui(layout_resolver.as_mut()).unwrap() - object.storage_rebate;
            let account_balance_i = account.current_balances[balance_idx];
            prop_assert_eq!(
                account_balance_i,
                total_sui_value,
                "account {} should have correct balance {} for object {} but got {}",
                idx,
                total_sui_value,
                acc_object.id(),
                account_balance_i
            );
        }
    }
    Ok(())
}