Skip to content

Commit

Permalink
Add Rewards Computation logging for audit (#821)
Browse files Browse the repository at this point in the history
  • Loading branch information
pietrodimarco-dfinity authored Aug 30, 2024
1 parent 936a17b commit 9a57edb
Show file tree
Hide file tree
Showing 14 changed files with 311 additions and 167 deletions.
64 changes: 38 additions & 26 deletions Cargo.Bazel.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"checksum": "8f8316053a9bbdfb2e006bb7300906283da5cbb9888e2eaaab85777bd8924e11",
"checksum": "2bbd3d15bf606c9af001e99012b5deb363a91b712bf83a21702c95fdf0ccdf21",
"crates": {
"actix-codec 0.5.2": {
"name": "actix-codec",
Expand Down Expand Up @@ -21383,7 +21383,7 @@
"target": "prost"
},
{
"id": "rust_decimal 1.35.0",
"id": "rust_decimal 1.36.0",
"target": "rust_decimal"
},
{
Expand Down Expand Up @@ -21658,7 +21658,7 @@
"target": "prost"
},
{
"id": "rust_decimal 1.35.0",
"id": "rust_decimal 1.36.0",
"target": "rust_decimal"
},
{
Expand Down Expand Up @@ -21961,7 +21961,7 @@
"target": "lazy_static"
},
{
"id": "rust_decimal 1.35.0",
"id": "rust_decimal 1.36.0",
"target": "rust_decimal"
},
{
Expand All @@ -21979,7 +21979,7 @@
"proc_macro_deps": {
"common": [
{
"id": "rust_decimal_macros 1.35.0",
"id": "rust_decimal_macros 1.36.0",
"target": "rust_decimal_macros"
}
],
Expand Down Expand Up @@ -22397,7 +22397,7 @@
"target": "registry_canister"
},
{
"id": "rust_decimal 1.35.0",
"id": "rust_decimal 1.36.0",
"target": "rust_decimal"
},
{
Expand Down Expand Up @@ -22442,7 +22442,7 @@
"target": "ic_nervous_system_common_build_metadata"
},
{
"id": "rust_decimal_macros 1.35.0",
"id": "rust_decimal_macros 1.36.0",
"target": "rust_decimal_macros"
},
{
Expand Down Expand Up @@ -24146,7 +24146,7 @@
"target": "rand_chacha"
},
{
"id": "rust_decimal 1.35.0",
"id": "rust_decimal 1.36.0",
"target": "rust_decimal"
},
{
Expand Down Expand Up @@ -24176,7 +24176,7 @@
"target": "ic_nervous_system_common_build_metadata"
},
{
"id": "rust_decimal_macros 1.35.0",
"id": "rust_decimal_macros 1.36.0",
"target": "rust_decimal_macros"
},
{
Expand Down Expand Up @@ -24293,7 +24293,7 @@
"target": "num_traits"
},
{
"id": "rust_decimal 1.35.0",
"id": "rust_decimal 1.36.0",
"target": "rust_decimal"
}
],
Expand All @@ -24303,7 +24303,7 @@
"proc_macro_deps": {
"common": [
{
"id": "rust_decimal_macros 1.35.0",
"id": "rust_decimal_macros 1.36.0",
"target": "rust_decimal_macros"
}
],
Expand Down Expand Up @@ -24392,7 +24392,7 @@
"target": "mockall"
},
{
"id": "rust_decimal 1.35.0",
"id": "rust_decimal 1.36.0",
"target": "rust_decimal"
}
],
Expand Down Expand Up @@ -24832,7 +24832,7 @@
"target": "prost"
},
{
"id": "rust_decimal 1.35.0",
"id": "rust_decimal 1.36.0",
"target": "rust_decimal"
},
{
Expand All @@ -24854,7 +24854,7 @@
"target": "async_trait"
},
{
"id": "rust_decimal_macros 1.35.0",
"id": "rust_decimal_macros 1.36.0",
"target": "rust_decimal_macros"
}
],
Expand Down Expand Up @@ -37794,13 +37794,13 @@
},
"license": "MIT OR Apache-2.0"
},
"rust_decimal 1.35.0": {
"rust_decimal 1.36.0": {
"name": "rust_decimal",
"version": "1.35.0",
"version": "1.36.0",
"repository": {
"Http": {
"url": "https://static.crates.io/crates/rust_decimal/1.35.0/download",
"sha256": "1790d1c4c0ca81211399e0e0af16333276f375209e71a37b67698a373db5b47a"
"url": "https://static.crates.io/crates/rust_decimal/1.36.0/download",
"sha256": "b082d80e3e3cc52b2ed634388d436fe1f4de6af5786cc2de9ba9737527bdf555"
}
},
"targets": [
Expand Down Expand Up @@ -37847,7 +37847,7 @@
"target": "num_traits"
},
{
"id": "rust_decimal 1.35.0",
"id": "rust_decimal 1.36.0",
"target": "build_script_build"
},
{
Expand All @@ -37858,7 +37858,7 @@
"selects": {}
},
"edition": "2021",
"version": "1.35.0"
"version": "1.36.0"
},
"build_script_attrs": {
"data_glob": [
Expand All @@ -37867,13 +37867,13 @@
},
"license": "MIT"
},
"rust_decimal_macros 1.35.0": {
"rust_decimal_macros 1.36.0": {
"name": "rust_decimal_macros",
"version": "1.35.0",
"version": "1.36.0",
"repository": {
"Http": {
"url": "https://static.crates.io/crates/rust_decimal_macros/1.35.0/download",
"sha256": "a05bf7103af0797dbce0667c471946b29b9eaea34652eff67324f360fec027de"
"url": "https://static.crates.io/crates/rust_decimal_macros/1.36.0/download",
"sha256": "da991f231869f34268415a49724c6578e740ad697ba0999199d6f22b3949332c"
}
},
"targets": [
Expand Down Expand Up @@ -37905,14 +37905,14 @@
"target": "quote"
},
{
"id": "rust_decimal 1.35.0",
"id": "rust_decimal 1.36.0",
"target": "rust_decimal"
}
],
"selects": {}
},
"edition": "2021",
"version": "1.35.0"
"version": "1.36.0"
},
"license": "MIT"
},
Expand Down Expand Up @@ -45528,6 +45528,14 @@
{
"id": "itertools 0.13.0",
"target": "itertools"
},
{
"id": "num-traits 0.2.19",
"target": "num_traits"
},
{
"id": "rust_decimal 1.36.0",
"target": "rust_decimal"
}
],
"selects": {}
Expand All @@ -45538,6 +45546,10 @@
{
"id": "ic-cdk-macros 0.15.0",
"target": "ic_cdk_macros"
},
{
"id": "rust_decimal_macros 1.36.0",
"target": "rust_decimal_macros"
}
],
"selects": {}
Expand Down
11 changes: 7 additions & 4 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ keyring = "3.2.0"
lazy_static = "1.5.0"
log = "0.4.22"
lru = "0.12.4"
num-traits = "0.2"
opentelemetry = { version = "0.22.0", features = ["metrics"] }
phantom_newtype = { git = "https://github.com/dfinity/ic.git", rev = "26d5f9d0bdca0a817c236134dc9c7317b32c69a5" }
pkcs11 = "0.5.0"
Expand All @@ -173,6 +174,8 @@ retry = "2.0.0"
reverse_geocoder = "4.1.1"
ring = "0.17.8"
rstest = { version = "0.22.0", default-features = false }
rust_decimal = "1.36.0" # or the latest version
rust_decimal_macros = "1.36.0" # optional, for using macros
serde = { version = "1.0", features = ["derive"] }
serde_derive = "1.0.209"
serde_json = "1.0.127"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const App: React.FC = () => {
to_ts: dateToNanoseconds(periodFilter.dateEnd),
};
const nodeRewardsResponse = await trustworthy_node_metrics.node_rewards(request);
const sortedNodeRewards = nodeRewardsResponse.sort((a, b) => a.rewards_percent - b.rewards_percent);
const sortedNodeRewards = nodeRewardsResponse.sort((a, b) => a.rewards_computation.rewards_percent - b.rewards_computation.rewards_percent);
const subnets = new Set(sortedNodeRewards.flatMap(node => node.daily_node_metrics.map(data => data.subnet_assigned.toText())));
const providers = new Set(sortedNodeRewards.flatMap(node => node.node_provider_id.toText()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ export const NodeList: React.FC<NodeListProps> = ({ nodeRewards }) => {
</TableCell>
<TableCell>{nodeMetrics.node_provider_id.toText()}</TableCell>
<TableCell>{nodeMetrics.daily_node_metrics.length}</TableCell>
<TableCell>{Math.round(nodeMetrics.rewards_percent * 100)}%</TableCell>
<TableCell>{Math.round(nodeMetrics.rewards_stats.failure_rate * 100)}%</TableCell>
<TableCell>{Math.round(nodeMetrics.rewards_computation.rewards_percent * 100)}%</TableCell>
<TableCell>{Math.round(nodeMetrics.rewards_computation.failure_rate * 100)}%</TableCell>
</TableRow>
))}
</TableBody>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface NodePageProps {
periodFilter: PeriodFilter;
}

const NodeMetricsStats: React.FC<{ stats: NodeRewardsResponse['rewards_stats'] }> = ({ stats }) => (
const NodeMetricsStats: React.FC<{ stats: NodeRewardsResponse['rewards_computation'] }> = ({ stats }) => (
<Box sx={boxStyleWidget('left')}>
<WidgetNumber value={stats.blocks_proposed.toString()} title="Blocks Proposed Total" />
<WidgetNumber value={stats.blocks_failed.toString()} title="Blocks Failed Total" />
Expand All @@ -40,8 +40,8 @@ export const NodePage: React.FC<NodePageProps> = ({ nodeRewards, periodFilter })
}

const chartDailyData: ChartData[] = generateChartData(periodFilter, nodeMetrics.daily_node_metrics);
const failureRateAvg = Math.round(nodeMetrics.rewards_stats.failure_rate * 100);
const rewardsPercent = Math.round(nodeMetrics.rewards_percent * 100);
const failureRateAvg = Math.round(nodeMetrics.rewards_computation.failure_rate * 100);
const rewardsPercent = Math.round(nodeMetrics.rewards_computation.rewards_percent * 100);
const rewardsReduction = 100 - rewardsPercent;

const rows: GridRowsProp = nodeMetrics.daily_node_metrics.map((data, index) => {
Expand Down Expand Up @@ -81,7 +81,7 @@ export const NodePage: React.FC<NodePageProps> = ({ nodeRewards, periodFilter })
<WidgetGauge value={rewardsPercent} title={"Rewards Total"} />
</Grid>
<Grid item xs={12} md={4}>
<NodeMetricsStats stats={nodeMetrics.rewards_stats} />
<NodeMetricsStats stats={nodeMetrics.rewards_computation} />
</Grid>
<Grid item xs={12} md={8}>
<NodePerformanceStats rewardsReduction={rewardsReduction.toString().concat("%")} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const NodeProviderPage: React.FC<NodeProviderPageProps> = ({ nodeRewards,
const providerNodeMetrics = nodeRewards
.filter((nodeMetrics) => nodeMetrics.node_provider_id.toText() === provider)
const highFailureRateChart = providerNodeMetrics
.filter(nodeMetrics => nodeMetrics.rewards_stats.rewards_reduction > 0)
.filter(nodeMetrics => nodeMetrics.rewards_computation.rewards_reduction > 0)
.flatMap(nodeMetrics => {
const chartData = generateChartData(periodFilter, nodeMetrics.daily_node_metrics);
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const RewardTable: React.FC<RewardTableProps> = ({ nodeRewards }) => {
{nodeMetrics.node_id.toText()}
</TableCell>
<TableCell component="th" scope="row">
{Math.round(nodeMetrics.rewards_stats.failure_rate * 100)}%
{nodeMetrics.rewards_computation.failure_rate * 100}%
</TableCell>
</TableRow>
))}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::borrow::Cow;
use std::{borrow::Cow, fmt};

use candid::{CandidType, Decode, Deserialize, Encode, Principal};
use dfn_core::api::PrincipalId;
Expand Down Expand Up @@ -79,6 +79,16 @@ pub struct DailyNodeMetrics {
pub failure_rate: f64,
}

impl fmt::Display for DailyNodeMetrics {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"timestamp_nanoseconds: {}, num_blocks_proposed: {}, num_blocks_failed: {}",
self.ts, self.num_blocks_proposed, self.num_blocks_failed
)
}
}

impl DailyNodeMetrics {
pub fn new(ts: TimestampNanos, subnet_assignment: Principal, proposed_blocks: u64, failed_blocks: u64) -> Self {
let total_blocks = failed_blocks + proposed_blocks;
Expand All @@ -99,19 +109,20 @@ impl DailyNodeMetrics {
}

#[derive(Debug, Deserialize, CandidType)]
pub struct RewardsStats {
pub struct RewardsComputationResult {
pub rewards_percent: f64,
pub rewards_reduction: f64,
pub blocks_failed: u64,
pub blocks_proposed: u64,
pub blocks_total: u64,
pub failure_rate: f64,
pub rewards_reduction: f64,
pub computation_log: String,
}

#[derive(Debug, Deserialize, CandidType)]
pub struct NodeRewardsResponse {
pub node_id: Principal,
pub node_provider_id: Principal,
pub rewards_percent: f64,
pub daily_node_metrics: Vec<DailyNodeMetrics>,
pub rewards_stats: RewardsStats,
pub rewards_computation: RewardsComputationResult,
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ dfn_core = { workspace = true }
trustworthy-node-metrics-types = { workspace = true }
ic-base-types = { workspace = true }
ic-registry-keys = { workspace = true }
rust_decimal = { workspace = true }
rust_decimal_macros = { workspace = true }
num-traits = { workspace = true }
Loading

0 comments on commit 9a57edb

Please sign in to comment.