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
32 changes: 22 additions & 10 deletions mm2src/coins/tendermint/tendermint_tx_history_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ where
}

/// Reads sender and receiver addresses properly from an HTLC event.
fn read_real_htlc_addresses(transfer_details: &mut TransferDetails, msg_event: &&Event) {
fn read_real_htlc_addresses(transfer_details: &mut TransferDetails, msg_event: &Event) {
match msg_event.kind.as_str() {
CREATE_HTLC_EVENT => {
let from = some_or_return!(get_value_from_event_attributes(
Expand Down Expand Up @@ -492,10 +492,17 @@ where
}
}

fn parse_transfer_values_from_events(tx_events: Vec<&Event>) -> Vec<TransferDetails> {
fn parse_transfer_values_from_events(mut tx_events: Vec<&Event>) -> Vec<TransferDetails> {
let mut transfer_details_list: Vec<TransferDetails> = vec![];

for event in tx_events.iter() {
for i in 0..tx_events.len() {
// Avoid out-of-bounds exceptions after removing HTLC and IBC elements below.
if i >= tx_events.len() {
break;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this condition can never be true...or is my assumption wrong? cc. @onur-ozkan

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be true when tx_events.remove is called. I will add explanatory comment on this.

let event = tx_events[i];

let amount_with_denoms = some_or_continue!(get_value_from_event_attributes(
&event.attributes,
AMOUNT_TAG_KEY,
Expand Down Expand Up @@ -533,18 +540,20 @@ where

// For HTLC transactions, the sender and receiver addresses in the "transfer" event will be incorrect.
// Use `read_real_htlc_addresses` to handle them properly.
if let Some(htlc_event) = tx_events
if let Some(htlc_event_index) = tx_events
.iter()
.find(|e| [CREATE_HTLC_EVENT, CLAIM_HTLC_EVENT].contains(&e.kind.as_str()))
.position(|e| [CREATE_HTLC_EVENT, CLAIM_HTLC_EVENT].contains(&e.kind.as_str()))
{
read_real_htlc_addresses(&mut tx_details, htlc_event);
read_real_htlc_addresses(&mut tx_details, tx_events[htlc_event_index]);
tx_events.remove(htlc_event_index);
}
// For IBC transactions, the sender and receiver addresses in the "transfer" event will be incorrect.
// Use `read_real_ibc_addresses` to handle them properly.
else if let Some(ibc_event) = tx_events.iter().find(|e| {
else if let Some(ibc_event_index) = tx_events.iter().position(|e| {
[IBC_SEND_EVENT, IBC_RECEIVE_EVENT, IBC_NFT_RECEIVE_EVENT].contains(&e.kind.as_str())
}) {
read_real_ibc_addresses(&mut tx_details, ibc_event);
read_real_ibc_addresses(&mut tx_details, tx_events[ibc_event_index]);
tx_events.remove(ibc_event_index);
}

handle_new_transfer_event(&mut transfer_details_list, tx_details);
Expand Down Expand Up @@ -617,7 +626,7 @@ where
},

unrecognized => {
log::warn!(
covered_warn!(
"Found an unrecognized event '{unrecognized}' in transaction history processing."
);
},
Expand All @@ -643,7 +652,10 @@ where
}
}

fn get_transfer_details(tx_events: Vec<Event>, fee_amount_with_denom: String) -> Vec<TransferDetails> {
fn get_transfer_details(mut tx_events: Vec<Event>, fee_amount_with_denom: String) -> Vec<TransferDetails> {
tx_events.sort_by(|a, b| a.kind.cmp(&b.kind));
tx_events.dedup();

// We are only interested `DELEGATE_EVENT` events for delegation transactions.
if let Some(delegate_event) = tx_events.iter().find(|e| e.kind == DELEGATE_EVENT) {
return parse_transfer_values_from_events(vec![delegate_event]);
Expand Down
22 changes: 22 additions & 0 deletions mm2src/common/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,22 @@ pub fn short_log_time(ms: u64) -> DelayedFormat<StrftimeItems<'static>> {
time.format("%d %H:%M:%S")
}

#[cfg(not(test))]
#[macro_export]
macro_rules! covered_warn {
($($arg:tt)+) => {
common::log::warn!($($arg)+)
};
}

#[cfg(test)]
#[macro_export]
macro_rules! covered_warn {
($($arg:tt)+) => {
panic!($($arg)+)
};
}

/// Debug logging.
///
/// This logging SHOULD be human-readable but it is not intended for the end users specifically.
Expand Down Expand Up @@ -1176,4 +1192,10 @@ pub mod tests {
assert!(tail[0].ends_with("/3:33) [tag] status 1%…"));
});
}

#[test]
#[should_panic(expected = "Fail with me...")]
fn test_covered_warn() {
covered_warn!("Fail with me...");
}
}