From e93c9fbeaf6312fb77bce1ad96a2abb44124f136 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sun, 22 Jul 2018 18:19:28 -0400 Subject: [PATCH] Migrate all Uint256s used for channel_ids to [u8; 32] --- fuzz/fuzz_targets/full_stack_target.rs | 3 +- src/chain/transaction.rs | 34 +++++-- src/ln/channel.rs | 7 +- src/ln/channelmanager.rs | 26 +++--- src/ln/msgs.rs | 118 +++++++++++++++---------- src/util/events.rs | 3 +- src/util/rng.rs | 19 ++-- 7 files changed, 130 insertions(+), 80 deletions(-) diff --git a/fuzz/fuzz_targets/full_stack_target.rs b/fuzz/fuzz_targets/full_stack_target.rs index 79bc8581c..bc9e23472 100644 --- a/fuzz/fuzz_targets/full_stack_target.rs +++ b/fuzz/fuzz_targets/full_stack_target.rs @@ -9,7 +9,6 @@ use bitcoin::blockdata::script::Script; use bitcoin::network::constants::Network; use bitcoin::network::serialize::{serialize, BitcoinHash}; use bitcoin::util::hash::Sha256dHash; -use bitcoin::util::uint::Uint256; use crypto::sha2::Sha256; use crypto::digest::Digest; @@ -168,7 +167,7 @@ pub fn do_test(data: &[u8]) { let mut should_forward = false; let mut payments_received = Vec::new(); let mut payments_sent = 0; - let mut pending_funding_generation: Vec<(Uint256, u64, Script)> = Vec::new(); + let mut pending_funding_generation: Vec<([u8; 32], u64, Script)> = Vec::new(); let mut pending_funding_signatures = HashMap::new(); let mut pending_funding_relay = Vec::new(); diff --git a/src/chain/transaction.rs b/src/chain/transaction.rs index 42a4f952e..934c77272 100644 --- a/src/chain/transaction.rs +++ b/src/chain/transaction.rs @@ -1,5 +1,4 @@ use bitcoin::util::hash::Sha256dHash; -use bitcoin::util::uint::Uint256; /// A reference to a transaction output. /// Differs from bitcoin::blockdata::transaction::TxOutRef as the index is a u16 instead of usize @@ -19,10 +18,33 @@ impl OutPoint { } /// Convert an `OutPoint` to a lightning channel id. - pub fn to_channel_id(&self) -> Uint256 { - let mut index = [0; 32]; - index[30] = ((self.index >> 8) & 0xff) as u8; - index[31] = ((self.index >> 0) & 0xff) as u8; - self.txid.into_le() ^ Sha256dHash::from(&index[..]).into_le() + pub fn to_channel_id(&self) -> [u8; 32] { + let mut res = [0; 32]; + res[..].copy_from_slice(&self.txid[..]); + res[30] ^= ((self.index >> 8) & 0xff) as u8; + res[31] ^= ((self.index >> 0) & 0xff) as u8; + res + } +} + +#[cfg(test)] +mod tests { + use chain::transaction::OutPoint; + + use bitcoin::blockdata::transaction::Transaction; + use bitcoin::network::serialize; + use bitcoin::util::misc::hex_bytes; + + #[test] + fn test_channel_id_calculation() { + let tx: Transaction = serialize::deserialize(&hex_bytes("020000000001010e0adef48412e4361325ac1c6e36411299ab09d4f083b9d8ddb55fbc06e1b0c00000000000feffffff0220a1070000000000220020f81d95e040bd0a493e38bae27bff52fe2bb58b93b293eb579c01c31b05c5af1dc072cfee54a3000016001434b1d6211af5551905dc2642d05f5b04d25a8fe80247304402207f570e3f0de50546aad25a872e3df059d277e776dda4269fa0d2cc8c2ee6ec9a022054e7fae5ca94d47534c86705857c24ceea3ad51c69dd6051c5850304880fc43a012103cb11a1bacc223d98d91f1946c6752e358a5eb1a1c983b3e6fb15378f453b76bd00000000").unwrap()[..]).unwrap(); + assert_eq!(&OutPoint { + txid: tx.txid(), + index: 0 + }.to_channel_id(), &hex_bytes("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25e").unwrap()[..]); + assert_eq!(&OutPoint { + txid: tx.txid(), + index: 1 + }.to_channel_id(), &hex_bytes("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25f").unwrap()[..]); } } diff --git a/src/ln/channel.rs b/src/ln/channel.rs index d4d264b0a..7cc0dea5f 100644 --- a/src/ln/channel.rs +++ b/src/ln/channel.rs @@ -2,7 +2,6 @@ use bitcoin::blockdata::block::BlockHeader; use bitcoin::blockdata::script::{Script,Builder}; use bitcoin::blockdata::transaction::{TxIn, TxOut, Transaction, SigHashType}; use bitcoin::blockdata::opcodes; -use bitcoin::util::uint::Uint256; use bitcoin::util::hash::{Sha256dHash, Hash160}; use bitcoin::util::bip143; use bitcoin::network::serialize::BitcoinHash; @@ -236,7 +235,7 @@ const BOTH_SIDES_SHUTDOWN_MASK: u32 = (ChannelState::LocalShutdownSent as u32 | pub struct Channel { user_id: u64, - channel_id: Uint256, + channel_id: [u8; 32], channel_state: u32, channel_outbound: bool, secp_ctx: Secp256k1, @@ -380,7 +379,7 @@ impl Channel { Channel { user_id: user_id, - channel_id: rng::rand_uint256(), + channel_id: rng::rand_u832(), channel_state: ChannelState::OurInitSent as u32, channel_outbound: true, secp_ctx: secp_ctx, @@ -1798,7 +1797,7 @@ impl Channel { // Public utilities: - pub fn channel_id(&self) -> Uint256 { + pub fn channel_id(&self) -> [u8; 32] { self.channel_id } diff --git a/src/ln/channelmanager.rs b/src/ln/channelmanager.rs index f4f9ef881..6494153d6 100644 --- a/src/ln/channelmanager.rs +++ b/src/ln/channelmanager.rs @@ -4,7 +4,6 @@ use bitcoin::blockdata::constants::genesis_block; use bitcoin::network::constants::Network; use bitcoin::network::serialize::BitcoinHash; use bitcoin::util::hash::Sha256dHash; -use bitcoin::util::uint::Uint256; use secp256k1::key::{SecretKey,PublicKey}; use secp256k1::{Secp256k1,Message}; @@ -111,16 +110,16 @@ enum PendingOutboundHTLC { const MIN_HTLC_RELAY_HOLDING_CELL_MILLIS: u32 = 50; struct ChannelHolder { - by_id: HashMap, - short_to_id: HashMap, + by_id: HashMap<[u8; 32], Channel>, + short_to_id: HashMap, next_forward: Instant, /// short channel id -> forward infos. Key of 0 means payments received forward_htlcs: HashMap>, claimable_htlcs: HashMap<[u8; 32], PendingOutboundHTLC>, } struct MutChannelHolder<'a> { - by_id: &'a mut HashMap, - short_to_id: &'a mut HashMap, + by_id: &'a mut HashMap<[u8; 32], Channel>, + short_to_id: &'a mut HashMap, next_forward: &'a mut Instant, /// short channel id -> forward infos. Key of 0 means payments received forward_htlcs: &'a mut HashMap>, @@ -187,7 +186,7 @@ pub struct ChannelDetails { /// thereafter this is the txid of the funding transaction xor the funding transaction output). /// Note that this means this value is *not* persistent - it can change once during the /// lifetime of the channel. - pub channel_id: Uint256, + pub channel_id: [u8; 32], /// The position of the funding transaction in the chain. None if the funding transaction has /// not yet been confirmed and the channel fully opened. pub short_channel_id: Option, @@ -297,7 +296,7 @@ impl ChannelManager { /// Begins the process of closing a channel. After this call (plus some timeout), no new HTLCs /// will be accepted on the given channel, and after additional timeout/the closing of all /// pending HTLCs, the channel will be closed on chain. - pub fn close_channel(&self, channel_id: &Uint256) -> Result { + pub fn close_channel(&self, channel_id: &[u8; 32]) -> Result { let (res, chan_option) = { let mut channel_state_lock = self.channel_state.lock().unwrap(); let channel_state = channel_state_lock.borrow_parts(); @@ -676,10 +675,10 @@ impl ChannelManager { /// Call this upon creation of a funding transaction for the given channel. /// Panics if a funding transaction has already been provided for this channel. - pub fn funding_transaction_generated(&self, temporary_channel_id: &Uint256, funding_txo: OutPoint) { + pub fn funding_transaction_generated(&self, temporary_channel_id: &[u8; 32], funding_txo: OutPoint) { let (chan, msg, chan_monitor) = { let mut channel_state = self.channel_state.lock().unwrap(); - match channel_state.by_id.remove(&temporary_channel_id) { + match channel_state.by_id.remove(temporary_channel_id) { Some(mut chan) => { match chan.get_outbound_funding_created(funding_txo) { Ok(funding_msg) => { @@ -1838,7 +1837,6 @@ mod tests { use bitcoin::util::misc::hex_bytes; use bitcoin::util::hash::Sha256dHash; - use bitcoin::util::uint::Uint256; use bitcoin::blockdata::block::{Block, BlockHeader}; use bitcoin::blockdata::transaction::{Transaction, TxOut}; use bitcoin::network::constants::Network; @@ -2030,7 +2028,7 @@ mod tests { } static mut CHAN_COUNT: u32 = 0; - fn create_chan_between_nodes(node_a: &Node, node_b: &Node) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, Uint256, Transaction) { + fn create_chan_between_nodes(node_a: &Node, node_b: &Node) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) { node_a.node.create_channel(node_b.node.get_our_node_id(), 100000, 42).unwrap(); let events_1 = node_a.node.get_and_clear_pending_events(); @@ -2158,7 +2156,7 @@ mod tests { ((*announcement).clone(), (*as_update).clone(), (*bs_update).clone(), channel_id, tx) } - fn create_announced_chan_between_nodes(nodes: &Vec, a: usize, b: usize) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, Uint256, Transaction) { + fn create_announced_chan_between_nodes(nodes: &Vec, a: usize, b: usize) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) { let chan_announcement = create_chan_between_nodes(&nodes[a], &nodes[b]); for node in nodes { assert!(node.router.handle_channel_announcement(&chan_announcement.0).unwrap()); @@ -2168,7 +2166,7 @@ mod tests { (chan_announcement.1, chan_announcement.2, chan_announcement.3, chan_announcement.4) } - fn close_channel(outbound_node: &Node, inbound_node: &Node, channel_id: &Uint256, funding_tx: Transaction, close_inbound_first: bool) -> (msgs::ChannelUpdate, msgs::ChannelUpdate) { + fn close_channel(outbound_node: &Node, inbound_node: &Node, channel_id: &[u8; 32], funding_tx: Transaction, close_inbound_first: bool) -> (msgs::ChannelUpdate, msgs::ChannelUpdate) { let (node_a, broadcaster_a) = if close_inbound_first { (&inbound_node.node, &inbound_node.tx_broadcaster) } else { (&outbound_node.node, &outbound_node.tx_broadcaster) }; let (node_b, broadcaster_b) = if close_inbound_first { (&outbound_node.node, &outbound_node.tx_broadcaster) } else { (&inbound_node.node, &inbound_node.tx_broadcaster) }; let (tx_a, tx_b); @@ -2679,7 +2677,7 @@ mod tests { #[derive(PartialEq)] enum HTLCType { NONE, TIMEOUT, SUCCESS } - fn test_txn_broadcast(node: &Node, chan: &(msgs::ChannelUpdate, msgs::ChannelUpdate, Uint256, Transaction), commitment_tx: Option, has_htlc_tx: HTLCType) -> Vec { + fn test_txn_broadcast(node: &Node, chan: &(msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction), commitment_tx: Option, has_htlc_tx: HTLCType) -> Vec { let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap(); assert!(node_txn.len() >= if commitment_tx.is_some() { 0 } else { 1 } + if has_htlc_tx == HTLCType::NONE { 0 } else { 1 }); diff --git a/src/ln/msgs.rs b/src/ln/msgs.rs index 3a0719854..a36202e46 100644 --- a/src/ln/msgs.rs +++ b/src/ln/msgs.rs @@ -1,6 +1,5 @@ use secp256k1::key::PublicKey; use secp256k1::{Secp256k1, Signature}; -use bitcoin::util::uint::Uint256; use bitcoin::util::hash::Sha256dHash; use bitcoin::network::serialize::{deserialize,serialize}; use bitcoin::blockdata::script::Script; @@ -150,7 +149,7 @@ pub struct Pong { pub struct OpenChannel { pub chain_hash: Sha256dHash, - pub temporary_channel_id: Uint256, + pub temporary_channel_id: [u8; 32], pub funding_satoshis: u64, pub push_msat: u64, pub dust_limit_satoshis: u64, @@ -171,7 +170,7 @@ pub struct OpenChannel { } pub struct AcceptChannel { - pub temporary_channel_id: Uint256, + pub temporary_channel_id: [u8; 32], pub dust_limit_satoshis: u64, pub max_htlc_value_in_flight_msat: u64, pub channel_reserve_satoshis: u64, @@ -189,36 +188,36 @@ pub struct AcceptChannel { } pub struct FundingCreated { - pub temporary_channel_id: Uint256, + pub temporary_channel_id: [u8; 32], pub funding_txid: Sha256dHash, pub funding_output_index: u16, pub signature: Signature, } pub struct FundingSigned { - pub channel_id: Uint256, + pub channel_id: [u8; 32], pub signature: Signature, } pub struct FundingLocked { - pub channel_id: Uint256, + pub channel_id: [u8; 32], pub next_per_commitment_point: PublicKey, } pub struct Shutdown { - pub channel_id: Uint256, + pub channel_id: [u8; 32], pub scriptpubkey: Script, } pub struct ClosingSigned { - pub channel_id: Uint256, + pub channel_id: [u8; 32], pub fee_satoshis: u64, pub signature: Signature, } #[derive(Clone)] pub struct UpdateAddHTLC { - pub channel_id: Uint256, + pub channel_id: [u8; 32], pub htlc_id: u64, pub amount_msat: u64, pub payment_hash: [u8; 32], @@ -228,21 +227,21 @@ pub struct UpdateAddHTLC { #[derive(Clone)] pub struct UpdateFulfillHTLC { - pub channel_id: Uint256, + pub channel_id: [u8; 32], pub htlc_id: u64, pub payment_preimage: [u8; 32], } #[derive(Clone)] pub struct UpdateFailHTLC { - pub channel_id: Uint256, + pub channel_id: [u8; 32], pub htlc_id: u64, pub reason: OnionErrorPacket, } #[derive(Clone)] pub struct UpdateFailMalformedHTLC { - pub channel_id: Uint256, + pub channel_id: [u8; 32], pub htlc_id: u64, pub sha256_of_onion: [u8; 32], pub failure_code: u16, @@ -250,24 +249,24 @@ pub struct UpdateFailMalformedHTLC { #[derive(Clone)] pub struct CommitmentSigned { - pub channel_id: Uint256, + pub channel_id: [u8; 32], pub signature: Signature, pub htlc_signatures: Vec, } pub struct RevokeAndACK { - pub channel_id: Uint256, + pub channel_id: [u8; 32], pub per_commitment_secret: [u8; 32], pub next_per_commitment_point: PublicKey, } pub struct UpdateFee { - pub channel_id: Uint256, + pub channel_id: [u8; 32], pub feerate_per_kw: u32, } pub struct ChannelReestablish { - pub channel_id: Uint256, + pub channel_id: [u8; 32], pub next_local_commitment_number: u64, pub next_remote_commitment_number: u64, pub your_last_per_commitment_secret: Option<[u8; 32]>, @@ -276,7 +275,7 @@ pub struct ChannelReestablish { #[derive(Clone)] pub struct AnnouncementSignatures { - pub channel_id: Uint256, + pub channel_id: [u8; 32], pub short_channel_id: u64, pub node_signature: Signature, pub bitcoin_signature: Signature, @@ -727,8 +726,10 @@ impl MsgDecodable for AcceptChannel { return Err(DecodeError::WrongLength); } + let mut temporary_channel_id = [0; 32]; + temporary_channel_id[..].copy_from_slice(&v[0..32]); Ok(Self { - temporary_channel_id: deserialize(&v[0..32]).unwrap(), + temporary_channel_id, dust_limit_satoshis: byte_utils::slice_to_be64(&v[32..40]), max_htlc_value_in_flight_msat: byte_utils::slice_to_be64(&v[40..48]), channel_reserve_satoshis: byte_utils::slice_to_be64(&v[48..56]), @@ -752,7 +753,7 @@ impl MsgEncodable for AcceptChannel { &Some(ref script) => Vec::with_capacity(270 + 2 + script.len()), &None => Vec::with_capacity(270), }; - res.extend_from_slice(&serialize(&self.temporary_channel_id).unwrap()[..]); + res.extend_from_slice(&self.temporary_channel_id); res.extend_from_slice(&byte_utils::be64_to_array(self.dust_limit_satoshis)); res.extend_from_slice(&byte_utils::be64_to_array(self.max_htlc_value_in_flight_msat)); res.extend_from_slice(&byte_utils::be64_to_array(self.channel_reserve_satoshis)); @@ -780,8 +781,10 @@ impl MsgDecodable for FundingCreated { return Err(DecodeError::WrongLength); } let ctx = Secp256k1::without_caps(); + let mut temporary_channel_id = [0; 32]; + temporary_channel_id[..].copy_from_slice(&v[0..32]); Ok(Self { - temporary_channel_id: deserialize(&v[0..32]).unwrap(), + temporary_channel_id, funding_txid: deserialize(&v[32..64]).unwrap(), funding_output_index: byte_utils::slice_to_be16(&v[64..66]), signature: secp_signature!(&ctx, &v[66..130]), @@ -791,7 +794,7 @@ impl MsgDecodable for FundingCreated { impl MsgEncodable for FundingCreated { fn encode(&self) -> Vec { let mut res = Vec::with_capacity(32+32+2+64); - res.extend_from_slice(&serialize(&self.temporary_channel_id).unwrap()[..]); + res.extend_from_slice(&self.temporary_channel_id); res.extend_from_slice(&serialize(&self.funding_txid).unwrap()[..]); res.extend_from_slice(&byte_utils::be16_to_array(self.funding_output_index)); let secp_ctx = Secp256k1::without_caps(); @@ -806,8 +809,10 @@ impl MsgDecodable for FundingSigned { return Err(DecodeError::WrongLength); } let ctx = Secp256k1::without_caps(); + let mut channel_id = [0; 32]; + channel_id[..].copy_from_slice(&v[0..32]); Ok(Self { - channel_id: deserialize(&v[0..32]).unwrap(), + channel_id, signature: secp_signature!(&ctx, &v[32..96]), }) } @@ -815,7 +820,7 @@ impl MsgDecodable for FundingSigned { impl MsgEncodable for FundingSigned { fn encode(&self) -> Vec { let mut res = Vec::with_capacity(32+64); - res.extend_from_slice(&serialize(&self.channel_id).unwrap()); + res.extend_from_slice(&self.channel_id); res.extend_from_slice(&self.signature.serialize_compact(&Secp256k1::without_caps())); res } @@ -827,8 +832,10 @@ impl MsgDecodable for FundingLocked { return Err(DecodeError::WrongLength); } let ctx = Secp256k1::without_caps(); + let mut channel_id = [0; 32]; + channel_id[..].copy_from_slice(&v[0..32]); Ok(Self { - channel_id: deserialize(&v[0..32]).unwrap(), + channel_id, next_per_commitment_point: secp_pubkey!(&ctx, &v[32..65]), }) } @@ -836,7 +843,7 @@ impl MsgDecodable for FundingLocked { impl MsgEncodable for FundingLocked { fn encode(&self) -> Vec { let mut res = Vec::with_capacity(32+33); - res.extend_from_slice(&serialize(&self.channel_id).unwrap()); + res.extend_from_slice(&self.channel_id); res.extend_from_slice(&self.next_per_commitment_point.serialize()); res } @@ -851,8 +858,10 @@ impl MsgDecodable for Shutdown { if v.len() < 32 + 2 + scriptlen { return Err(DecodeError::WrongLength); } + let mut channel_id = [0; 32]; + channel_id[..].copy_from_slice(&v[0..32]); Ok(Self { - channel_id: deserialize(&v[0..32]).unwrap(), + channel_id, scriptpubkey: Script::from(v[34..34 + scriptlen].to_vec()), }) } @@ -860,7 +869,7 @@ impl MsgDecodable for Shutdown { impl MsgEncodable for Shutdown { fn encode(&self) -> Vec { let mut res = Vec::with_capacity(32 + 2 + self.scriptpubkey.len()); - res.extend_from_slice(&serialize(&self.channel_id).unwrap()); + res.extend_from_slice(&self.channel_id); res.extend_from_slice(&byte_utils::be16_to_array(self.scriptpubkey.len() as u16)); res.extend_from_slice(&self.scriptpubkey[..]); res @@ -873,8 +882,10 @@ impl MsgDecodable for ClosingSigned { return Err(DecodeError::WrongLength); } let secp_ctx = Secp256k1::without_caps(); + let mut channel_id = [0; 32]; + channel_id[..].copy_from_slice(&v[0..32]); Ok(Self { - channel_id: deserialize(&v[0..32]).unwrap(), + channel_id, fee_satoshis: byte_utils::slice_to_be64(&v[32..40]), signature: secp_signature!(&secp_ctx, &v[40..104]), }) @@ -883,7 +894,7 @@ impl MsgDecodable for ClosingSigned { impl MsgEncodable for ClosingSigned { fn encode(&self) -> Vec { let mut res = Vec::with_capacity(32+8+64); - res.extend_from_slice(&serialize(&self.channel_id).unwrap()); + res.extend_from_slice(&self.channel_id); res.extend_from_slice(&byte_utils::be64_to_array(self.fee_satoshis)); let secp_ctx = Secp256k1::without_caps(); res.extend_from_slice(&self.signature.serialize_compact(&secp_ctx)); @@ -896,10 +907,12 @@ impl MsgDecodable for UpdateAddHTLC { if v.len() < 32+8+8+32+4+1+33+20*65+32 { return Err(DecodeError::WrongLength); } + let mut channel_id = [0; 32]; + channel_id[..].copy_from_slice(&v[0..32]); let mut payment_hash = [0; 32]; payment_hash.copy_from_slice(&v[48..80]); Ok(Self{ - channel_id: deserialize(&v[0..32]).unwrap(), + channel_id, htlc_id: byte_utils::slice_to_be64(&v[32..40]), amount_msat: byte_utils::slice_to_be64(&v[40..48]), payment_hash, @@ -911,7 +924,7 @@ impl MsgDecodable for UpdateAddHTLC { impl MsgEncodable for UpdateAddHTLC { fn encode(&self) -> Vec { let mut res = Vec::with_capacity(32+8+8+32+4+1+1366); - res.extend_from_slice(&serialize(&self.channel_id).unwrap()); + res.extend_from_slice(&self.channel_id); res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_id)); res.extend_from_slice(&byte_utils::be64_to_array(self.amount_msat)); res.extend_from_slice(&self.payment_hash); @@ -926,10 +939,12 @@ impl MsgDecodable for UpdateFulfillHTLC { if v.len() < 32+8+32 { return Err(DecodeError::WrongLength); } + let mut channel_id = [0; 32]; + channel_id[..].copy_from_slice(&v[0..32]); let mut payment_preimage = [0; 32]; payment_preimage.copy_from_slice(&v[40..72]); Ok(Self{ - channel_id: deserialize(&v[0..32]).unwrap(), + channel_id, htlc_id: byte_utils::slice_to_be64(&v[32..40]), payment_preimage, }) @@ -938,7 +953,7 @@ impl MsgDecodable for UpdateFulfillHTLC { impl MsgEncodable for UpdateFulfillHTLC { fn encode(&self) -> Vec { let mut res = Vec::with_capacity(32+8+32); - res.extend_from_slice(&serialize(&self.channel_id).unwrap()); + res.extend_from_slice(&self.channel_id); res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_id)); res.extend_from_slice(&self.payment_preimage); res @@ -950,8 +965,10 @@ impl MsgDecodable for UpdateFailHTLC { if v.len() < 32+8 { return Err(DecodeError::WrongLength); } + let mut channel_id = [0; 32]; + channel_id[..].copy_from_slice(&v[0..32]); Ok(Self{ - channel_id: deserialize(&v[0..32]).unwrap(), + channel_id, htlc_id: byte_utils::slice_to_be64(&v[32..40]), reason: OnionErrorPacket::decode(&v[40..])?, }) @@ -961,7 +978,7 @@ impl MsgEncodable for UpdateFailHTLC { fn encode(&self) -> Vec { let reason = self.reason.encode(); let mut res = Vec::with_capacity(32+8+reason.len()); - res.extend_from_slice(&serialize(&self.channel_id).unwrap()); + res.extend_from_slice(&self.channel_id); res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_id)); res.extend_from_slice(&reason[..]); res @@ -973,10 +990,12 @@ impl MsgDecodable for UpdateFailMalformedHTLC { if v.len() < 32+8+32+2 { return Err(DecodeError::WrongLength); } + let mut channel_id = [0; 32]; + channel_id[..].copy_from_slice(&v[0..32]); let mut sha256_of_onion = [0; 32]; sha256_of_onion.copy_from_slice(&v[40..72]); Ok(Self{ - channel_id: deserialize(&v[0..32]).unwrap(), + channel_id, htlc_id: byte_utils::slice_to_be64(&v[32..40]), sha256_of_onion, failure_code: byte_utils::slice_to_be16(&v[72..74]), @@ -986,7 +1005,7 @@ impl MsgDecodable for UpdateFailMalformedHTLC { impl MsgEncodable for UpdateFailMalformedHTLC { fn encode(&self) -> Vec { let mut res = Vec::with_capacity(32+8+32+2); - res.extend_from_slice(&serialize(&self.channel_id).unwrap()); + res.extend_from_slice(&self.channel_id); res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_id)); res.extend_from_slice(&self.sha256_of_onion); res.extend_from_slice(&byte_utils::be16_to_array(self.failure_code)); @@ -999,6 +1018,9 @@ impl MsgDecodable for CommitmentSigned { if v.len() < 32+64+2 { return Err(DecodeError::WrongLength); } + let mut channel_id = [0; 32]; + channel_id[..].copy_from_slice(&v[0..32]); + let htlcs = byte_utils::slice_to_be16(&v[96..98]) as usize; if v.len() < 32+64+2+htlcs*64 { return Err(DecodeError::WrongLength); @@ -1009,7 +1031,7 @@ impl MsgDecodable for CommitmentSigned { htlc_signatures.push(secp_signature!(&secp_ctx, &v[98+i*64..98+(i+1)*64])); } Ok(Self { - channel_id: deserialize(&v[0..32]).unwrap(), + channel_id, signature: secp_signature!(&secp_ctx, &v[32..96]), htlc_signatures, }) @@ -1018,7 +1040,7 @@ impl MsgDecodable for CommitmentSigned { impl MsgEncodable for CommitmentSigned { fn encode(&self) -> Vec { let mut res = Vec::with_capacity(32+64+2+self.htlc_signatures.len()*64); - res.extend_from_slice(&serialize(&self.channel_id).unwrap()); + res.extend_from_slice(&self.channel_id); let secp_ctx = Secp256k1::without_caps(); res.extend_from_slice(&self.signature.serialize_compact(&secp_ctx)); res.extend_from_slice(&byte_utils::be16_to_array(self.htlc_signatures.len() as u16)); @@ -1034,11 +1056,13 @@ impl MsgDecodable for RevokeAndACK { if v.len() < 32+32+33 { return Err(DecodeError::WrongLength); } + let mut channel_id = [0; 32]; + channel_id[..].copy_from_slice(&v[0..32]); let mut per_commitment_secret = [0; 32]; per_commitment_secret.copy_from_slice(&v[32..64]); let secp_ctx = Secp256k1::without_caps(); Ok(Self { - channel_id: deserialize(&v[0..32]).unwrap(), + channel_id, per_commitment_secret, next_per_commitment_point: secp_pubkey!(&secp_ctx, &v[64..97]), }) @@ -1047,7 +1071,7 @@ impl MsgDecodable for RevokeAndACK { impl MsgEncodable for RevokeAndACK { fn encode(&self) -> Vec { let mut res = Vec::with_capacity(32+32+33); - res.extend_from_slice(&serialize(&self.channel_id).unwrap()); + res.extend_from_slice(&self.channel_id); res.extend_from_slice(&self.per_commitment_secret); res.extend_from_slice(&self.next_per_commitment_point.serialize()); res @@ -1059,8 +1083,10 @@ impl MsgDecodable for UpdateFee { if v.len() < 32+4 { return Err(DecodeError::WrongLength); } + let mut channel_id = [0; 32]; + channel_id[..].copy_from_slice(&v[0..32]); Ok(Self { - channel_id: deserialize(&v[0..32]).unwrap(), + channel_id, feerate_per_kw: byte_utils::slice_to_be32(&v[32..36]), }) } @@ -1068,7 +1094,7 @@ impl MsgDecodable for UpdateFee { impl MsgEncodable for UpdateFee { fn encode(&self) -> Vec { let mut res = Vec::with_capacity(32+4); - res.extend_from_slice(&serialize(&self.channel_id).unwrap()); + res.extend_from_slice(&self.channel_id); res.extend_from_slice(&byte_utils::be32_to_array(self.feerate_per_kw)); res } @@ -1091,8 +1117,10 @@ impl MsgDecodable for AnnouncementSignatures { return Err(DecodeError::WrongLength); } let secp_ctx = Secp256k1::without_caps(); + let mut channel_id = [0; 32]; + channel_id[..].copy_from_slice(&v[0..32]); Ok(Self { - channel_id: deserialize(&v[0..32]).unwrap(), + channel_id, short_channel_id: byte_utils::slice_to_be64(&v[32..40]), node_signature: secp_signature!(&secp_ctx, &v[40..104]), bitcoin_signature: secp_signature!(&secp_ctx, &v[104..168]), @@ -1102,7 +1130,7 @@ impl MsgDecodable for AnnouncementSignatures { impl MsgEncodable for AnnouncementSignatures { fn encode(&self) -> Vec { let mut res = Vec::with_capacity(32+8+64*2); - res.extend_from_slice(&serialize(&self.channel_id).unwrap()); + res.extend_from_slice(&self.channel_id); res.extend_from_slice(&byte_utils::be64_to_array(self.short_channel_id)); let secp_ctx = Secp256k1::without_caps(); res.extend_from_slice(&self.node_signature.serialize_compact(&secp_ctx)); diff --git a/src/util/events.rs b/src/util/events.rs index 0c3332f99..b090fba40 100644 --- a/src/util/events.rs +++ b/src/util/events.rs @@ -2,7 +2,6 @@ use ln::msgs; use chain::transaction::OutPoint; use bitcoin::blockdata::script::Script; -use bitcoin::util::uint::Uint256; use secp256k1::key::PublicKey; @@ -14,7 +13,7 @@ pub enum Event { /// parameters and then call ChannelManager::funding_transaction_generated. /// Generated in ChannelManager message handling. FundingGenerationReady { - temporary_channel_id: Uint256, + temporary_channel_id: [u8; 32], channel_value_satoshis: u64, output_script: Script, /// The value passed in to ChannelManager::create_channel diff --git a/src/util/rng.rs b/src/util/rng.rs index 818528b8e..ae50154d9 100644 --- a/src/util/rng.rs +++ b/src/util/rng.rs @@ -1,16 +1,16 @@ #[cfg(not(feature = "fuzztarget"))] mod real_rng { use rand::{thread_rng,Rng}; - use bitcoin::util::uint::Uint256; pub fn fill_bytes(data: &mut [u8]) { let mut rng = thread_rng(); rng.fill_bytes(data); } - pub fn rand_uint256() -> Uint256 { - let mut rng = thread_rng(); - Uint256([rng.gen(), rng.gen(), rng.gen(), rng.gen()]) + pub fn rand_u832() -> [u8; 32] { + let mut res = [0; 32]; + fill_bytes(&mut res); + res } pub fn rand_f32() -> f32 { @@ -23,7 +23,6 @@ pub use self::real_rng::*; #[cfg(feature = "fuzztarget")] mod fuzzy_rng { - use bitcoin::util::uint::Uint256; use util::byte_utils; static mut RNG_ITER: u64 = 0; @@ -38,9 +37,15 @@ mod fuzzy_rng { data[off..].copy_from_slice(&byte_utils::be64_to_array(rng)[0..rem]); } - pub fn rand_uint256() -> Uint256 { + pub fn rand_u832() -> [u8; 32] { let rng = unsafe { RNG_ITER += 1; RNG_ITER - 1 }; - Uint256([rng, rng, rng, rng]) + let mut res = [0; 32]; + let data = byte_utils::le64_to_array(rng); + res[8*0..8*1].copy_from_slice(&data); + res[8*1..8*2].copy_from_slice(&data); + res[8*2..8*3].copy_from_slice(&data); + res[8*3..8*4].copy_from_slice(&data); + res } pub fn rand_f32() -> f32 { -- 2.39.5