-
Notifications
You must be signed in to change notification settings - Fork 65
feat: Implemented the is_valid_block_hash, is_valid_versioned_hashes functions #63
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: Implemented the is_valid_block_hash, is_valid_versioned_hashes functions #63
Conversation
3af4327 to
491f361
Compare
491f361 to
e02d9e3
Compare
crates/common/consensus/Cargo.toml
Outdated
|
|
||
| [dependencies] | ||
| alloy-primitives.workspace = true | ||
| alloy-rlp = { version = "0.3.8", default-features = false, features = ["derive"] } |
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.
| alloy-rlp = { version = "0.3.8", default-features = false, features = ["derive"] } | |
| alloy-rlp = { workspace = true } |
Same for eth_trie
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 would do alloy-rlp.workspace = true as it is more minimalist
syjn99
left a comment
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.
Mostly seems good!
| 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() |
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.
| 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> { |
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.
Would be nice if blob tx decoding fails, then return false.
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.
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, |
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.
| self: ExecutionEngine, | |
| &self, |
| } | ||
|
|
||
| impl TransactionType { | ||
| pub fn find_transaction_type(transaction: &[u8]) -> anyhow::Result<TransactionType> { |
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.
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)),
}
}
}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.
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
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 do think using try_from is an improvement though 👍
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.
Ah my bad. yes I just wanted to suggest a naive example of using try_from
KolbyML
left a comment
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.
Here is some additional feedback
crates/common/consensus/Cargo.toml
Outdated
|
|
||
| [dependencies] | ||
| alloy-primitives.workspace = true | ||
| alloy-rlp = { version = "0.3.8", default-features = false, features = ["derive"] } |
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 would do alloy-rlp.workspace = true as it is more minimalist
| 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:?}")) | ||
| } |
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.
| 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
| let transactions_root = calculate_merkle_patricia_root(transactions.iter()).expect(""); | ||
| let withdrawals_root = calculate_merkle_patricia_root(self.withdrawals.iter()).expect(""); |
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.
| 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" |
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.
| eth_trie = "0.5.0" | |
| alloy-consensus = { version = "0.11.1", default-features = false } |
crates/common/consensus/Cargo.toml
Outdated
| blst.workspace = true | ||
| kzg.workspace = true | ||
| ethereum_hashing.workspace = true | ||
| eth_trie = "0.5.0" |
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.
| 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 } |
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 package should be sorted in the list
| } | ||
| /// Calculates the root hash of the withdrawals. |
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.
| } | |
| /// Calculates the root hash of the withdrawals. | |
| } | |
| /// Calculates the root hash of the withdrawals. |
can you add a space here
KolbyML
left a comment
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.
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
crates/common/consensus/Cargo.toml
Outdated
| blst.workspace = true | ||
| kzg.workspace = true | ||
| ethereum_hashing.workspace = true | ||
| alloy-consensus.workspace = true |
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.
please sort
KolbyML
left a comment
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.
PR looks good
Implemented the is_valid_block_hash, is_valid_versioned_hashes functions
https://ethereum.github.io/consensus-specs/specs/deneb/beacon-chain/#is_valid_block_hash
https://ethereum.github.io/consensus-specs/specs/deneb/beacon-chain/#is_valid_versioned_hashes