|
| 1 | +use std::convert::TryInto; |
| 2 | + |
| 3 | +use async_trait::async_trait; |
| 4 | +use axelar_wasm_std::voting::{PollId, Vote}; |
| 5 | +use cosmrs::cosmwasm::MsgExecuteContract; |
| 6 | +use cosmrs::tx::Msg; |
| 7 | +use cosmrs::Any; |
| 8 | +use error_stack::ResultExt; |
| 9 | +use events::Error::EventTypeMismatch; |
| 10 | +use events::Event; |
| 11 | +use events_derive::try_from; |
| 12 | +use multisig::verifier_set::VerifierSet; |
| 13 | +use serde::Deserialize; |
| 14 | +use tokio::sync::watch::Receiver; |
| 15 | +use tracing::{info, info_span}; |
| 16 | +use valuable::Valuable; |
| 17 | +use voting_verifier::msg::ExecuteMsg; |
| 18 | + |
| 19 | +use crate::event_processor::EventHandler; |
| 20 | +use crate::handlers::errors::Error; |
| 21 | +use crate::stacks::http_client::Client; |
| 22 | +use crate::stacks::verifier::verify_verifier_set; |
| 23 | +use crate::types::{Hash, TMAddress}; |
| 24 | + |
| 25 | +#[derive(Deserialize, Debug)] |
| 26 | +pub struct VerifierSetConfirmation { |
| 27 | + pub tx_id: Hash, |
| 28 | + pub event_index: u32, |
| 29 | + pub verifier_set: VerifierSet, |
| 30 | +} |
| 31 | + |
| 32 | +#[derive(Deserialize, Debug)] |
| 33 | +#[try_from("wasm-verifier_set_poll_started")] |
| 34 | +struct PollStartedEvent { |
| 35 | + poll_id: PollId, |
| 36 | + source_gateway_address: String, |
| 37 | + verifier_set: VerifierSetConfirmation, |
| 38 | + participants: Vec<TMAddress>, |
| 39 | + expires_at: u64, |
| 40 | +} |
| 41 | + |
| 42 | +pub struct Handler { |
| 43 | + verifier: TMAddress, |
| 44 | + voting_verifier_contract: TMAddress, |
| 45 | + http_client: Client, |
| 46 | + latest_block_height: Receiver<u64>, |
| 47 | +} |
| 48 | + |
| 49 | +impl Handler { |
| 50 | + pub fn new( |
| 51 | + verifier: TMAddress, |
| 52 | + voting_verifier_contract: TMAddress, |
| 53 | + http_client: Client, |
| 54 | + latest_block_height: Receiver<u64>, |
| 55 | + ) -> Self { |
| 56 | + Self { |
| 57 | + verifier, |
| 58 | + voting_verifier_contract, |
| 59 | + http_client, |
| 60 | + latest_block_height, |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + fn vote_msg(&self, poll_id: PollId, vote: Vote) -> MsgExecuteContract { |
| 65 | + MsgExecuteContract { |
| 66 | + sender: self.verifier.as_ref().clone(), |
| 67 | + contract: self.voting_verifier_contract.as_ref().clone(), |
| 68 | + msg: serde_json::to_vec(&ExecuteMsg::Vote { |
| 69 | + poll_id, |
| 70 | + votes: vec![vote], |
| 71 | + }) |
| 72 | + .expect("vote msg should serialize"), |
| 73 | + funds: vec![], |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +#[async_trait] |
| 79 | +impl EventHandler for Handler { |
| 80 | + type Err = Error; |
| 81 | + |
| 82 | + async fn handle(&self, event: &Event) -> error_stack::Result<Vec<Any>, Error> { |
| 83 | + if !event.is_from_contract(self.voting_verifier_contract.as_ref()) { |
| 84 | + return Ok(vec![]); |
| 85 | + } |
| 86 | + |
| 87 | + let PollStartedEvent { |
| 88 | + poll_id, |
| 89 | + source_gateway_address, |
| 90 | + verifier_set, |
| 91 | + participants, |
| 92 | + expires_at, |
| 93 | + .. |
| 94 | + } = match event.try_into() as error_stack::Result<_, _> { |
| 95 | + Err(report) if matches!(report.current_context(), EventTypeMismatch(_)) => { |
| 96 | + return Ok(vec![]); |
| 97 | + } |
| 98 | + event => event.change_context(Error::DeserializeEvent)?, |
| 99 | + }; |
| 100 | + |
| 101 | + if !participants.contains(&self.verifier) { |
| 102 | + return Ok(vec![]); |
| 103 | + } |
| 104 | + |
| 105 | + let latest_block_height = *self.latest_block_height.borrow(); |
| 106 | + if latest_block_height >= expires_at { |
| 107 | + info!(poll_id = poll_id.to_string(), "skipping expired poll"); |
| 108 | + return Ok(vec![]); |
| 109 | + } |
| 110 | + |
| 111 | + let transaction = self |
| 112 | + .http_client |
| 113 | + .get_valid_transaction(&verifier_set.tx_id) |
| 114 | + .await; |
| 115 | + |
| 116 | + let vote = info_span!( |
| 117 | + "verify a new verifier set for Stacks", |
| 118 | + poll_id = poll_id.to_string(), |
| 119 | + id = format!("{}_{}", verifier_set.tx_id, verifier_set.event_index) |
| 120 | + ) |
| 121 | + .in_scope(|| { |
| 122 | + info!("ready to verify a new worker set in poll"); |
| 123 | + |
| 124 | + let vote = transaction.map_or(Vote::NotFound, |transaction| { |
| 125 | + verify_verifier_set(&source_gateway_address, &transaction, verifier_set) |
| 126 | + }); |
| 127 | + info!( |
| 128 | + vote = vote.as_value(), |
| 129 | + "ready to vote for a new worker set in poll" |
| 130 | + ); |
| 131 | + |
| 132 | + vote |
| 133 | + }); |
| 134 | + |
| 135 | + Ok(vec![self |
| 136 | + .vote_msg(poll_id, vote) |
| 137 | + .into_any() |
| 138 | + .expect("vote msg should serialize")]) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +#[cfg(test)] |
| 143 | +mod tests { |
| 144 | + use std::collections::HashMap; |
| 145 | + use std::convert::TryInto; |
| 146 | + |
| 147 | + use cosmrs::cosmwasm::MsgExecuteContract; |
| 148 | + use cosmrs::tx::Msg; |
| 149 | + use cosmwasm_std; |
| 150 | + use error_stack::Result; |
| 151 | + use multisig::key::KeyType; |
| 152 | + use multisig::test::common::{build_verifier_set, ecdsa_test_data}; |
| 153 | + use tokio::sync::watch; |
| 154 | + use tokio::test as async_test; |
| 155 | + use voting_verifier::events::{ |
| 156 | + PollMetadata, PollStarted, TxEventConfirmation, VerifierSetConfirmation, |
| 157 | + }; |
| 158 | + |
| 159 | + use super::PollStartedEvent; |
| 160 | + use crate::event_processor::EventHandler; |
| 161 | + use crate::handlers::tests::into_structured_event; |
| 162 | + use crate::stacks::http_client::Client; |
| 163 | + use crate::types::{EVMAddress, Hash, TMAddress}; |
| 164 | + use crate::PREFIX; |
| 165 | + |
| 166 | + #[test] |
| 167 | + fn should_deserialize_verifier_set_poll_started_event() { |
| 168 | + let event: Result<PollStartedEvent, events::Error> = into_structured_event( |
| 169 | + verifier_set_poll_started_event(participants(5, None), 100), |
| 170 | + &TMAddress::random(PREFIX), |
| 171 | + ) |
| 172 | + .try_into(); |
| 173 | + |
| 174 | + assert!(event.is_ok()); |
| 175 | + |
| 176 | + let event = event.unwrap(); |
| 177 | + |
| 178 | + assert!(event.poll_id == 100u64.into()); |
| 179 | + assert!( |
| 180 | + event.source_gateway_address |
| 181 | + == "SP2N959SER36FZ5QT1CX9BR63W3E8X35WQCMBYYWC.axelar-gateway" |
| 182 | + ); |
| 183 | + |
| 184 | + let verifier_set = event.verifier_set; |
| 185 | + |
| 186 | + assert!( |
| 187 | + verifier_set.tx_id |
| 188 | + == "0xee0049faf8dde5507418140ed72bd64f73cc001b08de98e0c16a3a8d9f2c38cf" |
| 189 | + .parse() |
| 190 | + .unwrap() |
| 191 | + ); |
| 192 | + assert!(verifier_set.event_index == 1u32); |
| 193 | + assert!(verifier_set.verifier_set.signers.len() == 3); |
| 194 | + assert_eq!(verifier_set.verifier_set.threshold, Uint128::from(2u128)); |
| 195 | + |
| 196 | + let mut signers = verifier_set.verifier_set.signers.values(); |
| 197 | + let signer1 = signers.next().unwrap(); |
| 198 | + let signer2 = signers.next().unwrap(); |
| 199 | + |
| 200 | + assert_eq!(signer1.pub_key.as_ref(), HexBinary::from_hex( |
| 201 | + "45e67eaf446e6c26eb3a2b55b64339ecf3a4d1d03180bee20eb5afdd23fa644f", |
| 202 | + ) |
| 203 | + .unwrap().as_ref()); |
| 204 | + assert_eq!(signer1.weight, Uint128::from(1u128)); |
| 205 | + |
| 206 | + assert_eq!(signer2.pub_key.as_ref(), HexBinary::from_hex( |
| 207 | + "dd9822c7fa239dda9913ebee813ecbe69e35d88ff651548d5cc42c033a8a667b", |
| 208 | + ) |
| 209 | + .unwrap().as_ref()); |
| 210 | + assert_eq!(signer2.weight, Uint128::from(1u128)); |
| 211 | + } |
| 212 | + |
| 213 | + fn verifier_set_poll_started_event( |
| 214 | + participants: Vec<TMAddress>, |
| 215 | + expires_at: u64, |
| 216 | + ) -> PollStarted { |
| 217 | + PollStarted::VerifierSet { |
| 218 | + metadata: PollMetadata { |
| 219 | + poll_id: "100".parse().unwrap(), |
| 220 | + source_chain: "multiversx".parse().unwrap(), |
| 221 | + source_gateway_address: "SP2N959SER36FZ5QT1CX9BR63W3E8X35WQCMBYYWC.axelar-gateway" |
| 222 | + .parse() |
| 223 | + .unwrap(), |
| 224 | + confirmation_height: 15, |
| 225 | + expires_at, |
| 226 | + participants: participants |
| 227 | + .into_iter() |
| 228 | + .map(|addr| cosmwasm_std::Addr::unchecked(addr.to_string())) |
| 229 | + .collect(), |
| 230 | + }, |
| 231 | + verifier_set: VerifierSetConfirmation { |
| 232 | + tx_id: "0xee0049faf8dde5507418140ed72bd64f73cc001b08de98e0c16a3a8d9f2c38cf" |
| 233 | + .parse() |
| 234 | + .unwrap(), |
| 235 | + event_index: 1, |
| 236 | + verifier_set: build_verifier_set(KeyType::Ecdsa, &ecdsa_test_data::signers()), |
| 237 | + }, |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + fn participants(n: u8, worker: Option<TMAddress>) -> Vec<TMAddress> { |
| 242 | + (0..n) |
| 243 | + .map(|_| TMAddress::random(PREFIX)) |
| 244 | + .chain(worker) |
| 245 | + .collect() |
| 246 | + } |
| 247 | +} |
0 commit comments