Skip to content

Commit

Permalink
Suggested PR changes for #4 (2)
Browse files Browse the repository at this point in the history
  • Loading branch information
BGluth committed Jan 22, 2024
1 parent d3a1b83 commit 0792e3a
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions protocol_decoder/src/decoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub enum TraceParsingError {
#[error("Missing keys when creating sub-partial tries (Trie type: {0})")]
MissingKeysCreatingSubPartialTrie(TrieType),

#[error("No account present at {0:x} (hashed: {1:x}) to withdrawal {2} Gwei from!")]
#[error("No account present at {0:x} (hashed: {1:x}) to withdraw {2} Gwei from!")]
MissingWithdrawalAccount(Address, HashedAccountAddr, U256),
}

Expand Down Expand Up @@ -279,29 +279,40 @@ impl ProcessedBlockTrace {
Ok(())
}

/// Pads a generated IR vec with additional "dummy" entries if needed.
/// We need to ensure that generated IR always has at least `2` elements,
/// and if there are only `0` or `1` elements, then we need to pad so
/// that we have two entries in total. These dummy entries serve only to
/// allow the proof generation process to finish. Specifically, we need
/// at least two entries to generate an agg proof, and we need an agg
/// proof to generate a block proof. These entries do not mutate state
/// (unless there are withdrawals in the block (see
/// `[add_withdrawals_to_txns]`), where the final one will mutate the
/// state trie.
fn pad_gen_inputs_with_dummy_inputs_if_needed(
gen_inputs: &mut Vec<TxnProofGenIR>,
other_data: &OtherBlockData,
initial_trie_state: &PartialTrieState,
) -> bool {
let mut dummies_added = true;

match gen_inputs.len() {
0 => {
// Need to pad with two dummy txns.
// Need to pad with two dummy entries.
gen_inputs.extend(create_dummy_txn_pair_for_empty_block(
other_data,
initial_trie_state,
));

true
}
1 => {
// Just need one.
let dummy_txn = create_dummy_gen_input(other_data, initial_trie_state, 0);
gen_inputs.insert(0, dummy_txn);

true
}
_ => dummies_added = false,
_ => false,
}

dummies_added
}

/// The withdrawals are always in the final ir payload. How they are placed
Expand All @@ -319,7 +330,7 @@ impl ProcessedBlockTrace {
dummies_already_added: bool,
) -> TraceParsingResult<()> {
// Withdrawals update balances in the account trie, so we need to do that here.
Self::update_trie_state_from_withdrawals(withdrawals.iter(), &mut final_trie_state.state)?;
Self::update_trie_state_from_withdrawals(&withdrawals, &mut final_trie_state.state)?;

match dummies_already_added {
false => {
Expand All @@ -338,14 +349,16 @@ impl ProcessedBlockTrace {
// If we have dummy proofs (note: `txn_ir[1]` is always a dummy txn in this
// case), then this dummy will get the withdrawals.
txn_ir[1].gen_inputs.withdrawals = withdrawals;
txn_ir[1].gen_inputs.tries.state_trie = final_trie_state.state.clone();
txn_ir[1].gen_inputs.trie_roots_after.state_root = final_trie_state.state.hash();
}
}

Ok(())
}

fn update_trie_state_from_withdrawals<'a>(
withdrawals: impl Iterator<Item = &'a (Address, U256)> + 'a,
withdrawals: impl IntoIterator<Item = &'a (Address, U256)> + 'a,
state: &mut HashedPartialTrie,
) -> TraceParsingResult<()> {
for (addr, amt) in withdrawals {
Expand Down

0 comments on commit 0792e3a

Please sign in to comment.