Skip to content

Commit b5f2135

Browse files
authored
refactor!(all): remove ethers and use alloy (#235)
* 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]>
1 parent 4cbeb32 commit b5f2135

28 files changed

+304
-384
lines changed

.github/workflows/tests.yml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@ name: tests
33

44
on:
55
push:
6-
branches: [ main ]
6+
branches: [main]
77
pull_request:
88
workflow_dispatch:
99

1010
jobs:
1111
fmt:
1212
name: cargo fmt
1313
runs-on: ubuntu-latest
14-
container:
15-
image: rust:1.74-bookworm
14+
container:
15+
image: rust:1.79-bookworm
1616
steps:
1717
- uses: actions/checkout@v3
1818
- run: |
1919
rustup component add rustfmt
2020
cargo fmt --all -- --check
21-
21+
2222
clippy:
2323
name: cargo clippy
2424
runs-on: ubuntu-latest
25-
container:
26-
image: rust:1.74-bookworm
25+
container:
26+
image: rust:1.79-bookworm
2727
steps:
2828
- uses: actions/checkout@v3
2929
- uses: actions/cache@v3
@@ -36,9 +36,9 @@ jobs:
3636
target/
3737
key: ${{ runner.os }}-cargo-clippy
3838
- run: |
39-
rustup component add clippy
40-
cargo clippy --all-targets --all-features -- -D warnings
41-
39+
rustup component add clippy
40+
cargo clippy --all-targets --all-features -- -D warnings
41+
4242
test-and-coverage:
4343
name: cargo test and coverage
4444
runs-on: ubuntu-latest
@@ -47,7 +47,7 @@ jobs:
4747
pull-requests: write
4848
actions: read
4949
container:
50-
image: rust:1.74-bookworm
50+
image: rust:1.79-bookworm
5151
steps:
5252
- uses: actions/checkout@v3
5353
- uses: actions/cache@v3
@@ -62,7 +62,7 @@ jobs:
6262
- name: Install cargo-llvm-cov
6363
uses: taiki-e/install-action@cargo-llvm-cov
6464
- name: Run tests and generate coverage report
65-
run: cargo llvm-cov test --all-features --workspace --lcov --output-path lcov.info
65+
run: cargo llvm-cov test --all-features --workspace --lcov --output-path lcov.info
6666
- name: Upload coverage to Coveralls
6767
uses: coverallsapp/[email protected]
6868
with:
@@ -72,7 +72,7 @@ jobs:
7272
name: cargo test docs
7373
runs-on: ubuntu-latest
7474
container:
75-
image: rust:1.74-bookworm
75+
image: rust:1.79-bookworm
7676
steps:
7777
- uses: actions/checkout@v3
7878
- uses: actions/cache@v3

Dockerfile.tap_aggregator

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM rust:1.74-bookworm as build
1+
FROM rust:1.79-bookworm as build
22

33
WORKDIR /root
44
COPY . .

tap_aggregator/Cargo.toml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ anyhow = "1.0.70"
1616
tokio = { version = "1.27.0", features = ["macros", "signal"] }
1717
tap_core = { version = "1.0.0", path = "../tap_core" }
1818
jsonrpsee = { version = "0.18.0", features = ["server", "macros"] }
19-
ethers-signers = "2.0.3"
2019
clap = { version = "4.2.4", features = ["derive", "env"] }
21-
ethers-core = "2.0.3"
2220
serde = { version = "1.0.163", features = ["derive"] }
2321
serde_json = { version = "1.0.96", features = ["raw_value"] }
2422
strum = { version = "0.24.1", features = ["strum_macros", "derive"] }
@@ -28,9 +26,7 @@ prometheus = "0.13.3"
2826
axum = "0.6.18"
2927
futures-util = "0.3.28"
3028
lazy_static = "1.4.0"
31-
alloy-sol-types = { version = "0.7.0", features = ["eip712-serde"] }
32-
alloy-primitives = { version = "0.7.0", features = ["serde"] }
33-
ethereum-types = "0.14.1"
29+
alloy = { version = "0.2.0", features = ["full"] }
3430
ruint = "1.10.1"
3531

3632
[dev-dependencies]

tap_aggregator/src/aggregator.rs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@
33

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

6-
use alloy_primitives::Address;
7-
use alloy_sol_types::{Eip712Domain, SolStruct};
6+
use alloy::{
7+
dyn_abi::Eip712Domain, primitives::Address, signers::local::PrivateKeySigner,
8+
sol_types::SolStruct,
9+
};
810
use anyhow::{bail, Ok, Result};
9-
use ethers_core::types::Signature;
10-
use ethers_signers::LocalWallet;
1111

1212
use tap_core::{
13-
rav::ReceiptAggregateVoucher, receipt::Receipt, signed_message::EIP712SignedMessage,
13+
rav::ReceiptAggregateVoucher,
14+
receipt::Receipt,
15+
signed_message::{EIP712SignedMessage, SignatureBytes, SignatureBytesExt},
1416
};
1517

1618
pub fn check_and_aggregate_receipts(
1719
domain_separator: &Eip712Domain,
1820
receipts: &[EIP712SignedMessage<Receipt>],
1921
previous_rav: Option<EIP712SignedMessage<ReceiptAggregateVoucher>>,
20-
wallet: &LocalWallet,
22+
wallet: &PrivateKeySigner,
2123
accepted_addresses: &HashSet<Address>,
2224
) -> Result<EIP712SignedMessage<ReceiptAggregateVoucher>> {
2325
check_signatures_unique(receipts)?;
@@ -99,11 +101,15 @@ fn check_allocation_id(
99101
}
100102

101103
fn check_signatures_unique(receipts: &[EIP712SignedMessage<Receipt>]) -> Result<()> {
102-
let mut receipt_signatures: hash_set::HashSet<Signature> = hash_set::HashSet::new();
104+
let mut receipt_signatures: hash_set::HashSet<SignatureBytes> = hash_set::HashSet::new();
103105
for receipt in receipts.iter() {
104-
let signature = receipt.signature;
106+
let signature = receipt.signature.get_signature_bytes();
105107
if !receipt_signatures.insert(signature) {
106-
return Err(tap_core::Error::DuplicateReceiptSignature(signature.to_string()).into());
108+
return Err(tap_core::Error::DuplicateReceiptSignature(format!(
109+
"{:?}",
110+
receipt.signature
111+
))
112+
.into());
107113
}
108114
}
109115
Ok(())
@@ -133,22 +139,17 @@ fn check_receipt_timestamps(
133139
mod tests {
134140
use std::str::FromStr;
135141

136-
use alloy_primitives::Address;
137-
use alloy_sol_types::Eip712Domain;
138-
use ethers_signers::{LocalWallet, Signer};
142+
use alloy::{dyn_abi::Eip712Domain, primitives::Address, signers::local::PrivateKeySigner};
139143
use rstest::*;
140144

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

144148
#[fixture]
145-
fn keys() -> (LocalWallet, Address) {
146-
let wallet = LocalWallet::from_str(
147-
"1ab42cc412b618bdea3a599e3c9bae199ebf030895b039e9db1e30dafb12b727",
148-
)
149-
.unwrap();
150-
let address: [u8; 20] = wallet.address().into();
151-
(wallet, address.into())
149+
fn keys() -> (PrivateKeySigner, Address) {
150+
let wallet = PrivateKeySigner::random();
151+
let address = wallet.address();
152+
(wallet, address)
152153
}
153154

154155
#[fixture]
@@ -169,7 +170,7 @@ mod tests {
169170
#[rstest]
170171
#[test]
171172
fn check_signatures_unique_fail(
172-
keys: (LocalWallet, Address),
173+
keys: (PrivateKeySigner, Address),
173174
allocation_ids: Vec<Address>,
174175
domain_separator: Eip712Domain,
175176
) {
@@ -191,7 +192,7 @@ mod tests {
191192
#[rstest]
192193
#[test]
193194
fn check_signatures_unique_ok(
194-
keys: (LocalWallet, Address),
195+
keys: (PrivateKeySigner, Address),
195196
allocation_ids: Vec<Address>,
196197
domain_separator: Eip712Domain,
197198
) {
@@ -219,7 +220,7 @@ mod tests {
219220
#[test]
220221
/// Test that a receipt with a timestamp greater then the rav timestamp passes
221222
fn check_receipt_timestamps(
222-
keys: (LocalWallet, Address),
223+
keys: (PrivateKeySigner, Address),
223224
allocation_ids: Vec<Address>,
224225
domain_separator: Eip712Domain,
225226
) {
@@ -289,7 +290,7 @@ mod tests {
289290
/// Test check_allocation_id with 2 receipts that have the correct allocation id
290291
/// and 1 receipt that has the wrong allocation id
291292
fn check_allocation_id_fail(
292-
keys: (LocalWallet, Address),
293+
keys: (PrivateKeySigner, Address),
293294
allocation_ids: Vec<Address>,
294295
domain_separator: Eip712Domain,
295296
) {
@@ -323,7 +324,7 @@ mod tests {
323324
#[test]
324325
/// Test check_allocation_id with 3 receipts that have the correct allocation id
325326
fn check_allocation_id_ok(
326-
keys: (LocalWallet, Address),
327+
keys: (PrivateKeySigner, Address),
327328
allocation_ids: Vec<Address>,
328329
domain_separator: Eip712Domain,
329330
) {

tap_aggregator/src/main.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ use std::borrow::Cow;
77
use std::collections::HashSet;
88
use std::str::FromStr;
99

10-
use alloy_primitives::{Address, FixedBytes, U256};
11-
use alloy_sol_types::Eip712Domain;
10+
use alloy::dyn_abi::Eip712Domain;
11+
use alloy::primitives::Address;
12+
use alloy::primitives::FixedBytes;
13+
use alloy::signers::local::PrivateKeySigner;
1214
use anyhow::Result;
1315
use clap::Parser;
14-
use ethers_signers::{LocalWallet, Signer};
16+
use ruint::aliases::U256;
1517
use tokio::signal::unix::{signal, SignalKind};
1618

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

98100
// Create a wallet from the mnemonic.
99-
let wallet = LocalWallet::from_str(&args.private_key)?;
101+
let wallet = PrivateKeySigner::from_str(&args.private_key)?;
100102

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

tap_aggregator/src/server.rs

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

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

6-
use alloy_primitives::Address;
7-
use alloy_sol_types::Eip712Domain;
6+
use alloy::dyn_abi::Eip712Domain;
7+
use alloy::primitives::Address;
8+
use alloy::signers::local::PrivateKeySigner;
89
use anyhow::Result;
9-
use ethers_signers::LocalWallet;
1010
use jsonrpsee::{proc_macros::rpc, server::ServerBuilder, server::ServerHandle};
1111
use lazy_static::lazy_static;
1212
use prometheus::{register_counter, register_int_counter, Counter, IntCounter};
@@ -91,7 +91,7 @@ pub trait Rpc {
9191
}
9292

9393
struct RpcImpl {
94-
wallet: LocalWallet,
94+
wallet: PrivateKeySigner,
9595
accepted_addresses: HashSet<Address>,
9696
domain_separator: Eip712Domain,
9797
}
@@ -128,7 +128,7 @@ fn check_api_version_deprecation(api_version: &TapRpcApiVersion) -> Option<JsonR
128128

129129
fn aggregate_receipts_(
130130
api_version: String,
131-
wallet: &LocalWallet,
131+
wallet: &PrivateKeySigner,
132132
accepted_addresses: &HashSet<Address>,
133133
domain_separator: &Eip712Domain,
134134
receipts: Vec<EIP712SignedMessage<Receipt>>,
@@ -210,7 +210,7 @@ impl RpcServer for RpcImpl {
210210

211211
pub async fn run_server(
212212
port: u16,
213-
wallet: LocalWallet,
213+
wallet: PrivateKeySigner,
214214
accepted_addresses: HashSet<Address>,
215215
domain_separator: Eip712Domain,
216216
max_request_body_size: u32,
@@ -243,9 +243,7 @@ mod tests {
243243
use std::collections::HashSet;
244244
use std::str::FromStr;
245245

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

260258
#[derive(Clone)]
261259
struct Keys {
262-
wallet: LocalWallet,
260+
wallet: PrivateKeySigner,
263261
address: Address,
264262
}
265263

266-
fn keys(index: u32) -> Keys {
267-
let wallet: LocalWallet = MnemonicBuilder::<English>::default()
268-
.phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about")
269-
.index(index)
270-
.unwrap()
271-
.build()
272-
.unwrap();
273-
// Alloy library does not have feature parity with ethers library (yet) This workaround is needed to get the address
274-
// to convert to an alloy Address. This will not be needed when the alloy library has wallet support.
275-
let address: [u8; 20] = wallet.address().into();
276-
277-
Keys {
278-
wallet,
279-
address: address.into(),
280-
}
264+
fn keys() -> Keys {
265+
let wallet = PrivateKeySigner::random();
266+
let address = wallet.address();
267+
Keys { wallet, address }
281268
}
282269

283270
#[fixture]
@@ -319,7 +306,7 @@ mod tests {
319306
http_max_concurrent_connections: u32,
320307
) {
321308
// The keys that will be used to sign the new RAVs
322-
let keys_main = keys(0);
309+
let keys_main = keys();
323310

324311
// Start the JSON-RPC server.
325312
let (handle, local_addr) = server::run_server(
@@ -362,10 +349,10 @@ mod tests {
362349
#[values(0, 1, 2)] random_seed: u64,
363350
) {
364351
// The keys that will be used to sign the new RAVs
365-
let keys_main = keys(0);
352+
let keys_main = keys();
366353
// Extra keys to test the server's ability to accept multiple signers as input
367-
let keys_0 = keys(1);
368-
let keys_1 = keys(2);
354+
let keys_0 = keys();
355+
let keys_1 = keys();
369356
// Vector of all wallets to make it easier to select one randomly
370357
let all_wallets = vec![keys_main.clone(), keys_0.clone(), keys_1.clone()];
371358
// PRNG for selecting a random wallet
@@ -443,10 +430,10 @@ mod tests {
443430
#[values(0, 1, 2, 3, 4)] random_seed: u64,
444431
) {
445432
// The keys that will be used to sign the new RAVs
446-
let keys_main = keys(0);
433+
let keys_main = keys();
447434
// Extra keys to test the server's ability to accept multiple signers as input
448-
let keys_0 = keys(1);
449-
let keys_1 = keys(2);
435+
let keys_0 = keys();
436+
let keys_1 = keys();
450437
// Vector of all wallets to make it easier to select one randomly
451438
let all_wallets = vec![keys_main.clone(), keys_0.clone(), keys_1.clone()];
452439
// PRNG for selecting a random wallet
@@ -528,7 +515,7 @@ mod tests {
528515
allocation_ids: Vec<Address>,
529516
) {
530517
// The keys that will be used to sign the new RAVs
531-
let keys_main = keys(0);
518+
let keys_main = keys();
532519

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

616603
// Set the request byte size limit to a value that easily triggers the HTTP 413
617604
// error.

0 commit comments

Comments
 (0)