Skip to content

Commit

Permalink
Add mechanism for using meta params to update fmd params periodically
Browse files Browse the repository at this point in the history
  • Loading branch information
cronokirby committed Mar 29, 2024
1 parent 188bdcc commit 8738ac9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
26 changes: 23 additions & 3 deletions crates/core/component/shielded-pool/src/component/shielded_pool.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::sync::Arc;

use crate::fmd::should_update_fmd_params;
use crate::params::ShieldedPoolParameters;
use crate::{fmd, genesis, state_key};
use anyhow::anyhow;
Expand Down Expand Up @@ -62,11 +63,30 @@ impl Component for ShieldedPool {
) {
}

#[instrument(name = "shielded_pool", skip(_state, _end_block))]
#[instrument(name = "shielded_pool", skip(state, end_block))]
async fn end_block<S: StateWrite + 'static>(
_state: &mut Arc<S>,
_end_block: &abci::request::EndBlock,
state: &mut Arc<S>,
end_block: &abci::request::EndBlock,
) {
let height: u64 = end_block
.height
.try_into()
.expect("height should not be negative");
if should_update_fmd_params(height) {
let state = Arc::get_mut(state).expect("the state should not be shared");
let meta_params = state
.get_shielded_pool_params()
.await
.expect("should be able to read state")
.fmd_meta_params;
let old = state
.get_current_fmd_parameters()
.await
.expect("should be able to read state");
let new = meta_params.updated_fmd_params(&old, height);
state.put_previous_fmd_parameters(old);
state.put_current_fmd_parameters(new);
}
}

async fn end_epoch<S: StateWrite + 'static>(mut _state: &mut Arc<S>) -> Result<()> {
Expand Down
27 changes: 26 additions & 1 deletion crates/core/component/shielded-pool/src/fmd.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
use anyhow::anyhow;
use decaf377_fmd::Precision;
use penumbra_proto::{core::component::shielded_pool::v1 as pb, DomainType};
use penumbra_proto::{
core::component::shielded_pool::v1::{self as pb},
DomainType,
};
use serde::{Deserialize, Serialize};

pub mod state_key;

/// How long users have to switch to updated parameters.
pub const FMD_GRACE_PERIOD_BLOCKS: u64 = 1 << 4;
/// How often we update the params.
pub const FMD_UPDATE_FREQUENCY_BLOCKS: u64 = 1 << 6;
/// How many blocks we expect per day, approximately.
const _BLOCKS_PER_DAY: u64 = 1 << 13;

pub fn should_update_fmd_params(height: u64) -> bool {
height % FMD_UPDATE_FREQUENCY_BLOCKS == 0
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(try_from = "pb::FmdParameters", into = "pb::FmdParameters")]
pub struct Parameters {
Expand Down Expand Up @@ -88,3 +102,14 @@ impl Default for MetaParameters {
Self::Fixed(Precision::default())
}
}

impl MetaParameters {
pub fn updated_fmd_params(&self, _old: &Parameters, height: u64) -> Parameters {
match *self {
MetaParameters::Fixed(precision) => Parameters {
precision,
as_of_block_height: height,
},
}
}
}

0 comments on commit 8738ac9

Please sign in to comment.