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: add network-specific pre-state execution in decoder #633

Merged
merged 10 commits into from
Sep 23, 2024

Conversation

Nashtare
Copy link
Collaborator

  • adds pre-execution rule specific to cdk-erigon
  • fix precompile range
  • couple tweaks in KERNEL constants module (state keys are hashes of addresses, not the addresses themselves, etc..)

What I haven't done:

  • including the scalable / GER touched slots into prefetching for native tracer. Will we even care about using native tracer with CDK-erigon, or always rely on the Zero tracer? cc @muursh
  • sanity checks based on target network in the decoder entrypoint. Should we assume the data being fed to be valid or do we want those? (i.e. no withdrawals for non-eth mainnet, no GER data for non-cdk-erigon, etc..) cc @0xaatif

closes #604

@github-actions github-actions bot added crate: trace_decoder Anything related to the trace_decoder crate. crate: evm_arithmetization Anything related to the evm_arithmetization crate. crate: zero_bin Anything related to the zero-bin subcrates. labels Sep 17, 2024
@@ -533,10 +547,147 @@ fn middle<StateTrieT: StateTrie + Clone>(
Ok(out)
}

#[allow(unused_variables)]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

only because of the hanging ger_data when not targeting cdk-erigon

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we could make this tighter, but I'd prefer a different approach

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Note that with the latest suggestion on your discuss PR, the lint isn't triggered anymore because both conditional blocks would be living in the same binary

trace_decoder/src/typed_mpt.rs Outdated Show resolved Hide resolved
@Nashtare Nashtare self-assigned this Sep 17, 2024
Copy link
Contributor

@LindaGuiga LindaGuiga left a comment

Choose a reason for hiding this comment

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

This is a first pass (almost complete, but I'd like to check again quickly). It generally looks good, I just have a couple of questions

trace_decoder/src/core.rs Outdated Show resolved Hide resolved
Comment on lines +638 to +639
(block.block_number - 1).to_big_endian(&mut arr[0..32]);
U256::from(STATE_ROOT_STORAGE_POS.1).to_big_endian(&mut arr[32..64]);
Copy link
Contributor

Choose a reason for hiding this comment

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

In the link, the slot number seems to be 1|blockNumber. But this implementation seems to differ? Or am I missing something?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hmm I'm not sure. In cdk_erigon, we call:

		//save prev block hash
		sdb.scalableSetBlockHash(blockNumber-1, prevBlockHash)

with function definition:

func (sdb *IntraBlockState) scalableSetBlockHash(blockNum uint64, blockHash *libcommon.Hash) {
	// create mapping with keccak256(blockNum,position) -> smt root
	d1 := common.LeftPadBytes(uint256.NewInt(blockNum).Bytes(), 32)
	d2 := common.LeftPadBytes(STATE_ROOT_STORAGE_POS.Bytes(), 32)
	mapKey := keccak256.Hash(d1, d2)
	mkh := libcommon.BytesToHash(mapKey)

	hashAsBigU := uint256.NewInt(0).SetBytes(blockHash.Bytes())

	sdb.SetState(ADDRESS_SCALABLE_L2, &mkh, *hashAsBigU)
}

i.e. this line: mapKey := keccak256.Hash(d1, d2) is hashing the concatenation blockNum || STATE_ROOT_STORAGE_POS i.e. with initial inputs: blockNumber-1 || 0x01.
Unless I am also missing something

@@ -533,10 +547,147 @@ fn middle<StateTrieT: StateTrie + Clone>(
Ok(out)
}

#[allow(unused_variables)]
/// Performs all the pre-txn execution rules of the targeted network.
fn do_pre_execution<StateTrieT: StateTrie + Clone>(
Copy link
Contributor

Choose a reason for hiding this comment

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

Are you also storing the BLOCK_INFO_ROOT?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I believe this is only during post_state_execution, not pre, and following discussions of this morning we may actually not need to write this BLOCK_INFO_ROOT (pending design decisions)

Copy link
Contributor

@0xaatif 0xaatif left a comment

Choose a reason for hiding this comment

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

How do you feel about phrasing this as a runtime rather than compile time idea?
I've sketched this in #643

I'm dubious of any significant performance delta, and the code would be:

  • easier to reason about
  • get all the affordances of development with no feature gating (better IDE experience etc...)

@atanmarko
Copy link
Member

OK from the zero side.

Copy link
Contributor

@0xaatif 0xaatif left a comment

Choose a reason for hiding this comment

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

I don't think I should keep blocking this review on wider architectural decisions. The code is good as is, any arch refactors after the fact are trivial :)

@Nashtare
Copy link
Collaborator Author

Nashtare commented Sep 23, 2024

@LindaGuiga let me know if you still need clarifications on the items above

@0xaatif would you want me to apply the last suggestion of #643 here or should we keep exploring options?

@0xaatif
Copy link
Contributor

0xaatif commented Sep 23, 2024

I think it's a great suggestion :) happy for it to be included

@Nashtare Nashtare merged commit f6ce57b into develop Sep 23, 2024
17 checks passed
@Nashtare Nashtare deleted the decoder/pre_state branch September 23, 2024 13:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crate: evm_arithmetization Anything related to the evm_arithmetization crate. crate: trace_decoder Anything related to the trace_decoder crate. crate: zero_bin Anything related to the zero-bin subcrates.
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Parameterise precompiled address ranges
5 participants