Skip to content

Commit

Permalink
Merge branch 'onbjerg/alloy-temp-provider-trait' of github.com:alloy-…
Browse files Browse the repository at this point in the history
…rs/alloy into onbjerg/alloy-temp-provider-trait
  • Loading branch information
onbjerg committed Nov 13, 2023
2 parents d1ba59d + 52291a7 commit 342e2d9
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 40 deletions.
6 changes: 3 additions & 3 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
* @danipopes @gakonst @prestwich
* @danipopes @gakonst @prestwich @evalir

crates/json-rpc @prestwich
crates/transports @prestwich
crates/transports @prestwich @evalir
crates/networks @prestwich
crates/providers @prestwich
crates/providers @prestwich @evalir
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/BUG-FORM.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ body:
- transports
- networks
- providers
- rpc-types
validations:
required: true
- type: input
Expand Down
2 changes: 1 addition & 1 deletion crates/providers/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ mod providers_test {
let anvil = Anvil::new().spawn();
let provider = Provider::try_from(&anvil.endpoint()).unwrap();
let num = provider.get_block_number().await.unwrap();
assert_eq!(U256::ZERO, num)
assert_eq!(U64::ZERO, num)
}

#[tokio::test]
Expand Down
19 changes: 12 additions & 7 deletions crates/rpc-types/src/eth/block.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Contains types that represent ethereum types when used in RPC
use crate::{Transaction, Withdrawal};
use alloy_primitives::{Address, BlockHash, BlockNumber, Bloom, Bytes, B256, B64, U256, U64};
use alloy_primitives::{
ruint::ParseError, Address, BlockHash, BlockNumber, Bloom, Bytes, B256, B64, U256, U64,
};
use alloy_rlp::{bytes, Decodable, Encodable, Error as RlpError};
use serde::{
de::{MapAccess, Visitor},
Expand Down Expand Up @@ -244,12 +246,12 @@ pub enum BlockNumberOrTag {
/// Pending block (not yet part of the blockchain)
Pending,
/// Block by number from canon chain
Number(u64),
Number(U64),
}

impl BlockNumberOrTag {
/// Returns the numeric block number if explicitly set
pub const fn as_number(&self) -> Option<u64> {
pub const fn as_number(&self) -> Option<U64> {
match *self {
BlockNumberOrTag::Number(num) => Some(num),
_ => None,
Expand Down Expand Up @@ -289,13 +291,13 @@ impl BlockNumberOrTag {

impl From<u64> for BlockNumberOrTag {
fn from(num: u64) -> Self {
BlockNumberOrTag::Number(num)
BlockNumberOrTag::Number(U64::from(num))
}
}

impl From<U64> for BlockNumberOrTag {
fn from(num: U64) -> Self {
num.into_limbs()[0].into()
BlockNumberOrTag::Number(num)
}
}

Expand Down Expand Up @@ -337,7 +339,7 @@ impl FromStr for BlockNumberOrTag {
"pending" => Self::Pending,
_number => {
if let Some(hex_val) = s.strip_prefix("0x") {
let number = u64::from_str_radix(hex_val, 16);
let number = U64::from_str_radix(hex_val, 16);
BlockNumberOrTag::Number(number?)
} else {
return Err(HexStringMissingPrefixError::default().into());
Expand Down Expand Up @@ -367,6 +369,9 @@ pub enum ParseBlockNumberError {
/// Failed to parse hex value
#[error(transparent)]
ParseIntErr(#[from] ParseIntError),
/// Failed to parse hex value
#[error(transparent)]
ParseErr(#[from] ParseError),
/// Block numbers should be 0x-prefixed
#[error(transparent)]
MissingPrefix(#[from] HexStringMissingPrefixError),
Expand Down Expand Up @@ -412,7 +417,7 @@ impl BlockId {

impl From<u64> for BlockId {
fn from(num: u64) -> Self {
BlockNumberOrTag::Number(num).into()
BlockNumberOrTag::Number(U64::from(num)).into()
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/rpc-types/src/eth/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,12 +480,12 @@ impl Filter {
}

/// Returns the numeric value of the `toBlock` field
pub fn get_to_block(&self) -> Option<u64> {
pub fn get_to_block(&self) -> Option<U64> {
self.block_option.get_to_block().and_then(|b| b.as_number())
}

/// Returns the numeric value of the `fromBlock` field
pub fn get_from_block(&self) -> Option<u64> {
pub fn get_from_block(&self) -> Option<U64> {
self.block_option
.get_from_block()
.and_then(|b| b.as_number())
Expand Down Expand Up @@ -798,7 +798,7 @@ impl FilteredParams {
}

/// Returns true if the filter matches the given block number
pub fn filter_block_range(&self, block_number: u64) -> bool {
pub fn filter_block_range(&self, block_number: U64) -> bool {
if self.filter.is_none() {
return true;
}
Expand Down
42 changes: 16 additions & 26 deletions crates/rpc-types/src/eth/transaction/access_list.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloy_primitives::{Address, B256, U256};
use alloy_primitives::{Address, U256};
use serde::{Deserialize, Serialize};

/// A list of addresses and storage keys that the transaction plans to access.
Expand All @@ -9,7 +9,7 @@ pub struct AccessListItem {
/// Account addresses that would be loaded at the start of execution
pub address: Address,
/// Keys of storage that would be loaded at the start of execution
pub storage_keys: Vec<B256>,
pub storage_keys: Vec<U256>,
}

/// AccessList as defined in EIP-2930
Expand All @@ -19,7 +19,9 @@ pub struct AccessList(pub Vec<AccessListItem>);
impl AccessList {
/// Converts the list into a vec, expected by revm
pub fn flattened(&self) -> Vec<(Address, Vec<U256>)> {
self.flatten().collect()
self.flatten()
.map(|(addr, keys)| (addr, keys.to_vec()))
.collect()
}

/// Consumes the type and converts the list into a vec, expected by revm
Expand All @@ -29,28 +31,16 @@ impl AccessList {

/// Consumes the type and returns an iterator over the list's addresses and storage keys.
pub fn into_flatten(self) -> impl Iterator<Item = (Address, Vec<U256>)> {
self.0.into_iter().map(|item| {
(
item.address,
item.storage_keys
.into_iter()
.map(|slot| U256::from_be_bytes(slot.0))
.collect(),
)
})
self.0
.into_iter()
.map(|item| (item.address, item.storage_keys))
}

/// Returns an iterator over the list's addresses and storage keys.
pub fn flatten(&self) -> impl Iterator<Item = (Address, Vec<U256>)> + '_ {
self.0.iter().map(|item| {
(
item.address,
item.storage_keys
.iter()
.map(|slot| U256::from_be_bytes(slot.0))
.collect(),
)
})
pub fn flatten(&self) -> impl Iterator<Item = (Address, &[U256])> + '_ {
self.0
.iter()
.map(|item| (item.address, item.storage_keys.as_slice()))
}
}

Expand All @@ -73,11 +63,11 @@ mod tests {
let list = AccessList(vec![
AccessListItem {
address: Address::ZERO,
storage_keys: vec![B256::ZERO],
storage_keys: vec![U256::ZERO],
},
AccessListItem {
address: Address::ZERO,
storage_keys: vec![B256::ZERO],
storage_keys: vec![U256::ZERO],
},
]);
let json = serde_json::to_string(&list).unwrap();
Expand All @@ -91,11 +81,11 @@ mod tests {
access_list: AccessList(vec![
AccessListItem {
address: Address::ZERO,
storage_keys: vec![B256::ZERO],
storage_keys: vec![U256::ZERO],
},
AccessListItem {
address: Address::ZERO,
storage_keys: vec![B256::ZERO],
storage_keys: vec![U256::ZERO],
},
]),
gas_used: U256::from(100),
Expand Down

0 comments on commit 342e2d9

Please sign in to comment.