Swap read_event read type for a slice isntead of a Vec
authorMatt Corallo <git@bluematt.me>
Sat, 1 Feb 2020 01:57:01 +0000 (20:57 -0500)
committerMatt Corallo <git@bluematt.me>
Tue, 10 Mar 2020 15:52:12 +0000 (11:52 -0400)
It looks like we don't currently use the Vec as a Vec, and can
happily take a slice, which makes things easier on the calling
side.

fuzz/src/full_stack.rs
lightning-net-tokio/src/lib.rs
lightning/src/ln/peer_handler.rs

index ab63162f54e8166415b12b2a1159bb801232edbc..3879d0b1da3cd506443bb8e29b4c7778b069ea7d 100644 (file)
@@ -384,7 +384,7 @@ pub fn do_test(data: &[u8], logger: &Arc<dyn Logger>) {
                        3 => {
                                let peer_id = get_slice!(1)[0];
                                if !peers.borrow()[peer_id as usize] { return; }
-                               match loss_detector.handler.read_event(&mut Peer{id: peer_id, peers_connected: &peers}, get_slice!(get_slice!(1)[0]).to_vec()) {
+                               match loss_detector.handler.read_event(&mut Peer{id: peer_id, peers_connected: &peers}, get_slice!(get_slice!(1)[0])) {
                                        Ok(res) => assert!(!res),
                                        Err(_) => { peers.borrow_mut()[peer_id as usize] = false; }
                                }
index c2bac324bd3c245490797fb5daf02f3554a56859..d4621e0003c3d2c5f0e67ef1dc4a118791541166 100644 (file)
@@ -62,7 +62,7 @@ impl Connection {
                        //TODO: There's a race where we don't meet the requirements of socket_disconnected if its
                        //called right here, after we release the us_ref lock in the scope above, but before we
                        //call read_event!
-                       match peer_manager.read_event(&mut SocketDescriptor::new(us_ref.clone(), peer_manager.clone()), pending_read) {
+                       match peer_manager.read_event(&mut SocketDescriptor::new(us_ref.clone(), peer_manager.clone()), &pending_read) {
                                Ok(pause_read) => {
                                        if pause_read {
                                                let mut lock = us_ref.lock().unwrap();
@@ -181,7 +181,7 @@ impl<CMH: ChannelMessageHandler> peer_handler::SocketDescriptor for SocketDescri
                                        }
                                        if !read_data.is_empty() {
                                                let mut us_clone = $us_ref.clone();
-                                               match $us_ref.peer_manager.read_event(&mut us_clone, read_data) {
+                                               match $us_ref.peer_manager.read_event(&mut us_clone, &read_data) {
                                                        Ok(pause_read) => {
                                                                if pause_read { return Ok(()); }
                                                        },
index 9cbd02121e9e55231a58b7b4b3c935a9b0b07a23..12cf937bc0f07785fbb58d93db58355dbbf0dce9 100644 (file)
@@ -445,7 +445,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
        /// on this file descriptor has resume_read set (preventing DoS issues in the send buffer).
        ///
        /// Panics if the descriptor was not previously registered in a new_*_connection event.
-       pub fn read_event(&self, peer_descriptor: &mut Descriptor, data: Vec<u8>) -> Result<bool, PeerHandleError> {
+       pub fn read_event(&self, peer_descriptor: &mut Descriptor, data: &[u8]) -> Result<bool, PeerHandleError> {
                match self.do_read_event(peer_descriptor, data) {
                        Ok(res) => Ok(res),
                        Err(e) => {
@@ -455,7 +455,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
                }
        }
 
-       fn do_read_event(&self, peer_descriptor: &mut Descriptor, data: Vec<u8>) -> Result<bool, PeerHandleError> {
+       fn do_read_event(&self, peer_descriptor: &mut Descriptor, data: &[u8]) -> Result<bool, PeerHandleError> {
                let pause_read = {
                        let mut peers_lock = self.peers.lock().unwrap();
                        let peers = &mut *peers_lock;
@@ -1228,9 +1228,9 @@ mod tests {
                let mut fd_b = FileDescriptor { fd: 1, outbound_data: Arc::new(Mutex::new(Vec::new())) };
                let initial_data = peer_b.new_outbound_connection(a_id, fd_b.clone()).unwrap();
                peer_a.new_inbound_connection(fd_a.clone()).unwrap();
-               assert_eq!(peer_a.read_event(&mut fd_a, initial_data).unwrap(), false);
-               assert_eq!(peer_b.read_event(&mut fd_b, fd_a.outbound_data.lock().unwrap().split_off(0)).unwrap(), false);
-               assert_eq!(peer_a.read_event(&mut fd_a, fd_b.outbound_data.lock().unwrap().split_off(0)).unwrap(), false);
+               assert_eq!(peer_a.read_event(&mut fd_a, &initial_data).unwrap(), false);
+               assert_eq!(peer_b.read_event(&mut fd_b, &fd_a.outbound_data.lock().unwrap().split_off(0)).unwrap(), false);
+               assert_eq!(peer_a.read_event(&mut fd_a, &fd_b.outbound_data.lock().unwrap().split_off(0)).unwrap(), false);
        }
 
        #[test]