forked from risechain/pevm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
74 lines (61 loc) · 2.42 KB
/
mod.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
pub mod contract;
use contract::ERC20Token;
use pevm::{Bytecodes, ChainState, EvmAccount};
use revm::primitives::{uint, Address, TransactTo, TxEnv, U256};
pub const GAS_LIMIT: u64 = 26_938;
// TODO: Better randomness control. Sometimes we want duplicates to test
// dependent transactions, sometimes we want to guarantee non-duplicates
// for independent benchmarks.
fn generate_addresses(length: usize) -> Vec<Address> {
(0..length).map(|_| Address::new(rand::random())).collect()
}
pub fn generate_cluster(
num_families: usize,
num_people_per_family: usize,
num_transfers_per_person: usize,
) -> (ChainState, Bytecodes, Vec<TxEnv>) {
let families: Vec<Vec<Address>> = (0..num_families)
.map(|_| generate_addresses(num_people_per_family))
.collect();
let people_addresses: Vec<Address> = families.clone().into_iter().flatten().collect();
let gld_address = Address::new(rand::random());
let gld_account = ERC20Token::new("Gold Token", "GLD", 18, 222_222_000_000_000_000_000_000u128)
.add_balances(&people_addresses, uint!(1_000_000_000_000_000_000_U256))
.build();
let mut state = ChainState::from_iter([(gld_address, gld_account)]);
let mut txs = Vec::new();
for person in people_addresses.iter() {
state.insert(
*person,
EvmAccount {
balance: uint!(4_567_000_000_000_000_000_000_U256),
..EvmAccount::default()
},
);
}
for nonce in 0..num_transfers_per_person {
for family in families.iter() {
for person in family {
let recipient = family[(rand::random::<usize>()) % (family.len())];
let calldata = ERC20Token::transfer(recipient, U256::from(rand::random::<u8>()));
txs.push(TxEnv {
caller: *person,
gas_limit: GAS_LIMIT,
gas_price: U256::from(0xb2d05e07u64),
transact_to: TransactTo::Call(gld_address),
data: calldata,
nonce: Some(nonce as u64),
..TxEnv::default()
})
}
}
}
let mut bytecodes = Bytecodes::default();
for account in state.values_mut() {
let code = account.code.take();
if let Some(code) = code {
bytecodes.insert(account.code_hash.unwrap(), code);
}
}
(state, bytecodes, txs)
}