Skip to content

Commit

Permalink
Update node reward table to compute XDR with penalties (#935)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent 9a241c0 commit 751adf1
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 6 deletions.
6 changes: 5 additions & 1 deletion Cargo.Bazel.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"checksum": "ac1c7f35e7cd235485ebc4e9085ef127ba2881abba9deee4940ac3a40ac97b9a",
"checksum": "b56548df73e3611b6967660ea1984112a5afc5cc24a198a8386dba53f8a6106d",
"crates": {
"actix-codec 0.5.2": {
"name": "actix-codec",
Expand Down Expand Up @@ -44044,6 +44044,10 @@
"id": "ic-management-canister-types 0.9.0",
"target": "ic_management_canister_types"
},
{
"id": "ic-protobuf 0.9.0",
"target": "ic_protobuf"
},
{
"id": "ic-stable-structures 0.6.5",
"target": "ic_stable_structures"
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ serde_json = { workspace = true }
dfn_core = { workspace = true }
ic-management-canister-types = { workspace = true }
ic-stable-structures = { workspace = true }
ic-protobuf = { workspace = true }
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{borrow::Cow, fmt};
use candid::{CandidType, Decode, Deserialize, Encode, Principal};
use dfn_core::api::PrincipalId;
use ic_management_canister_types::NodeMetricsHistoryResponse;
use ic_protobuf::registry::node_rewards::v2::NodeRewardRates;
use ic_stable_structures::{storable::Bound, Storable};
use serde::Serialize;

Expand All @@ -21,7 +22,7 @@ pub struct NodeMetricsStored {
pub num_blocks_failed: u64,
}

const MAX_VALUE_SIZE_BYTES: u32 = 102;
const MAX_VALUE_SIZE_BYTES_NODE_METRICS: u32 = 102;

impl Storable for NodeMetricsStored {
fn to_bytes(&self) -> std::borrow::Cow<[u8]> {
Expand All @@ -33,7 +34,29 @@ impl Storable for NodeMetricsStored {
}

const BOUND: Bound = Bound::Bounded {
max_size: MAX_VALUE_SIZE_BYTES,
max_size: MAX_VALUE_SIZE_BYTES_NODE_METRICS,
is_fixed_size: false,
};
}

#[derive(Debug, Deserialize, Serialize, CandidType, Clone)]
pub struct NodeRewardRatesStored {
pub rewards_rates: NodeRewardRates,
}

const MAX_VALUE_SIZE_BYTES_REWARD_RATES: u32 = 133;

impl Storable for NodeRewardRatesStored {
fn to_bytes(&self) -> std::borrow::Cow<[u8]> {
Cow::Owned(Encode!(self).unwrap())
}

fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self {
Decode!(bytes.as_ref(), Self).unwrap()
}

const BOUND: Bound = Bound::Bounded {
max_size: MAX_VALUE_SIZE_BYTES_REWARD_RATES,
is_fixed_size: false,
};
}
Expand All @@ -54,7 +77,7 @@ impl Storable for NodeMetadataStored {
}

const BOUND: Bound = Bound::Bounded {
max_size: MAX_VALUE_SIZE_BYTES,
max_size: MAX_VALUE_SIZE_BYTES_NODE_METRICS,
is_fixed_size: false,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ async fn update_metrics_task() {
ic_cdk::println!("Error updating metrics: {}", e);
}
}

match rewards_manager::update_node_rewards_table().await {
Ok(_) => {
ic_cdk::println!("Successfully updated node_rewards_table");
}
Err(e) => {
ic_cdk::println!("Error updating node_rewards_table: {}", e);
}
}
}

fn setup_timers() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
use ic_protobuf::registry::node_rewards::v2::NodeRewardsTable;
use ic_registry_keys::NODE_REWARDS_TABLE_KEY;
use itertools::Itertools;
use num_traits::ToPrimitive;
use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use trustworthy_node_metrics_types::types::{DailyNodeMetrics, RewardsComputationResult};

use crate::computation_logger::{ComputationLogger, Operation, OperationExecutor};
use crate::{
computation_logger::{ComputationLogger, Operation, OperationExecutor},
stable_memory,
};

const MIN_FAILURE_RATE: Decimal = dec!(0.1);
const MAX_FAILURE_RATE: Decimal = dec!(0.6);
Expand Down Expand Up @@ -115,6 +120,16 @@ pub fn compute_rewards_percent(daily_metrics: &[DailyNodeMetrics]) -> RewardsCom
}
}

/// Update node rewards table
pub async fn update_node_rewards_table() -> anyhow::Result<()> {
let (rewards_table, _): (NodeRewardsTable, _) = ic_nns_common::registry::get_value(NODE_REWARDS_TABLE_KEY.as_bytes(), None).await?;
for (area, rewards_rates) in rewards_table.table {
stable_memory::insert_rewards_rates(area, rewards_rates)
}

Ok(())
}

#[cfg(test)]
mod tests {
use candid::Principal;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use candid::Principal;
use ic_protobuf::registry::node_rewards::v2::NodeRewardRates;
use ic_stable_structures::memory_manager::{MemoryId, MemoryManager, VirtualMemory};
use ic_stable_structures::{DefaultMemoryImpl, StableBTreeMap};
use itertools::Itertools;
use std::cell::RefCell;
use std::collections::BTreeMap;

use trustworthy_node_metrics_types::types::{NodeMetadata, NodeMetadataStored, NodeMetricsStored, NodeMetricsStoredKey, TimestampNanos};
use trustworthy_node_metrics_types::types::{
NodeMetadata, NodeMetadataStored, NodeMetricsStored, NodeMetricsStoredKey, NodeRewardRatesStored, TimestampNanos,
};

type Memory = VirtualMemory<DefaultMemoryImpl>;

Expand All @@ -31,6 +34,10 @@ thread_local! {
MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(3)))
));

static REWARDS_TABLE: RefCell<StableBTreeMap<String, NodeRewardRatesStored, Memory>> =
RefCell::new(StableBTreeMap::init(
MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(4)))
));
}

pub fn insert_node_metrics(key: NodeMetricsStoredKey, value: NodeMetricsStored) {
Expand Down Expand Up @@ -120,3 +127,7 @@ pub fn nodes_metadata() -> Vec<NodeMetadata> {
.collect_vec()
})
}

pub fn insert_rewards_rates(area: String, rewards_rates: NodeRewardRates) {
REWARDS_TABLE.with_borrow_mut(|rewards_table| rewards_table.insert(area, NodeRewardRatesStored { rewards_rates }));
}

0 comments on commit 751adf1

Please sign in to comment.