Skip to content

Commit

Permalink
rpc(funding): rpc service scaffolding for nullifier queries
Browse files Browse the repository at this point in the history
  • Loading branch information
TalDerei committed Feb 5, 2025
1 parent 8bec0e9 commit 8f578f9
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion crates/core/component/funding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ component = [
"penumbra-sdk-shielded-pool/component",
"penumbra-sdk-stake/component",
"metrics",
"futures"
"futures",
"tonic",
]
default = ["component"]
parallel = [
Expand Down Expand Up @@ -62,6 +63,7 @@ penumbra-sdk-txhash = {workspace = true, default-features = false}
serde = {workspace = true, features = ["derive"]}
tendermint = {workspace = true}
tracing = {workspace = true}
tonic = {workspace = true, optional = true}

[dev-dependencies]
proptest = {workspace = true}
1 change: 1 addition & 0 deletions crates/core/component/funding/src/component.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod metrics;
pub mod rpc;
mod state_key;
pub mod view;
use ::metrics::{gauge, histogram};
Expand Down
55 changes: 55 additions & 0 deletions crates/core/component/funding/src/component/rpc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use cnidarium::Storage;
use penumbra_sdk_proto::core::component::funding::v1::{
self as pb, funding_service_server::FundingService,
};
use penumbra_sdk_sct::Nullifier;

use super::liquidity_tournament::nullifier::NullifierRead;

pub struct Server {
storage: Storage,
}

impl Server {
pub fn new(storage: Storage) -> Self {
Self { storage }
}
}

#[tonic::async_trait]
impl FundingService for Server {
async fn lqt_current_epoch_voted(
&self,
request: tonic::Request<pb::LqtCurrentEpochVotedRequest>,
) -> Result<tonic::Response<pb::LqtCurrentEpochVotedResponse>, tonic::Status> {
// Retrieve latest state snapshot.
let state = self.storage.latest_snapshot();

let req_nullifier = request.into_inner();
let nullifier_proto = req_nullifier
.nullifier
.ok_or_else(|| tonic::Status::invalid_argument("missing nullifier"))?;

// Proto to domain type conversion.
let nullifier: Nullifier = nullifier_proto
.try_into()
.map_err(|e| tonic::Status::invalid_argument(format!("invalid nullifier: {e}")))?;

// Perform a state nullifier lookup.
let maybe_tx_id = state.get_lqt_spent_nullifier(nullifier).await;

if let Some(tx_id) = maybe_tx_id {
// Nullifier was spent and user has already voted.
Ok(tonic::Response::new(pb::LqtCurrentEpochVotedResponse {
tx_id: Some(tx_id.into()),
already_voted: true,
}))
} else {
// Nullifier was not spent and user has not voted yet.
Ok(tonic::Response::new(pb::LqtCurrentEpochVotedResponse {
tx_id: None,
already_voted: false,
}))
}
}
}

0 comments on commit 8f578f9

Please sign in to comment.