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. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
2018-02-16 04:26:24 +04:00
|
|
|
open Chain_validator_worker_state
|
2018-01-22 20:21:23 +04:00
|
|
|
|
|
|
|
module Name = struct
|
2018-02-16 04:26:24 +04:00
|
|
|
type t = Chain_id.t
|
|
|
|
let encoding = Chain_id.encoding
|
2018-02-26 17:11:10 +04:00
|
|
|
let base = [ "validator.chain" ]
|
2018-02-16 04:26:24 +04:00
|
|
|
let pp = Chain_id.pp_short
|
2018-01-22 20:21:23 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
module Request = struct
|
|
|
|
include Request
|
|
|
|
type _ t = Validated : State.Block.t -> Event.update t
|
|
|
|
let view (type a) (Validated block : a t) : view =
|
|
|
|
State.Block.hash block
|
|
|
|
end
|
|
|
|
|
|
|
|
type limits = {
|
|
|
|
bootstrap_threshold: int ;
|
|
|
|
worker_limits: Worker_types.limits
|
|
|
|
}
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2018-01-22 20:21:23 +04:00
|
|
|
module Types = struct
|
|
|
|
include Worker_state
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2018-01-22 20:21:23 +04:00
|
|
|
type parameters = {
|
|
|
|
parent: Name.t option ;
|
|
|
|
db: Distributed_db.t ;
|
2018-02-16 04:26:24 +04:00
|
|
|
chain_state: State.Chain.t ;
|
|
|
|
chain_db: Distributed_db.chain_db ;
|
2018-01-22 20:21:23 +04:00
|
|
|
block_validator: Block_validator.t ;
|
|
|
|
global_valid_block_input: State.Block.t Lwt_watcher.input ;
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2018-01-22 20:21:23 +04:00
|
|
|
prevalidator_limits: Prevalidator.limits ;
|
|
|
|
peer_validator_limits: Peer_validator.limits ;
|
|
|
|
max_child_ttl: int option ;
|
|
|
|
limits: limits;
|
|
|
|
}
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2018-01-22 20:21:23 +04:00
|
|
|
type state = {
|
|
|
|
parameters: parameters ;
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2018-01-22 20:21:23 +04:00
|
|
|
mutable bootstrapped: bool ;
|
|
|
|
bootstrapped_waiter: unit Lwt.t ;
|
|
|
|
bootstrapped_wakener: unit Lwt.u ;
|
|
|
|
valid_block_input: State.Block.t Lwt_watcher.input ;
|
|
|
|
new_head_input: State.Block.t Lwt_watcher.input ;
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2018-01-22 20:21:23 +04:00
|
|
|
mutable child:
|
|
|
|
(state * (unit -> unit Lwt.t (* shutdown *))) option ;
|
2018-05-17 13:55:22 +04:00
|
|
|
prevalidator: Prevalidator.t option ;
|
2018-01-24 15:48:25 +04:00
|
|
|
active_peers: Peer_validator.t Lwt.t P2p_peer.Table.t ;
|
|
|
|
bootstrapped_peers: unit P2p_peer.Table.t ;
|
2018-01-22 20:21:23 +04:00
|
|
|
}
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2018-01-22 20:21:23 +04:00
|
|
|
let view (state : state) _ : view =
|
|
|
|
let { bootstrapped ; active_peers ; bootstrapped_peers } = state in
|
|
|
|
{ bootstrapped ;
|
|
|
|
active_peers =
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_peer.Table.fold (fun id _ l -> id :: l) active_peers [] ;
|
2018-01-22 20:21:23 +04:00
|
|
|
bootstrapped_peers =
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_peer.Table.fold (fun id _ l -> id :: l) bootstrapped_peers [] }
|
2018-01-22 20:21:23 +04:00
|
|
|
end
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2018-01-22 20:21:23 +04:00
|
|
|
module Worker = Worker.Make (Name) (Event) (Request) (Types)
|
2017-11-13 17:25:02 +04:00
|
|
|
|
2018-01-22 20:21:23 +04:00
|
|
|
open Types
|
2017-11-13 17:25:02 +04:00
|
|
|
|
2018-01-22 20:21:23 +04:00
|
|
|
type t = Worker.infinite Worker.queue Worker.t
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2018-01-22 20:21:23 +04:00
|
|
|
let table = Worker.create_table Queue
|
|
|
|
|
|
|
|
let shutdown w =
|
|
|
|
Worker.shutdown w
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2018-01-22 20:21:23 +04:00
|
|
|
let shutdown_child nv =
|
|
|
|
Lwt_utils.may ~f:(fun (_, shutdown) -> shutdown ()) nv.child
|
|
|
|
|
|
|
|
let notify_new_block w block =
|
|
|
|
let nv = Worker.state w in
|
|
|
|
Option.iter nv.parameters.parent
|
|
|
|
~f:(fun id -> try
|
|
|
|
let w = List.assoc id (Worker.list table) in
|
|
|
|
let nv = Worker.state w in
|
|
|
|
Lwt_watcher.notify nv.valid_block_input block
|
|
|
|
with Not_found -> ()) ;
|
2017-11-27 09:13:12 +04:00
|
|
|
Lwt_watcher.notify nv.valid_block_input block ;
|
2018-01-22 20:21:23 +04:00
|
|
|
Lwt_watcher.notify nv.parameters.global_valid_block_input block ;
|
|
|
|
Worker.push_request_now w (Validated block)
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2018-02-16 04:26:24 +04:00
|
|
|
let may_toggle_bootstrapped_chain w =
|
2018-01-22 20:21:23 +04:00
|
|
|
let nv = Worker.state w in
|
2017-11-11 06:34:12 +04:00
|
|
|
if not nv.bootstrapped &&
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_peer.Table.length nv.bootstrapped_peers >= nv.parameters.limits.bootstrap_threshold
|
2017-11-11 06:34:12 +04:00
|
|
|
then begin
|
|
|
|
nv.bootstrapped <- true ;
|
|
|
|
Lwt.wakeup_later nv.bootstrapped_wakener () ;
|
|
|
|
end
|
|
|
|
|
2018-01-22 20:21:23 +04:00
|
|
|
let may_activate_peer_validator w peer_id =
|
|
|
|
let nv = Worker.state w in
|
2018-01-24 15:48:25 +04:00
|
|
|
try P2p_peer.Table.find nv.active_peers peer_id
|
2017-11-11 06:34:12 +04:00
|
|
|
with Not_found ->
|
|
|
|
let pv =
|
|
|
|
Peer_validator.create
|
2018-01-22 20:21:23 +04:00
|
|
|
~notify_new_block:(notify_new_block w)
|
2017-11-11 06:34:12 +04:00
|
|
|
~notify_bootstrapped: begin fun () ->
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_peer.Table.add nv.bootstrapped_peers peer_id () ;
|
2018-02-16 04:26:24 +04:00
|
|
|
may_toggle_bootstrapped_chain w
|
2017-11-11 06:34:12 +04:00
|
|
|
end
|
|
|
|
~notify_termination: begin fun _pv ->
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_peer.Table.remove nv.active_peers peer_id ;
|
|
|
|
P2p_peer.Table.remove nv.bootstrapped_peers peer_id ;
|
2017-11-11 06:34:12 +04:00
|
|
|
end
|
2018-01-22 20:21:23 +04:00
|
|
|
nv.parameters.peer_validator_limits
|
|
|
|
nv.parameters.block_validator
|
2018-02-16 04:26:24 +04:00
|
|
|
nv.parameters.chain_db
|
2018-01-22 20:21:23 +04:00
|
|
|
peer_id in
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_peer.Table.add nv.active_peers peer_id pv ;
|
2017-11-11 06:34:12 +04:00
|
|
|
pv
|
|
|
|
|
2018-05-10 18:30:41 +04:00
|
|
|
let may_update_checkpoint chain_state new_head =
|
|
|
|
State.Chain.checkpoint chain_state >>= fun (old_level, _old_block) ->
|
|
|
|
let new_level = State.Block.last_allowed_fork_level new_head in
|
|
|
|
if new_level <= old_level then
|
|
|
|
Lwt.return_unit
|
|
|
|
else
|
|
|
|
let head_level = State.Block.level new_head in
|
|
|
|
State.Block.predecessor_n new_head
|
|
|
|
(Int32.to_int (Int32.sub head_level new_level)) >>= function
|
|
|
|
| None -> Lwt.return_unit (* should not happen *)
|
|
|
|
| Some new_block ->
|
|
|
|
State.Chain.set_checkpoint chain_state (new_level, new_block)
|
|
|
|
|
2018-02-16 04:26:24 +04:00
|
|
|
let may_switch_test_chain w spawn_child block =
|
2018-01-22 20:21:23 +04:00
|
|
|
let nv = Worker.state w in
|
2017-11-11 06:34:12 +04:00
|
|
|
let create_child genesis protocol expiration =
|
2018-02-16 04:26:24 +04:00
|
|
|
if State.Chain.allow_forked_chain nv.parameters.chain_state then begin
|
2017-11-11 06:34:12 +04:00
|
|
|
shutdown_child nv >>= fun () ->
|
|
|
|
begin
|
2018-02-16 04:26:24 +04:00
|
|
|
let chain_id = Chain_id.of_block_hash (State.Block.hash genesis) in
|
|
|
|
State.Chain.get
|
|
|
|
(State.Chain.global_state nv.parameters.chain_state) chain_id >>= function
|
|
|
|
| Ok chain_state -> return chain_state
|
2017-11-11 06:34:12 +04:00
|
|
|
| Error _ ->
|
2018-02-16 04:26:24 +04:00
|
|
|
State.fork_testchain
|
|
|
|
genesis protocol expiration >>=? fun chain_state ->
|
|
|
|
Chain.head chain_state >>= fun new_genesis_block ->
|
2018-01-22 20:21:23 +04:00
|
|
|
Lwt_watcher.notify nv.parameters.global_valid_block_input new_genesis_block ;
|
2017-11-27 09:13:12 +04:00
|
|
|
Lwt_watcher.notify nv.valid_block_input new_genesis_block ;
|
2018-02-16 04:26:24 +04:00
|
|
|
return chain_state
|
|
|
|
end >>=? fun chain_state ->
|
2018-01-22 20:21:23 +04:00
|
|
|
spawn_child
|
2018-02-16 04:26:24 +04:00
|
|
|
~parent:(State.Chain.id chain_state)
|
2018-01-22 20:21:23 +04:00
|
|
|
nv.parameters.peer_validator_limits
|
|
|
|
nv.parameters.prevalidator_limits
|
|
|
|
nv.parameters.block_validator
|
|
|
|
nv.parameters.global_valid_block_input
|
2018-02-16 04:26:24 +04:00
|
|
|
nv.parameters.db chain_state
|
2018-01-22 20:21:23 +04:00
|
|
|
nv.parameters.limits (* TODO: different limits main/test ? *) >>= fun child ->
|
2017-11-11 06:34:12 +04:00
|
|
|
nv.child <- Some child ;
|
|
|
|
return ()
|
|
|
|
end else begin
|
|
|
|
(* Ignoring request... *)
|
|
|
|
return ()
|
|
|
|
end in
|
|
|
|
|
|
|
|
let check_child genesis protocol expiration current_time =
|
|
|
|
let activated =
|
|
|
|
match nv.child with
|
|
|
|
| None -> false
|
2018-01-22 20:21:23 +04:00
|
|
|
| Some (child , _) ->
|
2017-11-11 06:34:12 +04:00
|
|
|
Block_hash.equal
|
2018-02-16 04:26:24 +04:00
|
|
|
(State.Chain.genesis child.parameters.chain_state).block
|
2017-11-11 06:34:12 +04:00
|
|
|
genesis in
|
2018-02-16 04:26:24 +04:00
|
|
|
State.Block.read nv.parameters.chain_state genesis >>=? fun genesis ->
|
2017-11-11 06:34:12 +04:00
|
|
|
begin
|
2018-01-22 20:21:23 +04:00
|
|
|
match nv.parameters.max_child_ttl with
|
2017-11-11 06:34:12 +04:00
|
|
|
| None -> Lwt.return expiration
|
|
|
|
| Some ttl ->
|
|
|
|
Lwt.return
|
|
|
|
(Time.min expiration
|
|
|
|
(Time.add (State.Block.timestamp genesis) (Int64.of_int ttl)))
|
|
|
|
end >>= fun local_expiration ->
|
|
|
|
let expired = Time.(local_expiration <= current_time) in
|
|
|
|
if expired && activated then
|
|
|
|
shutdown_child nv >>= return
|
|
|
|
else if not activated && not expired then
|
|
|
|
create_child genesis protocol expiration
|
|
|
|
else
|
|
|
|
return () in
|
|
|
|
|
|
|
|
begin
|
|
|
|
let block_header = State.Block.header block in
|
2018-02-16 04:26:24 +04:00
|
|
|
State.Block.test_chain block >>= function
|
2017-11-11 06:34:12 +04:00
|
|
|
| Not_running -> shutdown_child nv >>= return
|
|
|
|
| Running { genesis ; protocol ; expiration } ->
|
|
|
|
check_child genesis protocol expiration
|
|
|
|
block_header.shell.timestamp
|
|
|
|
| Forking { protocol ; expiration } ->
|
|
|
|
create_child block protocol expiration
|
|
|
|
end >>= function
|
|
|
|
| Ok () -> Lwt.return_unit
|
|
|
|
| Error err ->
|
2018-02-16 04:26:24 +04:00
|
|
|
Worker.record_event w (Could_not_switch_testchain err) ;
|
2018-01-22 20:21:23 +04:00
|
|
|
Lwt.return_unit
|
|
|
|
|
|
|
|
let broadcast_head w ~previous block =
|
|
|
|
let nv = Worker.state w in
|
|
|
|
if not nv.bootstrapped then
|
|
|
|
Lwt.return_unit
|
|
|
|
else begin
|
|
|
|
begin
|
|
|
|
State.Block.predecessor block >>= function
|
|
|
|
| None -> Lwt.return_true
|
|
|
|
| Some predecessor ->
|
|
|
|
Lwt.return (State.Block.equal predecessor previous)
|
|
|
|
end >>= fun successor ->
|
|
|
|
if successor then begin
|
|
|
|
Distributed_db.Advertise.current_head
|
2018-02-16 04:26:24 +04:00
|
|
|
nv.parameters.chain_db block ;
|
2017-11-11 06:34:12 +04:00
|
|
|
Lwt.return_unit
|
2018-01-22 20:21:23 +04:00
|
|
|
end else begin
|
2018-03-21 18:38:41 +04:00
|
|
|
Distributed_db.Advertise.current_branch nv.parameters.chain_db
|
2018-01-22 20:21:23 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let on_request (type a) w spawn_child (req : a Request.t) : a tzresult Lwt.t =
|
|
|
|
let Request.Validated block = req in
|
|
|
|
let nv = Worker.state w in
|
2018-02-16 04:26:24 +04:00
|
|
|
Chain.head nv.parameters.chain_state >>= fun head ->
|
2018-01-22 20:21:23 +04:00
|
|
|
let head_header = State.Block.header head
|
|
|
|
and head_hash = State.Block.hash head
|
|
|
|
and block_header = State.Block.header block
|
|
|
|
and block_hash = State.Block.hash block in
|
|
|
|
if
|
|
|
|
Fitness.(block_header.shell.fitness <= head_header.shell.fitness)
|
|
|
|
then
|
|
|
|
return Event.Ignored_head
|
|
|
|
else begin
|
2018-02-16 04:26:24 +04:00
|
|
|
Chain.set_head nv.parameters.chain_state block >>= fun previous ->
|
2018-05-10 18:30:41 +04:00
|
|
|
may_update_checkpoint nv.parameters.chain_state block >>= fun () ->
|
2018-01-22 20:21:23 +04:00
|
|
|
broadcast_head w ~previous block >>= fun () ->
|
2018-05-17 13:55:22 +04:00
|
|
|
begin match nv.prevalidator with
|
|
|
|
| Some prevalidator ->
|
|
|
|
Prevalidator.flush prevalidator block_hash
|
|
|
|
| None -> return ()
|
|
|
|
end >>=? fun () ->
|
2018-02-16 04:26:24 +04:00
|
|
|
may_switch_test_chain w spawn_child block >>= fun () ->
|
2018-01-22 20:21:23 +04:00
|
|
|
Lwt_watcher.notify nv.new_head_input block ;
|
|
|
|
if Block_hash.equal head_hash block_header.shell.predecessor then
|
|
|
|
return Event.Head_incrememt
|
|
|
|
else
|
|
|
|
return Event.Branch_switch
|
|
|
|
end
|
|
|
|
|
|
|
|
let on_completion (type a) w (req : a Request.t) (update : a) request_status =
|
|
|
|
let Request.Validated block = req in
|
|
|
|
let fitness = State.Block.fitness block in
|
|
|
|
let request = State.Block.hash block in
|
|
|
|
Worker.record_event w (Processed_block { request ; request_status ; update ; fitness }) ;
|
|
|
|
Lwt.return ()
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2018-01-22 20:21:23 +04:00
|
|
|
let on_close w =
|
|
|
|
let nv = Worker.state w in
|
2018-02-16 04:26:24 +04:00
|
|
|
Distributed_db.deactivate nv.parameters.chain_db >>= fun () ->
|
2018-01-22 20:21:23 +04:00
|
|
|
Lwt.join
|
2018-05-17 13:55:22 +04:00
|
|
|
(begin match nv.prevalidator with
|
|
|
|
| Some prevalidator -> Prevalidator.shutdown prevalidator
|
|
|
|
| None -> Lwt.return_unit
|
|
|
|
end ::
|
2018-01-22 20:21:23 +04:00
|
|
|
Lwt_utils.may ~f:(fun (_, shutdown) -> shutdown ()) nv.child ::
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_peer.Table.fold
|
2018-01-22 20:21:23 +04:00
|
|
|
(fun _ pv acc -> (pv >>= Peer_validator.shutdown) :: acc)
|
|
|
|
nv.active_peers []) >>= fun () ->
|
|
|
|
Lwt.return_unit
|
|
|
|
|
2018-05-17 13:55:22 +04:00
|
|
|
let on_launch start_prevalidator w _ parameters =
|
2018-02-16 04:26:24 +04:00
|
|
|
Chain.init_head parameters.chain_state >>= fun () ->
|
2018-05-17 13:55:22 +04:00
|
|
|
(if start_prevalidator then
|
|
|
|
Prevalidator.create
|
|
|
|
parameters.prevalidator_limits parameters.chain_db >>= fun prevalidator ->
|
|
|
|
Lwt.return_some prevalidator
|
|
|
|
else Lwt.return_none) >>= fun prevalidator ->
|
2018-01-22 20:21:23 +04:00
|
|
|
let valid_block_input = Lwt_watcher.create_input () in
|
|
|
|
let new_head_input = Lwt_watcher.create_input () in
|
|
|
|
let bootstrapped_waiter, bootstrapped_wakener = Lwt.wait () in
|
|
|
|
let nv =
|
|
|
|
{ parameters ;
|
|
|
|
valid_block_input ;
|
|
|
|
new_head_input ;
|
|
|
|
bootstrapped_wakener ;
|
|
|
|
bootstrapped_waiter ;
|
|
|
|
bootstrapped = (parameters.limits.bootstrap_threshold <= 0) ;
|
|
|
|
active_peers =
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_peer.Table.create 50 ; (* TODO use `2 * max_connection` *)
|
2018-01-22 20:21:23 +04:00
|
|
|
bootstrapped_peers =
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_peer.Table.create 50 ; (* TODO use `2 * max_connection` *)
|
2018-01-22 20:21:23 +04:00
|
|
|
child = None ;
|
|
|
|
prevalidator } in
|
|
|
|
if nv.bootstrapped then Lwt.wakeup_later bootstrapped_wakener () ;
|
2018-02-16 04:26:24 +04:00
|
|
|
Distributed_db.set_callback parameters.chain_db {
|
2018-01-22 20:21:23 +04:00
|
|
|
notify_branch = begin fun peer_id locator ->
|
|
|
|
Lwt.async begin fun () ->
|
|
|
|
may_activate_peer_validator w peer_id >>= fun pv ->
|
|
|
|
Peer_validator.notify_branch pv locator ;
|
|
|
|
Lwt.return_unit
|
|
|
|
end
|
|
|
|
end ;
|
|
|
|
notify_head = begin fun peer_id block ops ->
|
|
|
|
Lwt.async begin fun () ->
|
|
|
|
may_activate_peer_validator w peer_id >>= fun pv ->
|
|
|
|
Peer_validator.notify_head pv block ;
|
|
|
|
(* TODO notify prevalidator only if head is known ??? *)
|
2018-05-17 13:55:22 +04:00
|
|
|
begin match nv.prevalidator with
|
|
|
|
| Some prevalidator ->
|
|
|
|
Prevalidator.notify_operations prevalidator peer_id ops
|
|
|
|
| None -> ()
|
|
|
|
end ;
|
2018-01-22 20:21:23 +04:00
|
|
|
Lwt.return_unit
|
|
|
|
end;
|
|
|
|
end ;
|
|
|
|
disconnection = begin fun peer_id ->
|
|
|
|
Lwt.async begin fun () ->
|
|
|
|
may_activate_peer_validator w peer_id >>= fun pv ->
|
|
|
|
Peer_validator.shutdown pv >>= fun () ->
|
|
|
|
Lwt.return_unit
|
|
|
|
end
|
|
|
|
end ;
|
|
|
|
} ;
|
|
|
|
Lwt.return nv
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2018-01-22 20:21:23 +04:00
|
|
|
let rec create
|
2018-05-17 13:55:22 +04:00
|
|
|
?max_child_ttl ~start_prevalidator ?parent
|
2018-01-22 20:21:23 +04:00
|
|
|
peer_validator_limits prevalidator_limits block_validator
|
2018-02-16 04:26:24 +04:00
|
|
|
global_valid_block_input db chain_state limits =
|
2018-01-22 20:21:23 +04:00
|
|
|
let spawn_child ~parent pvl pl bl gvbi db n l =
|
2018-05-17 13:55:22 +04:00
|
|
|
create ~start_prevalidator ~parent pvl pl bl gvbi db n l >>= fun w ->
|
2018-01-22 20:21:23 +04:00
|
|
|
Lwt.return (Worker.state w, (fun () -> Worker.shutdown w)) in
|
|
|
|
let module Handlers = struct
|
|
|
|
type self = t
|
2018-05-17 13:55:22 +04:00
|
|
|
let on_launch = on_launch start_prevalidator
|
2018-01-22 20:21:23 +04:00
|
|
|
let on_request w = on_request w spawn_child
|
|
|
|
let on_close = on_close
|
|
|
|
let on_error _ _ _ errs = Lwt.return (Error errs)
|
|
|
|
let on_completion = on_completion
|
|
|
|
let on_no_request _ = return ()
|
|
|
|
end in
|
|
|
|
let parameters =
|
|
|
|
{ max_child_ttl ;
|
|
|
|
parent ;
|
|
|
|
peer_validator_limits ;
|
|
|
|
prevalidator_limits ;
|
|
|
|
block_validator ;
|
|
|
|
global_valid_block_input ;
|
|
|
|
db ;
|
2018-02-16 04:26:24 +04:00
|
|
|
chain_db = Distributed_db.activate db chain_state ;
|
|
|
|
chain_state ;
|
2018-01-22 20:21:23 +04:00
|
|
|
limits } in
|
|
|
|
Worker.launch table
|
|
|
|
prevalidator_limits.worker_limits
|
2018-02-16 04:26:24 +04:00
|
|
|
(State.Chain.id chain_state)
|
2018-01-22 20:21:23 +04:00
|
|
|
parameters
|
|
|
|
(module Handlers)
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2018-01-22 20:21:23 +04:00
|
|
|
(** Current block computation *)
|
2017-11-11 06:34:12 +04:00
|
|
|
|
|
|
|
let create
|
|
|
|
?max_child_ttl
|
2018-05-17 13:55:22 +04:00
|
|
|
~start_prevalidator
|
2018-01-22 20:21:23 +04:00
|
|
|
peer_validator_limits prevalidator_limits
|
|
|
|
block_validator global_valid_block_input global_db state limits =
|
2017-11-11 06:34:12 +04:00
|
|
|
(* hide the optional ?parent *)
|
|
|
|
create
|
|
|
|
?max_child_ttl
|
2018-05-17 13:55:22 +04:00
|
|
|
~start_prevalidator
|
2018-01-22 20:21:23 +04:00
|
|
|
peer_validator_limits prevalidator_limits
|
|
|
|
block_validator global_valid_block_input global_db state limits
|
|
|
|
|
2018-02-16 04:26:24 +04:00
|
|
|
let chain_id w =
|
|
|
|
let { parameters = { chain_state } } = Worker.state w in
|
|
|
|
State.Chain.id chain_state
|
2018-01-22 20:21:23 +04:00
|
|
|
|
2018-02-16 04:26:24 +04:00
|
|
|
let chain_state w =
|
|
|
|
let { parameters = { chain_state } } = Worker.state w in
|
|
|
|
chain_state
|
2018-01-22 20:21:23 +04:00
|
|
|
|
|
|
|
let prevalidator w =
|
|
|
|
let { prevalidator } = Worker.state w in
|
|
|
|
prevalidator
|
|
|
|
|
2018-02-16 04:26:24 +04:00
|
|
|
let chain_db w =
|
|
|
|
let { parameters = { chain_db } } = Worker.state w in
|
|
|
|
chain_db
|
2018-01-22 20:21:23 +04:00
|
|
|
|
|
|
|
let child w =
|
|
|
|
match (Worker.state w).child with
|
|
|
|
| None -> None
|
2018-02-16 04:26:24 +04:00
|
|
|
| Some ({ parameters = { chain_state } }, _) ->
|
|
|
|
try Some (List.assoc (State.Chain.id chain_state) (Worker.list table))
|
2018-01-22 20:21:23 +04:00
|
|
|
with Not_found -> None
|
|
|
|
|
2018-05-10 18:57:58 +04:00
|
|
|
let assert_fitness_increases ?(force = false) w distant_header =
|
|
|
|
let pv = Worker.state w in
|
|
|
|
let chain_state = Distributed_db.chain_state pv.parameters.chain_db in
|
|
|
|
Chain.head chain_state >>= fun local_header ->
|
|
|
|
fail_when
|
|
|
|
(not force &&
|
|
|
|
Fitness.compare
|
|
|
|
distant_header.Block_header.shell.fitness
|
|
|
|
(State.Block.fitness local_header) <= 0)
|
|
|
|
(failure "Fitness too low")
|
|
|
|
|
|
|
|
let assert_checkpoint 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.Chain.acceptable_block chain_state hash header >>= fun acceptable ->
|
|
|
|
fail_unless acceptable
|
|
|
|
(Validation_errors.Checkpoint_error (hash, None))
|
|
|
|
|
|
|
|
let validate_block w ?force hash block operations =
|
2018-01-22 20:21:23 +04:00
|
|
|
let nv = Worker.state w in
|
2017-11-11 06:34:12 +04:00
|
|
|
assert (Block_hash.equal hash (Block_header.hash block)) ;
|
2018-05-10 18:57:58 +04:00
|
|
|
assert_fitness_increases ?force w block >>=? fun () ->
|
|
|
|
assert_checkpoint w hash block >>=? fun () ->
|
|
|
|
Block_validator.validate
|
|
|
|
~canceler:(Worker.canceler w)
|
|
|
|
~notify_new_block:(notify_new_block w)
|
|
|
|
nv.parameters.block_validator
|
|
|
|
nv.parameters.chain_db
|
|
|
|
hash block operations
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2018-01-22 20:21:23 +04:00
|
|
|
let bootstrapped w =
|
|
|
|
let { bootstrapped_waiter } = Worker.state w in
|
2017-12-29 18:29:29 +04:00
|
|
|
Lwt.protected bootstrapped_waiter
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2018-01-22 20:21:23 +04:00
|
|
|
let valid_block_watcher w =
|
|
|
|
let{ valid_block_input } = Worker.state w in
|
2017-11-27 09:13:12 +04:00
|
|
|
Lwt_watcher.create_stream valid_block_input
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2018-01-22 20:21:23 +04:00
|
|
|
let new_head_watcher w =
|
|
|
|
let { new_head_input } = Worker.state w in
|
2017-11-27 09:13:12 +04:00
|
|
|
Lwt_watcher.create_stream new_head_input
|
2018-01-22 20:21:23 +04:00
|
|
|
|
|
|
|
let status = Worker.status
|
|
|
|
|
|
|
|
let running_workers () = Worker.list table
|
|
|
|
|
|
|
|
let pending_requests t = Worker.pending_requests t
|
|
|
|
|
|
|
|
let current_request t = Worker.current_request t
|
|
|
|
|
|
|
|
let last_events = Worker.last_events
|