Skip to content

Commit 5ca1bf8

Browse files
committed
refactor!: add v2 proto
refactor!: move grpc structs to v1 refactor: create shared uint128 proto Signed-off-by: Gustavo Inacio <[email protected]>
1 parent dbae001 commit 5ca1bf8

File tree

8 files changed

+191
-115
lines changed

8 files changed

+191
-115
lines changed

tap_aggregator/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ tokio.workspace = true
3939
tonic = { version = "0.12.3", features = ["transport", "zstd"] }
4040
tower = { version = "0.5.2", features = ["util", "steer"] }
4141
tracing-subscriber = "0.3.17"
42-
tap_graph = { version = "0.2.0", path = "../tap_graph" }
42+
tap_graph = { version = "0.2.0", path = "../tap_graph", features = ["v2"] }
4343

4444
[build-dependencies]
4545
tonic-build = "0.12.3"

tap_aggregator/build.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
66
let out_dir = std::env::var("OUT_DIR").expect("OUT_DIR not set by Cargo");
77
println!("OUT_DIR: {}", out_dir); // This should print the output directory
88

9-
tonic_build::compile_protos("./proto/tap_aggregator.proto")?;
9+
tonic_build::configure().compile_protos(
10+
&[
11+
"proto/uint128.proto",
12+
"proto/tap_aggregator.proto",
13+
"proto/v2.proto",
14+
],
15+
&["proto"],
16+
)?;
17+
1018
Ok(())
1119
}

tap_aggregator/proto/tap_aggregator.proto

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
syntax = "proto3";
55
package tap_aggregator.v1;
66

7+
import "uint128.proto";
8+
79
message Receipt {
810
bytes allocation_id = 1;
911
uint64 timestamp_ns = 2;
1012
uint64 nonce = 3;
11-
Uint128 value = 4;
13+
grpc.uint128.Uint128 value = 4;
1214
}
1315

1416
message SignedReceipt {
@@ -19,7 +21,7 @@ message SignedReceipt {
1921
message ReceiptAggregateVoucher {
2022
bytes allocation_id = 1;
2123
uint64 timestamp_ns = 2;
22-
Uint128 value_aggregate = 3;
24+
grpc.uint128.Uint128 value_aggregate = 3;
2325
}
2426

2527
message SignedRav {
@@ -39,10 +41,3 @@ message RavResponse {
3941
service TapAggregator {
4042
rpc AggregateReceipts(RavRequest) returns (RavResponse);
4143
}
42-
43-
message Uint128 {
44-
// Highest 64 bits of a 128 bit number.
45-
uint64 high = 1;
46-
// Lowest 64 bits of a 128 bit number.
47-
uint64 low = 2;
48-
}

tap_aggregator/proto/uint128.proto

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2023-, Semiotic AI, Inc.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
syntax = "proto3";
5+
package grpc.uint128;
6+
7+
message Uint128 {
8+
// Highest 64 bits of a 128 bit number.
9+
uint64 high = 1;
10+
// Lowest 64 bits of a 128 bit number.
11+
uint64 low = 2;
12+
}

tap_aggregator/proto/v2.proto

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2023-, Semiotic AI, Inc.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
syntax = "proto3";
5+
package tap_aggregator.v2;
6+
7+
import "uint128.proto";
8+
9+
message Receipt {
10+
bytes allocation_id = 1;
11+
bytes payer = 2;
12+
bytes data_service = 3;
13+
bytes service_provider = 4;
14+
uint64 timestamp_ns = 5;
15+
uint64 nonce = 6;
16+
grpc.uint128.Uint128 value = 7;
17+
}
18+
19+
message SignedReceipt {
20+
Receipt message = 1;
21+
bytes signature = 2;
22+
}
23+
24+
message ReceiptAggregateVoucher {
25+
bytes allocation_id = 1;
26+
bytes payer = 2;
27+
bytes data_service = 3;
28+
bytes service_provider = 4;
29+
uint64 timestamp_ns = 5;
30+
grpc.uint128.Uint128 value_aggregate = 6;
31+
bytes metadata = 7;
32+
}
33+
34+
message SignedRav {
35+
ReceiptAggregateVoucher message = 1;
36+
bytes signature = 2;
37+
}
38+
39+
message RavRequest {
40+
repeated SignedReceipt receipts = 1;
41+
optional SignedRav previous_rav = 2;
42+
}
43+
44+
message RavResponse {
45+
SignedRav rav = 1;
46+
}
47+
48+
service TapAggregator {
49+
rpc AggregateReceipts(RavRequest) returns (RavResponse);
50+
}
51+

tap_aggregator/src/grpc.rs

Lines changed: 112 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,135 +1,145 @@
11
// Copyright 2023-, Semiotic AI, Inc.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use anyhow::anyhow;
5-
use tap_core::signed_message::Eip712SignedMessage;
6-
7-
tonic::include_proto!("tap_aggregator.v1");
8-
9-
impl TryFrom<Receipt> for tap_graph::Receipt {
10-
type Error = anyhow::Error;
11-
fn try_from(receipt: Receipt) -> Result<Self, Self::Error> {
12-
Ok(Self {
13-
allocation_id: receipt.allocation_id.as_slice().try_into()?,
14-
timestamp_ns: receipt.timestamp_ns,
15-
value: receipt.value.ok_or(anyhow!("Missing value"))?.into(),
16-
nonce: receipt.nonce,
17-
})
4+
pub mod uint128 {
5+
tonic::include_proto!("grpc.uint128");
6+
7+
impl From<Uint128> for u128 {
8+
fn from(Uint128 { high, low }: Uint128) -> Self {
9+
((high as u128) << 64) | low as u128
10+
}
1811
}
19-
}
2012

21-
impl TryFrom<SignedReceipt> for tap_graph::SignedReceipt {
22-
type Error = anyhow::Error;
23-
fn try_from(receipt: SignedReceipt) -> Result<Self, Self::Error> {
24-
Ok(Self {
25-
signature: receipt.signature.as_slice().try_into()?,
26-
message: receipt
27-
.message
28-
.ok_or(anyhow!("Missing message"))?
29-
.try_into()?,
30-
})
13+
impl From<u128> for Uint128 {
14+
fn from(value: u128) -> Self {
15+
let high = (value >> 64) as u64;
16+
let low = value as u64;
17+
Self { high, low }
18+
}
3119
}
3220
}
3321

34-
impl From<tap_graph::Receipt> for Receipt {
35-
fn from(value: tap_graph::Receipt) -> Self {
36-
Self {
37-
allocation_id: value.allocation_id.as_slice().to_vec(),
38-
timestamp_ns: value.timestamp_ns,
39-
nonce: value.nonce,
40-
value: Some(value.value.into()),
22+
pub mod v1 {
23+
use anyhow::anyhow;
24+
use tap_core::signed_message::Eip712SignedMessage;
25+
26+
tonic::include_proto!("tap_aggregator.v1");
27+
28+
impl TryFrom<self::Receipt> for tap_graph::Receipt {
29+
type Error = anyhow::Error;
30+
fn try_from(receipt: self::Receipt) -> Result<Self, Self::Error> {
31+
Ok(Self {
32+
allocation_id: receipt.allocation_id.as_slice().try_into()?,
33+
timestamp_ns: receipt.timestamp_ns,
34+
value: receipt.value.ok_or(anyhow!("Missing value"))?.into(),
35+
nonce: receipt.nonce,
36+
})
4137
}
4238
}
43-
}
4439

45-
impl From<tap_graph::SignedReceipt> for SignedReceipt {
46-
fn from(value: tap_graph::SignedReceipt) -> Self {
47-
Self {
48-
message: Some(value.message.into()),
49-
signature: value.signature.as_bytes().to_vec(),
40+
impl TryFrom<self::SignedReceipt> for tap_graph::SignedReceipt {
41+
type Error = anyhow::Error;
42+
fn try_from(receipt: self::SignedReceipt) -> Result<Self, Self::Error> {
43+
Ok(Self {
44+
signature: receipt.signature.as_slice().try_into()?,
45+
message: receipt
46+
.message
47+
.ok_or(anyhow!("Missing message"))?
48+
.try_into()?,
49+
})
5050
}
5151
}
52-
}
5352

54-
impl TryFrom<SignedRav> for Eip712SignedMessage<tap_graph::ReceiptAggregateVoucher> {
55-
type Error = anyhow::Error;
56-
fn try_from(voucher: SignedRav) -> Result<Self, Self::Error> {
57-
Ok(Self {
58-
signature: voucher.signature.as_slice().try_into()?,
59-
message: voucher
60-
.message
61-
.ok_or(anyhow!("Missing message"))?
62-
.try_into()?,
63-
})
53+
impl From<tap_graph::Receipt> for self::Receipt {
54+
fn from(value: tap_graph::Receipt) -> Self {
55+
Self {
56+
allocation_id: value.allocation_id.as_slice().to_vec(),
57+
timestamp_ns: value.timestamp_ns,
58+
nonce: value.nonce,
59+
value: Some(value.value.into()),
60+
}
61+
}
6462
}
65-
}
6663

67-
impl From<Eip712SignedMessage<tap_graph::ReceiptAggregateVoucher>> for SignedRav {
68-
fn from(voucher: Eip712SignedMessage<tap_graph::ReceiptAggregateVoucher>) -> Self {
69-
Self {
70-
signature: voucher.signature.as_bytes().to_vec(),
71-
message: Some(voucher.message.into()),
64+
impl From<tap_graph::SignedReceipt> for self::SignedReceipt {
65+
fn from(value: tap_graph::SignedReceipt) -> Self {
66+
Self {
67+
message: Some(value.message.into()),
68+
signature: value.signature.as_bytes().to_vec(),
69+
}
7270
}
7371
}
74-
}
7572

76-
impl TryFrom<ReceiptAggregateVoucher> for tap_graph::ReceiptAggregateVoucher {
77-
type Error = anyhow::Error;
78-
fn try_from(voucher: ReceiptAggregateVoucher) -> Result<Self, Self::Error> {
79-
Ok(Self {
80-
allocationId: voucher.allocation_id.as_slice().try_into()?,
81-
timestampNs: voucher.timestamp_ns,
82-
valueAggregate: voucher
83-
.value_aggregate
84-
.ok_or(anyhow!("Missing Value Aggregate"))?
85-
.into(),
86-
})
73+
impl TryFrom<self::SignedRav> for Eip712SignedMessage<tap_graph::ReceiptAggregateVoucher> {
74+
type Error = anyhow::Error;
75+
fn try_from(voucher: self::SignedRav) -> Result<Self, Self::Error> {
76+
Ok(Self {
77+
signature: voucher.signature.as_slice().try_into()?,
78+
message: voucher
79+
.message
80+
.ok_or(anyhow!("Missing message"))?
81+
.try_into()?,
82+
})
83+
}
8784
}
88-
}
8985

90-
impl From<tap_graph::ReceiptAggregateVoucher> for ReceiptAggregateVoucher {
91-
fn from(voucher: tap_graph::ReceiptAggregateVoucher) -> Self {
92-
Self {
93-
allocation_id: voucher.allocationId.to_vec(),
94-
timestamp_ns: voucher.timestampNs,
95-
value_aggregate: Some(voucher.valueAggregate.into()),
86+
impl From<Eip712SignedMessage<tap_graph::ReceiptAggregateVoucher>> for self::SignedRav {
87+
fn from(voucher: Eip712SignedMessage<tap_graph::ReceiptAggregateVoucher>) -> Self {
88+
Self {
89+
signature: voucher.signature.as_bytes().to_vec(),
90+
message: Some(voucher.message.into()),
91+
}
9692
}
9793
}
98-
}
9994

100-
impl From<Uint128> for u128 {
101-
fn from(Uint128 { high, low }: Uint128) -> Self {
102-
((high as u128) << 64) | low as u128
95+
impl TryFrom<self::ReceiptAggregateVoucher> for tap_graph::ReceiptAggregateVoucher {
96+
type Error = anyhow::Error;
97+
fn try_from(voucher: self::ReceiptAggregateVoucher) -> Result<Self, Self::Error> {
98+
Ok(Self {
99+
allocationId: voucher.allocation_id.as_slice().try_into()?,
100+
timestampNs: voucher.timestamp_ns,
101+
valueAggregate: voucher
102+
.value_aggregate
103+
.ok_or(anyhow!("Missing Value Aggregate"))?
104+
.into(),
105+
})
106+
}
103107
}
104-
}
105108

106-
impl From<u128> for Uint128 {
107-
fn from(value: u128) -> Self {
108-
let high = (value >> 64) as u64;
109-
let low = value as u64;
110-
Self { high, low }
109+
impl From<tap_graph::ReceiptAggregateVoucher> for self::ReceiptAggregateVoucher {
110+
fn from(voucher: tap_graph::ReceiptAggregateVoucher) -> Self {
111+
Self {
112+
allocation_id: voucher.allocationId.to_vec(),
113+
timestamp_ns: voucher.timestampNs,
114+
value_aggregate: Some(voucher.valueAggregate.into()),
115+
}
116+
}
111117
}
112-
}
113118

114-
impl RavRequest {
115-
pub fn new(
116-
receipts: Vec<tap_graph::SignedReceipt>,
117-
previous_rav: Option<tap_graph::SignedRav>,
118-
) -> Self {
119-
Self {
120-
receipts: receipts.into_iter().map(Into::into).collect(),
121-
previous_rav: previous_rav.map(Into::into),
119+
impl self::RavRequest {
120+
pub fn new(
121+
receipts: Vec<tap_graph::SignedReceipt>,
122+
previous_rav: Option<tap_graph::SignedRav>,
123+
) -> Self {
124+
Self {
125+
receipts: receipts.into_iter().map(Into::into).collect(),
126+
previous_rav: previous_rav.map(Into::into),
127+
}
122128
}
123129
}
124-
}
125130

126-
impl RavResponse {
127-
pub fn signed_rav(mut self) -> anyhow::Result<tap_graph::SignedRav> {
128-
let signed_rav: tap_graph::SignedRav = self
129-
.rav
130-
.take()
131-
.ok_or(anyhow!("Couldn't find rav"))?
132-
.try_into()?;
133-
Ok(signed_rav)
131+
impl self::RavResponse {
132+
pub fn signed_rav(mut self) -> anyhow::Result<tap_graph::SignedRav> {
133+
let signed_rav: tap_graph::SignedRav = self
134+
.rav
135+
.take()
136+
.ok_or(anyhow!("Couldn't find rav"))?
137+
.try_into()?;
138+
Ok(signed_rav)
139+
}
134140
}
135141
}
142+
143+
pub mod v2 {
144+
tonic::include_proto!("tap_aggregator.v2");
145+
}

tap_aggregator/src/server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::{
2727
TAP_RPC_API_VERSIONS_DEPRECATED,
2828
},
2929
error_codes::{JsonRpcErrorCode, JsonRpcWarningCode},
30-
grpc::{
30+
grpc::v1::{
3131
tap_aggregator_server::{TapAggregator, TapAggregatorServer},
3232
RavRequest, RavResponse,
3333
},

tap_aggregator/tests/aggregate_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{collections::HashSet, str::FromStr};
66
use alloy::{primitives::Address, signers::local::PrivateKeySigner};
77
use jsonrpsee::{core::client::ClientT, http_client::HttpClientBuilder, rpc_params};
88
use tap_aggregator::{
9-
grpc::{tap_aggregator_client::TapAggregatorClient, RavRequest},
9+
grpc::v1::{tap_aggregator_client::TapAggregatorClient, RavRequest},
1010
jsonrpsee_helpers::JsonRpcResponse,
1111
server,
1212
};

0 commit comments

Comments
 (0)