P2p: do not propose private nodes for peers swap

This commit is contained in:
OCamlPro-Iguernlala 2018-06-01 12:08:22 +02:00 committed by Grégoire Henry
parent d08cd57c69
commit 5456824960

View File

@ -557,30 +557,36 @@ module Connection = struct
let list pool = let list pool =
fold pool ~init:[] ~f:(fun peer_id c acc -> (peer_id, c) :: acc) fold pool ~init:[] ~f:(fun peer_id c acc -> (peer_id, c) :: acc)
let random ?different_than pool = let random ?different_than ~no_private pool =
let candidates = let candidates =
fold pool ~init:[] ~f:begin fun _peer conn acc -> fold pool ~init:[] ~f:begin fun _peer conn acc ->
match different_than with if no_private && (P2p_socket.private_node conn.conn) then
| Some excluded_conn acc
when P2p_socket.equal conn.conn excluded_conn.conn -> acc else
| Some _ | None -> conn :: acc match different_than with
| Some excluded_conn
when P2p_socket.equal conn.conn excluded_conn.conn -> acc
| Some _ | None -> conn :: acc
end in end in
match candidates with match candidates with
| [] -> None | [] -> None
| _ :: _ -> | _ :: _ ->
Some (List.nth candidates (Random.int @@ List.length candidates)) Some (List.nth candidates (Random.int @@ List.length candidates))
let random_lowid ?different_than pool = let random_lowid ?different_than ~no_private pool =
let candidates = let candidates =
fold pool ~init:[] ~f:begin fun _peer conn acc -> fold pool ~init:[] ~f:begin fun _peer conn acc ->
match different_than with if no_private && (P2p_socket.private_node conn.conn) then
| Some excluded_conn acc
when P2p_socket.equal conn.conn excluded_conn.conn -> acc else
| Some _ | None -> match different_than with
let ci = P2p_socket.info conn.conn in | Some excluded_conn
match ci.id_point with when P2p_socket.equal conn.conn excluded_conn.conn -> acc
| _, None -> acc | Some _ | None ->
| addr, Some port -> ((addr, port), ci.peer_id, conn) :: acc let ci = P2p_socket.info conn.conn in
match ci.id_point with
| _, None -> acc
| addr, Some port -> ((addr, port), ci.peer_id, conn) :: acc
end in end in
match candidates with match candidates with
| [] -> None | [] -> None
@ -1016,7 +1022,7 @@ and swap_request pool conn new_point _new_peer_id =
log pool (Swap_request_ignored { source = source_peer_id }) ; log pool (Swap_request_ignored { source = source_peer_id }) ;
lwt_log_info "Ignoring swap request from %a" P2p_peer.Id.pp source_peer_id lwt_log_info "Ignoring swap request from %a" P2p_peer.Id.pp source_peer_id
end else begin end else begin
match Connection.random_lowid pool with match Connection.random_lowid pool ~no_private:true with
| None -> | None ->
lwt_log_info lwt_log_info
"No swap candidate for %a" P2p_peer.Id.pp source_peer_id "No swap candidate for %a" P2p_peer.Id.pp source_peer_id
@ -1093,10 +1099,14 @@ let accept pool fd point =
end end
let send_swap_request pool = let send_swap_request pool =
match Connection.random pool with match Connection.random ~no_private:true pool with
| Some recipient when not pool.config.private_mode -> begin | Some recipient when not pool.config.private_mode -> begin
let recipient_peer_id = (Connection.info recipient).peer_id in let recipient_peer_id = (Connection.info recipient).peer_id in
match Connection.random_lowid ~different_than:recipient pool with match
Connection.random_lowid
~different_than:recipient
~no_private:true pool
with
| None -> () | None -> ()
| Some (proposed_point, proposed_peer_id, _proposed_conn) -> | Some (proposed_point, proposed_peer_id, _proposed_conn) ->
log pool (Swap_request_sent { source = recipient_peer_id }) ; log pool (Swap_request_sent { source = recipient_peer_id }) ;