Skip to content

Commit

Permalink
Merge branch 'stacks_ampd_v2' into stacks_contracts_v2
Browse files Browse the repository at this point in the history
  • Loading branch information
raress96 committed Jan 27, 2025
2 parents a90047a + 017a077 commit 49fc49a
Show file tree
Hide file tree
Showing 9 changed files with 394 additions and 155 deletions.
42 changes: 26 additions & 16 deletions ampd/src/handlers/stacks_verify_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use voting_verifier::msg::ExecuteMsg;

use crate::event_processor::EventHandler;
use crate::handlers::errors::Error;
use crate::stacks::finalizer::latest_finalized_block_height;
use crate::stacks::http_client::Client;
use crate::stacks::verifier::verify_message;
use crate::types::{Hash, TMAddress};
Expand All @@ -41,6 +42,7 @@ struct PollStartedEvent {
poll_id: PollId,
source_chain: ChainName,
source_gateway_address: String,
confirmation_height: u64,
messages: Vec<Message>,
participants: Vec<TMAddress>,
expires_at: u64,
Expand Down Expand Up @@ -92,6 +94,7 @@ impl EventHandler for Handler {
poll_id,
source_chain,
source_gateway_address,
confirmation_height,
messages,
participants,
expires_at,
Expand All @@ -114,11 +117,19 @@ impl EventHandler for Handler {
return Ok(vec![]);
}

let latest_finalized_block_height =
latest_finalized_block_height(&self.http_client, confirmation_height)
.await
.change_context(Error::Finalizer)?;

let tx_hashes: HashSet<Hash> = messages
.iter()
.map(|message| message.message_id.tx_hash.into())
.collect();
let transactions = self.http_client.get_transactions(tx_hashes).await;
let transactions = self
.http_client
.get_finalized_transactions(tx_hashes, latest_finalized_block_height)
.await;

let message_ids = messages
.iter()
Expand Down Expand Up @@ -170,19 +181,20 @@ mod tests {
use cosmrs::tx::Msg;
use cosmwasm_std;
use error_stack::Result;
use ethers_core::types::{H160, U64};
use tokio::sync::watch;
use tokio::test as async_test;
use voting_verifier::events::{PollMetadata, PollStarted, TxEventConfirmation};

use super::{Handler, Message, PollStartedEvent};
use crate::event_processor::EventHandler;
use crate::handlers::tests::into_structured_event;
use crate::stacks::http_client::Client;
use crate::handlers::tests::{into_structured_event, participants};
use crate::stacks::http_client::{Block, Client};
use crate::types::{EVMAddress, Hash, TMAddress};
use crate::PREFIX;

#[test]
fn should_deserialize_poll_started_event() {
fn stacks_should_deserialize_poll_started_event() {
let event: Result<PollStartedEvent, events::Error> = into_structured_event(
poll_started_event(participants(5, None)),
&TMAddress::random(PREFIX),
Expand All @@ -193,11 +205,14 @@ mod tests {

let event = event.unwrap();

goldie::assert_debug!(&event);

assert!(event.poll_id == 100u64.into());
assert!(
event.source_gateway_address
== "SP2N959SER36FZ5QT1CX9BR63W3E8X35WQCMBYYWC.axelar-gateway"
);
assert!(event.confirmation_height == 15);

let message: &Message = event.messages.first().unwrap();

Expand Down Expand Up @@ -256,7 +271,8 @@ mod tests {
#[async_test]
async fn should_vote_correctly() {
let mut client = Client::faux();
faux::when!(client.get_transactions).then(|_| HashMap::new());
faux::when!(client.get_latest_block).then(|_| Ok(Block { height: 1 }));
faux::when!(client.get_finalized_transactions).then(|_| HashMap::new());

let voting_verifier = TMAddress::random(PREFIX);
let worker = TMAddress::random(PREFIX);
Expand All @@ -277,7 +293,8 @@ mod tests {
#[async_test]
async fn should_skip_expired_poll() {
let mut client = Client::faux();
faux::when!(client.get_transactions).then(|_| HashMap::new());
faux::when!(client.get_latest_block).then(|_| Ok(Block { height: 1 }));
faux::when!(client.get_finalized_transactions).then(|_| HashMap::new());

let voting_verifier = TMAddress::random(PREFIX);
let worker = TMAddress::random(PREFIX);
Expand Down Expand Up @@ -319,7 +336,7 @@ mod tests {
}

fn poll_started_event(participants: Vec<TMAddress>) -> PollStarted {
let msg_id = HexTxHashAndEventIndex::new(Hash::random(), 1u64);
let msg_id = HexTxHashAndEventIndex::new(Hash::from([3; 32]), 1u64);

PollStarted::Messages {
metadata: PollMetadata {
Expand All @@ -344,16 +361,9 @@ mod tests {
message_id: msg_id.to_string().parse().unwrap(),
source_address: "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM".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()
}
}
39 changes: 25 additions & 14 deletions ampd/src/handlers/stacks_verify_verifier_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ use voting_verifier::msg::ExecuteMsg;

use crate::event_processor::EventHandler;
use crate::handlers::errors::Error;
use crate::stacks::finalizer::latest_finalized_block_height;
use crate::stacks::http_client::Client;
use crate::stacks::verifier::verify_verifier_set;
use crate::types::TMAddress;

type Result<T> = error_stack::Result<T, Error>;

#[derive(Deserialize, Debug)]
pub struct VerifierSetConfirmation {
pub message_id: HexTxHashAndEventIndex,
Expand All @@ -37,6 +40,7 @@ struct PollStartedEvent {
verifier_set: VerifierSetConfirmation,
participants: Vec<TMAddress>,
expires_at: u64,
confirmation_height: u64,
}

pub struct Handler {
Expand Down Expand Up @@ -79,7 +83,7 @@ impl Handler {
impl EventHandler for Handler {
type Err = Error;

async fn handle(&self, event: &Event) -> error_stack::Result<Vec<Any>, Error> {
async fn handle(&self, event: &Event) -> Result<Vec<Any>> {
if !event.is_from_contract(self.voting_verifier_contract.as_ref()) {
return Ok(vec![]);
}
Expand All @@ -90,6 +94,7 @@ impl EventHandler for Handler {
verifier_set,
participants,
expires_at,
confirmation_height,
..
} = match event.try_into() as error_stack::Result<_, _> {
Err(report) if matches!(report.current_context(), EventTypeMismatch(_)) => {
Expand All @@ -108,9 +113,17 @@ impl EventHandler for Handler {
return Ok(vec![]);
}

let latest_finalized_block_height =
latest_finalized_block_height(&self.http_client, confirmation_height)
.await
.change_context(Error::Finalizer)?;

let transaction = self
.http_client
.get_valid_transaction(&verifier_set.message_id.tx_hash.into())
.get_valid_transaction(
&verifier_set.message_id.tx_hash.into(),
latest_finalized_block_height,
)
.await;

let vote = info_span!(
Expand Down Expand Up @@ -157,13 +170,13 @@ mod tests {

use super::PollStartedEvent;
use crate::event_processor::EventHandler;
use crate::handlers::tests::into_structured_event;
use crate::stacks::http_client::Client;
use crate::handlers::tests::{into_structured_event, participants};
use crate::stacks::http_client::{Block, Client};
use crate::types::{Hash, TMAddress};
use crate::PREFIX;

#[test]
fn should_deserialize_verifier_set_poll_started_event() {
fn stacks_should_deserialize_verifier_set_poll_started_event() {
let event: PollStartedEvent = assert_ok!(into_structured_event(
verifier_set_poll_started_event(participants(5, None), 100),
&TMAddress::random(PREFIX),
Expand All @@ -177,6 +190,7 @@ mod tests {
event.source_gateway_address
== "SP2N959SER36FZ5QT1CX9BR63W3E8X35WQCMBYYWC.axelar-gateway"
);
assert!(event.confirmation_height == 15);

let verifier_set = event.verifier_set;

Expand Down Expand Up @@ -264,6 +278,7 @@ mod tests {
#[async_test]
async fn should_skip_expired_poll() {
let mut client = Client::faux();
faux::when!(client.get_latest_block).then(|_| Ok(Block { height: 1 }));
faux::when!(client.get_valid_transaction).then(|_| None);

let voting_verifier = TMAddress::random(PREFIX);
Expand Down Expand Up @@ -294,6 +309,7 @@ mod tests {
#[async_test]
async fn should_vote_correctly() {
let mut client = Client::faux();
faux::when!(client.get_latest_block).then(|_| Ok(Block { height: 1 }));
faux::when!(client.get_valid_transaction).then(|_| None);

let voting_verifier = TMAddress::random(PREFIX);
Expand All @@ -315,7 +331,7 @@ mod tests {
participants: Vec<TMAddress>,
expires_at: u64,
) -> PollStarted {
let msg_id = HexTxHashAndEventIndex::new(Hash::random(), 1u64);
let msg_id = HexTxHashAndEventIndex::new(Hash::from([3; 32]), 1u64);

PollStarted::VerifierSet {
metadata: PollMetadata {
Expand All @@ -331,7 +347,9 @@ mod tests {
.map(|addr| cosmwasm_std::Addr::unchecked(addr.to_string()))
.collect(),
},
#[allow(deprecated)] // TODO: The below events use the deprecated tx_id and event_index fields. Remove this attribute when those fields are removed
#[allow(
deprecated
)] // TODO: The below events use the deprecated tx_id and event_index fields. Remove this attribute when those fields are removed
verifier_set: VerifierSetConfirmation {
tx_id: msg_id.tx_hash_as_hex(),
event_index: u32::try_from(msg_id.event_index).unwrap(),
Expand All @@ -340,11 +358,4 @@ mod tests {
},
}
}

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

This file was deleted.

Loading

0 comments on commit 49fc49a

Please sign in to comment.