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.
This commit is contained in:
Benjamin Canou 2018-01-03 16:56:38 +01:00 committed by Grégoire Henry
parent 41ad73a3ed
commit 9060122b26

View File

@ -159,6 +159,22 @@ let validate_new_head w hash (header : Block_header.t) =
set_bootstrapped pv ; set_bootstrapped pv ;
return () 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 may_validate_new_head w hash header =
let pv = Worker.state w in let pv = Worker.state w in
let net_state = Distributed_db.net_state pv.parameters.net_db in let net_state = Distributed_db.net_state pv.parameters.net_db in
@ -181,24 +197,13 @@ let may_validate_new_head w hash header =
fail Known_invalid fail Known_invalid
end end
| false -> | false ->
only_if_fitness_increases w header @@ fun () ->
validate_new_head w hash header validate_new_head w hash header
let may_validate_new_branch w distant_hash locator = let may_validate_new_branch w distant_hash locator =
let pv = Worker.state w in let pv = Worker.state w in
let distant_header, _ = (locator : Block_locator.t :> Block_header.t * _) in let distant_header, _ = (locator : Block_locator.t :> Block_header.t * _) in
let net_state = Distributed_db.net_state pv.parameters.net_db in only_if_fitness_increases w distant_header @@ fun () ->
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 let net_state = Distributed_db.net_state pv.parameters.net_db in
Block_locator_iterator.known_ancestor net_state locator >>= function Block_locator_iterator.known_ancestor net_state locator >>= function
| None -> | None ->
@ -209,7 +214,6 @@ let may_validate_new_branch w distant_hash locator =
fail Unknown_ancestor fail Unknown_ancestor
| Some (ancestor, unknown_prefix) -> | Some (ancestor, unknown_prefix) ->
bootstrap_new_branch w ancestor distant_header unknown_prefix bootstrap_new_branch w ancestor distant_header unknown_prefix
end
let on_no_request w = let on_no_request w =
let pv = Worker.state w in let pv = Worker.state w in