Skip to content

Commit 1e4ea11

Browse files
authored
refactor: add horizon support to tap-agent (#594)
Signed-off-by: Gustavo Inacio <[email protected]>
1 parent e927bcf commit 1e4ea11

File tree

15 files changed

+286
-129
lines changed

15 files changed

+286
-129
lines changed

Cargo.lock

Lines changed: 17 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ members = [
44
"crates/attestation",
55
"crates/config",
66
"crates/dips",
7+
"crates/indexer-receipt",
78
"crates/monitor",
89
"crates/query",
910
"crates/service",
@@ -84,12 +85,12 @@ tonic-build = "0.12.3"
8485

8586
[patch.crates-io.tap_core]
8687
git = "https://github.com/semiotic-ai/timeline-aggregation-protocol"
87-
rev = "dbae001"
88+
rev = "e5546a6"
8889

8990
[patch.crates-io.tap_aggregator]
9091
git = "https://github.com/semiotic-ai/timeline-aggregation-protocol"
91-
rev = "dbae001"
92+
rev = "e5546a6"
9293

9394
[patch.crates-io.tap_graph]
9495
git = "https://github.com/semiotic-ai/timeline-aggregation-protocol"
95-
rev = "dbae001"
96+
rev = "e5546a6"

crates/indexer-receipt/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "indexer-receipt"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
tap_core.workspace = true
8+
tap_graph.workspace = true
9+
thegraph-core.workspace = true
10+
anyhow.workspace = true

crates/indexer-receipt/src/lib.rs

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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+
}

crates/service/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ indexer-allocation = { path = "../allocation" }
1313
indexer-config = { path = "../config" }
1414
indexer-dips = { path = "../dips" }
1515
indexer-query = { path = "../query" }
16+
indexer-receipt = { path = "../indexer-receipt" }
1617
anyhow = { workspace = true }
1718
prometheus = { workspace = true }
1819
reqwest = { workspace = true }

crates/service/src/middleware/auth/tap.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use tap_core::{
2020
manager::{adapters::ReceiptStore, Manager},
2121
receipt::Context,
2222
};
23-
use tap_graph::ReceiptAggregateVoucher;
2423
use tower_http::auth::AsyncAuthorizeRequest;
2524

2625
use crate::{
@@ -33,7 +32,7 @@ use crate::{
3332
///
3433
/// Requires TapReceipt, MetricLabels and Arc<Context> extensions
3534
pub fn tap_receipt_authorize<T, B>(
36-
tap_manager: Arc<Manager<T, TapReceipt, ReceiptAggregateVoucher>>,
35+
tap_manager: Arc<Manager<T, TapReceipt>>,
3736
failed_receipt_metric: &'static prometheus::CounterVec,
3837
) -> impl AsyncAuthorizeRequest<
3938
B,

crates/service/src/tap.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ use crate::tap::checks::{
2222
};
2323

2424
mod checks;
25-
mod receipt;
2625
mod receipt_store;
2726

27+
pub use ::indexer_receipt::TapReceipt;
2828
pub use checks::value_check::AgoraQuery;
29-
pub use receipt::TapReceipt;
3029

3130
pub type CheckingReceipt = ReceiptWithState<Checking, TapReceipt>;
3231

0 commit comments

Comments
 (0)