Skip to content

Commit

Permalink
Checked operations in masp vp
Browse files Browse the repository at this point in the history
  • Loading branch information
grarco committed Jan 29, 2024
1 parent a150f83 commit 8dcdea2
Showing 1 changed file with 51 additions and 20 deletions.
71 changes: 51 additions & 20 deletions crates/namada/src/ledger/native_vp/masp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ where
pub ctx: Ctx<'a, DB, H, CA>,
}

struct TransparentTransferData {
source: Address,
target: Address,
token: Address,
amount: Amount,
}

impl<'a, DB, H, CA> MaspVp<'a, DB, H, CA>
where
DB: 'static + namada_state::DB + for<'iter> namada_state::DBIter<'iter>,
Expand Down Expand Up @@ -390,13 +397,6 @@ fn unepoched_tokens(
Ok(unepoched_tokens)
}

struct TransparentTransferData {
source: Address,
target: Address,
token: Address,
amount: Amount,
}

impl<'a, DB, H, CA> NativeVp for MaspVp<'a, DB, H, CA>
where
DB: 'static + namada_state::DB + for<'iter> namada_state::DBIter<'iter>,
Expand Down Expand Up @@ -492,9 +492,17 @@ where
&& *asset_denom == denom
&& *asset_epoch == epoch =>
{
total_in_values += token::Amount::from_masp_denominated(
vin.value, *digit,
);
total_in_values = total_in_values
.checked_add(token::Amount::from_masp_denominated(
vin.value, *digit,
))
.ok_or_else(|| {
Error::NativeVpError(
native_vp::Error::SimpleMessage(
"Overflow in total in value sum",
),
)
})?;
}
// Maybe the asset type has no attached epoch
None if unepoched_tokens.contains_key(&vin.asset_type) => {
Expand All @@ -521,10 +529,19 @@ where
} else {
// Otherwise note the contribution to this
// trransparent input
total_in_values +=
token::Amount::from_masp_denominated(
vin.value, *digit,
);
total_in_values = total_in_values
.checked_add(
token::Amount::from_masp_denominated(
vin.value, *digit,
),
)
.ok_or_else(|| {
Error::NativeVpError(
native_vp::Error::SimpleMessage(
"Overflow in total in values sum",
),
)
})?;
}
}
// unrecognized asset
Expand Down Expand Up @@ -617,21 +634,35 @@ where
&& *asset_denom == denom
&& *asset_epoch <= epoch =>
{
total_out_values +=
token::Amount::from_masp_denominated(
total_out_values = total_out_values
.checked_add(token::Amount::from_masp_denominated(
out.value, *digit,
);
))
.ok_or_else(|| {
Error::NativeVpError(
native_vp::Error::SimpleMessage(
"Overflow in total out values sum",
),
)
})?;
}
// Maybe the asset type has no attached epoch
None if unepoched_tokens.contains_key(&out.asset_type) => {
let (_token, _denom, digit) =
&unepoched_tokens[&out.asset_type];
// Otherwise note the contribution to this
// trransparent input
total_out_values +=
token::Amount::from_masp_denominated(
total_out_values = total_out_values
.checked_add(token::Amount::from_masp_denominated(
out.value, *digit,
);
))
.ok_or_else(|| {
Error::NativeVpError(
native_vp::Error::SimpleMessage(
"Overflow in total out values sum",
),
)
})?;
}
// unrecognized asset
_ => return Ok(false),
Expand Down

0 comments on commit 8dcdea2

Please sign in to comment.