Skip to content

Commit

Permalink
feat(minor-voting-verifier)!: use transaction log index instead of bl…
Browse files Browse the repository at this point in the history
…ock log index to identify EVM events (#314)

* feat(minor-voting-verifier)!: use transaction log index instead of block log index to identify EVM events

* Improve comments

Co-authored-by: Christian Gorenflo <[email protected]>

* replace cast with num_traits

---------

Co-authored-by: Christian Gorenflo <[email protected]>
  • Loading branch information
fish-sammy and cgorenflo authored Mar 18, 2024
1 parent 1b76958 commit 00ac62f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 14 deletions.
16 changes: 8 additions & 8 deletions ampd/src/evm/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use ethers::abi::{encode, Token};
use ethers::contract::EthLogDecode;
use ethers::prelude::abigen;
use ethers::types::{Log, TransactionReceipt, H256};
use num_traits::cast;

use crate::handlers::evm_verify_msg::Message;
use crate::handlers::evm_verify_worker_set::WorkerSetConfirmation;
Expand All @@ -19,7 +20,6 @@ impl PartialEq<IAxelarGatewayEventsWithLog<'_>> for &Message {
match event {
IAxelarGatewayEvents::ContractCallFilter(event) => {
log.transaction_hash == Some(self.tx_id)
&& log.log_index == Some(self.event_index.into())
&& event.sender == self.source_address
&& self.destination_chain == event.destination_chain
&& event.destination_contract_address == self.destination_address
Expand Down Expand Up @@ -49,7 +49,6 @@ impl PartialEq<IAxelarGatewayEventsWithLog<'_>> for &WorkerSetConfirmation {
.unzip();

log.transaction_hash == Some(self.tx_id)
&& log.log_index == Some(self.event_index.into())
&& event.new_operators_data
== encode(&[
Token::Array(operators),
Expand All @@ -69,12 +68,13 @@ fn has_failed(tx_receipt: &TransactionReceipt) -> bool {
fn get_event<'a>(
gateway_address: &EVMAddress,
tx_receipt: &'a TransactionReceipt,
log_index: u64,
log_index: u32,
) -> Option<IAxelarGatewayEventsWithLog<'a>> {
let log_index: usize = cast(log_index).expect("log_index must be a valid usize");

tx_receipt
.logs
.iter()
.find(|log| log.log_index == Some(log_index.into()))
.get(log_index)
.filter(|log| log.address == *gateway_address)
.and_then(
|log| match IAxelarGatewayEvents::decode_log(&log.clone().into()) {
Expand All @@ -89,7 +89,7 @@ fn verify<'a, V>(
tx_receipt: &'a TransactionReceipt,
to_verify: V,
expected_transaction_hash: H256,
expected_event_index: u64,
expected_event_index: u32,
) -> Vote
where
V: PartialEq<IAxelarGatewayEventsWithLog<'a>>,
Expand Down Expand Up @@ -302,7 +302,7 @@ mod tests {
fn get_matching_worker_set_and_tx_receipt(
) -> (EVMAddress, TransactionReceipt, WorkerSetConfirmation) {
let tx_id = Hash::random();
let log_index = 999;
let log_index = 1;
let gateway_address = EVMAddress::random();

let worker_set = WorkerSetConfirmation {
Expand Down Expand Up @@ -353,7 +353,7 @@ mod tests {

fn get_matching_msg_and_tx_receipt() -> (EVMAddress, TransactionReceipt, Message) {
let tx_id = Hash::random();
let log_index = 999;
let log_index = 1;
let gateway_address = EVMAddress::random();

let msg = Message {
Expand Down
2 changes: 1 addition & 1 deletion ampd/src/handlers/evm_verify_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Result<T> = error_stack::Result<T, Error>;
#[derive(Deserialize, Debug)]
pub struct Message {
pub tx_id: Hash,
pub event_index: u64,
pub event_index: u32,
pub destination_address: String,
pub destination_chain: connection_router_api::ChainName,
pub source_address: EVMAddress,
Expand Down
2 changes: 1 addition & 1 deletion ampd/src/handlers/evm_verify_worker_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct Operators {
#[derive(Deserialize, Debug)]
pub struct WorkerSetConfirmation {
pub tx_id: Hash,
pub event_index: u64,
pub event_index: u32,
pub operators: Operators,
}

Expand Down
2 changes: 2 additions & 0 deletions contracts/nexus-gateway/src/nexus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub struct Message {

impl CustomMsg for Message {}

// it's parsed into u64 instead of u32 (https://github.com/axelarnetwork/axelar-amplifier/blob/bf0b3049c83e540989c7dad1c609c7e2ef6ed2e5/contracts/voting-verifier/src/events.rs#L162)
// here in order to match the message type defined in the nexus module. Changing nexus to use u32 instead is not worth the effort.
fn parse_message_id(message_id: &str) -> Result<(nonempty::Vec<u8>, u64), ContractError> {
// expected format: <tx_id>:<index>
let components = message_id.split(ID_SEPARATOR).collect::<Vec<_>>();
Expand Down
9 changes: 5 additions & 4 deletions contracts/voting-verifier/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,14 @@ impl From<PollStarted> for Event {
#[cw_serde]
pub struct WorkerSetConfirmation {
pub tx_id: nonempty::String,
pub event_index: u64,
pub event_index: u32,
pub operators: Operators,
}

impl WorkerSetConfirmation {
pub fn new(message_id: nonempty::String, operators: Operators) -> Result<Self, ContractError> {
let (tx_id, event_index) = parse_message_id(&message_id)?;

Ok(Self {
tx_id,
event_index,
Expand All @@ -130,7 +131,7 @@ impl WorkerSetConfirmation {
#[cw_serde]
pub struct TxEventConfirmation {
pub tx_id: nonempty::String,
pub event_index: u64,
pub event_index: u32,
pub destination_address: Address,
pub destination_chain: ChainName,
pub source_address: Address,
Expand Down Expand Up @@ -160,7 +161,7 @@ impl TryFrom<Message> for TxEventConfirmation {

fn parse_message_id(
message_id: &nonempty::String,
) -> Result<(nonempty::String, u64), ContractError> {
) -> Result<(nonempty::String, u32), ContractError> {
// expected format: <tx_id>:<index>
let components = message_id.split(ID_SEPARATOR).collect::<Vec<_>>();

Expand All @@ -171,7 +172,7 @@ fn parse_message_id(
Ok((
components[0].try_into()?,
components[1]
.parse::<u64>()
.parse()
.map_err(|_| ContractError::InvalidMessageID(message_id.to_string()))?,
))
}
Expand Down

0 comments on commit 00ac62f

Please sign in to comment.