forked from zemse/partial-mpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainnet-block-1000008.rs
98 lines (89 loc) · 3.16 KB
/
mainnet-block-1000008.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
use dotenvy::dotenv;
use ethers::{
providers::{Middleware, Provider},
types::{Address, BlockId, BlockNumber, H256},
};
use partial_mpt::StateTrie;
#[tokio::main]
async fn main() {
dotenv().ok();
let api_key = std::env::var("ALCHEMY_API_KEY").unwrap();
let rpc_url = format!("https://eth-mainnet.g.alchemy.com/v2/{}", api_key);
let provider = Provider::try_from(rpc_url).unwrap();
let prev_block_number = Some(BlockId::Number(BlockNumber::from(1000007)));
let current_block_number = Some(BlockId::Number(BlockNumber::from(1000008)));
let prev_block = provider
.get_block(prev_block_number.unwrap())
.await
.unwrap()
.unwrap();
let mut state_trie = StateTrie::from_root(prev_block.state_root);
// block 1000008 only has one tx where sender sends funds to receiver.
let sender_address = "0x2a65Aca4D5fC5B5C859090a6c34d164135398226"
.parse::<Address>()
.unwrap();
let receiver_address = "0xb6046a76bD03474b16aD52B1fC581CD5a2465Bd3"
.parse::<Address>()
.unwrap();
let miner_address = "0x68795C4AA09D6f4Ed3E5DeDDf8c2AD3049A601da"
.parse::<Address>()
.unwrap();
// download EIP-1186 state proofs for these three accounts.
let proof_sender = provider
.get_proof(sender_address, vec![H256::zero()], prev_block_number)
.await
.unwrap();
let proof_receiver = provider
.get_proof(receiver_address, vec![H256::zero()], prev_block_number)
.await
.unwrap();
let proof_miner = provider
.get_proof(miner_address, vec![H256::zero()], prev_block_number)
.await
.unwrap();
state_trie.load_proof(proof_sender).unwrap();
state_trie.load_proof(proof_receiver).unwrap();
state_trie.load_proof(proof_miner).unwrap();
// sender balance decreases, nonce increases.
// receiver and miner both balance increases.
let sender_new_balance = provider
.get_balance(sender_address, current_block_number)
.await
.unwrap();
let sender_new_nonce = provider
.get_transaction_count(sender_address, current_block_number)
.await
.unwrap();
let receiver_new_balance = provider
.get_balance(receiver_address, current_block_number)
.await
.unwrap();
let miner_new_balance = provider
.get_balance(miner_address, current_block_number)
.await
.unwrap();
state_trie
.account_trie
.set_balance(sender_address, sender_new_balance)
.unwrap();
state_trie
.account_trie
.set_nonce(sender_address, sender_new_nonce)
.unwrap();
state_trie
.account_trie
.set_balance(receiver_address, receiver_new_balance)
.unwrap();
state_trie
.account_trie
.set_balance(miner_address, miner_new_balance)
.unwrap();
// now our trie should have calculated the state root correctly.
let current_block = provider
.get_block(current_block_number.unwrap())
.await
.unwrap()
.unwrap();
assert_eq!(state_trie.root().unwrap(), current_block.state_root);
println!("state root matched!");
}