@@ -43,49 +43,55 @@ fn find_message_pda(wormhole_pid: &Pubkey, slot: u64) -> Pubkey {
43
43
44
44
const FAILED_TO_DECODE : & str = "Failed to decode account data" ;
45
45
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" ;
55
49
56
50
fn decode_and_verify_update (
57
51
wormhole_pid : & Pubkey ,
58
52
accumulator_address : & Pubkey ,
59
53
update : Response < RpcKeyedAccount > ,
60
- ) -> Result < PostedMessageUnreliableData , VerifyUpdateError > {
54
+ ) -> anyhow :: Result < PostedMessageUnreliableData > {
61
55
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 ) ) ;
63
57
}
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
+ } ) ?;
72
65
let unreliable_data: PostedMessageUnreliableData =
73
66
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) )
75
73
} ) ?;
76
74
77
75
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 ) ) ;
79
81
}
80
82
81
83
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 ) ) ;
83
89
}
84
90
85
91
Ok ( unreliable_data)
86
92
}
87
93
88
- fn new_body ( unreliable_data : & PostedMessageUnreliableData ) -> Body < & RawMessage > {
94
+ fn message_data_to_body ( unreliable_data : & PostedMessageUnreliableData ) -> Body < & RawMessage > {
89
95
Body {
90
96
timestamp : unreliable_data. submission_time ,
91
97
nonce : unreliable_data. nonce ,
@@ -124,18 +130,13 @@ async fn run_listener(input: RunListenerInput) -> Result<(), PubsubClientError>
124
130
match decode_and_verify_update ( & input. wormhole_pid , & input. accumulator_address , update)
125
131
{
126
132
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 ,
133
134
} ;
134
135
135
136
tokio:: spawn ( {
136
137
let api_client = input. api_client . clone ( ) ;
137
138
async move {
138
- let body = new_body ( & unreliable_data) ;
139
+ let body = message_data_to_body ( & unreliable_data) ;
139
140
match Observation :: try_new ( body. clone ( ) , input. secret_key ) {
140
141
Ok ( observation) => {
141
142
if let Err ( e) = api_client. post_observation ( observation) . await {
@@ -204,11 +205,12 @@ async fn main() {
204
205
205
206
#[ cfg( test) ]
206
207
mod tests {
208
+ use super :: * ;
209
+
207
210
use base64:: Engine ;
208
211
use borsh:: BorshSerialize ;
209
212
use solana_account_decoder:: { UiAccount , UiAccountData } ;
210
213
211
- use super :: * ;
212
214
use crate :: posted_message:: MessageData ;
213
215
214
216
fn get_wormhole_pid ( ) -> Pubkey {
@@ -279,7 +281,7 @@ mod tests {
279
281
#[ test]
280
282
fn test_get_body ( ) {
281
283
let unreliable_data = get_unreliable_data ( ) ;
282
- let body = new_body ( & unreliable_data) ;
284
+ let body = message_data_to_body ( & unreliable_data) ;
283
285
assert_eq ! ( body. timestamp, unreliable_data. submission_time) ;
284
286
assert_eq ! ( body. nonce, unreliable_data. nonce) ;
285
287
assert_eq ! ( body. emitter_chain, Chain :: Pythnet ) ;
@@ -337,7 +339,7 @@ mod tests {
337
339
update. context . slot += 1 ;
338
340
let result =
339
341
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 ) ;
341
343
}
342
344
343
345
#[ test]
@@ -347,9 +349,7 @@ mod tests {
347
349
UiAccountData :: Binary ( "invalid_base64" . to_string ( ) , UiAccountEncoding :: Base64 ) ;
348
350
let result =
349
351
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 ) ;
353
353
}
354
354
355
355
#[ test]
@@ -364,10 +364,7 @@ mod tests {
364
364
INVALID_UNRELIABLE_DATA_FORMAT ,
365
365
"Magic mismatch. Expected [109, 115, 117] but got [4, 1, 2]"
366
366
) ;
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) ;
371
368
}
372
369
373
370
#[ test]
@@ -379,10 +376,7 @@ mod tests {
379
376
& get_accumulator_address ( ) ,
380
377
get_update ( unreliable_data) ,
381
378
) ;
382
- assert ! ( matches!(
383
- result,
384
- Err ( VerifyUpdateError :: InvalidEmitterChain )
385
- ) ) ;
379
+ assert_eq ! ( result. unwrap_err( ) . to_string( ) , INVALID_EMITTER_CHAIN ) ;
386
380
}
387
381
388
382
#[ test]
@@ -394,9 +388,6 @@ mod tests {
394
388
& get_accumulator_address ( ) ,
395
389
get_update ( unreliable_data) ,
396
390
) ;
397
- assert ! ( matches!(
398
- result,
399
- Err ( VerifyUpdateError :: InvalidAccumulatorAddress )
400
- ) ) ;
391
+ assert_eq ! ( result. unwrap_err( ) . to_string( ) , INVALID_ACCUMULATOR_ADDRESS ) ;
401
392
}
402
393
}
0 commit comments