From d4913fa5d880bab096c445f256de1af686cc344d Mon Sep 17 00:00:00 2001 From: Lucas Meier Date: Fri, 29 Mar 2024 13:49:58 -0700 Subject: [PATCH] Add mechanism for using meta params to update fmd params periodically --- .../src/component/shielded_pool.rs | 26 +++++++++++++++--- .../core/component/shielded-pool/src/fmd.rs | 27 ++++++++++++++++++- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/crates/core/component/shielded-pool/src/component/shielded_pool.rs b/crates/core/component/shielded-pool/src/component/shielded_pool.rs index a565a3c253..4c0c809fac 100644 --- a/crates/core/component/shielded-pool/src/component/shielded_pool.rs +++ b/crates/core/component/shielded-pool/src/component/shielded_pool.rs @@ -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; @@ -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( - _state: &mut Arc, - _end_block: &abci::request::EndBlock, + state: &mut Arc, + 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(mut _state: &mut Arc) -> Result<()> { diff --git a/crates/core/component/shielded-pool/src/fmd.rs b/crates/core/component/shielded-pool/src/fmd.rs index 0d0ac6671f..e8e1541949 100644 --- a/crates/core/component/shielded-pool/src/fmd.rs +++ b/crates/core/component/shielded-pool/src/fmd.rs @@ -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 { @@ -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, + }, + } + } +}