-
Notifications
You must be signed in to change notification settings - Fork 20
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
refactor: add horizon support to tap-agent #594
Merged
+286
−129
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this necessary with the
v1
v2
separation?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just for Aggregation, it's especially important that we can only aggregate V1 Receipts into V1 Ravs and for V2 as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't the enum handle that? I'm trying to understand how a V2 receipt could match as V1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you suggest doing it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm asking you, why you think this won't work:
Maybe I'm missing something!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Each one has a different type, the compiler would say
tap_graph::SignedReceipt
is not an instance oftap_graph::v2::SignedReceipt
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I see that now, the
v1
having no similar label threw me.But do the
SignedReceipt
types not implement some shared behavior? But it doesn't have to look so confusing?