From 0a3e42700c9cf77e06fc86e442a7bd5704fd7022 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogdan-=C8=98tefan=20Neac=C5=9Fu?= Date: Tue, 31 Jan 2023 14:58:47 +0200 Subject: [PATCH] Fix vote soft error everywhere (#2941) --- nym-api/src/coconut/dkg/verification_key.rs | 15 +++++++++------ nym-api/src/coconut/helpers.rs | 18 ++++++++++++++++++ nym-api/src/coconut/mod.rs | 15 ++++----------- 3 files changed, 31 insertions(+), 17 deletions(-) create mode 100644 nym-api/src/coconut/helpers.rs diff --git a/nym-api/src/coconut/dkg/verification_key.rs b/nym-api/src/coconut/dkg/verification_key.rs index 15f0138a90..9aed655cfa 100644 --- a/nym-api/src/coconut/dkg/verification_key.rs +++ b/nym-api/src/coconut/dkg/verification_key.rs @@ -5,6 +5,7 @@ use crate::coconut::dkg::client::DkgClient; use crate::coconut::dkg::complaints::ComplaintReason; use crate::coconut::dkg::state::{ConsistentState, State}; use crate::coconut::error::CoconutError; +use crate::coconut::helpers::accepted_vote_err; use coconut_dkg_common::event_attributes::DKG_PROPOSAL_ID; use coconut_dkg_common::types::{NodeIndex, TOTAL_DEALINGS}; use coconut_dkg_common::verification_key::owner_from_cosmos_msgs; @@ -203,21 +204,23 @@ pub(crate) async fn verification_key_validation( .iter() .position(|node_index| contract_share.node_index == *node_index) { - if !check_vk_pairing(¶ms, &recovered_partials[idx], &vk) { + let ret = if !check_vk_pairing(¶ms, &recovered_partials[idx], &vk) { dkg_client .vote_verification_key_share(proposal_id, false) - .await?; + .await } else { dkg_client .vote_verification_key_share(proposal_id, true) - .await?; - } + .await + }; + accepted_vote_err(ret)?; } } Err(_) => { - dkg_client + let ret = dkg_client .vote_verification_key_share(proposal_id, false) - .await? + .await; + accepted_vote_err(ret)?; } } } diff --git a/nym-api/src/coconut/helpers.rs b/nym-api/src/coconut/helpers.rs new file mode 100644 index 0000000000..986bb6b305 --- /dev/null +++ b/nym-api/src/coconut/helpers.rs @@ -0,0 +1,18 @@ +// Copyright 2023 - Nym Technologies SA +// SPDX-License-Identifier: Apache-2.0 + +use crate::coconut::error::CoconutError; +use validator_client::nyxd::error::NyxdError::AbciError; + +// If the result is already established, the vote might be redundant and +// thus the transaction might fail +pub(crate) fn accepted_vote_err(ret: Result<(), CoconutError>) -> Result<(), CoconutError> { + if let Err(CoconutError::NyxdError(AbciError { ref log, .. })) = ret { + let accepted_err = multisig_contract_common::error::ContractError::NotOpen {}.to_string(); + // If redundant voting is not the case, error out on all other error variants + if !log.value().contains(&accepted_err) { + ret?; + } + } + Ok(()) +} diff --git a/nym-api/src/coconut/mod.rs b/nym-api/src/coconut/mod.rs index 8d4999f32a..dd059923cb 100644 --- a/nym-api/src/coconut/mod.rs +++ b/nym-api/src/coconut/mod.rs @@ -5,6 +5,7 @@ use self::comm::APICommunicationChannel; use crate::coconut::client::Client as LocalClient; use crate::coconut::deposit::extract_encryption_key; use crate::coconut::error::{CoconutError, Result}; +use crate::coconut::helpers::accepted_vote_err; use crate::support::storage::NymApiStorage; use coconut_bandwidth_contract_common::spend_credential::{ funds_from_cosmos_msgs, SpendCredentialStatus, @@ -33,7 +34,6 @@ use rocket::State as RocketState; use std::sync::Arc; use tokio::sync::Mutex; use validator_client::nym_api::routes::{BANDWIDTH, COCONUT_ROUTES}; -use validator_client::nyxd::error::NyxdError::AbciError; use validator_client::nyxd::{Coin, Fee}; pub(crate) mod client; @@ -41,6 +41,7 @@ pub(crate) mod comm; mod deposit; pub(crate) mod dkg; pub(crate) mod error; +pub(crate) mod helpers; pub(crate) mod keypair; #[cfg(test)] pub(crate) mod tests; @@ -301,6 +302,7 @@ pub async fn verify_bandwidth_credential( state.mix_denom.clone(), ); + // Vote yes or no on the proposal based on the verification result let ret = state .client .vote_proposal( @@ -313,16 +315,7 @@ pub async fn verify_bandwidth_credential( )), ) .await; - // Vote yes or no on the proposal based on the verification result - // If the result is already established, the vote might be redundant and - // thus the transaction might fail - if let Err(CoconutError::NyxdError(AbciError { ref log, .. })) = ret { - let accepted_err = multisig_contract_common::error::ContractError::NotOpen {}.to_string(); - // If redundant voting is not the case, error out on all other error variants - if !log.value().contains(&accepted_err) { - ret?; - } - } + accepted_vote_err(ret)?; Ok(Json(VerifyCredentialResponse::new(vote_yes))) }