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

feat: add tap v2 #254

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
336 changes: 288 additions & 48 deletions tap_aggregator/src/aggregator.rs

Large diffs are not rendered by default.

72 changes: 49 additions & 23 deletions tap_aggregator/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,12 @@ pub async fn run_server(
#[allow(clippy::too_many_arguments)]
mod tests {
use std::collections::HashSet;
use std::str::FromStr;

use alloy::{dyn_abi::Eip712Domain, primitives::Address, signers::local::PrivateKeySigner};
use alloy::{
dyn_abi::Eip712Domain,
primitives::{address, Address},
signers::local::PrivateKeySigner,
};
use jsonrpsee::{core::client::ClientT, http_client::HttpClientBuilder, rpc_params};
use rand::prelude::*;
use rand::seq::SliceRandom;
Expand All @@ -268,13 +271,18 @@ mod tests {
}

#[fixture]
fn allocation_ids() -> Vec<Address> {
vec![
Address::from_str("0xabababababababababababababababababababab").unwrap(),
Address::from_str("0xdeaddeaddeaddeaddeaddeaddeaddeaddeaddead").unwrap(),
Address::from_str("0xbeefbeefbeefbeefbeefbeefbeefbeefbeefbeef").unwrap(),
Address::from_str("0x1234567890abcdef1234567890abcdef12345678").unwrap(),
]
fn payer() -> Address {
address!("abababababababababababababababababababab")
}

#[fixture]
fn data_service() -> Address {
address!("deaddeaddeaddeaddeaddeaddeaddeaddeaddead")
}

#[fixture]
fn service_provider() -> Address {
address!("beefbeefbeefbeefbeefbeefbeefbeefbeefbeef")
}

#[fixture]
Expand Down Expand Up @@ -343,7 +351,9 @@ mod tests {
http_request_size_limit: u32,
http_response_size_limit: u32,
http_max_concurrent_connections: u32,
allocation_ids: Vec<Address>,
payer: Address,
data_service: Address,
service_provider: Address,
#[case] values: Vec<u128>,
#[values("0.0")] api_version: &str,
#[values(0, 1, 2)] random_seed: u64,
Expand Down Expand Up @@ -382,7 +392,7 @@ mod tests {
receipts.push(
EIP712SignedMessage::new(
&domain_separator,
Receipt::new(allocation_ids[0], value).unwrap(),
Receipt::new(payer, data_service, service_provider, value).unwrap(),
&all_wallets.choose(&mut rng).unwrap().wallet,
)
.unwrap(),
Expand All @@ -401,11 +411,18 @@ mod tests {

let remote_rav = res.data;

let local_rav =
ReceiptAggregateVoucher::aggregate_receipts(allocation_ids[0], &receipts, None)
.unwrap();
let local_rav = ReceiptAggregateVoucher::aggregate_receipts(
payer,
data_service,
service_provider,
&receipts,
None,
)
.unwrap();

assert!(remote_rav.message.allocationId == local_rav.allocationId);
assert!(remote_rav.message.payer == local_rav.payer);
assert!(remote_rav.message.dataService == local_rav.dataService);
assert!(remote_rav.message.serviceProvider == local_rav.serviceProvider);
assert!(remote_rav.message.timestampNs == local_rav.timestampNs);
assert!(remote_rav.message.valueAggregate == local_rav.valueAggregate);

Expand All @@ -424,7 +441,9 @@ mod tests {
http_request_size_limit: u32,
http_response_size_limit: u32,
http_max_concurrent_connections: u32,
allocation_ids: Vec<Address>,
payer: Address,
data_service: Address,
service_provider: Address,
#[case] values: Vec<u128>,
#[values("0.0")] api_version: &str,
#[values(0, 1, 2, 3, 4)] random_seed: u64,
Expand Down Expand Up @@ -463,7 +482,7 @@ mod tests {
receipts.push(
EIP712SignedMessage::new(
&domain_separator,
Receipt::new(allocation_ids[0], value).unwrap(),
Receipt::new(payer, data_service, service_provider, value).unwrap(),
&all_wallets.choose(&mut rng).unwrap().wallet,
)
.unwrap(),
Expand All @@ -472,7 +491,9 @@ mod tests {

// Create previous RAV from first half of receipts locally
let prev_rav = ReceiptAggregateVoucher::aggregate_receipts(
allocation_ids[0],
payer,
data_service,
service_provider,
&receipts[0..receipts.len() / 2],
None,
)
Expand Down Expand Up @@ -512,7 +533,9 @@ mod tests {
http_request_size_limit: u32,
http_response_size_limit: u32,
http_max_concurrent_connections: u32,
allocation_ids: Vec<Address>,
payer: Address,
data_service: Address,
service_provider: Address,
) {
// The keys that will be used to sign the new RAVs
let keys_main = keys();
Expand All @@ -538,7 +561,7 @@ mod tests {
// Create receipts
let receipts = vec![EIP712SignedMessage::new(
&domain_separator,
Receipt::new(allocation_ids[0], 42).unwrap(),
Receipt::new(payer, data_service, service_provider, 42).unwrap(),
&keys_main.wallet,
)
.unwrap()];
Expand Down Expand Up @@ -594,15 +617,17 @@ mod tests {
domain_separator: Eip712Domain,
http_response_size_limit: u32,
http_max_concurrent_connections: u32,
allocation_ids: Vec<Address>,
payer: Address,
data_service: Address,
service_provider: Address,
#[values("0.0")] api_version: &str,
) {
// The keys that will be used to sign the new RAVs
let keys_main = keys();

// Set the request byte size limit to a value that easily triggers the HTTP 413
// error.
let http_request_size_limit = 100 * 1024;
let http_request_size_limit = 120 * 1024;

// Number of receipts that is just above the number that would fit within the
// request size limit. This value is hard-coded here because it supports the
Expand Down Expand Up @@ -633,7 +658,7 @@ mod tests {
receipts.push(
EIP712SignedMessage::new(
&domain_separator,
Receipt::new(allocation_ids[0], u128::MAX / 1000).unwrap(),
Receipt::new(payer, data_service, service_provider, u128::MAX / 1000).unwrap(),
&keys_main.wallet,
)
.unwrap(),
Expand All @@ -656,6 +681,7 @@ mod tests {
),
)
.await;
println!("{res:?}");
assert!(res.is_ok());

// Create RAV through the JSON-RPC server.
Expand Down
47 changes: 39 additions & 8 deletions tap_core/benches/timeline_aggretion_protocol_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ use tap_core::{

pub fn create_and_sign_receipt(
domain_separator: &Eip712Domain,
allocation_id: Address,
payer: Address,
data_service: Address,
service_provider: Address,
value: u128,
wallet: &PrivateKeySigner,
) -> EIP712SignedMessage<Receipt> {
EIP712SignedMessage::new(
domain_separator,
Receipt::new(allocation_id, value).unwrap(),
Receipt::new(payer, data_service, service_provider, value).unwrap(),
wallet,
)
.unwrap()
Expand All @@ -40,21 +42,32 @@ pub fn criterion_benchmark(c: &mut Criterion) {
let address = wallet.address();

// Arbitrary values wrapped in black box to avoid compiler optimizing them out
let allocation_id = Address::from_str("0xabababababababababababababababababababab").unwrap();
let payer = Address::from_str("0xabababababababababababababababababababab").unwrap();
let data_service = Address::from_str("0xabababababababababababababababababababab").unwrap();
let service_provider = Address::from_str("0xabababababababababababababababababababab").unwrap();
let value = 12345u128;

c.bench_function("Create Receipt", |b| {
b.iter(|| {
create_and_sign_receipt(
black_box(&domain_seperator),
black_box(allocation_id),
black_box(payer),
black_box(data_service),
black_box(service_provider),
black_box(value),
black_box(&wallet),
)
})
});

let receipt = create_and_sign_receipt(&domain_seperator, allocation_id, value, &wallet);
let receipt = create_and_sign_receipt(
&domain_seperator,
payer,
data_service,
service_provider,
value,
&wallet,
);

c.bench_function("Validate Receipt", |b| {
b.iter(|| {
Expand All @@ -68,15 +81,26 @@ pub fn criterion_benchmark(c: &mut Criterion) {

for log_number_of_receipts in 10..30 {
let receipts = (0..2 ^ log_number_of_receipts)
.map(|_| create_and_sign_receipt(&domain_seperator, allocation_id, value, &wallet))
.map(|_| {
create_and_sign_receipt(
&domain_seperator,
payer,
data_service,
service_provider,
value,
&wallet,
)
})
.collect::<Vec<_>>();

rav_group.bench_function(
format!("Create RAV w/ 2^{} receipt's", log_number_of_receipts),
|b| {
b.iter(|| {
ReceiptAggregateVoucher::aggregate_receipts(
black_box(allocation_id),
black_box(payer),
black_box(data_service),
black_box(service_provider),
black_box(&receipts),
black_box(None),
)
Expand All @@ -86,7 +110,14 @@ pub fn criterion_benchmark(c: &mut Criterion) {

let signed_rav = EIP712SignedMessage::new(
&domain_seperator,
ReceiptAggregateVoucher::aggregate_receipts(allocation_id, &receipts, None).unwrap(),
ReceiptAggregateVoucher::aggregate_receipts(
payer,
data_service,
service_provider,
&receipts,
None,
)
.unwrap(),
&wallet,
)
.unwrap();
Expand Down
14 changes: 12 additions & 2 deletions tap_core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,18 @@ pub enum Error {
NoValidReceiptsForRAVRequest,

/// Error when the previous RAV allocation id does not match the allocation id from the new receipt
#[error("Previous RAV allocation id ({prev_id}) doesn't match the allocation id from the new receipt ({new_id}).")]
RavAllocationIdMismatch { prev_id: String, new_id: String },
#[error(
"Previous RAV payer ({prev_id}) doesn't match the payer from the new receipt ({new_id})."
)]
RavPayerMismatch { prev_id: String, new_id: String },

/// Error when the previous RAV data service does not match the data service from the new receipt
#[error("Previous RAV data_service ({prev_id}) doesn't match the data_service from the new receipt ({new_id}).")]
RavDataServiceMismatch { prev_id: String, new_id: String },

/// Error when the previous RAV service provider does not match the service provider from the new receipt
#[error("Previous RAV service_provider ({prev_id}) doesn't match the service_provider from the new receipt ({new_id}).")]
RavServiceProviderMismatch { prev_id: String, new_id: String },

/// Error when all receipts do not have the same allocation id
///
Expand Down
Loading
Loading