Skip to content

Commit

Permalink
pindexer: supply: refactor pass 2: simplify genesis logic
Browse files Browse the repository at this point in the history
  • Loading branch information
cronokirby committed Sep 22, 2024
1 parent 68360e8 commit 61ab60c
Showing 1 changed file with 34 additions and 26 deletions.
60 changes: 34 additions & 26 deletions crates/bin/pindexer/src/supply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ use std::collections::{BTreeMap, HashSet};

use anyhow::{anyhow, Context, Result};
use cometindex::{async_trait, sqlx, AppView, ContextualizedEvent, PgTransaction};
use penumbra_app::genesis::AppState;
use penumbra_asset::asset;
use penumbra_app::genesis::{AppState, Content};
use penumbra_asset::{asset, STAKING_TOKEN_ASSET_ID};
use penumbra_num::Amount;
use penumbra_proto::{
event::ProtoEvent, penumbra::core::component::funding::v1 as pb_funding,
penumbra::core::component::stake::v1 as pb_stake,
};
use penumbra_stake::{rate::RateData, validator::Validator, IdentityKey};
use sqlx::{PgPool, Postgres, Transaction};
use std::iter;

mod unstaked_supply {
//! This module handles updates around the unstaked supply.
Expand Down Expand Up @@ -491,38 +492,45 @@ async fn add_genesis_native_token_allocation_supply<'a>(
dbtx: &mut PgTransaction<'a>,
app_state: &AppState,
) -> Result<()> {
fn content_mints(content: &Content) -> BTreeMap<asset::Id, Amount> {
let community_pool_mint = iter::once((
*STAKING_TOKEN_ASSET_ID,
content.community_pool_content.initial_balance.amount,
));
let allocation_mints = content
.shielded_pool_content
.allocations
.iter()
.map(|allocation| {
let value = allocation.value();
(value.asset_id, value.amount)
});

let mut out = BTreeMap::new();
for (id, amount) in community_pool_mint.chain(allocation_mints) {
out.entry(id).and_modify(|x| *x += amount);
}
out
}

let content = app_state
.content()
.ok_or_else(|| anyhow::anyhow!("cannot initialized indexer from checkpoint genesis"))?;
let mints = content_mints(content);

let mut unstaked_native_token_sum: Amount = Amount::zero();
for allo in &content.shielded_pool_content.allocations {
if allo.denom().base_denom().denom == "upenumbra" {
let value = allo.value();
unstaked_native_token_sum = unstaked_native_token_sum
.checked_add(&value.amount)
.unwrap();
}
}
// Add community pool allocation
unstaked_native_token_sum += content.community_pool_content.initial_balance.amount;
let unstaked_native_token_sum = u64::try_from(unstaked_native_token_sum.value())?;

unstaked_supply::modify(dbtx, 0, |_| Ok(unstaked_native_token_sum)).await?;

let mut allos = BTreeMap::<asset::Id, Amount>::new();
for allo in &content.shielded_pool_content.allocations {
let value = allo.value();
let sum = allos.entry(value.asset_id).or_default();
*sum = sum
.checked_add(&value.amount)
.ok_or_else(|| anyhow::anyhow!("overflow adding genesis allos (should not happen)"))?;
}
let unstaked_mint = u64::try_from(
mints
.get(&*STAKING_TOKEN_ASSET_ID)
.copied()
.unwrap_or_default()
.value(),
)?;
unstaked_supply::modify(dbtx, 0, |_| Ok(unstaked_mint)).await?;

// at genesis, assume a 1:1 ratio between delegation amount and native token amount.
for val in &content.stake_content.validators {
let val = Validator::try_from(val.clone())?;
let delegation_amount: i64 = allos
let delegation_amount: i64 = mints
.get(&val.token().id())
.cloned()
.unwrap_or_default()
Expand Down

0 comments on commit 61ab60c

Please sign in to comment.