Skip to content

Commit

Permalink
Now includes txn & receipt tries in proof gen payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
BGluth committed Oct 4, 2023
1 parent d986d59 commit 0f77cf5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
5 changes: 1 addition & 4 deletions parser/src/edge_payloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,11 @@ pub struct TxnBytesAndTraces {
/// The root of the txn trie after the txn has been executed.
pub txn_root: H256,

// ReceiptNodeHash is the hash of the new txn node added by the txn.
pub txn_node_bytes: Vec<u8>,

/// The root of the receipt trie after the txn has been executed.
pub receipt_root: H256,

// ReceiptNodeHash is the hash of the new receipt node added by the txn.
pub receipt_node_bytes: Vec<u8>,
pub receipt: Vec<u8>,

// GasUsed is the amount of gas used by the transaction
pub gas_used: u64,
Expand Down
50 changes: 45 additions & 5 deletions parser/src/plonky2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ impl EdgeBlockTrace {
let txn_partial_tries = Self::create_minimal_partial_tries_needed_by_txn(
&block_tries,
processed_txn_traces.nodes_used_by_txn,
txn_idx,
)?;

println!("Base storage tries:");
Expand Down Expand Up @@ -278,6 +279,16 @@ impl EdgeBlockTrace {
&mut addrs_to_code,
)?;

Self::update_receipt_and_txn_tries(
&mut block_tries.receipt,
&mut block_tries.txn,
txn_trace_info.txn.clone(),
txn_trace_info.receipt,
txn_idx,
);
assert_eq!(block_tries.receipt.hash(), txn_trace_info.receipt_root);
assert_eq!(block_tries.txn.hash(), txn_trace_info.txn_root);

let trie_roots_after = TrieRoots {
state_root: block_tries.state.hash(),
transactions_root: txn_trace_info.txn_root,
Expand Down Expand Up @@ -411,6 +422,8 @@ impl EdgeBlockTrace {
BlockPartialTries {
state: state_trie,
storage: acc_storage_tries,
receipt: HashedPartialTrie::default(),
txn: HashedPartialTrie::default(),
},
accounts_to_code,
))
Expand Down Expand Up @@ -534,6 +547,7 @@ impl EdgeBlockTrace {
fn create_minimal_partial_tries_needed_by_txn(
curr_block_tries: &BlockPartialTries,
nodes_used_by_txn: NodesUsedByTxn,
txn_idx: usize,
) -> TraceParsingResult<TrieInputs> {
let subset_state_trie = create_trie_subset(
&curr_block_tries.state,
Expand Down Expand Up @@ -578,15 +592,24 @@ impl EdgeBlockTrace {

Ok(TrieInputs {
state_trie: subset_state_trie,
transactions_trie: HashedPartialTrie::default(), /* TODO: Wait for full node &
* Plonky2
* support... */
receipts_trie: HashedPartialTrie::default(), /* TODO: Wait for full node & Plonky2
* support... */
transactions_trie: Self::construct_partial_trie_from_idx(
&curr_block_tries.receipt,
txn_idx,
),
receipts_trie: Self::construct_partial_trie_from_idx(&curr_block_tries.txn, txn_idx),
storage_tries: subset_storage_tries,
})
}

fn construct_partial_trie_from_idx(
full_trie: &HashedPartialTrie,
idx: usize,
) -> HashedPartialTrie {
// Should be doing better errors here but this is currently just a hack.
create_trie_subset(full_trie, once(idx as u64))

Check failure on line 609 in parser/src/plonky2.rs

View workflow job for this annotation

GitHub Actions / check

cannot find function `once` in this scope

Check failure on line 609 in parser/src/plonky2.rs

View workflow job for this annotation

GitHub Actions / check

cannot find function `once` in this scope
.expect("Unable to create single element partial trie from an index")
}

fn apply_deltas_to_trie_state(
trie_state: &mut BlockPartialTries,
deltas: Vec<ProcessedTxnTrace>,
Expand Down Expand Up @@ -634,6 +657,21 @@ impl EdgeBlockTrace {
Ok(())
}

fn update_receipt_and_txn_tries(
receipt_trie: &mut HashedPartialTrie,
txn_trie: &mut HashedPartialTrie,
receipt_node: Vec<u8>,
txn_node: Vec<u8>,
txn_idx: usize,
) {
Self::add_indexed_node_to_trie(receipt_trie, receipt_node, txn_idx);
Self::add_indexed_node_to_trie(txn_trie, txn_node, txn_idx);
}

fn add_indexed_node_to_trie(trie: &mut HashedPartialTrie, node: Vec<u8>, txn_idx: usize) {
trie.insert(txn_idx as u64, node)
}

pub fn num_txns(&self) -> usize {
self.txn_bytes_and_traces.len()
}
Expand All @@ -653,6 +691,8 @@ fn update_val_if_some<T>(target: &mut T, opt: Option<T>) {
struct BlockPartialTries {
state: HashedPartialTrie,
storage: HashMap<H256, HashedPartialTrie>,
receipt: HashedPartialTrie,
txn: HashedPartialTrie,
}

#[derive(Debug)]
Expand Down

0 comments on commit 0f77cf5

Please sign in to comment.