Skip to content

Commit

Permalink
Fallible masp epoch conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
grarco committed May 27, 2024
1 parent e1e6ff3 commit 594f7f9
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
23 changes: 14 additions & 9 deletions crates/core/src/masp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ impl FromStr for MaspEpoch {
impl MaspEpoch {
/// Converts and `Epoch` into a `MaspEpoch` based on the provided conversion
/// rate
pub fn from_epoch(epoch: Epoch, masp_epoch_multiplier: u64) -> Self {
Self(
pub fn try_from_epoch(
epoch: Epoch,
masp_epoch_multiplier: u64,
) -> Result<Self, &'static str> {
Ok(Self(
epoch
.checked_div(masp_epoch_multiplier)
// FIXME: error here
.expect("Masp epoch multiplier should not be 0"),
)
.ok_or("Masp epoch multiplier cannot be 0")?,
))
}

/// Returns a 0 masp epoch
Expand All @@ -82,13 +84,16 @@ impl MaspEpoch {

/// 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)
pub fn is_masp_new_epoch(
epoch: Epoch,
masp_epoch_multiplier: u64,
) -> Result<bool, &'static str> {
Ok(matches!(
Self::try_from_epoch(epoch, masp_epoch_multiplier)?
.0
.checked_rem(masp_epoch_multiplier),
Some(Epoch(0))
)
))
}

/// Initialize a new masp epoch from the provided one
Expand Down
7 changes: 5 additions & 2 deletions crates/namada/src/ledger/native_vp/masp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,13 @@ where
"Missing expected masp epoch multiplier parameter",
))
})?;
let masp_epoch = MaspEpoch::from_epoch(
let masp_epoch = MaspEpoch::try_from_epoch(
self.ctx.get_block_epoch()?,
masp_epoch_multiplier,
);
)
.map_err(|msg| {
Error::NativeVpError(native_vp::Error::new_const(msg))
})?;
let conversion_state = self.ctx.state.in_mem().get_conversion_state();
let shielded_tx = self.ctx.get_shielded_action(tx_data)?;

Expand Down
3 changes: 2 additions & 1 deletion crates/sdk/src/queries/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,8 @@ where
"Missing expected masp epoch multiplier parameter",
)
})?;
Ok(MaspEpoch::from_epoch(epoch, masp_epoch_multiplier))
MaspEpoch::try_from_epoch(epoch, masp_epoch_multiplier)
.map_err(namada_storage::Error::new_const)
}

fn native_token<D, H, V, T>(
Expand Down
5 changes: 3 additions & 2 deletions crates/shielded_token/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,11 @@ where
let masp_epoch_multiplier = storage
.read::<u64>(&parameters::storage::get_masp_epoch_multiplier_key())?
.expect("masp epoch multiplier should properly decode");
let masp_epoch = MaspEpoch::from_epoch(
let masp_epoch = MaspEpoch::try_from_epoch(
storage.get_block_epoch()?,
masp_epoch_multiplier,
);
)
.map_err(namada_storage::Error::new_const)?;
let prev_masp_epoch = match masp_epoch.prev() {
Some(epoch) => epoch,
None => return Ok(()),
Expand Down
8 changes: 5 additions & 3 deletions crates/state/src/wl_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,15 @@ where
&& MaspEpoch::is_masp_new_epoch(
self.in_mem.block.epoch,
masp_epoch_multiplier,
);
)
.map_err(namada_storage::Error::new_const)?;

if masp_new_epoch {
let masp_epoch = MaspEpoch::from_epoch(
let masp_epoch = MaspEpoch::try_from_epoch(
self.in_mem.block.epoch,
masp_epoch_multiplier,
);
)
.map_err(namada_storage::Error::new_const)?;
tracing::info!("Began a new masp epoch {masp_epoch}");
}

Expand Down

0 comments on commit 594f7f9

Please sign in to comment.