Skip to content

2019 07 369 fix spaces #370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/target/
/net-tokio/target/
**/*.rs.bk
Cargo.lock
/target/
**/*.rs.bk
.idea
.idea

5 changes: 2 additions & 3 deletions fuzz/fuzz_targets/full_stack_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,8 @@ struct Peer<'a> {
peers_connected: &'a RefCell<[bool; 256]>,
}
impl<'a> SocketDescriptor for Peer<'a> {
fn send_data(&mut self, data: &Vec<u8>, write_offset: usize, _resume_read: bool) -> usize {
assert!(write_offset < data.len());
data.len() - write_offset
fn send_data(&mut self, data: &[u8], _resume_read: bool) -> usize {
data.len()
}
fn disconnect_socket(&mut self) {
assert!(self.peers_connected.borrow()[self.id as usize]);
Expand Down
12 changes: 6 additions & 6 deletions net-tokio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl Connection {
let (reader, us) = Self::new(event_notify, stream);

if let Ok(initial_send) = peer_manager.new_outbound_connection(their_node_id, SocketDescriptor::new(us.clone(), peer_manager.clone())) {
if SocketDescriptor::new(us.clone(), peer_manager.clone()).send_data(&initial_send, 0, true) == initial_send.len() {
if SocketDescriptor::new(us.clone(), peer_manager.clone()).send_data(&initial_send, true) == initial_send.len() {
Self::schedule_read(peer_manager, us, reader);
} else {
println!("Failed to write first full message to socket!");
Expand Down Expand Up @@ -170,7 +170,7 @@ impl SocketDescriptor {
}
}
impl peer_handler::SocketDescriptor for SocketDescriptor {
fn send_data(&mut self, data: &Vec<u8>, write_offset: usize, resume_read: bool) -> usize {
fn send_data(&mut self, data: &[u8], resume_read: bool) -> usize {
macro_rules! schedule_read {
($us_ref: expr) => {
tokio::spawn(future::lazy(move || -> Result<(), ()> {
Expand Down Expand Up @@ -211,20 +211,20 @@ impl peer_handler::SocketDescriptor for SocketDescriptor {
let us_ref = self.clone();
schedule_read!(us_ref);
}
if data.len() == write_offset { return 0; }
if data.is_empty() { return 0; }
if us.writer.is_none() {
us.read_paused = true;
return 0;
}

let mut bytes = bytes::BytesMut::with_capacity(data.len() - write_offset);
bytes.put(&data[write_offset..]);
let mut bytes = bytes::BytesMut::with_capacity(data.len());
bytes.put(data);
let write_res = us.writer.as_mut().unwrap().start_send(bytes.freeze());
match write_res {
Ok(res) => {
match res {
AsyncSink::Ready => {
data.len() - write_offset
data.len()
},
AsyncSink::NotReady(_) => {
us.read_paused = true;
Expand Down
15 changes: 8 additions & 7 deletions src/ln/peer_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,21 @@ pub struct MessageHandler {
/// careful to ensure you don't have races whereby you might register a new connection with an fd
/// the same as a yet-to-be-disconnect_event()-ed.
pub trait SocketDescriptor : cmp::Eq + hash::Hash + Clone {
/// Attempts to send some data from the given Vec starting at the given offset to the peer.
/// Attempts to send some data from the given slice to the peer.
///
/// Returns the amount of data which was sent, possibly 0 if the socket has since disconnected.
/// Note that in the disconnected case, a disconnect_event must still fire and further write
/// attempts may occur until that time.
///
/// If the returned size is smaller than data.len() - write_offset, a write_available event must
/// If the returned size is smaller than data.len(), a write_available event must
/// trigger the next time more data can be written. Additionally, until the a send_data event
/// completes fully, no further read_events should trigger on the same peer!
///
/// If a read_event on this descriptor had previously returned true (indicating that read
/// events should be paused to prevent DoS in the send buffer), resume_read may be set
/// indicating that read events on this descriptor should resume. A resume_read of false does
/// *not* imply that further read events should be paused.
fn send_data(&mut self, data: &Vec<u8>, write_offset: usize, resume_read: bool) -> usize;
fn send_data(&mut self, data: &[u8], resume_read: bool) -> usize;
/// Disconnect the socket pointed to by this SocketDescriptor. Once this function returns, no
/// more calls to write_event, read_event or disconnect_event may be made with this descriptor.
/// No disconnect_event should be generated as a result of this call, though obviously races
Expand Down Expand Up @@ -387,7 +388,8 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
};

let should_be_reading = peer.pending_outbound_buffer.len() < MSG_BUFF_SIZE;
let data_sent = descriptor.send_data(next_buff, peer.pending_outbound_buffer_first_msg_offset, should_be_reading);
let pending = &next_buff[peer.pending_outbound_buffer_first_msg_offset..];
let data_sent = descriptor.send_data(pending, should_be_reading);
peer.pending_outbound_buffer_first_msg_offset += data_sent;
if peer.pending_outbound_buffer_first_msg_offset == next_buff.len() { true } else { false }
} {
Expand Down Expand Up @@ -1122,9 +1124,8 @@ mod tests {
}

impl SocketDescriptor for FileDescriptor {
fn send_data(&mut self, data: &Vec<u8>, write_offset: usize, _resume_read: bool) -> usize {
assert!(write_offset < data.len());
data.len() - write_offset
fn send_data(&mut self, data: &[u8], _resume_read: bool) -> usize {
data.len()
}

fn disconnect_socket(&mut self) {}
Expand Down