From 134ab2f0d6d27939d8d1d5cfb59645e92532b391 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Mon, 27 Mar 2023 14:52:40 +0100 Subject: [PATCH] Stop using rewarding reports as a source of truth for whether mixnodes got rewards (#3227) use contract information instead --- nym-api/src/epoch_operations/error.rs | 5 +++- nym-api/src/epoch_operations/rewarding.rs | 30 +++++++---------------- nym-api/src/support/storage/manager.rs | 2 ++ nym-api/src/support/storage/mod.rs | 24 ++---------------- nym-api/src/support/storage/models.rs | 2 ++ 5 files changed, 19 insertions(+), 44 deletions(-) diff --git a/nym-api/src/epoch_operations/error.rs b/nym-api/src/epoch_operations/error.rs index a57e0153fc..0f6e2c9421 100644 --- a/nym-api/src/epoch_operations/error.rs +++ b/nym-api/src/epoch_operations/error.rs @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 use crate::node_status_api::models::NymApiStorageError; -use nym_mixnet_contract_common::EpochState; +use nym_mixnet_contract_common::{EpochState, MixId}; use thiserror::Error; use validator_client::nyxd::error::NyxdError; use validator_client::nyxd::AccountId; @@ -22,6 +22,9 @@ pub enum RewardingError { operation: String, }, + #[error("it seems the current epoch is in mid-rewarding state (last rewarded is {last_rewarded}). With our current nym-api this shouldn't have been possible. Manual intervention is required.")] + MidMixRewarding { last_rewarded: MixId }, + // #[error("There were no mixnodes to reward (network is dead)")] // NoMixnodesToReward, #[error("Failed to execute the smart contract - {0}")] diff --git a/nym-api/src/epoch_operations/rewarding.rs b/nym-api/src/epoch_operations/rewarding.rs index 1630d369d7..1d2e5f5d13 100644 --- a/nym-api/src/epoch_operations/rewarding.rs +++ b/nym-api/src/epoch_operations/rewarding.rs @@ -2,7 +2,6 @@ // SPDX-License-Identifier: Apache-2.0 use crate::epoch_operations::error::RewardingError; -use crate::support::storage::models::RewardingReport; use crate::RewardedSetUpdater; use nym_mixnet_contract_common::reward_params::Performance; use nym_mixnet_contract_common::{EpochState, ExecuteMsg, Interval, MixId}; @@ -42,8 +41,15 @@ impl RewardedSetUpdater { warn!("we seem to have crashed mid epoch operations... no need to reward mixnodes as we've already done that! (or this could be a false positive if there were no nodes to reward - to fix this warning later)"); Ok(()) } - EpochState::Rewarding { .. } => { + EpochState::Rewarding { last_rewarded, .. } => { log::info!("Rewarding the current rewarded set..."); + + // with how the nym-api is currently coded, this should never happen as we're always + // rewarding ALL mixnodes at once, but who knows what we might decide to do in the future... + if last_rewarded != 0 { + return Err(RewardingError::MidMixRewarding { last_rewarded }); + } + if let Err(err) = self._reward_current_rewarded_set(current_interval).await { log::error!("FAILED to reward rewarded set - {err}"); Err(err) @@ -62,17 +68,8 @@ impl RewardedSetUpdater { let mut to_reward = self.nodes_to_reward(current_interval).await; to_reward.sort_by_key(|a| a.mix_id); - if let Some(existing_report) = self - .storage - .get_rewarding_report(current_interval.current_epoch_absolute_id()) - .await? - { - warn!("We have already rewarded mixnodes for this rewarding epoch ({}). {} nodes should have gotten rewards", existing_report.absolute_epoch_id, existing_report.eligible_mixnodes); - return Ok(()); - } - if to_reward.is_empty() { - info!("There are no nodes to reward in this epoch"); + error!("There are no nodes to reward in this epoch - we shouldn't have been in the 'Rewarding' state!"); } else if let Err(err) = self.nyxd_client.send_rewarding_messages(&to_reward).await { error!( "failed to perform mixnode rewarding for epoch {}! Error encountered: {err}", @@ -83,15 +80,6 @@ impl RewardedSetUpdater { log::info!("rewarded {} mixnodes...", to_reward.len()); - let rewarding_report = RewardingReport { - absolute_epoch_id: current_interval.current_epoch_absolute_id(), - eligible_mixnodes: to_reward.len() as u32, - }; - - self.storage - .insert_rewarding_report(rewarding_report) - .await?; - Ok(()) } diff --git a/nym-api/src/support/storage/manager.rs b/nym-api/src/support/storage/manager.rs index f3e4d6e011..737cb1cfab 100644 --- a/nym-api/src/support/storage/manager.rs +++ b/nym-api/src/support/storage/manager.rs @@ -867,6 +867,7 @@ impl StorageManager { /// # Arguments /// /// * `report`: report to insert into the database + #[allow(unused)] pub(crate) async fn insert_rewarding_report( &self, report: RewardingReport, @@ -885,6 +886,7 @@ impl StorageManager { Ok(()) } + #[allow(unused)] pub(crate) async fn get_rewarding_report( &self, absolute_epoch_id: EpochId, diff --git a/nym-api/src/support/storage/mod.rs b/nym-api/src/support/storage/mod.rs index ce80251e3d..4a9d3691d2 100644 --- a/nym-api/src/support/storage/mod.rs +++ b/nym-api/src/support/storage/mod.rs @@ -9,8 +9,8 @@ use crate::node_status_api::models::{ }; use crate::node_status_api::{ONE_DAY, ONE_HOUR}; use crate::storage::manager::StorageManager; -use crate::storage::models::{NodeStatus, RewardingReport, TestingRoute}; -use nym_mixnet_contract_common::{EpochId, MixId}; +use crate::storage::models::{NodeStatus, TestingRoute}; +use nym_mixnet_contract_common::MixId; use rocket::fairing::AdHoc; use sqlx::ConnectOptions; use std::path::PathBuf; @@ -717,26 +717,6 @@ impl NymApiStorage { .map_err(|err| err.into()) } - pub(crate) async fn insert_rewarding_report( - &self, - report: RewardingReport, - ) -> Result<(), NymApiStorageError> { - self.manager - .insert_rewarding_report(report) - .await - .map_err(|err| err.into()) - } - - pub(crate) async fn get_rewarding_report( - &self, - absolute_epoch_id: EpochId, - ) -> Result, NymApiStorageError> { - self.manager - .get_rewarding_report(absolute_epoch_id) - .await - .map_err(|err| err.into()) - } - pub(crate) async fn get_blinded_signature_response( &self, tx_hash: &str, diff --git a/nym-api/src/support/storage/models.rs b/nym-api/src/support/storage/models.rs index 607629a46c..69d3554ca1 100644 --- a/nym-api/src/support/storage/models.rs +++ b/nym-api/src/support/storage/models.rs @@ -41,6 +41,8 @@ pub(crate) struct TestingRoute { pub(crate) monitor_run_db_id: i64, } +// for now let's leave it here to have a data model to use with existing database tables +#[allow(unused)] pub(crate) struct RewardingReport { // references particular interval_rewarding pub(crate) absolute_epoch_id: u32,