Skip to content

Commit c820e65

Browse files
committed
add example
1 parent 0aebf1c commit c820e65

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

examples/insert/main.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
use std::env;
2+
3+
use alloy_primitives::{Address, StorageKey, StorageValue, U256};
4+
use alloy_trie::{EMPTY_ROOT_HASH, KECCAK_EMPTY};
5+
use rand::prelude::*;
6+
use triedb::{
7+
account::Account,
8+
path::{AddressPath, StoragePath},
9+
transaction::TransactionError,
10+
Database,
11+
};
12+
13+
pub const DEFAULT_SETUP_DB_EOA_SIZE: usize = 1_000_000;
14+
pub const DEFAULT_SETUP_DB_CONTRACT_SIZE: usize = 100_000;
15+
pub const DEFAULT_SETUP_DB_STORAGE_PER_CONTRACT: usize = 10;
16+
const SEED_EOA: u64 = 42; // EOA seeding value
17+
const SEED_CONTRACT: u64 = 43; // contract account seeding value
18+
19+
pub fn generate_random_address(rng: &mut StdRng) -> AddressPath {
20+
let addr = Address::random_with(rng);
21+
AddressPath::for_address(addr)
22+
}
23+
24+
pub fn setup_database(
25+
db: &Database,
26+
repeat: usize,
27+
eoa_size: usize,
28+
contract_size: usize,
29+
storage_per_contract: usize,
30+
) -> Result<(), TransactionError> {
31+
// Populate database with initial accounts
32+
let mut eoa_rng = StdRng::seed_from_u64(SEED_EOA);
33+
let mut contract_rng = StdRng::seed_from_u64(SEED_CONTRACT);
34+
for _i in 0..repeat {
35+
let mut tx = db.begin_rw()?;
36+
for i in 1..=eoa_size {
37+
let address = generate_random_address(&mut eoa_rng);
38+
let account =
39+
Account::new(i as u64, U256::from(i as u64), EMPTY_ROOT_HASH, KECCAK_EMPTY);
40+
41+
tx.set_account(address, Some(account))?;
42+
}
43+
44+
for i in 1..=contract_size {
45+
let address = generate_random_address(&mut contract_rng);
46+
let account =
47+
Account::new(i as u64, U256::from(i as u64), EMPTY_ROOT_HASH, KECCAK_EMPTY);
48+
49+
tx.set_account(address.clone(), Some(account))?;
50+
51+
// add random storage to each account
52+
for key in 1..=storage_per_contract {
53+
let storage_key = StorageKey::from(U256::from(key));
54+
let storage_path =
55+
StoragePath::for_address_path_and_slot(address.clone(), storage_key);
56+
let storage_value =
57+
StorageValue::from_be_slice(storage_path.get_slot().pack().as_slice());
58+
59+
tx.set_storage_slot(storage_path, Some(storage_value))?;
60+
}
61+
}
62+
63+
tx.commit()?;
64+
}
65+
println!("root hash: {:?}", db.state_root());
66+
67+
Ok(())
68+
}
69+
70+
fn main() {
71+
let args: Vec<String> = env::args().collect();
72+
73+
let db_path = args.get(1).unwrap();
74+
let repeat = args.get(2).and_then(|s| s.parse::<usize>().ok()).unwrap_or(1);
75+
let eoa_size =
76+
args.get(3).and_then(|s| s.parse::<usize>().ok()).unwrap_or(DEFAULT_SETUP_DB_EOA_SIZE);
77+
let contract_size =
78+
args.get(4).and_then(|s| s.parse::<usize>().ok()).unwrap_or(DEFAULT_SETUP_DB_CONTRACT_SIZE);
79+
let storage_per_contract = args
80+
.get(5)
81+
.and_then(|s| s.parse::<usize>().ok())
82+
.unwrap_or(DEFAULT_SETUP_DB_STORAGE_PER_CONTRACT);
83+
84+
let db = Database::create_new(db_path).unwrap();
85+
86+
println!("eoa size: {eoa_size}");
87+
println!("repeat {repeat} times");
88+
println!("contract size: {contract_size}, storage per contract: {storage_per_contract}");
89+
90+
setup_database(&db, repeat, eoa_size, contract_size, storage_per_contract).unwrap();
91+
}

0 commit comments

Comments
 (0)