Skip to content

Commit

Permalink
shielded-pool: remove update_token_supply
Browse files Browse the repository at this point in the history
In #3640, we added two helper methods:
- `increase_token_supply`
- `decrease_token_supply`

These methods are somewhat similar, and objectively are more
verbose than a single `update_token_supply` method. However,
it seems an improvement to not have to do any cognitive work
to piece together whether a method call increases or decreases
the token supply.

Another advantage of this approach is that one can easily use
rust-analyzer to filter for places that increase the token supply
vs. places where it is being shrinked.
  • Loading branch information
erwanor committed Jan 29, 2024
1 parent a1cd044 commit 528ff9b
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub trait NoteManager: StateWrite {

let note = Note::from_parts(*address, value, Rseed(rseed_bytes))?;
// Now record the note and update the total supply:
self.update_token_supply(&value.asset_id, value.amount.value() as i128)
self.increase_token_supply(&value.asset_id, value.amount)
.await?;
self.add_note_payload(note.payload(), source).await;

Expand Down
40 changes: 0 additions & 40 deletions crates/core/component/shielded-pool/src/component/supply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,46 +92,6 @@ pub trait SupplyWrite: StateWrite {
self.put(key, new_supply);
Ok(())
}

// TODO: should this really be separate from note management?
// #[instrument(skip(self, change))]
async fn update_token_supply(&mut self, asset_id: &asset::Id, change: i128) -> Result<()> {
let key = state_key::token_supply(asset_id);
let current_supply: Amount = self.get(&key).await?.unwrap_or(0u64.into());

// TODO: replace with a single checked_add_signed call when mixed_integer_ops lands in stable (1.66)
let new_supply: Amount = if change < 0 {
current_supply
.value()
.checked_sub(change.unsigned_abs())
.ok_or_else(|| {
anyhow::anyhow!(
"underflow updating token {} supply {} with delta {}",
asset_id,
current_supply,
change
)
})?
.into()
} else {
current_supply
.value()
.checked_add(change as u128)
.ok_or_else(|| {
anyhow::anyhow!(
"overflow updating token {} supply {} with delta {}",
asset_id,
current_supply,
change
)
})?
.into()
};
tracing::debug!(?current_supply, ?new_supply, ?change);

self.put(key, new_supply);
Ok(())
}
}

impl<T: StateWrite + ?Sized> SupplyWrite for T {}
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ pub trait Ics20TransferWriteExt: StateWrite {
);

// update supply tracking of burned note
self.update_token_supply(&withdrawal.denom.id(), -(withdrawal.amount.value() as i128))
self.decrease_token_supply(&withdrawal.denom.id(), withdrawal.amount)
.await
.expect("couldn't update token supply in ics20 withdrawal!");
}
Expand Down

0 comments on commit 528ff9b

Please sign in to comment.