|
| 1 | +// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use anyhow::anyhow; |
| 5 | +use tap_core::{ |
| 6 | + receipt::{ |
| 7 | + rav::{Aggregate, AggregationError}, |
| 8 | + WithUniqueId, WithValueAndTimestamp, |
| 9 | + }, |
| 10 | + signed_message::SignatureBytes, |
| 11 | +}; |
| 12 | +use thegraph_core::alloy::{dyn_abi::Eip712Domain, primitives::Address, signers::Signature}; |
| 13 | + |
| 14 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 15 | +pub enum TapReceipt { |
| 16 | + V1(tap_graph::SignedReceipt), |
| 17 | + V2(tap_graph::v2::SignedReceipt), |
| 18 | +} |
| 19 | + |
| 20 | +impl Aggregate<TapReceipt> for tap_graph::ReceiptAggregateVoucher { |
| 21 | + fn aggregate_receipts( |
| 22 | + receipts: &[tap_core::receipt::ReceiptWithState< |
| 23 | + tap_core::receipt::state::Checked, |
| 24 | + TapReceipt, |
| 25 | + >], |
| 26 | + previous_rav: Option<tap_core::signed_message::Eip712SignedMessage<Self>>, |
| 27 | + ) -> Result<Self, tap_core::receipt::rav::AggregationError> { |
| 28 | + if receipts.is_empty() { |
| 29 | + return Err(AggregationError::NoValidReceiptsForRavRequest); |
| 30 | + } |
| 31 | + let receipts: Vec<_> = receipts |
| 32 | + .iter() |
| 33 | + .map(|receipt| { |
| 34 | + receipt |
| 35 | + .signed_receipt() |
| 36 | + .get_v1_receipt() |
| 37 | + .cloned() |
| 38 | + .ok_or(anyhow!("Receipt is not v1")) |
| 39 | + }) |
| 40 | + .collect::<Result<_, _>>() |
| 41 | + .map_err(AggregationError::Other)?; |
| 42 | + let allocation_id = receipts[0].message.allocation_id; |
| 43 | + tap_graph::ReceiptAggregateVoucher::aggregate_receipts( |
| 44 | + allocation_id, |
| 45 | + receipts.as_slice(), |
| 46 | + previous_rav, |
| 47 | + ) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +impl Aggregate<TapReceipt> for tap_graph::v2::ReceiptAggregateVoucher { |
| 52 | + fn aggregate_receipts( |
| 53 | + receipts: &[tap_core::receipt::ReceiptWithState< |
| 54 | + tap_core::receipt::state::Checked, |
| 55 | + TapReceipt, |
| 56 | + >], |
| 57 | + previous_rav: Option<tap_core::signed_message::Eip712SignedMessage<Self>>, |
| 58 | + ) -> Result<Self, tap_core::receipt::rav::AggregationError> { |
| 59 | + if receipts.is_empty() { |
| 60 | + return Err(AggregationError::NoValidReceiptsForRavRequest); |
| 61 | + } |
| 62 | + let receipts: Vec<_> = receipts |
| 63 | + .iter() |
| 64 | + .map(|receipt| { |
| 65 | + receipt |
| 66 | + .signed_receipt() |
| 67 | + .get_v2_receipt() |
| 68 | + .cloned() |
| 69 | + .ok_or(anyhow!("Receipt is not v2")) |
| 70 | + }) |
| 71 | + .collect::<Result<_, _>>() |
| 72 | + .map_err(AggregationError::Other)?; |
| 73 | + let allocation_id = receipts[0].message.allocation_id; |
| 74 | + let payer = receipts[0].message.payer; |
| 75 | + let data_service = receipts[0].message.data_service; |
| 76 | + let service_provider = receipts[0].message.service_provider; |
| 77 | + |
| 78 | + tap_graph::v2::ReceiptAggregateVoucher::aggregate_receipts( |
| 79 | + allocation_id, |
| 80 | + payer, |
| 81 | + data_service, |
| 82 | + service_provider, |
| 83 | + receipts.as_slice(), |
| 84 | + previous_rav, |
| 85 | + ) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +impl TapReceipt { |
| 90 | + pub fn as_v1(self) -> Option<tap_graph::SignedReceipt> { |
| 91 | + match self { |
| 92 | + TapReceipt::V1(receipt) => Some(receipt), |
| 93 | + _ => None, |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + pub fn as_v2(self) -> Option<tap_graph::v2::SignedReceipt> { |
| 98 | + match self { |
| 99 | + TapReceipt::V2(receipt) => Some(receipt), |
| 100 | + _ => None, |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + pub fn get_v1_receipt(&self) -> Option<&tap_graph::SignedReceipt> { |
| 105 | + match self { |
| 106 | + TapReceipt::V1(receipt) => Some(receipt), |
| 107 | + _ => None, |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + pub fn get_v2_receipt(&self) -> Option<&tap_graph::v2::SignedReceipt> { |
| 112 | + match self { |
| 113 | + TapReceipt::V2(receipt) => Some(receipt), |
| 114 | + _ => None, |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + pub fn allocation_id(&self) -> Address { |
| 119 | + match self { |
| 120 | + TapReceipt::V1(receipt) => receipt.message.allocation_id, |
| 121 | + TapReceipt::V2(receipt) => receipt.message.allocation_id, |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + pub fn signature(&self) -> Signature { |
| 126 | + match self { |
| 127 | + TapReceipt::V1(receipt) => receipt.signature, |
| 128 | + TapReceipt::V2(receipt) => receipt.signature, |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + pub fn nonce(&self) -> u64 { |
| 133 | + match self { |
| 134 | + TapReceipt::V1(receipt) => receipt.message.nonce, |
| 135 | + TapReceipt::V2(receipt) => receipt.message.nonce, |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + pub fn recover_signer( |
| 140 | + &self, |
| 141 | + domain_separator: &Eip712Domain, |
| 142 | + ) -> Result<Address, tap_core::signed_message::Eip712Error> { |
| 143 | + match self { |
| 144 | + TapReceipt::V1(receipt) => receipt.recover_signer(domain_separator), |
| 145 | + TapReceipt::V2(receipt) => receipt.recover_signer(domain_separator), |
| 146 | + } |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +impl WithValueAndTimestamp for TapReceipt { |
| 151 | + fn value(&self) -> u128 { |
| 152 | + match self { |
| 153 | + TapReceipt::V1(receipt) => receipt.value(), |
| 154 | + TapReceipt::V2(receipt) => receipt.value(), |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + fn timestamp_ns(&self) -> u64 { |
| 159 | + match self { |
| 160 | + TapReceipt::V1(receipt) => receipt.timestamp_ns(), |
| 161 | + TapReceipt::V2(receipt) => receipt.timestamp_ns(), |
| 162 | + } |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +impl WithUniqueId for TapReceipt { |
| 167 | + type Output = SignatureBytes; |
| 168 | + |
| 169 | + fn unique_id(&self) -> Self::Output { |
| 170 | + match self { |
| 171 | + TapReceipt::V1(receipt) => receipt.unique_id(), |
| 172 | + TapReceipt::V2(receipt) => receipt.unique_id(), |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments