Skip to content

Commit 22cba34

Browse files
Merge pull request #16 from Lagrange-Labs/distributed-query-integration-modifications
Modifications for integration with `distributed-query`
2 parents e8bc6ce + e797ff1 commit 22cba34

File tree

6 files changed

+38
-32
lines changed

6 files changed

+38
-32
lines changed

src/eth.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use eth_trie::{EthTrie, MemoryDB, Node, Trie};
55
use ethers::{
66
providers::{Http, Middleware, Provider},
77
types::{Block, BlockId, Bytes, Transaction, TransactionReceipt, U64},
8+
utils::hex,
89
};
910
use rlp::{Encodable, Rlp, RlpStream};
1011
#[cfg(feature = "ci")]
@@ -14,7 +15,7 @@ use std::sync::Arc;
1415
use crate::utils::keccak256;
1516
/// A wrapper around a transaction and its receipt. The receipt is used to filter
1617
/// bad transactions, so we only compute over valid transactions.
17-
pub(crate) struct TxAndReceipt(Transaction, TransactionReceipt);
18+
pub struct TxAndReceipt(Transaction, TransactionReceipt);
1819

1920
impl TxAndReceipt {
2021
pub fn tx(&self) -> &Transaction {
@@ -188,7 +189,7 @@ pub(crate) fn rlp_opt<T: rlp::Encodable>(rlp: &mut rlp::RlpStream, opt: &Option<
188189
}
189190

190191
/// Extract the hash in case of Extension node, or all the hashes in case of a Branch node.
191-
pub(crate) fn extract_child_hashes(rlp_data: &[u8]) -> Vec<Vec<u8>> {
192+
pub fn extract_child_hashes(rlp_data: &[u8]) -> Vec<Vec<u8>> {
192193
let rlp = Rlp::new(rlp_data);
193194
let mut hashes = Vec::new();
194195

src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ use serde::{Deserialize, Serialize};
1616

1717
mod benches;
1818
mod eth;
19+
1920
mod hash;
2021
mod rlp;
21-
mod transaction;
22+
pub mod transaction;
2223
mod utils;
2324

2425
/// Bundle containing the raw proof, the verification key, and some common data

src/transaction/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
mod header;
22
mod mpt;
3-
mod proof;
4-
#[cfg(test)]
5-
mod prover;
3+
pub mod proof;
4+
pub mod prover;
65

76
/// Length of a hash in bytes.
87
const HASH_LEN: usize = 32;

src/transaction/mpt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const HASH_LENGTH: usize = 32;
4343

4444
/// There are different ways to extract values from a transaction. This enum
4545
/// list some.
46-
pub(crate) enum ExtractionMethod {
46+
pub enum ExtractionMethod {
4747
/// RLPBased decodes each header consecutively and extract the gas value
4848
/// TODO: Currently hardcode that the gas value is 3rd item in the tx list
4949
/// because we use const generics and can't pass the index as a parameter.

src/transaction/proof.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ pub struct TransactionMPT {
6161
}
6262

6363
impl TransactionMPT {
64-
fn compute_proof<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const D: usize>(
64+
pub fn compute_proof<
65+
F: RichField + Extendable<D>,
66+
C: GenericConfig<D, F = F>,
67+
const D: usize,
68+
>(
6569
self,
6670
) -> Result<ProofTuple<F, C, D>> {
6771
let config = CircuitConfig::standard_recursion_config();
@@ -138,8 +142,8 @@ impl RootMPTHeader {
138142
}
139143
}
140144

141-
struct HeaderAggregation {
142-
header_proofs: Vec<Vec<u8>>,
145+
pub struct HeaderAggregation {
146+
pub header_proofs: Vec<Vec<u8>>,
143147
}
144148

145149
impl HeaderAggregation {

src/transaction/prover.rs

+23-22
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
use crate::eth::RLPBlock;
2-
use crate::hash::hash_to_fields;
32
use crate::transaction::proof::{IntermediateMPT, ProofType, RootMPTHeader, TransactionMPT};
4-
use crate::transaction::PACKED_HASH_LEN;
5-
use crate::ByteProofTuple;
63
use crate::{
74
eth::{extract_child_hashes, BlockData},
85
utils::keccak256,
@@ -16,7 +13,7 @@ use rlp::Encodable;
1613
use std::collections::HashMap;
1714

1815
#[derive(Debug, Default)]
19-
pub(crate) enum TxFilter {
16+
pub enum TxFilter {
2017
// only prove tx that are less than this size in bytes
2118
BySize(usize),
2219
// only prove the tx which have these hashes
@@ -41,28 +38,29 @@ impl TxFilter {
4138
}
4239
}
4340
}
44-
struct TxBlockProver {
45-
data: BlockData,
46-
policy: TxFilter,
41+
pub struct TxBlockProver {
42+
pub data: BlockData,
43+
pub policy: TxFilter,
4744
#[cfg(test)]
48-
nb_nodes: usize,
45+
pub nb_nodes: usize,
4946
}
5047

51-
struct MPTNode {
52-
node_bytes: Vec<u8>, // RLP byte representation of the node
53-
hash: Vec<u8>, // its hash
48+
#[derive(Debug, PartialEq, Eq, Clone)]
49+
pub struct MPTNode {
50+
pub node_bytes: Vec<u8>, // RLP byte representation of the node
51+
pub hash: Vec<u8>, // its hash
5452
// ALL the children of this node if any (zero if leaf for example)
55-
total_nb_children: usize,
53+
pub total_nb_children: usize,
5654
// expected children hashes - will be filled in with only the hashes that we
5755
// traverse in the proofs of all tx we are looking at. For the nodes that we don't
5856
// traverse, their hash may be in children_hashes, and we'll only provide a "null" proof
5957
// for them.
60-
exp_children: Vec<Vec<u8>>,
58+
pub exp_children: Vec<Vec<u8>>,
6159
// child i : (key, proof) - key needed locate where is the hash of the child in the node
62-
rcvd_children_proofs: Vec<ProverOutput>, // will be filled in
63-
parent_hash: Option<Vec<u8>>, // indicator to go up one level when this node has been "proven"
60+
pub rcvd_children_proofs: Vec<ProverOutput>, // will be filled in
61+
pub parent_hash: Option<Vec<u8>>, // indicator to go up one level when this node has been "proven"
6462
/// Used for information about tx in the leaf node proving phase
65-
transaction: Option<Transaction>,
63+
pub transaction: Option<Transaction>,
6664
}
6765
impl MPTNode {
6866
fn is_provable(&self) -> bool {
@@ -93,7 +91,8 @@ impl MPTNode {
9391
}
9492
}
9593

96-
struct ProverOutput {
94+
#[derive(Debug, PartialEq, Eq, Clone)]
95+
pub struct ProverOutput {
9796
parent_hash: Option<Vec<u8>>,
9897
// hash of this node
9998
hash: Vec<u8>,
@@ -116,6 +115,7 @@ impl TxBlockProver {
116115

117116
pub fn prove(&mut self) -> Result<Vec<u8>> {
118117
let (mut trie, leaves_hashes) = self.init_proofs_trie();
118+
119119
println!(
120120
"[+] Built internal trie with {} leaves, and {} nodes in total",
121121
leaves_hashes.len(),
@@ -204,7 +204,9 @@ impl TxBlockProver {
204204
intermediate_node: node_bytes,
205205
children_proofs: inner_proofs,
206206
});
207+
207208
let proof = prover.compute_proof()?;
209+
208210
println!(
209211
"[+] OK Valid recursive proof for node hash {}",
210212
hex::encode(&node_hash)
@@ -232,10 +234,6 @@ impl TxBlockProver {
232234
transaction,
233235
});
234236
let proof = prover.compute_proof()?;
235-
println!(
236-
"[+] OK Valid proof for leaf - hash {}",
237-
hex::encode(&leaf_hash)
238-
);
239237
Ok(ProverOutput {
240238
parent_hash,
241239
proof,
@@ -246,7 +244,7 @@ impl TxBlockProver {
246244
// Returns the hashmap filled with the trie info
247245
// and returns the initial list of nodes's hash, which happen to be leaves, to prove
248246
#[allow(clippy::type_complexity)]
249-
fn init_proofs_trie(&mut self) -> (HashMap<Vec<u8>, MPTNode>, Vec<Vec<u8>>) {
247+
pub fn init_proofs_trie(&mut self) -> (HashMap<Vec<u8>, MPTNode>, Vec<Vec<u8>>) {
250248
// H(node) => { MPTNode() }
251249
let mut tree = HashMap::new();
252250
let mut leaves = Vec::new();
@@ -264,10 +262,12 @@ impl TxBlockProver {
264262
let idx = txr.receipt().transaction_index;
265263
let key = idx.rlp_bytes().to_vec();
266264
let proof_bytes = self.data.tx_trie.get_proof(&key).unwrap();
265+
267266
let mut child_hash = None;
268267
for (i, node_bytes) in proof_bytes.iter().rev().enumerate() {
269268
let idx_in_path = proof_bytes.len() - 1 - i;
270269
let hash = keccak256(node_bytes);
270+
271271
if i == 0 {
272272
leaves.push(hash.clone());
273273
}
@@ -306,6 +306,7 @@ impl TxBlockProver {
306306
}
307307
}
308308

309+
#[cfg(test)]
309310
mod test {
310311
use std::time::Instant;
311312

0 commit comments

Comments
 (0)