Skip to content

Commit

Permalink
Merge branch 'main' into sat-network-optimize-operator-probe-nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
sasa-tomic authored Nov 8, 2024
2 parents 60e5794 + 29c5f0b commit f6894d8
Show file tree
Hide file tree
Showing 10 changed files with 977 additions and 1 deletion.
74 changes: 73 additions & 1 deletion Cargo.Bazel.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"checksum": "b9a098cf38a25d6eabe74b8d704b6ddcb5aebfdf40cb01dd1a81b2376028b32e",
"checksum": "7ea7a35de8b6493825ab6fe949a472115132c928f34a5084ca4d283a254ed4d3",
"crates": {
"actix-codec 0.5.2": {
"name": "actix-codec",
Expand Down Expand Up @@ -29575,6 +29575,77 @@
},
"license": "MIT"
},
"node-provider-rewards-lib 0.5.6": {
"name": "node-provider-rewards-lib",
"version": "0.5.6",
"repository": null,
"targets": [
{
"Library": {
"crate_name": "node_provider_rewards_lib",
"crate_root": "src/lib.rs",
"srcs": [
"**/*.rs"
]
}
}
],
"library_target_name": "node_provider_rewards_lib",
"common_attrs": {
"compile_data_glob": [
"**"
],
"deps": {
"common": [
{
"id": "ahash 0.8.11",
"target": "ahash"
},
{
"id": "ic-base-types 0.9.0",
"target": "ic_base_types"
},
{
"id": "ic-management-canister-types 0.9.0",
"target": "ic_management_canister_types"
},
{
"id": "ic-protobuf 0.9.0",
"target": "ic_protobuf"
},
{
"id": "itertools 0.13.0",
"target": "itertools"
},
{
"id": "lazy_static 1.5.0",
"target": "lazy_static"
},
{
"id": "num-traits 0.2.19",
"target": "num_traits"
},
{
"id": "rust_decimal 1.36.0",
"target": "rust_decimal"
}
],
"selects": {}
},
"edition": "2021",
"proc_macro_deps": {
"common": [
{
"id": "rust_decimal_macros 1.36.0",
"target": "rust_decimal_macros"
}
],
"selects": {}
},
"version": "0.5.6"
},
"license": "Apache-2.0"
},
"node-status-updater 0.5.6": {
"name": "node-status-updater",
"version": "0.5.6",
Expand Down Expand Up @@ -48600,6 +48671,7 @@
"multiservice-discovery 0.5.6": "rs/ic-observability/multiservice-discovery",
"multiservice-discovery-downloader 0.5.6": "rs/ic-observability/multiservice-discovery-downloader",
"multiservice-discovery-shared 0.5.6": "rs/ic-observability/multiservice-discovery-shared",
"node-provider-rewards-lib 0.5.6": "rs/dre-canisters/node-provider-rewards-lib",
"node-status-updater 0.5.6": "rs/ic-observability/node-status-updater",
"obs-canister-clients 0.5.6": "rs/ic-observability/obs-canister-clients",
"prometheus-config-updater 0.5.6": "rs/ic-observability/prometheus-config-updater",
Expand Down
15 changes: 15 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ members = [
"rs/log-fetcher",
"rs/canister-log-fetcher",
"rs/slack-notifications",
"rs/dre-canisters/node-provider-rewards-lib",
"rs/dre-canisters/trustworthy-node-metrics/src/trustworthy-node-metrics",
"rs/dre-canisters/trustworthy-node-metrics/src/trustworthy-node-metrics-types",
]
Expand Down
1 change: 1 addition & 0 deletions bazel/external_crates.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def external_crates_repository():
"//rs/ic-observability/sns-downloader:Cargo.toml",
"//rs/log-fetcher:Cargo.toml",
"//rs/slack-notifications:Cargo.toml",
"//rs/dre-canisters/node-provider-rewards-lib:Cargo.toml",
"//rs/dre-canisters/trustworthy-node-metrics/src/trustworthy-node-metrics:Cargo.toml",
"//rs/dre-canisters/trustworthy-node-metrics/src/trustworthy-node-metrics-types:Cargo.toml",
],
Expand Down
31 changes: 31 additions & 0 deletions rs/dre-canisters/node-provider-rewards-lib/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
load("@crate_index_dre//:defs.bzl", "aliases", "all_crate_deps")
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")

package(default_visibility = ["//visibility:public"])

rust_library(
name = "node-provider-rewards-lib",
srcs = glob(["src/**/*.rs"]),
aliases = aliases(),
proc_macro_deps = all_crate_deps(
proc_macro = True,
),
deps = all_crate_deps(
normal = True,
),
)

rust_test(
name = "unit_test",
aliases = aliases(
normal_dev = True,
proc_macro_dev = True,
),
crate = ":node-provider-rewards-lib",
proc_macro_deps = all_crate_deps(
proc_macro_dev = True,
),
deps = all_crate_deps(
normal_dev = True,
),
)
23 changes: 23 additions & 0 deletions rs/dre-canisters/node-provider-rewards-lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "node-provider-rewards-lib"
version.workspace = true
authors.workspace = true
description.workspace = true
documentation.workspace = true
edition.workspace = true
license.workspace = true

[dependencies]
ahash = { workspace = true }
ic-base-types = { workspace = true }
ic-protobuf = { workspace = true }
itertools = { workspace = true }
lazy_static = { workspace = true }
num-traits = { workspace = true }
rust_decimal = { workspace = true }
rust_decimal_macros = { workspace = true }
ic-management-canister-types = { workspace = true }

[lib]
name = "node_provider_rewards_lib"
path = "src/lib.rs"
3 changes: 3 additions & 0 deletions rs/dre-canisters/node-provider-rewards-lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod v1_logs;
pub mod v1_rewards;
pub mod v1_types;
165 changes: 165 additions & 0 deletions rs/dre-canisters/node-provider-rewards-lib/src/v1_logs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
use ic_base_types::PrincipalId;
use itertools::Itertools;
use rust_decimal::{prelude::Zero, Decimal};
use std::fmt;

pub enum Operation {
Sum(Vec<Decimal>),
Avg(Vec<Decimal>),
Subtract(Decimal, Decimal),
Multiply(Decimal, Decimal),
Divide(Decimal, Decimal),
Set(Decimal),
SumOps(Vec<Operation>),
}

impl Operation {
fn sum(operators: &[Decimal]) -> Decimal {
operators.iter().fold(Decimal::zero(), |acc, val| acc + val)
}

fn format_values<T: fmt::Display>(items: &[T], prefix: &str) -> String {
if items.is_empty() {
"0".to_string()
} else {
format!("{}({})", prefix, items.iter().map(|item| format!("{}", item)).join(","),)
}
}

fn execute(&self) -> Decimal {
match self {
Operation::Sum(operators) => Self::sum(operators),
Operation::Avg(operators) => Self::sum(operators) / Decimal::from(operators.len().max(1)),
Operation::Subtract(o1, o2) => o1 - o2,
Operation::Divide(o1, o2) => o1 / o2,
Operation::Multiply(o1, o2) => o1 * o2,
Operation::Set(o1) => *o1,
Operation::SumOps(operations) => Self::sum(&operations.iter().map(|operation| operation.execute()).collect_vec()),
}
}
}

impl fmt::Display for Operation {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let (symbol, o1, o2) = match self {
Operation::Sum(values) => return write!(f, "{}", Operation::format_values(values, "sum")),
Operation::SumOps(operations) => return write!(f, "{}", Operation::format_values(operations, "sum")),
Operation::Avg(values) => return write!(f, "{}", Operation::format_values(values, "avg")),
Operation::Subtract(o1, o2) => ("-", o1, o2),
Operation::Divide(o1, o2) => ("/", o1, o2),
Operation::Multiply(o1, o2) => ("*", o1, o2),
Operation::Set(o1) => return write!(f, "set {}", o1),
};
write!(f, "{} {} {}", o1.round_dp(4), symbol, o2.round_dp(4))
}
}

pub enum LogEntry {
RewardsForNodeProvider(PrincipalId),
ComputeRewardMultiplierForNode(PrincipalId),
RewardsXDRTotal(Decimal),
Execute {
reason: String,
operation: Operation,
result: Decimal,
},
RewardablesInRegionNodeType {
node_type: String,
region: String,
count: usize,
assigned_multipliers: Vec<Decimal>,
unassigned_multipliers: Vec<Decimal>,
},
RateNotFoundInRewardTable {
node_type: String,
region: String,
},
RewardTableEntry {
node_type: String,
region: String,
coeff: Decimal,
base_rewards: Decimal,
},
AvgType3Rewards(String, Decimal),
}

impl fmt::Display for LogEntry {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
LogEntry::Execute { reason, operation, result } => {
write!(f, "ExecuteOperation: reason={}, operation={}, result={}", reason, operation, result)
}
LogEntry::RewardsForNodeProvider(principal) => {
write!(f, "RewardsForNodeProvider: principal={}", principal)
}
LogEntry::ComputeRewardMultiplierForNode(principal) => {
write!(f, "RewardsMultiplier: principal={}", principal)
}
LogEntry::RewardsXDRTotal(rewards_xdr_total) => {
write!(f, "Total rewards XDR permyriad: {}", rewards_xdr_total)
}
LogEntry::RateNotFoundInRewardTable { node_type, region } => {
write!(f, "RateNotFoundInRewardTable: node_type={}, region={}", node_type, region)
}
LogEntry::RewardTableEntry {
node_type,
region,
coeff,
base_rewards,
} => {
write!(
f,
"RewardTableEntry: node_type={}, region={}, coeff={}, base_rewards={}",
node_type, region, coeff, base_rewards
)
}
LogEntry::RewardablesInRegionNodeType {
node_type,
region,
count,
assigned_multipliers: assigned_multiplier,
unassigned_multipliers: unassigned_multiplier,
} => {
write!(
f,
"Region {} with type: {} | Rewardable Nodes: {} Assigned Multipliers: {:?} Unassigned Multipliers: {:?}",
node_type, region, count, assigned_multiplier, unassigned_multiplier
)
}
LogEntry::AvgType3Rewards(region, avg_rewards) => {
write!(f, "Avg. rewards for nodes with type: type3* in region: {} is {}", region, avg_rewards)
}
}
}
}

#[derive(Default)]
pub struct RewardsLog {
entries: Vec<LogEntry>,
}

impl RewardsLog {
pub fn add_entry(&mut self, entry: LogEntry) {
self.entries.push(entry);
}

pub fn execute(&mut self, reason: &str, operation: Operation) -> Decimal {
let result = operation.execute();
let entry = LogEntry::Execute {
reason: reason.to_string(),
operation,
result,
};
self.entries.push(entry);
result
}

pub fn get_log(&self) -> String {
let mut log = Vec::new();

for (index, entry) in self.entries.iter().enumerate() {
log.push(format!("Entry {}: {} ", index, entry));
}
log.join("\n")
}
}
Loading

0 comments on commit f6894d8

Please sign in to comment.