Skip to content

Commit

Permalink
fix(host): Insert empty MPT root hash (#483)
Browse files Browse the repository at this point in the history
* fix(host): Insert empty MPT root hash

* lint
  • Loading branch information
clabby authored Sep 4, 2024
1 parent f35606c commit 8e9c665
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions bin/host/src/fetcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
//! remote source.

use crate::{kv::KeyValueStore, util};
use alloy_consensus::{Header, TxEnvelope};
use alloy_consensus::{Header, TxEnvelope, EMPTY_ROOT_HASH};
use alloy_eips::{eip2718::Encodable2718, eip4844::FIELD_ELEMENTS_PER_BLOB, BlockId};
use alloy_primitives::{address, keccak256, Address, Bytes, B256};
use alloy_provider::{Provider, ReqwestProvider};
use alloy_rlp::Decodable;
use alloy_rlp::{Decodable, EMPTY_STRING_CODE};
use alloy_rpc_types::{
Block, BlockNumberOrTag, BlockTransactions, BlockTransactionsKind, Transaction,
};
Expand Down Expand Up @@ -537,14 +537,23 @@ where
/// Stores intermediate trie nodes in the key-value store. Assumes that all nodes passed are
/// raw, RLP encoded trie nodes.
async fn store_trie_nodes<T: AsRef<[u8]>>(&self, nodes: &[T]) -> Result<()> {
let mut kv_write_lock = self.kv_store.write().await;

// If the list of nodes is empty, store the empty root hash and exit early.
// The `HashBuilder` will not push the preimage of the empty root hash to the
// `ProofRetainer` in the event that there are no leaves inserted.
if nodes.is_empty() {
let empty_key = PreimageKey::new(*EMPTY_ROOT_HASH, PreimageKeyType::Keccak256);
kv_write_lock.set(empty_key.into(), [EMPTY_STRING_CODE].into());
return Ok(())
}

let mut hb = kona_mpt::ordered_trie_with_encoder(nodes, |node, buf| {
buf.put_slice(node.as_ref());
});
hb.root();
let intermediates = hb.take_proofs();

let mut kv_write_lock = self.kv_store.write().await;

for (_, value) in intermediates.into_iter() {
let value_hash = keccak256(value.as_ref());
let key = PreimageKey::new(*value_hash, PreimageKeyType::Keccak256);
Expand Down

0 comments on commit 8e9c665

Please sign in to comment.