Skip to content

Commit

Permalink
address feedback: emit event per block instead of per epoch
Browse files Browse the repository at this point in the history
  • Loading branch information
TalDerei committed Feb 3, 2025
1 parent ffe4037 commit 34bd554
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 57 deletions.
75 changes: 44 additions & 31 deletions crates/core/component/distributions/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,36 @@ impl Component for Distributions {
) {
}

#[instrument(name = "distributions", skip(_state, _end_block))]
#[instrument(name = "distributions", skip(state))]
async fn end_block<S: StateWrite + 'static>(
_state: &mut Arc<S>,
_end_block: &abci::request::EndBlock,
state: &mut Arc<S>,
end_block: &abci::request::EndBlock,
) {
let state = Arc::get_mut(state).expect("the state should not be shared");

let current_epoch = state.get_current_epoch().await.expect("msg");
let current_block_height = end_block
.height
.try_into()
.expect("block height should not be negative");

let new_issuance = state
.get_distributions_params()
.await
.expect("distribution parameters should be available")
.staking_issuance_per_block as u128;

let total_new_issuance = state
.compute_lqt_issuance_from_blocks(current_epoch, current_block_height)
.await
.expect("should be able to compute LQT issuance from block");

// Emit an event for LQT pool size increase at the end of the block.
state.record_proto(event::event_lqt_pool_size_increase(
current_epoch.index,
new_issuance.into(),
total_new_issuance,
))
}

#[instrument(name = "distributions", skip(state))]
Expand Down Expand Up @@ -109,9 +134,12 @@ trait DistributionManager: StateWriteExt {
Ok(self.set_staking_token_issuance_for_epoch(new_issuance))
}

/// Computes total LQT reward issuance for the epoch.
async fn compute_new_lqt_issuance(&self, current_epoch: Epoch) -> Result<Amount> {
let current_block_height = self.get_block_height().await?;
/// Helper function that computes the LQT issuance for the current block height in the epoch.
async fn compute_lqt_issuance_from_blocks(
&self,
current_epoch: Epoch,
current_block_height: u64,
) -> Result<Amount> {
let epoch_length = current_block_height
.checked_sub(current_epoch.start_height)
.unwrap_or_else(|| panic!("epoch start height is greater than current block height (epoch_start={}, current_height={}", current_epoch.start_height, current_block_height));
Expand Down Expand Up @@ -139,6 +167,15 @@ trait DistributionManager: StateWriteExt {
Ok(Amount::from(total_pool_size_for_epoch))
}

/// Computes total LQT reward issuance for the epoch.
async fn compute_new_lqt_issuance(&self, current_epoch: Epoch) -> Result<Amount> {
let current_block_height = self.get_block_height().await?;

Ok(self
.compute_lqt_issuance_from_blocks(current_epoch, current_block_height)
.await?)
}

/// Update the nonverifiable storage with the newly issued LQT rewards for the current epoch.
async fn define_lqt_budget(&mut self) -> Result<()> {
// Grab the ambient epoch index.
Expand All @@ -152,31 +189,7 @@ trait DistributionManager: StateWriteExt {
current_epoch.index
);

// Retrieve the previous cumulative LQT issuance total from NV storage.
let previous_issuance = self
.get_cummulative_lqt_reward_issuance(current_epoch.index - 1)
.await
.ok_or_else(|| {
tonic::Status::not_found(format!(
"failed to retrieve cumulative LQT issuance for epoch {} from non-verifiable storage",
current_epoch.index,
))
})?;

// Compute cumulative LQT issuance up until current epoch.
let cumulative_issuance = previous_issuance + new_issuance;

// Emit an event for LQT pool size increase.
self.record_proto(event::event_lqt_pool_size_increase(
current_epoch.index,
new_issuance,
cumulative_issuance,
));

self.set_lqt_reward_issuance_for_epoch(current_epoch.index, new_issuance);
self.set_cumulative_lqt_reward_issuance(current_epoch.index, cumulative_issuance);

Ok(())
Ok(self.set_lqt_reward_issuance_for_epoch(current_epoch.index, new_issuance))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ pub mod lqt {
key[0..48].copy_from_slice(prefix_bytes.as_bytes());
key
}

/// The amount of LQT rewards issued up until this epoch.
pub fn cumulative(epoch_index: u64) -> [u8; 59] {
let key_str = format!("distributions/lqt/v1/budget/cumulative/{epoch_index:020}");
let mut key = [0u8; 59];
key.copy_from_slice(key_str.as_bytes());
key
}
}
}
}
Expand Down
18 changes: 0 additions & 18 deletions crates/core/component/distributions/src/component/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ pub trait StateReadExt: StateRead {
None
})
}

// Get the cumulative amount of LQT rewards issued up until this epoch.
async fn get_cummulative_lqt_reward_issuance(&self, epoch_index: u64) -> Option<Amount> {
let key = state_key::lqt::v1::budget::cumulative(epoch_index);

self.nonverifiable_get(&key).await.unwrap_or_else(|_| {
tracing::error!("cumulative LQT issuance does not exist for epoch");
None
})
}
}

impl<T: StateRead + ?Sized> StateReadExt for T {}
Expand All @@ -62,13 +52,5 @@ pub trait StateWriteExt: StateWrite + StateReadExt {
issuance,
);
}

/// Set the cumulative amount of LQT rewards issued up until this epoch.
fn set_cumulative_lqt_reward_issuance(&mut self, epoch_index: u64, issuance: Amount) {
self.nonverifiable_put(
state_key::lqt::v1::budget::cumulative(epoch_index).into(),
issuance,
);
}
}
impl<T: StateWrite + ?Sized> StateWriteExt for T {}

0 comments on commit 34bd554

Please sign in to comment.