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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
use crate::{
account::AccountData,
account_universe::{default_num_accounts, default_num_transactions, AccountCurrent},
executor::FakeExecutor,
};
use diem_proptest_helpers::{pick_slice_idxs, Index};
use proptest::{
collection::{vec, SizeRange},
prelude::*,
};
use proptest_derive::Arbitrary;
#[derive(Clone, Debug)]
pub struct AccountUniverseGen {
accounts: Vec<AccountData>,
pick_style: AccountPickStyle,
}
#[derive(Clone, Debug)]
pub struct AccountUniverse {
accounts: Vec<AccountCurrent>,
picker: AccountPicker,
ignore_new_accounts: bool,
}
#[derive(Arbitrary, Clone, Debug)]
pub struct AccountPairGen {
pair: [Index; 2],
reverse: bool,
}
#[derive(Clone, Debug)]
pub enum AccountPickStyle {
Unlimited,
Limited(usize),
}
impl AccountUniverseGen {
pub fn strategy(
num_accounts: impl Into<SizeRange>,
balance_strategy: impl Strategy<Value = u64>,
) -> impl Strategy<Value = Self> {
vec(AccountData::strategy(balance_strategy), num_accounts).prop_map(|accounts| Self {
accounts,
pick_style: AccountPickStyle::Unlimited,
})
}
pub fn success_strategy(min_accounts: usize) -> impl Strategy<Value = Self> {
let min_balance = (100_000 * (default_num_transactions()) * 5) as u64;
let max_balance = min_balance * 10;
Self::strategy(
min_accounts..default_num_accounts(),
min_balance..max_balance,
)
}
pub fn set_pick_style(&mut self, pick_style: AccountPickStyle) -> &mut Self {
self.pick_style = pick_style;
self
}
pub fn num_accounts(&self) -> usize {
self.accounts.len()
}
pub fn setup(self, executor: &mut FakeExecutor) -> AccountUniverse {
for account_data in &self.accounts {
executor.add_account_data(account_data);
}
AccountUniverse::new(self.accounts, self.pick_style, false)
}
pub fn setup_gas_cost_stability(self, executor: &mut FakeExecutor) -> AccountUniverse {
for account_data in &self.accounts {
executor.add_account_data(account_data);
}
AccountUniverse::new(self.accounts, self.pick_style, true)
}
}
impl AccountUniverse {
fn new(
accounts: Vec<AccountData>,
pick_style: AccountPickStyle,
ignore_new_accounts: bool,
) -> Self {
let accounts: Vec<_> = accounts.into_iter().map(AccountCurrent::new).collect();
let picker = AccountPicker::new(pick_style, accounts.len());
Self {
accounts,
picker,
ignore_new_accounts,
}
}
pub fn num_accounts(&self) -> usize {
self.accounts.len()
}
pub fn accounts(&self) -> &[AccountCurrent] {
&self.accounts
}
pub fn add_account(&mut self, account_data: AccountData) {
if !self.ignore_new_accounts {
self.accounts.push(AccountCurrent::new(account_data));
}
}
pub fn pick(&mut self, index: Index) -> (usize, &mut AccountCurrent) {
let idx = self.picker.pick(index);
(idx, &mut self.accounts[idx])
}
}
#[derive(Clone, Debug)]
enum AccountPicker {
Unlimited(usize),
Limited(Vec<(usize, usize)>),
}
impl AccountPicker {
fn new(pick_style: AccountPickStyle, num_accounts: usize) -> Self {
match pick_style {
AccountPickStyle::Unlimited => AccountPicker::Unlimited(num_accounts),
AccountPickStyle::Limited(limit) => {
let remaining = (0..num_accounts).map(|idx| (idx, limit)).collect();
AccountPicker::Limited(remaining)
}
}
}
fn pick(&mut self, index: Index) -> usize {
match self {
AccountPicker::Unlimited(num_accounts) => index.index(*num_accounts),
AccountPicker::Limited(remaining) => {
let remaining_idx = index.index(remaining.len());
Self::pick_limited(remaining, remaining_idx)
}
}
}
fn pick_pair(&mut self, indexes: &[Index; 2]) -> [usize; 2] {
match self {
AccountPicker::Unlimited(num_accounts) => Self::pick_pair_impl(*num_accounts, indexes),
AccountPicker::Limited(remaining) => {
let [remaining_idx_1, remaining_idx_2] =
Self::pick_pair_impl(remaining.len(), indexes);
let account_idx_2 = Self::pick_limited(remaining, remaining_idx_2);
let account_idx_1 = Self::pick_limited(remaining, remaining_idx_1);
[account_idx_1, account_idx_2]
}
}
}
fn pick_pair_impl(max: usize, indexes: &[Index; 2]) -> [usize; 2] {
let idxs = pick_slice_idxs(max, indexes);
assert_eq!(idxs.len(), 2);
let idxs = [idxs[0], idxs[1]];
assert!(
idxs[0] < idxs[1],
"pick_slice_idxs should return sorted order"
);
idxs
}
fn pick_limited(remaining: &mut Vec<(usize, usize)>, remaining_idx: usize) -> usize {
let (account_idx, times_remaining) = {
let (account_idx, times_remaining) = &mut remaining[remaining_idx];
*times_remaining -= 1;
(*account_idx, *times_remaining)
};
if times_remaining == 0 {
remaining.remove(remaining_idx);
}
account_idx
}
}
impl AccountPairGen {
pub fn pick<'a>(&self, universe: &'a mut AccountUniverse) -> AccountPair<'a> {
let [low_idx, high_idx] = universe.picker.pick_pair(&self.pair);
let (head, tail) = universe.accounts.split_at_mut(low_idx + 1);
let (low_account, high_account) = (&mut head[low_idx], &mut tail[high_idx - low_idx - 1]);
if self.reverse {
AccountPair {
idx_1: high_idx,
idx_2: low_idx,
account_1: high_account,
account_2: low_account,
}
} else {
AccountPair {
idx_1: low_idx,
idx_2: high_idx,
account_1: low_account,
account_2: high_account,
}
}
}
}
pub struct AccountPair<'a> {
pub idx_1: usize,
pub idx_2: usize,
pub account_1: &'a mut AccountCurrent,
pub account_2: &'a mut AccountCurrent,
}