-
Notifications
You must be signed in to change notification settings - Fork 29
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: expand forkChoiceUpdatedV3 with basic block building #540
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
bc150e9
Revert "fix: remove totalDifficulty (#347)"
fmoletta 0c2cd4d
Complete forkChoiceUpdatedV3 up until payload validation
fmoletta 1a1b781
Add BuildPayloadArgs + id
fmoletta 7386f2b
Output proper payload id
fmoletta adf5204
Move execution payload to core
fmoletta 772fac1
Revert "Move execution payload to core"
fmoletta f8409b0
Push uncommited file
fmoletta 7446289
Add block building
fmoletta 2b2f4dd
Clippy + fmt
fmoletta 5e47673
Modularize fork choice update fn
fmoletta 31c380f
Fix timestamp
fmoletta 1bdf863
Fix ser
fmoletta 69d280b
Fix
fmoletta 677a7ec
Merge branch 'main' of github.com:lambdaclass/ethereum_rust into engi…
fmoletta 9737674
Integrate previous changes (part 1)
fmoletta 1e3d710
fetch only header instead of block
fmoletta a634c13
Fix typo + remove unwrap
fmoletta 995aca8
fetch only header instead of block
fmoletta a856b7a
Clarify hardcoded values
fmoletta 3f1e4ed
Add doc
fmoletta 8adfe28
Add InvalidForkChoiceState RpcErr
fmoletta 69607ae
revert uneeded changes
fmoletta 9618e32
Change miner to builder
fmoletta 76e4bdf
rename: local_block -> payload
fmoletta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
use std::cmp::min; | ||
|
||
use ethereum_rust_core::{ | ||
types::{ | ||
calculate_base_fee_per_gas, compute_receipts_root, compute_transactions_root, | ||
compute_withdrawals_root, Block, BlockBody, BlockHash, BlockHeader, Withdrawal, | ||
DEFAULT_OMMERS_HASH, | ||
}, | ||
Address, Bloom, Bytes, H256, U256, | ||
}; | ||
use ethereum_rust_rlp::encode::RLPEncode; | ||
use ethereum_rust_storage::{error::StoreError, Store}; | ||
use sha3::{Digest, Keccak256}; | ||
|
||
use crate::constants::{GAS_LIMIT_BOUND_DIVISOR, MIN_GAS_LIMIT, TARGET_BLOB_GAS_PER_BLOCK}; | ||
|
||
pub struct BuildPayloadArgs { | ||
pub parent: BlockHash, | ||
pub timestamp: u64, | ||
pub fee_recipient: Address, | ||
pub random: H256, | ||
pub withdrawals: Vec<Withdrawal>, | ||
pub beacon_root: Option<H256>, | ||
pub version: u8, | ||
} | ||
|
||
impl BuildPayloadArgs { | ||
/// Computes an 8-byte identifier by hashing the components of the payload arguments. | ||
pub fn id(&self) -> u64 { | ||
let mut hasher = Keccak256::new(); | ||
hasher.update(self.parent); | ||
hasher.update(self.timestamp.to_be_bytes()); | ||
hasher.update(self.random); | ||
hasher.update(self.fee_recipient); | ||
hasher.update(self.withdrawals.encode_to_vec()); | ||
if let Some(beacon_root) = self.beacon_root { | ||
hasher.update(beacon_root); | ||
} | ||
let res = &mut hasher.finalize()[..8]; | ||
res[0] = self.version; | ||
u64::from_be_bytes(res.try_into().unwrap()) | ||
} | ||
} | ||
|
||
/// Builds a new payload based on the payload arguments | ||
// Basic payload block building, can and should be improved | ||
pub fn build_payload(args: &BuildPayloadArgs, storage: &Store) -> Result<Block, StoreError> { | ||
// TODO: check where we should get builder values from | ||
const DEFAULT_BUILDER_GAS_CEIL: u64 = 30_000_000; | ||
// Presence of a parent block should have been checked or guaranteed before calling this function | ||
// So we can treat a missing parent block as an internal storage error | ||
let parent_block = storage | ||
.get_block_header_by_hash(args.parent)? | ||
.ok_or_else(|| StoreError::Custom("unexpected missing parent block".to_string()))?; | ||
let chain_config = storage.get_chain_config()?; | ||
let gas_limit = calc_gas_limit(parent_block.gas_limit, DEFAULT_BUILDER_GAS_CEIL); | ||
Ok(Block { | ||
header: BlockHeader { | ||
parent_hash: args.parent, | ||
ommers_hash: *DEFAULT_OMMERS_HASH, | ||
coinbase: args.fee_recipient, | ||
state_root: parent_block.state_root, | ||
transactions_root: compute_transactions_root(&[]), | ||
receipts_root: compute_receipts_root(&[]), | ||
logs_bloom: Bloom::default(), | ||
difficulty: U256::zero(), | ||
number: parent_block.number.saturating_add(1), | ||
gas_limit, | ||
gas_used: 0, | ||
timestamp: args.timestamp, | ||
// TODO: should use builder config's extra_data | ||
extra_data: Bytes::new(), | ||
prev_randao: args.random, | ||
nonce: 0, | ||
base_fee_per_gas: calculate_base_fee_per_gas( | ||
gas_limit, | ||
parent_block.gas_limit, | ||
parent_block.gas_used, | ||
parent_block.base_fee_per_gas.unwrap_or_default(), | ||
), | ||
withdrawals_root: chain_config | ||
.is_shanghai_activated(args.timestamp) | ||
.then_some(compute_withdrawals_root(&args.withdrawals)), | ||
blob_gas_used: Some(0), | ||
excess_blob_gas: chain_config.is_cancun_activated(args.timestamp).then_some( | ||
calc_excess_blob_gas( | ||
parent_block.excess_blob_gas.unwrap_or_default(), | ||
parent_block.blob_gas_used.unwrap_or_default(), | ||
), | ||
), | ||
parent_beacon_block_root: args.beacon_root, | ||
}, | ||
// Empty body as we just created this payload | ||
body: BlockBody { | ||
transactions: Vec::new(), | ||
ommers: Vec::new(), | ||
withdrawals: Some(args.withdrawals.clone()), | ||
}, | ||
}) | ||
} | ||
|
||
fn calc_gas_limit(parent_gas_limit: u64, desired_limit: u64) -> u64 { | ||
let delta = parent_gas_limit / GAS_LIMIT_BOUND_DIVISOR - 1; | ||
let mut limit = parent_gas_limit; | ||
let desired_limit = min(desired_limit, MIN_GAS_LIMIT); | ||
if limit < desired_limit { | ||
limit = parent_gas_limit + delta; | ||
if limit > desired_limit { | ||
limit = desired_limit | ||
} | ||
return limit; | ||
} | ||
if limit > desired_limit { | ||
limit = parent_gas_limit - delta; | ||
if limit < desired_limit { | ||
limit = desired_limit | ||
} | ||
} | ||
limit | ||
} | ||
|
||
fn calc_excess_blob_gas(parent_excess_blob_gas: u64, parent_blob_gas_used: u64) -> u64 { | ||
let excess_blob_gas = parent_excess_blob_gas + parent_blob_gas_used; | ||
if excess_blob_gas < TARGET_BLOB_GAS_PER_BLOCK { | ||
0 | ||
} else { | ||
excess_blob_gas - TARGET_BLOB_GAS_PER_BLOCK | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is what the spec says? not against it, just curious
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I took it from geth. The spec says
8 Bytes - identifier of the payload build process