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 19, 2024
1 parent d0b4f3d commit 4de6577
Showing 1 changed file with 49 additions and 27 deletions.
76 changes: 49 additions & 27 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 Down

0 comments on commit 4de6577

Please sign in to comment.