From 9060122b26664fb0af6250ef024217d89498df11 Mon Sep 17 00:00:00 2001 From: Benjamin Canou Date: Wed, 3 Jan 2018 16:56:38 +0100 Subject: [PATCH] Node: only validate branches that can increase the current head's fitness And also make the check when processing new head increments. That way we prevent spamming with valid head successors that bear the same fitness. --- src/lib_node_shell/peer_validator.ml | 50 +++++++++++++++------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/src/lib_node_shell/peer_validator.ml b/src/lib_node_shell/peer_validator.ml index 0c53861b4..49ea64c02 100644 --- a/src/lib_node_shell/peer_validator.ml +++ b/src/lib_node_shell/peer_validator.ml @@ -159,6 +159,22 @@ let validate_new_head w hash (header : Block_header.t) = set_bootstrapped pv ; return () +let only_if_fitness_increases w distant_header cont = + let pv = Worker.state w in + let net_state = Distributed_db.net_state pv.parameters.net_db in + Chain.head net_state >>= fun local_header -> + if Fitness.compare + distant_header.Block_header.shell.fitness + (State.Block.fitness local_header) <= 0 then begin + set_bootstrapped pv ; + debug w + "ignoring head %a with non increasing fitness from peer: %a." + Block_hash.pp_short (Block_header.hash distant_header) + P2p.Peer_id.pp_short pv.peer_id ; + (* Don't download a branch that cannot beat the current head. *) + return () + end else cont () + let may_validate_new_head w hash header = let pv = Worker.state w in let net_state = Distributed_db.net_state pv.parameters.net_db in @@ -181,35 +197,23 @@ let may_validate_new_head w hash header = fail Known_invalid end | false -> + only_if_fitness_increases w header @@ fun () -> validate_new_head w hash header let may_validate_new_branch w distant_hash locator = let pv = Worker.state w in let distant_header, _ = (locator : Block_locator.t :> Block_header.t * _) in + only_if_fitness_increases w distant_header @@ fun () -> let net_state = Distributed_db.net_state pv.parameters.net_db in - Chain.head net_state >>= fun local_header -> - if Fitness.compare - distant_header.Block_header.shell.fitness - (State.Block.fitness local_header) < 0 then begin - set_bootstrapped pv ; - debug w - "ignoring branch %a with low fitness from peer: %a." - Block_hash.pp_short distant_hash - P2p.Peer_id.pp_short pv.peer_id ; - (* Don't bother with downloading a branch with a low fitness. *) - return () - end else begin - let net_state = Distributed_db.net_state pv.parameters.net_db in - Block_locator_iterator.known_ancestor net_state locator >>= function - | None -> - debug w - "ignoring branch %a without common ancestor from peer: %a." - Block_hash.pp_short distant_hash - P2p.Peer_id.pp_short pv.peer_id ; - fail Unknown_ancestor - | Some (ancestor, unknown_prefix) -> - bootstrap_new_branch w ancestor distant_header unknown_prefix - end + Block_locator_iterator.known_ancestor net_state locator >>= function + | None -> + debug w + "ignoring branch %a without common ancestor from peer: %a." + Block_hash.pp_short distant_hash + P2p.Peer_id.pp_short pv.peer_id ; + fail Unknown_ancestor + | Some (ancestor, unknown_prefix) -> + bootstrap_new_branch w ancestor distant_header unknown_prefix let on_no_request w = let pv = Worker.state w in