Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checks refactor #207

Merged
merged 32 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
ec5bad8
refactor: add check trait
gusinacio Dec 21, 2023
0a89801
refactor: use check trait instead of receipt check enum
gusinacio Dec 21, 2023
1266c4e
refactor: remove receipt checks adapter
gusinacio Dec 21, 2023
b778f84
refactor: fix rebase conflicts
gusinacio Jan 15, 2024
5da3e9c
refactor: use check batch inside check trait
gusinacio Jan 15, 2024
f7e6c15
refactor: use mutable reference for checkingchecks
gusinacio Mar 4, 2024
3de3cc7
refactor: add rav signature checker
gusinacio Mar 4, 2024
bfb1eb0
fix: update timestamp check only after rav update
gusinacio Mar 4, 2024
06cf24c
fix: compile tests
gusinacio Mar 4, 2024
b32d969
refactor: remove unneeded async function
gusinacio Mar 4, 2024
a970a4c
test: add sender_id to indexer_mock
gusinacio Mar 4, 2024
ab0ddb5
refactor: remove checks mock adapter, fix warnings, update tests
gusinacio Mar 4, 2024
8c95954
fix: missing merge conflicts
gusinacio Mar 4, 2024
0c7bc1c
fix: rebase main
gusinacio Mar 4, 2024
e8980e6
test: add mock checks
gusinacio Mar 4, 2024
3d6ec98
style: remove unused parameters
gusinacio Mar 4, 2024
3391b45
style: clippy and fmt
gusinacio Mar 4, 2024
9aa467a
refactor: remove escrow adapter mock
gusinacio Mar 4, 2024
0dfbf97
refactor: remove rav storage adapter
gusinacio Mar 4, 2024
e782b05
refactor: remove receipt storage adapter mock
gusinacio Mar 4, 2024
09a15f1
refactor: remove auditor executor mock
gusinacio Mar 4, 2024
a992181
chore: add license
gusinacio Mar 5, 2024
bc86d16
style: fix fmt and clippy
gusinacio Mar 5, 2024
568144f
test: fix manager and received receipt tests
gusinacio Mar 5, 2024
2eca6a1
test: fix unused variables
gusinacio Mar 5, 2024
f763410
test: convert checks to mock
gusinacio Mar 5, 2024
9523309
test: fix integration tests
gusinacio Mar 5, 2024
ace9c00
test: use static slice for prices
gusinacio Mar 5, 2024
e6aec70
test: remove unneeded test
gusinacio Mar 5, 2024
0a3b983
refactor!: remove timestamp check from manager
gusinacio Mar 5, 2024
797d178
chore: update futures dependency
gusinacio Mar 6, 2024
c5b890a
chore: remove unused comments
gusinacio Mar 7, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading