2017-11-11 06:34:12 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
2018-02-06 00:17:03 +04:00
|
|
|
(* Copyright (c) 2014 - 2018. *)
|
2017-11-11 06:34:12 +04:00
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
|
|
|
(* FIXME ignore/postpone fetching/validating of block in the future... *)
|
|
|
|
|
2018-01-22 20:47:18 +04:00
|
|
|
open Peer_validator_worker_state
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2018-01-22 20:47:18 +04:00
|
|
|
module Name = struct
|
2018-02-16 04:26:24 +04:00
|
|
|
type t = Chain_id.t * P2p_peer.Id.t
|
2018-01-22 20:47:18 +04:00
|
|
|
let encoding =
|
2018-02-16 04:26:24 +04:00
|
|
|
Data_encoding.tup2 Chain_id.encoding P2p_peer.Id.encoding
|
2018-02-26 17:11:10 +04:00
|
|
|
let base = [ "validator.peer" ]
|
2018-02-16 04:26:24 +04:00
|
|
|
let pp ppf (chain, peer) =
|
2018-01-22 20:47:18 +04:00
|
|
|
Format.fprintf ppf "%a:%a"
|
2018-02-16 04:26:24 +04:00
|
|
|
Chain_id.pp_short chain P2p_peer.Id.pp_short peer
|
2018-01-22 20:47:18 +04:00
|
|
|
end
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2018-01-22 20:47:18 +04:00
|
|
|
module Request = struct
|
|
|
|
include Request
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2018-01-22 20:47:18 +04:00
|
|
|
type _ t =
|
|
|
|
| New_head : Block_hash.t * Block_header.t -> unit t
|
2018-03-21 18:38:41 +04:00
|
|
|
| New_branch : Block_hash.t * Block_locator.t * Block_locator.seed -> unit t
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2018-01-22 20:47:18 +04:00
|
|
|
let view (type a) (req : a t) : view = match req with
|
|
|
|
| New_head (hash, _) ->
|
|
|
|
New_head hash
|
2018-03-21 18:38:41 +04:00
|
|
|
| New_branch (hash, locator, seed) ->
|
|
|
|
(* the seed is associated to each locator
|
|
|
|
w.r.t. the peer_id of the sender *)
|
|
|
|
New_branch (hash, Block_locator.estimated_length seed locator)
|
2018-01-22 20:47:18 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
type limits = {
|
2017-11-13 17:25:02 +04:00
|
|
|
new_head_request_timeout: float ;
|
|
|
|
block_header_timeout: float ;
|
|
|
|
block_operations_timeout: float ;
|
|
|
|
protocol_timeout: float ;
|
2018-01-22 20:47:18 +04:00
|
|
|
worker_limits: Worker_types.limits
|
|
|
|
}
|
2017-11-13 17:25:02 +04:00
|
|
|
|
2018-01-22 20:47:18 +04:00
|
|
|
module Types = struct
|
|
|
|
include Worker_state
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2018-01-22 20:47:18 +04:00
|
|
|
type parameters = {
|
2018-02-16 04:26:24 +04:00
|
|
|
chain_db: Distributed_db.chain_db ;
|
2018-01-22 20:47:18 +04:00
|
|
|
block_validator: Block_validator.t ;
|
2018-02-16 04:26:24 +04:00
|
|
|
(* callback to chain_validator *)
|
2018-01-22 20:47:18 +04:00
|
|
|
notify_new_block: State.Block.t -> unit ;
|
|
|
|
notify_bootstrapped: unit -> unit ;
|
|
|
|
notify_termination: unit -> unit ;
|
|
|
|
limits: limits;
|
|
|
|
}
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2018-01-22 20:47:18 +04:00
|
|
|
type state = {
|
2018-01-24 15:48:25 +04:00
|
|
|
peer_id: P2p_peer.Id.t ;
|
2018-01-22 20:47:18 +04:00
|
|
|
parameters : parameters ;
|
|
|
|
mutable bootstrapped: bool ;
|
|
|
|
mutable last_validated_head: Block_header.t ;
|
|
|
|
mutable last_advertised_head: Block_header.t ;
|
|
|
|
}
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2018-01-22 20:47:18 +04:00
|
|
|
let view (state : state) _ : view =
|
|
|
|
let { bootstrapped ; last_validated_head ; last_advertised_head } = state in
|
|
|
|
{ bootstrapped ;
|
|
|
|
last_validated_head = Block_header.hash last_validated_head ;
|
|
|
|
last_advertised_head = Block_header.hash last_advertised_head }
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
module Worker = Worker.Make (Name) (Event) (Request) (Types)
|
|
|
|
|
|
|
|
open Types
|
|
|
|
|
|
|
|
type t = Worker.dropbox Worker.t
|
|
|
|
|
|
|
|
let debug w =
|
|
|
|
Format.kasprintf (fun msg -> Worker.record_event w (Debug msg))
|
2017-11-11 06:34:12 +04:00
|
|
|
|
|
|
|
let set_bootstrapped pv =
|
|
|
|
if not pv.bootstrapped then begin
|
|
|
|
pv.bootstrapped <- true ;
|
2018-01-22 20:47:18 +04:00
|
|
|
pv.parameters.notify_bootstrapped () ;
|
2017-11-11 06:34:12 +04:00
|
|
|
end
|
|
|
|
|
2018-01-22 20:47:18 +04:00
|
|
|
let bootstrap_new_branch w _ancestor _head unknown_prefix =
|
|
|
|
let pv = Worker.state w in
|
2018-03-21 18:38:41 +04:00
|
|
|
let sender_id = Distributed_db.my_peer_id pv.parameters.chain_db in
|
|
|
|
(* sender and receiver are inverted here because they are from
|
|
|
|
the point of view of the node sending the locator *)
|
|
|
|
let seed = {Block_locator.sender_id=pv.peer_id; receiver_id = sender_id } in
|
|
|
|
let len = Block_locator.estimated_length seed unknown_prefix in
|
2018-01-22 20:47:18 +04:00
|
|
|
debug w
|
2017-11-11 06:34:12 +04:00
|
|
|
"validating new branch from peer %a (approx. %d blocks)"
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_peer.Id.pp_short pv.peer_id len ;
|
2017-11-11 06:34:12 +04:00
|
|
|
let pipeline =
|
|
|
|
Bootstrap_pipeline.create
|
2018-01-22 20:47:18 +04:00
|
|
|
~notify_new_block:pv.parameters.notify_new_block
|
|
|
|
~block_header_timeout:pv.parameters.limits.block_header_timeout
|
|
|
|
~block_operations_timeout:pv.parameters.limits.block_operations_timeout
|
|
|
|
pv.parameters.block_validator
|
2018-02-16 04:26:24 +04:00
|
|
|
pv.peer_id pv.parameters.chain_db unknown_prefix in
|
2018-01-22 20:47:18 +04:00
|
|
|
Worker.protect w
|
2017-11-11 06:34:12 +04:00
|
|
|
~on_error:begin fun error ->
|
|
|
|
(* if the peer_validator is killed, let's cancel the pipeline *)
|
|
|
|
Bootstrap_pipeline.cancel pipeline >>= fun () ->
|
|
|
|
Lwt.return_error error
|
|
|
|
end
|
|
|
|
begin fun () ->
|
|
|
|
Bootstrap_pipeline.wait pipeline
|
|
|
|
end >>=? fun () ->
|
|
|
|
set_bootstrapped pv ;
|
2018-01-22 20:47:18 +04:00
|
|
|
debug w
|
2017-11-11 06:34:12 +04:00
|
|
|
"done validating new branch from peer %a."
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_peer.Id.pp_short pv.peer_id ;
|
2018-06-26 13:07:12 +04:00
|
|
|
return_unit
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2018-01-22 20:47:18 +04:00
|
|
|
let validate_new_head w hash (header : Block_header.t) =
|
|
|
|
let pv = Worker.state w in
|
2018-05-10 18:59:48 +04:00
|
|
|
debug w
|
|
|
|
"fetching operations for new head %a from peer %a"
|
|
|
|
Block_hash.pp_short hash
|
|
|
|
P2p_peer.Id.pp_short pv.peer_id ;
|
|
|
|
map_p
|
|
|
|
(fun i ->
|
|
|
|
Worker.protect w begin fun () ->
|
|
|
|
Distributed_db.Operations.fetch
|
|
|
|
~timeout:pv.parameters.limits.block_operations_timeout
|
|
|
|
pv.parameters.chain_db ~peer:pv.peer_id
|
|
|
|
(hash, i) header.shell.operations_hash
|
|
|
|
end)
|
|
|
|
(0 -- (header.shell.validation_passes - 1)) >>=? fun operations ->
|
|
|
|
debug w
|
|
|
|
"requesting validation for new head %a from peer %a"
|
|
|
|
Block_hash.pp_short hash
|
|
|
|
P2p_peer.Id.pp_short pv.peer_id ;
|
|
|
|
Block_validator.validate
|
|
|
|
~notify_new_block:pv.parameters.notify_new_block
|
|
|
|
pv.parameters.block_validator pv.parameters.chain_db
|
|
|
|
hash header operations >>=? fun _block ->
|
|
|
|
debug w
|
|
|
|
"end of validation for new head %a from peer %a"
|
|
|
|
Block_hash.pp_short hash
|
|
|
|
P2p_peer.Id.pp_short pv.peer_id ;
|
|
|
|
set_bootstrapped pv ;
|
2018-06-26 13:07:12 +04:00
|
|
|
return_unit
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2018-01-03 19:56:38 +04:00
|
|
|
let only_if_fitness_increases w distant_header cont =
|
|
|
|
let pv = Worker.state w in
|
2018-02-16 04:26:24 +04:00
|
|
|
let chain_state = Distributed_db.chain_state pv.parameters.chain_db in
|
|
|
|
Chain.head chain_state >>= fun local_header ->
|
2018-01-03 19:56:38 +04:00
|
|
|
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)
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_peer.Id.pp_short pv.peer_id ;
|
2018-01-03 19:56:38 +04:00
|
|
|
(* Don't download a branch that cannot beat the current head. *)
|
2018-06-26 13:07:12 +04:00
|
|
|
return_unit
|
2018-01-03 19:56:38 +04:00
|
|
|
end else cont ()
|
|
|
|
|
2018-05-10 18:59:48 +04:00
|
|
|
let assert_acceptable_head w hash (header: Block_header.t) =
|
2018-01-22 20:47:18 +04:00
|
|
|
let pv = Worker.state w in
|
2018-02-16 04:26:24 +04:00
|
|
|
let chain_state = Distributed_db.chain_state pv.parameters.chain_db in
|
2018-05-10 18:59:48 +04:00
|
|
|
State.Chain.acceptable_block chain_state hash header >>= fun acceptable ->
|
|
|
|
fail_unless acceptable
|
|
|
|
(Validation_errors.Checkpoint_error (hash, Some pv.peer_id))
|
|
|
|
|
|
|
|
let may_validate_new_head w hash (header : Block_header.t) =
|
|
|
|
let pv = Worker.state w in
|
|
|
|
let chain_state = Distributed_db.chain_state pv.parameters.chain_db in
|
|
|
|
State.Block.known_valid chain_state hash >>= fun valid_block ->
|
|
|
|
State.Block.known_invalid chain_state hash >>= fun invalid_block ->
|
|
|
|
State.Block.known_valid chain_state
|
|
|
|
header.shell.predecessor >>= fun valid_predecessor ->
|
|
|
|
State.Block.known_invalid chain_state
|
|
|
|
header.shell.predecessor >>= fun invalid_predecessor ->
|
|
|
|
if valid_block then begin
|
|
|
|
debug w
|
|
|
|
"ignoring previously validated block %a from peer %a"
|
|
|
|
Block_hash.pp_short hash
|
|
|
|
P2p_peer.Id.pp_short pv.peer_id ;
|
|
|
|
set_bootstrapped pv ;
|
|
|
|
pv.last_validated_head <- header ;
|
2018-06-26 13:07:12 +04:00
|
|
|
return_unit
|
2018-05-10 18:59:48 +04:00
|
|
|
end else if invalid_block then begin
|
|
|
|
debug w
|
|
|
|
"ignoring known invalid block %a from peer %a"
|
|
|
|
Block_hash.pp_short hash
|
|
|
|
P2p_peer.Id.pp_short pv.peer_id ;
|
|
|
|
fail Validation_errors.Known_invalid
|
|
|
|
end else if invalid_predecessor then begin
|
|
|
|
debug w
|
|
|
|
"ignoring known invalid block %a from peer %a"
|
|
|
|
Block_hash.pp_short hash
|
|
|
|
P2p_peer.Id.pp_short pv.peer_id ;
|
|
|
|
Distributed_db.commit_invalid_block pv.parameters.chain_db
|
|
|
|
hash header [Validation_errors.Known_invalid] >>=? fun _ ->
|
|
|
|
fail Validation_errors.Known_invalid
|
|
|
|
end else if not valid_predecessor then begin
|
|
|
|
debug w
|
|
|
|
"missing predecessor for new head %a from peer %a"
|
|
|
|
Block_hash.pp_short hash
|
|
|
|
P2p_peer.Id.pp_short pv.peer_id ;
|
|
|
|
Distributed_db.Request.current_branch
|
|
|
|
pv.parameters.chain_db ~peer:pv.peer_id () ;
|
2018-06-26 13:07:12 +04:00
|
|
|
return_unit
|
2018-05-10 18:59:48 +04:00
|
|
|
end else begin
|
|
|
|
only_if_fitness_increases w header @@ fun () ->
|
|
|
|
assert_acceptable_head w hash header >>=? fun () ->
|
|
|
|
validate_new_head w hash header
|
|
|
|
end
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2018-01-22 20:47:18 +04:00
|
|
|
let may_validate_new_branch w distant_hash locator =
|
|
|
|
let pv = Worker.state w in
|
2017-11-11 06:34:12 +04:00
|
|
|
let distant_header, _ = (locator : Block_locator.t :> Block_header.t * _) in
|
2018-01-03 19:56:38 +04:00
|
|
|
only_if_fitness_increases w distant_header @@ fun () ->
|
2018-05-10 18:59:48 +04:00
|
|
|
assert_acceptable_head w
|
|
|
|
(Block_header.hash distant_header) distant_header >>=? fun () ->
|
2018-02-16 04:26:24 +04:00
|
|
|
let chain_state = Distributed_db.chain_state pv.parameters.chain_db in
|
2018-03-21 18:38:41 +04:00
|
|
|
State.Block.known_ancestor chain_state locator >>= function
|
2018-01-03 19:56:38 +04:00
|
|
|
| None ->
|
|
|
|
debug w
|
|
|
|
"ignoring branch %a without common ancestor from peer: %a."
|
|
|
|
Block_hash.pp_short distant_hash
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_peer.Id.pp_short pv.peer_id ;
|
2018-02-13 17:12:09 +04:00
|
|
|
fail Validation_errors.Unknown_ancestor
|
2018-01-03 19:56:38 +04:00
|
|
|
| Some (ancestor, unknown_prefix) ->
|
|
|
|
bootstrap_new_branch w ancestor distant_header unknown_prefix
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2018-01-22 20:47:18 +04:00
|
|
|
let on_no_request w =
|
|
|
|
let pv = Worker.state w in
|
|
|
|
debug w "no new head from peer %a for %g seconds."
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_peer.Id.pp_short pv.peer_id
|
2018-01-22 20:47:18 +04:00
|
|
|
pv.parameters.limits.new_head_request_timeout ;
|
2018-02-16 04:26:24 +04:00
|
|
|
Distributed_db.Request.current_head pv.parameters.chain_db ~peer:pv.peer_id () ;
|
2018-06-26 13:07:12 +04:00
|
|
|
return_unit
|
2018-01-22 20:47:18 +04:00
|
|
|
|
|
|
|
let on_request (type a) w (req : a Request.t) : a tzresult Lwt.t =
|
|
|
|
let pv = Worker.state w in
|
|
|
|
match req with
|
|
|
|
| Request.New_head (hash, header) ->
|
|
|
|
debug w
|
|
|
|
"processing new head %a from peer %a."
|
|
|
|
Block_hash.pp_short hash
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_peer.Id.pp_short pv.peer_id ;
|
2018-01-22 20:47:18 +04:00
|
|
|
may_validate_new_head w hash header
|
2018-03-21 18:38:41 +04:00
|
|
|
| Request.New_branch (hash, locator, _seed) ->
|
2018-01-22 20:47:18 +04:00
|
|
|
(* TODO penalize empty locator... ?? *)
|
|
|
|
debug w "processing new branch %a from peer %a."
|
|
|
|
Block_hash.pp_short hash
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_peer.Id.pp_short pv.peer_id ;
|
2018-01-22 20:47:18 +04:00
|
|
|
may_validate_new_branch w hash locator
|
|
|
|
|
|
|
|
let on_completion w r _ st =
|
|
|
|
Worker.record_event w (Event.Request (Request.view r, st, None )) ;
|
2018-06-26 13:07:12 +04:00
|
|
|
Lwt.return_unit
|
2018-01-22 20:47:18 +04:00
|
|
|
|
|
|
|
let on_error w r st errs =
|
|
|
|
let pv = Worker.state w in
|
|
|
|
match errs with
|
2018-02-13 17:12:09 +04:00
|
|
|
((( Validation_errors.Unknown_ancestor
|
|
|
|
| Validation_errors.Invalid_locator _
|
2018-01-22 20:47:18 +04:00
|
|
|
| Block_validator_errors.Invalid_block _ ) :: _) as errors ) ->
|
2018-03-11 18:02:59 +04:00
|
|
|
Distributed_db.greylist pv.parameters.chain_db pv.peer_id >>= fun () ->
|
2018-01-22 20:47:18 +04:00
|
|
|
debug w
|
|
|
|
"Terminating the validation worker for peer %a (kickban)."
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_peer.Id.pp_short pv.peer_id ;
|
2018-01-22 20:47:18 +04:00
|
|
|
debug w "%a" Error_monad.pp_print_error errors ;
|
|
|
|
Worker.trigger_shutdown w ;
|
|
|
|
Worker.record_event w (Event.Request (r, st, Some errs)) ;
|
|
|
|
Lwt.return (Error errs)
|
|
|
|
| [Block_validator_errors.Unavailable_protocol { protocol } ] -> begin
|
2017-11-11 06:34:12 +04:00
|
|
|
Block_validator.fetch_and_compile_protocol
|
2018-01-22 20:47:18 +04:00
|
|
|
pv.parameters.block_validator
|
2017-11-13 17:25:02 +04:00
|
|
|
~peer:pv.peer_id
|
2018-01-22 20:47:18 +04:00
|
|
|
~timeout:pv.parameters.limits.protocol_timeout
|
2017-11-13 17:25:02 +04:00
|
|
|
protocol >>= function
|
2018-06-02 21:06:54 +04:00
|
|
|
| Ok _ ->
|
|
|
|
Distributed_db.Request.current_head
|
|
|
|
pv.parameters.chain_db ~peer:pv.peer_id () ;
|
2018-06-26 13:07:12 +04:00
|
|
|
return_unit
|
2017-11-11 06:34:12 +04:00
|
|
|
| Error _ ->
|
2018-02-22 18:35:50 +04:00
|
|
|
(* TODO: punish *)
|
2018-01-22 20:47:18 +04:00
|
|
|
debug w
|
|
|
|
"Terminating the validation worker for peer %a \
|
|
|
|
(missing protocol %a)."
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_peer.Id.pp_short pv.peer_id
|
2018-01-22 20:47:18 +04:00
|
|
|
Protocol_hash.pp_short protocol ;
|
|
|
|
Worker.record_event w (Event.Request (r, st, Some errs)) ;
|
|
|
|
Lwt.return (Error errs)
|
2017-11-11 06:34:12 +04:00
|
|
|
end
|
2018-01-22 20:47:18 +04:00
|
|
|
| _ ->
|
|
|
|
Worker.record_event w (Event.Request (r, st, Some errs)) ;
|
|
|
|
Lwt.return (Error errs)
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2018-01-22 20:47:18 +04:00
|
|
|
let on_close w =
|
|
|
|
let pv = Worker.state w in
|
2018-02-16 04:26:24 +04:00
|
|
|
Distributed_db.disconnect pv.parameters.chain_db pv.peer_id >>= fun () ->
|
2018-02-23 21:32:26 +04:00
|
|
|
pv.parameters.notify_termination () ;
|
2018-06-26 13:07:12 +04:00
|
|
|
Lwt.return_unit
|
2018-01-22 20:47:18 +04:00
|
|
|
|
|
|
|
let on_launch _ name parameters =
|
2018-02-16 04:26:24 +04:00
|
|
|
let chain_state = Distributed_db.chain_state parameters.chain_db in
|
|
|
|
State.Block.read_exn chain_state
|
|
|
|
(State.Chain.genesis chain_state).block >>= fun genesis ->
|
2018-01-22 20:47:18 +04:00
|
|
|
let rec pv = {
|
|
|
|
peer_id = snd name ;
|
|
|
|
parameters = { parameters with notify_new_block } ;
|
|
|
|
bootstrapped = false ;
|
|
|
|
last_validated_head = State.Block.header genesis ;
|
|
|
|
last_advertised_head = State.Block.header genesis ;
|
|
|
|
}
|
|
|
|
and notify_new_block block =
|
2017-11-14 02:27:19 +04:00
|
|
|
pv.last_validated_head <- State.Block.header block ;
|
2018-01-22 20:47:18 +04:00
|
|
|
parameters.notify_new_block block in
|
|
|
|
Lwt.return pv
|
|
|
|
|
|
|
|
let table =
|
|
|
|
let merge w (Worker.Any_request neu) old =
|
|
|
|
let pv = Worker.state w in
|
|
|
|
match neu with
|
2018-03-21 18:38:41 +04:00
|
|
|
| Request.New_branch (_, locator, _) ->
|
2018-01-22 20:47:18 +04:00
|
|
|
let header, _ = (locator : Block_locator.t :> _ * _) in
|
|
|
|
pv.last_advertised_head <- header ;
|
|
|
|
Some (Worker.Any_request neu)
|
|
|
|
| Request.New_head (_, header) ->
|
|
|
|
pv.last_advertised_head <- header ;
|
|
|
|
(* TODO penalize decreasing fitness *)
|
|
|
|
match old with
|
|
|
|
| Some (Worker.Any_request (Request.New_branch _) as old) ->
|
|
|
|
Some old (* ignore *)
|
|
|
|
| Some (Worker.Any_request (Request.New_head _)) ->
|
|
|
|
Some (Any_request neu)
|
|
|
|
| None ->
|
|
|
|
Some (Any_request neu) in
|
|
|
|
Worker.create_table (Dropbox { merge })
|
|
|
|
|
|
|
|
let create
|
|
|
|
?(notify_new_block = fun _ -> ())
|
|
|
|
?(notify_bootstrapped = fun () -> ())
|
|
|
|
?(notify_termination = fun _ -> ())
|
2018-02-16 04:26:24 +04:00
|
|
|
limits block_validator chain_db peer_id =
|
|
|
|
let name = (State.Chain.id (Distributed_db.chain_state chain_db), peer_id) in
|
2018-01-22 20:47:18 +04:00
|
|
|
let parameters = {
|
2018-02-16 04:26:24 +04:00
|
|
|
chain_db ;
|
2018-01-22 20:47:18 +04:00
|
|
|
notify_termination ;
|
2017-11-11 06:34:12 +04:00
|
|
|
block_validator ;
|
|
|
|
notify_new_block ;
|
|
|
|
notify_bootstrapped ;
|
2018-01-22 20:47:18 +04:00
|
|
|
limits ;
|
2017-11-11 06:34:12 +04:00
|
|
|
} in
|
2018-01-22 20:47:18 +04:00
|
|
|
let module Handlers = struct
|
|
|
|
type self = t
|
|
|
|
let on_launch = on_launch
|
|
|
|
let on_request = on_request
|
|
|
|
let on_close = on_close
|
|
|
|
let on_error = on_error
|
|
|
|
let on_completion = on_completion
|
2018-06-26 13:07:12 +04:00
|
|
|
let on_no_request _ = return_unit
|
2018-01-22 20:47:18 +04:00
|
|
|
end in
|
|
|
|
Worker.launch table ~timeout: limits.new_head_request_timeout limits.worker_limits
|
|
|
|
name parameters
|
|
|
|
(module Handlers)
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2018-01-22 20:47:18 +04:00
|
|
|
let notify_branch w locator =
|
2017-11-14 02:27:19 +04:00
|
|
|
let header, _ = (locator : Block_locator.t :> _ * _) in
|
|
|
|
let hash = Block_header.hash header in
|
2018-03-21 18:38:41 +04:00
|
|
|
let pv = Worker.state w in
|
|
|
|
let sender_id = Distributed_db.my_peer_id pv.parameters.chain_db in
|
|
|
|
(* sender and receiver are inverted here because they are from
|
|
|
|
the point of view of the node sending the locator *)
|
|
|
|
let seed = {Block_locator.sender_id=pv.peer_id; receiver_id=sender_id } in
|
|
|
|
Worker.drop_request w (New_branch (hash, locator, seed))
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2018-01-22 20:47:18 +04:00
|
|
|
let notify_head w header =
|
2017-11-11 06:34:12 +04:00
|
|
|
let hash = Block_header.hash header in
|
2018-01-22 20:47:18 +04:00
|
|
|
Worker.drop_request w (New_head (hash, header))
|
|
|
|
|
|
|
|
let shutdown w =
|
|
|
|
Worker.shutdown w
|
|
|
|
|
|
|
|
let peer_id w =
|
|
|
|
let pv = Worker.state w in
|
|
|
|
pv.peer_id
|
|
|
|
|
|
|
|
let bootstrapped w =
|
|
|
|
let pv = Worker.state w in
|
|
|
|
pv.bootstrapped
|
|
|
|
|
|
|
|
let current_head w =
|
|
|
|
let pv = Worker.state w in
|
|
|
|
pv.last_validated_head
|
|
|
|
|
|
|
|
let status = Worker.status
|
|
|
|
|
|
|
|
let running_workers () = Worker.list table
|
|
|
|
|
|
|
|
let current_request t = Worker.current_request t
|
|
|
|
|
|
|
|
let last_events = Worker.last_events
|