From e5896d9b71e515ddc8b0411d8a37c1a30aae17c6 Mon Sep 17 00:00:00 2001 From: sean Date: Sun, 5 Feb 2023 18:14:24 -0500 Subject: [PATCH] re-order methods --- beacon_node/execution_layer/src/lib.rs | 251 ++++++++++++------------- 1 file changed, 125 insertions(+), 126 deletions(-) diff --git a/beacon_node/execution_layer/src/lib.rs b/beacon_node/execution_layer/src/lib.rs index 902b5beec1..64d46d4852 100644 --- a/beacon_node/execution_layer/src/lib.rs +++ b/beacon_node/execution_layer/src/lib.rs @@ -1931,6 +1931,131 @@ async fn timed_future, T>(metric: &str, future: F) -> (T, (result, duration) } +#[derive(Debug)] +pub enum BlobTxConversionError { + /// The transaction type was not set. + NoTransactionType, + /// The transaction chain ID was not set. + NoChainId, + /// The transaction nonce was too large to fit in a `u64`. + NonceTooLarge, + /// The transaction gas was too large to fit in a `u64`. + GasTooHigh, + /// Missing the `max_fee_per_gas` field. + MaxFeePerGasMissing, + /// Missing the `max_priority_fee_per_gas` field. + MaxPriorityFeePerGasMissing, + /// Missing the `access_list` field. + AccessListMissing, + /// Missing the `max_fee_per_data_gas` field. + MaxFeePerDataGasMissing, + /// Missing the `max_data_gas` field. + BlobVersionedHashesMissing, + /// There was an error converting the transaction to SSZ. + SszError(ssz_types::Error), + /// There was an error converting the transaction from JSON. + SerdeJson(serde_json::Error), +} + +impl From for BlobTxConversionError { + fn from(value: ssz_types::Error) -> Self { + Self::SszError(value) + } +} + +impl From for BlobTxConversionError { + fn from(value: serde_json::Error) -> Self { + Self::SerdeJson(value) + } +} + +/// A utility function to convert a `ethers-rs` `Transaction` into the correct bytes encoding based +/// on transaction type. That means RLP encoding if this is a transaction other than a +/// `BLOB_TX_TYPE` transaction in which case, SSZ encoding will be used. +fn ethers_tx_to_bytes( + transaction: &EthersTransaction, +) -> Result, BlobTxConversionError> { + let tx_type = transaction + .transaction_type + .ok_or(BlobTxConversionError::NoTransactionType)? + .as_u64(); + let tx = if BLOB_TX_TYPE as u64 == tx_type { + let chain_id = transaction + .chain_id + .ok_or(BlobTxConversionError::NoChainId)?; + let nonce = if transaction.nonce > Uint256::from(u64::MAX) { + return Err(BlobTxConversionError::NonceTooLarge); + } else { + transaction.nonce.as_u64() + }; + let max_priority_fee_per_gas = transaction + .max_priority_fee_per_gas + .ok_or(BlobTxConversionError::MaxPriorityFeePerGasMissing)?; + let max_fee_per_gas = transaction + .max_fee_per_gas + .ok_or(BlobTxConversionError::MaxFeePerGasMissing)?; + let gas = if transaction.gas > Uint256::from(u64::MAX) { + return Err(BlobTxConversionError::GasTooHigh); + } else { + transaction.gas.as_u64() + }; + let to = transaction.to; + let value = transaction.value; + let data = VariableList::new(transaction.input.to_vec())?; + let access_list = VariableList::new( + transaction + .access_list + .as_ref() + .ok_or(BlobTxConversionError::AccessListMissing)? + .0 + .iter() + .map(|access_tuple| { + Ok(AccessTuple { + address: access_tuple.address, + storage_keys: VariableList::new(access_tuple.storage_keys.clone())?, + }) + }) + .collect::, BlobTxConversionError>>()?, + )?; + let max_fee_per_data_gas = transaction + .other + .get("max_fee_per_data_gas") + .ok_or(BlobTxConversionError::MaxFeePerDataGasMissing)? + .as_str() + .ok_or(BlobTxConversionError::MaxFeePerDataGasMissing)? + .parse() + .map_err(|_| BlobTxConversionError::MaxFeePerDataGasMissing)?; + let blob_versioned_hashes = serde_json::from_str( + transaction + .other + .get("blob_versioned_hashes") + .ok_or(BlobTxConversionError::BlobVersionedHashesMissing)? + .as_str() + .ok_or(BlobTxConversionError::BlobVersionedHashesMissing)?, + )?; + BlobTransaction { + chain_id, + nonce, + max_priority_fee_per_gas, + max_fee_per_gas, + gas, + to, + value, + data, + access_list, + max_fee_per_data_gas, + blob_versioned_hashes, + }.as_ssz_bytes() + } else { + transaction.rlp().to_vec() + }; + VariableList::new(tx).map_err(Into::into) +} + +fn noop(_: &ExecutionLayer, _: &ExecutionPayload) -> Option> { + None +} + #[cfg(test)] mod test { use super::*; @@ -2078,132 +2203,6 @@ mod test { } } -fn noop(_: &ExecutionLayer, _: &ExecutionPayload) -> Option> { - None -} - -#[derive(Debug)] -pub enum BlobTxConversionError { - /// The transaction type was not set. - NoTransactionType, - /// The transaction chain ID was not set. - NoChainId, - /// The transaction nonce was too large to fit in a `u64`. - NonceTooLarge, - /// The transaction gas was too large to fit in a `u64`. - GasTooHigh, - /// Missing the `max_fee_per_gas` field. - MaxFeePerGasMissing, - /// Missing the `max_priority_fee_per_gas` field. - MaxPriorityFeePerGasMissing, - /// Missing the `access_list` field. - AccessListMissing, - /// Missing the `max_fee_per_data_gas` field. - MaxFeePerDataGasMissing, - /// Missing the `max_data_gas` field. - BlobVersionedHashesMissing, - /// There was an error converting the transaction to SSZ. - SszError(ssz_types::Error), - /// There was an error converting the transaction from JSON. - SerdeJson(serde_json::Error), -} - -impl From for BlobTxConversionError { - fn from(value: ssz_types::Error) -> Self { - Self::SszError(value) - } -} - -impl From for BlobTxConversionError { - fn from(value: serde_json::Error) -> Self { - Self::SerdeJson(value) - } -} - -/// A utility function to convert a `ethers-rs` `Transaction` into the correct bytes encoding based -/// on transaction type. That means RLP encoding if this is a transaction other than a -/// `BLOB_TX_TYPE` transaction in which case, SSZ encoding will be used. -fn ethers_tx_to_bytes( - transaction: &EthersTransaction, -) -> Result, BlobTxConversionError> { - let tx_type = transaction - .transaction_type - .ok_or(BlobTxConversionError::NoTransactionType)? - .as_u64(); - let tx = if BLOB_TX_TYPE as u64 == tx_type { - let chain_id = transaction - .chain_id - .ok_or(BlobTxConversionError::NoChainId)?; - let nonce = if transaction.nonce > Uint256::from(u64::MAX) { - return Err(BlobTxConversionError::NonceTooLarge); - } else { - transaction.nonce.as_u64() - }; - let max_priority_fee_per_gas = transaction - .max_priority_fee_per_gas - .ok_or(BlobTxConversionError::MaxPriorityFeePerGasMissing)?; - let max_fee_per_gas = transaction - .max_fee_per_gas - .ok_or(BlobTxConversionError::MaxFeePerGasMissing)?; - let gas = if transaction.gas > Uint256::from(u64::MAX) { - return Err(BlobTxConversionError::GasTooHigh); - } else { - transaction.gas.as_u64() - }; - let to = transaction.to; - let value = transaction.value; - let data = VariableList::new(transaction.input.to_vec())?; - let access_list = VariableList::new( - transaction - .access_list - .as_ref() - .ok_or(BlobTxConversionError::AccessListMissing)? - .0 - .iter() - .map(|access_tuple| { - Ok(AccessTuple { - address: access_tuple.address, - storage_keys: VariableList::new(access_tuple.storage_keys.clone())?, - }) - }) - .collect::, BlobTxConversionError>>()?, - )?; - let max_fee_per_data_gas = transaction - .other - .get("max_fee_per_data_gas") - .ok_or(BlobTxConversionError::MaxFeePerDataGasMissing)? - .as_str() - .ok_or(BlobTxConversionError::MaxFeePerDataGasMissing)? - .parse() - .map_err(|_| BlobTxConversionError::MaxFeePerDataGasMissing)?; - let blob_versioned_hashes = serde_json::from_str( - transaction - .other - .get("blob_versioned_hashes") - .ok_or(BlobTxConversionError::BlobVersionedHashesMissing)? - .as_str() - .ok_or(BlobTxConversionError::BlobVersionedHashesMissing)?, - )?; - BlobTransaction { - chain_id, - nonce, - max_priority_fee_per_gas, - max_fee_per_gas, - gas, - to, - value, - data, - access_list, - max_fee_per_data_gas, - blob_versioned_hashes, - } - .as_ssz_bytes() - } else { - transaction.rlp().to_vec() - }; - VariableList::new(tx).map_err(Into::into) -} - #[cfg(test)] /// Returns the duration since the unix epoch. fn timestamp_now() -> u64 {