re-order methods

This commit is contained in:
sean 2023-02-05 18:14:24 -05:00
parent 1315098c15
commit e5896d9b71

View File

@ -1931,6 +1931,131 @@ async fn timed_future<F: Future<Output = T>, 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<ssz_types::Error> for BlobTxConversionError {
fn from(value: ssz_types::Error) -> Self {
Self::SszError(value)
}
}
impl From<serde_json::Error> 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<T: EthSpec>(
transaction: &EthersTransaction,
) -> Result<Transaction<T::MaxBytesPerTransaction>, 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::<Result<Vec<AccessTuple>, 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<T: EthSpec>(_: &ExecutionLayer<T>, _: &ExecutionPayload<T>) -> Option<ExecutionPayload<T>> {
None
}
#[cfg(test)]
mod test {
use super::*;
@ -2078,132 +2203,6 @@ mod test {
}
}
fn noop<T: EthSpec>(_: &ExecutionLayer<T>, _: &ExecutionPayload<T>) -> Option<ExecutionPayload<T>> {
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<ssz_types::Error> for BlobTxConversionError {
fn from(value: ssz_types::Error) -> Self {
Self::SszError(value)
}
}
impl From<serde_json::Error> 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<T: EthSpec>(
transaction: &EthersTransaction,
) -> Result<Transaction<T::MaxBytesPerTransaction>, 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::<Result<Vec<AccessTuple>, 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 {