Skip to content

Conversation

@Kayden-ML
Copy link
Contributor

@Kayden-ML Kayden-ML commented Feb 2, 2025

@Kayden-ML Kayden-ML force-pushed the process_execution_payload branch from 3af4327 to 491f361 Compare February 16, 2025 07:04
@Kayden-ML Kayden-ML force-pushed the process_execution_payload branch from 491f361 to e02d9e3 Compare February 16, 2025 07:07
@Kayden-ML Kayden-ML marked this pull request as ready for review February 16, 2025 07:07
@Kayden-ML Kayden-ML changed the title feat: implementing execution engine feat: Implemented the is_valid_block_hash, is_valid_versioned_hashes functions Feb 16, 2025

[dependencies]
alloy-primitives.workspace = true
alloy-rlp = { version = "0.3.8", default-features = false, features = ["derive"] }
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
alloy-rlp = { version = "0.3.8", default-features = false, features = ["derive"] }
alloy-rlp = { workspace = true }

Same for eth_trie

Copy link
Contributor

Choose a reason for hiding this comment

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

I would do alloy-rlp.workspace = true as it is more minimalist

Copy link
Member

@syjn99 syjn99 left a comment

Choose a reason for hiding this comment

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

Mostly seems good!

Comment on lines 102 to 121
self.parent_hash.length()
+ EMPTY_UNCLE_ROOT_HASH.length()
+ self.fee_recipient.length()
+ self.state_root.length()
+ transactions_root.length()
+ self.receipts_root.length()
+ self.logs_bloom.length()
+ U256::ZERO.length()
+ self.block_number.length()
+ self.gas_limit.length()
+ self.gas_used.length()
+ self.timestamp.length()
+ self.extra_data.to_vec().as_slice().length()
+ self.prev_randao.length()
+ B64::ZERO.length()
+ self.base_fee_per_gas.length()
+ withdrawals_root.length()
+ self.blob_gas_used.length()
+ self.excess_blob_gas.length()
+ parent_beacon_block_root.length()
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
self.parent_hash.length()
+ EMPTY_UNCLE_ROOT_HASH.length()
+ self.fee_recipient.length()
+ self.state_root.length()
+ transactions_root.length()
+ self.receipts_root.length()
+ self.logs_bloom.length()
+ U256::ZERO.length()
+ self.block_number.length()
+ self.gas_limit.length()
+ self.gas_used.length()
+ self.timestamp.length()
+ self.extra_data.to_vec().as_slice().length()
+ self.prev_randao.length()
+ B64::ZERO.length()
+ self.base_fee_per_gas.length()
+ withdrawals_root.length()
+ self.blob_gas_used.length()
+ self.excess_blob_gas.length()
+ parent_beacon_block_root.length()
self.parent_hash.length()
+ EMPTY_UNCLE_ROOT_HASH.length() // ommers_hash
+ self.fee_recipient.length()
+ self.state_root.length()
+ transactions_root.length()
+ self.receipts_root.length()
+ self.logs_bloom.length()
+ U256::ZERO.length() // difficulty
+ self.block_number.length()
+ self.gas_limit.length()
+ self.gas_used.length()
+ self.timestamp.length()
+ self.extra_data.to_vec().as_slice().length()
+ self.prev_randao.length()
+ B64::ZERO.length() // nonce
+ self.base_fee_per_gas.length()
+ withdrawals_root.length()
+ self.blob_gas_used.length()
+ self.excess_blob_gas.length()
+ parent_beacon_block_root.length()

pub fn is_valid_versioned_hashes(
self: ExecutionEngine,
new_payload_request: NewPayloadRequest,
) -> anyhow::Result<bool> {
Copy link
Member

Choose a reason for hiding this comment

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

Would be nice if blob tx decoding fails, then return false.

Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't the higher level code handle the error, doing this suggestion would hide errors no?

/// Return ``True`` if and only if the version hashes computed by the blob transactions of
/// ``new_payload_request.execution_payload`` matches ``new_payload_request.versioned_hashes``.
pub fn is_valid_versioned_hashes(
self: ExecutionEngine,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
self: ExecutionEngine,
&self,

}

impl TransactionType {
pub fn find_transaction_type(transaction: &[u8]) -> anyhow::Result<TransactionType> {
Copy link
Member

Choose a reason for hiding this comment

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

How about using TryFrom trait? It would be like this... (Code is incomplete)

#[derive(Debug)]
pub enum TransactionTypeError {
    InvalidType(u8),
    EmptyTransaction,
}

impl TryFrom<&[u8]> for TransactionType {
    type Error = TransactionTypeError;

    fn try_from(transaction: &[u8]) -> anyhow::Result<Self> {
        let first_byte = transaction.first()
            .ok_or(TransactionTypeError::EmptyTransaction)?;
            
        match first_byte {
            3 => Ok(TransactionType::BlobTransaction),
            2 => Ok(TransactionType::FeeMarketTransaction),
            1 => Ok(TransactionType::AccessListTransaction),
            0 => Ok(TransactionType::LegacyTransaction),
            &x => Err(TransactionTypeError::InvalidType(x)),
        }
    }
}

Copy link
Contributor

Choose a reason for hiding this comment

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

            0 => Ok(TransactionType::LegacyTransaction),
            &x => Err(TransactionTypeError::InvalidType(x)),

This is invalid, the behavior already existing in the PR is correct. Legacy transactions are handled differently

Copy link
Contributor

Choose a reason for hiding this comment

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

I do think using try_from is an improvement though 👍

Copy link
Member

Choose a reason for hiding this comment

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

Ah my bad. yes I just wanted to suggest a naive example of using try_from

Copy link
Contributor

@KolbyML KolbyML left a comment

Choose a reason for hiding this comment

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

Here is some additional feedback


[dependencies]
alloy-primitives.workspace = true
alloy-rlp = { version = "0.3.8", default-features = false, features = ["derive"] }
Copy link
Contributor

Choose a reason for hiding this comment

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

I would do alloy-rlp.workspace = true as it is more minimalist

Comment on lines 125 to 141
pub fn calculate_merkle_patricia_root<'a, T: Encodable + 'a>(
items: impl IntoIterator<Item = &'a T>,
) -> anyhow::Result<B256> {
let memdb = Arc::new(MemoryDB::new(true));
let mut trie = EthTrie::new(memdb);

// Insert items into merkle patricia trie
for (index, tx) in items.into_iter().enumerate() {
let path = alloy_rlp::encode(index);
let encoded_tx = alloy_rlp::encode(tx);
trie.insert(&path, &encoded_tx)
.map_err(|err| anyhow!("Error inserting into merkle patricia trie: {err:?}"))?;
}

trie.root_hash()
.map_err(|err| anyhow!("Error calculating merkle patricia trie root: {err:?}"))
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
pub fn calculate_merkle_patricia_root<'a, T: Encodable + 'a>(
items: impl IntoIterator<Item = &'a T>,
) -> anyhow::Result<B256> {
let memdb = Arc::new(MemoryDB::new(true));
let mut trie = EthTrie::new(memdb);
// Insert items into merkle patricia trie
for (index, tx) in items.into_iter().enumerate() {
let path = alloy_rlp::encode(index);
let encoded_tx = alloy_rlp::encode(tx);
trie.insert(&path, &encoded_tx)
.map_err(|err| anyhow!("Error inserting into merkle patricia trie: {err:?}"))?;
}
trie.root_hash()
.map_err(|err| anyhow!("Error calculating merkle patricia trie root: {err:?}"))
}
/// Calculate the Merkle Patricia Trie root hash from a list of items
/// `(rlp(index), encoded(item))` pairs.
pub fn calculate_transactions_root<T>(transactions: &[T]) -> B256
where
T: Encodable,
{
ordered_trie_root_with_encoder(transactions, |tx: &T, buf| tx.encode(buf))
}
/// Calculates the root hash of the withdrawals.
pub fn calculate_withdrawals_root(withdrawals: &[Withdrawal]) -> B256 {
ordered_trie_root(withdrawals)
}

lets use alloy_trie instead to avoid passing an error

Comment on lines 62 to 63
let transactions_root = calculate_merkle_patricia_root(transactions.iter()).expect("");
let withdrawals_root = calculate_merkle_patricia_root(self.withdrawals.iter()).expect("");
Copy link
Contributor

@KolbyML KolbyML Feb 18, 2025

Choose a reason for hiding this comment

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

Suggested change
let transactions_root = calculate_merkle_patricia_root(transactions.iter()).expect("");
let withdrawals_root = calculate_merkle_patricia_root(self.withdrawals.iter()).expect("");
let transactions_root = calculate_transactions_root(&transactions);
let withdrawals_root = calculate_withdrawals_root(&self.withdrawal);

Cargo.toml Outdated
clap = "4"
discv5 = { version = "0.9.0", features = ["libp2p"] }
enr = "0.13.0"
eth_trie = "0.5.0"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
eth_trie = "0.5.0"
alloy-consensus = { version = "0.11.1", default-features = false }

blst.workspace = true
kzg.workspace = true
ethereum_hashing.workspace = true
eth_trie = "0.5.0"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
eth_trie = "0.5.0"
alloy-consensus.workspace = true

Cargo.toml Outdated
clap = "4"
discv5 = { version = "0.9.0", features = ["libp2p"] }
enr = "0.13.0"
alloy-consensus = { version = "0.11.1", default-features = false }
Copy link
Contributor

Choose a reason for hiding this comment

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

this package should be sorted in the list

Comment on lines 129 to 130
}
/// Calculates the root hash of the withdrawals.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
}
/// Calculates the root hash of the withdrawals.
}
/// Calculates the root hash of the withdrawals.

can you add a space here

Copy link
Contributor

@KolbyML KolbyML left a comment

Choose a reason for hiding this comment

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

going forward after you address a review, could you let us know by sending a message, and can you indicate which comments you dealt with, one way you could do this is 👍 the comment, or writing a comment if you disagree with the feedback

blst.workspace = true
kzg.workspace = true
ethereum_hashing.workspace = true
alloy-consensus.workspace = true
Copy link
Contributor

Choose a reason for hiding this comment

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

please sort

Copy link
Contributor

@KolbyML KolbyML left a comment

Choose a reason for hiding this comment

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

:shipit: PR looks good

@Kayden-ML Kayden-ML added this pull request to the merge queue Feb 18, 2025
Merged via the queue into ReamLabs:master with commit e40343a Feb 18, 2025
5 checks passed
@KolbyML KolbyML added this to the v0.1.0 milestone Feb 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants