Skip to content

Commit

Permalink
chore: fix warnings, check-cfg (#776)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored May 24, 2024
1 parent f12cdd3 commit f78a268
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 150 deletions.
10 changes: 5 additions & 5 deletions crates/contract/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,10 @@ mod tests {
let all = event.query().await.unwrap();
assert_eq!(all.len(), 0);

#[cfg(feature = "ws")]
#[cfg(feature = "pubsub")]
{
let provider = alloy_provider::ProviderBuilder::default()
.on_ws(anvil.ws_endpoint())
let provider = alloy_provider::ProviderBuilder::new()
.on_builtin(&anvil.ws_endpoint())
.await
.unwrap();

Expand All @@ -303,8 +303,8 @@ mod tests {
stream_log.topics().first().unwrap().0
);
assert_eq!(stream_event, expected_event);
assert_eq!(stream_log.address, *contract.address());
assert_eq!(stream_log.block_number, Some(U256::from(3)));
assert_eq!(stream_log.address(), *contract.address());
assert_eq!(stream_log.block_number, Some(4));

// send the request to emit the wrong event
contract
Expand Down
66 changes: 0 additions & 66 deletions crates/eips/src/eip1559/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,70 +101,4 @@ mod tests {
);
}
}

#[cfg(feature = "optimism")]
#[test]
fn calculate_optimism_base_fee_success() {
let base_fee = [
1000000000, 1000000000, 1000000000, 1072671875, 1059263476, 1049238967, 1049238967, 0,
1, 2,
];
let gas_used = [
10000000, 10000000, 10000000, 9000000, 10001000, 0, 10000000, 10000000, 10000000,
10000000,
];
let gas_limit = [
10000000, 12000000, 14000000, 10000000, 14000000, 2000000, 18000000, 18000000,
18000000, 18000000,
];
let next_base_fee = [
1100000048, 1080000000, 1065714297, 1167067046, 1128881311, 1028254188, 1098203452, 1,
2, 3,
];

for i in 0..base_fee.len() {
assert_eq!(
next_base_fee[i],
calc_next_block_base_fee(
gas_used[i],
gas_limit[i],
base_fee[i],
crate::BaseFeeParams::optimism(),
)
);
}
}

#[cfg(feature = "optimism")]
#[test]
fn calculate_optimism_goerli_base_fee_success() {
let base_fee = [
1000000000, 1000000000, 1000000000, 1072671875, 1059263476, 1049238967, 1049238967, 0,
1, 2,
];
let gas_used = [
10000000, 10000000, 10000000, 9000000, 10001000, 0, 10000000, 10000000, 10000000,
10000000,
];
let gas_limit = [
10000000, 12000000, 14000000, 10000000, 14000000, 2000000, 18000000, 18000000,
18000000, 18000000,
];
let next_base_fee = [
1180000000, 1146666666, 1122857142, 1244299375, 1189416692, 1028254188, 1144836295, 1,
2, 3,
];

for i in 0..base_fee.len() {
assert_eq!(
next_base_fee[i],
calc_next_block_base_fee(
gas_used[i],
gas_limit[i],
base_fee[i],
crate::BaseFeeParams::optimism_goerli(),
)
);
}
}
}
2 changes: 2 additions & 0 deletions crates/eips/src/eip1898.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! [EIP-1898]: https://eips.ethereum.org/EIPS/eip-1898

#![allow(unknown_lints, non_local_definitions)]

use alloy_primitives::{hex::FromHexError, ruint::ParseError, BlockHash, BlockNumber, B256, U64};
use alloy_rlp::{bytes, Decodable, Encodable, Error as RlpError};
use core::{
Expand Down
2 changes: 2 additions & 0 deletions crates/eips/src/eip7002.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//!
//! See also [EIP-7002](https://eips.ethereum.org/EIPS/eip-7002): Execution layer triggerable withdrawals

#![allow(unknown_lints, non_local_definitions)]

use alloy_primitives::{address, bytes, Address, Bytes, FixedBytes};
use alloy_rlp::{RlpDecodable, RlpEncodable};

Expand Down
1 change: 1 addition & 0 deletions crates/network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ alloy-rpc-types.workspace = true
alloy-signer.workspace = true
alloy-sol-types.workspace = true

auto_impl.workspace = true
async-trait.workspace = true
futures-utils-wasm.workspace = true
thiserror.workspace = true
Expand Down
4 changes: 4 additions & 0 deletions crates/network/src/transaction/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{Network, TransactionBuilder};
use alloy_consensus::SignableTransaction;
use alloy_primitives::Address;
use async_trait::async_trait;
use auto_impl::auto_impl;
use futures_utils_wasm::impl_future;

/// A signer capable of signing any transaction for the given network.
Expand All @@ -16,6 +17,7 @@ use futures_utils_wasm::impl_future;
/// no specific signer address is specified.
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[auto_impl(&, &mut, Box, Rc, Arc)]
pub trait NetworkSigner<N: Network>: std::fmt::Debug + Send + Sync {
/// Get the default signer address. This address should be used
/// in [`NetworkSigner::sign_transaction_from`] when no specific signer is
Expand Down Expand Up @@ -72,6 +74,7 @@ pub trait NetworkSigner<N: Network>: std::fmt::Debug + Send + Sync {
/// [`ChainId`]: alloy_primitives::ChainId
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[auto_impl(&, &mut, Box, Rc, Arc)]
pub trait TxSigner<Signature> {
/// Get the address of the signer.
fn address(&self) -> Address;
Expand All @@ -98,6 +101,7 @@ pub trait TxSigner<Signature> {
///
/// [EIP-155]: https://eips.ethereum.org/EIPS/eip-155
/// [`ChainId`]: alloy_primitives::ChainId
#[auto_impl(&, &mut, Box, Rc, Arc)]
pub trait TxSignerSync<Signature> {
/// Get the address of the signer.
fn address(&self) -> Address;
Expand Down
5 changes: 4 additions & 1 deletion crates/signer-trezor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ alloy-network.workspace = true
alloy-primitives.workspace = true
alloy-signer.workspace = true

trezor-client = { version = "=0.1.3", default-features = false, features = ["ethereum"] }
trezor-client = { version = "=0.1.3", default-features = false, features = [
"ethereum",
] }

async-trait.workspace = true
semver.workspace = true
thiserror.workspace = true
tracing.workspace = true

[dev-dependencies]
alloy-rpc-types.workspace = true
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
134 changes: 59 additions & 75 deletions crates/signer-trezor/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,9 @@ fn signature_from_trezor(x: trezor_client::client::Signature) -> Result<Signatur
#[cfg(test)]
mod tests {
use super::*;
use alloy_primitives::address;
use alloy_network::{EthereumSigner, TransactionBuilder};
use alloy_primitives::{address, b256};
use alloy_rpc_types::{AccessList, AccessListItem, TransactionRequest};

#[tokio::test]
#[ignore]
Expand Down Expand Up @@ -310,124 +312,106 @@ mod tests {

#[tokio::test]
#[ignore]
#[cfg(TODO)] // TODO: TypedTransaction
async fn test_sign_tx() {
let trezor = TrezorSigner::new(DerivationType::TrezorLive(0), 1).await.unwrap();
let trezor = TrezorSigner::new(DerivationType::TrezorLive(0), Some(1)).await.unwrap();

// approve uni v2 router 0xff
let data = hex::decode("095ea7b30000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap();

let tx_req = TransactionRequest::new()
.to("2ed7afa17473e17ac59908f088b4371d28585476".parse::<Address>().unwrap())
.gas(1000000)
.gas_price(400e9 as u64)
.nonce(5)
.data(data)
.value(ethers_core::utils::parse_ether(100).unwrap())
.into();
let tx = trezor.sign_transaction(&tx_req).await.unwrap();
let _tx = TransactionRequest::default()
.to(address!("2ed7afa17473e17ac59908f088b4371d28585476"))
.with_gas_limit(1000000)
.with_gas_price(400e9 as u128)
.with_nonce(5)
.with_input(data)
.with_value(U256::from(100e18 as u128))
.build(&EthereumSigner::new(trezor))
.await
.unwrap();
}

#[tokio::test]
#[ignore]
#[cfg(TODO)] // TODO: TypedTransaction
async fn test_sign_big_data_tx() {
let trezor = TrezorSigner::new(DerivationType::TrezorLive(0), 1).await.unwrap();
let trezor = TrezorSigner::new(DerivationType::TrezorLive(0), Some(1)).await.unwrap();

// invalid data
let big_data = hex::decode("095ea7b30000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff".to_string()+ &"ff".repeat(1032*2) + "aa").unwrap();
let tx_req = TransactionRequest::new()
.to("2ed7afa17473e17ac59908f088b4371d28585476".parse::<Address>().unwrap())
.gas(1000000)
.gas_price(400e9 as u64)
.nonce(5)
.data(big_data)
.value(ethers_core::utils::parse_ether(100).unwrap())
.into();
let tx = trezor.sign_transaction(&tx_req).await.unwrap();
let _tx = TransactionRequest::default()
.to(address!("2ed7afa17473e17ac59908f088b4371d28585476"))
.with_gas_limit(1000000)
.with_gas_price(400e9 as u128)
.with_nonce(5)
.with_input(big_data)
.with_value(U256::from(100e18 as u128))
.build(&EthereumSigner::new(trezor))
.await
.unwrap();
}

#[tokio::test]
#[ignore]
#[cfg(TODO)] // TODO: TypedTransaction
async fn test_sign_empty_txes() {
// Contract creation (empty `to`), requires data.
// To test without the data field, we need to specify a `to` address.
let trezor = TrezorSigner::new(DerivationType::TrezorLive(0), 1, None).await.unwrap();
{
let tx_req = Eip1559TransactionRequest::new()
.to("2ed7afa17473e17ac59908f088b4371d28585476".parse::<Address>().unwrap())
.into();
let tx = trezor.sign_transaction(&tx_req).await.unwrap();
}
{
let tx_req = TransactionRequest::new()
.to("2ed7afa17473e17ac59908f088b4371d28585476".parse::<Address>().unwrap())
.into();
let tx = trezor.sign_transaction(&tx_req).await.unwrap();
}
let trezor = TrezorSigner::new(DerivationType::TrezorLive(0), Some(1)).await.unwrap();
TransactionRequest::default()
.to(address!("2ed7afa17473e17ac59908f088b4371d28585476"))
.with_gas_price(1)
.build(&EthereumSigner::new(trezor))
.await
.unwrap();

let data = hex::decode("095ea7b30000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap();

// Contract creation (empty `to`, with data) should show on the trezor device as:
// ` "0 Wei ETH
// ` new contract?"
let trezor = TrezorSigner::new(DerivationType::TrezorLive(0), 1).await.unwrap();
{
let tx_req = Eip1559TransactionRequest::new().data(data.clone()).into();
let tx = trezor.sign_transaction(&tx_req).await.unwrap();
}
let trezor = TrezorSigner::new(DerivationType::TrezorLive(0), Some(1)).await.unwrap();
{
let tx_req = TransactionRequest::new().data(data.clone()).into();
let tx = trezor.sign_transaction(&tx_req).await.unwrap();
let _tx = TransactionRequest::default()
.into_create()
.with_input(data)
.with_gas_price(1)
.build(&EthereumSigner::new(trezor))
.await
.unwrap();
}
}

#[tokio::test]
#[ignore]
#[cfg(TODO)] // TODO: TypedTransaction
async fn test_sign_eip1559_tx() {
let trezor = TrezorSigner::new(DerivationType::TrezorLive(0), 1).await.unwrap();
let trezor = TrezorSigner::new(DerivationType::TrezorLive(0), Some(1)).await.unwrap();

// approve uni v2 router 0xff
let data = hex::decode("095ea7b30000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap();

let lst = AccessList(vec![
AccessListItem {
address: "0x8ba1f109551bd432803012645ac136ddd64dba72".parse().unwrap(),
address: address!("8ba1f109551bd432803012645ac136ddd64dba72"),
storage_keys: vec![
"0x0000000000000000000000000000000000000000000000000000000000000000"
.parse()
.unwrap(),
"0x0000000000000000000000000000000000000000000000000000000000000042"
.parse()
.unwrap(),
b256!("0000000000000000000000000000000000000000000000000000000000000000"),
b256!("0000000000000000000000000000000000000000000000000000000000000042"),
],
},
AccessListItem {
address: "0x2ed7afa17473e17ac59908f088b4371d28585476".parse().unwrap(),
address: address!("2ed7afa17473e17ac59908f088b4371d28585476"),
storage_keys: vec![
"0x0000000000000000000000000000000000000000000000000000000000000000"
.parse()
.unwrap(),
"0x0000000000000000000000000000000000000000000000000000000000000042"
.parse()
.unwrap(),
b256!("0000000000000000000000000000000000000000000000000000000000000000"),
b256!("0000000000000000000000000000000000000000000000000000000000000042"),
],
},
]);

let tx_req = Eip1559TransactionRequest::new()
.to("2ed7afa17473e17ac59908f088b4371d28585476".parse::<Address>().unwrap())
.gas(1000000)
.max_fee_per_gas(400e9 as u64)
.max_priority_fee_per_gas(400e9 as u64)
.nonce(5)
.data(data)
.access_list(lst)
.value(ethers_core::utils::parse_ether(100).unwrap())
.into();

let tx = trezor.sign_transaction(&tx_req).await.unwrap();
let _tx = TransactionRequest::default()
.to(address!("2ed7afa17473e17ac59908f088b4371d28585476"))
.with_gas_limit(1000000)
.max_fee_per_gas(400e9 as u128)
.max_priority_fee_per_gas(400e9 as u128)
.with_nonce(5)
.with_input(data)
.with_access_list(lst)
.with_value(U256::from(100e18 as u128))
.build(&EthereumSigner::new(trezor))
.await
.unwrap();
}
}
12 changes: 10 additions & 2 deletions crates/signer-wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,20 @@ coins-bip39 = { version = "0.8.7", default-features = false, features = [
], optional = true }

# yubi
yubihsm = { version = "0.42", features = ["secp256k1", "http", "usb"], optional = true }
yubihsm = { version = "0.42", features = [
"secp256k1",
"http",
"usb",
], optional = true }

[dev-dependencies]
serde.workspace = true
alloy-dyn-abi.workspace = true
alloy-sol-types.workspace = true
alloy-consensus = { workspace = true, features = ["std"] }
alloy-network.workspace = true
assert_matches.workspace = true
serde_json.workspace = true
serde.workspace = true
tempfile.workspace = true
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }

Expand All @@ -52,3 +58,5 @@ keystore = ["dep:eth-keystore", "dep:elliptic-curve"]
mnemonic = ["dep:coins-bip32", "dep:coins-bip39"]
mnemonic-all-languages = ["mnemonic", "coins-bip39?/all-langs"]
yubihsm = ["dep:yubihsm", "dep:elliptic-curve"]

eip712 = ["alloy-signer/eip712"]
2 changes: 1 addition & 1 deletion crates/signer-wallet/src/private_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ mod tests {
let foo_bar = FooBar {
foo: I256::try_from(10u64).unwrap(),
bar: U256::from(20u64),
fizz: b"fizz".to_vec(),
fizz: b"fizz".to_vec().into(),
buzz: keccak256("buzz"),
far: "space".into(),
out: Address::ZERO,
Expand Down

0 comments on commit f78a268

Please sign in to comment.