Skip to content

Commit 32fb954

Browse files
committed
Addressed comments
1 parent a9c0c75 commit 32fb954

File tree

2 files changed

+48
-48
lines changed

2 files changed

+48
-48
lines changed

src/main.rs

Lines changed: 39 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -43,49 +43,55 @@ fn find_message_pda(wormhole_pid: &Pubkey, slot: u64) -> Pubkey {
4343

4444
const FAILED_TO_DECODE: &str = "Failed to decode account data";
4545
const INVALID_UNRELIABLE_DATA_FORMAT: &str = "Invalid unreliable data format";
46-
47-
#[derive(Debug)]
48-
enum VerifyUpdateError {
49-
InvalidMessagePDA,
50-
InvalidEmitterChain,
51-
InvalidAccumulatorAddress,
52-
#[allow(dead_code)]
53-
DecodingError(String),
54-
}
46+
const INVALID_PDA_MESSAGE: &str = "Invalid PDA message";
47+
const INVALID_EMITTER_CHAIN: &str = "Invalid emitter chain";
48+
const INVALID_ACCUMULATOR_ADDRESS: &str = "Invalid accumulator address";
5549

5650
fn decode_and_verify_update(
5751
wormhole_pid: &Pubkey,
5852
accumulator_address: &Pubkey,
5953
update: Response<RpcKeyedAccount>,
60-
) -> Result<PostedMessageUnreliableData, VerifyUpdateError> {
54+
) -> anyhow::Result<PostedMessageUnreliableData> {
6155
if find_message_pda(wormhole_pid, update.context.slot).to_string() != update.value.pubkey {
62-
return Err(VerifyUpdateError::InvalidMessagePDA);
56+
return Err(anyhow::anyhow!(INVALID_PDA_MESSAGE));
6357
}
64-
let data = update
65-
.value
66-
.account
67-
.data
68-
.decode()
69-
.ok_or(VerifyUpdateError::DecodingError(
70-
FAILED_TO_DECODE.to_string(),
71-
))?;
58+
let data = update.value.account.data.decode().ok_or_else(|| {
59+
tracing::error!(
60+
data = ?update.value.account.data,
61+
"Failed to decode account data",
62+
);
63+
anyhow::anyhow!(FAILED_TO_DECODE)
64+
})?;
7265
let unreliable_data: PostedMessageUnreliableData =
7366
BorshDeserialize::deserialize(&mut data.as_slice()).map_err(|e| {
74-
VerifyUpdateError::DecodingError(format!("{}: {}", INVALID_UNRELIABLE_DATA_FORMAT, e))
67+
tracing::error!(
68+
data = ?data,
69+
error = ?e,
70+
"Failed to decode unreliable data",
71+
);
72+
anyhow::anyhow!(format!("{}: {}", INVALID_UNRELIABLE_DATA_FORMAT, e))
7573
})?;
7674

7775
if Chain::Pythnet != unreliable_data.emitter_chain.into() {
78-
return Err(VerifyUpdateError::InvalidEmitterChain);
76+
tracing::error!(
77+
emitter_chain = unreliable_data.emitter_chain,
78+
"Invalid emitter chain"
79+
);
80+
return Err(anyhow::anyhow!(INVALID_EMITTER_CHAIN));
7981
}
8082

8183
if accumulator_address != &Pubkey::from(unreliable_data.emitter_address) {
82-
return Err(VerifyUpdateError::InvalidAccumulatorAddress);
84+
tracing::error!(
85+
emitter_address = ?unreliable_data.emitter_address,
86+
"Invalid accumulator address"
87+
);
88+
return Err(anyhow::anyhow!(INVALID_ACCUMULATOR_ADDRESS));
8389
}
8490

8591
Ok(unreliable_data)
8692
}
8793

88-
fn new_body(unreliable_data: &PostedMessageUnreliableData) -> Body<&RawMessage> {
94+
fn message_data_to_body(unreliable_data: &PostedMessageUnreliableData) -> Body<&RawMessage> {
8995
Body {
9096
timestamp: unreliable_data.submission_time,
9197
nonce: unreliable_data.nonce,
@@ -124,18 +130,13 @@ async fn run_listener(input: RunListenerInput) -> Result<(), PubsubClientError>
124130
match decode_and_verify_update(&input.wormhole_pid, &input.accumulator_address, update)
125131
{
126132
Ok(data) => data,
127-
Err(e) => {
128-
if !matches!(e, VerifyUpdateError::InvalidMessagePDA) {
129-
tracing::error!(error = ?e, "Received an invalid update");
130-
}
131-
continue;
132-
}
133+
Err(_) => continue,
133134
};
134135

135136
tokio::spawn({
136137
let api_client = input.api_client.clone();
137138
async move {
138-
let body = new_body(&unreliable_data);
139+
let body = message_data_to_body(&unreliable_data);
139140
match Observation::try_new(body.clone(), input.secret_key) {
140141
Ok(observation) => {
141142
if let Err(e) = api_client.post_observation(observation).await {
@@ -204,11 +205,12 @@ async fn main() {
204205

205206
#[cfg(test)]
206207
mod tests {
208+
use super::*;
209+
207210
use base64::Engine;
208211
use borsh::BorshSerialize;
209212
use solana_account_decoder::{UiAccount, UiAccountData};
210213

211-
use super::*;
212214
use crate::posted_message::MessageData;
213215

214216
fn get_wormhole_pid() -> Pubkey {
@@ -279,7 +281,7 @@ mod tests {
279281
#[test]
280282
fn test_get_body() {
281283
let unreliable_data = get_unreliable_data();
282-
let body = new_body(&unreliable_data);
284+
let body = message_data_to_body(&unreliable_data);
283285
assert_eq!(body.timestamp, unreliable_data.submission_time);
284286
assert_eq!(body.nonce, unreliable_data.nonce);
285287
assert_eq!(body.emitter_chain, Chain::Pythnet);
@@ -337,7 +339,7 @@ mod tests {
337339
update.context.slot += 1;
338340
let result =
339341
decode_and_verify_update(&get_wormhole_pid(), &get_accumulator_address(), update);
340-
assert!(matches!(result, Err(VerifyUpdateError::InvalidMessagePDA)));
342+
assert_eq!(result.unwrap_err().to_string(), INVALID_PDA_MESSAGE);
341343
}
342344

343345
#[test]
@@ -347,9 +349,7 @@ mod tests {
347349
UiAccountData::Binary("invalid_base64".to_string(), UiAccountEncoding::Base64);
348350
let result =
349351
decode_and_verify_update(&get_wormhole_pid(), &get_accumulator_address(), update);
350-
assert!(
351-
matches!(result, Err(VerifyUpdateError::DecodingError(ref msg)) if msg == FAILED_TO_DECODE),
352-
);
352+
assert_eq!(result.unwrap_err().to_string(), FAILED_TO_DECODE);
353353
}
354354

355355
#[test]
@@ -364,10 +364,7 @@ mod tests {
364364
INVALID_UNRELIABLE_DATA_FORMAT,
365365
"Magic mismatch. Expected [109, 115, 117] but got [4, 1, 2]"
366366
);
367-
assert!(
368-
matches!(result, Err(VerifyUpdateError::DecodingError(ref msg))
369-
if *msg == error_message)
370-
);
367+
assert_eq!(result.unwrap_err().to_string(), error_message);
371368
}
372369

373370
#[test]
@@ -379,10 +376,7 @@ mod tests {
379376
&get_accumulator_address(),
380377
get_update(unreliable_data),
381378
);
382-
assert!(matches!(
383-
result,
384-
Err(VerifyUpdateError::InvalidEmitterChain)
385-
));
379+
assert_eq!(result.unwrap_err().to_string(), INVALID_EMITTER_CHAIN);
386380
}
387381

388382
#[test]
@@ -394,9 +388,6 @@ mod tests {
394388
&get_accumulator_address(),
395389
get_update(unreliable_data),
396390
);
397-
assert!(matches!(
398-
result,
399-
Err(VerifyUpdateError::InvalidAccumulatorAddress)
400-
));
391+
assert_eq!(result.unwrap_err().to_string(), INVALID_ACCUMULATOR_ADDRESS);
401392
}
402393
}

src/posted_message.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
//! This module defines the `PostedMessage` structure used to parse and verify messages
2+
//! posted by the Wormhole protocol.
3+
//!
4+
//! ⚠️ Note: This is mostly a copy-paste from the Wormhole reference implementation.
5+
//! If you forget how it works or need updates, refer to the official source:
6+
//! https://github.com/wormhole-foundation/wormhole/blob/main/solana/bridge/program/src/accounts/posted_message.rs#
7+
//!
8+
//! Keep in sync if the upstream changes!
9+
110
use {
211
borsh::{BorshDeserialize, BorshSerialize},
312
serde::{Deserialize, Serialize},

0 commit comments

Comments
 (0)