Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fallback preimage recovery w/o debug_dbGet #30

Merged
merged 2 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ futures = "0.3"
url = "2.3"
thiserror = "1.0.61"
hex-literal = "0.4.1"
rayon = "1.10.0"

# workspace
rsp-rpc-db = { path = "./crates/storage/rpc-db" }
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ and the command `rsp` will be installed.

### RPC Node Requirement

RSP fetches block and state data from a JSON-RPC node. **But, you must use a RPC node that supports the `debug_dbGet` endpoint.**
RSP fetches block and state data from a JSON-RPC node. It's recommended that you use a RPC node that supports the `debug_dbGet` endpoint.

This is required because in some cases the host needs to recover the preimage of a [Merkle Patricia Trie](https://ethereum.org/en/developers/docs/data-structures-and-encoding/patricia-merkle-trie/) node that's referenced by hash. To do this, the host utilizes the [`debug_dbGet` endpoint](https://geth.ethereum.org/docs/interacting-with-geth/rpc/ns-debug#debugdbget) of a Geth node running with options `--state.scheme=hash`, which is the default, and `--gcmode=archive`. An example command for running the node is:
This is recommended because in some cases the host needs to recover the preimage of a [Merkle Patricia Trie](https://ethereum.org/en/developers/docs/data-structures-and-encoding/patricia-merkle-trie/) node that's referenced by hash. To do this, the host utilizes the [`debug_dbGet` endpoint](https://geth.ethereum.org/docs/interacting-with-geth/rpc/ns-debug#debugdbget) of a Geth node running with options `--state.scheme=hash`, which is the default, and `--gcmode=archive`. An example command for running the node is:

```bash
geth \
Expand All @@ -33,7 +33,7 @@ geth \
--http.api=eth,debug
```

When running the host CLI or integration tests, **make sure to use an RPC URL pointing to a Geth node running with said options**, or errors will arise when preimage recovery is needed. You can reach out to the Succinct team to access an RPC URL that supports this endpoint.
However, in the absence of the `debug_dbGet` method, the host is able to fall back to a less efficient process of recovering the preimages via the standard `eth_getProof`. The fallback works in most cases but not all, so if you encounter a preimage recovery failure, you can reach out to the Succinct team to access an RPC URL that supports `debug_dbGet`.

### Running the CLI

Expand Down
23 changes: 20 additions & 3 deletions crates/mpt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use reth_trie::{
EMPTY_ROOT_HASH,
};
use revm_primitives::{keccak256, HashMap};
use rsp_primitives::storage::ExtDatabaseRef;
use rsp_primitives::storage::{ExtDatabaseRef, PreimageContext};

/// Computes the state root of a block's Merkle Patricia Trie given an [ExecutionOutcome] and a list
/// of [EIP1186AccountProofResponse] storage proofs.
Expand Down Expand Up @@ -76,6 +76,7 @@ where
(storage_nibbles.clone(), encoded, storage_proof.proof.clone())
}),
db,
Some(address),
)?
};
storage_roots.insert(hashed_address, root);
Expand All @@ -102,13 +103,15 @@ where
(account_nibbles.clone(), encoded, proof.proof.clone())
}),
db,
None,
)
}

/// Given a list of Merkle-Patricia proofs, compute the root of the trie.
fn compute_root_from_proofs<DB>(
items: impl IntoIterator<Item = (Nibbles, Option<Vec<u8>>, Vec<Bytes>)>,
db: &DB,
root_context: Option<Address>,
) -> eyre::Result<B256>
where
DB: ExtDatabaseRef<Error: std::fmt::Debug>,
Expand Down Expand Up @@ -278,7 +281,12 @@ where
// technically have to modify this branch node, but the `alloy-trie` hash
// builder handles this automatically when supplying child nodes.

let preimage = db.trie_node_ref(branch_hash).unwrap();
let preimage = db
.trie_node_ref(
branch_hash,
PreimageContext { address: &root_context, branch_path: &path },
)
.unwrap();
match TrieNode::decode(&mut &preimage[..]).unwrap() {
TrieNode::Branch(_) => {
// This node is a branch node that's referenced by hash. There's no need
Expand Down Expand Up @@ -365,7 +373,11 @@ mod tests {
impl ExtDatabaseRef for TestTrieDb {
type Error = std::convert::Infallible;

fn trie_node_ref(&self, hash: B256) -> std::result::Result<Bytes, Self::Error> {
fn trie_node_ref(
&self,
hash: B256,
_context: PreimageContext<'_>,
) -> std::result::Result<Bytes, Self::Error> {
for preimage in self.preimages.iter() {
if keccak256(preimage) == hash {
return std::result::Result::Ok(preimage.to_owned());
Expand Down Expand Up @@ -469,6 +481,7 @@ cb10a951f0e82cf2e461b98c4e5afb0348ccab5bb42180808080808080808080808080"
],
)],
&TestTrieDb::new(),
None,
)
.unwrap();

Expand Down Expand Up @@ -587,6 +600,7 @@ f2e461b98c4e5afb0348ccab5bb421808080808080808080808080"
),
],
&TestTrieDb::new(),
None,
)
.unwrap();

Expand Down Expand Up @@ -653,6 +667,7 @@ f2e461b98c4e5afb0348ccab5bb421808080808080808080808080"
),
],
&TestTrieDb::new(),
None,
)
.unwrap();

Expand Down Expand Up @@ -713,6 +728,7 @@ f2e461b98c4e5afb0348ccab5bb421808080808080808080808080"
),
],
&TestTrieDb::new(),
None,
)
.unwrap();

Expand Down Expand Up @@ -757,6 +773,7 @@ f2e461b98c4e5afb0348ccab5bb421808080808080808080808080"
],
)],
&TestTrieDb::new(),
None,
)
.unwrap();

Expand Down
15 changes: 13 additions & 2 deletions crates/primitives/src/storage.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
use reth_primitives::{Bytes, B256};
use reth_primitives::{Address, Bytes, B256};
use reth_trie::Nibbles;

/// Custom database access methods implemented by RSP storage backends.
pub trait ExtDatabaseRef {
/// The database error type.
type Error;

/// Gets the preimage of a trie node given its Keccak hash.
fn trie_node_ref(&self, hash: B256) -> Result<Bytes, Self::Error>;
fn trie_node_ref(&self, hash: B256, context: PreimageContext) -> Result<Bytes, Self::Error>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does passing this around affect the number of cycles in the client program? I feel like since client program cycles are super critical, we should make sure it doesn't

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about the same thing but thought it shouldn't matter since it's just passing 2 references. But I can do a quick benchmark on a before/after for this. Will update here. Do note that since the code is not yet deterministic it might not be 100% accurate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm to my surprise the cycle count change turns out to be not really negligible. I was expected it to be buried in the noise of the non-determinism. Turns out for the block tested 20600000 (avg out of 10 runs each):

  • Before: 762,727,324 cycles
  • After: 765,003,639 cycles

We got some 0.5% increase from this. This is quite surprising as this is really just passing 2 references, and I was half expecting the compiler to optimize it away since it's unused.

0.5% isn't huge but for something that can be avoided altogether it's not ideal. Will do a bit refactor here to add a feature that makes this host-only. Was hoping that it would be negligible so we don't have to over-engineer it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@puma314 Just pushed a new commit that uses Rust feature to make sure none of the changes affect client other than adding a unit type parameter (which I'm kina sure gets optimized away since it's zero-sized).

Ran the test against the same block and I'm now getting 762,685,729 which is actually even lower than before this change, though I'm pretty sure this comes from the noise of non-determinism.

In any case, we're now no longer sacrificing client efficiency for this feature.

}

/// Additional context for retrieving trie node preimages. These are useful when the JSON-RPC node
/// does not serve the `debug_dbGet`.
pub struct PreimageContext<'a> {
/// The account address if calculating a storage trie root; `None` if calculating the state
/// root.
pub address: &'a Option<Address>,
/// The trie key path of the branch child containing the hash whose preimage is being fetched.
pub branch_path: &'a Nibbles,
}
2 changes: 2 additions & 0 deletions crates/storage/rpc-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ tokio.workspace = true
futures.workspace = true
thiserror.workspace = true
tracing.workspace = true
rayon.workspace = true

# workspace
rsp-witness-db.workspace = true
Expand All @@ -21,6 +22,7 @@ rsp-primitives.workspace = true
reth-primitives.workspace = true
reth-storage-errors.workspace = true
reth-revm.workspace = true
reth-trie.workspace = true

# revm
revm-primitives.workspace = true
Expand Down
112 changes: 100 additions & 12 deletions crates/storage/rpc-db/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
use std::{cell::RefCell, marker::PhantomData};
use std::{cell::RefCell, iter::once, marker::PhantomData};

use alloy_provider::Provider;
use alloy_rpc_types::BlockId;
use alloy_transport::Transport;
use futures::future::join_all;
use rayon::prelude::*;
use reth_primitives::{
revm_primitives::{AccountInfo, Bytecode},
Address, Bytes, B256, U256,
};
use reth_revm::DatabaseRef;
use reth_storage_errors::{db::DatabaseError, provider::ProviderError};
use revm_primitives::{HashMap, HashSet};
use rsp_primitives::{account_proof::AccountProofWithBytecode, storage::ExtDatabaseRef};
use reth_trie::Nibbles;
use revm_primitives::{keccak256, HashMap, HashSet};
use rsp_primitives::{
account_proof::AccountProofWithBytecode,
storage::{ExtDatabaseRef, PreimageContext},
};
use rsp_witness_db::WitnessDb;

/// The maximum number of addresses/slots to attempt for brute-forcing the key to be used for
/// fetching trie node preimage via `eth_getProof`.
const BRUTE_FORCE_LIMIT: u64 = 0xffffffff_u64;

/// A database that fetches data from a [Provider] over a [Transport].
#[derive(Debug, Clone)]
pub struct RpcDb<T, P> {
Expand Down Expand Up @@ -42,6 +51,8 @@ pub enum RpcDbError {
RpcError(String),
#[error("failed to find block")]
BlockNotFound,
#[error("failed to find trie node preimage")]
PreimageNotFound,
}

impl<T: Transport + Clone, P: Provider<T> + Clone> RpcDb<T, P> {
Expand Down Expand Up @@ -138,16 +149,30 @@ impl<T: Transport + Clone, P: Provider<T> + Clone> RpcDb<T, P> {
}

/// Fetch a trie node based on its Keccak hash using the `debug_dbGet` method.
pub async fn fetch_trie_node(&self, hash: B256) -> Result<Bytes, RpcDbError> {
pub async fn fetch_trie_node(
&self,
hash: B256,
context: PreimageContext<'_>,
) -> Result<Bytes, RpcDbError> {
tracing::info!("fetching trie node {}", hash);

// Fetch the trie node value from a geth node with `state.scheme=hash`.
let value = self
.provider
.client()
.request::<_, Bytes>("debug_dbGet", (hash,))
.await
.map_err(|e| RpcDbError::RpcError(e.to_string()))?;
let value = match self.provider.client().request::<_, Bytes>("debug_dbGet", (hash,)).await {
Ok(value) => value,
Err(_) => {
// The `debug_dbGet` method failed for some reason. Fall back to brute-forcing the
// slot/address needed to recover the preimage via the `eth_getProof` method
// instead.
tracing::debug!(
"failed to fetch preimage from debug_dbGet; \
falling back to using eth_getProof: address={:?}, prefix={:?}",
context.address,
context.branch_path
);

self.fetch_trie_node_via_proof(hash, context).await?
}
};

// Record the trie node value to the state.
self.trie_nodes.borrow_mut().insert(hash, value.clone());
Expand Down Expand Up @@ -224,6 +249,68 @@ impl<T: Transport + Clone, P: Provider<T> + Clone> RpcDb<T, P> {

account_proofs
}

/// Fetches a trie node via `eth_getProof` with a hacky workaround when `debug_dbGet` is not
/// available.
async fn fetch_trie_node_via_proof(
&self,
hash: B256,
context: PreimageContext<'_>,
) -> Result<Bytes, RpcDbError> {
let (address, storage_keys) = match context.address {
Some(address) => {
// Computing storage root. Brute force the slot.
let slot = Self::find_key_preimage::<32>(context.branch_path)
.ok_or(RpcDbError::PreimageNotFound)?;

(address.to_owned(), vec![slot.into()])
}
None => {
// Computing state root. Brute force the address.
let address = Self::find_key_preimage::<20>(context.branch_path)
.ok_or(RpcDbError::PreimageNotFound)?;

(address.into(), vec![])
}
};

let account_proof = self
.provider
.get_proof(address, storage_keys)
.block_id(self.block)
.await
.map_err(|e| RpcDbError::RpcError(e.to_string()))?;

for proof in account_proof
.storage_proof
.into_iter()
.map(|storage_proof| storage_proof.proof)
.chain(once(account_proof.account_proof))
{
// The preimage we're looking for is more likely to be at the end of the proof.
for node in proof.into_iter().rev() {
if hash == keccak256(&node) {
return Ok(node)
}
}
}

Err(RpcDbError::PreimageNotFound)
}

/// Uses brute force to locate a key path preimage that contains a certain prefix.
fn find_key_preimage<const BYTES: usize>(prefix: &Nibbles) -> Option<[u8; BYTES]> {
(0..BRUTE_FORCE_LIMIT).into_par_iter().find_map_any(|nonce| {
let mut buffer = [0u8; BYTES];
buffer[(BYTES - 8)..].copy_from_slice(&nonce.to_be_bytes());

if Nibbles::unpack(keccak256(buffer)).starts_with(prefix) {
Some(buffer)
} else {
None
}
})
}
}

impl<T: Transport + Clone, P: Provider<T> + Clone> DatabaseRef for RpcDb<T, P> {
Expand Down Expand Up @@ -269,11 +356,12 @@ impl<T: Transport + Clone, P: Provider<T> + Clone> DatabaseRef for RpcDb<T, P> {
impl<T: Transport + Clone, P: Provider<T> + Clone> ExtDatabaseRef for RpcDb<T, P> {
type Error = ProviderError;

fn trie_node_ref(&self, hash: B256) -> Result<Bytes, Self::Error> {
fn trie_node_ref(&self, hash: B256, context: PreimageContext) -> Result<Bytes, Self::Error> {
let handle = tokio::runtime::Handle::try_current().map_err(|_| {
ProviderError::Database(DatabaseError::Other("no tokio runtime found".to_string()))
})?;
let result = tokio::task::block_in_place(|| handle.block_on(self.fetch_trie_node(hash)));
let result =
tokio::task::block_in_place(|| handle.block_on(self.fetch_trie_node(hash, context)));
let value =
result.map_err(|e| ProviderError::Database(DatabaseError::Other(e.to_string())))?;
Ok(value)
Expand Down
4 changes: 2 additions & 2 deletions crates/storage/witness-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use reth_primitives::{
};
use reth_storage_errors::provider::ProviderError;
use revm_primitives::{Address, HashMap, U256};
use rsp_primitives::storage::ExtDatabaseRef;
use rsp_primitives::storage::{ExtDatabaseRef, PreimageContext};
use serde::{Deserialize, Serialize};

/// A database used to witness state inside the zkVM.
Expand Down Expand Up @@ -45,7 +45,7 @@ impl DatabaseRef for WitnessDb {
impl ExtDatabaseRef for WitnessDb {
type Error = ProviderError;

fn trie_node_ref(&self, hash: B256) -> Result<Bytes, Self::Error> {
fn trie_node_ref(&self, hash: B256, _context: PreimageContext) -> Result<Bytes, Self::Error> {
// TODO: avoid cloning
Ok(self.trie_nodes.get(&hash).unwrap().to_owned())
}
Expand Down