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

fix: missing code read in state write #699

Merged
merged 4 commits into from
Oct 4, 2024
Merged
Changes from all commits
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
39 changes: 33 additions & 6 deletions trace_decoder/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ pub fn entrypoint(
code_db,
txn_info,
} = trace;

let fatal_missing_code = match trie_pre_images {
BlockTraceTriePreImages::Separate(_) => FatalMissingCode(true),
BlockTraceTriePreImages::Combined(_) => FatalMissingCode(false),
};
Comment on lines +44 to +47
Copy link
Contributor

Choose a reason for hiding this comment

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

This is my first time realising that native = separate, and compact = jerigon


let (state, storage, mut code) = start(trie_pre_images)?;
code.extend(code_db);

Expand Down Expand Up @@ -68,6 +74,7 @@ pub fn entrypoint(
&b_meta,
ger_data,
withdrawals,
fatal_missing_code,
observer,
)?;

Expand Down Expand Up @@ -270,6 +277,14 @@ pub struct IntraBlockTries<StateTrieT> {
pub receipt: ReceiptTrie,
}

/// Hacky handling of possibly missing contract bytecode in `Hash2Code` inner
/// map.
/// Allows incomplete payloads fetched with the zero tracer to skip these
/// silently.
// TODO(Nashtare): https://github.com/0xPolygonZero/zk_evm/issues/700
#[derive(Copy, Clone)]
pub struct FatalMissingCode(pub bool);

/// Does the main work mentioned in the [module documentation](super).
#[allow(clippy::too_many_arguments)]
fn middle<StateTrieT: StateTrie + Clone>(
Expand All @@ -285,6 +300,7 @@ fn middle<StateTrieT: StateTrie + Clone>(
ger_data: Option<(H256, H256)>,
// added to final batch
mut withdrawals: Vec<(Address, U256)>,
fatal_missing_code: FatalMissingCode,
// called with the untrimmed tries after each batch
observer: &mut impl Observer<StateTrieT>,
) -> anyhow::Result<Vec<Batch<StateTrieT>>> {
Expand Down Expand Up @@ -433,7 +449,21 @@ fn middle<StateTrieT: StateTrie + Clone>(
acct.code_hash = code_usage
.map(|it| match it {
ContractCodeUsage::Read(hash) => {
batch_contract_code.insert(code.get(hash)?);
// TODO(Nashtare): https://github.com/0xPolygonZero/zk_evm/issues/700
// This is a bug in the zero tracer, which shouldn't be giving us
// this read at all. Workaround for now.
match (fatal_missing_code, code.get(hash)) {
(FatalMissingCode(true), None) => {
bail!("no code for hash {hash:x}")
}
(_, Some(byte_code)) => {
batch_contract_code.insert(byte_code);
}
(_, None) => {
log::warn!("no code for {hash:x}")
}
}

anyhow::Ok(hash)
}
ContractCodeUsage::Write(bytes) => {
Expand Down Expand Up @@ -767,11 +797,8 @@ impl Hash2Code {
this.insert(vec![]);
this
}
pub fn get(&mut self, hash: H256) -> anyhow::Result<Vec<u8>> {
match self.inner.get(&hash) {
Some(code) => Ok(code.clone()),
None => bail!("no code for hash {:x}", hash),
}
pub fn get(&mut self, hash: H256) -> Option<Vec<u8>> {
self.inner.get(&hash).cloned()
}
pub fn insert(&mut self, code: Vec<u8>) {
self.inner.insert(keccak_hash::keccak(&code), code);
Expand Down
Loading