-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add horizon support to tap-agent
Signed-off-by: Gustavo Inacio <[email protected]>
- Loading branch information
Showing
15 changed files
with
286 additions
and
129 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "indexer-receipt" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
tap_core.workspace = true | ||
tap_graph.workspace = true | ||
thegraph-core.workspace = true | ||
anyhow.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use anyhow::anyhow; | ||
use tap_core::{ | ||
receipt::{ | ||
rav::{Aggregate, AggregationError}, | ||
WithUniqueId, WithValueAndTimestamp, | ||
}, | ||
signed_message::SignatureBytes, | ||
}; | ||
use thegraph_core::alloy::{dyn_abi::Eip712Domain, primitives::Address, signers::Signature}; | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub enum TapReceipt { | ||
V1(tap_graph::SignedReceipt), | ||
V2(tap_graph::v2::SignedReceipt), | ||
} | ||
|
||
impl Aggregate<TapReceipt> for tap_graph::ReceiptAggregateVoucher { | ||
fn aggregate_receipts( | ||
receipts: &[tap_core::receipt::ReceiptWithState< | ||
tap_core::receipt::state::Checked, | ||
TapReceipt, | ||
>], | ||
previous_rav: Option<tap_core::signed_message::Eip712SignedMessage<Self>>, | ||
) -> Result<Self, tap_core::receipt::rav::AggregationError> { | ||
if receipts.is_empty() { | ||
return Err(AggregationError::NoValidReceiptsForRavRequest); | ||
} | ||
let receipts: Vec<_> = receipts | ||
.iter() | ||
.map(|receipt| { | ||
receipt | ||
.signed_receipt() | ||
.get_v1_receipt() | ||
.cloned() | ||
.ok_or(anyhow!("Receipt is not v1")) | ||
}) | ||
.collect::<Result<_, _>>() | ||
.map_err(AggregationError::Other)?; | ||
let allocation_id = receipts[0].message.allocation_id; | ||
tap_graph::ReceiptAggregateVoucher::aggregate_receipts( | ||
allocation_id, | ||
receipts.as_slice(), | ||
previous_rav, | ||
) | ||
} | ||
} | ||
|
||
impl Aggregate<TapReceipt> for tap_graph::v2::ReceiptAggregateVoucher { | ||
fn aggregate_receipts( | ||
receipts: &[tap_core::receipt::ReceiptWithState< | ||
tap_core::receipt::state::Checked, | ||
TapReceipt, | ||
>], | ||
previous_rav: Option<tap_core::signed_message::Eip712SignedMessage<Self>>, | ||
) -> Result<Self, tap_core::receipt::rav::AggregationError> { | ||
if receipts.is_empty() { | ||
return Err(AggregationError::NoValidReceiptsForRavRequest); | ||
} | ||
let receipts: Vec<_> = receipts | ||
.iter() | ||
.map(|receipt| { | ||
receipt | ||
.signed_receipt() | ||
.get_v2_receipt() | ||
.cloned() | ||
.ok_or(anyhow!("Receipt is not v2")) | ||
}) | ||
.collect::<Result<_, _>>() | ||
.map_err(AggregationError::Other)?; | ||
let allocation_id = receipts[0].message.allocation_id; | ||
let payer = receipts[0].message.payer; | ||
let data_service = receipts[0].message.data_service; | ||
let service_provider = receipts[0].message.service_provider; | ||
|
||
tap_graph::v2::ReceiptAggregateVoucher::aggregate_receipts( | ||
allocation_id, | ||
payer, | ||
data_service, | ||
service_provider, | ||
receipts.as_slice(), | ||
previous_rav, | ||
) | ||
} | ||
} | ||
|
||
impl TapReceipt { | ||
pub fn as_v1(self) -> Option<tap_graph::SignedReceipt> { | ||
match self { | ||
TapReceipt::V1(receipt) => Some(receipt), | ||
_ => None, | ||
} | ||
} | ||
|
||
pub fn as_v2(self) -> Option<tap_graph::v2::SignedReceipt> { | ||
match self { | ||
TapReceipt::V2(receipt) => Some(receipt), | ||
_ => None, | ||
} | ||
} | ||
|
||
pub fn get_v1_receipt(&self) -> Option<&tap_graph::SignedReceipt> { | ||
match self { | ||
TapReceipt::V1(receipt) => Some(receipt), | ||
_ => None, | ||
} | ||
} | ||
|
||
pub fn get_v2_receipt(&self) -> Option<&tap_graph::v2::SignedReceipt> { | ||
match self { | ||
TapReceipt::V2(receipt) => Some(receipt), | ||
_ => None, | ||
} | ||
} | ||
|
||
pub fn allocation_id(&self) -> Address { | ||
match self { | ||
TapReceipt::V1(receipt) => receipt.message.allocation_id, | ||
TapReceipt::V2(receipt) => receipt.message.allocation_id, | ||
} | ||
} | ||
|
||
pub fn signature(&self) -> Signature { | ||
match self { | ||
TapReceipt::V1(receipt) => receipt.signature, | ||
TapReceipt::V2(receipt) => receipt.signature, | ||
} | ||
} | ||
|
||
pub fn nonce(&self) -> u64 { | ||
match self { | ||
TapReceipt::V1(receipt) => receipt.message.nonce, | ||
TapReceipt::V2(receipt) => receipt.message.nonce, | ||
} | ||
} | ||
|
||
pub fn recover_signer( | ||
&self, | ||
domain_separator: &Eip712Domain, | ||
) -> Result<Address, tap_core::signed_message::Eip712Error> { | ||
match self { | ||
TapReceipt::V1(receipt) => receipt.recover_signer(domain_separator), | ||
TapReceipt::V2(receipt) => receipt.recover_signer(domain_separator), | ||
} | ||
} | ||
} | ||
|
||
impl WithValueAndTimestamp for TapReceipt { | ||
fn value(&self) -> u128 { | ||
match self { | ||
TapReceipt::V1(receipt) => receipt.value(), | ||
TapReceipt::V2(receipt) => receipt.value(), | ||
} | ||
} | ||
|
||
fn timestamp_ns(&self) -> u64 { | ||
match self { | ||
TapReceipt::V1(receipt) => receipt.timestamp_ns(), | ||
TapReceipt::V2(receipt) => receipt.timestamp_ns(), | ||
} | ||
} | ||
} | ||
|
||
impl WithUniqueId for TapReceipt { | ||
type Output = SignatureBytes; | ||
|
||
fn unique_id(&self) -> Self::Output { | ||
match self { | ||
TapReceipt::V1(receipt) => receipt.unique_id(), | ||
TapReceipt::V2(receipt) => receipt.unique_id(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.