Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2021 - Nym Technologies SA <[email protected]>
// 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;
Expand All @@ -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<MsgResponse> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<u64, NyxdError> {
let maybe_attributes = logs
.iter()
Expand Down
34 changes: 33 additions & 1 deletion common/client-libs/validator-client/src/nyxd/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
// Copyright 2023 - Nym Technologies SA <[email protected]>
// 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<String> {
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<String> {
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
Expand All @@ -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<String> {
// 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)
}
21 changes: 11 additions & 10 deletions nym-api/src/ecash/dkg/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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::<NodeIndex>()
.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::<NodeIndex>()
.map_err(|_| EcashError::NodeIndexRecoveryError {
reason: String::from("node index could not be parsed"),
})?;

Ok(node_index)
}
Expand Down
31 changes: 16 additions & 15 deletions nym-api/src/ecash/dkg/key_derivation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -453,25 +454,25 @@ impl<R: RngCore + CryptoRng> DkgController<R> {
key: &VerificationKeyAuth,
resharing: bool,
) -> Result<u64, KeyDerivationError> {
fn extract_proposal_id_from_logs(
fn extract_proposal_id(
logs: &[Log],
events: &[Event],
tx_hash: Hash,
) -> Result<u64, KeyDerivationError> {
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(),
},
)?;

proposal_attribute
.value
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_value
.parse()
.map_err(|_| KeyDerivationError::UnparsableProposalId {
raw: proposal_attribute.value.clone(),
raw: proposal_value.clone(),
})
}

Expand All @@ -481,7 +482,7 @@ impl<R: RngCore + CryptoRng> DkgController<R> {
.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)
Expand Down