Skip to content
Merged
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
4 changes: 2 additions & 2 deletions tap_aggregator/proto/v2.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package tap_aggregator.v2;
import "uint128.proto";

message Receipt {
bytes allocation_id = 1;
bytes collection_id = 1;
bytes payer = 2;
bytes data_service = 3;
bytes service_provider = 4;
Expand All @@ -22,7 +22,7 @@ message SignedReceipt {
}

message ReceiptAggregateVoucher {
bytes allocation_id = 1;
bytes collection_id = 1;
bytes payer = 2;
bytes data_service = 3;
bytes service_provider = 4;
Expand Down
97 changes: 53 additions & 44 deletions tap_aggregator/src/aggregator/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use rayon::prelude::*;
use tap_core::{receipt::WithUniqueId, signed_message::Eip712SignedMessage};
use tap_graph::v2::{Receipt, ReceiptAggregateVoucher};
use thegraph_core::alloy::{
dyn_abi::Eip712Domain, primitives::Address, signers::local::PrivateKeySigner,
dyn_abi::Eip712Domain,
primitives::{Address, FixedBytes},
signers::local::PrivateKeySigner,
sol_types::SolStruct,
};

Expand Down Expand Up @@ -39,65 +41,65 @@ pub fn check_and_aggregate_receipts(
check_receipt_timestamps(receipts, previous_rav.as_ref())?;

// Get the allocation id from the first receipt, return error if there are no receipts
let (allocation_id, payer, data_service, service_provider) = match receipts.first() {
let (collection_id, payer, data_service, service_provider) = match receipts.first() {
Some(receipt) => (
receipt.message.allocation_id,
receipt.message.collection_id,
receipt.message.payer,
receipt.message.data_service,
receipt.message.service_provider,
),
None => return Err(tap_core::Error::NoValidReceiptsForRavRequest.into()),
};

// Check that the receipts all have the same allocation id
check_allocation_id(
// Check that the receipts all have the same collection id
check_collection_id(
receipts,
allocation_id,
collection_id,
payer,
data_service,
service_provider,
)?;

// Check that the rav has the correct allocation id
if let Some(previous_rav) = &previous_rav {
let prev_id = previous_rav.message.allocationId;
let prev_id = previous_rav.message.collectionId;
let prev_payer = previous_rav.message.payer;
let prev_data_service = previous_rav.message.dataService;
let prev_service_provider = previous_rav.message.serviceProvider;
if prev_id != allocation_id {
if prev_id != collection_id {
return Err(tap_core::Error::RavAllocationIdMismatch {
prev_id: format!("{prev_id:#X}"),
new_id: format!("{allocation_id:#X}"),
new_id: format!("{collection_id:#X}"),
}
.into());
}
if prev_payer != payer {
return Err(tap_core::Error::RavAllocationIdMismatch {
prev_id: format!("{prev_id:#X}"),
new_id: format!("{allocation_id:#X}"),
new_id: format!("{collection_id:#X}"),
}
.into());
}

if prev_data_service != data_service {
return Err(tap_core::Error::RavAllocationIdMismatch {
prev_id: format!("{prev_id:#X}"),
new_id: format!("{allocation_id:#X}"),
new_id: format!("{collection_id:#X}"),
}
.into());
}
if prev_service_provider != service_provider {
return Err(tap_core::Error::RavAllocationIdMismatch {
prev_id: format!("{prev_id:#X}"),
new_id: format!("{allocation_id:#X}"),
new_id: format!("{collection_id:#X}"),
}
.into());
}
}

// Aggregate the receipts
let rav = ReceiptAggregateVoucher::aggregate_receipts(
allocation_id,
collection_id,
payer,
data_service,
service_provider,
Expand All @@ -123,16 +125,16 @@ fn check_signature_is_from_one_of_addresses<M: SolStruct>(
Ok(())
}

fn check_allocation_id(
fn check_collection_id(
receipts: &[Eip712SignedMessage<Receipt>],
allocation_id: Address,
collection_id: FixedBytes<32>,
payer: Address,
data_service: Address,
service_provider: Address,
) -> Result<()> {
for receipt in receipts.iter() {
let receipt = &receipt.message;
if receipt.allocation_id != allocation_id {
if receipt.collection_id != collection_id {
return Err(tap_core::Error::RavAllocationIdNotUniform.into());
}
if receipt.payer != payer {
Expand Down Expand Up @@ -190,7 +192,7 @@ mod tests {
use tap_graph::v2::{Receipt, ReceiptAggregateVoucher};
use thegraph_core::alloy::{
dyn_abi::Eip712Domain,
primitives::{address, Address, Bytes},
primitives::{address, fixed_bytes, Address, Bytes, FixedBytes},
signers::local::PrivateKeySigner,
};

Expand All @@ -202,8 +204,8 @@ mod tests {
}

#[fixture]
fn allocation_id() -> Address {
address!("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef")
fn collection_id() -> FixedBytes<32> {
fixed_bytes!("deaddeaddeaddeaddeaddeaddeaddeaddeaddeaddeaddeaddeaddeaddeaddead")
}

#[fixture]
Expand All @@ -222,8 +224,8 @@ mod tests {
}

#[fixture]
fn other_address() -> Address {
address!("1234567890abcdef1234567890abcdef12345678")
fn other_collection_id() -> FixedBytes<32> {
fixed_bytes!("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef")
}
#[fixture]
fn domain_separator() -> Eip712Domain {
Expand All @@ -234,7 +236,7 @@ mod tests {
#[test]
fn check_signatures_unique_fail(
keys: (PrivateKeySigner, Address),
allocation_id: Address,
collection_id: FixedBytes<32>,
payer: Address,
data_service: Address,
service_provider: Address,
Expand All @@ -244,7 +246,7 @@ mod tests {
let mut receipts = Vec::new();
let receipt = Eip712SignedMessage::new(
&domain_separator,
Receipt::new(allocation_id, payer, data_service, service_provider, 42).unwrap(),
Receipt::new(collection_id, payer, data_service, service_provider, 42).unwrap(),
&keys.0,
)
.unwrap();
Expand All @@ -259,7 +261,7 @@ mod tests {
#[test]
fn check_signatures_unique_ok(
keys: (PrivateKeySigner, Address),
allocation_id: Address,
collection_id: FixedBytes<32>,
payer: Address,
data_service: Address,
service_provider: Address,
Expand All @@ -269,13 +271,13 @@ mod tests {
let receipts = vec![
Eip712SignedMessage::new(
&domain_separator,
Receipt::new(allocation_id, payer, data_service, service_provider, 42).unwrap(),
Receipt::new(collection_id, payer, data_service, service_provider, 42).unwrap(),
&keys.0,
)
.unwrap(),
Eip712SignedMessage::new(
&domain_separator,
Receipt::new(allocation_id, payer, data_service, service_provider, 42).unwrap(),
Receipt::new(collection_id, payer, data_service, service_provider, 42).unwrap(),
&keys.0,
)
.unwrap(),
Expand All @@ -290,7 +292,7 @@ mod tests {
/// Test that a receipt with a timestamp greater than the rav timestamp passes
fn check_receipt_timestamps(
keys: (PrivateKeySigner, Address),
allocation_id: Address,
collection_id: FixedBytes<32>,
payer: Address,
data_service: Address,
service_provider: Address,
Expand All @@ -304,7 +306,7 @@ mod tests {
Eip712SignedMessage::new(
&domain_separator,
Receipt {
allocation_id,
collection_id,
payer,
data_service,
service_provider,
Expand All @@ -322,7 +324,7 @@ mod tests {
let rav = Eip712SignedMessage::new(
&domain_separator,
ReceiptAggregateVoucher {
allocationId: allocation_id,
collectionId: collection_id,
dataService: data_service,
payer,
serviceProvider: service_provider,
Expand All @@ -340,7 +342,7 @@ mod tests {
let rav = Eip712SignedMessage::new(
&domain_separator,
ReceiptAggregateVoucher {
allocationId: allocation_id,
collectionId: collection_id,
dataService: data_service,
payer,
serviceProvider: service_provider,
Expand All @@ -358,7 +360,7 @@ mod tests {
let rav = Eip712SignedMessage::new(
&domain_separator,
ReceiptAggregateVoucher {
allocationId: allocation_id,
collectionId: collection_id,
dataService: data_service,
payer,
serviceProvider: service_provider,
Expand All @@ -378,37 +380,44 @@ mod tests {
/// and 1 receipt that has the wrong allocation id
fn check_allocation_id_fail(
keys: (PrivateKeySigner, Address),
allocation_id: Address,
collection_id: FixedBytes<32>,
payer: Address,
data_service: Address,
service_provider: Address,
other_address: Address,
other_collection_id: FixedBytes<32>,
domain_separator: Eip712Domain,
) {
let receipts = vec![
Eip712SignedMessage::new(
&domain_separator,
Receipt::new(allocation_id, payer, data_service, service_provider, 42).unwrap(),
Receipt::new(collection_id, payer, data_service, service_provider, 42).unwrap(),
&keys.0,
)
.unwrap(),
Eip712SignedMessage::new(
&domain_separator,
Receipt::new(allocation_id, payer, data_service, service_provider, 43).unwrap(),
Receipt::new(collection_id, payer, data_service, service_provider, 43).unwrap(),
&keys.0,
)
.unwrap(),
Eip712SignedMessage::new(
&domain_separator,
Receipt::new(other_address, payer, data_service, service_provider, 44).unwrap(),
Receipt::new(
other_collection_id,
payer,
data_service,
service_provider,
44,
)
.unwrap(),
&keys.0,
)
.unwrap(),
];

let res = super::check_allocation_id(
let res = super::check_collection_id(
&receipts,
allocation_id,
collection_id,
payer,
data_service,
service_provider,
Expand All @@ -422,7 +431,7 @@ mod tests {
/// Test check_allocation_id with 3 receipts that have the correct allocation id
fn check_allocation_id_ok(
keys: (PrivateKeySigner, Address),
allocation_id: Address,
collection_id: FixedBytes<32>,
payer: Address,
data_service: Address,
service_provider: Address,
Expand All @@ -431,27 +440,27 @@ mod tests {
let receipts = vec![
Eip712SignedMessage::new(
&domain_separator,
Receipt::new(allocation_id, payer, data_service, service_provider, 42).unwrap(),
Receipt::new(collection_id, payer, data_service, service_provider, 42).unwrap(),
&keys.0,
)
.unwrap(),
Eip712SignedMessage::new(
&domain_separator,
Receipt::new(allocation_id, payer, data_service, service_provider, 43).unwrap(),
Receipt::new(collection_id, payer, data_service, service_provider, 43).unwrap(),
&keys.0,
)
.unwrap(),
Eip712SignedMessage::new(
&domain_separator,
Receipt::new(allocation_id, payer, data_service, service_provider, 44).unwrap(),
Receipt::new(collection_id, payer, data_service, service_provider, 44).unwrap(),
&keys.0,
)
.unwrap(),
];

let res = super::check_allocation_id(
let res = super::check_collection_id(
&receipts,
allocation_id,
collection_id,
payer,
data_service,
service_provider,
Expand Down
8 changes: 4 additions & 4 deletions tap_aggregator/src/grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ pub mod v2 {
type Error = anyhow::Error;
fn try_from(receipt: self::Receipt) -> Result<Self, Self::Error> {
Ok(Self {
allocation_id: receipt.allocation_id.as_slice().try_into()?,
collection_id: receipt.collection_id.as_slice().try_into()?,
timestamp_ns: receipt.timestamp_ns,
value: receipt.value.ok_or(anyhow!("Missing value"))?.into(),
nonce: receipt.nonce,
Expand All @@ -178,7 +178,7 @@ pub mod v2 {
impl From<tap_graph::v2::Receipt> for self::Receipt {
fn from(value: tap_graph::v2::Receipt) -> Self {
Self {
allocation_id: value.allocation_id.as_slice().to_vec(),
collection_id: value.collection_id.as_slice().to_vec(),
timestamp_ns: value.timestamp_ns,
nonce: value.nonce,
value: Some(value.value.into()),
Expand Down Expand Up @@ -224,7 +224,7 @@ pub mod v2 {
type Error = anyhow::Error;
fn try_from(voucher: self::ReceiptAggregateVoucher) -> Result<Self, Self::Error> {
Ok(Self {
allocationId: voucher.allocation_id.as_slice().try_into()?,
collectionId: voucher.collection_id.as_slice().try_into()?,
timestampNs: voucher.timestamp_ns,
valueAggregate: voucher
.value_aggregate
Expand All @@ -241,7 +241,7 @@ pub mod v2 {
impl From<tap_graph::v2::ReceiptAggregateVoucher> for self::ReceiptAggregateVoucher {
fn from(voucher: tap_graph::v2::ReceiptAggregateVoucher) -> Self {
Self {
allocation_id: voucher.allocationId.to_vec(),
collection_id: voucher.collectionId.to_vec(),
timestamp_ns: voucher.timestampNs,
value_aggregate: Some(voucher.valueAggregate.into()),
payer: voucher.payer.to_vec(),
Expand Down
Loading