Skip to content

Commit

Permalink
Update balances in the MASP using a non-mutating style.
Browse files Browse the repository at this point in the history
  • Loading branch information
murisi committed Jun 20, 2024
1 parent d0b4f3d commit 0a5d480
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 29 deletions.
2 changes: 1 addition & 1 deletion crates/core/src/masp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ impl TransferSource {
}
}

/// Get the contained MaybeIbcAddress, if any
/// Get the contained transparent address data, if any
pub fn t_addr_data(&self) -> Option<TAddrData> {
match self {
Self::Address(x) => Some(TAddrData::Addr(x.clone())),
Expand Down
78 changes: 50 additions & 28 deletions crates/namada/src/ledger/native_vp/masp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ where
let Some(storage_commitment): Option<PacketCommitment> =
self.ctx.read_bytes_post(key)?.map(Into::into)
else {
// Ignore this event if it does not exist
// Ignore this key if the value does not exist
continue;
};
if packet_commitment == storage_commitment {
Expand Down Expand Up @@ -410,19 +410,25 @@ where
msg.chan_id_on_a.clone(),
&msg.packet_data.token.denom,
) {
let post_entry =
acc.post.entry(addr_taddr(IBC)).or_insert(ValueSum::zero());
*post_entry =
checked!(post_entry - &delta).map_err(native_vp::Error::new)?;
let ibc_taddr = addr_taddr(IBC);
let post_entry = acc
.post
.get(&ibc_taddr)
.cloned()
.unwrap_or(ValueSum::zero());
acc.post.insert(
ibc_taddr,
checked!(post_entry - &delta).map_err(native_vp::Error::new)?,
);
}
// Record an increase to the balance of a specific IBC receiver
let receiver = msg.packet_data.receiver.to_string();
let post_entry = acc
.post
.entry(ibc_taddr(receiver))
.or_insert(ValueSum::zero());
*post_entry =
checked!(post_entry + &delta).map_err(native_vp::Error::new)?;
let receiver = ibc_taddr(msg.packet_data.receiver.to_string());
let post_entry =
acc.post.get(&receiver).cloned().unwrap_or(ValueSum::zero());
acc.post.insert(
receiver,
checked!(post_entry + &delta).map_err(native_vp::Error::new)?,
);

Ok(acc)
}
Expand Down Expand Up @@ -491,10 +497,13 @@ where
// address and be deposited elsewhere
// Required for the IBC internal Address to release
// funds
let ibc_taddr = addr_taddr(IBC);
let pre_entry =
acc.pre.entry(addr_taddr(IBC)).or_insert(ValueSum::zero());
*pre_entry =
checked!(pre_entry + &delta).map_err(native_vp::Error::new)?;
acc.pre.get(&ibc_taddr).cloned().unwrap_or(ValueSum::zero());
acc.pre.insert(
ibc_taddr,
checked!(pre_entry + &delta).map_err(native_vp::Error::new)?,
);
}
Ok(acc)
}
Expand Down Expand Up @@ -569,19 +578,32 @@ where
.insert(address_hash, TAddrData::Addr(counterpart.clone()));
// Finally record the actual balance change starting with the initial
// state
let pre_entry =
result.pre.entry(address_hash).or_insert(ValueSum::zero());
*pre_entry = checked!(
pre_entry + &ValueSum::from_pair((*token).clone(), pre_balance)
)
.map_err(native_vp::Error::new)?;
let pre_entry = result
.pre
.get(&address_hash)
.cloned()
.unwrap_or(ValueSum::zero());
result.pre.insert(
address_hash,
checked!(
pre_entry + &ValueSum::from_pair((*token).clone(), pre_balance)
)
.map_err(native_vp::Error::new)?,
);
// And then record thee final state
let post_entry =
result.post.entry(address_hash).or_insert(ValueSum::zero());
*post_entry = checked!(
post_entry + &ValueSum::from_pair((*token).clone(), post_balance)
)
.map_err(native_vp::Error::new)?;
let post_entry = result
.post
.get(&address_hash)
.cloned()
.unwrap_or(ValueSum::zero());
result.post.insert(
address_hash,
checked!(
post_entry
+ &ValueSum::from_pair((*token).clone(), post_balance)
)
.map_err(native_vp::Error::new)?,
);
Result::<_>::Ok(result)
}

Expand All @@ -605,7 +627,7 @@ where
// Enable decoding the IBC address hash
changed_balances.decoder.insert(addr_taddr(IBC), ibc_addr);

// Go through the IBC events and note the balance chages they imply
// Go through the IBC events and note the balance changes they imply
let changed_balances =
ibc_msgs.iter().try_fold(changed_balances, |acc, ibc_msg| {
// Apply all IBC packets to the changed balances structure
Expand Down

0 comments on commit 0a5d480

Please sign in to comment.