Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shielded-pool(component): simplify tracking of fmd parameters #3729

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion crates/core/app/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,9 @@ impl App {
state_tx.put_ibc_params(ibc_params);
}
if let Some(shielded_pool_params) = app_params.new.shielded_pool_params {
state_tx.put_shielded_pool_params(shielded_pool_params);
state_tx
.put_shielded_pool_params(shielded_pool_params)
.await;
}
if let Some(sct_params) = app_params.new.sct_params {
state_tx.put_sct_params(sct_params);
Expand Down
59 changes: 36 additions & 23 deletions crates/core/component/shielded-pool/src/component/shielded_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ impl Component for ShieldedPool {
match app_state {
None => { /* Checkpoint -- no-op */ }
Some(genesis) => {
// MERGEBLOCK(erwan): the handling of those parameters is a bit weird.
// rationalize it before merging
state.put_shielded_pool_params(genesis.shielded_pool_params.clone());
state.put_current_fmd_parameters(fmd::Parameters::default());
state.put_previous_fmd_parameters(fmd::Parameters::default());
state
.put_shielded_pool_params(genesis.shielded_pool_params.clone())
.await;

// Register a denom for each asset in the genesis state
for allocation in &genesis.allocations {
Expand Down Expand Up @@ -79,17 +77,23 @@ impl Component for ShieldedPool {
/// Extension trait providing read access to shielded pool data.
#[async_trait]
pub trait StateReadExt: StateRead {
/// Retrieve the current FMD parameters.
///
/// # Errors
/// Returns an error if the current FMD parameters are not present.
async fn get_current_fmd_parameters(&self) -> Result<fmd::Parameters> {
self.get(fmd::state_key::parameters::current())
.await?
.ok_or_else(|| anyhow!("Missing FmdParameters"))
}

/// Gets the previous FMD parameters from the JMT.
async fn get_previous_fmd_parameters(&self) -> Result<fmd::Parameters> {
/// Retrieve the previous FMD parameters.
/// Returns `None` if the previous FMD parameters are not present e.g.
/// if the chain is at height 0.
async fn get_previous_fmd_parameters(&self) -> Option<fmd::Parameters> {
self.get(fmd::state_key::parameters::previous())
.await?
.ok_or_else(|| anyhow!("Missing FmdParameters"))
.await
.expect("deserialization does not fail")
}

async fn get_shielded_pool_params(&self) -> Result<ShieldedPoolParameters> {
Expand All @@ -109,21 +113,30 @@ impl<T: StateRead + ?Sized> StateReadExt for T {}
/// Extension trait providing write access to shielded pool data.
#[async_trait]
pub trait StateWriteExt: StateWrite + StateReadExt {
fn put_shielded_pool_params(&mut self, params: ShieldedPoolParameters) {
/// Write the shielded pool component parameters, updating the FMD indices.
async fn put_shielded_pool_params(&mut self, params: ShieldedPoolParameters) {
self.object_put(crate::state_key::shielded_pool_params_updated(), ());
self.put(crate::state_key::shielded_pool_params().into(), params)
}

/// Writes the current FMD parameters to the JMT.
fn put_current_fmd_parameters(&mut self, params: fmd::Parameters) {
// MERGEBLOCK(erwan): read through the update mechanism for FMD params
// do we need to flag that shielded pool params were updated?
self.put(fmd::state_key::parameters::current().into(), params)
}

/// Writes the previous FMD parameters to the JMT.
fn put_previous_fmd_parameters(&mut self, params: fmd::Parameters) {
self.put(fmd::state_key::parameters::previous().into(), params)
self.put(
crate::state_key::shielded_pool_params().into(),
params.clone(),
);

// Update the FMD parameters, tracking both the current and previous values.
// We store the previous FMD parameters to allow txs to validate even if their
// clue precision bits follows a slightly outdated FMD spec.
// See: [`penumbra_app::action_handler::transaction::stateful::FMD_GRACE_PERIOD_BLOCKS`]
let previous_fmd_params = self
.get_previous_fmd_parameters()
.await
.unwrap_or_else(|| params.fmd_params.clone());
self.put(
fmd::state_key::parameters::previous().into(),
previous_fmd_params,
);
self.put(
fmd::state_key::parameters::current().into(),
params.fmd_params,
);
}
}

Expand Down
Loading