From: Riccardo Casatta Date: Thu, 1 Mar 2018 08:54:56 +0000 (+0100) Subject: use ? instead of try macro X-Git-Tag: v0.0.12~425^2 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=b94365f3f560b095350bd36d4eb9bf63268679b0;p=rust-lightning use ? instead of try macro --- diff --git a/src/ln/chan_utils.rs b/src/ln/chan_utils.rs index b14f77e9d..dd5515c88 100644 --- a/src/ln/chan_utils.rs +++ b/src/ln/chan_utils.rs @@ -35,7 +35,7 @@ pub fn derive_private_key(secp_ctx: &Secp256k1, per_commitment_point: &PublicKey sha.result(&mut res); let mut key = base_secret.clone(); - try!(key.add_assign(&secp_ctx, &try!(SecretKey::from_slice(&secp_ctx, &res)))); + key.add_assign(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &res)?)?; Ok(key) } @@ -46,7 +46,7 @@ pub fn derive_public_key(secp_ctx: &Secp256k1, per_commitment_point: &PublicKey, let mut res = [0; 32]; sha.result(&mut res); - let hashkey = PublicKey::from_secret_key(&secp_ctx, &try!(SecretKey::from_slice(&secp_ctx, &res))).unwrap(); + let hashkey = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &res)?).unwrap(); base_point.combine(&secp_ctx, &hashkey) } @@ -62,7 +62,7 @@ pub fn derive_private_revocation_key(secp_ctx: &Secp256k1, per_commitment_secret let mut res = [0; 32]; sha.result(&mut res); - try!(SecretKey::from_slice(&secp_ctx, &res)) + SecretKey::from_slice(&secp_ctx, &res)? }; let commit_append_rev_hash_key = { let mut sha = Sha256::new(); @@ -71,14 +71,14 @@ pub fn derive_private_revocation_key(secp_ctx: &Secp256k1, per_commitment_secret let mut res = [0; 32]; sha.result(&mut res); - try!(SecretKey::from_slice(&secp_ctx, &res)) + SecretKey::from_slice(&secp_ctx, &res)? }; let mut part_a = revocation_base_secret.clone(); - try!(part_a.mul_assign(&secp_ctx, &rev_append_commit_hash_key)); + part_a.mul_assign(&secp_ctx, &rev_append_commit_hash_key)?; let mut part_b = per_commitment_secret.clone(); - try!(part_b.mul_assign(&secp_ctx, &commit_append_rev_hash_key)); - try!(part_a.add_assign(&secp_ctx, &part_b)); + part_b.mul_assign(&secp_ctx, &commit_append_rev_hash_key)?; + part_a.add_assign(&secp_ctx, &part_b)?; Ok(part_a) } @@ -90,7 +90,7 @@ pub fn derive_public_revocation_key(secp_ctx: &Secp256k1, per_commitment_point: let mut res = [0; 32]; sha.result(&mut res); - try!(SecretKey::from_slice(&secp_ctx, &res)) + SecretKey::from_slice(&secp_ctx, &res)? }; let commit_append_rev_hash_key = { let mut sha = Sha256::new(); @@ -99,13 +99,13 @@ pub fn derive_public_revocation_key(secp_ctx: &Secp256k1, per_commitment_point: let mut res = [0; 32]; sha.result(&mut res); - try!(SecretKey::from_slice(&secp_ctx, &res)) + SecretKey::from_slice(&secp_ctx, &res)? }; let mut part_a = revocation_base_point.clone(); - try!(part_a.mul_assign(&secp_ctx, &rev_append_commit_hash_key)); + part_a.mul_assign(&secp_ctx, &rev_append_commit_hash_key)?; let mut part_b = per_commitment_point.clone(); - try!(part_b.mul_assign(&secp_ctx, &commit_append_rev_hash_key)); + part_b.mul_assign(&secp_ctx, &commit_append_rev_hash_key)?; part_a.combine(&secp_ctx, &part_b) } @@ -122,11 +122,11 @@ impl TxCreationKeys { pub fn new(secp_ctx: &Secp256k1, per_commitment_point: &PublicKey, a_delayed_payment_base: &PublicKey, a_htlc_base: &PublicKey, b_revocation_base: &PublicKey, b_payment_base: &PublicKey, b_htlc_base: &PublicKey) -> Result { Ok(TxCreationKeys { per_commitment_point: per_commitment_point.clone(), - revocation_key: try!(derive_public_revocation_key(&secp_ctx, &per_commitment_point, &b_revocation_base)), - a_htlc_key: try!(derive_public_key(&secp_ctx, &per_commitment_point, &a_htlc_base)), - b_htlc_key: try!(derive_public_key(&secp_ctx, &per_commitment_point, &b_htlc_base)), - a_delayed_payment_key: try!(derive_public_key(&secp_ctx, &per_commitment_point, &a_delayed_payment_base)), - b_payment_key: try!(derive_public_key(&secp_ctx, &per_commitment_point, &b_payment_base)), + revocation_key: derive_public_revocation_key(&secp_ctx, &per_commitment_point, &b_revocation_base)?, + a_htlc_key: derive_public_key(&secp_ctx, &per_commitment_point, &a_htlc_base)?, + b_htlc_key: derive_public_key(&secp_ctx, &per_commitment_point, &b_htlc_base)?, + a_delayed_payment_key: derive_public_key(&secp_ctx, &per_commitment_point, &a_delayed_payment_base)?, + b_payment_key: derive_public_key(&secp_ctx, &per_commitment_point, &b_payment_base)?, }) } } diff --git a/src/ln/channel.rs b/src/ln/channel.rs index acc8c4e91..71b642211 100644 --- a/src/ln/channel.rs +++ b/src/ln/channel.rs @@ -50,25 +50,25 @@ impl ChannelKeys { let mut okm = [0; 32]; hkdf_expand(sha, &prk, b"rust-lightning funding key info", &mut okm); - let funding_key = try!(SecretKey::from_slice(&secp_ctx, &okm)); + let funding_key = SecretKey::from_slice(&secp_ctx, &okm)?; hkdf_expand(sha, &prk, b"rust-lightning revocation base key info", &mut okm); - let revocation_base_key = try!(SecretKey::from_slice(&secp_ctx, &okm)); + let revocation_base_key = SecretKey::from_slice(&secp_ctx, &okm)?; hkdf_expand(sha, &prk, b"rust-lightning payment base key info", &mut okm); - let payment_base_key = try!(SecretKey::from_slice(&secp_ctx, &okm)); + let payment_base_key = SecretKey::from_slice(&secp_ctx, &okm)?; hkdf_expand(sha, &prk, b"rust-lightning delayed payment base key info", &mut okm); - let delayed_payment_base_key = try!(SecretKey::from_slice(&secp_ctx, &okm)); + let delayed_payment_base_key = SecretKey::from_slice(&secp_ctx, &okm)?; hkdf_expand(sha, &prk, b"rust-lightning htlc base key info", &mut okm); - let htlc_base_key = try!(SecretKey::from_slice(&secp_ctx, &okm)); + let htlc_base_key = SecretKey::from_slice(&secp_ctx, &okm)?; hkdf_expand(sha, &prk, b"rust-lightning channel close key info", &mut okm); - let channel_close_key = try!(SecretKey::from_slice(&secp_ctx, &okm)); + let channel_close_key = SecretKey::from_slice(&secp_ctx, &okm)?; hkdf_expand(sha, &prk, b"rust-lightning channel monitor claim key info", &mut okm); - let channel_monitor_claim_key = try!(SecretKey::from_slice(&secp_ctx, &okm)); + let channel_monitor_claim_key = SecretKey::from_slice(&secp_ctx, &okm)?; hkdf_expand(sha, &prk, b"rust-lightning local commitment seed info", &mut okm); @@ -610,7 +610,7 @@ impl Channel { /// The result is a transaction which we can revoke ownership of (ie a "local" transaction) /// TODO Some magic rust shit to compile-time check this? fn build_local_transaction_keys(&self, commitment_number: u64) -> Result { - let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &try!(self.build_local_commitment_secret(commitment_number))).unwrap(); + let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &self.build_local_commitment_secret(commitment_number)?).unwrap(); let delayed_payment_base = PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.delayed_payment_base_key).unwrap(); let htlc_basepoint = PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.htlc_base_key).unwrap(); @@ -845,12 +845,12 @@ impl Channel { fn funding_created_signature(&mut self, sig: &Signature) -> Result<(Transaction, Signature), HandleError> { let funding_script = self.get_funding_redeemscript(); - let remote_keys = try!(self.build_remote_transaction_keys()); - let remote_initial_commitment_tx = try!(self.build_commitment_transaction(self.cur_remote_commitment_transaction_number, &remote_keys, false, false)).0; + let remote_keys = self.build_remote_transaction_keys()?; + let remote_initial_commitment_tx = self.build_commitment_transaction(self.cur_remote_commitment_transaction_number, &remote_keys, false, false)?.0; let remote_sighash = secp_call!(Message::from_slice(&bip143::SighashComponents::new(&remote_initial_commitment_tx).sighash_all(&remote_initial_commitment_tx, 0, &funding_script, self.channel_value_satoshis)[..])); - let local_keys = try!(self.build_local_transaction_keys(self.cur_local_commitment_transaction_number)); - let local_initial_commitment_tx = try!(self.build_commitment_transaction(self.cur_local_commitment_transaction_number, &local_keys, true, false)).0; + let local_keys = self.build_local_transaction_keys(self.cur_local_commitment_transaction_number)?; + let local_initial_commitment_tx = self.build_commitment_transaction(self.cur_local_commitment_transaction_number, &local_keys, true, false)?.0; let local_sighash = secp_call!(Message::from_slice(&bip143::SighashComponents::new(&local_initial_commitment_tx).sighash_all(&local_initial_commitment_tx, 0, &funding_script, self.channel_value_satoshis)[..])); // They sign the "local" commitment transaction, allowing us to broadcast the tx if we wish. @@ -911,8 +911,8 @@ impl Channel { let funding_script = self.get_funding_redeemscript(); - let local_keys = try!(self.build_local_transaction_keys(self.cur_local_commitment_transaction_number)); - let local_initial_commitment_tx = try!(self.build_commitment_transaction(self.cur_local_commitment_transaction_number, &local_keys, true, false)).0; + let local_keys = self.build_local_transaction_keys(self.cur_local_commitment_transaction_number)?; + let local_initial_commitment_tx = self.build_commitment_transaction(self.cur_local_commitment_transaction_number, &local_keys, true, false)?.0; let local_sighash = secp_call!(Message::from_slice(&bip143::SighashComponents::new(&local_initial_commitment_tx).sighash_all(&local_initial_commitment_tx, 0, &funding_script, self.channel_value_satoshis)[..])); // They sign the "local" commitment transaction, allowing us to broadcast the tx if we wish. @@ -1061,7 +1061,7 @@ impl Channel { //TODO: Need to examine the type of err - if its a fee issue or similar we may want to //fail it back the route, if its a temporary issue we can ignore it... if update_add_msgs.len() > 0 { - Ok(Some((update_add_msgs, try!(self.send_commitment())))) + Ok(Some((update_add_msgs, self.send_commitment()?))) } else { Err(err.unwrap()) } @@ -1156,8 +1156,8 @@ impl Channel { let funding_script = self.get_funding_redeemscript(); - let local_keys = try!(self.build_local_transaction_keys(self.cur_local_commitment_transaction_number)); - let local_commitment_tx = try!(self.build_commitment_transaction(self.cur_local_commitment_transaction_number, &local_keys, true, false)); + let local_keys = self.build_local_transaction_keys(self.cur_local_commitment_transaction_number)?; + let local_commitment_tx = self.build_commitment_transaction(self.cur_local_commitment_transaction_number, &local_keys, true, false)?; let local_sighash = secp_call!(Message::from_slice(&bip143::SighashComponents::new(&local_commitment_tx.0).sighash_all(&local_commitment_tx.0, 0, &funding_script, self.channel_value_satoshis)[..])); secp_call!(self.secp_ctx.verify(&local_sighash, &msg.signature, &self.their_funding_pubkey)); @@ -1166,13 +1166,13 @@ impl Channel { } for (idx, ref htlc) in local_commitment_tx.1.iter().enumerate() { - let htlc_tx = try!(self.build_htlc_transaction(&local_commitment_tx.0.txid(), htlc, true, &local_keys)); + let htlc_tx = self.build_htlc_transaction(&local_commitment_tx.0.txid(), htlc, true, &local_keys)?; let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &local_keys, htlc.offered); let htlc_sighash = secp_call!(Message::from_slice(&bip143::SighashComponents::new(&htlc_tx).sighash_all(&htlc_tx, 0, &htlc_redeemscript, htlc.amount_msat / 1000)[..])); secp_call!(self.secp_ctx.verify(&htlc_sighash, &msg.htlc_signatures[idx], &local_keys.b_htlc_key)); } - let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &try!(self.build_local_commitment_secret(self.cur_local_commitment_transaction_number - 1))).unwrap(); + let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &self.build_local_commitment_secret(self.cur_local_commitment_transaction_number - 1)?).unwrap(); let per_commitment_secret = chan_utils::build_commitment_secret(self.local_keys.commitment_seed, self.cur_local_commitment_transaction_number); //TODO: Store htlc keys in our channel_watcher @@ -1208,7 +1208,7 @@ impl Channel { if PublicKey::from_secret_key(&self.secp_ctx, &get_key!(&self.secp_ctx, &msg.per_commitment_secret)).unwrap() != self.their_cur_commitment_point { return Err(HandleError{err: "Got a revoke commitment secret which didn't correspond to their current pubkey", msg: None}); } - try!(self.channel_monitor.provide_secret(self.cur_remote_commitment_transaction_number, msg.per_commitment_secret)); + self.channel_monitor.provide_secret(self.cur_remote_commitment_transaction_number, msg.per_commitment_secret)?; // Update state now that we've passed all the can-fail calls... // (note that we may still fail to generate the new commitment_signed message, but that's @@ -1393,7 +1393,7 @@ impl Channel { panic!("Tried to send an open_channel for a channel that has already advanced"); } - let local_commitment_secret = try!(self.build_local_commitment_secret(self.cur_local_commitment_transaction_number)); + let local_commitment_secret = self.build_local_commitment_secret(self.cur_local_commitment_transaction_number)?; Ok(msgs::OpenChannel { chain_hash: chain_hash, @@ -1429,7 +1429,7 @@ impl Channel { panic!("Tried to send an accept_channel for a channel that has already advanced"); } - let local_commitment_secret = try!(self.build_local_commitment_secret(self.cur_local_commitment_transaction_number)); + let local_commitment_secret = self.build_local_commitment_secret(self.cur_local_commitment_transaction_number)?; Ok(msgs::AcceptChannel { temporary_channel_id: self.channel_id, @@ -1453,8 +1453,8 @@ impl Channel { fn get_outbound_funding_created_signature(&mut self) -> Result { let funding_script = self.get_funding_redeemscript(); - let remote_keys = try!(self.build_remote_transaction_keys()); - let remote_initial_commitment_tx = try!(self.build_commitment_transaction(self.cur_remote_commitment_transaction_number, &remote_keys, false, false)).0; + let remote_keys = self.build_remote_transaction_keys()?; + let remote_initial_commitment_tx = self.build_commitment_transaction(self.cur_remote_commitment_transaction_number, &remote_keys, false, false)?.0; let remote_sighash = secp_call!(Message::from_slice(&bip143::SighashComponents::new(&remote_initial_commitment_tx).sighash_all(&remote_initial_commitment_tx, 0, &funding_script, self.channel_value_satoshis)[..])); // We sign the "remote" commitment transaction, allowing them to broadcast the tx if they wish. @@ -1615,15 +1615,15 @@ impl Channel { let funding_script = self.get_funding_redeemscript(); - let remote_keys = try!(self.build_remote_transaction_keys()); - let remote_commitment_tx = try!(self.build_commitment_transaction(self.cur_remote_commitment_transaction_number, &remote_keys, false, true)); + let remote_keys = self.build_remote_transaction_keys()?; + let remote_commitment_tx = self.build_commitment_transaction(self.cur_remote_commitment_transaction_number, &remote_keys, false, true)?; let remote_sighash = secp_call!(Message::from_slice(&bip143::SighashComponents::new(&remote_commitment_tx.0).sighash_all(&remote_commitment_tx.0, 0, &funding_script, self.channel_value_satoshis)[..])); let our_sig = secp_call!(self.secp_ctx.sign(&remote_sighash, &self.local_keys.funding_key)); let mut htlc_sigs = Vec::new(); for ref htlc in remote_commitment_tx.1.iter() { - let htlc_tx = try!(self.build_htlc_transaction(&remote_commitment_tx.0.txid(), htlc, false, &remote_keys)); + let htlc_tx = self.build_htlc_transaction(&remote_commitment_tx.0.txid(), htlc, false, &remote_keys)?; let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &remote_keys, htlc.offered); let htlc_sighash = secp_call!(Message::from_slice(&bip143::SighashComponents::new(&htlc_tx).sighash_all(&htlc_tx, 0, &htlc_redeemscript, htlc.amount_msat / 1000)[..])); let our_htlc_key = secp_call!(chan_utils::derive_private_key(&self.secp_ctx, &remote_keys.per_commitment_point, &self.local_keys.htlc_base_key)); @@ -1645,9 +1645,9 @@ impl Channel { /// Shorthand for calling send_htlc() followed by send_commitment(), see docs on those for /// more info. pub fn send_htlc_and_commit(&mut self, amount_msat: u64, payment_hash: [u8; 32], cltv_expiry: u32, onion_routing_packet: msgs::OnionPacket) -> Result, HandleError> { - match try!(self.send_htlc(amount_msat, payment_hash, cltv_expiry, onion_routing_packet)) { + match self.send_htlc(amount_msat, payment_hash, cltv_expiry, onion_routing_packet)? { Some(update_add_htlc) => - Ok(Some((update_add_htlc, try!(self.send_commitment())))), + Ok(Some((update_add_htlc, self.send_commitment()?))), None => Ok(None) } } diff --git a/src/ln/channelmanager.rs b/src/ln/channelmanager.rs index 3d7840311..9d9f7543c 100644 --- a/src/ln/channelmanager.rs +++ b/src/ln/channelmanager.rs @@ -144,7 +144,7 @@ impl ChannelManager { pub fn create_channel(&self, their_network_key: PublicKey, channel_value_satoshis: u64, user_id: u64) -> Result { let channel = Channel::new_outbound(&*self.fee_estimator, their_network_key, channel_value_satoshis, self.announce_channels_publicly, user_id); - let res = try!(channel.get_open_channel(self.genesis_hash.clone(), &*self.fee_estimator)); + let res = channel.get_open_channel(self.genesis_hash.clone(), &*self.fee_estimator)?; let mut channels = self.channels.lock().unwrap(); match channels.by_id.insert(channel.channel_id(), channel) { Some(_) => panic!("RNG is bad???"), @@ -429,9 +429,9 @@ impl ChannelManager { let associated_data = Vec::new(); //TODO: What to put here? - let onion_keys = try!(ChannelManager::construct_onion_keys(&self.secp_ctx, route, &session_priv)); - let (onion_payloads, htlc_msat, htlc_cltv) = try!(ChannelManager::build_onion_payloads(route)); - let onion_packet = try!(ChannelManager::construct_onion_packet(onion_payloads, onion_keys, associated_data)); + let onion_keys = ChannelManager::construct_onion_keys(&self.secp_ctx, route, &session_priv)?; + let (onion_payloads, htlc_msat, htlc_cltv) = ChannelManager::build_onion_payloads(route)?; + let onion_packet = ChannelManager::construct_onion_packet(onion_payloads, onion_keys, associated_data)?; let mut channels = self.channels.lock().unwrap(); let id = match channels.short_to_id.get(&route.hops.first().unwrap().short_channel_id) { @@ -489,7 +489,7 @@ impl ChannelManager { fn get_announcement_sigs(&self, chan: &Channel) -> Result, HandleError> { if !chan.is_usable() { return Ok(None) } - let (announcement, our_bitcoin_sig) = try!(chan.get_channel_announcement(self.get_our_node_id(), self.genesis_hash.clone())); + let (announcement, our_bitcoin_sig) = chan.get_channel_announcement(self.get_our_node_id(), self.genesis_hash.clone())?; let msghash = Message::from_slice(&Sha256dHash::from_data(&announcement.encode()[..])[..]).unwrap(); let our_node_sig = secp_call!(self.secp_ctx.sign(&msghash, &self.our_network_key)); @@ -706,8 +706,8 @@ impl ChannelMessageHandler for ChannelManager { if channels.by_id.contains_key(&msg.temporary_channel_id) { return Err(HandleError{err: "temporary_channel_id collision!", msg: None}); } - let channel = try!(Channel::new_from_req(&*self.fee_estimator, their_node_id.clone(), msg, 0, self.announce_channels_publicly)); - let accept_msg = try!(channel.get_accept_channel()); + let channel = Channel::new_from_req(&*self.fee_estimator, their_node_id.clone(), msg, 0, self.announce_channels_publicly)?; + let accept_msg = channel.get_accept_channel()?; channels.by_id.insert(channel.channel_id(), channel); Ok(accept_msg) } @@ -720,7 +720,7 @@ impl ChannelMessageHandler for ChannelManager { if chan.get_their_node_id() != *their_node_id { return Err(HandleError{err: "Got a message for a channel from the wrong node!", msg: None}) } - try!(chan.accept_channel(&msg)); + chan.accept_channel(&msg)?; (chan.get_value_satoshis(), chan.get_funding_redeemscript().to_v0_p2wsh(), chan.get_user_id()) }, None => return Err(HandleError{err: "Failed to find corresponding channel", msg: None}) @@ -763,7 +763,7 @@ impl ChannelMessageHandler for ChannelManager { // channel back-to-back with funding_created, we'll end up thinking they sent a message // for a bogus channel. let chan_monitor = chan.0.channel_monitor(); - try!(self.monitor.add_update_monitor(chan_monitor.get_funding_txo().unwrap(), chan_monitor)); + self.monitor.add_update_monitor(chan_monitor.get_funding_txo().unwrap(), chan_monitor)?; let mut channels = self.channels.lock().unwrap(); channels.by_id.insert(chan.1.channel_id, chan.0); Ok(chan.1) @@ -777,7 +777,7 @@ impl ChannelMessageHandler for ChannelManager { if chan.get_their_node_id() != *their_node_id { return Err(HandleError{err: "Got a message for a channel from the wrong node!", msg: None}) } - try!(chan.funding_signed(&msg)); + chan.funding_signed(&msg)?; (chan.get_funding_txo().unwrap(), chan.get_user_id()) }, None => return Err(HandleError{err: "Failed to find corresponding channel", msg: None}) @@ -798,8 +798,8 @@ impl ChannelMessageHandler for ChannelManager { if chan.get_their_node_id() != *their_node_id { return Err(HandleError{err: "Got a message for a channel from the wrong node!", msg: None}) } - try!(chan.funding_locked(&msg)); - return Ok(try!(self.get_announcement_sigs(chan))); + chan.funding_locked(&msg)?; + return Ok(self.get_announcement_sigs(chan)?); }, None => return Err(HandleError{err: "Failed to find corresponding channel", msg: None}) }; @@ -1099,13 +1099,13 @@ impl ChannelMessageHandler for ChannelManager { if chan.get_their_node_id() != *their_node_id { return Err(HandleError{err: "Got a message for a channel from the wrong node!", msg: None}) } - (try!(chan.commitment_signed(&msg)), chan.channel_monitor()) + (chan.commitment_signed(&msg)?, chan.channel_monitor()) }, None => return Err(HandleError{err: "Failed to find corresponding channel", msg: None}) } }; //TODO: Only if we store HTLC sigs - try!(self.monitor.add_update_monitor(monitor.get_funding_txo().unwrap(), monitor)); + self.monitor.add_update_monitor(monitor.get_funding_txo().unwrap(), monitor)?; let mut forward_event = None; { @@ -1147,13 +1147,13 @@ impl ChannelMessageHandler for ChannelManager { if chan.get_their_node_id() != *their_node_id { return Err(HandleError{err: "Got a message for a channel from the wrong node!", msg: None}) } - try!(chan.revoke_and_ack(&msg)); + chan.revoke_and_ack(&msg)?; chan.channel_monitor() }, None => return Err(HandleError{err: "Failed to find corresponding channel", msg: None}) } }; - try!(self.monitor.add_update_monitor(monitor.get_funding_txo().unwrap(), monitor)); + self.monitor.add_update_monitor(monitor.get_funding_txo().unwrap(), monitor)?; Ok(()) } @@ -1183,7 +1183,7 @@ impl ChannelMessageHandler for ChannelManager { } let our_node_id = self.get_our_node_id(); - let (announcement, our_bitcoin_sig) = try!(chan.get_channel_announcement(our_node_id.clone(), self.genesis_hash.clone())); + let (announcement, our_bitcoin_sig) = chan.get_channel_announcement(our_node_id.clone(), self.genesis_hash.clone())?; let were_node_one = announcement.node_id_1 == our_node_id; let msghash = Message::from_slice(&Sha256dHash::from_data(&announcement.encode()[..])[..]).unwrap(); diff --git a/src/ln/msgs.rs b/src/ln/msgs.rs index 520fe5f0f..259f90d7d 100644 --- a/src/ln/msgs.rs +++ b/src/ln/msgs.rs @@ -477,11 +477,11 @@ impl MsgEncodable for GlobalFeatures { impl MsgDecodable for Init { fn decode(v: &[u8]) -> Result { - let global_features = try!(GlobalFeatures::decode(v)); + let global_features = GlobalFeatures::decode(v)?; if global_features.flags.len() + 4 <= v.len() { return Err(DecodeError::WrongLength); } - let local_features = try!(LocalFeatures::decode(&v[global_features.flags.len() + 2..])); + let local_features = LocalFeatures::decode(&v[global_features.flags.len() + 2..])?; if global_features.flags.len() + local_features.flags.len() + 4 != v.len() { return Err(DecodeError::WrongLength); } @@ -873,7 +873,7 @@ impl MsgDecodable for OnionHopData { hmac[..].copy_from_slice(&v[33..65]); Ok(OnionHopData { realm: realm, - data: try!(OnionRealm0HopData::decode(&v[1..33])), + data: OnionRealm0HopData::decode(&v[1..33])?, hmac: hmac, }) } diff --git a/src/ln/peer_channel_encryptor.rs b/src/ln/peer_channel_encryptor.rs index ceb9ccb78..2de38e68f 100644 --- a/src/ln/peer_channel_encryptor.rs +++ b/src/ln/peer_channel_encryptor.rs @@ -216,7 +216,7 @@ impl PeerChannelEncryptor { let temp_k = PeerChannelEncryptor::hkdf(state, ss); let mut dec = [0; 0]; - try!(PeerChannelEncryptor::decrypt_with_ad(&mut dec, 0, &temp_k, &state.h, &act[34..])); + PeerChannelEncryptor::decrypt_with_ad(&mut dec, 0, &temp_k, &state.h, &act[34..])?; sha.reset(); sha.input(&state.h); @@ -257,7 +257,7 @@ impl PeerChannelEncryptor { panic!("Requested act at wrong step"); } - let (their_pub, _) = try!(PeerChannelEncryptor::inbound_noise_act(&self.secp_ctx, bidirectional_state, act_one, &our_node_secret)); + let (their_pub, _) = PeerChannelEncryptor::inbound_noise_act(&self.secp_ctx, bidirectional_state, act_one, &our_node_secret)?; ie.get_or_insert(their_pub); re.get_or_insert(our_ephemeral); @@ -296,7 +296,7 @@ impl PeerChannelEncryptor { panic!("Requested act at wrong step"); } - let (re, temp_k2) = try!(PeerChannelEncryptor::inbound_noise_act(&self.secp_ctx, bidirectional_state, act_two, &ie)); + let (re, temp_k2) = PeerChannelEncryptor::inbound_noise_act(&self.secp_ctx, bidirectional_state, act_two, &ie)?; let mut res = [0; 66]; let our_node_id = PublicKey::from_secret_key(&self.secp_ctx, &our_node_secret).unwrap(); //TODO: nicer rng-is-bad error message @@ -359,7 +359,7 @@ impl PeerChannelEncryptor { } let mut their_node_id = [0; 33]; - try!(PeerChannelEncryptor::decrypt_with_ad(&mut their_node_id, 1, &temp_k2.unwrap(), &bidirectional_state.h, &act_three[1..50])); + PeerChannelEncryptor::decrypt_with_ad(&mut their_node_id, 1, &temp_k2.unwrap(), &bidirectional_state.h, &act_three[1..50])?; self.their_node_id = Some(match PublicKey::from_slice(&self.secp_ctx, &their_node_id) { Ok(key) => key, Err(_) => return Err(HandleError{err: "Bad node_id from peer", msg: Some(msgs::ErrorMessage::DisconnectPeer{})}), @@ -373,7 +373,7 @@ impl PeerChannelEncryptor { let ss = SharedSecret::new(&self.secp_ctx, &self.their_node_id.unwrap(), &re.unwrap()); let temp_k = PeerChannelEncryptor::hkdf(bidirectional_state, ss); - try!(PeerChannelEncryptor::decrypt_with_ad(&mut [0; 0], 0, &temp_k, &bidirectional_state.h, &act_three[50..])); + PeerChannelEncryptor::decrypt_with_ad(&mut [0; 0], 0, &temp_k, &bidirectional_state.h, &act_three[50..])?; sha.reset(); let mut prk = [0; 32]; @@ -459,7 +459,7 @@ impl PeerChannelEncryptor { } let mut res = [0; 2]; - try!(Self::decrypt_with_ad(&mut res, *rn, rk, &[0; 0], msg)); + Self::decrypt_with_ad(&mut res, *rn, rk, &[0; 0], msg)?; *rn += 1; Ok(byte_utils::slice_to_be16(&res)) }, @@ -478,7 +478,7 @@ impl PeerChannelEncryptor { NoiseState::Finished { sk: _, sn: _, sck: _, ref rk, ref mut rn, rck: _ } => { let mut res = Vec::with_capacity(msg.len() - 16); res.resize(msg.len() - 16, 0); - try!(Self::decrypt_with_ad(&mut res[..], *rn, rk, &[0; 0], msg)); + Self::decrypt_with_ad(&mut res[..], *rn, rk, &[0; 0], msg)?; *rn += 1; Ok(res)