Skip to content

Commit

Permalink
bug: pindexer: allow dex and auction supply to be temporarily negative (
Browse files Browse the repository at this point in the history
#4907)

Closes #4906.

## Describe your changes

This allows the dex and auction components of the old supply app view to
become temporarily negative until the end of a block, so that the order
of debits and credits within a block doesn't matter.

This should unblock the currently crashing pindexer on mainnet, and one
can verify that the dex supply never goes below 0 empirically.

## Checklist before requesting a review

- [x] I have added guiding text to explain how a reviewer should test
these changes.

- [x] If this code contains consensus-breaking changes, I have added the
"consensus-breaking" label. Otherwise, I declare my belief that there
are not consensus-breaking changes, for the following reason:

  > pindexer only.
  • Loading branch information
cronokirby authored Oct 28, 2024
1 parent fcce631 commit 0ac1fbc
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions crates/bin/pindexer/src/supply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ mod unstaked_supply {
/// The supply that's not locked in any component.
pub um: u64,
/// The supply locked in the auction component.
pub auction: u64,
pub auction: i64,
/// The supply locked in the dex component.
pub dex: u64,
pub dex: i64,
/// The supply which has been (forever) locked away after arb.
pub arb: u64,
/// The supply which has been (forever) locked away as paid fees.
Expand Down Expand Up @@ -114,7 +114,9 @@ mod unstaked_supply {
f: impl FnOnce(Option<Supply>) -> Result<Supply>,
) -> Result<()> {
let supply = get_supply(dbtx, height).await?;
// tracing::warn!(?supply, "supply");
let new_supply = f(supply)?;
// tracing::warn!(?new_supply, "new_supply");
set_supply(dbtx, height, new_supply).await
}
}
Expand Down Expand Up @@ -513,12 +515,15 @@ impl Event {
return Ok(());
}

let added = u64::try_from(new_balance.value() - previous_balance.value())?;
let added = i64::try_from(new_balance.value() - previous_balance.value())?;
unstaked_supply::modify(dbtx, *height, |current| {
let current = current.unwrap_or_default();
Ok(unstaked_supply::Supply {
um: current.um - added,
auction: current.auction + added,
um: current.um - added as u64,
auction: current.auction.checked_add(added).ok_or(anyhow!(format!(
"AuctionVCB overflow: {} + {}",
current.auction, added
)))?,
..current
})
})
Expand All @@ -534,12 +539,15 @@ impl Event {
return Ok(());
}

let removed = u64::try_from(previous_balance.value() - new_balance.value())?;
let removed = i64::try_from(previous_balance.value() - new_balance.value())?;
unstaked_supply::modify(dbtx, *height, |current| {
let current = current.unwrap_or_default();
Ok(unstaked_supply::Supply {
um: current.um + removed,
auction: current.auction - removed,
um: current.um + removed as u64,
auction: current.auction.checked_sub(removed).ok_or(anyhow!(format!(
"AuctionVCB underflow: {} - {}",
current.auction, removed
)))?,
..current
})
})
Expand All @@ -555,12 +563,15 @@ impl Event {
return Ok(());
}

let added = u64::try_from(new_balance.value() - previous_balance.value())?;
let added = i64::try_from(new_balance.value() - previous_balance.value())?;
unstaked_supply::modify(dbtx, *height, |current| {
let current = current.unwrap_or_default();
Ok(unstaked_supply::Supply {
um: current.um - added,
dex: current.dex + added,
um: current.um - added as u64,
dex: current.dex.checked_sub(added).ok_or(anyhow!(format!(
"DexVCB overflow: {} + {}",
current.dex, added
)))?,
..current
})
})
Expand All @@ -576,12 +587,15 @@ impl Event {
return Ok(());
}

let removed = u64::try_from(previous_balance.value() - new_balance.value())?;
let removed = i64::try_from(previous_balance.value() - new_balance.value())?;
unstaked_supply::modify(dbtx, *height, |current| {
let current = current.unwrap_or_default();
Ok(unstaked_supply::Supply {
um: current.um + removed,
dex: current.dex - removed,
um: current.um + removed as u64,
dex: current.dex.checked_add(removed).ok_or(anyhow!(format!(
"DexVCB underflow: {} - {}",
current.dex, removed
)))?,
..current
})
})
Expand Down

0 comments on commit 0ac1fbc

Please sign in to comment.