diff --git a/tap_aggregator/src/aggregator.rs b/tap_aggregator/src/aggregator.rs index 643a214f..4026f063 100644 --- a/tap_aggregator/src/aggregator.rs +++ b/tap_aggregator/src/aggregator.rs @@ -9,16 +9,16 @@ use alloy::{ }; use anyhow::{bail, Ok, Result}; use rayon::prelude::*; -use tap_core::signed_message::{EIP712SignedMessage, SignatureBytes, SignatureBytesExt}; +use tap_core::signed_message::{Eip712SignedMessage, SignatureBytes, SignatureBytesExt}; use tap_graph::{Receipt, ReceiptAggregateVoucher}; pub fn check_and_aggregate_receipts( domain_separator: &Eip712Domain, - receipts: &[EIP712SignedMessage], - previous_rav: Option>, + receipts: &[Eip712SignedMessage], + previous_rav: Option>, wallet: &PrivateKeySigner, accepted_addresses: &HashSet
, -) -> Result> { +) -> Result> { check_signatures_unique(receipts)?; // Check that the receipts are signed by an accepted signer address @@ -63,11 +63,11 @@ pub 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)?) + Ok(Eip712SignedMessage::new(domain_separator, rav, wallet)?) } fn check_signature_is_from_one_of_addresses( - message: &EIP712SignedMessage, + message: &Eip712SignedMessage, domain_separator: &Eip712Domain, accepted_addresses: &HashSet
, ) -> Result<()> { @@ -81,7 +81,7 @@ fn check_signature_is_from_one_of_addresses( } fn check_allocation_id( - receipts: &[EIP712SignedMessage], + receipts: &[Eip712SignedMessage], allocation_id: Address, ) -> Result<()> { for receipt in receipts.iter() { @@ -93,7 +93,7 @@ fn check_allocation_id( Ok(()) } -fn check_signatures_unique(receipts: &[EIP712SignedMessage]) -> Result<()> { +fn check_signatures_unique(receipts: &[Eip712SignedMessage]) -> Result<()> { let mut receipt_signatures: hash_set::HashSet = hash_set::HashSet::new(); for receipt in receipts.iter() { let signature = receipt.signature.get_signature_bytes(); @@ -109,8 +109,8 @@ fn check_signatures_unique(receipts: &[EIP712SignedMessage]) -> Result< } fn check_receipt_timestamps( - receipts: &[EIP712SignedMessage], - previous_rav: Option<&EIP712SignedMessage>, + receipts: &[Eip712SignedMessage], + previous_rav: Option<&Eip712SignedMessage>, ) -> Result<()> { if let Some(previous_rav) = &previous_rav { for receipt in receipts.iter() { @@ -134,7 +134,7 @@ mod tests { use alloy::{dyn_abi::Eip712Domain, primitives::Address, signers::local::PrivateKeySigner}; use rstest::*; - use tap_core::{signed_message::EIP712SignedMessage, tap_eip712_domain}; + use tap_core::{signed_message::Eip712SignedMessage, tap_eip712_domain}; use tap_graph::{Receipt, ReceiptAggregateVoucher}; use crate::aggregator; @@ -170,7 +170,7 @@ mod tests { ) { // Create the same receipt twice (replay attack) let mut receipts = Vec::new(); - let receipt = EIP712SignedMessage::new( + let receipt = Eip712SignedMessage::new( &domain_separator, Receipt::new(allocation_ids[0], 42).unwrap(), &keys.0, @@ -192,13 +192,13 @@ mod tests { ) { // Create 2 different receipts let receipts = vec![ - EIP712SignedMessage::new( + Eip712SignedMessage::new( &domain_separator, Receipt::new(allocation_ids[0], 42).unwrap(), &keys.0, ) .unwrap(), - EIP712SignedMessage::new( + Eip712SignedMessage::new( &domain_separator, Receipt::new(allocation_ids[0], 43).unwrap(), &keys.0, @@ -223,7 +223,7 @@ mod tests { let mut receipts = Vec::new(); for i in receipt_timestamp_range.clone() { receipts.push( - EIP712SignedMessage::new( + Eip712SignedMessage::new( &domain_separator, Receipt { allocation_id: allocation_ids[0], @@ -238,7 +238,7 @@ mod tests { } // Create rav with max_timestamp below the receipts timestamps - let rav = EIP712SignedMessage::new( + let rav = Eip712SignedMessage::new( &domain_separator, ReceiptAggregateVoucher { allocationId: allocation_ids[0], @@ -252,7 +252,7 @@ mod tests { // Create rav with max_timestamp equal to the lowest receipt timestamp // Aggregation should fail - let rav = EIP712SignedMessage::new( + let rav = Eip712SignedMessage::new( &domain_separator, ReceiptAggregateVoucher { allocationId: allocation_ids[0], @@ -266,7 +266,7 @@ mod tests { // Create rav with max_timestamp above highest receipt timestamp // Aggregation should fail - let rav = EIP712SignedMessage::new( + let rav = Eip712SignedMessage::new( &domain_separator, ReceiptAggregateVoucher { allocationId: allocation_ids[0], @@ -289,19 +289,19 @@ mod tests { domain_separator: Eip712Domain, ) { let receipts = vec![ - EIP712SignedMessage::new( + Eip712SignedMessage::new( &domain_separator, Receipt::new(allocation_ids[0], 42).unwrap(), &keys.0, ) .unwrap(), - EIP712SignedMessage::new( + Eip712SignedMessage::new( &domain_separator, Receipt::new(allocation_ids[0], 43).unwrap(), &keys.0, ) .unwrap(), - EIP712SignedMessage::new( + Eip712SignedMessage::new( &domain_separator, Receipt::new(allocation_ids[1], 44).unwrap(), &keys.0, @@ -323,19 +323,19 @@ mod tests { domain_separator: Eip712Domain, ) { let receipts = vec![ - EIP712SignedMessage::new( + Eip712SignedMessage::new( &domain_separator, Receipt::new(allocation_ids[0], 42).unwrap(), &keys.0, ) .unwrap(), - EIP712SignedMessage::new( + Eip712SignedMessage::new( &domain_separator, Receipt::new(allocation_ids[0], 43).unwrap(), &keys.0, ) .unwrap(), - EIP712SignedMessage::new( + Eip712SignedMessage::new( &domain_separator, Receipt::new(allocation_ids[0], 44).unwrap(), &keys.0, diff --git a/tap_aggregator/src/grpc.rs b/tap_aggregator/src/grpc.rs index 2378e567..753e23af 100644 --- a/tap_aggregator/src/grpc.rs +++ b/tap_aggregator/src/grpc.rs @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 use anyhow::anyhow; -use tap_core::signed_message::EIP712SignedMessage; +use tap_core::signed_message::Eip712SignedMessage; tonic::include_proto!("tap_aggregator.v1"); @@ -51,7 +51,7 @@ impl From for SignedReceipt { } } -impl TryFrom for EIP712SignedMessage { +impl TryFrom for Eip712SignedMessage { type Error = anyhow::Error; fn try_from(voucher: SignedRav) -> Result { Ok(Self { @@ -64,8 +64,8 @@ impl TryFrom for EIP712SignedMessage> for SignedRav { - fn from(voucher: EIP712SignedMessage) -> Self { +impl From> for SignedRav { + fn from(voucher: Eip712SignedMessage) -> Self { Self { signature: voucher.signature.as_bytes().to_vec(), message: Some(voucher.message.into()), diff --git a/tap_aggregator/src/server.rs b/tap_aggregator/src/server.rs index 46796caa..30e2078f 100644 --- a/tap_aggregator/src/server.rs +++ b/tap_aggregator/src/server.rs @@ -14,7 +14,7 @@ use jsonrpsee::{ use lazy_static::lazy_static; use log::info; use prometheus::{register_counter, register_int_counter, Counter, IntCounter}; -use tap_core::signed_message::EIP712SignedMessage; +use tap_core::signed_message::Eip712SignedMessage; use tap_graph::{Receipt, ReceiptAggregateVoucher, SignedReceipt}; use tokio::{net::TcpListener, signal, task::JoinHandle}; use tonic::{codec::CompressionEncoding, service::Routes, Request, Response, Status}; @@ -87,9 +87,9 @@ pub trait Rpc { fn aggregate_receipts( &self, api_version: String, - receipts: Vec>, - previous_rav: Option>, - ) -> JsonRpcResult>; + receipts: Vec>, + previous_rav: Option>, + ) -> JsonRpcResult>; } #[derive(Clone)] @@ -134,9 +134,9 @@ fn aggregate_receipts_( wallet: &PrivateKeySigner, accepted_addresses: &HashSet
, domain_separator: &Eip712Domain, - receipts: Vec>, - previous_rav: Option>, -) -> JsonRpcResult> { + receipts: Vec>, + previous_rav: Option>, +) -> JsonRpcResult> { // Return an error if the API version is not supported. let api_version = match parse_api_version(api_version.as_str()) { Ok(v) => v, @@ -230,9 +230,9 @@ impl RpcServer for RpcImpl { fn aggregate_receipts( &self, api_version: String, - receipts: Vec>, - previous_rav: Option>, - ) -> JsonRpcResult> { + receipts: Vec>, + previous_rav: Option>, + ) -> JsonRpcResult> { // Values for Prometheus metrics let receipts_grt: u128 = receipts.iter().map(|r| r.message.value).sum(); let receipts_count: u64 = receipts.len() as u64; @@ -391,7 +391,7 @@ mod tests { use jsonrpsee::{core::client::ClientT, http_client::HttpClientBuilder, rpc_params}; use rand::{prelude::*, seq::SliceRandom}; use rstest::*; - use tap_core::{signed_message::EIP712SignedMessage, tap_eip712_domain}; + use tap_core::{signed_message::Eip712SignedMessage, tap_eip712_domain}; use tap_graph::{Receipt, ReceiptAggregateVoucher}; use crate::server; @@ -520,7 +520,7 @@ mod tests { let mut receipts = Vec::new(); for value in values { receipts.push( - EIP712SignedMessage::new( + Eip712SignedMessage::new( &domain_separator, Receipt::new(allocation_ids[0], value).unwrap(), &all_wallets.choose(&mut rng).unwrap().wallet, @@ -531,7 +531,7 @@ mod tests { // Skipping receipts validation in this test, aggregate_receipts assumes receipts are valid. // Create RAV through the JSON-RPC server. - let res: server::JsonRpcResponse> = client + let res: server::JsonRpcResponse> = client .request( "aggregate_receipts", rpc_params!(api_version, &receipts, None::<()>), @@ -600,7 +600,7 @@ mod tests { let mut receipts = Vec::new(); for value in values { receipts.push( - EIP712SignedMessage::new( + Eip712SignedMessage::new( &domain_separator, Receipt::new(allocation_ids[0], value).unwrap(), &all_wallets.choose(&mut rng).unwrap().wallet, @@ -616,7 +616,7 @@ mod tests { None, ) .unwrap(); - let signed_prev_rav = EIP712SignedMessage::new( + let signed_prev_rav = Eip712SignedMessage::new( &domain_separator, prev_rav, &all_wallets.choose(&mut rng).unwrap().wallet, @@ -624,7 +624,7 @@ mod tests { .unwrap(); // Create new RAV from last half of receipts and prev_rav through the JSON-RPC server - let res: server::JsonRpcResponse> = client + let res: server::JsonRpcResponse> = client .request( "aggregate_receipts", rpc_params!( @@ -674,7 +674,7 @@ mod tests { .unwrap(); // Create receipts - let receipts = vec![EIP712SignedMessage::new( + let receipts = vec![Eip712SignedMessage::new( &domain_separator, Receipt::new(allocation_ids[0], 42).unwrap(), &keys_main.wallet, @@ -684,7 +684,7 @@ mod tests { // Skipping receipts validation in this test, aggregate_receipts assumes receipts are valid. // Create RAV through the JSON-RPC server. let res: Result< - server::JsonRpcResponse>, + server::JsonRpcResponse>, jsonrpsee::core::ClientError, > = client .request( @@ -768,7 +768,7 @@ mod tests { let mut receipts = Vec::new(); for _ in 1..number_of_receipts_to_exceed_limit { receipts.push( - EIP712SignedMessage::new( + Eip712SignedMessage::new( &domain_separator, Receipt::new(allocation_ids[0], u128::MAX / 1000).unwrap(), &keys_main.wallet, @@ -781,7 +781,7 @@ mod tests { // Create RAV through the JSON-RPC server. // Test with a number of receipts that stays within request size limit let res: Result< - server::JsonRpcResponse>, + server::JsonRpcResponse>, jsonrpsee::core::ClientError, > = client .request( @@ -798,7 +798,7 @@ mod tests { // Create RAV through the JSON-RPC server. // Test with all receipts to exceed request size limit let res: Result< - server::JsonRpcResponse>, + server::JsonRpcResponse>, jsonrpsee::core::ClientError, > = client .request( diff --git a/tap_aggregator/tests/aggregate_test.rs b/tap_aggregator/tests/aggregate_test.rs index bc6d0c66..e4302e55 100644 --- a/tap_aggregator/tests/aggregate_test.rs +++ b/tap_aggregator/tests/aggregate_test.rs @@ -10,7 +10,7 @@ use tap_aggregator::{ jsonrpsee_helpers::JsonRpcResponse, server, }; -use tap_core::{signed_message::EIP712SignedMessage, tap_eip712_domain}; +use tap_core::{signed_message::Eip712SignedMessage, tap_eip712_domain}; use tap_graph::{Receipt, ReceiptAggregateVoucher}; use tonic::codec::CompressionEncoding; @@ -51,7 +51,7 @@ async fn aggregation_test() { let mut receipts = Vec::new(); for value in 50..60 { receipts.push( - EIP712SignedMessage::new( + Eip712SignedMessage::new( &domain_separator, Receipt::new(allocation_id, value).unwrap(), &wallet, @@ -68,7 +68,7 @@ async fn aggregation_test() { let previous_rav: Option = None; - let response: JsonRpcResponse> = sender_aggregator + let response: JsonRpcResponse> = sender_aggregator .request( "aggregate_receipts", rpc_params!( diff --git a/tap_core/benches/timeline_aggretion_protocol_benchmark.rs b/tap_core/benches/timeline_aggretion_protocol_benchmark.rs index a2ed91ea..c864828f 100644 --- a/tap_core/benches/timeline_aggretion_protocol_benchmark.rs +++ b/tap_core/benches/timeline_aggretion_protocol_benchmark.rs @@ -12,7 +12,7 @@ use std::str::FromStr; use alloy::{dyn_abi::Eip712Domain, primitives::Address, signers::local::PrivateKeySigner}; use criterion::{black_box, criterion_group, criterion_main, Criterion}; -use tap_core::{signed_message::EIP712SignedMessage, tap_eip712_domain}; +use tap_core::{signed_message::Eip712SignedMessage, tap_eip712_domain}; use tap_graph::{Receipt, ReceiptAggregateVoucher}; pub fn create_and_sign_receipt( @@ -20,8 +20,8 @@ pub fn create_and_sign_receipt( allocation_id: Address, value: u128, wallet: &PrivateKeySigner, -) -> EIP712SignedMessage { - EIP712SignedMessage::new( +) -> Eip712SignedMessage { + Eip712SignedMessage::new( domain_separator, Receipt::new(allocation_id, value).unwrap(), wallet, @@ -80,7 +80,7 @@ pub fn criterion_benchmark(c: &mut Criterion) { }, ); - let signed_rav = EIP712SignedMessage::new( + let signed_rav = Eip712SignedMessage::new( &domain_seperator, ReceiptAggregateVoucher::aggregate_receipts(allocation_id, &receipts, None).unwrap(), &wallet, diff --git a/tap_core/src/lib.rs b/tap_core/src/lib.rs index df6c7120..5c4162d7 100644 --- a/tap_core/src/lib.rs +++ b/tap_core/src/lib.rs @@ -64,7 +64,7 @@ mod tap_tests { use rstest::*; use tap_graph::{Receipt, ReceiptAggregateVoucher}; - use crate::{signed_message::EIP712SignedMessage, tap_eip712_domain}; + use crate::{signed_message::Eip712SignedMessage, tap_eip712_domain}; #[fixture] fn keys() -> (PrivateKeySigner, Address) { @@ -103,7 +103,7 @@ mod tap_tests { let mut receipts = Vec::new(); for value in values { receipts.push( - EIP712SignedMessage::new( + Eip712SignedMessage::new( &domain_separator, Receipt::new(allocation_ids[0], value).unwrap(), &keys.0, @@ -116,7 +116,7 @@ mod tap_tests { let rav = ReceiptAggregateVoucher::aggregate_receipts(allocation_ids[0], &receipts, None) .unwrap(); - let signed_rav = EIP712SignedMessage::new(&domain_separator, rav, &keys.0).unwrap(); + let signed_rav = Eip712SignedMessage::new(&domain_separator, rav, &keys.0).unwrap(); assert!(signed_rav.recover_signer(&domain_separator).unwrap() == keys.1); } @@ -134,7 +134,7 @@ mod tap_tests { let mut receipts = Vec::new(); for value in values { receipts.push( - EIP712SignedMessage::new( + Eip712SignedMessage::new( &domain_separator, Receipt::new(allocation_ids[0], value).unwrap(), &keys.0, @@ -151,7 +151,7 @@ mod tap_tests { ) .unwrap(); let signed_prev_rav = - EIP712SignedMessage::new(&domain_separator, prev_rav, &keys.0).unwrap(); + Eip712SignedMessage::new(&domain_separator, prev_rav, &keys.0).unwrap(); // Create new RAV from last half of receipts and prev_rav let rav = ReceiptAggregateVoucher::aggregate_receipts( @@ -160,7 +160,7 @@ mod tap_tests { Some(signed_prev_rav), ) .unwrap(); - let signed_rav = EIP712SignedMessage::new(&domain_separator, rav, &keys.0).unwrap(); + let signed_rav = Eip712SignedMessage::new(&domain_separator, rav, &keys.0).unwrap(); assert!(signed_rav.recover_signer(&domain_separator).unwrap() == keys.1); } @@ -172,7 +172,7 @@ mod tap_tests { allocation_ids: Vec
, domain_separator: Eip712Domain, ) { - let signed_message = EIP712SignedMessage::new( + let signed_message = Eip712SignedMessage::new( &domain_separator, Receipt::new(allocation_ids[0], 42).unwrap(), &keys.0, diff --git a/tap_core/src/manager/adapters/rav.rs b/tap_core/src/manager/adapters/rav.rs index 8c40a625..837349bd 100644 --- a/tap_core/src/manager/adapters/rav.rs +++ b/tap_core/src/manager/adapters/rav.rs @@ -4,7 +4,7 @@ use alloy::sol_types::SolStruct; use async_trait::async_trait; -use crate::signed_message::EIP712SignedMessage; +use crate::signed_message::Eip712SignedMessage; /// Stores the latest RAV in the storage. /// @@ -26,7 +26,7 @@ pub trait RavStore { /// This method should be implemented to store the most recent validated /// `SignedRAV` into your chosen storage system. Any errors that occur /// during this process should be captured and returned as an `AdapterError`. - async fn update_last_rav(&self, rav: EIP712SignedMessage) -> Result<(), Self::AdapterError>; + async fn update_last_rav(&self, rav: Eip712SignedMessage) -> Result<(), Self::AdapterError>; } /// Reads the RAV from storage @@ -47,5 +47,5 @@ pub trait RavRead { /// Retrieves the latest `SignedRAV` from the storage. /// /// If no `SignedRAV` is available, this method should return `None`. - async fn last_rav(&self) -> Result>, Self::AdapterError>; + async fn last_rav(&self) -> Result>, Self::AdapterError>; } diff --git a/tap_core/src/manager/adapters/signature.rs b/tap_core/src/manager/adapters/signature.rs index ffb34ae3..5c8bce09 100644 --- a/tap_core/src/manager/adapters/signature.rs +++ b/tap_core/src/manager/adapters/signature.rs @@ -4,7 +4,7 @@ use alloy::{dyn_abi::Eip712Domain, primitives::Address, sol_types::SolStruct}; use async_trait::async_trait; -use crate::{signed_message::EIP712SignedMessage, Error}; +use crate::{signed_message::Eip712SignedMessage, Error}; /// Manages the escrow operations /// @@ -27,7 +27,7 @@ pub trait SignatureChecker: Send + Sync { /// Checks if the signed message has a sender signature async fn check_signature( &self, - signed_message: &EIP712SignedMessage, + signed_message: &Eip712SignedMessage, domain_separator: &Eip712Domain, ) -> Result<(), Error> { let recovered_address = signed_message.recover_signer(domain_separator)?; diff --git a/tap_core/src/manager/mod.rs b/tap_core/src/manager/mod.rs index 42ef6eed..c13bdb0f 100644 --- a/tap_core/src/manager/mod.rs +++ b/tap_core/src/manager/mod.rs @@ -63,12 +63,12 @@ //! # async fn main() { //! # use alloy::{dyn_abi::Eip712Domain, primitives::Address, signers::local::PrivateKeySigner}; //! # use tap_graph::{Receipt, SignedReceipt, ReceiptAggregateVoucher}; -//! # use tap_core::signed_message::EIP712SignedMessage; +//! # use tap_core::signed_message::Eip712SignedMessage; //! # let domain_separator = Eip712Domain::default(); //! # let wallet = PrivateKeySigner::random(); //! # let message = Receipt::new(Address::from([0x11u8; 20]), 100).unwrap(); //! -//! let receipt = EIP712SignedMessage::new(&domain_separator, message, &wallet).unwrap(); +//! let receipt = Eip712SignedMessage::new(&domain_separator, message, &wallet).unwrap(); //! //! let manager = Manager::<_, _, ReceiptAggregateVoucher>::new(domain_separator, MyContext, CheckList::empty()); //! manager.verify_and_store_receipt(&Context::new(), receipt).await.unwrap() diff --git a/tap_core/src/manager/tap_manager.rs b/tap_core/src/manager/tap_manager.rs index 31d6899b..33727e55 100644 --- a/tap_core/src/manager/tap_manager.rs +++ b/tap_core/src/manager/tap_manager.rs @@ -16,7 +16,7 @@ use crate::{ state::{Checked, Failed}, Context, ReceiptError, ReceiptWithState, WithUniqueId, WithValueAndTimestamp, }, - signed_message::EIP712SignedMessage, + signed_message::Eip712SignedMessage, Error, }; @@ -67,7 +67,7 @@ where pub async fn verify_and_store_rav( &self, expected_rav: Rav, - signed_rav: EIP712SignedMessage, + signed_rav: Eip712SignedMessage, ) -> std::result::Result<(), Error> { self.context .check_signature(&signed_rav, &self.domain_separator) @@ -96,7 +96,7 @@ where E: RavRead, Rav: SolStruct, { - async fn get_previous_rav(&self) -> Result>, Error> { + async fn get_previous_rav(&self) -> Result>, Error> { let previous_rav = self .context .last_rav() diff --git a/tap_core/src/rav_request.rs b/tap_core/src/rav_request.rs index eba06924..f4848aac 100644 --- a/tap_core/src/rav_request.rs +++ b/tap_core/src/rav_request.rs @@ -1,6 +1,8 @@ // Copyright 2023-, Semiotic AI, Inc. // SPDX-License-Identifier: Apache-2.0 +//! Request to Tap Aggregator + use alloy::sol_types::SolStruct; use tap_receipt::rav::AggregationError; @@ -9,7 +11,7 @@ use crate::{ state::{Checked, Failed}, ReceiptWithState, }, - signed_message::EIP712SignedMessage, + signed_message::Eip712SignedMessage, }; /// Request to `tap_aggregator` to aggregate receipts into a Signed RAV. @@ -18,7 +20,7 @@ pub struct RavRequest { /// List of checked and reserved receipts to aggregate pub valid_receipts: Vec>, /// Optional previous RAV to aggregate with - pub previous_rav: Option>, + pub previous_rav: Option>, /// List of failed receipt used to log invalid receipts pub invalid_receipts: Vec>, /// Expected RAV to be created diff --git a/tap_core/src/receipt.rs b/tap_core/src/receipt.rs index 6171870d..5c292153 100644 --- a/tap_core/src/receipt.rs +++ b/tap_core/src/receipt.rs @@ -1,4 +1,23 @@ // Copyright 2023-, Semiotic AI, Inc. // SPDX-License-Identifier: Apache-2.0 -// + +//! # Receipts states and checks +//! +//! Receipts are used as single transaction promise of payment. A payment sender +//! creates a receipt and ECDSA signs it, then sends it to a payment receiver. +//! The payment receiver would verify the received receipt and store it to be +//! accumulated with other received receipts in the future. +//! +//! A list of checks are performed on the received receipts to ensure they are valid. +//! The checks are performed when storing the receipt and when aggregating the receipts +//! into a Receipt Aggregate Voucher (RAV). +//! +//! Each receipt is wrapped into a State Machine that can be in one of the following states: +//! - `Checking`: The receipt is being checked. +//! - `Failed`: The receipt has failed a check or validation. +//! - `AwaitingReserve`: The receipt has passed all checks and is awaiting escrow reservation. +//! - `Reserved`: The receipt has successfully reserved escrow. +//! +//! + pub use ::tap_receipt::*; diff --git a/tap_core/src/signed_message.rs b/tap_core/src/signed_message.rs index a886a512..674b9670 100644 --- a/tap_core/src/signed_message.rs +++ b/tap_core/src/signed_message.rs @@ -1,4 +1,26 @@ // Copyright 2023-, Semiotic AI, Inc. // SPDX-License-Identifier: Apache-2.0 +//! # EIP712 message and signatures +//! +//! This module contains the `EIP712SignedMessage` struct which is used to sign and verify messages +//! using EIP712 standard. +//! +//! # Example +//! ```rust +//! # use alloy::{dyn_abi::Eip712Domain, primitives::Address, signers::local::PrivateKeySigner}; +//! # use tap_graph::{Receipt}; +//! # let domain_separator = Eip712Domain::default(); +//! use tap_core::signed_message::Eip712SignedMessage; +//! # let wallet = PrivateKeySigner::random(); +//! # let wallet_address = wallet.address(); +//! # let message = Receipt::new(Address::from([0x11u8; 20]), 100).unwrap(); +//! +//! let signed_message = Eip712SignedMessage::new(&domain_separator, message, &wallet).unwrap(); +//! let signer = signed_message.recover_signer(&domain_separator).unwrap(); +//! +//! assert_eq!(signer, wallet_address); +//! ``` +//! + pub use ::tap_eip712_message::*; diff --git a/tap_core/tests/manager_test.rs b/tap_core/tests/manager_test.rs index 9a255712..8bd6ef64 100644 --- a/tap_core/tests/manager_test.rs +++ b/tap_core/tests/manager_test.rs @@ -28,7 +28,7 @@ use tap_core::{ state::Checking, Context, ReceiptWithState, }, - signed_message::EIP712SignedMessage, + signed_message::Eip712SignedMessage, tap_eip712_domain, }; use tap_graph::{Receipt, ReceiptAggregateVoucher, SignedReceipt}; @@ -132,7 +132,7 @@ async fn manager_verify_and_store_varying_initial_checks( Manager::<_, _, ReceiptAggregateVoucher>::new(domain_separator.clone(), context, checks); let value = 20u128; - let signed_receipt = EIP712SignedMessage::new( + let signed_receipt = Eip712SignedMessage::new( &domain_separator, Receipt::new(allocation_ids[0], value).unwrap(), &signer, @@ -175,7 +175,7 @@ async fn manager_create_rav_request_all_valid_receipts( let mut stored_signed_receipts = Vec::new(); for _ in 0..10 { let value = 20u128; - let signed_receipt = EIP712SignedMessage::new( + let signed_receipt = Eip712SignedMessage::new( &domain_separator, Receipt::new(allocation_ids[0], value).unwrap(), &signer, @@ -204,7 +204,7 @@ async fn manager_create_rav_request_all_valid_receipts( let expected_rav = rav_request.expected_rav.unwrap(); let signed_rav = - EIP712SignedMessage::new(&domain_separator, expected_rav.clone(), &signer).unwrap(); + Eip712SignedMessage::new(&domain_separator, expected_rav.clone(), &signer).unwrap(); assert!(manager .verify_and_store_rav(expected_rav, signed_rav) .await @@ -235,7 +235,7 @@ async fn deny_rav_due_to_wrong_value(domain_separator: Eip712Domain, context: Co }; let signed_rav_with_wrong_aggregate = - EIP712SignedMessage::new(&domain_separator, rav_wrong_value, &signer).unwrap(); + Eip712SignedMessage::new(&domain_separator, rav_wrong_value, &signer).unwrap(); assert!(manager .verify_and_store_rav(rav, signed_rav_with_wrong_aggregate) @@ -270,7 +270,7 @@ async fn manager_create_multiple_rav_requests_all_valid_receipts( let mut expected_accumulated_value = 0; for _ in 0..10 { let value = 20u128; - let signed_receipt = EIP712SignedMessage::new( + let signed_receipt = Eip712SignedMessage::new( &domain_separator, Receipt::new(allocation_ids[0], value).unwrap(), &signer, @@ -304,7 +304,7 @@ async fn manager_create_multiple_rav_requests_all_valid_receipts( assert!(rav_request.previous_rav.is_none()); let signed_rav = - EIP712SignedMessage::new(&domain_separator, expected_rav.clone(), &signer).unwrap(); + Eip712SignedMessage::new(&domain_separator, expected_rav.clone(), &signer).unwrap(); assert!(manager .verify_and_store_rav(expected_rav, signed_rav) .await @@ -313,7 +313,7 @@ async fn manager_create_multiple_rav_requests_all_valid_receipts( stored_signed_receipts.clear(); for _ in 10..20 { let value = 20u128; - let signed_receipt = EIP712SignedMessage::new( + let signed_receipt = Eip712SignedMessage::new( &domain_separator, Receipt::new(allocation_ids[0], value).unwrap(), &signer, @@ -348,7 +348,7 @@ async fn manager_create_multiple_rav_requests_all_valid_receipts( assert!(rav_request.previous_rav.is_some()); let signed_rav = - EIP712SignedMessage::new(&domain_separator, expected_rav.clone(), &signer).unwrap(); + Eip712SignedMessage::new(&domain_separator, expected_rav.clone(), &signer).unwrap(); assert!(manager .verify_and_store_rav(expected_rav, signed_rav) .await @@ -386,7 +386,7 @@ async fn manager_create_multiple_rav_requests_all_valid_receipts_consecutive_tim let value = 20u128; let mut receipt = Receipt::new(allocation_ids[0], value).unwrap(); receipt.timestamp_ns = starting_min_timestamp + query_id + 1; - let signed_receipt = EIP712SignedMessage::new(&domain_separator, receipt, &signer).unwrap(); + let signed_receipt = Eip712SignedMessage::new(&domain_separator, receipt, &signer).unwrap(); let query_id = signed_receipt.unique_hash(); stored_signed_receipts.push(signed_receipt.clone()); @@ -423,7 +423,7 @@ async fn manager_create_multiple_rav_requests_all_valid_receipts_consecutive_tim assert!(rav_request_1.previous_rav.is_none()); let signed_rav_1 = - EIP712SignedMessage::new(&domain_separator, expected_rav_1.clone(), &signer).unwrap(); + Eip712SignedMessage::new(&domain_separator, expected_rav_1.clone(), &signer).unwrap(); assert!(manager .verify_and_store_rav(expected_rav_1, signed_rav_1) .await @@ -434,7 +434,7 @@ async fn manager_create_multiple_rav_requests_all_valid_receipts_consecutive_tim let value = 20u128; let mut receipt = Receipt::new(allocation_ids[0], value).unwrap(); receipt.timestamp_ns = starting_min_timestamp + query_id + 1; - let signed_receipt = EIP712SignedMessage::new(&domain_separator, receipt, &signer).unwrap(); + let signed_receipt = Eip712SignedMessage::new(&domain_separator, receipt, &signer).unwrap(); let query_id = signed_receipt.unique_hash(); stored_signed_receipts.push(signed_receipt.clone()); query_appraisals.write().unwrap().insert(query_id, value); @@ -478,7 +478,7 @@ async fn manager_create_multiple_rav_requests_all_valid_receipts_consecutive_tim assert!(rav_request_2.previous_rav.is_some()); let signed_rav_2 = - EIP712SignedMessage::new(&domain_separator, expected_rav_2.clone(), &signer).unwrap(); + Eip712SignedMessage::new(&domain_separator, expected_rav_2.clone(), &signer).unwrap(); assert!(manager .verify_and_store_rav(expected_rav_2, signed_rav_2) .await @@ -516,7 +516,7 @@ async fn manager_create_rav_and_ignore_invalid_receipts( nonce: 1, value: 20u128, }; - let signed_receipt = EIP712SignedMessage::new(&domain_separator, receipt, &signer).unwrap(); + let signed_receipt = Eip712SignedMessage::new(&domain_separator, receipt, &signer).unwrap(); stored_signed_receipts.push(signed_receipt.clone()); manager .verify_and_store_receipt(&Context::new(), signed_receipt) @@ -597,7 +597,7 @@ async fn test_retryable_checks( nonce: i, value: 20u128, }; - let signed_receipt = EIP712SignedMessage::new(&domain_separator, receipt, &signer).unwrap(); + let signed_receipt = Eip712SignedMessage::new(&domain_separator, receipt, &signer).unwrap(); stored_signed_receipts.push(signed_receipt.clone()); manager .verify_and_store_receipt(&Context::new(), signed_receipt) diff --git a/tap_core/tests/rav_test.rs b/tap_core/tests/rav_test.rs index 1c0cadf8..a00d3078 100644 --- a/tap_core/tests/rav_test.rs +++ b/tap_core/tests/rav_test.rs @@ -20,7 +20,7 @@ use tap_core::{ context::memory::InMemoryContext, }, receipt::checks::StatefulTimestampCheck, - signed_message::EIP712SignedMessage, + signed_message::Eip712SignedMessage, tap_eip712_domain, }; use tap_graph::{Receipt, ReceiptAggregateVoucher}; @@ -55,7 +55,7 @@ fn check_for_rav_serialization(domain_separator: Eip712Domain) { let mut receipts = Vec::new(); for value in 50..60 { receipts.push( - EIP712SignedMessage::new( + Eip712SignedMessage::new( &domain_separator, Receipt { allocation_id, @@ -69,7 +69,7 @@ fn check_for_rav_serialization(domain_separator: Eip712Domain) { ); } - let signed_rav = EIP712SignedMessage::new( + let signed_rav = Eip712SignedMessage::new( &domain_separator, ReceiptAggregateVoucher::aggregate_receipts(allocation_id, &receipts, None).unwrap(), &wallet, @@ -101,7 +101,7 @@ async fn rav_storage_adapter_test(domain_separator: Eip712Domain, context: InMem let mut receipts = Vec::new(); for value in 50..60 { receipts.push( - EIP712SignedMessage::new( + Eip712SignedMessage::new( &domain_separator, Receipt::new(allocation_id, value).unwrap(), &wallet, @@ -110,7 +110,7 @@ async fn rav_storage_adapter_test(domain_separator: Eip712Domain, context: InMem ); } - let signed_rav = EIP712SignedMessage::new( + let signed_rav = Eip712SignedMessage::new( &domain_separator, ReceiptAggregateVoucher::aggregate_receipts(allocation_id, &receipts, None).unwrap(), &wallet, @@ -129,7 +129,7 @@ async fn rav_storage_adapter_test(domain_separator: Eip712Domain, context: InMem let mut receipts = Vec::new(); for value in 60..70 { receipts.push( - EIP712SignedMessage::new( + Eip712SignedMessage::new( &domain_separator, Receipt::new(allocation_id, value).unwrap(), &wallet, @@ -138,7 +138,7 @@ async fn rav_storage_adapter_test(domain_separator: Eip712Domain, context: InMem ); } - let signed_rav = EIP712SignedMessage::new( + let signed_rav = Eip712SignedMessage::new( &domain_separator, ReceiptAggregateVoucher::aggregate_receipts(allocation_id, &receipts, None).unwrap(), &wallet, diff --git a/tap_core/tests/receipt_test.rs b/tap_core/tests/receipt_test.rs index 5b4dcf56..4263ff5b 100644 --- a/tap_core/tests/receipt_test.rs +++ b/tap_core/tests/receipt_test.rs @@ -13,7 +13,7 @@ use rstest::*; use tap_core::{ manager::{adapters::ReceiptStore, context::memory::InMemoryContext}, receipt::{checks::StatefulTimestampCheck, state::Checking, ReceiptWithState}, - signed_message::EIP712SignedMessage, + signed_message::Eip712SignedMessage, tap_eip712_domain, }; use tap_graph::{Receipt, SignedReceipt}; @@ -48,7 +48,7 @@ async fn receipt_adapter_test(domain_separator: Eip712Domain, mut context: InMem // Create receipts let value = 100u128; let received_receipt = ReceiptWithState::new( - EIP712SignedMessage::new( + Eip712SignedMessage::new( &domain_separator, Receipt::new(allocation_id, value).unwrap(), &wallet, @@ -88,7 +88,7 @@ async fn multi_receipt_adapter_test(domain_separator: Eip712Domain, mut context: let mut received_receipts = Vec::new(); for value in 50..60 { received_receipts.push(ReceiptWithState::new( - EIP712SignedMessage::new( + Eip712SignedMessage::new( &domain_separator, Receipt::new(allocation_id, value).unwrap(), &wallet, @@ -165,7 +165,7 @@ fn safe_truncate_receipts_test( for timestamp in input.iter() { // The contents of the receipt only need to be unique for this test (so we can check) receipts_orig.push(ReceiptWithState::new( - EIP712SignedMessage::new( + Eip712SignedMessage::new( &domain_separator, Receipt { allocation_id: Address::ZERO, diff --git a/tap_core/tests/received_receipt_test.rs b/tap_core/tests/received_receipt_test.rs index 86f2215f..7da2399d 100644 --- a/tap_core/tests/received_receipt_test.rs +++ b/tap_core/tests/received_receipt_test.rs @@ -15,7 +15,7 @@ use tap_core::{ checks::{ReceiptCheck, StatefulTimestampCheck}, Context, ReceiptWithState, }, - signed_message::EIP712SignedMessage, + signed_message::Eip712SignedMessage, tap_eip712_domain, }; use tap_graph::{Receipt, SignedReceipt}; @@ -104,7 +104,7 @@ async fn partial_then_full_check_valid_receipt( } = context; let query_value = 20u128; - let signed_receipt = EIP712SignedMessage::new( + let signed_receipt = Eip712SignedMessage::new( &domain_separator, Receipt::new(allocation_ids[0], query_value).unwrap(), &signer, @@ -148,7 +148,7 @@ async fn partial_then_finalize_valid_receipt( } = context; let query_value = 20u128; - let signed_receipt = EIP712SignedMessage::new( + let signed_receipt = Eip712SignedMessage::new( &domain_separator, Receipt::new(allocation_ids[0], query_value).unwrap(), &signer, @@ -194,7 +194,7 @@ async fn standard_lifetime_valid_receipt( } = context; let query_value = 20u128; - let signed_receipt = EIP712SignedMessage::new( + let signed_receipt = Eip712SignedMessage::new( &domain_separator, Receipt::new(allocation_ids[0], query_value).unwrap(), &signer, diff --git a/tap_eip712_message/src/lib.rs b/tap_eip712_message/src/lib.rs index 1372f4cd..25f6a922 100644 --- a/tap_eip712_message/src/lib.rs +++ b/tap_eip712_message/src/lib.rs @@ -1,21 +1,21 @@ // Copyright 2023-, Semiotic AI, Inc. // SPDX-License-Identifier: Apache-2.0 -//! # EIP712 message and signature +//! # EIP712 signed message //! -//! This module contains the `EIP712SignedMessage` struct which is used to sign and verify messages +//! This crate contains the `EIP712SignedMessage` struct which is used to sign and verify messages //! using EIP712 standard. //! //! # Example //! ```rust //! # use alloy::{dyn_abi::Eip712Domain, primitives::Address, signers::local::PrivateKeySigner}; //! # let domain_separator = Eip712Domain::default(); -//! use tap_eip712_message::EIP712SignedMessage; +//! use tap_eip712_message::Eip712SignedMessage; //! # let wallet = PrivateKeySigner::random(); //! # let wallet_address = wallet.address(); //! # let message = msg::Receipt::new(Address::from([0x11u8; 20]), 100).unwrap(); //! -//! let signed_message = EIP712SignedMessage::new(&domain_separator, message, &wallet).unwrap(); +//! let signed_message = Eip712SignedMessage::new(&domain_separator, message, &wallet).unwrap(); //! let signer = signed_message.recover_signer(&domain_separator).unwrap(); //! //! assert_eq!(signer, wallet_address); @@ -30,6 +30,7 @@ use alloy::{ }; use serde::{Deserialize, Serialize}; +/// Errors returned by creation of messages and verify signature #[derive(thiserror::Error, Debug)] pub enum Eip712Error { /// `alloy` wallet error @@ -43,16 +44,18 @@ pub enum Eip712Error { /// EIP712 signed message #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)] -pub struct EIP712SignedMessage { +pub struct Eip712SignedMessage { /// Message to be signed pub message: M, /// ECDSA Signature of eip712 hash of message pub signature: Signature, } +/// Signature that can be used in a HashSet #[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] pub struct SignatureBytes([u8; 65]); +/// Extension for Signature to return [SignatureBytes] pub trait SignatureBytesExt { fn get_signature_bytes(&self) -> SignatureBytes; } @@ -74,7 +77,7 @@ impl SignatureBytesExt for Signature { #[derive(Debug, Eq, PartialEq, Hash)] pub struct MessageId(pub [u8; 32]); -impl EIP712SignedMessage { +impl Eip712SignedMessage { /// Creates a signed message with signed EIP712 hash of `message` using `signing_wallet` /// /// # Errors diff --git a/tap_graph/src/lib.rs b/tap_graph/src/lib.rs index 4d9f9a2f..8a3668c8 100644 --- a/tap_graph/src/lib.rs +++ b/tap_graph/src/lib.rs @@ -1,26 +1,13 @@ // Copyright 2023-, Semiotic AI, Inc. // SPDX-License-Identifier: Apache-2.0 -use std::time::{SystemTime, UNIX_EPOCH}; +//! # The Graph TAP structs +//! +//! These structs are used for communication between The Graph systems. +//! mod rav; mod receipt; pub use rav::{ReceiptAggregateVoucher, SignedRav}; pub use receipt::{Receipt, SignedReceipt}; - -#[derive(Debug, thiserror::Error)] -pub enum Error { - /// Error when Rust fails to get the current system time - #[error("Failed to get current system time: {source_error_message} ")] - InvalidSystemTime { source_error_message: String }, -} - -fn get_current_timestamp_u64_ns() -> Result { - Ok(SystemTime::now() - .duration_since(UNIX_EPOCH) - .map_err(|err| Error::InvalidSystemTime { - source_error_message: err.to_string(), - })? - .as_nanos() as u64) -} diff --git a/tap_graph/src/rav.rs b/tap_graph/src/rav.rs index 32637b7f..6d08b22e 100644 --- a/tap_graph/src/rav.rs +++ b/tap_graph/src/rav.rs @@ -41,7 +41,7 @@ use std::cmp; use alloy::{primitives::Address, sol}; use serde::{Deserialize, Serialize}; -use tap_eip712_message::EIP712SignedMessage; +use tap_eip712_message::Eip712SignedMessage; use tap_receipt::{ rav::{Aggregate, AggregationError}, state::Checked, @@ -50,11 +50,11 @@ use tap_receipt::{ use crate::{receipt::Receipt, SignedReceipt}; -/// EIP712 signed message for ReceiptAggregateVoucher -pub type SignedRav = EIP712SignedMessage; +/// A Rav wrapped in an Eip712SignedMessage +pub type SignedRav = Eip712SignedMessage; sol! { - /// Holds information needed for promise of payment signed with ECDSA + /// ReceiptAggregateVoucher struct sent to Arbitrum to redeem payments /// /// We use camelCase for field names to match the Ethereum ABI encoding #[derive(Debug, Serialize, Deserialize, Eq, PartialEq)] @@ -80,8 +80,8 @@ impl ReceiptAggregateVoucher { /// value to overflow pub fn aggregate_receipts( allocation_id: Address, - receipts: &[EIP712SignedMessage], - previous_rav: Option>, + receipts: &[Eip712SignedMessage], + previous_rav: Option>, ) -> Result { //TODO(#29): When receipts in flight struct in created check that the state // of every receipt is OK with all checks complete (relies on #28) @@ -113,7 +113,7 @@ impl ReceiptAggregateVoucher { impl Aggregate for ReceiptAggregateVoucher { fn aggregate_receipts( receipts: &[ReceiptWithState], - previous_rav: Option>, + previous_rav: Option>, ) -> Result { if receipts.is_empty() { return Err(AggregationError::NoValidReceiptsForRavRequest); diff --git a/tap_graph/src/receipt.rs b/tap_graph/src/receipt.rs index 136adf05..b4831910 100644 --- a/tap_graph/src/receipt.rs +++ b/tap_graph/src/receipt.rs @@ -8,17 +8,19 @@ //! The payment receiver would verify the received receipt and store it to be //! accumulated with other received receipts in the future. +use std::time::{SystemTime, SystemTimeError, UNIX_EPOCH}; + use alloy::{primitives::Address, sol}; use rand::{thread_rng, Rng}; use serde::{Deserialize, Serialize}; -use tap_eip712_message::EIP712SignedMessage; +use tap_eip712_message::Eip712SignedMessage; use tap_receipt::WithValueAndTimestamp; -/// A signed receipt message -pub type SignedReceipt = EIP712SignedMessage; +/// A Receipt wrapped in an Eip712SignedMessage +pub type SignedReceipt = Eip712SignedMessage; sol! { - /// Holds information needed for promise of payment signed with ECDSA + /// Receipt struct used to pay for an off-chain service #[derive(Debug, Serialize, Deserialize, Eq, PartialEq)] struct Receipt { /// Unique allocation id this receipt belongs to @@ -32,10 +34,14 @@ sol! { } } +fn get_current_timestamp_u64_ns() -> Result { + Ok(SystemTime::now().duration_since(UNIX_EPOCH)?.as_nanos() as u64) +} + impl Receipt { /// Returns a receipt with provided values - pub fn new(allocation_id: Address, value: u128) -> Result { - let timestamp_ns = crate::get_current_timestamp_u64_ns()?; + pub fn new(allocation_id: Address, value: u128) -> Result { + let timestamp_ns = get_current_timestamp_u64_ns()?; let nonce = thread_rng().gen::(); Ok(Self { allocation_id, diff --git a/tap_integration_tests/tests/showcase.rs b/tap_integration_tests/tests/showcase.rs index 5c5eb2b0..d504a002 100644 --- a/tap_integration_tests/tests/showcase.rs +++ b/tap_integration_tests/tests/showcase.rs @@ -27,7 +27,7 @@ use tap_aggregator::{jsonrpsee_helpers, server as agg_server}; use tap_core::{ manager::context::memory::{checks::get_full_list_of_checks, *}, receipt::checks::{CheckList, StatefulTimestampCheck}, - signed_message::{EIP712SignedMessage, MessageId}, + signed_message::{Eip712SignedMessage, MessageId}, tap_eip712_domain, }; use tap_graph::{Receipt, SignedRav, SignedReceipt}; @@ -209,7 +209,7 @@ fn requests_1( num_batches: u64, allocation_ids: Vec
, domain_separator: Eip712Domain, -) -> Vec> { +) -> Vec> { // Create your Receipt here generate_requests( query_price, @@ -227,7 +227,7 @@ fn requests_2( num_batches: u64, allocation_ids: Vec
, domain_separator: Eip712Domain, -) -> Vec> { +) -> Vec> { // Create your Receipt here generate_requests( query_price, @@ -246,7 +246,7 @@ fn repeated_timestamp_request( domain_separator: Eip712Domain, num_batches: u64, receipt_threshold_1: u64, -) -> Vec> { +) -> Vec> { // Create signed receipts let mut requests = generate_requests( query_price, @@ -270,7 +270,7 @@ fn repeated_timestamp_request( // Sign the new receipt and insert it in the second batch requests[receipt_threshold_1 as usize] = - EIP712SignedMessage::new(&domain_separator, repeat_receipt, &keys_sender).unwrap(); + Eip712SignedMessage::new(&domain_separator, repeat_receipt, &keys_sender).unwrap(); requests } @@ -282,7 +282,7 @@ fn repeated_timestamp_incremented_by_one_request( domain_separator: Eip712Domain, num_batches: u64, receipt_threshold_1: u64, -) -> Vec> { +) -> Vec> { // Create your Receipt here let mut requests = generate_requests( query_price, @@ -307,7 +307,7 @@ fn repeated_timestamp_incremented_by_one_request( // Sign the new receipt and insert it in the second batch requests[receipt_threshold_1 as usize] = - EIP712SignedMessage::new(&domain_separator, repeat_receipt, &keys_sender).unwrap(); + Eip712SignedMessage::new(&domain_separator, repeat_receipt, &keys_sender).unwrap(); requests } @@ -319,7 +319,7 @@ fn wrong_requests( num_batches: u64, allocation_ids: Vec
, domain_separator: Eip712Domain, -) -> Vec> { +) -> Vec> { // Create your Receipt here // Create your Receipt here generate_requests( @@ -491,7 +491,7 @@ async fn test_manager_one_indexer( (ServerHandle, SocketAddr, JoinHandle<()>, SocketAddr), Error, >, - requests_1: Vec>, + requests_1: Vec>, ) -> Result<(), Box> { let (_server_handle, socket_addr, _sender_handle, _sender_addr) = single_indexer_test_server.await?; @@ -524,8 +524,8 @@ async fn test_manager_two_indexers( ), Error, >, - requests_1: Vec>, - requests_2: Vec>, + requests_1: Vec>, + requests_2: Vec>, ) -> Result<()> { let ( _server_handle_1, @@ -559,7 +559,7 @@ async fn test_manager_wrong_aggregator_keys( (ServerHandle, SocketAddr, JoinHandle<()>, SocketAddr), Error, >, - requests_1: Vec>, + requests_1: Vec>, receipt_threshold_1: u64, ) -> Result<()> { let (_server_handle, socket_addr, _sender_handle, _sender_addr) = @@ -601,7 +601,7 @@ async fn test_manager_wrong_requestor_keys( (ServerHandle, SocketAddr, JoinHandle<()>, SocketAddr), Error, >, - wrong_requests: Vec>, + wrong_requests: Vec>, ) -> Result<()> { let (_server_handle, socket_addr, _sender_handle, _sender_addr) = single_indexer_test_server.await?; @@ -633,8 +633,8 @@ async fn test_tap_manager_rav_timestamp_cuttoff( ), Error, >, - repeated_timestamp_request: Vec>, - repeated_timestamp_incremented_by_one_request: Vec>, + repeated_timestamp_request: Vec>, + repeated_timestamp_incremented_by_one_request: Vec>, receipt_threshold_1: u64, ) -> Result<(), Box> { // This test checks that tap_core is correctly filtering receipts by timestamp. @@ -695,8 +695,8 @@ async fn test_tap_aggregator_rav_timestamp_cuttoff( http_request_size_limit: u32, http_response_size_limit: u32, http_max_concurrent_connections: u32, - repeated_timestamp_request: Vec>, - repeated_timestamp_incremented_by_one_request: Vec>, + repeated_timestamp_request: Vec>, + repeated_timestamp_incremented_by_one_request: Vec>, receipt_threshold_1: u64, ) -> Result<(), Box> { // This test checks that tap_aggregator is correctly rejecting receipts with invalid timestamps @@ -772,13 +772,13 @@ fn generate_requests( sender_key: &PrivateKeySigner, allocation_id: Address, domain_separator: &Eip712Domain, -) -> Vec> { - let mut requests: Vec> = Vec::new(); +) -> Vec> { + let mut requests: Vec> = Vec::new(); for _ in 0..num_batches { for value in query_price { requests.push( - EIP712SignedMessage::new( + Eip712SignedMessage::new( domain_separator, Receipt::new(allocation_id, *value).unwrap(), sender_key, diff --git a/tap_receipt/src/checks.rs b/tap_receipt/src/checks.rs index b50be069..eb59d7aa 100644 --- a/tap_receipt/src/checks.rs +++ b/tap_receipt/src/checks.rs @@ -215,7 +215,7 @@ mod tests { dyn_abi::Eip712Domain, primitives::Address, signers::local::PrivateKeySigner, sol, sol_types::eip712_domain, }; - use tap_eip712_message::EIP712SignedMessage; + use tap_eip712_message::Eip712SignedMessage; use super::*; @@ -238,7 +238,7 @@ mod tests { fn create_signed_receipt_with_custom_value( value: u128, - ) -> ReceiptWithState> { + ) -> ReceiptWithState> { let wallet: PrivateKeySigner = PrivateKeySigner::random(); let eip712_domain_separator: Eip712Domain = eip712_domain! { name: "TAP", @@ -255,7 +255,7 @@ mod tests { let timestamp_ns = timestamp as u64; let value: u128 = value; - let receipt = EIP712SignedMessage::new( + let receipt = Eip712SignedMessage::new( &eip712_domain_separator, MyReceipt { timestamp_ns, diff --git a/tap_receipt/src/lib.rs b/tap_receipt/src/lib.rs index 6a88c87c..04acd502 100644 --- a/tap_receipt/src/lib.rs +++ b/tap_receipt/src/lib.rs @@ -28,24 +28,27 @@ pub mod state; use alloy::sol_types::SolStruct; pub use error::ReceiptError; pub use received_receipt::ReceiptWithState; -use tap_eip712_message::{EIP712SignedMessage, SignatureBytes, SignatureBytesExt}; +use tap_eip712_message::{Eip712SignedMessage, SignatureBytes, SignatureBytesExt}; /// Result type for receipt pub type ReceiptResult = Result; +/// Extra information for [checks::Check] pub type Context = anymap3::Map; +/// Extension that allows TAP Aggregation for any SolStruct receipt pub trait WithValueAndTimestamp { fn value(&self) -> u128; fn timestamp_ns(&self) -> u64; } +/// Extension that allows UniqueCheck for any SolStruct receipt pub trait WithUniqueId { type Output: Eq + std::hash::Hash; fn unique_id(&self) -> Self::Output; } -impl WithValueAndTimestamp for EIP712SignedMessage +impl WithValueAndTimestamp for Eip712SignedMessage where T: SolStruct + WithValueAndTimestamp, { @@ -58,7 +61,7 @@ where } } -impl WithUniqueId for EIP712SignedMessage +impl WithUniqueId for Eip712SignedMessage where T: SolStruct, { diff --git a/tap_receipt/src/rav.rs b/tap_receipt/src/rav.rs index 3c275779..bc46460b 100644 --- a/tap_receipt/src/rav.rs +++ b/tap_receipt/src/rav.rs @@ -1,8 +1,10 @@ // Copyright 2023-, Semiotic AI, Inc. // SPDX-License-Identifier: Apache-2.0 +//! Aggregation of Receipts + use alloy::sol_types::SolStruct; -use tap_eip712_message::EIP712SignedMessage; +use tap_eip712_message::Eip712SignedMessage; use crate::{state::Checked, ReceiptWithState}; @@ -11,7 +13,7 @@ pub trait Aggregate: SolStruct { /// returning a new RAV if all provided items are valid or an error if not. fn aggregate_receipts( receipts: &[ReceiptWithState], - previous_rav: Option>, + previous_rav: Option>, ) -> Result; }