-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathstellar_verify_msg.rs
341 lines (301 loc) · 10.9 KB
/
stellar_verify_msg.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
use std::collections::HashSet;
use std::convert::TryInto;
use async_trait::async_trait;
use axelar_wasm_std::msg_id::HexTxHashAndEventIndex;
use axelar_wasm_std::voting::{PollId, Vote};
use cosmrs::cosmwasm::MsgExecuteContract;
use cosmrs::tx::Msg;
use error_stack::ResultExt;
use events::Error::EventTypeMismatch;
use events::Event;
use events_derive::try_from;
use prost_types::Any;
use router_api::ChainName;
use serde::Deserialize;
use serde_with::{serde_as, DisplayFromStr};
use stellar_xdr::curr::{ScAddress, ScBytes, ScString};
use tokio::sync::watch::Receiver;
use tracing::{info, info_span};
use valuable::Valuable;
use voting_verifier::msg::ExecuteMsg;
use crate::event_processor::EventHandler;
use crate::handlers::errors::Error;
use crate::handlers::errors::Error::DeserializeEvent;
use crate::stellar::rpc_client::Client;
use crate::stellar::verifier::verify_message;
use crate::types::TMAddress;
#[serde_as]
#[derive(Deserialize, Debug, Clone)]
pub struct Message {
pub message_id: HexTxHashAndEventIndex,
pub destination_address: ScString,
pub destination_chain: ScString,
#[serde_as(as = "DisplayFromStr")]
pub source_address: ScAddress,
pub payload_hash: ScBytes,
}
#[serde_as]
#[derive(Deserialize, Debug)]
#[try_from("wasm-messages_poll_started")]
struct PollStartedEvent {
poll_id: PollId,
source_chain: ChainName,
#[serde_as(as = "DisplayFromStr")]
source_gateway_address: ScAddress,
expires_at: u64,
messages: Vec<Message>,
participants: Vec<TMAddress>,
}
pub struct Handler {
verifier: TMAddress,
voting_verifier_contract: TMAddress,
http_client: Client,
latest_block_height: Receiver<u64>,
}
impl Handler {
pub fn new(
verifier: TMAddress,
voting_verifier_contract: TMAddress,
http_client: Client,
latest_block_height: Receiver<u64>,
) -> Self {
Self {
verifier,
voting_verifier_contract,
http_client,
latest_block_height,
}
}
fn vote_msg(&self, poll_id: PollId, votes: Vec<Vote>) -> MsgExecuteContract {
MsgExecuteContract {
sender: self.verifier.as_ref().clone(),
contract: self.voting_verifier_contract.as_ref().clone(),
msg: serde_json::to_vec(&ExecuteMsg::Vote { poll_id, votes })
.expect("vote msg should serialize"),
funds: vec![],
}
}
}
#[async_trait]
impl EventHandler for Handler {
type Err = Error;
async fn handle(&self, event: &Event) -> error_stack::Result<Vec<Any>, Self::Err> {
if !event.is_from_contract(self.voting_verifier_contract.as_ref()) {
return Ok(vec![]);
}
let PollStartedEvent {
poll_id,
source_chain,
source_gateway_address,
messages,
expires_at,
participants,
} = match event.try_into() as error_stack::Result<_, _> {
Err(report) if matches!(report.current_context(), EventTypeMismatch(_)) => {
return Ok(vec![])
}
event => event.change_context(DeserializeEvent)?,
};
if !participants.contains(&self.verifier) {
return Ok(vec![]);
}
if *self.latest_block_height.borrow() >= expires_at {
info!(poll_id = poll_id.to_string(), "skipping expired poll");
return Ok(vec![]);
}
let tx_hashes: HashSet<_> = messages
.iter()
.map(|message| message.message_id.tx_hash_as_hex_no_prefix().to_string())
.collect();
let transaction_responses = self
.http_client
.transaction_responses(tx_hashes)
.await
.change_context(Error::TxReceipts)?;
let message_ids = messages
.iter()
.map(|message| message.message_id.to_string())
.collect::<Vec<_>>();
let votes = info_span!(
"verify messages in poll",
poll_id = poll_id.to_string(),
source_chain = source_chain.to_string(),
message_ids = message_ids.as_value()
)
.in_scope(|| {
info!("ready to verify messages in poll",);
let votes: Vec<_> = messages
.iter()
.map(|msg| {
transaction_responses
.get(&msg.message_id.tx_hash_as_hex_no_prefix().to_string())
.map_or(Vote::NotFound, |tx_response| {
verify_message(&source_gateway_address, tx_response, msg)
})
})
.collect();
info!(
votes = votes.as_value(),
"ready to vote for messages in poll"
);
votes
});
Ok(vec![self
.vote_msg(poll_id, votes)
.into_any()
.expect("vote msg should serialize")])
}
}
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use std::convert::TryInto;
use axelar_wasm_std::msg_id::HexTxHashAndEventIndex;
use cosmrs::cosmwasm::MsgExecuteContract;
use cosmrs::tx::Msg;
use error_stack::Result;
use ethers_core::types::H160;
use events::Error::{DeserializationFailed, EventTypeMismatch};
use events::Event;
use stellar_xdr::curr::ScAddress;
use tokio::sync::watch;
use tokio::test as async_test;
use voting_verifier::events::{PollMetadata, PollStarted, TxEventConfirmation};
use super::PollStartedEvent;
use crate::event_processor::EventHandler;
use crate::handlers::tests::{into_structured_event, participants};
use crate::stellar::rpc_client::Client;
use crate::types::TMAddress;
use crate::PREFIX;
#[test]
fn should_not_deserialize_incorrect_event() {
// incorrect event type
let mut event: Event = into_structured_event(
poll_started_event(participants(5, None), 100),
&TMAddress::random(PREFIX),
);
match event {
Event::Abci {
ref mut event_type, ..
} => {
*event_type = "incorrect".into();
}
_ => panic!("incorrect event type"),
}
let event: Result<PollStartedEvent, events::Error> = (&event).try_into();
assert!(matches!(
event.unwrap_err().current_context(),
EventTypeMismatch(_)
));
// invalid field
let mut event: Event = into_structured_event(
poll_started_event(participants(5, None), 100),
&TMAddress::random(PREFIX),
);
match event {
Event::Abci {
ref mut attributes, ..
} => {
attributes.insert("source_gateway_address".into(), "invalid".into());
}
_ => panic!("incorrect event type"),
}
let event: Result<PollStartedEvent, events::Error> = (&event).try_into();
assert!(matches!(
event.unwrap_err().current_context(),
DeserializationFailed(_, _)
));
}
#[test]
fn stellar_verify_msg_should_deserialize_correct_event() {
let event: Event = into_structured_event(
poll_started_event(participants(5, None), 100),
&TMAddress::random(PREFIX),
);
let event: PollStartedEvent = event.try_into().unwrap();
goldie::assert_debug!(event);
}
#[async_test]
async fn contract_is_not_voting_verifier() {
let event = into_structured_event(
poll_started_event(participants(5, None), 100),
&TMAddress::random(PREFIX),
);
let handler = super::Handler::new(
TMAddress::random(PREFIX),
TMAddress::random(PREFIX),
Client::faux(),
watch::channel(0).1,
);
assert_eq!(handler.handle(&event).await.unwrap(), vec![]);
}
#[async_test]
async fn verifier_is_not_a_participant() {
let voting_verifier = TMAddress::random(PREFIX);
let event = into_structured_event(
poll_started_event(participants(5, None), 100),
&voting_verifier,
);
let handler = super::Handler::new(
TMAddress::random(PREFIX),
voting_verifier,
Client::faux(),
watch::channel(0).1,
);
assert_eq!(handler.handle(&event).await.unwrap(), vec![]);
}
#[async_test]
async fn should_vote_correctly() {
let mut client = Client::faux();
faux::when!(client.transaction_responses).then(|_| Ok(HashMap::new()));
let voting_verifier = TMAddress::random(PREFIX);
let verifier = TMAddress::random(PREFIX);
let event = into_structured_event(
poll_started_event(participants(5, Some(verifier.clone())), 100),
&voting_verifier,
);
let handler = super::Handler::new(verifier, voting_verifier, client, watch::channel(0).1);
let actual = handler.handle(&event).await.unwrap();
assert_eq!(actual.len(), 1);
assert!(MsgExecuteContract::from_any(actual.first().unwrap()).is_ok());
}
fn poll_started_event(participants: Vec<TMAddress>, expires_at: u64) -> PollStarted {
PollStarted::Messages {
metadata: PollMetadata {
poll_id: "100".parse().unwrap(),
source_chain: "stellar".parse().unwrap(),
source_gateway_address: ScAddress::Contract(stellar_xdr::curr::Hash::from([1; 32]))
.to_string()
.try_into()
.unwrap(),
confirmation_height: 15,
expires_at,
participants: participants
.into_iter()
.map(|addr| cosmwasm_std::Addr::unchecked(addr.to_string()))
.collect(),
},
messages: (0..2)
.map(|i| {
let msg_id = HexTxHashAndEventIndex::new([3; 32], i as u64);
#[allow(deprecated)]
// TODO: The below event uses the deprecated tx_id and event_index fields. Remove this attribute when those fields are removed
TxEventConfirmation {
tx_id: msg_id.tx_hash_as_hex(),
event_index: u32::try_from(msg_id.event_index).unwrap(),
message_id: msg_id.to_string().parse().unwrap(),
source_address: ScAddress::Contract(stellar_xdr::curr::Hash::from([2; 32]))
.to_string()
.try_into()
.unwrap(),
destination_chain: "ethereum".parse().unwrap(),
destination_address: format!("0x{:x}", H160::repeat_byte(i))
.parse()
.unwrap(),
payload_hash: [i; 32],
}
})
.collect::<Vec<_>>(),
}
}
}