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(network-primitives): expose more fields via block response traits #1229

Merged
merged 3 commits into from
Sep 3, 2024

Conversation

yash-atreya
Copy link
Member

@yash-atreya yash-atreya commented Sep 3, 2024

Motivation

HeaderResponse exposed very limited fields. BlockResponse doesn't expose other fields in case of WithOtherFields<Block>.

Solution

  • Expose miner, gas_limit, mix_hash, difficulty in HeaderResponse. Exposing these fields helps to initialize a revm::BlockEnv like so:
BlockEnv {
    number: U256::from(block.header().number()),
    timestamp: U256::from(block.header().timestamp()),
    coinbase: block.header().miner(),
    difficulty: block.header().difficulty(),
    prevrandao: block.header().mix_hash(),
    basefee: U256::from(block.header().base_fee_per_gas().unwrap_or_default()),
    gas_limit: U256::from(block.header().gas()),
    ..Default::default()
}
  • Add other_fields(&self) -> Option<OtherFields> to BlockResponse which returns None by default unless overridden.

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

a few nits

Comment on lines 80 to 81
/// Difficulty of the block
fn difficulty(&self) -> U256;
Copy link
Member

Choose a reason for hiding this comment

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

maybe we don't need this, I assume this could eventually be removed

Copy link
Member Author

Choose a reason for hiding this comment

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

OP stack based L2s use block.difficulty for generating random values.

Copy link
Member Author

Choose a reason for hiding this comment

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

But I guess block.prevrandao serves the same purpose on L2s

crates/network-primitives/src/traits.rs Outdated Show resolved Hide resolved
crates/network-primitives/src/traits.rs Show resolved Hide resolved
crates/network-primitives/src/traits.rs Outdated Show resolved Hide resolved
@yash-atreya yash-atreya changed the title feat(network-primitives): expose more fields via block related traits feat(network-primitives): expose more fields via block response traits Sep 3, 2024
Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

lgtm, pending @klkvr

@klkvr klkvr merged commit 06e0ae9 into main Sep 3, 2024
29 checks passed
@klkvr klkvr deleted the yash/expose-more-header-fields branch September 3, 2024 18:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants