Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
cronokirby committed Apr 18, 2024
1 parent 1376d4b commit bb3e620
Show file tree
Hide file tree
Showing 35 changed files with 157 additions and 50 deletions.
1 change: 1 addition & 0 deletions crates/bench/benches/swap_claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ fn swap_claim_proving_time(c: &mut Criterion) {
height: height.into(),
trading_pair: swap_plaintext.trading_pair,
epoch_starting_height: (epoch_duration * position.epoch()).into(),
sct_position_prefix: position,
};
let (lambda_1, lambda_2) = output_data.pro_rata_outputs((delta_1_i, delta_2_i));

Expand Down
1 change: 1 addition & 0 deletions crates/bin/pcli/tests/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ fn swap_claim_parameters_vs_current_swap_claim_circuit() {
height: height.into(),
trading_pair: swap_plaintext.trading_pair,
epoch_starting_height: (epoch_duration * position.epoch()).into(),
sct_position_prefix: position,
};
let (lambda_1, lambda_2) = output_data.pro_rata_outputs((delta_1_i, delta_2_i));

Expand Down
Binary file modified crates/cnidarium/src/gen/proto_descriptor.bin.no_lfs
Binary file not shown.
39 changes: 32 additions & 7 deletions crates/core/component/dex/src/batch_swap_output_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use ark_r1cs_std::{
use ark_relations::r1cs::{ConstraintSystemRef, SynthesisError};
use decaf377::{r1cs::FqVar, Fq};
use penumbra_proto::{penumbra::core::component::dex::v1 as pb, DomainType};
use penumbra_tct::Position;
use serde::{Deserialize, Serialize};

use penumbra_num::fixpoint::{bit_constrain, U128x128, U128x128Var};
Expand Down Expand Up @@ -38,6 +39,8 @@ pub struct BatchSwapOutputData {
pub trading_pair: TradingPair,
/// The starting block height of the epoch for which the batch swap data is valid.
pub epoch_starting_height: u64,
/// The position prefix where this batch swap occurred. The commitment index must be 0.
pub sct_position_prefix: Position,
}

impl BatchSwapOutputData {
Expand Down Expand Up @@ -142,9 +145,9 @@ pub struct BatchSwapOutputDataVar {
pub lambda_2: U128x128Var,
pub unfilled_1: U128x128Var,
pub unfilled_2: U128x128Var,
pub height: FqVar,
pub trading_pair: TradingPairVar,
pub epoch_starting_height: FqVar,
pub epoch: FqVar,
pub block: FqVar,
}

impl AllocVar<BatchSwapOutputData, Fq> for BatchSwapOutputDataVar {
Expand All @@ -168,9 +171,19 @@ impl AllocVar<BatchSwapOutputData, Fq> for BatchSwapOutputDataVar {
let unfilled_1 = U128x128Var::new_variable(cs.clone(), || Ok(unfilled_1_fixpoint), mode)?;
let unfilled_2_fixpoint: U128x128 = output_data.unfilled_2.into();
let unfilled_2 = U128x128Var::new_variable(cs.clone(), || Ok(unfilled_2_fixpoint), mode)?;
let height = FqVar::new_variable(cs.clone(), || Ok(Fq::from(output_data.height)), mode)?;
// Check the height is 64 bits
let _ = bit_constrain(height.clone(), 64);
let epoch = FqVar::new_variable(
cs.clone(),
|| Ok(Fq::from(output_data.sct_position_prefix.epoch())),
mode,
)?;
let block = FqVar::new_variable(
cs.clone(),
|| Ok(Fq::from(output_data.sct_position_prefix.block())),
mode,
)?;
// Check that epoch and block are 16 bits
let _ = bit_constrain(epoch.clone(), 16);
let _ = bit_constrain(block.clone(), 16);
let trading_pair = TradingPairVar::new_variable_unchecked(
cs.clone(),
|| Ok(output_data.trading_pair),
Expand All @@ -189,8 +202,8 @@ impl AllocVar<BatchSwapOutputData, Fq> for BatchSwapOutputDataVar {
unfilled_1,
unfilled_2,
trading_pair,
height,
epoch_starting_height,
epoch,
block,
})
}
}
Expand All @@ -211,6 +224,7 @@ impl From<BatchSwapOutputData> for pb::BatchSwapOutputData {
height: s.height,
epoch_starting_height: s.epoch_starting_height,
trading_pair: Some(s.trading_pair.into()),
sct_position_prefix: s.sct_position_prefix.into(),
}
}
}
Expand Down Expand Up @@ -276,6 +290,14 @@ impl From<BatchSwapOutputData> for pb::BatchSwapOutputDataResponse {
impl TryFrom<pb::BatchSwapOutputData> for BatchSwapOutputData {
type Error = anyhow::Error;
fn try_from(s: pb::BatchSwapOutputData) -> Result<Self, Self::Error> {
let sct_position_prefix = {
let prefix = Position::from(s.sct_position_prefix);
anyhow::ensure!(
prefix.commitment() == 0,
"sct_position_prefix.commitment() != 0"
);
prefix
};
Ok(Self {
delta_1: s
.delta_1
Expand Down Expand Up @@ -307,6 +329,7 @@ impl TryFrom<pb::BatchSwapOutputData> for BatchSwapOutputData {
.ok_or_else(|| anyhow!("Missing trading_pair"))?
.try_into()?,
epoch_starting_height: s.epoch_starting_height,
sct_position_prefix,
})
}
}
Expand Down Expand Up @@ -424,6 +447,7 @@ mod tests {
height: 1,
trading_pair,
epoch_starting_height: 1,
sct_position_prefix: Position::default(),
},
}
}
Expand All @@ -445,6 +469,7 @@ mod tests {
height: 0u64,
trading_pair,
epoch_starting_height: 0u64,
sct_position_prefix: Position::default(),
};

// Now suppose our user's contribution is:
Expand Down
12 changes: 11 additions & 1 deletion crates/core/component/dex/src/component/circuit_breaker/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ mod tests {
height: 1,
trading_pair: pair_1.into_directed_trading_pair().into(),
epoch_starting_height: 0,
sct_position_prefix: Default::default(),
},
None,
None,
Expand Down Expand Up @@ -250,7 +251,16 @@ mod tests {
let routing_params = state.routing_params().await.unwrap();
// This call should panic due to the outflow of gn not being covered by the circuit breaker.
state
.handle_batch_swaps(trading_pair, swap_flow, 0, 0, routing_params)
.handle_batch_swaps(
trading_pair,
swap_flow,
0,
Epoch {
index: 0,
start_height: 0,
},
routing_params,
)
.await
.expect("unable to process batch swaps");
}
Expand Down
2 changes: 1 addition & 1 deletion crates/core/component/dex/src/component/dex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl Component for Dex {
.height
.try_into()
.expect("height is part of the end block data"),
current_epoch.start_height,
current_epoch,
// Always include both ends of the target pair as fixed candidates.
routing_params
.clone()
Expand Down
21 changes: 11 additions & 10 deletions crates/core/component/dex/src/component/router/route_and_fill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use async_trait::async_trait;
use cnidarium::StateWrite;
use penumbra_asset::{asset, Value};
use penumbra_num::Amount;
use penumbra_sct::epoch::Epoch;
use tracing::instrument;

use crate::{
Expand All @@ -23,21 +24,14 @@ use super::fill_route::FillError;
/// a block's batch swap flows.
#[async_trait]
pub trait HandleBatchSwaps: StateWrite + Sized {
#[instrument(skip(
self,
trading_pair,
batch_data,
block_height,
epoch_starting_height,
params
))]
#[instrument(skip(self, trading_pair, batch_data, block_height, epoch, params))]
async fn handle_batch_swaps(
self: &mut Arc<Self>,
trading_pair: TradingPair,
batch_data: SwapFlow,
// TODO: why not read these 2 from the state?
block_height: u64,
epoch_starting_height: u64,
epoch: Epoch,
params: RoutingParams,
) -> Result<()>
where
Expand Down Expand Up @@ -97,14 +91,21 @@ pub trait HandleBatchSwaps: StateWrite + Sized {
};
let output_data = BatchSwapOutputData {
height: block_height,
epoch_starting_height,
epoch_starting_height: epoch.start_height,
trading_pair,
delta_1,
delta_2,
lambda_1,
lambda_2,
unfilled_1,
unfilled_2,
sct_position_prefix: (
u16::try_from(epoch.index).expect("epoch index should be small enough"),
u16::try_from(block_height - epoch.start_height)
.expect("block index should be small enough"),
0,
)
.into(),
};

// Fetch the swap execution object that should have been modified during the routing and filling.
Expand Down
23 changes: 21 additions & 2 deletions crates/core/component/dex/src/component/router/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use core::panic;
use futures::StreamExt;
use penumbra_asset::{asset, Value};
use penumbra_num::{fixpoint::U128x128, Amount};
use penumbra_sct::epoch::Epoch;
use rand_core::OsRng;
use std::sync::Arc;

Expand Down Expand Up @@ -1024,7 +1025,16 @@ async fn best_position_route_and_fill() -> anyhow::Result<()> {
.unwrap();
let routing_params = state.routing_params().await.unwrap();
state
.handle_batch_swaps(trading_pair, swap_flow, 0u32.into(), 0, routing_params)
.handle_batch_swaps(
trading_pair,
swap_flow,
0u32.into(),
Epoch {
index: 0,
start_height: 0,
},
routing_params,
)
.await
.expect("unable to process batch swaps");

Expand Down Expand Up @@ -1165,7 +1175,16 @@ async fn multi_hop_route_and_fill() -> anyhow::Result<()> {
.unwrap();
let routing_params = state.routing_params().await.unwrap();
state
.handle_batch_swaps(trading_pair, swap_flow, 0u32.into(), 0, routing_params)
.handle_batch_swaps(
trading_pair,
swap_flow,
0u32.into(),
Epoch {
index: 0,
start_height: 0,
},
routing_params,
)
.await
.expect("unable to process batch swaps");

Expand Down
24 changes: 22 additions & 2 deletions crates/core/component/dex/src/component/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use cnidarium::{ArcStateDeltaExt, StateDelta, TempStorage};
use futures::StreamExt;
use penumbra_asset::{asset, Value};
use penumbra_num::Amount;
use penumbra_sct::epoch::Epoch;
use rand_core::OsRng;

use crate::lp::action::PositionOpen;
Expand Down Expand Up @@ -632,7 +633,16 @@ async fn swap_execution_tests() -> anyhow::Result<()> {
.unwrap();
let routing_params = state.routing_params().await.unwrap();
state
.handle_batch_swaps(trading_pair, swap_flow, 0, 0, routing_params)
.handle_batch_swaps(
trading_pair,
swap_flow,
0,
Epoch {
index: 0,
start_height: 0,
},
routing_params,
)
.await
.expect("unable to process batch swaps");

Expand Down Expand Up @@ -740,7 +750,16 @@ async fn swap_execution_tests() -> anyhow::Result<()> {
.unwrap();
let routing_params = state.routing_params().await.unwrap();
state
.handle_batch_swaps(trading_pair, swap_flow, 0u32.into(), 0, routing_params)
.handle_batch_swaps(
trading_pair,
swap_flow,
0u32.into(),
Epoch {
index: 0,
start_height: 0,
},
routing_params,
)
.await
.expect("unable to process batch swaps");

Expand All @@ -758,6 +777,7 @@ async fn swap_execution_tests() -> anyhow::Result<()> {
height: 0,
epoch_starting_height: 0,
trading_pair,
sct_position_prefix: Default::default(),
}
);

Expand Down
14 changes: 9 additions & 5 deletions crates/core/component/dex/src/swap_claim/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,12 @@ impl ConstraintSynthesizer<Fq> for SwapClaimCircuit {
claimed_fee_var.enforce_equal(&swap_plaintext_var.claim_fee)?;

// Validate the swap commitment's height matches the output data's height (i.e. the clearing price height).
let block = position_var.block()?;
let note_commitment_block_height_var =
output_data_var.epoch_starting_height.clone() + block;
output_data_var
.height
.enforce_equal(&note_commitment_block_height_var)?;
.block
.enforce_equal(&position_var.block()?)?;
output_data_var
.epoch
.enforce_equal(&position_var.epoch()?)?;

// Validate that the output data's trading pair matches the note commitment's trading pair.
output_data_var
Expand Down Expand Up @@ -360,6 +360,7 @@ impl DummyWitness for SwapClaimCircuit {
height: 0,
trading_pair: swap_plaintext.trading_pair,
epoch_starting_height: 0,
sct_position_prefix: Default::default(),
};
let note_blinding_1 = Fq::from(1);
let note_blinding_2 = Fq::from(1);
Expand Down Expand Up @@ -643,6 +644,7 @@ mod tests {
height: height.into(),
trading_pair: swap_plaintext.trading_pair,
epoch_starting_height: (epoch_duration * position.epoch()).into(),
sct_position_prefix: Default::default(),
};
let (lambda_1, lambda_2) = output_data.pro_rata_outputs((delta_1_i, delta_2_i));

Expand Down Expand Up @@ -775,6 +777,7 @@ mod tests {
height: height.into(),
trading_pair: swap_plaintext.trading_pair,
epoch_starting_height: (epoch_duration * position.epoch()).into(),
sct_position_prefix: Default::default()
};
let (lambda_1, lambda_2) = output_data.pro_rata_outputs((delta_1_i, delta_2_i));

Expand Down Expand Up @@ -875,6 +878,7 @@ mod tests {
height: height.into(),
trading_pair: swap_plaintext.trading_pair,
epoch_starting_height: (epoch_duration * dummy_position.epoch()).into(),
sct_position_prefix: Default::default()
};
let (lambda_1, lambda_2) = output_data.pro_rata_outputs((delta_1_i, delta_2_i));

Expand Down
4 changes: 2 additions & 2 deletions crates/crypto/proof-params/src/gen/convert_id.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

pub const PROVING_KEY_ID: &'static str = "groth16pk1x4m7qnm4czf3mtys3s8p8fnzpcz22fv26udz7cgql3x047rqn6nqquqh7t";
pub const VERIFICATION_KEY_ID: &'static str = "groth16vk1eu7tznfakx6dh3kzfrtfzt034ypzc69eaqdmjptyayxjqp0jt84s94hy8m";
pub const PROVING_KEY_ID: &'static str = "groth16pk13glytj98ck9a92a7m3585sal6qkj8k5ukgq338r9zztd0kyg3lhsd3hkkq";
pub const VERIFICATION_KEY_ID: &'static str = "groth16vk1aavxh280mlezjh7zchdpdqhn9psv7hh400hskyaz330r75mejgus9h4xhn";
2 changes: 1 addition & 1 deletion crates/crypto/proof-params/src/gen/convert_pk.bin
Git LFS file not shown
Binary file modified crates/crypto/proof-params/src/gen/convert_vk.param
Binary file not shown.
4 changes: 2 additions & 2 deletions crates/crypto/proof-params/src/gen/delegator_vote_id.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

pub const PROVING_KEY_ID: &'static str = "groth16pk1wquddkhlkp3tj3fsjt7qtgaevfpndp2ejptvp7lxhafa0ddxqetqtp52ja";
pub const VERIFICATION_KEY_ID: &'static str = "groth16vk1j8jt9leawttxhqdye26apdtfkju4nmhzf40y3339g774ca73qyusq7wpcn";
pub const PROVING_KEY_ID: &'static str = "groth16pk1zrtc7z3e3nd6tf0k56l69rwurwf9lluwkxz0t86sman06r72r5cs2z0w35";
pub const VERIFICATION_KEY_ID: &'static str = "groth16vk1mefd3cm8z6zrwv6x85aywjdce2e0w97gzzy60w50a9gw3r7y5cdqxn043r";
2 changes: 1 addition & 1 deletion crates/crypto/proof-params/src/gen/delegator_vote_pk.bin
Git LFS file not shown
Binary file modified crates/crypto/proof-params/src/gen/delegator_vote_vk.param
Binary file not shown.
4 changes: 2 additions & 2 deletions crates/crypto/proof-params/src/gen/nullifier_derivation_id.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

pub const PROVING_KEY_ID: &'static str = "groth16pk1e79gm2pkg5rlkyeectfh4yualrjy9qdlez22l4f56g5tjqkqfv3syrrzsm";
pub const VERIFICATION_KEY_ID: &'static str = "groth16vk1ylw2upegj9ppaz9s0hd0czh8yeqxvqc4s39468733cnfcch2cfwqnm4gqj";
pub const PROVING_KEY_ID: &'static str = "groth16pk1quffd9q3qr9rz7002s99r2m56lpx8lsyaea080sqxp38vkw75w8qul72gh";
pub const VERIFICATION_KEY_ID: &'static str = "groth16vk1puayhurhsvfz25x64shg0zhjtgv0xgrj99q9a3z0sw2gea0t7edsyew60m";
Git LFS file not shown
Binary file modified crates/crypto/proof-params/src/gen/nullifier_derivation_vk.param
Binary file not shown.
Loading

0 comments on commit bb3e620

Please sign in to comment.