-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: accept grpc requests in tap-aggregator (#253)
* feat: add proto definitions Signed-off-by: Gustavo Inacio <[email protected]> * feat: gRPC service implementation Signed-off-by: Gustavo Inacio <[email protected]> Signed-off-by: pedro bufulin <[email protected]> * ci: add protobuf compiler Signed-off-by: Gustavo Inacio <[email protected]> * build: add protobuf compiler to dockerfile Signed-off-by: Gustavo Inacio <[email protected]> --------- Signed-off-by: Gustavo Inacio <[email protected]> Signed-off-by: pedro bufulin <[email protected]> Co-authored-by: pedro bufulin <[email protected]>
- Loading branch information
Showing
11 changed files
with
512 additions
and
93 deletions.
There are no files selected for viewing
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
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,11 @@ | ||
// Copyright 2023-, Semiotic AI, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
println!("Running build.rs..."); | ||
let out_dir = std::env::var("OUT_DIR").expect("OUT_DIR not set by Cargo"); | ||
println!("OUT_DIR: {}", out_dir); // This should print the output directory | ||
|
||
tonic_build::compile_protos("./proto/tap_aggregator.proto")?; | ||
Ok(()) | ||
} |
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,48 @@ | ||
// Copyright 2023-, Semiotic AI, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
syntax = "proto3"; | ||
package tap_aggregator.v1; | ||
|
||
message Receipt { | ||
bytes allocation_id = 1; | ||
uint64 timestamp_ns = 2; | ||
uint64 nonce = 3; | ||
Uint128 value = 4; | ||
} | ||
|
||
message SignedReceipt { | ||
Receipt message = 1; | ||
bytes signature = 2; | ||
} | ||
|
||
message ReceiptAggregateVoucher { | ||
bytes allocation_id = 1; | ||
uint64 timestamp_ns = 2; | ||
Uint128 value_aggregate = 3; | ||
} | ||
|
||
message SignedRav { | ||
ReceiptAggregateVoucher message = 1; | ||
bytes signature = 2; | ||
} | ||
|
||
message RavRequest { | ||
repeated SignedReceipt receipts = 1; | ||
optional SignedRav previous_rav = 2; | ||
} | ||
|
||
message RavResponse { | ||
SignedRav rav = 1; | ||
} | ||
|
||
service TapAggregator { | ||
rpc AggregateReceipts(RavRequest) returns (RavResponse); | ||
} | ||
|
||
message Uint128 { | ||
// Highest 64 bits of a 128 bit number. | ||
uint64 high = 1; | ||
// Lowest 64 bits of a 128 bit number. | ||
uint64 low = 2; | ||
} |
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,135 @@ | ||
// Copyright 2023-, Semiotic AI, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use anyhow::anyhow; | ||
use tap_core::signed_message::EIP712SignedMessage; | ||
|
||
tonic::include_proto!("tap_aggregator.v1"); | ||
|
||
impl TryFrom<Receipt> for tap_core::receipt::Receipt { | ||
type Error = anyhow::Error; | ||
fn try_from(receipt: Receipt) -> Result<Self, Self::Error> { | ||
Ok(Self { | ||
allocation_id: receipt.allocation_id.as_slice().try_into()?, | ||
timestamp_ns: receipt.timestamp_ns, | ||
value: receipt.value.ok_or(anyhow!("Missing value"))?.into(), | ||
nonce: receipt.nonce, | ||
}) | ||
} | ||
} | ||
|
||
impl TryFrom<SignedReceipt> for tap_core::receipt::SignedReceipt { | ||
type Error = anyhow::Error; | ||
fn try_from(receipt: SignedReceipt) -> Result<Self, Self::Error> { | ||
Ok(Self { | ||
signature: receipt.signature.as_slice().try_into()?, | ||
message: receipt | ||
.message | ||
.ok_or(anyhow!("Missing message"))? | ||
.try_into()?, | ||
}) | ||
} | ||
} | ||
|
||
impl From<tap_core::receipt::Receipt> for Receipt { | ||
fn from(value: tap_core::receipt::Receipt) -> Self { | ||
Self { | ||
allocation_id: value.allocation_id.as_slice().to_vec(), | ||
timestamp_ns: value.timestamp_ns, | ||
nonce: value.nonce, | ||
value: Some(value.value.into()), | ||
} | ||
} | ||
} | ||
|
||
impl From<tap_core::receipt::SignedReceipt> for SignedReceipt { | ||
fn from(value: tap_core::receipt::SignedReceipt) -> Self { | ||
Self { | ||
message: Some(value.message.into()), | ||
signature: value.signature.as_bytes().to_vec(), | ||
} | ||
} | ||
} | ||
|
||
impl TryFrom<SignedRav> for EIP712SignedMessage<tap_core::rav::ReceiptAggregateVoucher> { | ||
type Error = anyhow::Error; | ||
fn try_from(voucher: SignedRav) -> Result<Self, Self::Error> { | ||
Ok(Self { | ||
signature: voucher.signature.as_slice().try_into()?, | ||
message: voucher | ||
.message | ||
.ok_or(anyhow!("Missing message"))? | ||
.try_into()?, | ||
}) | ||
} | ||
} | ||
|
||
impl From<EIP712SignedMessage<tap_core::rav::ReceiptAggregateVoucher>> for SignedRav { | ||
fn from(voucher: EIP712SignedMessage<tap_core::rav::ReceiptAggregateVoucher>) -> Self { | ||
Self { | ||
signature: voucher.signature.as_bytes().to_vec(), | ||
message: Some(voucher.message.into()), | ||
} | ||
} | ||
} | ||
|
||
impl TryFrom<ReceiptAggregateVoucher> for tap_core::rav::ReceiptAggregateVoucher { | ||
type Error = anyhow::Error; | ||
fn try_from(voucher: ReceiptAggregateVoucher) -> Result<Self, Self::Error> { | ||
Ok(Self { | ||
allocationId: voucher.allocation_id.as_slice().try_into()?, | ||
timestampNs: voucher.timestamp_ns, | ||
valueAggregate: voucher | ||
.value_aggregate | ||
.ok_or(anyhow!("Missing Value Aggregate"))? | ||
.into(), | ||
}) | ||
} | ||
} | ||
|
||
impl From<tap_core::rav::ReceiptAggregateVoucher> for ReceiptAggregateVoucher { | ||
fn from(voucher: tap_core::rav::ReceiptAggregateVoucher) -> Self { | ||
Self { | ||
allocation_id: voucher.allocationId.to_vec(), | ||
timestamp_ns: voucher.timestampNs, | ||
value_aggregate: Some(voucher.valueAggregate.into()), | ||
} | ||
} | ||
} | ||
|
||
impl From<Uint128> for u128 { | ||
fn from(Uint128 { high, low }: Uint128) -> Self { | ||
((high as u128) << 64) | low as u128 | ||
} | ||
} | ||
|
||
impl From<u128> for Uint128 { | ||
fn from(value: u128) -> Self { | ||
let high = (value >> 64) as u64; | ||
let low = value as u64; | ||
Self { high, low } | ||
} | ||
} | ||
|
||
impl RavRequest { | ||
pub fn new( | ||
receipts: Vec<tap_core::receipt::SignedReceipt>, | ||
previous_rav: Option<tap_core::rav::SignedRAV>, | ||
) -> Self { | ||
Self { | ||
receipts: receipts.into_iter().map(Into::into).collect(), | ||
previous_rav: previous_rav.map(Into::into), | ||
} | ||
} | ||
} | ||
|
||
impl RavResponse { | ||
pub fn signed_rav(mut self) -> anyhow::Result<tap_core::rav::SignedRAV> { | ||
let signed_rav: tap_core::rav::SignedRAV = self | ||
.rav | ||
.take() | ||
.ok_or(anyhow!("Couldn't find rav"))? | ||
.try_into()?; | ||
Ok(signed_rav) | ||
} | ||
} |
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.