Skip to content

Commit

Permalink
Merge pull request #207 from semiotic-ai/gusinacio/check-trait-refactor
Browse files Browse the repository at this point in the history
Checks refactor
  • Loading branch information
gusinacio committed Mar 7, 2024
2 parents e16bc1e + c5b890a commit b577d19
Show file tree
Hide file tree
Showing 35 changed files with 1,595 additions and 2,606 deletions.
9 changes: 3 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
[workspace]
members = [
"tap_core",
"tap_aggregator",
"tap_integration_tests"
]
resolver = "2"
members = ["tap_core", "tap_aggregator", "tap_integration_tests"]

[workspace.package]
version="0.1.0"
version = "0.1.0"
edition = "2021"
license = "Apache-2.0"
62 changes: 18 additions & 44 deletions tap_aggregator/src/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use tap_core::{
receipt_aggregate_voucher::ReceiptAggregateVoucher, tap_receipt::Receipt,
};

pub async fn check_and_aggregate_receipts(
pub fn check_and_aggregate_receipts(
domain_separator: &Eip712Domain,
receipts: &[EIP712SignedMessage<Receipt>],
previous_rav: Option<EIP712SignedMessage<ReceiptAggregateVoucher>>,
Expand Down Expand Up @@ -69,7 +69,7 @@ pub async fn check_and_aggregate_receipts(
let rav = ReceiptAggregateVoucher::aggregate_receipts(allocation_id, receipts, previous_rav)?;

// Sign the rav and return
Ok(EIP712SignedMessage::new(domain_separator, rav, wallet).await?)
Ok(EIP712SignedMessage::new(domain_separator, rav, wallet)?)
}

fn check_signature_is_from_one_of_addresses<M: SolStruct>(
Expand Down Expand Up @@ -170,8 +170,8 @@ mod tests {
}

#[rstest]
#[tokio::test]
async fn check_signatures_unique_fail(
#[test]
fn check_signatures_unique_fail(
keys: (LocalWallet, Address),
allocation_ids: Vec<Address>,
domain_separator: Eip712Domain,
Expand All @@ -183,7 +183,6 @@ mod tests {
Receipt::new(allocation_ids[0], 42).unwrap(),
&keys.0,
)
.await
.unwrap();
receipts.push(receipt.clone());
receipts.push(receipt);
Expand All @@ -193,41 +192,36 @@ mod tests {
}

#[rstest]
#[tokio::test]
async fn check_signatures_unique_ok(
#[test]
fn check_signatures_unique_ok(
keys: (LocalWallet, Address),
allocation_ids: Vec<Address>,
domain_separator: Eip712Domain,
) {
// Create 2 different receipts
let mut receipts = Vec::new();
receipts.push(
let receipts = vec![
EIP712SignedMessage::new(
&domain_separator,
Receipt::new(allocation_ids[0], 42).unwrap(),
&keys.0,
)
.await
.unwrap(),
);
receipts.push(
EIP712SignedMessage::new(
&domain_separator,
Receipt::new(allocation_ids[0], 43).unwrap(),
&keys.0,
)
.await
.unwrap(),
);
];

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

#[rstest]
#[tokio::test]
#[test]
/// Test that a receipt with a timestamp greater then the rav timestamp passes
async fn check_receipt_timestamps(
fn check_receipt_timestamps(
keys: (LocalWallet, Address),
allocation_ids: Vec<Address>,
domain_separator: Eip712Domain,
Expand All @@ -247,7 +241,6 @@ mod tests {
},
&keys.0,
)
.await
.unwrap(),
);
}
Expand All @@ -262,7 +255,6 @@ mod tests {
},
&keys.0,
)
.await
.unwrap();
assert!(aggregator::check_receipt_timestamps(&receipts, Some(&rav)).is_ok());

Expand All @@ -277,7 +269,6 @@ mod tests {
},
&keys.0,
)
.await
.unwrap();
assert!(aggregator::check_receipt_timestamps(&receipts, Some(&rav)).is_err());

Expand All @@ -292,90 +283,73 @@ mod tests {
},
&keys.0,
)
.await
.unwrap();
assert!(aggregator::check_receipt_timestamps(&receipts, Some(&rav)).is_err());
}

#[rstest]
#[tokio::test]
#[test]
/// Test check_allocation_id with 2 receipts that have the correct allocation id
/// and 1 receipt that has the wrong allocation id
async fn check_allocation_id_fail(
fn check_allocation_id_fail(
keys: (LocalWallet, Address),
allocation_ids: Vec<Address>,
domain_separator: Eip712Domain,
) {
let mut receipts = Vec::new();
receipts.push(
let receipts = vec![
EIP712SignedMessage::new(
&domain_separator,
Receipt::new(allocation_ids[0], 42).unwrap(),
&keys.0,
)
.await
.unwrap(),
);
receipts.push(
EIP712SignedMessage::new(
&domain_separator,
Receipt::new(allocation_ids[0], 43).unwrap(),
&keys.0,
)
.await
.unwrap(),
);
receipts.push(
EIP712SignedMessage::new(
&domain_separator,
Receipt::new(allocation_ids[1], 44).unwrap(),
&keys.0,
)
.await
.unwrap(),
);
];

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

assert!(res.is_err());
}

#[rstest]
#[tokio::test]
#[test]
/// Test check_allocation_id with 3 receipts that have the correct allocation id
async fn check_allocation_id_ok(
fn check_allocation_id_ok(
keys: (LocalWallet, Address),
allocation_ids: Vec<Address>,
domain_separator: Eip712Domain,
) {
let mut receipts = Vec::new();
receipts.push(
let receipts = vec![
EIP712SignedMessage::new(
&domain_separator,
Receipt::new(allocation_ids[0], 42).unwrap(),
&keys.0,
)
.await
.unwrap(),
);
receipts.push(
EIP712SignedMessage::new(
&domain_separator,
Receipt::new(allocation_ids[0], 43).unwrap(),
&keys.0,
)
.await
.unwrap(),
);
receipts.push(
EIP712SignedMessage::new(
&domain_separator,
Receipt::new(allocation_ids[0], 44).unwrap(),
&keys.0,
)
.await
.unwrap(),
);
];

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

Expand Down
43 changes: 14 additions & 29 deletions tap_aggregator/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ use alloy_primitives::Address;
use alloy_sol_types::Eip712Domain;
use anyhow::Result;
use ethers_signers::LocalWallet;
use jsonrpsee::{
proc_macros::rpc,
server::ServerBuilder,
{core::async_trait, server::ServerHandle},
};
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 @@ -82,12 +78,12 @@ lazy_static! {
pub trait Rpc {
/// Returns the versions of the TAP JSON-RPC API implemented by this server.
#[method(name = "api_versions")]
async fn api_versions(&self) -> JsonRpcResult<TapRpcApiVersionsInfo>;
fn api_versions(&self) -> JsonRpcResult<TapRpcApiVersionsInfo>;

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

async fn aggregate_receipts_(
fn aggregate_receipts_(
api_version: String,
wallet: &LocalWallet,
accepted_addresses: &HashSet<Address>,
Expand All @@ -156,16 +152,13 @@ async fn aggregate_receipts_(
}

let res = match api_version {
TapRpcApiVersion::V0_0 => {
check_and_aggregate_receipts(
domain_separator,
&receipts,
previous_rav,
wallet,
accepted_addresses,
)
.await
}
TapRpcApiVersion::V0_0 => check_and_aggregate_receipts(
domain_separator,
&receipts,
previous_rav,
wallet,
accepted_addresses,
),
};

// Handle aggregation error
Expand All @@ -179,13 +172,12 @@ async fn aggregate_receipts_(
}
}

#[async_trait]
impl RpcServer for RpcImpl {
async fn api_versions(&self) -> JsonRpcResult<TapRpcApiVersionsInfo> {
fn api_versions(&self) -> JsonRpcResult<TapRpcApiVersionsInfo> {
Ok(JsonRpcResponse::ok(tap_rpc_api_versions_info()))
}

async fn aggregate_receipts(
fn aggregate_receipts(
&self,
api_version: String,
receipts: Vec<EIP712SignedMessage<Receipt>>,
Expand All @@ -202,9 +194,7 @@ impl RpcServer for RpcImpl {
&self.domain_separator,
receipts,
previous_rav,
)
.await
{
) {
Ok(res) => {
TOTAL_GRT_AGGREGATED.inc_by(receipts_grt as f64);
TOTAL_AGGREGATED_RECEIPTS.inc_by(receipts_count);
Expand Down Expand Up @@ -410,7 +400,6 @@ mod tests {
Receipt::new(allocation_ids[0], value).unwrap(),
&all_wallets.choose(&mut rng).unwrap().wallet,
)
.await
.unwrap(),
);
}
Expand Down Expand Up @@ -492,7 +481,6 @@ mod tests {
Receipt::new(allocation_ids[0], value).unwrap(),
&all_wallets.choose(&mut rng).unwrap().wallet,
)
.await
.unwrap(),
);
}
Expand All @@ -509,7 +497,6 @@ mod tests {
prev_rav,
&all_wallets.choose(&mut rng).unwrap().wallet,
)
.await
.unwrap();

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

// Skipping receipts validation in this test, aggregate_receipts assumes receipts are valid.
Expand Down Expand Up @@ -665,7 +651,6 @@ mod tests {
Receipt::new(allocation_ids[0], u128::MAX / 1000).unwrap(),
&keys_main.wallet,
)
.await
.unwrap(),
);
}
Expand Down
5 changes: 3 additions & 2 deletions tap_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ description = "Core Timeline Aggregation Protocol library: a fast, efficient and

[dependencies]
rand_core = "0.6.4"
serde = { version = "1.0", features = ["derive"] }
serde = { version = "1.0", features = ["derive", "rc"] }
rand = "0.8.5"
thiserror = "1.0.38"
ethereum-types = { version = "0.14.1" }
Expand All @@ -24,10 +24,11 @@ strum = "0.24.1"
strum_macros = "0.24.3"
async-trait = "0.1.72"
tokio = { version = "1.29.1", features = ["macros", "rt-multi-thread"] }
typetag = "0.2.14"
futures = "0.3.17"

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


[features]
Expand Down
Loading

0 comments on commit b577d19

Please sign in to comment.