Skip to content

Commit

Permalink
refactor!(all): remove ethers and use alloy (#235)
Browse files Browse the repository at this point in the history
* refactor(all): remove ethers and use alloy

Signed-off-by: Gustavo Inacio <[email protected]>

* chore: bump rust to 1.79

Signed-off-by: Gustavo Inacio <[email protected]>

* refactor: use SignatureBytes instead of [u8; 65]

Signed-off-by: Gustavo Inacio <[email protected]>

---------

Signed-off-by: Gustavo Inacio <[email protected]>
  • Loading branch information
gusinacio authored Aug 2, 2024
1 parent 4cbeb32 commit b5f2135
Show file tree
Hide file tree
Showing 28 changed files with 304 additions and 384 deletions.
24 changes: 12 additions & 12 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@ name: tests

on:
push:
branches: [ main ]
branches: [main]
pull_request:
workflow_dispatch:

jobs:
fmt:
name: cargo fmt
runs-on: ubuntu-latest
container:
image: rust:1.74-bookworm
container:
image: rust:1.79-bookworm
steps:
- uses: actions/checkout@v3
- run: |
rustup component add rustfmt
cargo fmt --all -- --check
clippy:
name: cargo clippy
runs-on: ubuntu-latest
container:
image: rust:1.74-bookworm
container:
image: rust:1.79-bookworm
steps:
- uses: actions/checkout@v3
- uses: actions/cache@v3
Expand All @@ -36,9 +36,9 @@ jobs:
target/
key: ${{ runner.os }}-cargo-clippy
- run: |
rustup component add clippy
cargo clippy --all-targets --all-features -- -D warnings
rustup component add clippy
cargo clippy --all-targets --all-features -- -D warnings
test-and-coverage:
name: cargo test and coverage
runs-on: ubuntu-latest
Expand All @@ -47,7 +47,7 @@ jobs:
pull-requests: write
actions: read
container:
image: rust:1.74-bookworm
image: rust:1.79-bookworm
steps:
- uses: actions/checkout@v3
- uses: actions/cache@v3
Expand All @@ -62,7 +62,7 @@ jobs:
- name: Install cargo-llvm-cov
uses: taiki-e/install-action@cargo-llvm-cov
- name: Run tests and generate coverage report
run: cargo llvm-cov test --all-features --workspace --lcov --output-path lcov.info
run: cargo llvm-cov test --all-features --workspace --lcov --output-path lcov.info
- name: Upload coverage to Coveralls
uses: coverallsapp/[email protected]
with:
Expand All @@ -72,7 +72,7 @@ jobs:
name: cargo test docs
runs-on: ubuntu-latest
container:
image: rust:1.74-bookworm
image: rust:1.79-bookworm
steps:
- uses: actions/checkout@v3
- uses: actions/cache@v3
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.tap_aggregator
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM rust:1.74-bookworm as build
FROM rust:1.79-bookworm as build

WORKDIR /root
COPY . .
Expand Down
6 changes: 1 addition & 5 deletions tap_aggregator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ anyhow = "1.0.70"
tokio = { version = "1.27.0", features = ["macros", "signal"] }
tap_core = { version = "1.0.0", path = "../tap_core" }
jsonrpsee = { version = "0.18.0", features = ["server", "macros"] }
ethers-signers = "2.0.3"
clap = { version = "4.2.4", features = ["derive", "env"] }
ethers-core = "2.0.3"
serde = { version = "1.0.163", features = ["derive"] }
serde_json = { version = "1.0.96", features = ["raw_value"] }
strum = { version = "0.24.1", features = ["strum_macros", "derive"] }
Expand All @@ -28,9 +26,7 @@ prometheus = "0.13.3"
axum = "0.6.18"
futures-util = "0.3.28"
lazy_static = "1.4.0"
alloy-sol-types = { version = "0.7.0", features = ["eip712-serde"] }
alloy-primitives = { version = "0.7.0", features = ["serde"] }
ethereum-types = "0.14.1"
alloy = { version = "0.2.0", features = ["full"] }
ruint = "1.10.1"

[dev-dependencies]
Expand Down
49 changes: 25 additions & 24 deletions tap_aggregator/src/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@

use std::collections::{hash_set, HashSet};

use alloy_primitives::Address;
use alloy_sol_types::{Eip712Domain, SolStruct};
use alloy::{
dyn_abi::Eip712Domain, primitives::Address, signers::local::PrivateKeySigner,
sol_types::SolStruct,
};
use anyhow::{bail, Ok, Result};
use ethers_core::types::Signature;
use ethers_signers::LocalWallet;

use tap_core::{
rav::ReceiptAggregateVoucher, receipt::Receipt, signed_message::EIP712SignedMessage,
rav::ReceiptAggregateVoucher,
receipt::Receipt,
signed_message::{EIP712SignedMessage, SignatureBytes, SignatureBytesExt},
};

pub fn check_and_aggregate_receipts(
domain_separator: &Eip712Domain,
receipts: &[EIP712SignedMessage<Receipt>],
previous_rav: Option<EIP712SignedMessage<ReceiptAggregateVoucher>>,
wallet: &LocalWallet,
wallet: &PrivateKeySigner,
accepted_addresses: &HashSet<Address>,
) -> Result<EIP712SignedMessage<ReceiptAggregateVoucher>> {
check_signatures_unique(receipts)?;
Expand Down Expand Up @@ -99,11 +101,15 @@ fn check_allocation_id(
}

fn check_signatures_unique(receipts: &[EIP712SignedMessage<Receipt>]) -> Result<()> {
let mut receipt_signatures: hash_set::HashSet<Signature> = hash_set::HashSet::new();
let mut receipt_signatures: hash_set::HashSet<SignatureBytes> = hash_set::HashSet::new();
for receipt in receipts.iter() {
let signature = receipt.signature;
let signature = receipt.signature.get_signature_bytes();
if !receipt_signatures.insert(signature) {
return Err(tap_core::Error::DuplicateReceiptSignature(signature.to_string()).into());
return Err(tap_core::Error::DuplicateReceiptSignature(format!(
"{:?}",
receipt.signature
))
.into());
}
}
Ok(())
Expand Down Expand Up @@ -133,22 +139,17 @@ fn check_receipt_timestamps(
mod tests {
use std::str::FromStr;

use alloy_primitives::Address;
use alloy_sol_types::Eip712Domain;
use ethers_signers::{LocalWallet, Signer};
use alloy::{dyn_abi::Eip712Domain, primitives::Address, signers::local::PrivateKeySigner};
use rstest::*;

use crate::aggregator;
use tap_core::{receipt::Receipt, signed_message::EIP712SignedMessage, tap_eip712_domain};

#[fixture]
fn keys() -> (LocalWallet, Address) {
let wallet = LocalWallet::from_str(
"1ab42cc412b618bdea3a599e3c9bae199ebf030895b039e9db1e30dafb12b727",
)
.unwrap();
let address: [u8; 20] = wallet.address().into();
(wallet, address.into())
fn keys() -> (PrivateKeySigner, Address) {
let wallet = PrivateKeySigner::random();
let address = wallet.address();
(wallet, address)
}

#[fixture]
Expand All @@ -169,7 +170,7 @@ mod tests {
#[rstest]
#[test]
fn check_signatures_unique_fail(
keys: (LocalWallet, Address),
keys: (PrivateKeySigner, Address),
allocation_ids: Vec<Address>,
domain_separator: Eip712Domain,
) {
Expand All @@ -191,7 +192,7 @@ mod tests {
#[rstest]
#[test]
fn check_signatures_unique_ok(
keys: (LocalWallet, Address),
keys: (PrivateKeySigner, Address),
allocation_ids: Vec<Address>,
domain_separator: Eip712Domain,
) {
Expand Down Expand Up @@ -219,7 +220,7 @@ mod tests {
#[test]
/// Test that a receipt with a timestamp greater then the rav timestamp passes
fn check_receipt_timestamps(
keys: (LocalWallet, Address),
keys: (PrivateKeySigner, Address),
allocation_ids: Vec<Address>,
domain_separator: Eip712Domain,
) {
Expand Down Expand Up @@ -289,7 +290,7 @@ mod tests {
/// Test check_allocation_id with 2 receipts that have the correct allocation id
/// and 1 receipt that has the wrong allocation id
fn check_allocation_id_fail(
keys: (LocalWallet, Address),
keys: (PrivateKeySigner, Address),
allocation_ids: Vec<Address>,
domain_separator: Eip712Domain,
) {
Expand Down Expand Up @@ -323,7 +324,7 @@ mod tests {
#[test]
/// Test check_allocation_id with 3 receipts that have the correct allocation id
fn check_allocation_id_ok(
keys: (LocalWallet, Address),
keys: (PrivateKeySigner, Address),
allocation_ids: Vec<Address>,
domain_separator: Eip712Domain,
) {
Expand Down
10 changes: 6 additions & 4 deletions tap_aggregator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ use std::borrow::Cow;
use std::collections::HashSet;
use std::str::FromStr;

use alloy_primitives::{Address, FixedBytes, U256};
use alloy_sol_types::Eip712Domain;
use alloy::dyn_abi::Eip712Domain;
use alloy::primitives::Address;
use alloy::primitives::FixedBytes;
use alloy::signers::local::PrivateKeySigner;
use anyhow::Result;
use clap::Parser;
use ethers_signers::{LocalWallet, Signer};
use ruint::aliases::U256;
use tokio::signal::unix::{signal, SignalKind};

use log::{debug, info};
Expand Down Expand Up @@ -96,7 +98,7 @@ async fn main() -> Result<()> {
tokio::spawn(metrics::run_server(args.metrics_port));

// Create a wallet from the mnemonic.
let wallet = LocalWallet::from_str(&args.private_key)?;
let wallet = PrivateKeySigner::from_str(&args.private_key)?;

info!("Wallet address: {:#40x}", wallet.address());

Expand Down
55 changes: 21 additions & 34 deletions tap_aggregator/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

use std::{collections::HashSet, str::FromStr};

use alloy_primitives::Address;
use alloy_sol_types::Eip712Domain;
use alloy::dyn_abi::Eip712Domain;
use alloy::primitives::Address;
use alloy::signers::local::PrivateKeySigner;
use anyhow::Result;
use ethers_signers::LocalWallet;
use jsonrpsee::{proc_macros::rpc, server::ServerBuilder, server::ServerHandle};
use lazy_static::lazy_static;
use prometheus::{register_counter, register_int_counter, Counter, IntCounter};
Expand Down Expand Up @@ -91,7 +91,7 @@ pub trait Rpc {
}

struct RpcImpl {
wallet: LocalWallet,
wallet: PrivateKeySigner,
accepted_addresses: HashSet<Address>,
domain_separator: Eip712Domain,
}
Expand Down Expand Up @@ -128,7 +128,7 @@ fn check_api_version_deprecation(api_version: &TapRpcApiVersion) -> Option<JsonR

fn aggregate_receipts_(
api_version: String,
wallet: &LocalWallet,
wallet: &PrivateKeySigner,
accepted_addresses: &HashSet<Address>,
domain_separator: &Eip712Domain,
receipts: Vec<EIP712SignedMessage<Receipt>>,
Expand Down Expand Up @@ -210,7 +210,7 @@ impl RpcServer for RpcImpl {

pub async fn run_server(
port: u16,
wallet: LocalWallet,
wallet: PrivateKeySigner,
accepted_addresses: HashSet<Address>,
domain_separator: Eip712Domain,
max_request_body_size: u32,
Expand Down Expand Up @@ -243,9 +243,7 @@ mod tests {
use std::collections::HashSet;
use std::str::FromStr;

use alloy_primitives::Address;
use alloy_sol_types::Eip712Domain;
use ethers_signers::{coins_bip39::English, LocalWallet, MnemonicBuilder, Signer};
use alloy::{dyn_abi::Eip712Domain, primitives::Address, signers::local::PrivateKeySigner};
use jsonrpsee::{core::client::ClientT, http_client::HttpClientBuilder, rpc_params};
use rand::prelude::*;
use rand::seq::SliceRandom;
Expand All @@ -259,25 +257,14 @@ mod tests {

#[derive(Clone)]
struct Keys {
wallet: LocalWallet,
wallet: PrivateKeySigner,
address: Address,
}

fn keys(index: u32) -> Keys {
let wallet: LocalWallet = MnemonicBuilder::<English>::default()
.phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about")
.index(index)
.unwrap()
.build()
.unwrap();
// Alloy library does not have feature parity with ethers library (yet) This workaround is needed to get the address
// to convert to an alloy Address. This will not be needed when the alloy library has wallet support.
let address: [u8; 20] = wallet.address().into();

Keys {
wallet,
address: address.into(),
}
fn keys() -> Keys {
let wallet = PrivateKeySigner::random();
let address = wallet.address();
Keys { wallet, address }
}

#[fixture]
Expand Down Expand Up @@ -319,7 +306,7 @@ mod tests {
http_max_concurrent_connections: u32,
) {
// The keys that will be used to sign the new RAVs
let keys_main = keys(0);
let keys_main = keys();

// Start the JSON-RPC server.
let (handle, local_addr) = server::run_server(
Expand Down Expand Up @@ -362,10 +349,10 @@ mod tests {
#[values(0, 1, 2)] random_seed: u64,
) {
// The keys that will be used to sign the new RAVs
let keys_main = keys(0);
let keys_main = keys();
// Extra keys to test the server's ability to accept multiple signers as input
let keys_0 = keys(1);
let keys_1 = keys(2);
let keys_0 = keys();
let keys_1 = keys();
// Vector of all wallets to make it easier to select one randomly
let all_wallets = vec![keys_main.clone(), keys_0.clone(), keys_1.clone()];
// PRNG for selecting a random wallet
Expand Down Expand Up @@ -443,10 +430,10 @@ mod tests {
#[values(0, 1, 2, 3, 4)] random_seed: u64,
) {
// The keys that will be used to sign the new RAVs
let keys_main = keys(0);
let keys_main = keys();
// Extra keys to test the server's ability to accept multiple signers as input
let keys_0 = keys(1);
let keys_1 = keys(2);
let keys_0 = keys();
let keys_1 = keys();
// Vector of all wallets to make it easier to select one randomly
let all_wallets = vec![keys_main.clone(), keys_0.clone(), keys_1.clone()];
// PRNG for selecting a random wallet
Expand Down Expand Up @@ -528,7 +515,7 @@ mod tests {
allocation_ids: Vec<Address>,
) {
// The keys that will be used to sign the new RAVs
let keys_main = keys(0);
let keys_main = keys();

// Start the JSON-RPC server.
let (handle, local_addr) = server::run_server(
Expand Down Expand Up @@ -611,7 +598,7 @@ mod tests {
#[values("0.0")] api_version: &str,
) {
// The keys that will be used to sign the new RAVs
let keys_main = keys(0);
let keys_main = keys();

// Set the request byte size limit to a value that easily triggers the HTTP 413
// error.
Expand Down
Loading

0 comments on commit b5f2135

Please sign in to comment.