Skip to content

Commit

Permalink
Look up swap from local storage in planner based on commitment
Browse files Browse the repository at this point in the history
  • Loading branch information
zbuc authored and conorsch committed Aug 18, 2023
1 parent e1f02dd commit 828f33b
Show file tree
Hide file tree
Showing 8 changed files with 758 additions and 723 deletions.
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.

8 changes: 5 additions & 3 deletions crates/proto/src/gen/penumbra.view.v1alpha1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,12 @@ pub mod transaction_planner_request {
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct SwapClaim {
/// Plan for the swap to be claimed.
/// SwapCommitment to identify the Swap to be claimed.
/// Use the commitment from the Swap message:
/// penumbra.core.dex.v1alpha1.Swap.body.payload.commitment.
#[prost(message, optional, tag = "1")]
pub swap_claim_plan: ::core::option::Option<
super::super::super::core::dex::v1alpha1::SwapClaimPlan,
pub swap_commitment: ::core::option::Option<
super::super::super::core::crypto::v1alpha1::StateCommitment,
>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
Expand Down
26 changes: 13 additions & 13 deletions crates/proto/src/gen/penumbra.view.v1alpha1.serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5555,12 +5555,12 @@ impl serde::Serialize for transaction_planner_request::SwapClaim {
{
use serde::ser::SerializeStruct;
let mut len = 0;
if self.swap_claim_plan.is_some() {
if self.swap_commitment.is_some() {
len += 1;
}
let mut struct_ser = serializer.serialize_struct("penumbra.view.v1alpha1.TransactionPlannerRequest.SwapClaim", len)?;
if let Some(v) = self.swap_claim_plan.as_ref() {
struct_ser.serialize_field("swapClaimPlan", v)?;
if let Some(v) = self.swap_commitment.as_ref() {
struct_ser.serialize_field("swapCommitment", v)?;
}
struct_ser.end()
}
Expand All @@ -5572,13 +5572,13 @@ impl<'de> serde::Deserialize<'de> for transaction_planner_request::SwapClaim {
D: serde::Deserializer<'de>,
{
const FIELDS: &[&str] = &[
"swap_claim_plan",
"swapClaimPlan",
"swap_commitment",
"swapCommitment",
];

#[allow(clippy::enum_variant_names)]
enum GeneratedField {
SwapClaimPlan,
SwapCommitment,
}
impl<'de> serde::Deserialize<'de> for GeneratedField {
fn deserialize<D>(deserializer: D) -> std::result::Result<GeneratedField, D::Error>
Expand All @@ -5600,7 +5600,7 @@ impl<'de> serde::Deserialize<'de> for transaction_planner_request::SwapClaim {
E: serde::de::Error,
{
match value {
"swapClaimPlan" | "swap_claim_plan" => Ok(GeneratedField::SwapClaimPlan),
"swapCommitment" | "swap_commitment" => Ok(GeneratedField::SwapCommitment),
_ => Err(serde::de::Error::unknown_field(value, FIELDS)),
}
}
Expand All @@ -5620,19 +5620,19 @@ impl<'de> serde::Deserialize<'de> for transaction_planner_request::SwapClaim {
where
V: serde::de::MapAccess<'de>,
{
let mut swap_claim_plan__ = None;
let mut swap_commitment__ = None;
while let Some(k) = map.next_key()? {
match k {
GeneratedField::SwapClaimPlan => {
if swap_claim_plan__.is_some() {
return Err(serde::de::Error::duplicate_field("swapClaimPlan"));
GeneratedField::SwapCommitment => {
if swap_commitment__.is_some() {
return Err(serde::de::Error::duplicate_field("swapCommitment"));
}
swap_claim_plan__ = map.next_value()?;
swap_commitment__ = map.next_value()?;
}
}
}
Ok(transaction_planner_request::SwapClaim {
swap_claim_plan: swap_claim_plan__,
swap_commitment: swap_commitment__,
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/proto/src/gen/proto_descriptor.bin
Git LFS file not shown
38 changes: 21 additions & 17 deletions crates/view/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,37 @@ publish = false
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = []
default = ["std"]
# When this feature is enabled, the view worker will request every single
# SCT root, to pinpoint exactly where any SCT root divergence occurs.
sct-divergence-check = []
std = ["ark-std/std"]

[dependencies]
# Workspace dependencies
penumbra-proto = { path = "../proto", features = ["rpc"] }
penumbra-tct = { path = "../crypto/tct" }
penumbra-num = { path = "../core/num" }
penumbra-keys = { path = "../core/keys" }
penumbra-asset = { path = "../core/asset" }
penumbra-chain = { path = "../core/component/chain" }
penumbra-shielded-pool = { path = "../core/component/shielded-pool", default-features = false, features = ["proving-keys"] }
penumbra-stake = { path = "../core/component/stake", default-features = false }
penumbra-ibc = { path = "../core/component/ibc", default-features = false }
penumbra-dao = { path = "../core/component/dao", default-features = false }
penumbra-dex = { path = "../core/component/dex", default-features = false }
penumbra-sct = { path = "../core/component/sct", default-features = false }
penumbra-fee = { path = "../core/component/fee", default-features = false }
penumbra-proto = { path = "../proto", features = ["rpc"] }
penumbra-tct = { path = "../crypto/tct" }
penumbra-num = { path = "../core/num" }
penumbra-keys = { path = "../core/keys" }
penumbra-asset = { path = "../core/asset" }
penumbra-chain = { path = "../core/component/chain" }
penumbra-shielded-pool = { path = "../core/component/shielded-pool", default-features = false, features = [
"proving-keys",
] }
penumbra-stake = { path = "../core/component/stake", default-features = false }
penumbra-ibc = { path = "../core/component/ibc", default-features = false }
penumbra-dao = { path = "../core/component/dao", default-features = false }
penumbra-dex = { path = "../core/component/dex", default-features = false }
penumbra-sct = { path = "../core/component/sct", default-features = false }
penumbra-fee = { path = "../core/component/fee", default-features = false }
penumbra-compact-block = { path = "../core/component/compact-block", default-features = false }
penumbra-app = { path = "../core/app" }
penumbra-transaction = { path = "../core/transaction" }
penumbra-app = { path = "../core/app" }
penumbra-transaction = { path = "../core/transaction" }

ibc-types = { version = "0.3.0", default-features = false }

decaf377 = {version = "0.5", features = ["r1cs"] }
ark-std = { version = "0.4", default-features = false }
decaf377 = { version = "0.5", features = ["r1cs"] }
tokio = { version = "1.22", features = ["full"] }
tokio-stream = { version = "0.1.8", features = ["sync"] }
anyhow = "1"
Expand Down
38 changes: 31 additions & 7 deletions crates/view/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ use std::{
};

use anyhow::{anyhow, Context};
use ark_std::UniformRand;
use async_stream::try_stream;
use camino::Utf8Path;
use decaf377::Fq;
use futures::stream::{StreamExt, TryStreamExt};
use penumbra_asset::{asset, Value};
use penumbra_dex::{
Expand Down Expand Up @@ -356,6 +358,11 @@ impl ViewProtocolService for ViewService {
) -> Result<tonic::Response<pb::TransactionPlannerResponse>, tonic::Status> {
let prq = request.into_inner();

let chain_params =
self.storage.chain_params().await.map_err(|e| {
tonic::Status::internal(format!("could not get chain params: {:#}", e))
})?;

let mut planner = Planner::new(OsRng);
planner
.fee(
Expand Down Expand Up @@ -431,17 +438,34 @@ impl ViewProtocolService for ViewService {
}

for swap_claim in prq.swap_claims {
let swap_claim_plan: SwapClaimPlan = swap_claim
.swap_claim_plan
.ok_or_else(|| tonic::Status::invalid_argument("Missing swap claim plan"))?
let swap_commitment: StateCommitment = swap_claim
.swap_commitment
.ok_or_else(|| tonic::Status::invalid_argument("Missing swap commitment"))?
.try_into()
.map_err(|e| {
tonic::Status::invalid_argument(format!(
"Could not parse swap claim plan: {e:#}"
"Could not parse swap commitment: {e:#}"
))
})?;
let swap_record = self
.storage
// TODO: should there be a timeout on detection here instead?
.swap_by_commitment(swap_commitment, false)
.await
.map_err(|e| {
tonic::Status::invalid_argument(format!(
"Could not fetch swap by commitment: {e:#}"
))
})?;

planner.swap_claim(swap_claim_plan);
planner.swap_claim(SwapClaimPlan {
swap_plaintext: swap_record.swap,
position: swap_record.position,
output_data: swap_record.output_data,
epoch_duration: chain_params.epoch_duration,
proof_blinding_r: Fq::rand(&mut OsRng),
proof_blinding_s: Fq::rand(&mut OsRng),
});
}

for delegation in prq.delegations {
Expand Down Expand Up @@ -538,11 +562,11 @@ impl ViewProtocolService for ViewService {
planner.position_withdraw(position_id, reserves, trading_pair);
}

let mut client_of_self =
ViewProtocolServiceClient::new(ViewProtocolServiceServer::new(self.clone()));
let fvk = self.storage.full_viewing_key().await.map_err(|e| {
tonic::Status::failed_precondition(format!("Error retrieving full viewing key: {e:#}"))
})?;
let mut client_of_self =
ViewProtocolServiceClient::new(ViewProtocolServiceServer::new(self.clone()));
let plan = planner
.plan(&mut client_of_self, fvk.account_group_id(), 0u32.into())
.await
Expand Down
Loading

0 comments on commit 828f33b

Please sign in to comment.