Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
4956: Introducing seigniorage proportion gauge r=EdHastingsCasperAssociation a=AlexanderLimonov

casper-network#4935 

Introduces a crude gauge of reward trajectory adherence

Co-authored-by: Alexander Limonov <[email protected]>
  • Loading branch information
2 parents 26943ed + b40eb1d commit 194e50b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
12 changes: 12 additions & 0 deletions node/src/components/contract_runtime/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ const DB_FLUSH_TIME_HELP: &str = "time in seconds to flush changes to the databa
const SCRATCH_LMDB_WRITE_TIME: &str = "contract_runtime_scratch_lmdb_write_time";
const SCRATCH_LMDB_WRITE_TIME_HELP: &str = "time in seconds to write changes to the database";

const SEIGNIORAGE_TARGET_FRACTION: &str = "contract_runtime_seigniorage_target_fraction";
const SEIGNIORAGE_TARGET_FRACTION_HELP: &str = "fraction of target seigniorage minted in era";

/// Metrics for the contract runtime component.
#[derive(Debug)]
pub struct Metrics {
Expand Down Expand Up @@ -148,6 +151,7 @@ pub struct Metrics {
pub(super) pruning_time: Histogram,
pub(super) database_flush_time: Histogram,
pub(super) scratch_lmdb_write_time: Histogram,
pub(super) seigniorage_target_fraction: Gauge,
registry: Registry,
}

Expand Down Expand Up @@ -179,6 +183,12 @@ impl Metrics {
let exec_queue_size = IntGauge::new(EXEC_QUEUE_SIZE_NAME, EXEC_QUEUE_SIZE_HELP)?;
registry.register(Box::new(exec_queue_size.clone()))?;

let seigniorage_target_fraction = Gauge::new(
SEIGNIORAGE_TARGET_FRACTION,
SEIGNIORAGE_TARGET_FRACTION_HELP,
)?;
registry.register(Box::new(seigniorage_target_fraction.clone()))?;

Ok(Metrics {
exec_block_pre_processing: utils::register_histogram_metric(
registry,
Expand Down Expand Up @@ -344,6 +354,7 @@ impl Metrics {
SCRATCH_LMDB_WRITE_TIME_HELP,
wider_buckets.clone(),
)?,
seigniorage_target_fraction,
registry: registry.clone(),
})
}
Expand Down Expand Up @@ -379,5 +390,6 @@ impl Drop for Metrics {
unregister_metric!(self.registry, self.pruning_time);
unregister_metric!(self.registry, self.database_flush_time);
unregister_metric!(self.registry, self.scratch_lmdb_write_time);
unregister_metric!(self.registry, self.seigniorage_target_fraction);
}
}
52 changes: 46 additions & 6 deletions node/src/components/contract_runtime/rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ use futures::stream::{self, StreamExt as _, TryStreamExt as _};

use itertools::Itertools;
use num_rational::Ratio;
use num_traits::{CheckedAdd, CheckedMul};
use num_traits::{CheckedAdd, CheckedMul, ToPrimitive};
use tracing::trace;

use crate::{
contract_runtime::metrics::Metrics,
effect::{
requests::{ContractRuntimeRequest, StorageRequest},
EffectBuilder,
Expand Down Expand Up @@ -254,10 +255,10 @@ impl RewardsInfo {
TotalSupplyResult::Success { total_supply } => total_supply,
};

let seignorate_rate_request =
let seigniorage_rate_request =
RoundSeigniorageRateRequest::new(state_root_hash, protocol_version);
let seignorate_rate =
match data_access_layer.round_seigniorage_rate(seignorate_rate_request) {
let seigniorage_rate =
match data_access_layer.round_seigniorage_rate(seigniorage_rate_request) {
RoundSeigniorageRateResult::RootNotFound
| RoundSeigniorageRateResult::MintNotFound
| RoundSeigniorageRateResult::ValueNotFound(_)
Expand All @@ -267,7 +268,7 @@ impl RewardsInfo {
RoundSeigniorageRateResult::Success { rate } => rate,
};

let reward_per_round = seignorate_rate * total_supply;
let reward_per_round = seigniorage_rate * total_supply;
let total_weights = weights.values().copied().sum();

Ok::<_, RewardsError>((
Expand Down Expand Up @@ -380,6 +381,7 @@ pub(crate) async fn fetch_data_and_calculate_rewards_for_era<REv: ReactorEventT>
effect_builder: EffectBuilder<REv>,
data_access_layer: Arc<DataAccessLayer<LmdbGlobalState>>,
chainspec: &Chainspec,
metrics: &Arc<Metrics>,
executable_block: ExecutableBlock,
) -> Result<BTreeMap<PublicKey, Vec<U512>>, RewardsError> {
let current_era_id = executable_block.era_id;
Expand Down Expand Up @@ -410,7 +412,45 @@ pub(crate) async fn fetch_data_and_calculate_rewards_for_era<REv: ReactorEventT>
)
.await?;

rewards_for_era(rewards_info, current_era_id, &chainspec.core_config)
let cited_blocks_count_current_era = rewards_info.blocks_from_era(current_era_id).count();

let reward_per_round_current_era = rewards_info
.eras_info
.get(&current_era_id)
.expect("expected EraInfo")
.reward_per_round;

let rewards = rewards_for_era(rewards_info, current_era_id, &chainspec.core_config);

// Calculate and push reward metric(s)
match &rewards {
Ok(rewards_map) => {
let expected_total_seigniorage = reward_per_round_current_era
.to_integer()
.saturating_mul(U512::from(cited_blocks_count_current_era as u64));
let actual_total_seigniorage =
rewards_map
.iter()
.fold(U512::zero(), |acc, (_, rewards_vec)| {
let current_era_reward = rewards_vec
.first()
.expect("expected current era reward amount");
acc.saturating_add(*current_era_reward)
});
let seigniorage_target_fraction = Ratio::new(
actual_total_seigniorage.low_u128(),
expected_total_seigniorage.low_u128(),
);
let gauge_value = match Ratio::to_f64(&seigniorage_target_fraction) {
Some(v) => v,
None => f64::NAN,
};
metrics.seigniorage_target_fraction.set(gauge_value)
}
Err(_) => (),
}

rewards
}
}

Expand Down
1 change: 1 addition & 0 deletions node/src/components/contract_runtime/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ pub(super) async fn exec_or_requeue<REv>(
effect_builder,
data_access_layer.clone(),
chainspec.as_ref(),
&metrics,
executable_block.clone(),
)
.await
Expand Down

0 comments on commit 194e50b

Please sign in to comment.