-
Notifications
You must be signed in to change notification settings - Fork 1
/
zkvm.rs
43 lines (40 loc) · 1.61 KB
/
zkvm.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
use super::TransactionMemorizer;
use crate::memorizer::{
keys::HeaderKey, keys::MemorizerKey, keys::TransactionKey, values::MemorizerValue,
HeaderMemorizer, Memorizer, MemorizerError,
};
use crate::mpt::Mpt;
use alloy_consensus::TxEnvelope;
use alloy_rlp::Decodable;
impl TransactionMemorizer for Memorizer {
fn get_transaction(&mut self, key: TransactionKey) -> Result<TxEnvelope, MemorizerError> {
// 1. Header
let header_key = HeaderKey {
block_number: key.block_number,
chain_id: key.chain_id,
};
let header = self.get_header(header_key)?;
// 2. Transaction
let tx_root = header.transactions_root;
let tx_key: MemorizerKey = key.into();
if let Some((MemorizerValue::Transaction(tx_value), is_verified)) =
self.map.get_mut(&tx_key)
{
if *is_verified {
println!("Transaction MPT already verified");
let tx_encoded = tx_value.transaction_encoded.clone();
Ok(TxEnvelope::decode(&mut tx_encoded.as_ref())?)
} else {
let mpt = Mpt { root: tx_root };
println!("cycle-tracker-start: mpt (transaction)");
mpt.verify_transaction(tx_value.tx_index, tx_value.proof.clone())?;
println!("cycle-tracker-end: mpt (transaction)");
*is_verified = true;
let tx_encoded = tx_value.transaction_encoded.clone();
Ok(TxEnvelope::decode(&mut tx_encoded.as_ref())?)
}
} else {
Err(MemorizerError::MissingTransaction)
}
}
}