Skip to content

Commit b577d19

Browse files
authored
Merge pull request #207 from semiotic-ai/gusinacio/check-trait-refactor
Checks refactor
2 parents e16bc1e + c5b890a commit b577d19

35 files changed

+1595
-2606
lines changed

Cargo.toml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
[workspace]
2-
members = [
3-
"tap_core",
4-
"tap_aggregator",
5-
"tap_integration_tests"
6-
]
2+
resolver = "2"
3+
members = ["tap_core", "tap_aggregator", "tap_integration_tests"]
74

85
[workspace.package]
9-
version="0.1.0"
6+
version = "0.1.0"
107
edition = "2021"
118
license = "Apache-2.0"

tap_aggregator/src/aggregator.rs

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use tap_core::{
1414
receipt_aggregate_voucher::ReceiptAggregateVoucher, tap_receipt::Receipt,
1515
};
1616

17-
pub async fn check_and_aggregate_receipts(
17+
pub fn check_and_aggregate_receipts(
1818
domain_separator: &Eip712Domain,
1919
receipts: &[EIP712SignedMessage<Receipt>],
2020
previous_rav: Option<EIP712SignedMessage<ReceiptAggregateVoucher>>,
@@ -69,7 +69,7 @@ pub async fn check_and_aggregate_receipts(
6969
let rav = ReceiptAggregateVoucher::aggregate_receipts(allocation_id, receipts, previous_rav)?;
7070

7171
// Sign the rav and return
72-
Ok(EIP712SignedMessage::new(domain_separator, rav, wallet).await?)
72+
Ok(EIP712SignedMessage::new(domain_separator, rav, wallet)?)
7373
}
7474

7575
fn check_signature_is_from_one_of_addresses<M: SolStruct>(
@@ -170,8 +170,8 @@ mod tests {
170170
}
171171

172172
#[rstest]
173-
#[tokio::test]
174-
async fn check_signatures_unique_fail(
173+
#[test]
174+
fn check_signatures_unique_fail(
175175
keys: (LocalWallet, Address),
176176
allocation_ids: Vec<Address>,
177177
domain_separator: Eip712Domain,
@@ -183,7 +183,6 @@ mod tests {
183183
Receipt::new(allocation_ids[0], 42).unwrap(),
184184
&keys.0,
185185
)
186-
.await
187186
.unwrap();
188187
receipts.push(receipt.clone());
189188
receipts.push(receipt);
@@ -193,41 +192,36 @@ mod tests {
193192
}
194193

195194
#[rstest]
196-
#[tokio::test]
197-
async fn check_signatures_unique_ok(
195+
#[test]
196+
fn check_signatures_unique_ok(
198197
keys: (LocalWallet, Address),
199198
allocation_ids: Vec<Address>,
200199
domain_separator: Eip712Domain,
201200
) {
202201
// Create 2 different receipts
203-
let mut receipts = Vec::new();
204-
receipts.push(
202+
let receipts = vec![
205203
EIP712SignedMessage::new(
206204
&domain_separator,
207205
Receipt::new(allocation_ids[0], 42).unwrap(),
208206
&keys.0,
209207
)
210-
.await
211208
.unwrap(),
212-
);
213-
receipts.push(
214209
EIP712SignedMessage::new(
215210
&domain_separator,
216211
Receipt::new(allocation_ids[0], 43).unwrap(),
217212
&keys.0,
218213
)
219-
.await
220214
.unwrap(),
221-
);
215+
];
222216

223217
let res = aggregator::check_signatures_unique(&receipts);
224218
assert!(res.is_ok());
225219
}
226220

227221
#[rstest]
228-
#[tokio::test]
222+
#[test]
229223
/// Test that a receipt with a timestamp greater then the rav timestamp passes
230-
async fn check_receipt_timestamps(
224+
fn check_receipt_timestamps(
231225
keys: (LocalWallet, Address),
232226
allocation_ids: Vec<Address>,
233227
domain_separator: Eip712Domain,
@@ -247,7 +241,6 @@ mod tests {
247241
},
248242
&keys.0,
249243
)
250-
.await
251244
.unwrap(),
252245
);
253246
}
@@ -262,7 +255,6 @@ mod tests {
262255
},
263256
&keys.0,
264257
)
265-
.await
266258
.unwrap();
267259
assert!(aggregator::check_receipt_timestamps(&receipts, Some(&rav)).is_ok());
268260

@@ -277,7 +269,6 @@ mod tests {
277269
},
278270
&keys.0,
279271
)
280-
.await
281272
.unwrap();
282273
assert!(aggregator::check_receipt_timestamps(&receipts, Some(&rav)).is_err());
283274

@@ -292,90 +283,73 @@ mod tests {
292283
},
293284
&keys.0,
294285
)
295-
.await
296286
.unwrap();
297287
assert!(aggregator::check_receipt_timestamps(&receipts, Some(&rav)).is_err());
298288
}
299289

300290
#[rstest]
301-
#[tokio::test]
291+
#[test]
302292
/// Test check_allocation_id with 2 receipts that have the correct allocation id
303293
/// and 1 receipt that has the wrong allocation id
304-
async fn check_allocation_id_fail(
294+
fn check_allocation_id_fail(
305295
keys: (LocalWallet, Address),
306296
allocation_ids: Vec<Address>,
307297
domain_separator: Eip712Domain,
308298
) {
309-
let mut receipts = Vec::new();
310-
receipts.push(
299+
let receipts = vec![
311300
EIP712SignedMessage::new(
312301
&domain_separator,
313302
Receipt::new(allocation_ids[0], 42).unwrap(),
314303
&keys.0,
315304
)
316-
.await
317305
.unwrap(),
318-
);
319-
receipts.push(
320306
EIP712SignedMessage::new(
321307
&domain_separator,
322308
Receipt::new(allocation_ids[0], 43).unwrap(),
323309
&keys.0,
324310
)
325-
.await
326311
.unwrap(),
327-
);
328-
receipts.push(
329312
EIP712SignedMessage::new(
330313
&domain_separator,
331314
Receipt::new(allocation_ids[1], 44).unwrap(),
332315
&keys.0,
333316
)
334-
.await
335317
.unwrap(),
336-
);
318+
];
337319

338320
let res = aggregator::check_allocation_id(&receipts, allocation_ids[0]);
339321

340322
assert!(res.is_err());
341323
}
342324

343325
#[rstest]
344-
#[tokio::test]
326+
#[test]
345327
/// Test check_allocation_id with 3 receipts that have the correct allocation id
346-
async fn check_allocation_id_ok(
328+
fn check_allocation_id_ok(
347329
keys: (LocalWallet, Address),
348330
allocation_ids: Vec<Address>,
349331
domain_separator: Eip712Domain,
350332
) {
351-
let mut receipts = Vec::new();
352-
receipts.push(
333+
let receipts = vec![
353334
EIP712SignedMessage::new(
354335
&domain_separator,
355336
Receipt::new(allocation_ids[0], 42).unwrap(),
356337
&keys.0,
357338
)
358-
.await
359339
.unwrap(),
360-
);
361-
receipts.push(
362340
EIP712SignedMessage::new(
363341
&domain_separator,
364342
Receipt::new(allocation_ids[0], 43).unwrap(),
365343
&keys.0,
366344
)
367-
.await
368345
.unwrap(),
369-
);
370-
receipts.push(
371346
EIP712SignedMessage::new(
372347
&domain_separator,
373348
Receipt::new(allocation_ids[0], 44).unwrap(),
374349
&keys.0,
375350
)
376-
.await
377351
.unwrap(),
378-
);
352+
];
379353

380354
let res = aggregator::check_allocation_id(&receipts, allocation_ids[0]);
381355

tap_aggregator/src/server.rs

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ use alloy_primitives::Address;
77
use alloy_sol_types::Eip712Domain;
88
use anyhow::Result;
99
use ethers_signers::LocalWallet;
10-
use jsonrpsee::{
11-
proc_macros::rpc,
12-
server::ServerBuilder,
13-
{core::async_trait, server::ServerHandle},
14-
};
10+
use jsonrpsee::{proc_macros::rpc, server::ServerBuilder, server::ServerHandle};
1511
use lazy_static::lazy_static;
1612
use prometheus::{register_counter, register_int_counter, Counter, IntCounter};
1713

@@ -82,12 +78,12 @@ lazy_static! {
8278
pub trait Rpc {
8379
/// Returns the versions of the TAP JSON-RPC API implemented by this server.
8480
#[method(name = "api_versions")]
85-
async fn api_versions(&self) -> JsonRpcResult<TapRpcApiVersionsInfo>;
81+
fn api_versions(&self) -> JsonRpcResult<TapRpcApiVersionsInfo>;
8682

8783
/// Aggregates the given receipts into a receipt aggregate voucher.
8884
/// Returns an error if the user expected API version is not supported.
8985
#[method(name = "aggregate_receipts")]
90-
async fn aggregate_receipts(
86+
fn aggregate_receipts(
9187
&self,
9288
api_version: String,
9389
receipts: Vec<EIP712SignedMessage<Receipt>>,
@@ -131,7 +127,7 @@ fn check_api_version_deprecation(api_version: &TapRpcApiVersion) -> Option<JsonR
131127
}
132128
}
133129

134-
async fn aggregate_receipts_(
130+
fn aggregate_receipts_(
135131
api_version: String,
136132
wallet: &LocalWallet,
137133
accepted_addresses: &HashSet<Address>,
@@ -156,16 +152,13 @@ async fn aggregate_receipts_(
156152
}
157153

158154
let res = match api_version {
159-
TapRpcApiVersion::V0_0 => {
160-
check_and_aggregate_receipts(
161-
domain_separator,
162-
&receipts,
163-
previous_rav,
164-
wallet,
165-
accepted_addresses,
166-
)
167-
.await
168-
}
155+
TapRpcApiVersion::V0_0 => check_and_aggregate_receipts(
156+
domain_separator,
157+
&receipts,
158+
previous_rav,
159+
wallet,
160+
accepted_addresses,
161+
),
169162
};
170163

171164
// Handle aggregation error
@@ -179,13 +172,12 @@ async fn aggregate_receipts_(
179172
}
180173
}
181174

182-
#[async_trait]
183175
impl RpcServer for RpcImpl {
184-
async fn api_versions(&self) -> JsonRpcResult<TapRpcApiVersionsInfo> {
176+
fn api_versions(&self) -> JsonRpcResult<TapRpcApiVersionsInfo> {
185177
Ok(JsonRpcResponse::ok(tap_rpc_api_versions_info()))
186178
}
187179

188-
async fn aggregate_receipts(
180+
fn aggregate_receipts(
189181
&self,
190182
api_version: String,
191183
receipts: Vec<EIP712SignedMessage<Receipt>>,
@@ -202,9 +194,7 @@ impl RpcServer for RpcImpl {
202194
&self.domain_separator,
203195
receipts,
204196
previous_rav,
205-
)
206-
.await
207-
{
197+
) {
208198
Ok(res) => {
209199
TOTAL_GRT_AGGREGATED.inc_by(receipts_grt as f64);
210200
TOTAL_AGGREGATED_RECEIPTS.inc_by(receipts_count);
@@ -410,7 +400,6 @@ mod tests {
410400
Receipt::new(allocation_ids[0], value).unwrap(),
411401
&all_wallets.choose(&mut rng).unwrap().wallet,
412402
)
413-
.await
414403
.unwrap(),
415404
);
416405
}
@@ -492,7 +481,6 @@ mod tests {
492481
Receipt::new(allocation_ids[0], value).unwrap(),
493482
&all_wallets.choose(&mut rng).unwrap().wallet,
494483
)
495-
.await
496484
.unwrap(),
497485
);
498486
}
@@ -509,7 +497,6 @@ mod tests {
509497
prev_rav,
510498
&all_wallets.choose(&mut rng).unwrap().wallet,
511499
)
512-
.await
513500
.unwrap();
514501

515502
// Create new RAV from last half of receipts and prev_rav through the JSON-RPC server
@@ -569,7 +556,6 @@ mod tests {
569556
Receipt::new(allocation_ids[0], 42).unwrap(),
570557
&keys_main.wallet,
571558
)
572-
.await
573559
.unwrap()];
574560

575561
// Skipping receipts validation in this test, aggregate_receipts assumes receipts are valid.
@@ -665,7 +651,6 @@ mod tests {
665651
Receipt::new(allocation_ids[0], u128::MAX / 1000).unwrap(),
666652
&keys_main.wallet,
667653
)
668-
.await
669654
.unwrap(),
670655
);
671656
}

tap_core/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ description = "Core Timeline Aggregation Protocol library: a fast, efficient and
77

88
[dependencies]
99
rand_core = "0.6.4"
10-
serde = { version = "1.0", features = ["derive"] }
10+
serde = { version = "1.0", features = ["derive", "rc"] }
1111
rand = "0.8.5"
1212
thiserror = "1.0.38"
1313
ethereum-types = { version = "0.14.1" }
@@ -24,10 +24,11 @@ strum = "0.24.1"
2424
strum_macros = "0.24.3"
2525
async-trait = "0.1.72"
2626
tokio = { version = "1.29.1", features = ["macros", "rt-multi-thread"] }
27+
typetag = "0.2.14"
28+
futures = "0.3.17"
2729

2830
[dev-dependencies]
2931
criterion = { version = "0.5", features = ["async_std"] }
30-
futures = "0.3.17"
3132

3233

3334
[features]

0 commit comments

Comments
 (0)