Skip to content

Commit

Permalink
Add AccessListResult type (EIP-2930) (#1110)
Browse files Browse the repository at this point in the history
* replace type, change type of result and add type in mod.rs

* add new methods

* add docs and impl

* docs linking

* clippy

* rustmft

---------

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
  • Loading branch information
mvares and mattsse authored Aug 1, 2024
1 parent bad667e commit 8e5a9fb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
40 changes: 37 additions & 3 deletions crates/eips/src/eip2930.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! [EIP-2930]: https://eips.ethereum.org/EIPS/eip-2930

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use alloc::{string::String, vec::Vec};

use alloy_primitives::{Address, B256, U256};
use alloy_rlp::{RlpDecodable, RlpDecodableWrapper, RlpEncodable, RlpEncodableWrapper};
Expand Down Expand Up @@ -141,6 +141,39 @@ pub struct AccessListWithGasUsed {
pub gas_used: U256,
}

/// `AccessListResult` for handling errors from `eth_createAccessList`
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct AccessListResult {
/// List with accounts accessed during transaction.
pub access_list: AccessList,
/// Estimated gas used with access list.
pub gas_used: U256,
/// Optional error message if the transaction failed.
#[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "Option::is_none"))]
pub error: Option<String>,
}

impl AccessListResult {
/// Ensures the result is OK, returning [`AccessListWithGasUsed`] if so, or an error message if
/// not.
pub fn ensure_ok(self) -> Result<AccessListWithGasUsed, String> {
match self.error {
Some(err) => Err(err),
None => {
Ok(AccessListWithGasUsed { access_list: self.access_list, gas_used: self.gas_used })
}
}
}

/// Checks if there is an error in the result.
#[inline]
pub const fn is_err(&self) -> bool {
self.error.is_some()
}
}

#[cfg(all(test, feature = "serde"))]
mod tests {
use super::*;
Expand All @@ -158,15 +191,16 @@ mod tests {

#[test]
fn access_list_with_gas_used() {
let list = AccessListWithGasUsed {
let list = AccessListResult {
access_list: AccessList(vec![
AccessListItem { address: Address::ZERO, storage_keys: vec![B256::ZERO] },
AccessListItem { address: Address::ZERO, storage_keys: vec![B256::ZERO] },
]),
gas_used: U256::from(100),
error: None,
};
let json = serde_json::to_string(&list).unwrap();
let list2 = serde_json::from_str::<AccessListWithGasUsed>(&json).unwrap();
let list2 = serde_json::from_str(&json).unwrap();
assert_eq!(list, list2);
}
}
6 changes: 3 additions & 3 deletions crates/provider/src/provider/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use alloy_primitives::{
};
use alloy_rpc_client::{ClientRef, PollerBuilder, RpcCall, WeakClient};
use alloy_rpc_types_eth::{
AccessListWithGasUsed, Block, BlockId, BlockNumberOrTag, EIP1186AccountProofResponse,
FeeHistory, Filter, FilterChanges, Log, SyncStatus,
AccessListResult, Block, BlockId, BlockNumberOrTag, EIP1186AccountProofResponse, FeeHistory,
Filter, FilterChanges, Log, SyncStatus,
};
use alloy_transport::{BoxTransport, Transport, TransportErrorKind, TransportResult};
use serde_json::value::RawValue;
Expand Down Expand Up @@ -163,7 +163,7 @@ pub trait Provider<T: Transport + Clone = BoxTransport, N: Network = Ethereum>:
fn create_access_list<'a>(
&self,
request: &'a N::TransactionRequest,
) -> RpcWithBlock<T, &'a N::TransactionRequest, AccessListWithGasUsed> {
) -> RpcWithBlock<T, &'a N::TransactionRequest, AccessListResult> {
RpcWithBlock::new(self.weak_client(), "eth_createAccessList", request)
}

Expand Down
2 changes: 1 addition & 1 deletion crates/rpc-types-eth/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use serde::{Deserialize, Serialize};

pub use alloy_consensus::BlobTransactionSidecar;
pub use alloy_eips::{
eip2930::{AccessList, AccessListItem, AccessListWithGasUsed},
eip2930::{AccessList, AccessListItem, AccessListResult},
eip7702::Authorization,
};

Expand Down

0 comments on commit 8e5a9fb

Please sign in to comment.