diff --git a/common/client-libs/validator-client/src/nyxd/cosmwasm_client/helpers.rs b/common/client-libs/validator-client/src/nyxd/cosmwasm_client/helpers.rs index c437125b16..2212aa4cff 100644 --- a/common/client-libs/validator-client/src/nyxd/cosmwasm_client/helpers.rs +++ b/common/client-libs/validator-client/src/nyxd/cosmwasm_client/helpers.rs @@ -1,6 +1,7 @@ // Copyright 2021 - Nym Technologies SA // SPDX-License-Identifier: Apache-2.0 +use crate::nyxd::cosmwasm_client::types::ExecuteResult; use crate::nyxd::error::NyxdError; use cosmrs::abci::TxMsgData; use cosmrs::cosmwasm::MsgExecuteContractResponse; @@ -9,7 +10,6 @@ use log::error; use prost::bytes::Bytes; use tendermint_rpc::endpoint::broadcast; -use crate::nyxd::cosmwasm_client::types::ExecuteResult; pub use cosmrs::abci::MsgResponse; pub fn parse_msg_responses(data: Bytes) -> Vec { diff --git a/common/client-libs/validator-client/src/nyxd/cosmwasm_client/logs.rs b/common/client-libs/validator-client/src/nyxd/cosmwasm_client/logs.rs index 4a31a1fc7e..3d5ecab961 100644 --- a/common/client-libs/validator-client/src/nyxd/cosmwasm_client/logs.rs +++ b/common/client-libs/validator-client/src/nyxd/cosmwasm_client/logs.rs @@ -21,7 +21,8 @@ pub struct Log { /// Searches in logs for the first event of the given event type and in that event /// for the first attribute with the given attribute key. -pub fn find_attribute<'a>( +#[deprecated] +pub fn find_attribute_in_logs<'a>( logs: &'a [Log], event_type: &str, attribute_key: &str, @@ -35,6 +36,7 @@ pub fn find_attribute<'a>( } /// Search for the proposal id in the given log. It'll be in the LAST wasm event, with attribute key "proposal_id" +#[deprecated] pub fn find_proposal_id(logs: &[Log]) -> Result { let maybe_attributes = logs .iter() diff --git a/common/client-libs/validator-client/src/nyxd/helpers.rs b/common/client-libs/validator-client/src/nyxd/helpers.rs index cec865252a..3fbfdaaaf3 100644 --- a/common/client-libs/validator-client/src/nyxd/helpers.rs +++ b/common/client-libs/validator-client/src/nyxd/helpers.rs @@ -1,12 +1,24 @@ // Copyright 2023 - Nym Technologies SA // SPDX-License-Identifier: Apache-2.0 +use crate::nyxd::cosmwasm_client::logs::Log; use crate::nyxd::TxResponse; +use cosmrs::tendermint::abci; + +pub use abci::Event; // Searches in events for an event of the given event type which contains an // attribute for with the given key. pub fn find_tx_attribute(tx: &TxResponse, event_type: &str, attribute_key: &str) -> Option { - let event = tx.tx_result.events.iter().find(|e| e.kind == event_type)?; + find_event_attribute(&tx.tx_result.events, event_type, attribute_key) +} + +pub fn find_event_attribute( + events: &[Event], + event_type: &str, + attribute_key: &str, +) -> Option { + let event = events.iter().find(|e| e.kind == event_type)?; let attribute = event.attributes.iter().find(|&attr| { if let Ok(key_str) = attr.key_str() { key_str == attribute_key @@ -16,3 +28,23 @@ pub fn find_tx_attribute(tx: &TxResponse, event_type: &str, attribute_key: &str) })?; Some(attribute.value_str().ok().map(|str| str.to_string())).flatten() } + +pub fn find_attribute_value_in_logs_or_events( + logs: &[Log], + events: &[Event], + event_type: &str, + attribute_key: &str, +) -> Option { + // if logs are empty, i.e. we're using post 0.50 code, parse the events instead + if !logs.is_empty() { + #[allow(deprecated)] + return crate::nyxd::cosmwasm_client::logs::find_attribute_in_logs( + logs, + event_type, + attribute_key, + ) + .map(|attr| attr.value.clone()); + } + + find_event_attribute(events, event_type, attribute_key) +} diff --git a/nym-api/src/ecash/dkg/client.rs b/nym-api/src/ecash/dkg/client.rs index 1471083060..5c8f993690 100644 --- a/nym-api/src/ecash/dkg/client.rs +++ b/nym-api/src/ecash/dkg/client.rs @@ -16,8 +16,9 @@ use nym_coconut_dkg_common::types::{ use nym_coconut_dkg_common::verification_key::{ContractVKShare, VerificationKeyShare}; use nym_contracts_common::IdentityKey; use nym_dkg::Threshold; -use nym_validator_client::nyxd::cosmwasm_client::logs::{find_attribute, NODE_INDEX}; +use nym_validator_client::nyxd::cosmwasm_client::logs::NODE_INDEX; use nym_validator_client::nyxd::cosmwasm_client::types::ExecuteResult; +use nym_validator_client::nyxd::helpers::find_attribute_value_in_logs_or_events; use nym_validator_client::nyxd::AccountId; pub(crate) struct DkgClient { @@ -168,15 +169,15 @@ impl DkgClient { .inner .register_dealer(bte_key, identity_key, announce_address, resharing) .await?; - let node_index = find_attribute(&res.logs, "wasm", NODE_INDEX) - .ok_or(EcashError::NodeIndexRecoveryError { - reason: String::from("node index not found"), - })? - .value - .parse::() - .map_err(|_| EcashError::NodeIndexRecoveryError { - reason: String::from("node index could not be parsed"), - })?; + let node_index = + find_attribute_value_in_logs_or_events(&res.logs, &res.events, "wasm", NODE_INDEX) + .ok_or(EcashError::NodeIndexRecoveryError { + reason: String::from("node index not found"), + })? + .parse::() + .map_err(|_| EcashError::NodeIndexRecoveryError { + reason: String::from("node index could not be parsed"), + })?; Ok(node_index) } diff --git a/nym-api/src/ecash/dkg/key_derivation.rs b/nym-api/src/ecash/dkg/key_derivation.rs index e2ad2a7cb2..23291806b8 100644 --- a/nym-api/src/ecash/dkg/key_derivation.rs +++ b/nym-api/src/ecash/dkg/key_derivation.rs @@ -18,8 +18,9 @@ use nym_dkg::{ bte::{self, decrypt_share}, combine_shares, try_recover_verification_keys, Dealing, }; -use nym_validator_client::nyxd::cosmwasm_client::logs::{find_attribute, Log}; -use nym_validator_client::nyxd::Hash; +use nym_validator_client::nyxd::cosmwasm_client::logs::Log; +use nym_validator_client::nyxd::helpers::find_attribute_value_in_logs_or_events; +use nym_validator_client::nyxd::{Event, Hash}; use rand::{CryptoRng, RngCore}; use std::collections::{BTreeMap, HashMap}; use std::ops::Deref; @@ -453,25 +454,25 @@ impl DkgController { key: &VerificationKeyAuth, resharing: bool, ) -> Result { - fn extract_proposal_id_from_logs( + fn extract_proposal_id( logs: &[Log], + events: &[Event], tx_hash: Hash, ) -> Result { let event_type = "wasm"; let attribute_key = DKG_PROPOSAL_ID; - let proposal_attribute = find_attribute(logs, event_type, attribute_key).ok_or( - KeyDerivationError::MissingProposalIdAttribute { - tx_hash, - event_type: event_type.to_string(), - attribute_key: attribute_key.to_string(), - }, - )?; + let proposal_value = + find_attribute_value_in_logs_or_events(logs, events, event_type, attribute_key) + .ok_or(KeyDerivationError::MissingProposalIdAttribute { + tx_hash, + event_type: event_type.to_string(), + attribute_key: attribute_key.to_string(), + })?; - proposal_attribute - .value + proposal_value .parse() .map_err(|_| KeyDerivationError::UnparsableProposalId { - raw: proposal_attribute.value.clone(), + raw: proposal_value.clone(), }) } @@ -481,7 +482,7 @@ impl DkgController { .submit_verification_key_share(key.to_bs58(), resharing) .await?; let hash = res.transaction_hash; - let proposal_id = extract_proposal_id_from_logs(&res.logs, hash)?; + let proposal_id = extract_proposal_id(&res.logs, &res.events, hash)?; debug!("Submitted own verification key share, proposal id {proposal_id} is attached to it. tx hash: {hash}"); Ok(proposal_id)