From: Matt Corallo Date: Sun, 16 Sep 2018 18:03:36 +0000 (-0400) Subject: Drop MsgEncodable in favor of default fns on Writeable X-Git-Tag: v0.0.12~305^2 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=refs%2Fheads%2F2018-09-ser-rework-2;p=rust-lightning Drop MsgEncodable in favor of default fns on Writeable --- diff --git a/src/ln/channel.rs b/src/ln/channel.rs index c8bf624c9..bc1e8b45d 100644 --- a/src/ln/channel.rs +++ b/src/ln/channel.rs @@ -14,7 +14,7 @@ use crypto::digest::Digest; use crypto::hkdf::{hkdf_extract,hkdf_expand}; use ln::msgs; -use ln::msgs::{ErrorAction, HandleError, MsgEncodable}; +use ln::msgs::{ErrorAction, HandleError}; use ln::channelmonitor::ChannelMonitor; use ln::channelmanager::{PendingHTLCStatus, HTLCSource, PendingForwardHTLCInfo, HTLCFailReason, HTLCFailureMsg}; use ln::chan_utils::{TxCreationKeys,HTLCOutputInCommitment,HTLC_SUCCESS_TX_WEIGHT,HTLC_TIMEOUT_TX_WEIGHT}; @@ -22,6 +22,7 @@ use ln::chan_utils; use chain::chaininterface::{FeeEstimator,ConfirmationTarget}; use chain::transaction::OutPoint; use util::{transaction_utils,rng}; +use util::ser::Writeable; use util::sha2::Sha256; use util::logger::Logger; use util::errors::APIError; diff --git a/src/ln/channelmanager.rs b/src/ln/channelmanager.rs index fd156a9ae..fbf4b1128 100644 --- a/src/ln/channelmanager.rs +++ b/src/ln/channelmanager.rs @@ -16,10 +16,10 @@ use ln::channel::{Channel, ChannelKeys}; use ln::channelmonitor::ManyChannelMonitor; use ln::router::{Route,RouteHop}; use ln::msgs; -use ln::msgs::{HandleError,ChannelMessageHandler,MsgEncodable}; +use ln::msgs::{HandleError,ChannelMessageHandler}; use util::{byte_utils, events, internal_traits, rng}; use util::sha2::Sha256; -use util::ser::Readable; +use util::ser::{Readable, Writeable}; use util::chacha20poly1305rfc::ChaCha20; use util::logger::Logger; use util::errors::APIError; @@ -2252,11 +2252,12 @@ mod tests { use ln::channelmanager::{ChannelManager,OnionKeys}; use ln::router::{Route, RouteHop, Router}; use ln::msgs; - use ln::msgs::{MsgEncodable,ChannelMessageHandler,RoutingMessageHandler}; + use ln::msgs::{ChannelMessageHandler,RoutingMessageHandler}; use util::test_utils; use util::events::{Event, EventsProvider}; - use util::logger::Logger; use util::errors::APIError; + use util::logger::Logger; + use util::ser::Writeable; use bitcoin::util::hash::Sha256dHash; use bitcoin::blockdata::block::{Block, BlockHeader}; diff --git a/src/ln/msgs.rs b/src/ln/msgs.rs index b2f48997c..92e900a21 100644 --- a/src/ln/msgs.rs +++ b/src/ln/msgs.rs @@ -2,7 +2,6 @@ use secp256k1::key::PublicKey; use secp256k1::{Secp256k1, Signature}; use secp256k1; use bitcoin::util::hash::Sha256dHash; -use bitcoin::network::serialize::serialize; use bitcoin::blockdata::script::Script; use std::error::Error; @@ -13,19 +12,6 @@ use std::result::Result; use util::{byte_utils, internal_traits, events}; use util::ser::{Readable, Writeable, Writer}; -pub trait MsgEncodable { - fn encode(&self) -> Vec; - #[inline] - fn encoded_len(&self) -> usize { self.encode().len() } - #[inline] - fn encode_with_len(&self) -> Vec { - let enc = self.encode(); - let mut res = Vec::with_capacity(enc.len() + 2); - res.extend_from_slice(&byte_utils::be16_to_array(enc.len() as u16)); - res.extend_from_slice(&enc); - res - } -} #[derive(Debug)] pub enum DecodeError { /// Unknown realm byte in an OnionHopData packet @@ -553,167 +539,6 @@ impl From<::std::io::Error> for DecodeError { } } -impl MsgEncodable for GlobalFeatures { - fn encode(&self) -> Vec { - let mut res = Vec::with_capacity(self.flags.len() + 2); - res.extend_from_slice(&byte_utils::be16_to_array(self.flags.len() as u16)); - res.extend_from_slice(&self.flags[..]); - res - } - fn encoded_len(&self) -> usize { self.flags.len() + 2 } -} - -impl MsgEncodable for ChannelReestablish { - fn encode(&self) -> Vec { - let mut res = Vec::with_capacity(if self.data_loss_protect.is_some() { 32+2*8+33+32 } else { 32+2*8 }); - - res.extend_from_slice(&serialize(&self.channel_id).unwrap()[..]); - res.extend_from_slice(&byte_utils::be64_to_array(self.next_local_commitment_number)); - res.extend_from_slice(&byte_utils::be64_to_array(self.next_remote_commitment_number)); - - if let &Some(ref data_loss_protect) = &self.data_loss_protect { - res.extend_from_slice(&data_loss_protect.your_last_per_commitment_secret[..]); - res.extend_from_slice(&data_loss_protect.my_current_per_commitment_point.serialize()); - } - res - } -} - -impl MsgEncodable for UnsignedNodeAnnouncement { - fn encode(&self) -> Vec { - let features = self.features.encode(); - let mut res = Vec::with_capacity(74 + features.len() + self.addresses.len()*7 + self.excess_address_data.len() + self.excess_data.len()); - res.extend_from_slice(&features[..]); - res.extend_from_slice(&byte_utils::be32_to_array(self.timestamp)); - res.extend_from_slice(&self.node_id.serialize()); - res.extend_from_slice(&self.rgb); - res.extend_from_slice(&self.alias); - let mut addr_slice = Vec::with_capacity(self.addresses.len() * 18); - let mut addrs_to_encode = self.addresses.clone(); - addrs_to_encode.sort_unstable_by(|a, b| { a.get_id().cmp(&b.get_id()) }); - addrs_to_encode.dedup_by(|a, b| { a.get_id() == b.get_id() }); - for addr in addrs_to_encode.iter() { - match addr { - &NetAddress::IPv4{addr, port} => { - addr_slice.push(1); - addr_slice.extend_from_slice(&addr); - addr_slice.extend_from_slice(&byte_utils::be16_to_array(port)); - }, - &NetAddress::IPv6{addr, port} => { - addr_slice.push(2); - addr_slice.extend_from_slice(&addr); - addr_slice.extend_from_slice(&byte_utils::be16_to_array(port)); - }, - &NetAddress::OnionV2{addr, port} => { - addr_slice.push(3); - addr_slice.extend_from_slice(&addr); - addr_slice.extend_from_slice(&byte_utils::be16_to_array(port)); - }, - &NetAddress::OnionV3{ed25519_pubkey, checksum, version, port} => { - addr_slice.push(4); - addr_slice.extend_from_slice(&ed25519_pubkey); - addr_slice.extend_from_slice(&byte_utils::be16_to_array(checksum)); - addr_slice.push(version); - addr_slice.extend_from_slice(&byte_utils::be16_to_array(port)); - }, - } - } - res.extend_from_slice(&byte_utils::be16_to_array((addr_slice.len() + self.excess_address_data.len()) as u16)); - res.extend_from_slice(&addr_slice[..]); - res.extend_from_slice(&self.excess_address_data[..]); - res.extend_from_slice(&self.excess_data[..]); - res - } -} - -impl MsgEncodable for UnsignedChannelAnnouncement { - fn encode(&self) -> Vec { - let features = self.features.encode(); - let mut res = Vec::with_capacity(172 + features.len() + self.excess_data.len()); - res.extend_from_slice(&features[..]); - res.extend_from_slice(&self.chain_hash[..]); - res.extend_from_slice(&byte_utils::be64_to_array(self.short_channel_id)); - res.extend_from_slice(&self.node_id_1.serialize()); - res.extend_from_slice(&self.node_id_2.serialize()); - res.extend_from_slice(&self.bitcoin_key_1.serialize()); - res.extend_from_slice(&self.bitcoin_key_2.serialize()); - res.extend_from_slice(&self.excess_data[..]); - res - } -} - -impl MsgEncodable for UnsignedChannelUpdate { - fn encode(&self) -> Vec { - let mut res = Vec::with_capacity(64 + self.excess_data.len()); - res.extend_from_slice(&self.chain_hash[..]); - res.extend_from_slice(&byte_utils::be64_to_array(self.short_channel_id)); - res.extend_from_slice(&byte_utils::be32_to_array(self.timestamp)); - res.extend_from_slice(&byte_utils::be16_to_array(self.flags)); - res.extend_from_slice(&byte_utils::be16_to_array(self.cltv_expiry_delta)); - res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_minimum_msat)); - res.extend_from_slice(&byte_utils::be32_to_array(self.fee_base_msat)); - res.extend_from_slice(&byte_utils::be32_to_array(self.fee_proportional_millionths)); - res.extend_from_slice(&self.excess_data[..]); - res - } -} - -impl MsgEncodable for ChannelUpdate { - fn encode(&self) -> Vec { - let mut res = Vec::with_capacity(128); - res.extend_from_slice(&self.signature.serialize_compact(&Secp256k1::without_caps())[..]); - res.extend_from_slice(&self.contents.encode()[..]); - res - } -} - -impl MsgEncodable for OnionRealm0HopData { - fn encode(&self) -> Vec { - let mut res = Vec::with_capacity(32); - res.extend_from_slice(&byte_utils::be64_to_array(self.short_channel_id)); - res.extend_from_slice(&byte_utils::be64_to_array(self.amt_to_forward)); - res.extend_from_slice(&byte_utils::be32_to_array(self.outgoing_cltv_value)); - res.resize(32, 0); - res - } -} - -impl MsgEncodable for OnionHopData { - fn encode(&self) -> Vec { - let mut res = Vec::with_capacity(65); - res.push(self.realm); - res.extend_from_slice(&self.data.encode()[..]); - res.extend_from_slice(&self.hmac); - res - } -} - -impl MsgEncodable for OnionPacket { - fn encode(&self) -> Vec { - let mut res = Vec::with_capacity(1 + 33 + 20*65 + 32); - res.push(self.version); - match self.public_key { - Ok(pubkey) => res.extend_from_slice(&pubkey.serialize()), - Err(_) => res.extend_from_slice(&[0; 33]), - } - res.extend_from_slice(&self.hop_data); - res.extend_from_slice(&self.hmac); - res - } -} - -impl MsgEncodable for DecodedOnionErrorPacket { - fn encode(&self) -> Vec { - let mut res = Vec::with_capacity(32 + 4 + self.failuremsg.len() + self.pad.len()); - res.extend_from_slice(&self.hmac); - res.extend_from_slice(&[((self.failuremsg.len() >> 8) & 0xff) as u8, (self.failuremsg.len() & 0xff) as u8]); - res.extend_from_slice(&self.failuremsg); - res.extend_from_slice(&[((self.pad.len() >> 8) & 0xff) as u8, (self.pad.len() & 0xff) as u8]); - res.extend_from_slice(&self.pad); - res - } -} - impl_writeable_len_match!(AcceptChannel, { {AcceptChannel{ shutdown_scriptpubkey: Some(ref script), ..}, 270 + 2 + script.len()}, {_, 270} @@ -1339,8 +1164,8 @@ impl_writeable_len_match!(NodeAnnouncement, { #[cfg(test)] mod tests { use hex; - use ln::msgs::MsgEncodable; use ln::msgs; + use util::ser::Writeable; use secp256k1::key::{PublicKey,SecretKey}; use secp256k1::Secp256k1; diff --git a/src/ln/router.rs b/src/ln/router.rs index 6b20d541e..d2d428a38 100644 --- a/src/ln/router.rs +++ b/src/ln/router.rs @@ -8,8 +8,9 @@ use bitcoin::blockdata::opcodes; use chain::chaininterface::{ChainError, ChainWatchInterface}; use ln::channelmanager; -use ln::msgs::{ErrorAction,HandleError,RoutingMessageHandler,MsgEncodable,NetAddress,GlobalFeatures}; +use ln::msgs::{ErrorAction,HandleError,RoutingMessageHandler,NetAddress,GlobalFeatures}; use ln::msgs; +use util::ser::Writeable; use util::logger::Logger; use std::cmp; diff --git a/src/util/ser.rs b/src/util/ser.rs index 2dea21ef1..18e558204 100644 --- a/src/util/ser.rs +++ b/src/util/ser.rs @@ -9,6 +9,7 @@ use bitcoin::util::hash::Sha256dHash; use bitcoin::blockdata::script::Script; use std::marker::Sized; use ln::msgs::DecodeError; +use util::byte_utils; use util::byte_utils::{be64_to_array, be32_to_array, be16_to_array, slice_to_be16, slice_to_be32, slice_to_be64}; @@ -35,10 +36,38 @@ impl Writer for W { fn size_hint(&mut self, _size: usize) { } } +struct VecWriter(Vec); +impl Writer for VecWriter { + fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> { + self.0.extend_from_slice(buf); + Ok(()) + } + fn size_hint(&mut self, size: usize) { + self.0.reserve_exact(size); + } +} + /// A trait that various rust-lightning types implement allowing them to be written out to a Writer pub trait Writeable { /// Writes self out to the given Writer fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error>; + + /// Writes self out to a Vec + fn encode(&self) -> Vec { + let mut msg = VecWriter(Vec::new()); + self.write(&mut msg).unwrap(); + msg.0 + } + + /// Writes self out to a Vec + fn encode_with_len(&self) -> Vec { + let mut msg = VecWriter(Vec::new()); + 0u16.write(&mut msg).unwrap(); + self.write(&mut msg).unwrap(); + let len = msg.0.len(); + msg.0[..2].copy_from_slice(&byte_utils::be16_to_array(len as u16 - 2)); + msg.0 + } } /// A trait that various rust-lightning types implement allowing them to be read in from a Read