Skip to content

Commit

Permalink
feat(minor-router)!: change u32 to u64 in message ids
Browse files Browse the repository at this point in the history
* This change also affects axelarnet-gateway, voting-verifier, and ampd
* In some cases, ampd will automatically vote NotFound if the u64 overflows
  u32 (since some RPC interfaces do not allow indexing events via u64)
* Emit message id in poll started event. Ampd parses message id.
* Deprecate emitting tx id and event index in poll started event.
  • Loading branch information
cjcobb23 committed Oct 24, 2024
1 parent 7decfc9 commit 5b4540b
Show file tree
Hide file tree
Showing 23 changed files with 620 additions and 543 deletions.
15 changes: 8 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 28 additions & 21 deletions ampd/src/evm/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use axelar_wasm_std::voting::Vote;
use ethers_contract::EthLogDecode;
use ethers_core::types::{Log, TransactionReceipt, H256};
use evm_gateway::{IAxelarAmplifierGatewayEvents, WeightedSigners};
use num_traits::cast;

use crate::handlers::evm_verify_msg::Message;
use crate::handlers::evm_verify_verifier_set::VerifierSetConfirmation;
Expand All @@ -16,7 +15,7 @@ impl PartialEq<IAxelarGatewayEventsWithLog<'_>> for &Message {

match event {
IAxelarAmplifierGatewayEvents::ContractCallFilter(event) => {
log.transaction_hash == Some(self.tx_id)
log.transaction_hash == Some(self.message_id.tx_hash.into())
&& event.sender == self.source_address
&& self.destination_chain == event.destination_chain
&& event.destination_contract_address == self.destination_address
Expand All @@ -37,7 +36,7 @@ impl PartialEq<IAxelarGatewayEventsWithLog<'_>> for &VerifierSetConfirmation {
Err(_) => return false,
};

log.transaction_hash == Some(self.tx_id)
log.transaction_hash == Some(self.message_id.tx_hash.into())
&& event.signers_hash == weighted_signers.hash()
&& event.signers == weighted_signers.abi_encode()
}
Expand All @@ -53,9 +52,9 @@ fn has_failed(tx_receipt: &TransactionReceipt) -> bool {
fn event<'a>(
gateway_address: &EVMAddress,
tx_receipt: &'a TransactionReceipt,
log_index: u32,
log_index: u64,
) -> Option<IAxelarGatewayEventsWithLog<'a>> {
let log_index: usize = cast(log_index).expect("log_index must be a valid usize");
let log_index: usize = usize::try_from(log_index).ok()?;

tx_receipt
.logs
Expand All @@ -74,7 +73,7 @@ fn verify<'a, V>(
tx_receipt: &'a TransactionReceipt,
to_verify: V,
expected_transaction_hash: H256,
expected_event_index: u32,
expected_event_index: u64,
) -> Vote
where
V: PartialEq<IAxelarGatewayEventsWithLog<'a>>,
Expand All @@ -98,7 +97,13 @@ pub fn verify_message(
tx_receipt: &TransactionReceipt,
msg: &Message,
) -> Vote {
verify(gateway_address, tx_receipt, msg, msg.tx_id, msg.event_index)
verify(
gateway_address,
tx_receipt,
msg,
msg.message_id.tx_hash.into(),
msg.message_id.event_index,
)
}

pub fn verify_verifier_set(
Expand All @@ -110,13 +115,14 @@ pub fn verify_verifier_set(
gateway_address,
tx_receipt,
confirmation,
confirmation.tx_id,
confirmation.event_index,
confirmation.message_id.tx_hash.into(),
confirmation.message_id.event_index,
)
}

#[cfg(test)]
mod tests {
use axelar_wasm_std::msg_id::HexTxHashAndEventIndex;
use axelar_wasm_std::voting::Vote;
use cosmwasm_std::Uint128;
use ethers_contract::EthEvent;
Expand All @@ -137,7 +143,7 @@ mod tests {
let (gateway_address, tx_receipt, mut verifier_set) =
matching_verifier_set_and_tx_receipt();

verifier_set.tx_id = Hash::random();
verifier_set.message_id.tx_hash = Hash::random().into();
assert_eq!(
verify_verifier_set(&gateway_address, &tx_receipt, &verifier_set),
Vote::NotFound
Expand Down Expand Up @@ -172,17 +178,17 @@ mod tests {
let (gateway_address, tx_receipt, mut verifier_set) =
matching_verifier_set_and_tx_receipt();

verifier_set.event_index = 0;
verifier_set.message_id.event_index = 0;
assert_eq!(
verify_verifier_set(&gateway_address, &tx_receipt, &verifier_set),
Vote::NotFound
);
verifier_set.event_index = 2;
verifier_set.message_id.event_index = 2;
assert_eq!(
verify_verifier_set(&gateway_address, &tx_receipt, &verifier_set),
Vote::NotFound
);
verifier_set.event_index = 3;
verifier_set.message_id.event_index = 3;
assert_eq!(
verify_verifier_set(&gateway_address, &tx_receipt, &verifier_set),
Vote::NotFound
Expand Down Expand Up @@ -215,7 +221,7 @@ mod tests {
fn should_not_verify_msg_if_tx_id_does_not_match() {
let (gateway_address, tx_receipt, mut msg) = matching_msg_and_tx_receipt();

msg.tx_id = Hash::random();
msg.message_id.tx_hash = Hash::random().into();
assert_eq!(
verify_message(&gateway_address, &tx_receipt, &msg),
Vote::NotFound
Expand Down Expand Up @@ -248,17 +254,17 @@ mod tests {
fn should_not_verify_msg_if_log_index_does_not_match() {
let (gateway_address, tx_receipt, mut msg) = matching_msg_and_tx_receipt();

msg.event_index = 0;
msg.message_id.event_index = 0;
assert_eq!(
verify_message(&gateway_address, &tx_receipt, &msg),
Vote::NotFound
);
msg.event_index = 2;
msg.message_id.event_index = 2;
assert_eq!(
verify_message(&gateway_address, &tx_receipt, &msg),
Vote::NotFound
);
msg.event_index = 3;
msg.message_id.event_index = 3;
assert_eq!(
verify_message(&gateway_address, &tx_receipt, &msg),
Vote::NotFound
Expand Down Expand Up @@ -295,8 +301,7 @@ mod tests {
let verifier_set = build_verifier_set(KeyType::Ecdsa, &ecdsa_test_data::signers());

let verifier_set = VerifierSetConfirmation {
tx_id,
event_index: log_index,
message_id: HexTxHashAndEventIndex::new(tx_id, log_index as u64),
verifier_set,
};

Expand Down Expand Up @@ -331,8 +336,10 @@ mod tests {
let gateway_address = EVMAddress::random();

let msg = Message {
tx_id,
event_index: log_index,
message_id: HexTxHashAndEventIndex::new(tx_id, log_index as u64)
.to_string()
.parse()
.unwrap(),
source_address: "0xd48e199950589a4336e4dc43bd2c72ba0c0baa86"
.parse()
.unwrap(),
Expand Down
2 changes: 2 additions & 0 deletions ampd/src/handlers/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ pub enum Error {
TxReceipts,
#[error("unsupported key type {0}")]
KeyType(String),
#[error("failed to parse message ids")]
MessageIds,
}
47 changes: 29 additions & 18 deletions ampd/src/handlers/evm_verify_msg.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::{HashMap, HashSet};
use std::convert::TryInto;
use std::str::FromStr;

use async_trait::async_trait;
use axelar_wasm_std::msg_id::HexTxHashAndEventIndex;
Expand All @@ -12,6 +13,7 @@ use ethers_core::types::{TransactionReceipt, U64};
use events::Error::EventTypeMismatch;
use events_derive::try_from;
use futures::future::join_all;
use itertools::Itertools;
use router_api::ChainName;
use serde::Deserialize;
use tokio::sync::watch::Receiver;
Expand All @@ -32,8 +34,7 @@ type Result<T> = error_stack::Result<T, Error>;

#[derive(Deserialize, Debug)]
pub struct Message {
pub tx_id: Hash,
pub event_index: u32,
pub message_id: HexTxHashAndEventIndex,
pub destination_address: String,
pub destination_chain: ChainName,
pub source_address: EVMAddress,
Expand Down Expand Up @@ -121,7 +122,6 @@ where
})
.collect())
}

fn vote_msg(&self, poll_id: PollId, votes: Vec<Vote>) -> MsgExecuteContract {
MsgExecuteContract {
sender: self.verifier.as_ref().clone(),
Expand Down Expand Up @@ -174,24 +174,26 @@ where
return Ok(vec![]);
}

let tx_hashes: HashSet<_> = messages.iter().map(|message| message.tx_id).collect();
let tx_hashes: HashSet<Hash> = messages
.iter()
.map(|msg| Hash::from_str(&msg.message_id.tx_hash_as_hex().to_string()))
.try_collect()
.change_context(Error::MessageIds)?;
let finalized_tx_receipts = self
.finalized_tx_receipts(tx_hashes, confirmation_height)
.await?;

let poll_id_str: String = poll_id.into();
let source_chain_str: String = source_chain.into();
let message_ids = messages
.iter()
.map(|message| {
HexTxHashAndEventIndex::new(message.tx_id, message.event_index).to_string()
})
.collect::<Vec<_>>();
let votes = info_span!(
"verify messages from an EVM chain",
poll_id = poll_id_str,
source_chain = source_chain_str,
message_ids = message_ids.as_value()
message_ids = messages
.iter()
.map(|msg| msg.message_id.to_string())
.collect::<Vec<String>>()
.as_value(),
)
.in_scope(|| {
info!("ready to verify messages in poll",);
Expand All @@ -200,7 +202,7 @@ where
.iter()
.map(|msg| {
finalized_tx_receipts
.get(&msg.tx_id)
.get(&msg.message_id.tx_hash.into())
.map_or(Vote::NotFound, |tx_receipt| {
verify_message(&source_gateway_address, tx_receipt, msg)
})
Expand All @@ -226,6 +228,7 @@ mod tests {
use std::convert::TryInto;
use std::str::FromStr;

use axelar_wasm_std::msg_id::HexTxHashAndEventIndex;
use base64::engine::general_purpose::STANDARD;
use base64::Engine;
use cosmwasm_std;
Expand All @@ -247,6 +250,11 @@ mod tests {
use crate::PREFIX;

fn poll_started_event(participants: Vec<TMAddress>, expires_at: u64) -> PollStarted {
let msg_ids = vec![
HexTxHashAndEventIndex::new(Hash::random(), 0u64),
HexTxHashAndEventIndex::new(Hash::random(), 1u64),
HexTxHashAndEventIndex::new(Hash::random(), 10u64),
];
PollStarted::Messages {
metadata: PollMetadata {
poll_id: "100".parse().unwrap(),
Expand All @@ -263,24 +271,27 @@ mod tests {
},
messages: vec![
TxEventConfirmation {
tx_id: format!("0x{:x}", Hash::random()).parse().unwrap(),
event_index: 0,
tx_id: msg_ids[0].tx_hash_as_hex(),
event_index: msg_ids[0].event_index as u32,
message_id: msg_ids[0].to_string().parse().unwrap(),
source_address: format!("0x{:x}", EVMAddress::random()).parse().unwrap(),
destination_chain: "ethereum".parse().unwrap(),
destination_address: format!("0x{:x}", EVMAddress::random()).parse().unwrap(),
payload_hash: Hash::random().to_fixed_bytes(),
},
TxEventConfirmation {
tx_id: format!("0x{:x}", Hash::random()).parse().unwrap(),
event_index: 1,
tx_id: msg_ids[1].tx_hash_as_hex(),
event_index: msg_ids[1].event_index as u32,
message_id: msg_ids[1].to_string().parse().unwrap(),
source_address: format!("0x{:x}", EVMAddress::random()).parse().unwrap(),
destination_chain: "ethereum".parse().unwrap(),
destination_address: format!("0x{:x}", EVMAddress::random()).parse().unwrap(),
payload_hash: Hash::random().to_fixed_bytes(),
},
TxEventConfirmation {
tx_id: format!("0x{:x}", Hash::random()).parse().unwrap(),
event_index: 10,
tx_id: msg_ids[2].tx_hash_as_hex(),
event_index: msg_ids[2].event_index as u32,
message_id: msg_ids[2].to_string().parse().unwrap(),
source_address: format!("0x{:x}", EVMAddress::random()).parse().unwrap(),
destination_chain: "ethereum".parse().unwrap(),
destination_address: format!("0x{:x}", EVMAddress::random()).parse().unwrap(),
Expand Down
Loading

0 comments on commit 5b4540b

Please sign in to comment.