Skip to content

Commit

Permalink
refactor(ampd-minor): make event deserialization tests deterministic …
Browse files Browse the repository at this point in the history
…and use goldie (#742)
  • Loading branch information
fish-sammy authored Jan 10, 2025
1 parent 9eb9413 commit 0ede89e
Show file tree
Hide file tree
Showing 18 changed files with 994 additions and 197 deletions.
72 changes: 23 additions & 49 deletions ampd/src/handlers/evm_verify_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,13 @@ mod tests {
use std::str::FromStr;

use axelar_wasm_std::msg_id::HexTxHashAndEventIndex;
use base64::engine::general_purpose::STANDARD;
use base64::Engine;
use cosmwasm_std;
use error_stack::{Report, Result};
use ethers_core::types::{H160, H256};
use ethers_providers::ProviderError;
use events::Error::{DeserializationFailed, EventTypeMismatch};
use events::Event;
use router_api::ChainName;
use tendermint::abci;
use tokio::sync::watch;
use tokio::test as async_test;
use voting_verifier::events::{PollMetadata, PollStarted, TxEventConfirmation};
Expand All @@ -243,14 +241,15 @@ mod tests {
use crate::event_processor::EventHandler;
use crate::evm::finalizer::Finalization;
use crate::evm::json_rpc::MockEthereumClient;
use crate::types::{EVMAddress, Hash, TMAddress};
use crate::handlers::tests::{into_structured_event, participants};
use crate::types::TMAddress;
use crate::PREFIX;

fn poll_started_event(participants: Vec<TMAddress>, expires_at: u64) -> PollStarted {
let msg_ids = [
HexTxHashAndEventIndex::new(Hash::random(), 0u64),
HexTxHashAndEventIndex::new(Hash::random(), 1u64),
HexTxHashAndEventIndex::new(Hash::random(), 10u64),
HexTxHashAndEventIndex::new(H256::repeat_byte(1), 0u64),
HexTxHashAndEventIndex::new(H256::repeat_byte(2), 1u64),
HexTxHashAndEventIndex::new(H256::repeat_byte(3), 10u64),
];
PollStarted::Messages {
metadata: PollMetadata {
Expand All @@ -272,28 +271,28 @@ mod tests {
tx_id: msg_ids[0].tx_hash_as_hex(),
event_index: u32::try_from(msg_ids[0].event_index).unwrap(),
message_id: msg_ids[0].to_string().parse().unwrap(),
source_address: format!("0x{:x}", EVMAddress::random()).parse().unwrap(),
source_address: format!("0x{:x}", H160::repeat_byte(1)).parse().unwrap(),
destination_chain: "ethereum".parse().unwrap(),
destination_address: format!("0x{:x}", EVMAddress::random()).parse().unwrap(),
payload_hash: Hash::random().to_fixed_bytes(),
destination_address: format!("0x{:x}", H160::repeat_byte(2)).parse().unwrap(),
payload_hash: H256::repeat_byte(4).to_fixed_bytes(),
},
TxEventConfirmation {
tx_id: msg_ids[1].tx_hash_as_hex(),
event_index: u32::try_from(msg_ids[1].event_index).unwrap(),
message_id: msg_ids[1].to_string().parse().unwrap(),
source_address: format!("0x{:x}", EVMAddress::random()).parse().unwrap(),
source_address: format!("0x{:x}", H160::repeat_byte(3)).parse().unwrap(),
destination_chain: "ethereum".parse().unwrap(),
destination_address: format!("0x{:x}", EVMAddress::random()).parse().unwrap(),
payload_hash: Hash::random().to_fixed_bytes(),
destination_address: format!("0x{:x}", H160::repeat_byte(4)).parse().unwrap(),
payload_hash: H256::repeat_byte(5).to_fixed_bytes(),
},
TxEventConfirmation {
tx_id: msg_ids[2].tx_hash_as_hex(),
event_index: u32::try_from(msg_ids[2].event_index).unwrap(),
message_id: msg_ids[2].to_string().parse().unwrap(),
source_address: format!("0x{:x}", EVMAddress::random()).parse().unwrap(),
source_address: format!("0x{:x}", H160::repeat_byte(5)).parse().unwrap(),
destination_chain: "ethereum".parse().unwrap(),
destination_address: format!("0x{:x}", EVMAddress::random()).parse().unwrap(),
payload_hash: Hash::random().to_fixed_bytes(),
destination_address: format!("0x{:x}", H160::repeat_byte(6)).parse().unwrap(),
payload_hash: H256::repeat_byte(6).to_fixed_bytes(),
},
],
}
Expand All @@ -302,7 +301,7 @@ mod tests {
#[test]
fn should_not_deserialize_incorrect_event() {
// incorrect event type
let mut event: Event = to_event(
let mut event: Event = into_structured_event(
poll_started_event(participants(5, None), 100),
&TMAddress::random(PREFIX),
);
Expand All @@ -322,7 +321,7 @@ mod tests {
));

// invalid field
let mut event: Event = to_event(
let mut event: Event = into_structured_event(
poll_started_event(participants(5, None), 100),
&TMAddress::random(PREFIX),
);
Expand All @@ -344,13 +343,14 @@ mod tests {
}

#[test]
fn should_deserialize_correct_event() {
let event: Event = to_event(
fn evm_verify_msg_should_deserialize_correct_event() {
let event: Event = into_structured_event(
poll_started_event(participants(5, None), 100),
&TMAddress::random(PREFIX),
);
let event: Result<PollStartedEvent, events::Error> = event.try_into();
assert!(event.is_ok());
let event: PollStartedEvent = event.try_into().unwrap();

goldie::assert_debug!(event);
}

#[async_test]
Expand All @@ -366,7 +366,7 @@ mod tests {
let voting_verifier_contract = TMAddress::random(PREFIX);
let verifier = TMAddress::random(PREFIX);
let expiration = 100u64;
let event: Event = to_event(
let event: Event = into_structured_event(
poll_started_event(participants(5, Some(verifier.clone())), expiration),
&voting_verifier_contract,
);
Expand All @@ -390,30 +390,4 @@ mod tests {
// poll is expired, should not hit rpc error now
assert_eq!(handler.handle(&event).await.unwrap(), vec![]);
}

fn to_event(event: impl Into<cosmwasm_std::Event>, contract_address: &TMAddress) -> Event {
let mut event: cosmwasm_std::Event = event.into();

event.ty = format!("wasm-{}", event.ty);
event = event.add_attribute("_contract_address", contract_address.to_string());

abci::Event::new(
event.ty,
event
.attributes
.into_iter()
.map(|cosmwasm_std::Attribute { key, value }| {
(STANDARD.encode(key), STANDARD.encode(value))
}),
)
.try_into()
.unwrap()
}

fn participants(n: u8, verifier: Option<TMAddress>) -> Vec<TMAddress> {
(0..n)
.map(|_| TMAddress::random(PREFIX))
.chain(verifier)
.collect()
}
}
47 changes: 10 additions & 37 deletions ampd/src/handlers/evm_verify_verifier_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,13 @@ mod tests {
use std::str::FromStr;

use axelar_wasm_std::msg_id::HexTxHashAndEventIndex;
use base64::engine::general_purpose::STANDARD;
use base64::Engine;
use error_stack::{Report, Result};
use error_stack::Report;
use ethers_core::types::H256;
use ethers_providers::ProviderError;
use events::Event;
use multisig::key::KeyType;
use multisig::test::common::{build_verifier_set, ecdsa_test_data};
use router_api::ChainName;
use tendermint::abci;
use tokio::sync::watch;
use tokio::test as async_test;
use voting_verifier::events::{PollMetadata, PollStarted, VerifierSetConfirmation};
Expand All @@ -217,18 +215,19 @@ mod tests {
use crate::evm::finalizer::Finalization;
use crate::evm::json_rpc::MockEthereumClient;
use crate::handlers::evm_verify_verifier_set::PollStartedEvent;
use crate::types::{Hash, TMAddress};
use crate::handlers::tests::{into_structured_event, participants};
use crate::types::TMAddress;
use crate::PREFIX;

#[test]
fn should_deserialize_correct_event() {
let event: Event = to_event(
fn evm_verify_verifier_set_should_deserialize_correct_event() {
let event: Event = into_structured_event(
poll_started_event(participants(5, None), 100),
&TMAddress::random(PREFIX),
);
let event: Result<PollStartedEvent, events::Error> = event.try_into();
let event: PollStartedEvent = event.try_into().unwrap();

assert!(event.is_ok());
goldie::assert_debug!(event);
}

#[async_test]
Expand All @@ -244,7 +243,7 @@ mod tests {
let voting_verifier = TMAddress::random(PREFIX);
let verifier = TMAddress::random(PREFIX);
let expiration = 100u64;
let event: Event = to_event(
let event: Event = into_structured_event(
poll_started_event(participants(5, Some(verifier.clone())), expiration),
&voting_verifier,
);
Expand All @@ -270,7 +269,7 @@ mod tests {
}

fn poll_started_event(participants: Vec<TMAddress>, expires_at: u64) -> PollStarted {
let msg_id = HexTxHashAndEventIndex::new(Hash::random(), 100u64);
let msg_id = HexTxHashAndEventIndex::new(H256::repeat_byte(1), 100u64);
PollStarted::VerifierSet {
#[allow(deprecated)] // TODO: The below event uses the deprecated tx_id and event_index fields. Remove this attribute when those fields are removed
verifier_set: VerifierSetConfirmation {
Expand All @@ -294,30 +293,4 @@ mod tests {
},
}
}

fn to_event(event: impl Into<cosmwasm_std::Event>, contract_address: &TMAddress) -> Event {
let mut event: cosmwasm_std::Event = event.into();

event.ty = format!("wasm-{}", event.ty);
event = event.add_attribute("_contract_address", contract_address.to_string());

abci::Event::new(
event.ty,
event
.attributes
.into_iter()
.map(|cosmwasm_std::Attribute { key, value }| {
(STANDARD.encode(key), STANDARD.encode(value))
}),
)
.try_into()
.unwrap()
}

fn participants(n: u8, verifier: Option<TMAddress>) -> Vec<TMAddress> {
(0..n)
.map(|_| TMAddress::random(PREFIX))
.chain(verifier)
.collect()
}
}
7 changes: 6 additions & 1 deletion ampd/src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod tests {

use base64::engine::general_purpose::STANDARD;
use base64::Engine;
use cosmrs::AccountId;
use events::Event;
use tendermint::abci;

Expand Down Expand Up @@ -47,7 +48,11 @@ mod tests {

pub fn participants(n: u8, verifier: Option<TMAddress>) -> Vec<TMAddress> {
(0..n)
.map(|_| TMAddress::random(PREFIX))
.map(|i| {
AccountId::new(PREFIX, &[i; AccountId::MAX_LENGTH])
.unwrap()
.into()
})
.chain(verifier)
.collect()
}
Expand Down
28 changes: 10 additions & 18 deletions ampd/src/handlers/mvx_verify_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,30 +152,29 @@ mod tests {
use cosmrs::cosmwasm::MsgExecuteContract;
use cosmrs::tx::Msg;
use cosmwasm_std;
use error_stack::Result;
use ethers_core::types::H160;
use hex::ToHex;
use tokio::sync::watch;
use tokio::test as async_test;
use voting_verifier::events::{PollMetadata, PollStarted, TxEventConfirmation};

use super::PollStartedEvent;
use crate::event_processor::EventHandler;
use crate::handlers::tests::into_structured_event;
use crate::handlers::tests::{into_structured_event, participants};
use crate::mvx::proxy::MockMvxProxy;
use crate::types::{EVMAddress, Hash, TMAddress};
use crate::types::TMAddress;
use crate::PREFIX;

#[test]
fn should_deserialize_poll_started_event() {
let event: Result<PollStartedEvent, events::Error> = into_structured_event(
fn mvx_verify_msg_should_deserialize_correct_event() {
let event: PollStartedEvent = into_structured_event(
poll_started_event(participants(5, None)),
&TMAddress::random(PREFIX),
)
.try_into();
.try_into()
.unwrap();

assert!(event.is_ok());

let event = event.unwrap();
goldie::assert_debug!(&event);

assert!(event.poll_id == 100u64.into());
assert!(
Expand Down Expand Up @@ -331,16 +330,9 @@ mod tests {
.parse()
.unwrap(),
destination_chain: "ethereum".parse().unwrap(),
destination_address: format!("0x{:x}", EVMAddress::random()).parse().unwrap(),
payload_hash: Hash::random().to_fixed_bytes(),
destination_address: format!("0x{:x}", H160::repeat_byte(2)).parse().unwrap(),
payload_hash: [1;32],
}],
}
}

fn participants(n: u8, worker: Option<TMAddress>) -> Vec<TMAddress> {
(0..n)
.map(|_| TMAddress::random(PREFIX))
.chain(worker)
.collect()
}
}
14 changes: 4 additions & 10 deletions ampd/src/handlers/mvx_verify_verifier_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,19 +168,21 @@ mod tests {

use super::PollStartedEvent;
use crate::event_processor::EventHandler;
use crate::handlers::tests::into_structured_event;
use crate::handlers::tests::{into_structured_event, participants};
use crate::mvx::proxy::MockMvxProxy;
use crate::types::TMAddress;
use crate::PREFIX;

#[test]
fn should_deserialize_verifier_set_poll_started_event() {
fn mvx_verify_verifier_set_should_deserialize_correct_event() {
let event: PollStartedEvent = assert_ok!(into_structured_event(
verifier_set_poll_started_event(participants(5, None), 100),
&TMAddress::random(PREFIX),
)
.try_into());

goldie::assert_debug!(&event);

assert!(event.poll_id == 100u64.into());
assert!(
event.source_gateway_address.to_bech32_string().unwrap()
Expand All @@ -196,7 +198,6 @@ mod tests {
assert!(verifier_set.event_index == 1u32);
assert!(verifier_set.verifier_set.signers.len() == 3);
assert_eq!(verifier_set.verifier_set.threshold, Uint128::from(2u128));
goldie::assert_json!(verifier_set.verifier_set.signers);
}

#[async_test]
Expand Down Expand Up @@ -338,11 +339,4 @@ mod tests {
},
}
}

fn participants(n: u8, worker: Option<TMAddress>) -> Vec<TMAddress> {
(0..n)
.map(|_| TMAddress::random(PREFIX))
.chain(worker)
.collect()
}
}
Loading

0 comments on commit 0ede89e

Please sign in to comment.