Skip to content

Commit

Permalink
Refactors is_masp_new_epoch
Browse files Browse the repository at this point in the history
  • Loading branch information
grarco committed May 27, 2024
1 parent 58d5796 commit e1e6ff3
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 14 deletions.
11 changes: 11 additions & 0 deletions crates/core/src/masp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ impl MaspEpoch {
Some(Self(self.0.checked_sub(1)?))
}

/// Check if the given epoch is also a new masp epoch based on the
/// multiplier provided
pub fn is_masp_new_epoch(epoch: Epoch, masp_epoch_multiplier: u64) -> bool {
matches!(
Self::from_epoch(epoch, masp_epoch_multiplier)
.0
.checked_rem(masp_epoch_multiplier),
Some(Epoch(0))
)
}

/// Initialize a new masp epoch from the provided one
#[cfg(any(test, feature = "testing"))]
pub fn new(epoch: u64) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion crates/namada/src/ledger/native_vp/masp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ where
)?
.ok_or_else(|| {
Error::NativeVpError(native_vp::Error::SimpleMessage(
"Could not deserialize masp epoch multiplier",
"Missing expected masp epoch multiplier parameter",
))
})?;
let masp_epoch = MaspEpoch::from_epoch(
Expand Down
2 changes: 1 addition & 1 deletion crates/node/src/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ where

// Begin the new block and check if a new epoch has begun
let (height, new_epoch) = self.update_state(req.header);
let is_masp_new_epoch = self.state.is_masp_new_epoch(new_epoch);
let is_masp_new_epoch = self.state.is_masp_new_epoch(new_epoch)?;

let (current_epoch, _gas) = self.state.in_mem().get_current_epoch();
let update_for_tendermint = matches!(
Expand Down
2 changes: 1 addition & 1 deletion crates/sdk/src/queries/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ where
)?
.ok_or_else(|| {
namada_storage::Error::new_const(
"Could not deserialize masp epoch multiplier",
"Missing expected masp epoch multiplier parameter",
)
})?;
Ok(MaspEpoch::from_epoch(epoch, masp_epoch_multiplier))
Expand Down
31 changes: 20 additions & 11 deletions crates/state/src/wl_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use namada_core::address::Address;
use namada_core::arith::checked;
use namada_core::borsh::BorshSerializeExt;
use namada_core::chain::ChainId;
use namada_core::masp::MaspEpoch;
use namada_core::storage;
use namada_core::time::DateTimeUtc;
use namada_events::{EmitEvents, EventToEmit};
Expand Down Expand Up @@ -190,23 +191,31 @@ where
}

/// Returns `true` if a new masp epoch has begun
pub fn is_masp_new_epoch(&self, is_new_epoch: bool) -> bool {
// FIXME: read from protocol param
// FIXME: need a function to convert from epoch to masp epoch?
pub fn is_masp_new_epoch(&self, is_new_epoch: bool) -> StorageResult<bool> {
let masp_epoch_multiplier = self
.read::<u64>(
&namada_parameters::storage::get_masp_epoch_multiplier_key(),
)?
.ok_or_else(|| {
namada_storage::Error::new_const(
"Missing expected masp epoch multiplier parameter",
)
})?;
let masp_new_epoch = is_new_epoch
&& checked!(u64::from(self.in_mem.block.epoch) % 4)
.expect("Masp epoch multiplier cannot be 0")
== 0;
&& MaspEpoch::is_masp_new_epoch(
self.in_mem.block.epoch,
masp_epoch_multiplier,
);

if masp_new_epoch {
tracing::info!(
"Began a new masp epoch {}",
checked!(u64::from(self.in_mem.block.epoch) / 4)
.expect("Masp epoch multiplier cannot be 0")
let masp_epoch = MaspEpoch::from_epoch(
self.in_mem.block.epoch,
masp_epoch_multiplier,
);
tracing::info!("Began a new masp epoch {masp_epoch}");
}

masp_new_epoch
Ok(masp_new_epoch)
}

/// Commit the current block's write log to the storage and commit the block
Expand Down

0 comments on commit e1e6ff3

Please sign in to comment.