Skip to content

Commit c6df39e

Browse files
committed
fixed unwrap and expect issues
1 parent 0148862 commit c6df39e

File tree

4 files changed

+24
-18
lines changed

4 files changed

+24
-18
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ pub enum PythReceiverError {
1515
NewPriceUnavailable,
1616
InvalidAccumulatorMessageType,
1717
InsufficientFee,
18+
InvalidEmitterAddress,
19+
TooManyUpdates
1820
}
1921

2022
impl core::fmt::Debug for PythReceiverError {
@@ -39,6 +41,8 @@ impl From<PythReceiverError> for Vec<u8> {
3941
PythReceiverError::NewPriceUnavailable => 11,
4042
PythReceiverError::InvalidAccumulatorMessageType => 12,
4143
PythReceiverError::InsufficientFee => 13,
44+
PythReceiverError::InvalidEmitterAddress => 14,
45+
PythReceiverError::TooManyUpdates => 15
4246
}]
4347
}
4448
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ mod test {
5656
fn e2e_valid_test(pyth_contract: Contract<PythReceiver>, wormhole_contract: Contract<WormholeContract>, alice: Address) {
5757
let guardians = current_guardians();
5858
let governance_contract = Address::from_slice(&GOVERNANCE_CONTRACT.to_be_bytes::<32>()[12..32]);
59-
wormhole_contract.sender(alice).initialize(guardians, CHAIN_ID, GOVERNANCE_CHAIN_ID, governance_contract).unwrap();
59+
wormhole_contract.sender(alice).initialize(guardians, 4, CHAIN_ID, GOVERNANCE_CHAIN_ID, governance_contract).unwrap();
6060
// let result = wormhole_contract.sender(alice).store_gs(4, current_guardians(), 0);
6161

6262
let single_update_fee = U256::from(100u64);
@@ -107,7 +107,7 @@ mod test {
107107
fn test_update_price_feed_insufficient_fee(pyth_contract: Contract<PythReceiver>, wormhole_contract: Contract<WormholeContract>, alice: Address) {
108108
let guardians = current_guardians();
109109
let governance_contract = Address::from_slice(&GOVERNANCE_CONTRACT.to_be_bytes::<32>()[12..32]);
110-
wormhole_contract.sender(alice).initialize(guardians, CHAIN_ID, GOVERNANCE_CHAIN_ID, governance_contract).unwrap();
110+
wormhole_contract.sender(alice).initialize(guardians, 4, CHAIN_ID, GOVERNANCE_CHAIN_ID, governance_contract).unwrap();
111111
// let result = wormhole_contract.sender(alice).store_gs(4, current_guardians(), 0);
112112

113113
let single_update_fee = U256::from(100u64);
@@ -147,7 +147,7 @@ mod test {
147147
fn test_get_price_after_multiple_updates(pyth_contract: Contract<PythReceiver>, wormhole_contract: Contract<WormholeContract>, alice: Address) {
148148
let guardians = current_guardians();
149149
let governance_contract = Address::from_slice(&GOVERNANCE_CONTRACT.to_be_bytes::<32>()[12..32]);
150-
wormhole_contract.sender(alice).initialize(guardians, CHAIN_ID, GOVERNANCE_CHAIN_ID, governance_contract).unwrap();
150+
wormhole_contract.sender(alice).initialize(guardians, 4, CHAIN_ID, GOVERNANCE_CHAIN_ID, governance_contract).unwrap();
151151

152152
let single_update_fee = U256::from(100u64);
153153
let valid_time_period = U256::from(3600u64);
@@ -198,7 +198,7 @@ mod test {
198198
fn test_get_price_unavailable_no_update(pyth_contract: Contract<PythReceiver>, wormhole_contract: Contract<WormholeContract>, alice: Address) {
199199
let guardians = current_guardians();
200200
let governance_contract = Address::from_slice(&GOVERNANCE_CONTRACT.to_be_bytes::<32>()[12..32]);
201-
wormhole_contract.sender(alice).initialize(guardians, CHAIN_ID, GOVERNANCE_CHAIN_ID, governance_contract).unwrap();
201+
wormhole_contract.sender(alice).initialize(guardians, 4, CHAIN_ID, GOVERNANCE_CHAIN_ID, governance_contract).unwrap();
202202

203203
let single_update_fee = U256::from(100u64);
204204
let valid_time_period = U256::from(3600u64);
@@ -232,7 +232,7 @@ mod test {
232232
fn test_get_price_no_older_than_unavailable_random_id(pyth_contract: Contract<PythReceiver>, wormhole_contract: Contract<WormholeContract>, alice: Address) {
233233
let guardians = current_guardians();
234234
let governance_contract = Address::from_slice(&GOVERNANCE_CONTRACT.to_be_bytes::<32>()[12..32]);
235-
wormhole_contract.sender(alice).initialize(guardians, CHAIN_ID, GOVERNANCE_CHAIN_ID, governance_contract).unwrap();
235+
wormhole_contract.sender(alice).initialize(guardians, 4, CHAIN_ID, GOVERNANCE_CHAIN_ID, governance_contract).unwrap();
236236

237237
let single_update_fee = U256::from(100u64);
238238
let valid_time_period = U256::from(3600u64);
@@ -271,7 +271,7 @@ mod test {
271271
fn test_get_price_no_older_than_valid_max_age(pyth_contract: Contract<PythReceiver>, wormhole_contract: Contract<WormholeContract>, alice: Address) {
272272
let guardians = current_guardians();
273273
let governance_contract = Address::from_slice(&GOVERNANCE_CONTRACT.to_be_bytes::<32>()[12..32]);
274-
wormhole_contract.sender(alice).initialize(guardians, CHAIN_ID, GOVERNANCE_CHAIN_ID, governance_contract).unwrap();
274+
wormhole_contract.sender(alice).initialize(guardians, 4, CHAIN_ID, GOVERNANCE_CHAIN_ID, governance_contract).unwrap();
275275

276276
let single_update_fee = U256::from(100u64);
277277
let valid_time_period = U256::from(3600u64);
@@ -318,7 +318,7 @@ mod test {
318318
fn test_get_price_no_older_than_too_old(pyth_contract: Contract<PythReceiver>, wormhole_contract: Contract<WormholeContract>, alice: Address) {
319319
let guardians = current_guardians();
320320
let governance_contract = Address::from_slice(&GOVERNANCE_CONTRACT.to_be_bytes::<32>()[12..32]);
321-
wormhole_contract.sender(alice).initialize(guardians, CHAIN_ID, GOVERNANCE_CHAIN_ID, governance_contract).unwrap();
321+
wormhole_contract.sender(alice).initialize(guardians, 4, CHAIN_ID, GOVERNANCE_CHAIN_ID, governance_contract).unwrap();
322322

323323
let single_update_fee = U256::from(100u64);
324324
let valid_time_period = U256::from(3600u64);
@@ -357,7 +357,7 @@ mod test {
357357
fn test_multiple_updates_both_ids(pyth_contract: Contract<PythReceiver>, wormhole_contract: Contract<WormholeContract>, alice: Address) {
358358
let guardians = current_guardians();
359359
let governance_contract = Address::from_slice(&GOVERNANCE_CONTRACT.to_be_bytes::<32>()[12..32]);
360-
wormhole_contract.sender(alice).initialize(guardians, CHAIN_ID, GOVERNANCE_CHAIN_ID, governance_contract).unwrap();
360+
wormhole_contract.sender(alice).initialize(guardians, 4, CHAIN_ID, GOVERNANCE_CHAIN_ID, governance_contract).unwrap();
361361

362362
let single_update_fee = U256::from(100u64);
363363
let valid_time_period = U256::from(3600u64);

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,17 +178,22 @@ impl PythReceiver {
178178
return Err(PythReceiverError::InvalidAccumulatorMessage);
179179
}
180180

181-
let update_data = AccumulatorUpdateData::try_from_slice(&update_data_array).unwrap();
181+
let update_data = AccumulatorUpdateData::try_from_slice(&update_data_array).map_err(|_| PythReceiverError::InvalidAccumulatorMessage)?;
182182

183183
match update_data.proof {
184184
Proof::WormholeMerkle { vaa, updates } => {
185185
let wormhole: IWormholeContract = IWormholeContract::new(self.wormhole.get());
186186
let config = Call::new();
187-
wormhole.parse_and_verify_vm(config, Vec::from(vaa.clone())).map_err(|_| PythReceiverError::InvalidWormholeMessage).unwrap();
187+
wormhole.parse_and_verify_vm(config, Vec::from(vaa.clone())).map_err(|_| PythReceiverError::InvalidWormholeMessage)?;
188188

189-
let vaa = Vaa::read(&mut Vec::from(vaa.clone()).as_slice()).unwrap();
189+
let vaa = Vaa::read(&mut Vec::from(vaa.clone()).as_slice()).map_err(|_| PythReceiverError::VaaVerificationFailed)?;
190190

191-
let cur_emitter_address: &[u8; 32] = vaa.body.emitter_address.as_slice().try_into().expect("emitter address must be 32 bytes");
191+
let cur_emitter_address: &[u8; 32] = vaa
192+
.body
193+
.emitter_address
194+
.as_slice()
195+
.try_into()
196+
.map_err(|_| PythReceiverError::InvalidEmitterAddress)?;
192197

193198
let cur_data_source = DataSource {
194199
chain_id: U16::from(vaa.body.emitter_chain),
@@ -199,9 +204,10 @@ impl PythReceiver {
199204
return Err(PythReceiverError::InvalidWormholeMessage);
200205
}
201206

202-
let root_digest: MerkleRoot<Keccak160> = parse_wormhole_proof(vaa).unwrap();
207+
let root_digest: MerkleRoot<Keccak160> = parse_wormhole_proof(vaa)?;
208+
209+
let num_updates = u8::try_from(updates.len()).map_err(|_| PythReceiverError::TooManyUpdates)?;
203210

204-
let num_updates = u8::try_from(updates.len()).expect("value doesn't fit in u8");
205211
let total_fee = self.get_total_fee(num_updates);
206212

207213
let value = self.vm().msg_value();

target_chains/stylus/contracts/wormhole/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ extern crate alloc;
77
#[global_allocator]
88
static ALLOC: mini_alloc::MiniAlloc = mini_alloc::MiniAlloc::INIT;
99

10-
11-
mod types;
12-
use types::{GuardianSet, GuardianSignature, VerifiedVM, WormholeError};
13-
1410
use alloc::{vec, vec::Vec};
1511
use stylus_sdk::{
1612
prelude::*,

0 commit comments

Comments
 (0)