Skip to content

Commit 76aa0c0

Browse files
authored
fix(ampd): stellar tx receipt query and event check (#647)
1 parent c8c27d4 commit 76aa0c0

File tree

3 files changed

+48
-25
lines changed

3 files changed

+48
-25
lines changed

ampd/src/handlers/stellar_verify_msg.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ use events::Event;
1111
use events_derive::try_from;
1212
use prost_types::Any;
1313
use router_api::ChainName;
14-
use serde::Deserialize;
14+
use serde::de::Error as DeserializeError;
15+
use serde::{Deserialize, Deserializer};
1516
use serde_with::{serde_as, DisplayFromStr};
1617
use stellar_xdr::curr::{ScAddress, ScBytes, ScString};
1718
use tokio::sync::watch::Receiver;
@@ -26,9 +27,22 @@ use crate::stellar::http_client::Client;
2627
use crate::stellar::verifier::verify_message;
2728
use crate::types::TMAddress;
2829

30+
pub fn deserialize_tx_id<'de, D>(deserializer: D) -> Result<String, D::Error>
31+
where
32+
D: Deserializer<'de>,
33+
{
34+
let tx_id = String::deserialize(deserializer)?;
35+
36+
tx_id
37+
.strip_prefix("0x")
38+
.map(String::from)
39+
.ok_or(D::Error::custom(Error::DeserializeEvent))
40+
}
41+
2942
#[serde_as]
3043
#[derive(Deserialize, Debug, Clone)]
3144
pub struct Message {
45+
#[serde(deserialize_with = "deserialize_tx_id")]
3246
pub tx_id: String,
3347
pub event_index: u32,
3448
pub destination_address: ScString,
@@ -129,7 +143,7 @@ impl EventHandler for Handler {
129143

130144
let message_ids = messages
131145
.iter()
132-
.map(|message| format!("{}-{}", message.tx_id, message.event_index))
146+
.map(|message| format!("{}-{}", message.tx_id.clone(), message.event_index))
133147
.collect::<Vec<_>>();
134148

135149
let votes = info_span!(
@@ -316,7 +330,7 @@ mod tests {
316330
},
317331
messages: (0..2)
318332
.map(|i| TxEventConfirmation {
319-
tx_id: format!("{:x}", Hash::random()).parse().unwrap(),
333+
tx_id: format!("0x{:x}", Hash::random()).parse().unwrap(),
320334
event_index: i,
321335
source_address: ScAddress::Contract(stellar_xdr::curr::Hash::from(
322336
Hash::random().0,

ampd/src/handlers/stellar_verify_verifier_set.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use tracing::{info, info_span};
1818
use valuable::Valuable;
1919
use voting_verifier::msg::ExecuteMsg;
2020

21+
use super::stellar_verify_msg::deserialize_tx_id;
2122
use crate::event_processor::EventHandler;
2223
use crate::handlers::errors::Error;
2324
use crate::handlers::errors::Error::DeserializeEvent;
@@ -27,6 +28,7 @@ use crate::types::TMAddress;
2728

2829
#[derive(Deserialize, Debug)]
2930
pub struct VerifierSetConfirmation {
31+
#[serde(deserialize_with = "deserialize_tx_id")]
3032
pub tx_id: String,
3133
pub event_index: u32,
3234
pub verifier_set: VerifierSet,
@@ -117,7 +119,7 @@ impl EventHandler for Handler {
117119
let vote = info_span!(
118120
"verify a new verifier set",
119121
poll_id = poll_id.to_string(),
120-
id = format!("{}-{}", verifier_set.tx_id, verifier_set.event_index),
122+
id = format!("0x{}-{}", verifier_set.tx_id, verifier_set.event_index),
121123
)
122124
.in_scope(|| {
123125
info!("ready to verify verifier set in poll",);
@@ -291,7 +293,7 @@ mod tests {
291293
.collect(),
292294
},
293295
verifier_set: VerifierSetConfirmation {
294-
tx_id: format!("{:x}", Hash::random()).parse().unwrap(),
296+
tx_id: format!("0x{:x}", Hash::random()).parse().unwrap(),
295297
event_index: 0,
296298
verifier_set: build_verifier_set(KeyType::Ed25519, &ed25519_test_data::signers()),
297299
},

ampd/src/stellar/verifier.rs

+27-20
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,33 @@ impl PartialEq<ContractEventBody> for VerifierSetConfirmation {
4848
fn eq(&self, event: &ContractEventBody) -> bool {
4949
let ContractEventBody::V0(body) = event;
5050

51-
if body.topics.len() != 3 {
51+
if body.topics.len() != 1 {
5252
return false;
5353
}
5454

55-
let [symbol, _, signer_hash] = &body.topics[..] else {
55+
let [symbol] = &body.topics[..] else {
5656
return false;
5757
};
5858

5959
let expected_topic: ScVal =
6060
ScSymbol(StringM::from_str(TOPIC_ROTATED).expect("must convert str to ScSymbol"))
6161
.into();
6262

63+
let rotated_signers = match &body.data {
64+
ScVal::Vec(Some(data)) if data.len() == 1 => {
65+
let [rotated_signers] = &data[..] else {
66+
return false;
67+
};
68+
rotated_signers.clone()
69+
}
70+
_ => return false,
71+
};
72+
6373
WeightedSigners::try_from(&self.verifier_set)
6474
.ok()
65-
.and_then(|signers| signers.hash().ok())
66-
.and_then(|hash| ScVal::try_from(hash).ok())
67-
.map_or(false, |hash| {
68-
symbol == &expected_topic && signer_hash == &hash
75+
.and_then(|signers| ScVal::try_from(signers).ok())
76+
.map_or(false, |signers: ScVal| {
77+
symbol == &expected_topic && signers == rotated_signers
6978
})
7079
}
7180
}
@@ -138,7 +147,7 @@ mod test {
138147
use stellar::WeightedSigners;
139148
use stellar_xdr::curr::{
140149
AccountId, BytesM, ContractEvent, ContractEventBody, ContractEventType, ContractEventV0,
141-
PublicKey, ScAddress, ScBytes, ScString, ScSymbol, ScVal, StringM, Uint256,
150+
PublicKey, ScAddress, ScBytes, ScString, ScSymbol, ScVal, ScVec, StringM, Uint256,
142151
};
143152

144153
use crate::handlers::stellar_verify_msg::Message;
@@ -293,7 +302,7 @@ mod test {
293302
let signing_key = SigningKey::generate(&mut OsRng);
294303

295304
let msg = Message {
296-
tx_id: Hash::random().to_string(),
305+
tx_id: format!("{:x}", Hash::random()),
297306
event_index: 0,
298307
source_address: ScAddress::Account(AccountId(PublicKey::PublicKeyTypeEd25519(
299308
Uint256::from(signing_key.verifying_key().to_bytes()),
@@ -351,7 +360,7 @@ mod test {
351360
let threshold = Uint128::new(2u128);
352361

353362
let verifier_set_confirmation = VerifierSetConfirmation {
354-
tx_id: Hash::random().to_string(),
363+
tx_id: format!("{:x}", Hash::random()),
355364
event_index: 0,
356365
verifier_set: VerifierSet {
357366
signers: signers
@@ -363,21 +372,19 @@ mod test {
363372
},
364373
};
365374

366-
let weighted_signers =
367-
WeightedSigners::try_from(&verifier_set_confirmation.verifier_set).unwrap();
368-
let signer_hash = weighted_signers.hash().unwrap();
375+
let weighted_signers: ScVal =
376+
WeightedSigners::try_from(&verifier_set_confirmation.verifier_set)
377+
.unwrap()
378+
.try_into()
379+
.unwrap();
369380

370381
let event_body = ContractEventBody::V0(ContractEventV0 {
371-
topics: vec![
372-
ScVal::Symbol(ScSymbol(StringM::from_str(TOPIC_ROTATED).unwrap())),
373-
ScVal::Bytes(ScBytes(
374-
BytesM::try_from(Hash::random().to_fixed_bytes()).unwrap(),
375-
)),
376-
ScVal::try_from(signer_hash).unwrap(),
377-
]
382+
topics: vec![ScVal::Symbol(ScSymbol(
383+
StringM::from_str(TOPIC_ROTATED).unwrap(),
384+
))]
378385
.try_into()
379386
.unwrap(),
380-
data: ScVal::Vec(None),
387+
data: ScVal::Vec(Some(ScVec::try_from(vec![weighted_signers]).unwrap())),
381388
});
382389

383390
let event = ContractEvent {

0 commit comments

Comments
 (0)