add use_self clippy lint (#8325)

This commit is contained in:
Thomas Coratger
2024-05-29 15:14:14 +02:00
committed by GitHub
parent 0cb5358fef
commit 19c529e8df
200 changed files with 1817 additions and 1954 deletions

View File

@ -212,7 +212,7 @@ mod elias_fano {
}
impl<'de> Deserialize<'de> for IntegerList {
fn deserialize<D>(deserializer: D) -> Result<IntegerList, D::Error>
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{

View File

@ -38,7 +38,7 @@ impl Account {
/// Makes an [Account] from [GenesisAccount] type
pub fn from_genesis_account(value: &GenesisAccount) -> Self {
Account {
Self {
// nonce must exist, so we default to zero when converting a genesis account
nonce: value.nonce.unwrap_or_default(),
balance: value.balance,
@ -115,9 +115,9 @@ impl Compact for Bytecode {
let bytes = Bytes::from(buf.copy_to_bytes(len as usize));
let variant = buf.read_u8().expect("could not read bytecode variant");
let decoded = match variant {
0 => Bytecode(RevmBytecode::new_raw(bytes)),
0 => Self(RevmBytecode::new_raw(bytes)),
1 => unreachable!("Junk data in database: checked Bytecode variant was removed"),
2 => Bytecode(unsafe {
2 => Self(unsafe {
RevmBytecode::new_analyzed(
bytes,
buf.read_u64::<BigEndian>().unwrap() as usize,

View File

@ -122,7 +122,7 @@ impl TryFrom<alloy_rpc_types::Transaction> for Transaction {
.into(),
))
}
Ok(Transaction::Legacy(TxLegacy {
Ok(Self::Legacy(TxLegacy {
chain_id: tx.chain_id,
nonce: tx.nonce,
gas_price: tx.gas_price.ok_or(ConversionError::MissingGasPrice)?,
@ -137,7 +137,7 @@ impl TryFrom<alloy_rpc_types::Transaction> for Transaction {
}
Some(TxType::Eip2930) => {
// eip2930
Ok(Transaction::Eip2930(TxEip2930 {
Ok(Self::Eip2930(TxEip2930 {
chain_id: tx.chain_id.ok_or(ConversionError::MissingChainId)?,
nonce: tx.nonce,
gas_limit: tx
@ -153,7 +153,7 @@ impl TryFrom<alloy_rpc_types::Transaction> for Transaction {
}
Some(TxType::Eip1559) => {
// EIP-1559
Ok(Transaction::Eip1559(TxEip1559 {
Ok(Self::Eip1559(TxEip1559 {
chain_id: tx.chain_id.ok_or(ConversionError::MissingChainId)?,
nonce: tx.nonce,
max_priority_fee_per_gas: tx
@ -174,7 +174,7 @@ impl TryFrom<alloy_rpc_types::Transaction> for Transaction {
}
Some(TxType::Eip4844) => {
// EIP-4844
Ok(Transaction::Eip4844(TxEip4844 {
Ok(Self::Eip4844(TxEip4844 {
chain_id: tx.chain_id.ok_or(ConversionError::MissingChainId)?,
nonce: tx.nonce,
max_priority_fee_per_gas: tx
@ -215,7 +215,7 @@ impl TryFrom<alloy_rpc_types::Transaction> for TransactionSigned {
let signature = tx.signature.ok_or(ConversionError::MissingSignature)?;
let transaction: Transaction = tx.try_into()?;
Ok(TransactionSigned::from_transaction_and_signature(
Ok(Self::from_transaction_and_signature(
transaction.clone(),
Signature {
r: signature.r,

View File

@ -11,6 +11,6 @@ pub struct ChainInfo {
impl From<ChainInfo> for BlockNumHash {
fn from(value: ChainInfo) -> Self {
BlockNumHash { number: value.best_number, hash: value.best_hash }
Self { number: value.best_number, hash: value.best_hash }
}
}

View File

@ -468,13 +468,13 @@ pub enum BaseFeeParamsKind {
impl From<BaseFeeParams> for BaseFeeParamsKind {
fn from(params: BaseFeeParams) -> Self {
BaseFeeParamsKind::Constant(params)
Self::Constant(params)
}
}
impl From<ForkBaseFeeParams> for BaseFeeParamsKind {
fn from(params: ForkBaseFeeParams) -> Self {
BaseFeeParamsKind::Variable(params)
Self::Variable(params)
}
}
@ -485,7 +485,7 @@ pub struct ForkBaseFeeParams(Vec<(Hardfork, BaseFeeParams)>);
impl From<Vec<(Hardfork, BaseFeeParams)>> for ForkBaseFeeParams {
fn from(params: Vec<(Hardfork, BaseFeeParams)>) -> Self {
ForkBaseFeeParams(params)
Self(params)
}
}
@ -533,8 +533,8 @@ pub struct ChainSpec {
}
impl Default for ChainSpec {
fn default() -> ChainSpec {
ChainSpec {
fn default() -> Self {
Self {
chain: Default::default(),
genesis_hash: Default::default(),
genesis: Default::default(),
@ -1347,7 +1347,7 @@ pub enum ForkCondition {
impl ForkCondition {
/// Returns true if the fork condition is timestamp based.
pub fn is_timestamp(&self) -> bool {
matches!(self, ForkCondition::Timestamp(_))
matches!(self, Self::Timestamp(_))
}
/// Checks whether the fork condition is satisfied at the given block.
@ -1356,15 +1356,15 @@ impl ForkCondition {
///
/// For timestamp conditions, this will always return false.
pub fn active_at_block(&self, current_block: BlockNumber) -> bool {
matches!(self, ForkCondition::Block(block)
| ForkCondition::TTD { fork_block: Some(block), .. } if current_block >= *block)
matches!(self, Self::Block(block)
| Self::TTD { fork_block: Some(block), .. } if current_block >= *block)
}
/// Checks if the given block is the first block that satisfies the fork condition.
///
/// This will return false for any condition that is not block based.
pub fn transitions_at_block(&self, current_block: BlockNumber) -> bool {
matches!(self, ForkCondition::Block(block) if current_block == *block)
matches!(self, Self::Block(block) if current_block == *block)
}
/// Checks whether the fork condition is satisfied at the given total difficulty and difficulty
@ -1377,7 +1377,7 @@ impl ForkCondition {
///
/// This will return false for any condition that is not TTD-based.
pub fn active_at_ttd(&self, ttd: U256, difficulty: U256) -> bool {
matches!(self, ForkCondition::TTD { total_difficulty, .. }
matches!(self, Self::TTD { total_difficulty, .. }
if ttd.saturating_sub(difficulty) >= *total_difficulty)
}
@ -1385,7 +1385,7 @@ impl ForkCondition {
///
/// This will return false for any condition that is not timestamp-based.
pub fn active_at_timestamp(&self, timestamp: u64) -> bool {
matches!(self, ForkCondition::Timestamp(time) if timestamp >= *time)
matches!(self, Self::Timestamp(time) if timestamp >= *time)
}
/// Checks whether the fork condition is satisfied at the given head block.
@ -1406,7 +1406,7 @@ impl ForkCondition {
/// Returns `None` for fork conditions that are not TTD based.
pub fn ttd(&self) -> Option<U256> {
match self {
ForkCondition::TTD { total_difficulty, .. } => Some(*total_difficulty),
Self::TTD { total_difficulty, .. } => Some(*total_difficulty),
_ => None,
}
}
@ -1414,7 +1414,7 @@ impl ForkCondition {
/// Returns the timestamp of the fork condition, if it is timestamp based.
pub fn as_timestamp(&self) -> Option<u64> {
match self {
ForkCondition::Timestamp(timestamp) => Some(*timestamp),
Self::Timestamp(timestamp) => Some(*timestamp),
_ => None,
}
}
@ -1602,7 +1602,7 @@ pub struct DepositContract {
impl DepositContract {
fn new(address: Address, block: BlockNumber, topic: B256) -> Self {
DepositContract { address, block, topic }
Self { address, block, topic }
}
}

View File

@ -111,7 +111,7 @@ pub struct Header {
impl Default for Header {
fn default() -> Self {
Header {
Self {
parent_hash: Default::default(),
ommers_hash: EMPTY_OMMER_ROOT_HASH,
beneficiary: Default::default(),
@ -674,7 +674,7 @@ impl SealedHeader {
#[inline(always)]
fn validate_gas_limit(
&self,
parent: &SealedHeader,
parent: &Self,
chain_spec: &ChainSpec,
) -> Result<(), HeaderValidationError> {
// Determine the parent gas limit, considering elasticity multiplier on the London fork.
@ -739,7 +739,7 @@ impl SealedHeader {
/// of certain features (e.g., Optimism feature) or the activation of specific hardforks.
pub fn validate_against_parent(
&self,
parent: &SealedHeader,
parent: &Self,
chain_spec: &ChainSpec,
) -> Result<(), HeaderValidationError> {
// Parent number is consistent.
@ -826,7 +826,7 @@ impl SealedHeader {
/// parent header fields.
pub fn validate_4844_header_against_parent(
&self,
parent: &SealedHeader,
parent: &Self,
) -> Result<(), HeaderValidationError> {
// From [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844#header-extension):
//
@ -885,7 +885,7 @@ impl proptest::arbitrary::Arbitrary for SealedHeader {
// map valid header strategy by sealing
valid_header_strategy().prop_map(|header| header.seal_slow()).boxed()
}
type Strategy = proptest::strategy::BoxedStrategy<SealedHeader>;
type Strategy = proptest::strategy::BoxedStrategy<Self>;
}
#[cfg(any(test, feature = "arbitrary"))]
@ -971,12 +971,12 @@ pub enum HeadersDirection {
impl HeadersDirection {
/// Returns true for rising block numbers
pub fn is_rising(&self) -> bool {
matches!(self, HeadersDirection::Rising)
matches!(self, Self::Rising)
}
/// Returns true for falling block numbers
pub fn is_falling(&self) -> bool {
matches!(self, HeadersDirection::Falling)
matches!(self, Self::Falling)
}
/// Converts the bool into a direction.
@ -987,9 +987,9 @@ impl HeadersDirection {
/// [`HeadersDirection::Falling`] block numbers for `reverse == 1 == true`
pub fn new(reverse: bool) -> Self {
if reverse {
HeadersDirection::Falling
Self::Falling
} else {
HeadersDirection::Rising
Self::Rising
}
}
}

View File

@ -123,7 +123,7 @@ impl<'de> Visitor<'de> for IntegerListVisitor {
}
impl<'de> Deserialize<'de> for IntegerList {
fn deserialize<D>(deserializer: D) -> Result<IntegerList, D::Error>
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{

View File

@ -51,8 +51,8 @@ mod tests {
}
impl From<Log> for AlloyLog {
fn from(log: Log) -> AlloyLog {
AlloyLog::new_unchecked(log.address, log.topics, log.data)
fn from(log: Log) -> Self {
Self::new_unchecked(log.address, log.topics, log.data)
}
}

View File

@ -29,16 +29,16 @@ impl PruneMode {
tip: BlockNumber,
segment: PruneSegment,
purpose: PrunePurpose,
) -> Result<Option<(BlockNumber, PruneMode)>, PruneSegmentError> {
) -> Result<Option<(BlockNumber, Self)>, PruneSegmentError> {
let result = match self {
PruneMode::Full if segment.min_blocks(purpose) == 0 => Some((tip, *self)),
PruneMode::Distance(distance) if *distance > tip => None, // Nothing to prune yet
PruneMode::Distance(distance) if *distance >= segment.min_blocks(purpose) => {
Self::Full if segment.min_blocks(purpose) == 0 => Some((tip, *self)),
Self::Distance(distance) if *distance > tip => None, // Nothing to prune yet
Self::Distance(distance) if *distance >= segment.min_blocks(purpose) => {
Some((tip - distance, *self))
}
PruneMode::Before(n) if *n == tip + 1 && purpose.is_static_file() => Some((tip, *self)),
PruneMode::Before(n) if *n > tip => None, // Nothing to prune yet
PruneMode::Before(n) if tip - n >= segment.min_blocks(purpose) => Some((n - 1, *self)),
Self::Before(n) if *n == tip + 1 && purpose.is_static_file() => Some((tip, *self)),
Self::Before(n) if *n > tip => None, // Nothing to prune yet
Self::Before(n) if tip - n >= segment.min_blocks(purpose) => Some((n - 1, *self)),
_ => return Err(PruneSegmentError::Configuration(segment)),
};
Ok(result)
@ -47,14 +47,14 @@ impl PruneMode {
/// Check if target block should be pruned according to the provided prune mode and tip.
pub fn should_prune(&self, block: BlockNumber, tip: BlockNumber) -> bool {
match self {
PruneMode::Full => true,
PruneMode::Distance(distance) => {
Self::Full => true,
Self::Distance(distance) => {
if *distance > tip {
return false
}
block < tip - *distance
}
PruneMode::Before(n) => *n > block,
Self::Before(n) => *n > block,
}
}

View File

@ -48,7 +48,7 @@ pub struct PruneModes {
impl PruneModes {
/// Sets pruning to no target.
pub fn none() -> Self {
PruneModes::default()
Self::default()
}
/// Sets pruning to all targets.

View File

@ -157,7 +157,7 @@ impl FromIterator<Vec<Option<Receipt>>> for Receipts {
impl From<Receipt> for ReceiptWithBloom {
fn from(receipt: Receipt) -> Self {
let bloom = receipt.bloom_slow();
ReceiptWithBloom { receipt, bloom }
Self { receipt, bloom }
}
}
@ -245,7 +245,7 @@ impl proptest::arbitrary::Arbitrary for Receipt {
arbitrary_receipt().boxed()
}
type Strategy = proptest::strategy::BoxedStrategy<Receipt>;
type Strategy = proptest::strategy::BoxedStrategy<Self>;
}
#[cfg(any(test, feature = "arbitrary"))]

View File

@ -71,7 +71,7 @@ impl Compact for MerkleCheckpoint {
}
let (state, buf) = HashBuilderState::from_compact(buf, 0);
(MerkleCheckpoint { target_block, last_account_key, walker_stack, state }, buf)
(Self { target_block, last_account_key, walker_stack, state }, buf)
}
}

View File

@ -38,66 +38,66 @@ pub enum StageId {
impl StageId {
/// All supported Stages
pub const ALL: [StageId; 12] = [
StageId::Headers,
StageId::Bodies,
StageId::SenderRecovery,
StageId::Execution,
StageId::MerkleUnwind,
StageId::AccountHashing,
StageId::StorageHashing,
StageId::MerkleExecute,
StageId::TransactionLookup,
StageId::IndexStorageHistory,
StageId::IndexAccountHistory,
StageId::Finish,
pub const ALL: [Self; 12] = [
Self::Headers,
Self::Bodies,
Self::SenderRecovery,
Self::Execution,
Self::MerkleUnwind,
Self::AccountHashing,
Self::StorageHashing,
Self::MerkleExecute,
Self::TransactionLookup,
Self::IndexStorageHistory,
Self::IndexAccountHistory,
Self::Finish,
];
/// Stages that require state.
pub const STATE_REQUIRED: [StageId; 7] = [
StageId::Execution,
StageId::MerkleUnwind,
StageId::AccountHashing,
StageId::StorageHashing,
StageId::MerkleExecute,
StageId::IndexStorageHistory,
StageId::IndexAccountHistory,
pub const STATE_REQUIRED: [Self; 7] = [
Self::Execution,
Self::MerkleUnwind,
Self::AccountHashing,
Self::StorageHashing,
Self::MerkleExecute,
Self::IndexStorageHistory,
Self::IndexAccountHistory,
];
/// Return stage id formatted as string.
pub fn as_str(&self) -> &str {
match self {
#[allow(deprecated)]
StageId::StaticFile => "StaticFile",
StageId::Headers => "Headers",
StageId::Bodies => "Bodies",
StageId::SenderRecovery => "SenderRecovery",
StageId::Execution => "Execution",
StageId::MerkleUnwind => "MerkleUnwind",
StageId::AccountHashing => "AccountHashing",
StageId::StorageHashing => "StorageHashing",
StageId::MerkleExecute => "MerkleExecute",
StageId::TransactionLookup => "TransactionLookup",
StageId::IndexAccountHistory => "IndexAccountHistory",
StageId::IndexStorageHistory => "IndexStorageHistory",
StageId::Finish => "Finish",
StageId::Other(s) => s,
Self::StaticFile => "StaticFile",
Self::Headers => "Headers",
Self::Bodies => "Bodies",
Self::SenderRecovery => "SenderRecovery",
Self::Execution => "Execution",
Self::MerkleUnwind => "MerkleUnwind",
Self::AccountHashing => "AccountHashing",
Self::StorageHashing => "StorageHashing",
Self::MerkleExecute => "MerkleExecute",
Self::TransactionLookup => "TransactionLookup",
Self::IndexAccountHistory => "IndexAccountHistory",
Self::IndexStorageHistory => "IndexStorageHistory",
Self::Finish => "Finish",
Self::Other(s) => s,
}
}
/// Returns true if it's a downloading stage [StageId::Headers] or [StageId::Bodies]
pub fn is_downloading_stage(&self) -> bool {
matches!(self, StageId::Headers | StageId::Bodies)
matches!(self, Self::Headers | Self::Bodies)
}
/// Returns `true` if it's [TransactionLookup](StageId::TransactionLookup) stage.
pub fn is_tx_lookup(&self) -> bool {
matches!(self, StageId::TransactionLookup)
matches!(self, Self::TransactionLookup)
}
/// Returns true indicating if it's the finish stage [StageId::Finish]
pub fn is_finish(&self) -> bool {
matches!(self, StageId::Finish)
matches!(self, Self::Finish)
}
}

View File

@ -29,8 +29,8 @@ impl PipelineTarget {
/// - `None`: If the target is for backward unwinding.
pub fn sync_target(self) -> Option<BlockHash> {
match self {
PipelineTarget::Sync(hash) => Some(hash),
PipelineTarget::Unwind(_) => None,
Self::Sync(hash) => Some(hash),
Self::Unwind(_) => None,
}
}
@ -42,8 +42,8 @@ impl PipelineTarget {
/// - `None`: If the target is for forward synchronization.
pub fn unwind_target(self) -> Option<BlockNumber> {
match self {
PipelineTarget::Sync(_) => None,
PipelineTarget::Unwind(number) => Some(number),
Self::Sync(_) => None,
Self::Unwind(number) => Some(number),
}
}
}

View File

@ -141,36 +141,36 @@ impl Transaction {
/// It is only for signature signing or signer recovery.
pub fn signature_hash(&self) -> B256 {
match self {
Transaction::Legacy(tx) => tx.signature_hash(),
Transaction::Eip2930(tx) => tx.signature_hash(),
Transaction::Eip1559(tx) => tx.signature_hash(),
Transaction::Eip4844(tx) => tx.signature_hash(),
Self::Legacy(tx) => tx.signature_hash(),
Self::Eip2930(tx) => tx.signature_hash(),
Self::Eip1559(tx) => tx.signature_hash(),
Self::Eip4844(tx) => tx.signature_hash(),
#[cfg(feature = "optimism")]
Transaction::Deposit(_) => B256::ZERO,
Self::Deposit(_) => B256::ZERO,
}
}
/// Get chain_id.
pub fn chain_id(&self) -> Option<u64> {
match self {
Transaction::Legacy(TxLegacy { chain_id, .. }) => *chain_id,
Transaction::Eip2930(TxEip2930 { chain_id, .. }) |
Transaction::Eip1559(TxEip1559 { chain_id, .. }) |
Transaction::Eip4844(TxEip4844 { chain_id, .. }) => Some(*chain_id),
Self::Legacy(TxLegacy { chain_id, .. }) => *chain_id,
Self::Eip2930(TxEip2930 { chain_id, .. }) |
Self::Eip1559(TxEip1559 { chain_id, .. }) |
Self::Eip4844(TxEip4844 { chain_id, .. }) => Some(*chain_id),
#[cfg(feature = "optimism")]
Transaction::Deposit(_) => None,
Self::Deposit(_) => None,
}
}
/// Sets the transaction's chain id to the provided value.
pub fn set_chain_id(&mut self, chain_id: u64) {
match self {
Transaction::Legacy(TxLegacy { chain_id: ref mut c, .. }) => *c = Some(chain_id),
Transaction::Eip2930(TxEip2930 { chain_id: ref mut c, .. }) |
Transaction::Eip1559(TxEip1559 { chain_id: ref mut c, .. }) |
Transaction::Eip4844(TxEip4844 { chain_id: ref mut c, .. }) => *c = chain_id,
Self::Legacy(TxLegacy { chain_id: ref mut c, .. }) => *c = Some(chain_id),
Self::Eip2930(TxEip2930 { chain_id: ref mut c, .. }) |
Self::Eip1559(TxEip1559 { chain_id: ref mut c, .. }) |
Self::Eip4844(TxEip4844 { chain_id: ref mut c, .. }) => *c = chain_id,
#[cfg(feature = "optimism")]
Transaction::Deposit(_) => { /* noop */ }
Self::Deposit(_) => { /* noop */ }
}
}
@ -178,12 +178,12 @@ impl Transaction {
/// [`TxKind::Create`] if the transaction is a contract creation.
pub fn kind(&self) -> TxKind {
match self {
Transaction::Legacy(TxLegacy { to, .. }) |
Transaction::Eip2930(TxEip2930 { to, .. }) |
Transaction::Eip1559(TxEip1559 { to, .. }) => *to,
Transaction::Eip4844(TxEip4844 { to, .. }) => TxKind::Call(*to),
Self::Legacy(TxLegacy { to, .. }) |
Self::Eip2930(TxEip2930 { to, .. }) |
Self::Eip1559(TxEip1559 { to, .. }) => *to,
Self::Eip4844(TxEip4844 { to, .. }) => TxKind::Call(*to),
#[cfg(feature = "optimism")]
Transaction::Deposit(TxDeposit { to, .. }) => *to,
Self::Deposit(TxDeposit { to, .. }) => *to,
}
}
@ -198,37 +198,37 @@ impl Transaction {
/// Get the transaction's type
pub fn tx_type(&self) -> TxType {
match self {
Transaction::Legacy(legacy_tx) => legacy_tx.tx_type(),
Transaction::Eip2930(access_list_tx) => access_list_tx.tx_type(),
Transaction::Eip1559(dynamic_fee_tx) => dynamic_fee_tx.tx_type(),
Transaction::Eip4844(blob_tx) => blob_tx.tx_type(),
Self::Legacy(legacy_tx) => legacy_tx.tx_type(),
Self::Eip2930(access_list_tx) => access_list_tx.tx_type(),
Self::Eip1559(dynamic_fee_tx) => dynamic_fee_tx.tx_type(),
Self::Eip4844(blob_tx) => blob_tx.tx_type(),
#[cfg(feature = "optimism")]
Transaction::Deposit(deposit_tx) => deposit_tx.tx_type(),
Self::Deposit(deposit_tx) => deposit_tx.tx_type(),
}
}
/// Gets the transaction's value field.
pub fn value(&self) -> U256 {
*match self {
Transaction::Legacy(TxLegacy { value, .. }) |
Transaction::Eip2930(TxEip2930 { value, .. }) |
Transaction::Eip1559(TxEip1559 { value, .. }) |
Transaction::Eip4844(TxEip4844 { value, .. }) => value,
Self::Legacy(TxLegacy { value, .. }) |
Self::Eip2930(TxEip2930 { value, .. }) |
Self::Eip1559(TxEip1559 { value, .. }) |
Self::Eip4844(TxEip4844 { value, .. }) => value,
#[cfg(feature = "optimism")]
Transaction::Deposit(TxDeposit { value, .. }) => value,
Self::Deposit(TxDeposit { value, .. }) => value,
}
}
/// Get the transaction's nonce.
pub fn nonce(&self) -> u64 {
match self {
Transaction::Legacy(TxLegacy { nonce, .. }) |
Transaction::Eip2930(TxEip2930 { nonce, .. }) |
Transaction::Eip1559(TxEip1559 { nonce, .. }) |
Transaction::Eip4844(TxEip4844 { nonce, .. }) => *nonce,
Self::Legacy(TxLegacy { nonce, .. }) |
Self::Eip2930(TxEip2930 { nonce, .. }) |
Self::Eip1559(TxEip1559 { nonce, .. }) |
Self::Eip4844(TxEip4844 { nonce, .. }) => *nonce,
// Deposit transactions do not have nonces.
#[cfg(feature = "optimism")]
Transaction::Deposit(_) => 0,
Self::Deposit(_) => 0,
}
}
@ -237,34 +237,34 @@ impl Transaction {
/// Returns `None` for legacy transactions.
pub fn access_list(&self) -> Option<&AccessList> {
match self {
Transaction::Legacy(_) => None,
Transaction::Eip2930(tx) => Some(&tx.access_list),
Transaction::Eip1559(tx) => Some(&tx.access_list),
Transaction::Eip4844(tx) => Some(&tx.access_list),
Self::Legacy(_) => None,
Self::Eip2930(tx) => Some(&tx.access_list),
Self::Eip1559(tx) => Some(&tx.access_list),
Self::Eip4844(tx) => Some(&tx.access_list),
#[cfg(feature = "optimism")]
Transaction::Deposit(_) => None,
Self::Deposit(_) => None,
}
}
/// Get the gas limit of the transaction.
pub fn gas_limit(&self) -> u64 {
match self {
Transaction::Legacy(TxLegacy { gas_limit, .. }) |
Transaction::Eip2930(TxEip2930 { gas_limit, .. }) |
Transaction::Eip1559(TxEip1559 { gas_limit, .. }) |
Transaction::Eip4844(TxEip4844 { gas_limit, .. }) => *gas_limit,
Self::Legacy(TxLegacy { gas_limit, .. }) |
Self::Eip2930(TxEip2930 { gas_limit, .. }) |
Self::Eip1559(TxEip1559 { gas_limit, .. }) |
Self::Eip4844(TxEip4844 { gas_limit, .. }) => *gas_limit,
#[cfg(feature = "optimism")]
Transaction::Deposit(TxDeposit { gas_limit, .. }) => *gas_limit,
Self::Deposit(TxDeposit { gas_limit, .. }) => *gas_limit,
}
}
/// Returns true if the tx supports dynamic fees
pub fn is_dynamic_fee(&self) -> bool {
match self {
Transaction::Legacy(_) | Transaction::Eip2930(_) => false,
Transaction::Eip1559(_) | Transaction::Eip4844(_) => true,
Self::Legacy(_) | Self::Eip2930(_) => false,
Self::Eip1559(_) | Self::Eip4844(_) => true,
#[cfg(feature = "optimism")]
Transaction::Deposit(_) => false,
Self::Deposit(_) => false,
}
}
@ -273,14 +273,14 @@ impl Transaction {
/// This is also commonly referred to as the "Gas Fee Cap" (`GasFeeCap`).
pub fn max_fee_per_gas(&self) -> u128 {
match self {
Transaction::Legacy(TxLegacy { gas_price, .. }) |
Transaction::Eip2930(TxEip2930 { gas_price, .. }) => *gas_price,
Transaction::Eip1559(TxEip1559 { max_fee_per_gas, .. }) |
Transaction::Eip4844(TxEip4844 { max_fee_per_gas, .. }) => *max_fee_per_gas,
Self::Legacy(TxLegacy { gas_price, .. }) |
Self::Eip2930(TxEip2930 { gas_price, .. }) => *gas_price,
Self::Eip1559(TxEip1559 { max_fee_per_gas, .. }) |
Self::Eip4844(TxEip4844 { max_fee_per_gas, .. }) => *max_fee_per_gas,
// Deposit transactions buy their L2 gas on L1 and, as such, the L2 gas is not
// refundable.
#[cfg(feature = "optimism")]
Transaction::Deposit(_) => 0,
Self::Deposit(_) => 0,
}
}
@ -290,13 +290,13 @@ impl Transaction {
/// This is also commonly referred to as the "Gas Tip Cap" (`GasTipCap`).
pub fn max_priority_fee_per_gas(&self) -> Option<u128> {
match self {
Transaction::Legacy(_) | Transaction::Eip2930(_) => None,
Transaction::Eip1559(TxEip1559 { max_priority_fee_per_gas, .. }) |
Transaction::Eip4844(TxEip4844 { max_priority_fee_per_gas, .. }) => {
Self::Legacy(_) | Self::Eip2930(_) => None,
Self::Eip1559(TxEip1559 { max_priority_fee_per_gas, .. }) |
Self::Eip4844(TxEip4844 { max_priority_fee_per_gas, .. }) => {
Some(*max_priority_fee_per_gas)
}
#[cfg(feature = "optimism")]
Transaction::Deposit(_) => None,
Self::Deposit(_) => None,
}
}
@ -306,12 +306,12 @@ impl Transaction {
/// This is also commonly referred to as the "blob versioned hashes" (`BlobVersionedHashes`).
pub fn blob_versioned_hashes(&self) -> Option<Vec<B256>> {
match self {
Transaction::Legacy(_) | Transaction::Eip2930(_) | Transaction::Eip1559(_) => None,
Transaction::Eip4844(TxEip4844 { blob_versioned_hashes, .. }) => {
Self::Legacy(_) | Self::Eip2930(_) | Self::Eip1559(_) => None,
Self::Eip4844(TxEip4844 { blob_versioned_hashes, .. }) => {
Some(blob_versioned_hashes.to_vec())
}
#[cfg(feature = "optimism")]
Transaction::Deposit(_) => None,
Self::Deposit(_) => None,
}
}
@ -322,9 +322,7 @@ impl Transaction {
/// This is also commonly referred to as the "Blob Gas Fee Cap" (`BlobGasFeeCap`).
pub fn max_fee_per_blob_gas(&self) -> Option<u128> {
match self {
Transaction::Eip4844(TxEip4844 { max_fee_per_blob_gas, .. }) => {
Some(*max_fee_per_blob_gas)
}
Self::Eip4844(TxEip4844 { max_fee_per_blob_gas, .. }) => Some(*max_fee_per_blob_gas),
_ => None,
}
}
@ -347,14 +345,12 @@ impl Transaction {
/// non-EIP-1559 transactions.
pub fn priority_fee_or_price(&self) -> u128 {
match self {
Transaction::Legacy(TxLegacy { gas_price, .. }) |
Transaction::Eip2930(TxEip2930 { gas_price, .. }) => *gas_price,
Transaction::Eip1559(TxEip1559 { max_priority_fee_per_gas, .. }) |
Transaction::Eip4844(TxEip4844 { max_priority_fee_per_gas, .. }) => {
*max_priority_fee_per_gas
}
Self::Legacy(TxLegacy { gas_price, .. }) |
Self::Eip2930(TxEip2930 { gas_price, .. }) => *gas_price,
Self::Eip1559(TxEip1559 { max_priority_fee_per_gas, .. }) |
Self::Eip4844(TxEip4844 { max_priority_fee_per_gas, .. }) => *max_priority_fee_per_gas,
#[cfg(feature = "optimism")]
Transaction::Deposit(_) => 0,
Self::Deposit(_) => 0,
}
}
@ -363,12 +359,12 @@ impl Transaction {
/// If the transaction is a legacy or EIP2930 transaction, the gas price is returned.
pub fn effective_gas_price(&self, base_fee: Option<u64>) -> u128 {
match self {
Transaction::Legacy(tx) => tx.gas_price,
Transaction::Eip2930(tx) => tx.gas_price,
Transaction::Eip1559(dynamic_tx) => dynamic_tx.effective_gas_price(base_fee),
Transaction::Eip4844(dynamic_tx) => dynamic_tx.effective_gas_price(base_fee),
Self::Legacy(tx) => tx.gas_price,
Self::Eip2930(tx) => tx.gas_price,
Self::Eip1559(dynamic_tx) => dynamic_tx.effective_gas_price(base_fee),
Self::Eip4844(dynamic_tx) => dynamic_tx.effective_gas_price(base_fee),
#[cfg(feature = "optimism")]
Transaction::Deposit(_) => 0,
Self::Deposit(_) => 0,
}
}
@ -406,12 +402,12 @@ impl Transaction {
/// Get the transaction's input field.
pub fn input(&self) -> &Bytes {
match self {
Transaction::Legacy(TxLegacy { input, .. }) |
Transaction::Eip2930(TxEip2930 { input, .. }) |
Transaction::Eip1559(TxEip1559 { input, .. }) |
Transaction::Eip4844(TxEip4844 { input, .. }) => input,
Self::Legacy(TxLegacy { input, .. }) |
Self::Eip2930(TxEip2930 { input, .. }) |
Self::Eip1559(TxEip1559 { input, .. }) |
Self::Eip4844(TxEip4844 { input, .. }) => input,
#[cfg(feature = "optimism")]
Transaction::Deposit(TxDeposit { input, .. }) => input,
Self::Deposit(TxDeposit { input, .. }) => input,
}
}
@ -420,7 +416,7 @@ impl Transaction {
#[cfg(feature = "optimism")]
pub fn source_hash(&self) -> Option<B256> {
match self {
Transaction::Deposit(TxDeposit { source_hash, .. }) => Some(*source_hash),
Self::Deposit(TxDeposit { source_hash, .. }) => Some(*source_hash),
_ => None,
}
}
@ -430,7 +426,7 @@ impl Transaction {
#[cfg(feature = "optimism")]
pub fn mint(&self) -> Option<u128> {
match self {
Transaction::Deposit(TxDeposit { mint, .. }) => *mint,
Self::Deposit(TxDeposit { mint, .. }) => *mint,
_ => None,
}
}
@ -440,7 +436,7 @@ impl Transaction {
#[cfg(feature = "optimism")]
pub fn is_system_transaction(&self) -> bool {
match self {
Transaction::Deposit(TxDeposit { is_system_transaction, .. }) => *is_system_transaction,
Self::Deposit(TxDeposit { is_system_transaction, .. }) => *is_system_transaction,
_ => false,
}
}
@ -448,7 +444,7 @@ impl Transaction {
/// Returns whether or not the transaction is an Optimism Deposited transaction.
#[cfg(feature = "optimism")]
pub fn is_deposit(&self) -> bool {
matches!(self, Transaction::Deposit(_))
matches!(self, Self::Deposit(_))
}
/// This encodes the transaction _without_ the signature, and is only suitable for creating a
@ -466,57 +462,55 @@ impl Transaction {
with_header: bool,
) {
match self {
Transaction::Legacy(legacy_tx) => {
Self::Legacy(legacy_tx) => {
// do nothing w/ with_header
legacy_tx.encode_with_signature(signature, out)
}
Transaction::Eip2930(access_list_tx) => {
Self::Eip2930(access_list_tx) => {
access_list_tx.encode_with_signature(signature, out, with_header)
}
Transaction::Eip1559(dynamic_fee_tx) => {
Self::Eip1559(dynamic_fee_tx) => {
dynamic_fee_tx.encode_with_signature(signature, out, with_header)
}
Transaction::Eip4844(blob_tx) => {
blob_tx.encode_with_signature(signature, out, with_header)
}
Self::Eip4844(blob_tx) => blob_tx.encode_with_signature(signature, out, with_header),
#[cfg(feature = "optimism")]
Transaction::Deposit(deposit_tx) => deposit_tx.encode(out, with_header),
Self::Deposit(deposit_tx) => deposit_tx.encode(out, with_header),
}
}
/// This sets the transaction's nonce.
pub fn set_nonce(&mut self, nonce: u64) {
match self {
Transaction::Legacy(tx) => tx.nonce = nonce,
Transaction::Eip2930(tx) => tx.nonce = nonce,
Transaction::Eip1559(tx) => tx.nonce = nonce,
Transaction::Eip4844(tx) => tx.nonce = nonce,
Self::Legacy(tx) => tx.nonce = nonce,
Self::Eip2930(tx) => tx.nonce = nonce,
Self::Eip1559(tx) => tx.nonce = nonce,
Self::Eip4844(tx) => tx.nonce = nonce,
#[cfg(feature = "optimism")]
Transaction::Deposit(_) => { /* noop */ }
Self::Deposit(_) => { /* noop */ }
}
}
/// This sets the transaction's value.
pub fn set_value(&mut self, value: U256) {
match self {
Transaction::Legacy(tx) => tx.value = value,
Transaction::Eip2930(tx) => tx.value = value,
Transaction::Eip1559(tx) => tx.value = value,
Transaction::Eip4844(tx) => tx.value = value,
Self::Legacy(tx) => tx.value = value,
Self::Eip2930(tx) => tx.value = value,
Self::Eip1559(tx) => tx.value = value,
Self::Eip4844(tx) => tx.value = value,
#[cfg(feature = "optimism")]
Transaction::Deposit(tx) => tx.value = value,
Self::Deposit(tx) => tx.value = value,
}
}
/// This sets the transaction's input field.
pub fn set_input(&mut self, input: Bytes) {
match self {
Transaction::Legacy(tx) => tx.input = input,
Transaction::Eip2930(tx) => tx.input = input,
Transaction::Eip1559(tx) => tx.input = input,
Transaction::Eip4844(tx) => tx.input = input,
Self::Legacy(tx) => tx.input = input,
Self::Eip2930(tx) => tx.input = input,
Self::Eip1559(tx) => tx.input = input,
Self::Eip4844(tx) => tx.input = input,
#[cfg(feature = "optimism")]
Transaction::Deposit(tx) => tx.input = input,
Self::Deposit(tx) => tx.input = input,
}
}
@ -524,43 +518,43 @@ impl Transaction {
#[inline]
pub fn size(&self) -> usize {
match self {
Transaction::Legacy(tx) => tx.size(),
Transaction::Eip2930(tx) => tx.size(),
Transaction::Eip1559(tx) => tx.size(),
Transaction::Eip4844(tx) => tx.size(),
Self::Legacy(tx) => tx.size(),
Self::Eip2930(tx) => tx.size(),
Self::Eip1559(tx) => tx.size(),
Self::Eip4844(tx) => tx.size(),
#[cfg(feature = "optimism")]
Transaction::Deposit(tx) => tx.size(),
Self::Deposit(tx) => tx.size(),
}
}
/// Returns true if the transaction is a legacy transaction.
#[inline]
pub const fn is_legacy(&self) -> bool {
matches!(self, Transaction::Legacy(_))
matches!(self, Self::Legacy(_))
}
/// Returns true if the transaction is an EIP-2930 transaction.
#[inline]
pub const fn is_eip2930(&self) -> bool {
matches!(self, Transaction::Eip2930(_))
matches!(self, Self::Eip2930(_))
}
/// Returns true if the transaction is an EIP-1559 transaction.
#[inline]
pub const fn is_eip1559(&self) -> bool {
matches!(self, Transaction::Eip1559(_))
matches!(self, Self::Eip1559(_))
}
/// Returns true if the transaction is an EIP-4844 transaction.
#[inline]
pub const fn is_eip4844(&self) -> bool {
matches!(self, Transaction::Eip4844(_))
matches!(self, Self::Eip4844(_))
}
/// Returns the [TxLegacy] variant if the transaction is a legacy transaction.
pub fn as_legacy(&self) -> Option<&TxLegacy> {
match self {
Transaction::Legacy(tx) => Some(tx),
Self::Legacy(tx) => Some(tx),
_ => None,
}
}
@ -568,7 +562,7 @@ impl Transaction {
/// Returns the [TxEip2930] variant if the transaction is an EIP-2930 transaction.
pub fn as_eip2930(&self) -> Option<&TxEip2930> {
match self {
Transaction::Eip2930(tx) => Some(tx),
Self::Eip2930(tx) => Some(tx),
_ => None,
}
}
@ -576,7 +570,7 @@ impl Transaction {
/// Returns the [TxEip1559] variant if the transaction is an EIP-1559 transaction.
pub fn as_eip1559(&self) -> Option<&TxEip1559> {
match self {
Transaction::Eip1559(tx) => Some(tx),
Self::Eip1559(tx) => Some(tx),
_ => None,
}
}
@ -584,7 +578,7 @@ impl Transaction {
/// Returns the [TxEip4844] variant if the transaction is an EIP-4844 transaction.
pub fn as_eip4844(&self) -> Option<&TxEip4844> {
match self {
Transaction::Eip4844(tx) => Some(tx),
Self::Eip4844(tx) => Some(tx),
_ => None,
}
}
@ -592,25 +586,25 @@ impl Transaction {
impl From<TxLegacy> for Transaction {
fn from(tx: TxLegacy) -> Self {
Transaction::Legacy(tx)
Self::Legacy(tx)
}
}
impl From<TxEip2930> for Transaction {
fn from(tx: TxEip2930) -> Self {
Transaction::Eip2930(tx)
Self::Eip2930(tx)
}
}
impl From<TxEip1559> for Transaction {
fn from(tx: TxEip1559) -> Self {
Transaction::Eip1559(tx)
Self::Eip1559(tx)
}
}
impl From<TxEip4844> for Transaction {
fn from(tx: TxEip4844) -> Self {
Transaction::Eip4844(tx)
Self::Eip4844(tx)
}
}
@ -623,20 +617,20 @@ impl Compact for Transaction {
{
let identifier = self.tx_type().to_compact(buf);
match self {
Transaction::Legacy(tx) => {
Self::Legacy(tx) => {
tx.to_compact(buf);
}
Transaction::Eip2930(tx) => {
Self::Eip2930(tx) => {
tx.to_compact(buf);
}
Transaction::Eip1559(tx) => {
Self::Eip1559(tx) => {
tx.to_compact(buf);
}
Transaction::Eip4844(tx) => {
Self::Eip4844(tx) => {
tx.to_compact(buf);
}
#[cfg(feature = "optimism")]
Transaction::Deposit(tx) => {
Self::Deposit(tx) => {
tx.to_compact(buf);
}
}
@ -655,15 +649,15 @@ impl Compact for Transaction {
match identifier {
0 => {
let (tx, buf) = TxLegacy::from_compact(buf, buf.len());
(Transaction::Legacy(tx), buf)
(Self::Legacy(tx), buf)
}
1 => {
let (tx, buf) = TxEip2930::from_compact(buf, buf.len());
(Transaction::Eip2930(tx), buf)
(Self::Eip2930(tx), buf)
}
2 => {
let (tx, buf) = TxEip1559::from_compact(buf, buf.len());
(Transaction::Eip1559(tx), buf)
(Self::Eip1559(tx), buf)
}
3 => {
// An identifier of 3 indicates that the transaction type did not fit into
@ -675,12 +669,12 @@ impl Compact for Transaction {
match identifier {
3 => {
let (tx, buf) = TxEip4844::from_compact(buf, buf.len());
(Transaction::Eip4844(tx), buf)
(Self::Eip4844(tx), buf)
}
#[cfg(feature = "optimism")]
126 => {
let (tx, buf) = TxDeposit::from_compact(buf, buf.len());
(Transaction::Deposit(tx), buf)
(Self::Deposit(tx), buf)
}
_ => unreachable!("Junk data in database: unknown Transaction variant"),
}
@ -701,20 +695,20 @@ impl Encodable for Transaction {
/// hash intended for signing.
fn encode(&self, out: &mut dyn bytes::BufMut) {
match self {
Transaction::Legacy(legacy_tx) => {
Self::Legacy(legacy_tx) => {
legacy_tx.encode_for_signing(out);
}
Transaction::Eip2930(access_list_tx) => {
Self::Eip2930(access_list_tx) => {
access_list_tx.encode_for_signing(out);
}
Transaction::Eip1559(dynamic_fee_tx) => {
Self::Eip1559(dynamic_fee_tx) => {
dynamic_fee_tx.encode_for_signing(out);
}
Transaction::Eip4844(blob_tx) => {
Self::Eip4844(blob_tx) => {
blob_tx.encode_for_signing(out);
}
#[cfg(feature = "optimism")]
Transaction::Deposit(deposit_tx) => {
Self::Deposit(deposit_tx) => {
deposit_tx.encode(out, true);
}
}
@ -722,12 +716,12 @@ impl Encodable for Transaction {
fn length(&self) -> usize {
match self {
Transaction::Legacy(legacy_tx) => legacy_tx.payload_len_for_signature(),
Transaction::Eip2930(access_list_tx) => access_list_tx.payload_len_for_signature(),
Transaction::Eip1559(dynamic_fee_tx) => dynamic_fee_tx.payload_len_for_signature(),
Transaction::Eip4844(blob_tx) => blob_tx.payload_len_for_signature(),
Self::Legacy(legacy_tx) => legacy_tx.payload_len_for_signature(),
Self::Eip2930(access_list_tx) => access_list_tx.payload_len_for_signature(),
Self::Eip1559(dynamic_fee_tx) => dynamic_fee_tx.payload_len_for_signature(),
Self::Eip4844(blob_tx) => blob_tx.payload_len_for_signature(),
#[cfg(feature = "optimism")]
Transaction::Deposit(deposit_tx) => deposit_tx.payload_len(),
Self::Deposit(deposit_tx) => deposit_tx.payload_len(),
}
}
}
@ -892,7 +886,7 @@ impl Compact for TransactionSignedNoHash {
Transaction::from_compact(buf, transaction_type)
};
(TransactionSignedNoHash { signature, transaction }, buf)
(Self { signature, transaction }, buf)
}
}
@ -962,7 +956,7 @@ impl From<TransactionSignedNoHash> for TransactionSigned {
impl From<TransactionSigned> for TransactionSignedNoHash {
fn from(tx: TransactionSigned) -> Self {
TransactionSignedNoHash { signature: tx.signature, transaction: tx.transaction }
Self { signature: tx.signature, transaction: tx.transaction }
}
}
@ -1245,11 +1239,9 @@ impl TransactionSigned {
/// This expects `rlp(legacy_tx)`
// TODO: make buf advancement semantics consistent with `decode_enveloped_typed_transaction`,
// so decoding methods do not need to manually advance the buffer
pub fn decode_rlp_legacy_transaction(data: &mut &[u8]) -> alloy_rlp::Result<TransactionSigned> {
let (transaction, hash, signature) =
TransactionSigned::decode_rlp_legacy_transaction_tuple(data)?;
let signed =
TransactionSigned { transaction: Transaction::Legacy(transaction), hash, signature };
pub fn decode_rlp_legacy_transaction(data: &mut &[u8]) -> alloy_rlp::Result<Self> {
let (transaction, hash, signature) = Self::decode_rlp_legacy_transaction_tuple(data)?;
let signed = Self { transaction: Transaction::Legacy(transaction), hash, signature };
Ok(signed)
}
@ -1263,9 +1255,7 @@ impl TransactionSigned {
/// byte indicating the transaction type.
///
/// CAUTION: this expects that `data` is `tx-type || rlp(tx-data)`
pub fn decode_enveloped_typed_transaction(
data: &mut &[u8],
) -> alloy_rlp::Result<TransactionSigned> {
pub fn decode_enveloped_typed_transaction(data: &mut &[u8]) -> alloy_rlp::Result<Self> {
// keep this around so we can use it to calculate the hash
let original_encoding_without_header = *data;
@ -1313,7 +1303,7 @@ impl TransactionSigned {
}
let hash = keccak256(&original_encoding_without_header[..tx_length]);
let signed = TransactionSigned { transaction, hash, signature };
let signed = Self { transaction, hash, signature };
Ok(signed)
}
@ -1342,9 +1332,9 @@ impl TransactionSigned {
// Check if the tx is a list
let output_data = if input_data[0] >= EMPTY_LIST_CODE {
// decode as legacy transaction
TransactionSigned::decode_rlp_legacy_transaction(input_data)?
Self::decode_rlp_legacy_transaction(input_data)?
} else {
TransactionSigned::decode_enveloped_typed_transaction(input_data)?
Self::decode_enveloped_typed_transaction(input_data)?
};
if !input_data.is_empty() {
@ -1437,7 +1427,7 @@ impl Decodable for TransactionSigned {
// if the transaction is encoded as a string then it is a typed transaction
if !header.list {
let tx = TransactionSigned::decode_enveloped_typed_transaction(buf)?;
let tx = Self::decode_enveloped_typed_transaction(buf)?;
let bytes_consumed = remaining_len - buf.len();
// because Header::decode works for single bytes (including the tx type), returning a
@ -1449,7 +1439,7 @@ impl Decodable for TransactionSigned {
Ok(tx)
} else {
let tx = TransactionSigned::decode_rlp_legacy_transaction(&mut original_encoding)?;
let tx = Self::decode_rlp_legacy_transaction(&mut original_encoding)?;
// advance the buffer based on how far `decode_rlp_legacy_transaction` advanced the
// buffer
@ -1483,15 +1473,14 @@ impl proptest::arbitrary::Arbitrary for TransactionSigned {
if tx_eip_4844.to != Address::default() { Some(()) } else { None };
}
let mut tx =
TransactionSigned { hash: Default::default(), signature: sig, transaction };
let mut tx = Self { hash: Default::default(), signature: sig, transaction };
tx.hash = tx.recalculate_hash();
tx
})
.boxed()
}
type Strategy = proptest::strategy::BoxedStrategy<TransactionSigned>;
type Strategy = proptest::strategy::BoxedStrategy<Self>;
}
#[cfg(any(test, feature = "arbitrary"))]
@ -1512,7 +1501,7 @@ impl<'a> arbitrary::Arbitrary<'a> for TransactionSigned {
signature
};
Ok(TransactionSigned::from_transaction_and_signature(transaction, signature))
Ok(Self::from_transaction_and_signature(transaction, signature))
}
}
@ -1575,7 +1564,7 @@ impl Decodable for TransactionSignedEcRecovered {
let signer = signed_transaction
.recover_signer()
.ok_or(RlpError::Custom("Unable to recover decoded transaction signer."))?;
Ok(TransactionSignedEcRecovered { signer, signed_transaction })
Ok(Self { signer, signed_transaction })
}
}

View File

@ -58,13 +58,13 @@ impl PooledTransactionsElement {
pub fn try_from_broadcast(tx: TransactionSigned) -> Result<Self, TransactionSigned> {
match tx {
TransactionSigned { transaction: Transaction::Legacy(tx), signature, hash } => {
Ok(PooledTransactionsElement::Legacy { transaction: tx, signature, hash })
Ok(Self::Legacy { transaction: tx, signature, hash })
}
TransactionSigned { transaction: Transaction::Eip2930(tx), signature, hash } => {
Ok(PooledTransactionsElement::Eip2930 { transaction: tx, signature, hash })
Ok(Self::Eip2930 { transaction: tx, signature, hash })
}
TransactionSigned { transaction: Transaction::Eip1559(tx), signature, hash } => {
Ok(PooledTransactionsElement::Eip1559 { transaction: tx, signature, hash })
Ok(Self::Eip1559 { transaction: tx, signature, hash })
}
// Not supported because missing blob sidecar
tx @ TransactionSigned { transaction: Transaction::Eip4844(_), .. } => Err(tx),
@ -87,12 +87,7 @@ impl PooledTransactionsElement {
// If the transaction is an EIP-4844 transaction...
TransactionSigned { transaction: Transaction::Eip4844(tx), signature, hash } => {
// Construct a `PooledTransactionsElement::BlobTransaction` with provided sidecar.
PooledTransactionsElement::BlobTransaction(BlobTransaction {
transaction: tx,
signature,
hash,
sidecar,
})
Self::BlobTransaction(BlobTransaction { transaction: tx, signature, hash, sidecar })
}
// If the transaction is not EIP-4844, return an error with the original
// transaction.
@ -114,10 +109,10 @@ impl PooledTransactionsElement {
/// Reference to transaction hash. Used to identify transaction.
pub fn hash(&self) -> &TxHash {
match self {
PooledTransactionsElement::Legacy { hash, .. } |
PooledTransactionsElement::Eip2930 { hash, .. } |
PooledTransactionsElement::Eip1559 { hash, .. } => hash,
PooledTransactionsElement::BlobTransaction(tx) => &tx.hash,
Self::Legacy { hash, .. } | Self::Eip2930 { hash, .. } | Self::Eip1559 { hash, .. } => {
hash
}
Self::BlobTransaction(tx) => &tx.hash,
}
}
@ -215,7 +210,7 @@ impl PooledTransactionsElement {
// Now, we decode the inner blob transaction:
// `rlp([[chain_id, nonce, ...], blobs, commitments, proofs])`
let blob_tx = BlobTransaction::decode_inner(data)?;
Ok(PooledTransactionsElement::BlobTransaction(blob_tx))
Ok(Self::BlobTransaction(blob_tx))
} else {
// DO NOT advance the buffer for the type, since we want the enveloped decoding to
// decode it again and advance the buffer on its own.
@ -230,12 +225,12 @@ impl PooledTransactionsElement {
Transaction::Eip4844(_) => Err(RlpError::Custom(
"EIP-4844 transactions can only be decoded with transaction type 0x03",
)),
Transaction::Eip2930(tx) => Ok(PooledTransactionsElement::Eip2930 {
Transaction::Eip2930(tx) => Ok(Self::Eip2930 {
transaction: tx,
signature: typed_tx.signature,
hash: typed_tx.hash,
}),
Transaction::Eip1559(tx) => Ok(PooledTransactionsElement::Eip1559 {
Transaction::Eip1559(tx) => Ok(Self::Eip1559 {
transaction: tx,
signature: typed_tx.signature,
hash: typed_tx.hash,
@ -544,7 +539,7 @@ impl Decodable for PooledTransactionsElement {
return Err(RlpError::UnexpectedLength)
}
Ok(PooledTransactionsElement::BlobTransaction(blob_tx))
Ok(Self::BlobTransaction(blob_tx))
} else {
// DO NOT advance the buffer for the type, since we want the enveloped decoding to
// decode it again and advance the buffer on its own.
@ -565,12 +560,12 @@ impl Decodable for PooledTransactionsElement {
Transaction::Eip4844(_) => Err(RlpError::Custom(
"EIP-4844 transactions can only be decoded with transaction type 0x03",
)),
Transaction::Eip2930(tx) => Ok(PooledTransactionsElement::Eip2930 {
Transaction::Eip2930(tx) => Ok(Self::Eip2930 {
transaction: tx,
signature: typed_tx.signature,
hash: typed_tx.hash,
}),
Transaction::Eip1559(tx) => Ok(PooledTransactionsElement::Eip1559 {
Transaction::Eip1559(tx) => Ok(Self::Eip1559 {
transaction: tx,
signature: typed_tx.signature,
hash: typed_tx.hash,
@ -587,8 +582,7 @@ impl TryFrom<TransactionSigned> for PooledTransactionsElement {
type Error = TransactionConversionError;
fn try_from(tx: TransactionSigned) -> Result<Self, Self::Error> {
PooledTransactionsElement::try_from_broadcast(tx)
.map_err(|_| TransactionConversionError::UnsupportedForP2P)
Self::try_from_broadcast(tx).map_err(|_| TransactionConversionError::UnsupportedForP2P)
}
}
@ -605,11 +599,11 @@ impl<'a> arbitrary::Arbitrary<'a> for PooledTransactionsElement {
// Attempt to create a `TransactionSigned` with arbitrary data.
let tx_signed = TransactionSigned::arbitrary(u)?;
// Attempt to create a `PooledTransactionsElement` with arbitrary data, handling the Result.
match PooledTransactionsElement::try_from(tx_signed) {
Ok(PooledTransactionsElement::BlobTransaction(mut tx)) => {
match Self::try_from(tx_signed) {
Ok(Self::BlobTransaction(mut tx)) => {
// Successfully converted to a BlobTransaction, now generate a sidecar.
tx.sidecar = crate::BlobTransactionSidecar::arbitrary(u)?;
Ok(PooledTransactionsElement::BlobTransaction(tx))
Ok(Self::BlobTransaction(tx))
}
Ok(tx) => Ok(tx), // Successfully converted, but not a BlobTransaction.
Err(_) => Err(arbitrary::Error::IncorrectFormat), /* Conversion failed, return an
@ -626,13 +620,13 @@ impl proptest::arbitrary::Arbitrary for PooledTransactionsElement {
any::<(TransactionSigned, crate::BlobTransactionSidecar)>()
.prop_map(move |(transaction, sidecar)| {
match PooledTransactionsElement::try_from(transaction) {
Ok(PooledTransactionsElement::BlobTransaction(mut tx)) => {
match Self::try_from(transaction) {
Ok(Self::BlobTransaction(mut tx)) => {
tx.sidecar = sidecar;
PooledTransactionsElement::BlobTransaction(tx)
Self::BlobTransaction(tx)
}
Ok(tx) => tx,
Err(_) => PooledTransactionsElement::Eip1559 {
Err(_) => Self::Eip1559 {
transaction: Default::default(),
signature: Default::default(),
hash: Default::default(),
@ -642,7 +636,7 @@ impl proptest::arbitrary::Arbitrary for PooledTransactionsElement {
.boxed()
}
type Strategy = proptest::strategy::BoxedStrategy<PooledTransactionsElement>;
type Strategy = proptest::strategy::BoxedStrategy<Self>;
}
/// A signed pooled transaction with recovered signer.

View File

@ -49,15 +49,15 @@ pub enum TxType {
impl TxType {
/// The max type reserved by an EIP.
pub const MAX_RESERVED_EIP: TxType = Self::Eip4844;
pub const MAX_RESERVED_EIP: Self = Self::Eip4844;
/// Check if the transaction type has an access list.
pub const fn has_access_list(&self) -> bool {
match self {
TxType::Legacy => false,
TxType::Eip2930 | TxType::Eip1559 | TxType::Eip4844 => true,
Self::Legacy => false,
Self::Eip2930 | Self::Eip1559 | Self::Eip4844 => true,
#[cfg(feature = "optimism")]
TxType::Deposit => false,
Self::Deposit => false,
}
}
}
@ -77,7 +77,7 @@ impl From<TxType> for u8 {
impl From<TxType> for U8 {
fn from(value: TxType) -> Self {
U8::from(u8::from(value))
Self::from(u8::from(value))
}
}
@ -86,18 +86,18 @@ impl TryFrom<u8> for TxType {
fn try_from(value: u8) -> Result<Self, Self::Error> {
#[cfg(feature = "optimism")]
if value == TxType::Deposit {
return Ok(TxType::Deposit)
if value == Self::Deposit {
return Ok(Self::Deposit)
}
if value == TxType::Legacy {
return Ok(TxType::Legacy)
} else if value == TxType::Eip2930 {
return Ok(TxType::Eip2930)
} else if value == TxType::Eip1559 {
return Ok(TxType::Eip1559)
} else if value == TxType::Eip4844 {
return Ok(TxType::Eip4844)
if value == Self::Legacy {
return Ok(Self::Legacy)
} else if value == Self::Eip2930 {
return Ok(Self::Eip2930)
} else if value == Self::Eip1559 {
return Ok(Self::Eip1559)
} else if value == Self::Eip4844 {
return Ok(Self::Eip4844)
}
Err("invalid tx type")
@ -127,10 +127,10 @@ impl Compact for TxType {
B: bytes::BufMut + AsMut<[u8]>,
{
match self {
TxType::Legacy => 0,
TxType::Eip2930 => 1,
TxType::Eip1559 => 2,
TxType::Eip4844 => {
Self::Legacy => 0,
Self::Eip2930 => 1,
Self::Eip1559 => 2,
Self::Eip4844 => {
// Write the full transaction type to the buffer when encoding > 3.
// This allows compat decoding the [TyType] from a single byte as
// opposed to 2 bits for the backwards-compatible encoding.
@ -138,7 +138,7 @@ impl Compact for TxType {
3
}
#[cfg(feature = "optimism")]
TxType::Deposit => {
Self::Deposit => {
buf.put_u8(self as u8);
3
}
@ -151,15 +151,15 @@ impl Compact for TxType {
fn from_compact(mut buf: &[u8], identifier: usize) -> (Self, &[u8]) {
(
match identifier {
0 => TxType::Legacy,
1 => TxType::Eip2930,
2 => TxType::Eip1559,
0 => Self::Legacy,
1 => Self::Eip2930,
2 => Self::Eip1559,
3 => {
let extended_identifier = buf.get_u8();
match extended_identifier {
EIP4844_TX_TYPE_ID => TxType::Eip4844,
EIP4844_TX_TYPE_ID => Self::Eip4844,
#[cfg(feature = "optimism")]
DEPOSIT_TX_TYPE_ID => TxType::Deposit,
DEPOSIT_TX_TYPE_ID => Self::Deposit,
_ => panic!("Unsupported TxType identifier: {extended_identifier}"),
}
}
@ -178,7 +178,7 @@ impl PartialEq<u8> for TxType {
impl PartialEq<TxType> for u8 {
fn eq(&self, other: &TxType) -> bool {
*self == *other as u8
*self == *other as Self
}
}
@ -196,7 +196,7 @@ impl Decodable for TxType {
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
let ty = u8::decode(buf)?;
TxType::try_from(ty).map_err(alloy_rlp::Error::Custom)
Self::try_from(ty).map_err(alloy_rlp::Error::Custom)
}
}

View File

@ -26,18 +26,18 @@ impl TransactionSignedVariant {
/// Returns the raw transaction object
pub fn as_raw(&self) -> &Transaction {
match self {
TransactionSignedVariant::SignedNoHash(tx) => &tx.transaction,
TransactionSignedVariant::Signed(tx) => &tx.transaction,
TransactionSignedVariant::SignedEcRecovered(tx) => &tx.signed_transaction.transaction,
Self::SignedNoHash(tx) => &tx.transaction,
Self::Signed(tx) => &tx.transaction,
Self::SignedEcRecovered(tx) => &tx.signed_transaction.transaction,
}
}
/// Returns the hash of the transaction
pub fn hash(&self) -> B256 {
match self {
TransactionSignedVariant::SignedNoHash(tx) => tx.hash(),
TransactionSignedVariant::Signed(tx) => tx.hash,
TransactionSignedVariant::SignedEcRecovered(tx) => tx.hash,
Self::SignedNoHash(tx) => tx.hash(),
Self::Signed(tx) => tx.hash,
Self::SignedEcRecovered(tx) => tx.hash,
}
}
@ -46,9 +46,9 @@ impl TransactionSignedVariant {
/// If the transaction is of not of [TransactionSignedEcRecovered] it will be recovered.
pub fn signer(&self) -> Option<Address> {
match self {
TransactionSignedVariant::SignedNoHash(tx) => tx.recover_signer(),
TransactionSignedVariant::Signed(tx) => tx.recover_signer(),
TransactionSignedVariant::SignedEcRecovered(tx) => Some(tx.signer),
Self::SignedNoHash(tx) => tx.recover_signer(),
Self::Signed(tx) => tx.recover_signer(),
Self::SignedEcRecovered(tx) => Some(tx.signer),
}
}
@ -56,7 +56,7 @@ impl TransactionSignedVariant {
/// else None
pub fn as_signed(&self) -> Option<&TransactionSigned> {
match self {
TransactionSignedVariant::Signed(tx) => Some(tx),
Self::Signed(tx) => Some(tx),
_ => None,
}
}
@ -65,41 +65,41 @@ impl TransactionSignedVariant {
/// else None
pub fn as_signed_ec_recovered(&self) -> Option<&TransactionSignedEcRecovered> {
match self {
TransactionSignedVariant::SignedEcRecovered(tx) => Some(tx),
Self::SignedEcRecovered(tx) => Some(tx),
_ => None,
}
}
/// Returns true if the transaction is of [TransactionSigned] variant
pub fn is_signed(&self) -> bool {
matches!(self, TransactionSignedVariant::Signed(_))
matches!(self, Self::Signed(_))
}
/// Returns true if the transaction is of [TransactionSignedNoHash] variant
pub fn is_signed_no_hash(&self) -> bool {
matches!(self, TransactionSignedVariant::SignedNoHash(_))
matches!(self, Self::SignedNoHash(_))
}
/// Returns true if the transaction is of [TransactionSignedEcRecovered] variant
pub fn is_signed_ec_recovered(&self) -> bool {
matches!(self, TransactionSignedVariant::SignedEcRecovered(_))
matches!(self, Self::SignedEcRecovered(_))
}
/// Consumes the [TransactionSignedVariant] and returns the consumed [Transaction]
pub fn into_raw(self) -> Transaction {
match self {
TransactionSignedVariant::SignedNoHash(tx) => tx.transaction,
TransactionSignedVariant::Signed(tx) => tx.transaction,
TransactionSignedVariant::SignedEcRecovered(tx) => tx.signed_transaction.transaction,
Self::SignedNoHash(tx) => tx.transaction,
Self::Signed(tx) => tx.transaction,
Self::SignedEcRecovered(tx) => tx.signed_transaction.transaction,
}
}
/// Consumes the [TransactionSignedVariant] and returns the consumed [TransactionSigned]
pub fn into_signed(self) -> TransactionSigned {
match self {
TransactionSignedVariant::SignedNoHash(tx) => tx.with_hash(),
TransactionSignedVariant::Signed(tx) => tx,
TransactionSignedVariant::SignedEcRecovered(tx) => tx.signed_transaction,
Self::SignedNoHash(tx) => tx.with_hash(),
Self::Signed(tx) => tx,
Self::SignedEcRecovered(tx) => tx.signed_transaction,
}
}
@ -123,28 +123,28 @@ impl TransactionSignedVariant {
self,
) -> Result<TransactionSignedEcRecovered, TransactionSigned> {
match self {
TransactionSignedVariant::SignedEcRecovered(tx) => Ok(tx),
TransactionSignedVariant::Signed(tx) => tx.try_into_ecrecovered(),
TransactionSignedVariant::SignedNoHash(tx) => tx.with_hash().try_into_ecrecovered(),
Self::SignedEcRecovered(tx) => Ok(tx),
Self::Signed(tx) => tx.try_into_ecrecovered(),
Self::SignedNoHash(tx) => tx.with_hash().try_into_ecrecovered(),
}
}
}
impl From<TransactionSignedNoHash> for TransactionSignedVariant {
fn from(tx: TransactionSignedNoHash) -> Self {
TransactionSignedVariant::SignedNoHash(tx)
Self::SignedNoHash(tx)
}
}
impl From<TransactionSigned> for TransactionSignedVariant {
fn from(tx: TransactionSigned) -> Self {
TransactionSignedVariant::Signed(tx)
Self::Signed(tx)
}
}
impl From<TransactionSignedEcRecovered> for TransactionSignedVariant {
fn from(tx: TransactionSignedEcRecovered) -> Self {
TransactionSignedVariant::SignedEcRecovered(tx)
Self::SignedEcRecovered(tx)
}
}

View File

@ -63,7 +63,7 @@ impl Compact for StoredSubNode {
None
};
(StoredSubNode { key, nibble, node }, buf)
(Self { key, nibble, node }, buf)
}
}