Skip to content

Commit 1ecd765

Browse files
committed
improved error handling + variable names
1 parent 0bb1763 commit 1ecd765

File tree

2 files changed

+44
-38
lines changed

2 files changed

+44
-38
lines changed

target_chains/stylus/contracts/pyth-receiver/src/error.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
use alloc::vec::Vec;
22

33
pub enum PythReceiverError {
4-
PriceUnavailable
4+
PriceUnavailable,
5+
InvalidUpdateData,
6+
VaaVerificationFailed,
7+
InvalidVaa,
8+
InvalidWormholeMessage,
9+
InvalidMerkleProof,
10+
InvalidAccumulatorMessage,
11+
InvalidMerkleRoot,
12+
InvalidMerklePath,
13+
InvalidUnknownSource,
14+
NewPriceUnavailable,
15+
InvalidAccumulatorMessageType,
516
}
617

718
impl core::fmt::Debug for PythReceiverError {
@@ -14,6 +25,17 @@ impl From<PythReceiverError> for Vec<u8> {
1425
fn from(error: PythReceiverError) -> Vec<u8> {
1526
vec![match error {
1627
PythReceiverError::PriceUnavailable => 1,
28+
PythReceiverError::InvalidUpdateData => 2,
29+
PythReceiverError::VaaVerificationFailed => 3,
30+
PythReceiverError::InvalidVaa => 4,
31+
PythReceiverError::InvalidWormholeMessage => 5,
32+
PythReceiverError::InvalidMerkleProof => 6,
33+
PythReceiverError::InvalidAccumulatorMessage => 7,
34+
PythReceiverError::InvalidMerkleRoot => 8,
35+
PythReceiverError::InvalidMerklePath => 9,
36+
PythReceiverError::InvalidUnknownSource => 10,
37+
PythReceiverError::NewPriceUnavailable => 11,
38+
PythReceiverError::InvalidAccumulatorMessageType => 12,
1739
}]
1840
}
1941
}

target_chains/stylus/contracts/pyth-receiver/src/lib.rs

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ pub struct PythReceiver {
5757

5858
#[public]
5959
impl PythReceiver {
60-
pub fn initialize(&mut self, _wormhole: Address, _single_update_fee_in_wei: U256, _valid_time_period_seconds: U256,
60+
pub fn initialize(&mut self, wormhole: Address, single_update_fee_in_wei: U256, valid_time_period_seconds: U256,
6161
data_source_emitter_chain_ids: Vec<u16>, data_source_emitter_addresses: Vec<[u8; 32]>,
6262
governance_emitter_chain_id: u16, governance_emitter_address: [u8; 32],
63-
governance_initial_sequence: u64, _data: Vec<u8>) {
64-
self.wormhole.set(_wormhole);
65-
self.single_update_fee_in_wei.set(_single_update_fee_in_wei);
66-
self.valid_time_period_seconds.set(_valid_time_period_seconds);
63+
governance_initial_sequence: u64, data: Vec<u8>) {
64+
self.wormhole.set(wormhole);
65+
self.single_update_fee_in_wei.set(single_update_fee_in_wei);
66+
self.valid_time_period_seconds.set(valid_time_period_seconds);
6767

6868
self.governance_data_source_chain_id.set(U16::from(governance_emitter_chain_id));
6969
self.governance_data_source_emitter_address.set(FixedBytes::<32>::from(governance_emitter_address));
@@ -92,8 +92,8 @@ impl PythReceiver {
9292
}
9393
}
9494

95-
pub fn get_price_unsafe(&self, _id: [u8; 32]) -> Result<PriceInfoReturn, PythReceiverError> {
96-
let id_fb = FixedBytes::<32>::from(_id);
95+
pub fn get_price_unsafe(&self, id: [u8; 32]) -> Result<PriceInfoReturn, PythReceiverError> {
96+
let id_fb = FixedBytes::<32>::from(id);
9797

9898
let price_info = self.latest_price_info.get(id_fb);
9999

@@ -111,10 +111,10 @@ impl PythReceiver {
111111
))
112112
}
113113

114-
pub fn get_price_no_older_than(&self, _id: [u8; 32], _age: u64) -> Result<PriceInfoReturn, PythReceiverError> {
115-
let price_info = self.get_price_unsafe(_id)?;
116-
if !self.is_no_older_than(price_info.0, _age) {
117-
return Err(PythReceiverError::PriceUnavailable);
114+
pub fn get_price_no_older_than(&self, id: [u8; 32], age: u64) -> Result<PriceInfoReturn, PythReceiverError> {
115+
let price_info = self.get_price_unsafe(id)?;
116+
if !self.is_no_older_than(price_info.0, age) {
117+
return Err(PythReceiverError::NewPriceUnavailable);
118118
}
119119
Ok(price_info)
120120
}
@@ -144,14 +144,14 @@ impl PythReceiver {
144144
let update_data_array: &[u8] = &update_data;
145145
// Check the first 4 bytes of the update_data_array for the magic header
146146
if update_data_array.len() < 4 {
147-
panic!("update_data too short for magic header check");
147+
return Err(PythReceiverError::InvalidUpdateData);
148148
}
149149

150150
let mut header = [0u8; 4];
151151
header.copy_from_slice(&update_data_array[0..4]);
152152

153153
if &header != PYTHNET_ACCUMULATOR_UPDATE_MAGIC {
154-
panic!("Invalid update_data magic header");
154+
return Err(PythReceiverError::InvalidAccumulatorMessage);
155155
}
156156

157157
let update_data = AccumulatorUpdateData::try_from_slice(&update_data_array).unwrap();
@@ -160,32 +160,25 @@ impl PythReceiver {
160160
Proof::WormholeMerkle { vaa, updates } => {
161161
let wormhole: IWormholeContract = IWormholeContract::new(self.wormhole.get());
162162
let config = Call::new();
163-
let parsed_vaa = wormhole.parse_and_verify_vm(config, Vec::from(vaa)).map_err(|_| PythReceiverError::PriceUnavailable).unwrap();
163+
let parsed_vaa = wormhole.parse_and_verify_vm(config, Vec::from(vaa)).map_err(|_| PythReceiverError::InvalidWormholeMessage).unwrap();
164164
let vaa = Vaa::read(&mut parsed_vaa.as_slice()).unwrap();
165165

166166
// TODO: CHECK IF THE VAA IS FROM A VALID DATA SOURCE
167167

168168
let root_digest: MerkleRoot<Keccak160> = parse_wormhole_proof(vaa).unwrap();
169169

170170
for update in updates {
171-
// fill in update processing logic.
172-
// update is a merkle price update
173-
174-
// pub struct MerklePriceUpdate {
175-
// pub message: PrefixedVec<u16, u8>,
176-
// pub proof: MerklePath<Keccak160>,
177-
// }
178171

179172
let message_vec = Vec::from(update.message);
180173
let proof: MerklePath<Keccak160> = update.proof;
181174

182175
if !root_digest.check(proof, &message_vec) {
183-
return Err(PythReceiverError::PriceUnavailable);
176+
return Err(PythReceiverError::InvalidMerkleProof);
184177
}
185178

186-
// TODO: UPDATE THE PRICE INFO
179+
// UPDATE STORED PRICE INFO BASED ON THE UPDATES IN THE VAA
187180
let msg = from_slice::<byteorder::BE, Message>(&message_vec)
188-
.map_err(|_| PythReceiverError::PriceUnavailable)?;
181+
.map_err(|_| PythReceiverError::InvalidAccumulatorMessage)?;
189182

190183
match msg {
191184
Message::PriceFeedMessage(price_feed_message) => {
@@ -204,22 +197,11 @@ impl PythReceiver {
204197

205198

206199
},
207-
Message::TwapMessage(_) => {
208-
// Handle TWAP message - currently not implemented
209-
// This could be extended to handle TWAP price updates
210-
},
211-
Message::PublisherStakeCapsMessage(_) => {
212-
// Handle publisher stake caps message - currently not implemented
213-
// This could be extended to handle publisher stake updates
200+
_ => {
201+
return Err(PythReceiverError::InvalidAccumulatorMessageType);
214202
},
215203
}
216-
217-
218-
// TODO: STORE PRICE INFO IN OUTPUT
219-
220204
}
221-
222-
// TODO: FORM OUTPUT ARRAY
223205
}
224206
};
225207

@@ -300,3 +282,5 @@ fn parse_wormhole_proof(vaa: Vaa) -> Result<MerkleRoot<Keccak160>, PythReceiverE
300282
});
301283
Ok(root)
302284
}
285+
286+

0 commit comments

Comments
 (0)