Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add only tx hash message id #528

Merged
merged 7 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
10 changes: 8 additions & 2 deletions contracts/voting-verifier/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ pub fn migrate(
mod test {
use axelar_wasm_std::address::AddressFormat;
use axelar_wasm_std::msg_id::{
Base58SolanaTxSignatureAndEventIndex, Base58TxDigestAndEventIndex, HexTxHashAndEventIndex,
MessageIdFormat,
Base58SolanaTxSignatureAndEventIndex, Base58TxDigestAndEventIndex, HexTxHash,
HexTxHashAndEventIndex, MessageIdFormat,
};
use axelar_wasm_std::voting::Vote;
use axelar_wasm_std::{
Expand Down Expand Up @@ -244,6 +244,12 @@ mod test {
.parse()
.unwrap()
}
MessageIdFormat::HexTxHash => HexTxHash {
tx_hash: Keccak256::digest(id.as_bytes()).into(),
}
.to_string()
.parse()
.unwrap(),
}
}

Expand Down
29 changes: 25 additions & 4 deletions contracts/voting-verifier/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::str::FromStr;
use std::vec::Vec;

use axelar_wasm_std::msg_id::{
Base58SolanaTxSignatureAndEventIndex, Base58TxDigestAndEventIndex, HexTxHashAndEventIndex,
MessageIdFormat,
Base58SolanaTxSignatureAndEventIndex, Base58TxDigestAndEventIndex, HexTxHash,
HexTxHashAndEventIndex, MessageIdFormat,
};
use axelar_wasm_std::voting::{PollId, Vote};
use axelar_wasm_std::{nonempty, VerificationStatus};
Expand Down Expand Up @@ -164,6 +164,12 @@ fn parse_message_id(

Ok((id.signature_as_base58(), id.event_index))
}
MessageIdFormat::HexTxHash => {
let id = HexTxHash::from_str(&message_id)
.map_err(|_| ContractError::InvalidMessageID(message_id.into()))?;

Ok((id.tx_hash_as_hex(), 0))
}
}
}

Expand Down Expand Up @@ -286,7 +292,7 @@ mod test {
use std::collections::BTreeMap;

use axelar_wasm_std::msg_id::{
Base58TxDigestAndEventIndex, HexTxHashAndEventIndex, MessageIdFormat,
Base58TxDigestAndEventIndex, HexTxHash, HexTxHashAndEventIndex, MessageIdFormat,
};
use axelar_wasm_std::nonempty;
use cosmwasm_std::Uint128;
Expand Down Expand Up @@ -321,7 +327,7 @@ mod test {
}

#[test]
fn should_make_tx_event_confirmation_with_hex_msg_id() {
fn should_make_tx_event_confirmation_with_hex_event_index_msg_id() {
let msg_id = HexTxHashAndEventIndex {
tx_hash: random_32_bytes(),
event_index: 0,
Expand All @@ -337,6 +343,21 @@ mod test {
compare_event_to_message(event, msg);
}

#[test]
fn should_make_tx_event_confirmation_with_hex_msg_id() {
let msg_id = HexTxHash {
tx_hash: random_32_bytes(),
};
let msg = generate_msg(msg_id.to_string().parse().unwrap());

let event =
TxEventConfirmation::try_from((msg.clone(), &MessageIdFormat::HexTxHash)).unwrap();

assert_eq!(event.tx_id, msg_id.tx_hash_as_hex());
assert_eq!(event.event_index, 0);
compare_event_to_message(event, msg);
}

#[test]
fn should_make_tx_event_confirmation_with_base58_msg_id() {
let msg_id = Base58TxDigestAndEventIndex {
Expand Down
4 changes: 4 additions & 0 deletions packages/axelar-wasm-std/src/msg_id/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ use error_stack::Report;

pub use self::base_58_event_index::Base58TxDigestAndEventIndex;
pub use self::base_58_solana_event_index::Base58SolanaTxSignatureAndEventIndex;
pub use self::tx_hash::HexTxHash;
pub use self::tx_hash_event_index::HexTxHashAndEventIndex;

mod base_58_event_index;
mod base_58_solana_event_index;
mod tx_hash;
mod tx_hash_event_index;

#[derive(thiserror::Error)]
Expand Down Expand Up @@ -42,6 +44,7 @@ pub enum MessageIdFormat {
HexTxHashAndEventIndex,
Base58TxDigestAndEventIndex,
Base58SolanaTxSignatureAndEventIndex,
HexTxHash,
}

// function the router calls to verify msg ids
Expand All @@ -56,6 +59,7 @@ pub fn verify_msg_id(message_id: &str, format: &MessageIdFormat) -> Result<(), R
MessageIdFormat::Base58SolanaTxSignatureAndEventIndex => {
Base58SolanaTxSignatureAndEventIndex::from_str(message_id).map(|_| ())
}
MessageIdFormat::HexTxHash => HexTxHash::from_str(message_id).map(|_| ()),
}
}

Expand Down
138 changes: 138 additions & 0 deletions packages/axelar-wasm-std/src/msg_id/tx_hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
use core::fmt;
use std::fmt::Display;
use std::str::FromStr;

use cosmwasm_std::HexBinary;
use error_stack::{ensure, Report, ResultExt};
use lazy_static::lazy_static;
use regex::Regex;

use super::Error;
use crate::hash::Hash;
use crate::nonempty;

pub struct HexTxHash {
pub tx_hash: Hash,
}

impl HexTxHash {
pub fn tx_hash_as_hex(&self) -> nonempty::String {
HexBinary::from(self.tx_hash)
.to_hex()
.try_into()
.expect("failed to convert tx hash to non-empty string")
}

pub fn new(tx_id: impl Into<[u8; 32]>) -> Self {
Self {
tx_hash: tx_id.into(),
}
}
}

const PATTERN: &str = "^[0-9a-f]{64}$";
lazy_static! {
static ref REGEX: Regex = Regex::new(PATTERN).expect("invalid regex");
}

impl FromStr for HexTxHash {
type Err = Report<Error>;

fn from_str(message_id: &str) -> Result<Self, Self::Err>
where
Self: Sized,
{
// the PATTERN has exactly two capture groups, so the groups can be extracted safely
ensure!(
REGEX.is_match(message_id),
Error::InvalidMessageID {
id: message_id.to_string(),
expected_format: PATTERN.to_string(),
}
);
Ok(HexTxHash {
tx_hash: HexBinary::from_hex(message_id)
.change_context(Error::InvalidTxHash(message_id.to_string()))?
.as_slice()
.try_into()
.map_err(|_| Error::InvalidTxHash(message_id.to_string()))?,
})
}
}

impl Display for HexTxHash {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", HexBinary::from(self.tx_hash).to_hex(),)
}
}

#[cfg(test)]
mod tests {

use super::*;

fn random_hash() -> String {
let mut bytes = vec![];
for _ in 0..32 {
let byte: u8 = rand::random();
bytes.push(byte)
}
HexBinary::from(bytes).to_hex()
}

#[test]
fn should_parse_msg_id() {
let res =
HexTxHash::from_str("7cedbb3799cd99636045c84c5c55aef8a138f107ac8ba53a08cad1070ba4385b");
assert!(res.is_ok());

for _ in 0..1000 {
let msg_id = random_hash();

let res = HexTxHash::from_str(&msg_id);
let parsed = res.unwrap();
assert_eq!(parsed.tx_hash_as_hex(), msg_id.clone().try_into().unwrap());
assert_eq!(parsed.to_string(), msg_id);
}
}

#[test]
fn should_not_parse_msg_id_with_wrong_length_tx_hash() {
let tx_hash = random_hash();
// too long
let res = HexTxHash::from_str(&format!("{}ff", tx_hash));
assert!(res.is_err());

// too short
let res = HexTxHash::from_str(&tx_hash[..tx_hash.len() - 2].to_string());

Check failure on line 107 in packages/axelar-wasm-std/src/msg_id/tx_hash.rs

View workflow job for this annotation

GitHub Actions / Lints

unnecessary use of `to_string`
assert!(res.is_err());
}

#[test]
fn should_not_parse_msg_id_with_uppercase_tx_hash() {
let tx_hash = &random_hash();
let res = HexTxHash::from_str(&tx_hash.to_uppercase());
assert!(res.is_err());
}

#[test]
fn should_not_parse_msg_id_with_non_hex_tx_hash() {
let msg_id = "82GKYvWv5EKm7jnYksHoh3u5M2RxHN2boPreM8Df4ej9";
let res = HexTxHash::from_str(msg_id);
assert!(res.is_err());
}

#[test]
fn should_not_parse_msg_id_with_0x_prefix() {
let msg_id = "0x7cedbb3799cd99636045c84c5c55aef8a138f107ac8ba53a08cad1070ba4385b-1";
let res = HexTxHash::from_str(msg_id);
assert!(res.is_err());
}

#[test]
fn should_not_parse_msg_id_with_event_index() {
let tx_hash = random_hash();
let res = HexTxHash::from_str(&format!("{}-1", tx_hash));
assert!(res.is_err());
}
}
Loading