2016-09-08 21:13:10 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Copyright (c) 2014 - 2016. *)
|
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
|
|
|
open Logging.Node.Validator
|
|
|
|
|
2017-09-29 20:43:13 +04:00
|
|
|
type t = {
|
2017-09-29 20:43:13 +04:00
|
|
|
activate: ?parent:net_validator -> ?max_child_ttl:int -> State.Net.t -> net_validator Lwt.t ;
|
2017-09-29 20:43:13 +04:00
|
|
|
get: Net_id.t -> net_validator tzresult Lwt.t ;
|
|
|
|
get_exn: Net_id.t -> net_validator Lwt.t ;
|
|
|
|
deactivate: net_validator -> unit Lwt.t ;
|
2017-02-24 20:17:53 +04:00
|
|
|
inject_block:
|
2017-03-30 15:16:21 +04:00
|
|
|
?force:bool ->
|
2017-04-19 23:46:10 +04:00
|
|
|
MBytes.t -> Distributed_db.operation list list ->
|
|
|
|
(Block_hash.t * State.Block.t tzresult Lwt.t) tzresult Lwt.t ;
|
2017-04-19 21:21:23 +04:00
|
|
|
notify_block: Block_hash.t -> Block_header.t -> unit Lwt.t ;
|
2016-09-08 21:13:10 +04:00
|
|
|
shutdown: unit -> unit Lwt.t ;
|
2017-04-19 23:46:10 +04:00
|
|
|
valid_block_input: State.Block.t Watcher.input ;
|
2017-03-03 19:43:28 +04:00
|
|
|
db: Distributed_db.t ;
|
2016-09-08 21:13:10 +04:00
|
|
|
}
|
|
|
|
|
2017-09-29 20:43:13 +04:00
|
|
|
and net_validator = {
|
2016-09-08 21:13:10 +04:00
|
|
|
net: State.Net.t ;
|
2017-09-29 20:43:13 +04:00
|
|
|
worker: t ;
|
|
|
|
parent: net_validator option ;
|
|
|
|
mutable child: net_validator option ;
|
2016-09-08 21:13:10 +04:00
|
|
|
prevalidator: Prevalidator.t ;
|
2017-04-19 23:46:10 +04:00
|
|
|
net_db: Distributed_db.net_db ;
|
2017-04-19 21:21:23 +04:00
|
|
|
notify_block: Block_hash.t -> Block_header.t -> unit Lwt.t ;
|
2017-04-19 23:46:10 +04:00
|
|
|
fetch_block: Block_hash.t -> State.Block.t tzresult Lwt.t ;
|
2017-04-10 23:14:17 +04:00
|
|
|
create_child:
|
2017-04-19 23:46:10 +04:00
|
|
|
State.Block.t -> Protocol_hash.t -> Time.t -> unit tzresult Lwt.t ;
|
2017-04-10 23:14:17 +04:00
|
|
|
check_child:
|
|
|
|
Block_hash.t -> Protocol_hash.t -> Time.t -> Time.t -> unit tzresult Lwt.t ;
|
|
|
|
deactivate_child: unit -> unit Lwt.t ;
|
2017-09-29 20:43:13 +04:00
|
|
|
test_validator: unit -> (net_validator * Distributed_db.net_db) option ;
|
2016-09-08 21:13:10 +04:00
|
|
|
shutdown: unit -> unit Lwt.t ;
|
2017-04-19 23:46:10 +04:00
|
|
|
valid_block_input_for_net: State.Block.t Watcher.input ;
|
|
|
|
new_head_input: State.Block.t Watcher.input ;
|
2017-02-28 03:48:22 +04:00
|
|
|
bootstrapped: unit Lwt.t ;
|
2016-09-08 21:13:10 +04:00
|
|
|
}
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
let net_state { net } = net
|
|
|
|
let net_db { net_db } = net_db
|
|
|
|
|
2017-09-29 20:43:13 +04:00
|
|
|
let activate w ?max_child_ttl net = w.activate ?max_child_ttl net
|
2017-09-29 20:43:13 +04:00
|
|
|
let deactivate net_validator = net_validator.worker.deactivate net_validator
|
2016-09-08 21:13:10 +04:00
|
|
|
let get w = w.get
|
|
|
|
let get_exn w = w.get_exn
|
|
|
|
let notify_block w = w.notify_block
|
2017-02-24 20:17:53 +04:00
|
|
|
let inject_block w = w.inject_block
|
2016-09-08 21:13:10 +04:00
|
|
|
let shutdown w = w.shutdown ()
|
|
|
|
let test_validator w = w.test_validator ()
|
|
|
|
|
|
|
|
let fetch_block v = v.fetch_block
|
|
|
|
let prevalidator v = v.prevalidator
|
2017-02-28 03:48:22 +04:00
|
|
|
let bootstrapped v = v.bootstrapped
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
(** Current block computation *)
|
|
|
|
|
2017-03-03 19:43:28 +04:00
|
|
|
let fetch_protocol v hash =
|
|
|
|
lwt_log_notice "Fetching protocol %a"
|
|
|
|
Protocol_hash.pp_short hash >>= fun () ->
|
2017-11-08 15:42:09 +04:00
|
|
|
Distributed_db.Protocol.fetch v.worker.db hash () >>=? fun protocol ->
|
2017-03-03 19:43:28 +04:00
|
|
|
Updater.compile hash protocol >>= fun valid ->
|
|
|
|
if valid then begin
|
|
|
|
lwt_log_notice "Successfully compiled protocol %a"
|
|
|
|
Protocol_hash.pp_short hash >>= fun () ->
|
2017-04-19 23:46:10 +04:00
|
|
|
Distributed_db.commit_protocol v.worker.db hash >>=? fun _ ->
|
2017-03-03 19:43:28 +04:00
|
|
|
return true
|
|
|
|
end else begin
|
|
|
|
lwt_log_error "Failed to compile protocol %a"
|
|
|
|
Protocol_hash.pp_short hash >>= fun () ->
|
|
|
|
failwith "Cannot compile the protocol %a" Protocol_hash.pp_short hash
|
|
|
|
end
|
|
|
|
|
2017-04-19 23:46:10 +04:00
|
|
|
let fetch_protocols v (block: State.Block.t) =
|
|
|
|
State.Block.context block >>= fun context ->
|
2017-03-03 19:43:28 +04:00
|
|
|
let proto_updated =
|
2017-04-19 23:46:10 +04:00
|
|
|
Context.get_protocol context >>= fun protocol_hash ->
|
2017-10-09 12:55:12 +04:00
|
|
|
match State.Registred_protocol.get protocol_hash with
|
2017-03-03 19:43:28 +04:00
|
|
|
| Some _ -> return false
|
2017-04-19 23:46:10 +04:00
|
|
|
| None -> fetch_protocol v protocol_hash
|
2017-03-03 19:43:28 +04:00
|
|
|
and test_proto_updated =
|
2017-04-19 23:46:10 +04:00
|
|
|
Context.get_test_network context >>= function
|
2017-04-10 23:14:17 +04:00
|
|
|
| Not_running -> return false
|
|
|
|
| Forking { protocol }
|
|
|
|
| Running { protocol } ->
|
2017-10-09 12:55:12 +04:00
|
|
|
match State.Registred_protocol.get protocol with
|
2017-04-19 23:46:10 +04:00
|
|
|
| Some _ -> return false
|
|
|
|
| None -> fetch_protocol v protocol in
|
2017-03-03 19:43:28 +04:00
|
|
|
proto_updated >>=? fun proto_updated ->
|
2017-04-19 23:46:10 +04:00
|
|
|
test_proto_updated >>=? fun test_proto_updated ->
|
|
|
|
return (proto_updated && test_proto_updated)
|
|
|
|
|
|
|
|
let rec may_set_head v (block: State.Block.t) =
|
|
|
|
Chain.head v.net >>= fun head ->
|
|
|
|
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.compare
|
|
|
|
head_header.shell.fitness block_header.shell.fitness >= 0
|
|
|
|
then
|
2016-09-08 21:13:10 +04:00
|
|
|
Lwt.return_unit
|
2017-03-03 19:43:28 +04:00
|
|
|
else begin
|
2017-04-19 23:46:10 +04:00
|
|
|
Chain.test_and_set_head v.net ~old:head block >>= function
|
2016-09-08 21:13:10 +04:00
|
|
|
| false -> may_set_head v block
|
|
|
|
| true ->
|
2017-11-11 06:34:12 +04:00
|
|
|
Distributed_db.Advertise.current_head v.net_db block ;
|
2017-02-24 20:17:53 +04:00
|
|
|
Prevalidator.flush v.prevalidator block ;
|
2017-04-10 23:14:17 +04:00
|
|
|
begin
|
|
|
|
begin
|
2017-04-19 23:46:10 +04:00
|
|
|
State.Block.test_network block >>= function
|
2017-04-10 23:14:17 +04:00
|
|
|
| Not_running -> v.deactivate_child () >>= return
|
|
|
|
| Running { genesis ; protocol ; expiration } ->
|
2017-04-19 23:46:10 +04:00
|
|
|
v.check_child genesis protocol expiration
|
|
|
|
block_header.shell.timestamp
|
2017-04-10 23:14:17 +04:00
|
|
|
| Forking { protocol ; expiration } ->
|
|
|
|
v.create_child block protocol expiration
|
|
|
|
end >>= function
|
|
|
|
| Ok () -> Lwt.return_unit
|
|
|
|
| Error err ->
|
|
|
|
lwt_log_error "@[<v 2>Error while switch test network:@ %a@]"
|
|
|
|
Error_monad.pp_print_error err
|
|
|
|
end >>= fun () ->
|
2017-02-28 03:48:22 +04:00
|
|
|
Watcher.notify v.new_head_input block ;
|
2016-09-08 21:13:10 +04:00
|
|
|
lwt_log_notice "update current head %a %a %a(%t)"
|
2017-04-19 23:46:10 +04:00
|
|
|
Block_hash.pp_short block_hash
|
|
|
|
Fitness.pp block_header.shell.fitness
|
|
|
|
Time.pp_hum block_header.shell.timestamp
|
2016-09-08 21:13:10 +04:00
|
|
|
(fun ppf ->
|
2017-04-19 23:46:10 +04:00
|
|
|
if Block_hash.equal head_hash block_header.shell.predecessor then
|
2016-09-08 21:13:10 +04:00
|
|
|
Format.fprintf ppf "same branch"
|
|
|
|
else
|
|
|
|
Format.fprintf ppf "changing branch") >>= fun () ->
|
|
|
|
Lwt.return_unit
|
2017-03-03 19:43:28 +04:00
|
|
|
end
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
(** Block validation *)
|
|
|
|
|
2017-02-28 21:46:40 +04:00
|
|
|
type error +=
|
|
|
|
| Invalid_operation of Operation_hash.t
|
2017-04-19 23:46:10 +04:00
|
|
|
| Invalid_fitness of { block: Block_hash.t ;
|
|
|
|
expected: Fitness.t ;
|
|
|
|
found: Fitness.t }
|
|
|
|
| Unknown_protocol
|
2017-02-28 21:46:40 +04:00
|
|
|
| Non_increasing_timestamp
|
|
|
|
| Non_increasing_fitness
|
2017-04-10 15:01:22 +04:00
|
|
|
| Wrong_level of Int32.t * Int32.t
|
2017-04-12 20:22:40 +04:00
|
|
|
| Wrong_proto_level of int * int
|
2017-04-20 10:49:14 +04:00
|
|
|
| Replayed_operation of Operation_hash.t
|
|
|
|
| Outdated_operation of Operation_hash.t * Block_hash.t
|
2017-04-10 15:01:22 +04:00
|
|
|
|
|
|
|
let () =
|
2017-04-19 23:46:10 +04:00
|
|
|
Error_monad.register_error_kind
|
|
|
|
`Permanent
|
|
|
|
~id:"validator.invalid_fitness"
|
|
|
|
~title:"Invalid fitness"
|
|
|
|
~description:"The computed fitness differs from the fitness found \
|
|
|
|
\ in the block header."
|
|
|
|
~pp:(fun ppf (block, expected, found) ->
|
|
|
|
Format.fprintf ppf
|
|
|
|
"@[<v 2>Invalid fitness for block %a@ \
|
|
|
|
\ expected %a@ \
|
|
|
|
\ found %a"
|
|
|
|
Block_hash.pp_short block
|
|
|
|
Fitness.pp expected
|
|
|
|
Fitness.pp found)
|
|
|
|
Data_encoding.(obj3
|
|
|
|
(req "block" Block_hash.encoding)
|
|
|
|
(req "expected" Fitness.encoding)
|
|
|
|
(req "found" Fitness.encoding))
|
|
|
|
(function Invalid_fitness { block ; expected ; found } ->
|
|
|
|
Some (block, expected, found) | _ -> None)
|
|
|
|
(fun (block, expected, found) ->
|
|
|
|
Invalid_fitness { block ; expected ; found }) ;
|
2017-04-10 15:01:22 +04:00
|
|
|
register_error_kind
|
|
|
|
`Permanent
|
|
|
|
~id:"validator.wrong_level"
|
|
|
|
~title:"Wrong level"
|
|
|
|
~description:"The block level is not the expected one"
|
|
|
|
~pp:(fun ppf (e, g) ->
|
|
|
|
Format.fprintf ppf
|
|
|
|
"The declared level %ld is not %ld" g e)
|
|
|
|
Data_encoding.(obj2
|
|
|
|
(req "expected" int32)
|
|
|
|
(req "provided" int32))
|
|
|
|
(function Wrong_level (e, g) -> Some (e, g) | _ -> None)
|
2017-04-12 20:22:40 +04:00
|
|
|
(fun (e, g) -> Wrong_level (e, g)) ;
|
|
|
|
register_error_kind
|
|
|
|
`Permanent
|
|
|
|
~id:"validator.wrong_proto_level"
|
|
|
|
~title:"Wrong protocol level"
|
|
|
|
~description:"The protocol level is not the expected one"
|
|
|
|
~pp:(fun ppf (e, g) ->
|
|
|
|
Format.fprintf ppf
|
|
|
|
"The declared protocol level %d is not %d" g e)
|
|
|
|
Data_encoding.(obj2
|
|
|
|
(req "expected" uint8)
|
|
|
|
(req "provided" uint8))
|
|
|
|
(function Wrong_proto_level (e, g) -> Some (e, g) | _ -> None)
|
2017-04-20 10:49:14 +04:00
|
|
|
(fun (e, g) -> Wrong_proto_level (e, g)) ;
|
|
|
|
register_error_kind
|
|
|
|
`Permanent
|
|
|
|
~id:"validator.replayed_operation"
|
|
|
|
~title:"Replayed operation"
|
|
|
|
~description:"The block contains an operation that was previously \
|
|
|
|
included in the chain"
|
|
|
|
~pp:(fun ppf oph ->
|
|
|
|
Format.fprintf ppf
|
|
|
|
"The operation %a was previously included in the chain."
|
|
|
|
Operation_hash.pp oph)
|
|
|
|
Data_encoding.(obj1 (req "hash" Operation_hash.encoding))
|
|
|
|
(function Replayed_operation oph -> Some oph | _ -> None)
|
|
|
|
(function oph -> Replayed_operation oph) ;
|
|
|
|
register_error_kind
|
|
|
|
`Permanent
|
|
|
|
~id:"validator.outdated_operations"
|
|
|
|
~title:"Outdated operation"
|
|
|
|
~description:"The block contains an operation which is outdated."
|
|
|
|
~pp:(fun ppf (oph, bh)->
|
|
|
|
Format.fprintf ppf
|
|
|
|
"The operation %a is outdated (%a)"
|
|
|
|
Operation_hash.pp oph
|
|
|
|
Block_hash.pp bh)
|
|
|
|
Data_encoding.(obj2
|
|
|
|
(req "operation" Operation_hash.encoding)
|
|
|
|
(req "block" Block_hash.encoding))
|
|
|
|
(function Outdated_operation (oph, bh) -> Some (oph, bh) | _ -> None)
|
|
|
|
(function (oph, bh) -> Outdated_operation (oph, bh))
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-04-19 23:46:10 +04:00
|
|
|
let apply_block net_state db
|
|
|
|
(pred: State.Block.t) hash (block: Block_header.t) =
|
|
|
|
let pred_header = State.Block.header pred
|
|
|
|
and pred_hash = State.Block.hash pred in
|
|
|
|
State.Block.context pred >>= fun pred_context ->
|
|
|
|
let id = State.Net.id net_state in
|
2016-09-08 21:13:10 +04:00
|
|
|
lwt_log_notice "validate block %a (after %a), net %a"
|
|
|
|
Block_hash.pp_short hash
|
|
|
|
Block_hash.pp_short block.shell.predecessor
|
2017-04-20 10:49:14 +04:00
|
|
|
Net_id.pp id >>= fun () ->
|
2017-04-10 15:01:22 +04:00
|
|
|
fail_unless
|
2017-04-19 23:46:10 +04:00
|
|
|
(Int32.succ pred_header.shell.level = block.shell.level)
|
|
|
|
(Wrong_level (Int32.succ pred_header.shell.level,
|
|
|
|
block.shell.level)) >>=? fun () ->
|
2016-09-08 21:13:10 +04:00
|
|
|
lwt_log_info "validation of %a: looking for dependencies..."
|
|
|
|
Block_hash.pp_short hash >>= fun () ->
|
2017-04-19 23:46:10 +04:00
|
|
|
Distributed_db.Operations.fetch
|
2017-11-08 15:42:09 +04:00
|
|
|
db (hash, 0) block.shell.operations_hash >>=? fun operations ->
|
2017-09-29 20:43:13 +04:00
|
|
|
fail_unless (block.shell.validation_passes <= 1)
|
|
|
|
(* TODO constant to be exported from the protocol... *)
|
|
|
|
(failure "unexpected error (TO BE REMOVED)") >>=? fun () ->
|
2017-04-19 23:46:10 +04:00
|
|
|
let operation_hashes = List.map Operation.hash operations in
|
2016-09-08 21:13:10 +04:00
|
|
|
lwt_debug "validation of %a: found operations"
|
|
|
|
Block_hash.pp_short hash >>= fun () ->
|
|
|
|
begin (* Are we validating a block in an expired test network ? *)
|
2017-04-19 23:46:10 +04:00
|
|
|
match State.Net.expiration net_state with
|
2016-09-08 21:13:10 +04:00
|
|
|
| Some eol when Time.(eol <= block.shell.timestamp) ->
|
|
|
|
failwith "This test network expired..."
|
|
|
|
| None | Some _ -> return ()
|
|
|
|
end >>=? fun () ->
|
2017-02-28 21:46:40 +04:00
|
|
|
begin
|
2017-04-19 23:46:10 +04:00
|
|
|
if Time.(pred_header.shell.timestamp >= block.shell.timestamp) then
|
2017-02-28 21:46:40 +04:00
|
|
|
fail Non_increasing_timestamp
|
|
|
|
else
|
|
|
|
return ()
|
|
|
|
end >>=? fun () ->
|
|
|
|
begin
|
2017-04-19 23:46:10 +04:00
|
|
|
if Fitness.compare pred_header.shell.fitness block.shell.fitness >= 0 then
|
2017-02-28 21:46:40 +04:00
|
|
|
fail Non_increasing_fitness
|
|
|
|
else
|
|
|
|
return ()
|
|
|
|
end >>=? fun () ->
|
2017-04-20 10:49:14 +04:00
|
|
|
begin
|
|
|
|
Chain_traversal.live_blocks
|
|
|
|
pred (State.Block.max_operations_ttl pred) >>= fun (live_blocks,
|
|
|
|
live_operations) ->
|
|
|
|
let rec assert_no_duplicates live_operations = function
|
|
|
|
| [] -> return ()
|
|
|
|
| oph :: ophs ->
|
|
|
|
if Operation_hash.Set.mem oph live_operations then
|
|
|
|
fail (Replayed_operation oph)
|
|
|
|
else
|
|
|
|
assert_no_duplicates
|
|
|
|
(Operation_hash.Set.add oph live_operations) ophs in
|
|
|
|
let assert_live operations =
|
|
|
|
List.fold_left
|
|
|
|
(fun acc op ->
|
|
|
|
acc >>=? fun () ->
|
|
|
|
fail_unless
|
|
|
|
(Block_hash.Set.mem op.Operation.shell.branch live_blocks)
|
|
|
|
(Outdated_operation (Operation.hash op, op.shell.branch)))
|
|
|
|
(return ()) operations in
|
|
|
|
assert_no_duplicates live_operations operation_hashes >>=? fun () ->
|
|
|
|
assert_live operations
|
|
|
|
end >>=? fun () ->
|
2017-04-19 23:46:10 +04:00
|
|
|
Context.get_protocol pred_context >>= fun pred_protocol_hash ->
|
2016-09-08 21:13:10 +04:00
|
|
|
begin
|
2017-10-09 12:55:12 +04:00
|
|
|
match State.Registred_protocol.get pred_protocol_hash with
|
2017-04-19 23:46:10 +04:00
|
|
|
| None -> fail Unknown_protocol
|
2017-04-10 14:14:11 +04:00
|
|
|
| Some p -> return p
|
|
|
|
end >>=? fun (module Proto) ->
|
2016-09-08 21:13:10 +04:00
|
|
|
lwt_debug "validation of %a: Proto %a"
|
|
|
|
Block_hash.pp_short hash
|
|
|
|
Protocol_hash.pp_short Proto.hash >>= fun () ->
|
|
|
|
lwt_debug "validation of %a: parsing header..."
|
|
|
|
Block_hash.pp_short hash >>= fun () ->
|
|
|
|
lwt_debug "validation of %a: parsing operations..."
|
|
|
|
Block_hash.pp_short hash >>= fun () ->
|
|
|
|
map2_s
|
|
|
|
(fun op_hash raw ->
|
|
|
|
Lwt.return (Proto.parse_operation op_hash raw)
|
|
|
|
|> trace (Invalid_operation op_hash))
|
2017-03-30 15:16:21 +04:00
|
|
|
operation_hashes
|
2017-02-24 20:17:53 +04:00
|
|
|
operations >>=? fun parsed_operations ->
|
2016-09-08 21:13:10 +04:00
|
|
|
lwt_debug "validation of %a: applying block..."
|
|
|
|
Block_hash.pp_short hash >>= fun () ->
|
2017-04-10 23:14:17 +04:00
|
|
|
Context.reset_test_network
|
2017-04-19 23:46:10 +04:00
|
|
|
pred_context pred_hash block.shell.timestamp >>= fun context ->
|
2016-10-20 20:54:16 +04:00
|
|
|
Proto.begin_application
|
2017-04-10 23:14:17 +04:00
|
|
|
~predecessor_context:context
|
2017-04-19 23:46:10 +04:00
|
|
|
~predecessor_timestamp:pred_header.shell.timestamp
|
|
|
|
~predecessor_fitness:pred_header.shell.fitness
|
2016-10-20 20:54:16 +04:00
|
|
|
block >>=? fun state ->
|
|
|
|
fold_left_s (fun state op ->
|
|
|
|
Proto.apply_operation state op >>=? fun state ->
|
|
|
|
return state)
|
|
|
|
state parsed_operations >>=? fun state ->
|
|
|
|
Proto.finalize_block state >>=? fun new_context ->
|
2017-04-12 20:22:40 +04:00
|
|
|
Context.get_protocol new_context.context >>= fun new_protocol ->
|
|
|
|
let expected_proto_level =
|
2017-04-19 23:46:10 +04:00
|
|
|
if Protocol_hash.equal new_protocol pred_protocol_hash then
|
|
|
|
pred_header.shell.proto_level
|
2017-04-12 20:22:40 +04:00
|
|
|
else
|
2017-04-19 23:46:10 +04:00
|
|
|
(pred_header.shell.proto_level + 1) mod 256 in
|
2017-04-12 20:22:40 +04:00
|
|
|
fail_when (block.shell.proto_level <> expected_proto_level)
|
|
|
|
(Wrong_proto_level (block.shell.proto_level, expected_proto_level))
|
|
|
|
>>=? fun () ->
|
2017-04-19 23:46:10 +04:00
|
|
|
fail_unless
|
|
|
|
(Fitness.equal new_context.fitness block.shell.fitness)
|
|
|
|
(Invalid_fitness
|
|
|
|
{ block = hash ;
|
|
|
|
expected = block.shell.fitness ;
|
|
|
|
found = new_context.fitness ;
|
|
|
|
}) >>=? fun () ->
|
2017-04-20 10:49:14 +04:00
|
|
|
let max_operations_ttl =
|
|
|
|
max 0
|
|
|
|
(min
|
|
|
|
((State.Block.max_operations_ttl pred)+1)
|
|
|
|
new_context.max_operations_ttl) in
|
|
|
|
let new_context =
|
|
|
|
{ new_context with max_operations_ttl } in
|
2016-09-08 21:13:10 +04:00
|
|
|
lwt_log_info "validation of %a: success"
|
|
|
|
Block_hash.pp_short hash >>= fun () ->
|
|
|
|
return new_context
|
|
|
|
|
|
|
|
(** *)
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
module Context_db = struct
|
|
|
|
|
|
|
|
type data =
|
2017-09-29 20:43:13 +04:00
|
|
|
{ validator: net_validator ;
|
2017-04-19 21:21:23 +04:00
|
|
|
state: [ `Inited of Block_header.t tzresult
|
|
|
|
| `Initing of Block_header.t tzresult Lwt.t
|
2017-04-19 23:46:10 +04:00
|
|
|
| `Running of State.Block.t tzresult Lwt.t ] ;
|
|
|
|
wakener: State.Block.t tzresult Lwt.u }
|
2017-02-24 20:17:53 +04:00
|
|
|
|
2017-03-02 17:45:23 +04:00
|
|
|
type context =
|
2017-02-24 20:17:53 +04:00
|
|
|
{ tbl : data Block_hash.Table.t ;
|
|
|
|
canceler : Lwt_utils.Canceler.t ;
|
|
|
|
worker_trigger: unit -> unit;
|
|
|
|
worker_waiter: unit -> unit Lwt.t ;
|
|
|
|
worker: unit Lwt.t ;
|
2017-04-19 23:46:10 +04:00
|
|
|
net_db : Distributed_db.net_db ;
|
2017-03-02 17:45:23 +04:00
|
|
|
net_state : State.Net.t }
|
2017-02-24 20:17:53 +04:00
|
|
|
|
|
|
|
let pending_requests { tbl } =
|
|
|
|
Block_hash.Table.fold
|
|
|
|
(fun h data acc ->
|
|
|
|
match data.state with
|
|
|
|
| `Initing _ -> acc
|
2017-03-02 17:45:23 +04:00
|
|
|
| `Running _ -> acc
|
|
|
|
| `Inited d -> (h, d, data) :: acc)
|
2017-02-24 20:17:53 +04:00
|
|
|
tbl []
|
|
|
|
|
|
|
|
let pending { tbl } hash = Block_hash.Table.mem tbl hash
|
|
|
|
|
2017-03-02 17:45:23 +04:00
|
|
|
let request validator { tbl ; worker_trigger ; net_db } hash =
|
2017-02-24 20:17:53 +04:00
|
|
|
assert (not (Block_hash.Table.mem tbl hash));
|
|
|
|
let waiter, wakener = Lwt.wait () in
|
|
|
|
let data =
|
2017-11-08 15:42:09 +04:00
|
|
|
Distributed_db.Block_header.fetch net_db hash () in
|
2017-02-24 20:17:53 +04:00
|
|
|
match Lwt.state data with
|
|
|
|
| Lwt.Return data ->
|
|
|
|
let state = `Inited data in
|
|
|
|
Block_hash.Table.add tbl hash { validator ; state ; wakener } ;
|
|
|
|
worker_trigger () ;
|
|
|
|
waiter
|
|
|
|
| _ ->
|
|
|
|
let state = `Initing data in
|
|
|
|
Block_hash.Table.add tbl hash { validator ; state ; wakener } ;
|
|
|
|
Lwt.async
|
|
|
|
(fun () ->
|
|
|
|
data >>= fun data ->
|
|
|
|
let state = `Inited data in
|
2017-02-26 05:03:06 +04:00
|
|
|
Block_hash.Table.replace tbl hash { validator ; state ; wakener } ;
|
2017-02-24 20:17:53 +04:00
|
|
|
worker_trigger () ;
|
|
|
|
Lwt.return_unit) ;
|
|
|
|
waiter
|
|
|
|
|
2017-03-02 17:45:23 +04:00
|
|
|
let prefetch validator ({ net_state ; tbl } as session) hash =
|
2017-02-24 20:17:53 +04:00
|
|
|
Lwt.ignore_result
|
2017-04-19 23:46:10 +04:00
|
|
|
(State.Block.known_valid net_state hash >>= fun exists ->
|
2017-02-24 20:17:53 +04:00
|
|
|
if not exists && not (Block_hash.Table.mem tbl hash) then
|
2017-03-02 17:45:23 +04:00
|
|
|
request validator session hash >>= fun _ -> Lwt.return_unit
|
2017-02-24 20:17:53 +04:00
|
|
|
else
|
|
|
|
Lwt.return_unit)
|
|
|
|
|
2017-03-02 17:45:23 +04:00
|
|
|
let known { net_state } hash =
|
2017-04-19 23:46:10 +04:00
|
|
|
State.Block.known_valid net_state hash
|
2017-02-24 20:17:53 +04:00
|
|
|
|
2017-03-02 17:45:23 +04:00
|
|
|
let read { net_state } hash =
|
2017-04-19 23:46:10 +04:00
|
|
|
State.Block.read net_state hash
|
2017-02-24 20:17:53 +04:00
|
|
|
|
2017-03-02 17:45:23 +04:00
|
|
|
let fetch ({ net_state ; tbl } as session) validator hash =
|
2017-02-24 20:17:53 +04:00
|
|
|
try Lwt.waiter_of_wakener (Block_hash.Table.find tbl hash).wakener
|
|
|
|
with Not_found ->
|
2017-04-19 23:46:10 +04:00
|
|
|
State.Block.known_invalid net_state hash >>= fun known_invalid ->
|
|
|
|
if known_invalid then
|
|
|
|
Lwt.return (Error [failure "Invalid predecessor"])
|
|
|
|
else
|
|
|
|
State.Block.read_opt net_state hash >>= function
|
|
|
|
| Some op ->
|
|
|
|
Lwt.return (Ok op)
|
|
|
|
| None ->
|
|
|
|
try Lwt.waiter_of_wakener (Block_hash.Table.find tbl hash).wakener
|
|
|
|
with Not_found -> request validator session hash
|
|
|
|
|
|
|
|
let store { net_db ; tbl } hash data =
|
2017-02-24 20:17:53 +04:00
|
|
|
begin
|
|
|
|
match data with
|
2017-04-19 23:46:10 +04:00
|
|
|
| Ok data -> begin
|
2017-09-29 20:43:13 +04:00
|
|
|
Distributed_db.commit_block net_db hash data >>=? function
|
2017-04-19 23:46:10 +04:00
|
|
|
| None ->
|
|
|
|
(* Should not happen if the block is not validated twice *)
|
|
|
|
assert false
|
|
|
|
| Some block ->
|
|
|
|
return (Ok block)
|
2017-02-26 05:03:06 +04:00
|
|
|
end
|
2017-02-24 20:17:53 +04:00
|
|
|
| Error err ->
|
2017-09-29 20:43:13 +04:00
|
|
|
Distributed_db.commit_invalid_block net_db hash >>=? fun changed ->
|
2017-04-19 23:46:10 +04:00
|
|
|
assert changed ;
|
|
|
|
return (Error err)
|
2017-02-26 05:03:06 +04:00
|
|
|
end >>= function
|
2017-04-19 23:46:10 +04:00
|
|
|
| Ok block ->
|
2017-02-26 05:03:06 +04:00
|
|
|
let wakener = (Block_hash.Table.find tbl hash).wakener in
|
|
|
|
Block_hash.Table.remove tbl hash;
|
|
|
|
Lwt.wakeup wakener block ;
|
2017-04-19 23:46:10 +04:00
|
|
|
Lwt.return_unit
|
2017-02-26 05:03:06 +04:00
|
|
|
| Error _ as err ->
|
|
|
|
let wakener = (Block_hash.Table.find tbl hash).wakener in
|
|
|
|
Block_hash.Table.remove tbl hash;
|
|
|
|
Lwt.wakeup wakener err ;
|
2017-04-19 23:46:10 +04:00
|
|
|
Lwt.return_unit
|
2017-02-24 20:17:53 +04:00
|
|
|
|
2017-09-29 20:43:13 +04:00
|
|
|
let process (v: net_validator) ~get_context ~set_context hash block =
|
2017-11-09 14:26:25 +04:00
|
|
|
let net_state = Distributed_db.net_state v.net_db in
|
2017-04-19 21:21:23 +04:00
|
|
|
get_context v block.Block_header.shell.predecessor >>= function
|
2017-03-02 17:45:23 +04:00
|
|
|
| Error _ as error ->
|
2017-10-31 20:59:14 +04:00
|
|
|
Lwt_unix.yield () >>= fun () ->
|
2017-03-02 17:45:23 +04:00
|
|
|
set_context v hash (Error [(* TODO *)]) >>= fun () ->
|
|
|
|
Lwt.return error
|
|
|
|
| Ok _context ->
|
|
|
|
lwt_debug "process %a" Block_hash.pp_short hash >>= fun () ->
|
|
|
|
begin
|
2017-04-19 23:46:10 +04:00
|
|
|
Chain.genesis net_state >>= fun genesis ->
|
|
|
|
if Block_hash.equal (State.Block.hash genesis)
|
|
|
|
block.shell.predecessor then
|
2017-03-02 17:45:23 +04:00
|
|
|
Lwt.return genesis
|
|
|
|
else
|
2017-04-19 23:46:10 +04:00
|
|
|
State.Block.read_exn net_state block.shell.predecessor
|
2017-03-02 17:45:23 +04:00
|
|
|
end >>= fun pred ->
|
2017-04-19 23:46:10 +04:00
|
|
|
apply_block net_state v.net_db pred hash block >>= function
|
|
|
|
| Error ([Unknown_protocol] as err) as error ->
|
2017-03-02 17:45:23 +04:00
|
|
|
lwt_log_error
|
|
|
|
"@[<v 2>Ignoring block %a@ %a@]"
|
|
|
|
Block_hash.pp_short hash
|
|
|
|
Error_monad.pp_print_error err >>= fun () ->
|
|
|
|
Lwt.return error
|
|
|
|
| Error exns as error ->
|
|
|
|
set_context v hash error >>= fun () ->
|
|
|
|
lwt_warn "Failed to validate block %a."
|
|
|
|
Block_hash.pp_short hash >>= fun () ->
|
|
|
|
lwt_debug "%a" Error_monad.pp_print_error exns >>= fun () ->
|
|
|
|
Lwt.return error
|
|
|
|
| Ok new_context ->
|
|
|
|
(* The sanity check `set_context` detects differences
|
|
|
|
between the computed fitness and the fitness announced
|
2017-04-19 23:46:10 +04:00
|
|
|
in the block header. Then `Block.read` will
|
2017-03-02 17:45:23 +04:00
|
|
|
return an error. *)
|
|
|
|
set_context v hash (Ok new_context) >>= fun () ->
|
2017-04-19 23:46:10 +04:00
|
|
|
State.Block.read net_state hash >>= function
|
2017-03-02 17:45:23 +04:00
|
|
|
| Error err as error ->
|
|
|
|
lwt_log_error
|
|
|
|
"@[<v 2>Ignoring block %a@ %a@]"
|
|
|
|
Block_hash.pp_short hash
|
|
|
|
Error_monad.pp_print_error err >>= fun () ->
|
|
|
|
Lwt.return error
|
|
|
|
| Ok block ->
|
|
|
|
lwt_debug
|
|
|
|
"validation of %a: reevaluate current block"
|
|
|
|
Block_hash.pp_short hash >>= fun () ->
|
|
|
|
Watcher.notify v.worker.valid_block_input block ;
|
2017-04-19 23:46:10 +04:00
|
|
|
Watcher.notify v.valid_block_input_for_net block ;
|
|
|
|
fetch_protocols v block >>=? fun _fetched ->
|
2017-03-02 17:45:23 +04:00
|
|
|
may_set_head v block >>= fun () ->
|
|
|
|
return block
|
|
|
|
|
|
|
|
let request session ~get_context ~set_context pendings =
|
|
|
|
let time = Time.now () in
|
|
|
|
let min_block b pb =
|
|
|
|
match pb with
|
|
|
|
| None -> Some b
|
|
|
|
| Some pb
|
2017-04-19 21:21:23 +04:00
|
|
|
when b.Block_header.shell.timestamp
|
|
|
|
< pb.Block_header.shell.timestamp ->
|
2017-03-02 17:45:23 +04:00
|
|
|
Some b
|
|
|
|
| Some _ as pb -> pb in
|
|
|
|
let next =
|
|
|
|
List.fold_left
|
|
|
|
(fun acc (hash, block, (data : data)) ->
|
|
|
|
match block with
|
|
|
|
| Error _ ->
|
|
|
|
acc
|
|
|
|
| Ok block ->
|
2017-04-19 21:21:23 +04:00
|
|
|
if Time.(block.Block_header.shell.timestamp > time) then
|
2017-03-02 17:45:23 +04:00
|
|
|
min_block block acc
|
|
|
|
else begin
|
|
|
|
Block_hash.Table.replace session.tbl hash { data with state = `Running begin
|
2017-03-06 22:07:11 +04:00
|
|
|
Lwt_main.yield () >>= fun () ->
|
|
|
|
process data.validator ~get_context ~set_context hash block >>= fun res ->
|
|
|
|
Block_hash.Table.remove session.tbl hash ;
|
|
|
|
Lwt.return res
|
2017-03-02 17:45:23 +04:00
|
|
|
end } ;
|
|
|
|
acc
|
|
|
|
end)
|
|
|
|
None
|
|
|
|
pendings in
|
|
|
|
match next with
|
|
|
|
| None -> 0.
|
2017-04-19 21:21:23 +04:00
|
|
|
| Some b -> Int64.to_float (Time.diff b.Block_header.shell.timestamp time)
|
2017-03-02 17:45:23 +04:00
|
|
|
|
|
|
|
let create net_db =
|
2017-11-09 14:26:25 +04:00
|
|
|
let net_state = Distributed_db.net_state net_db in
|
2017-03-02 17:45:23 +04:00
|
|
|
let tbl = Block_hash.Table.create 50 in
|
|
|
|
let canceler = Lwt_utils.Canceler.create () in
|
|
|
|
let worker_trigger, worker_waiter = Lwt_utils.trigger () in
|
|
|
|
let session =
|
|
|
|
{ tbl ; net_db ; net_state ; worker = Lwt.return () ;
|
|
|
|
canceler ; worker_trigger ; worker_waiter } in
|
|
|
|
let worker =
|
|
|
|
let rec worker_loop () =
|
|
|
|
Lwt_utils.protect ~canceler begin fun () ->
|
|
|
|
worker_waiter () >>= return
|
|
|
|
end >>= function
|
|
|
|
| Error [Lwt_utils.Canceled] -> Lwt.return_unit
|
|
|
|
| Error err ->
|
|
|
|
lwt_log_error
|
|
|
|
"@[Unexpected error in validation:@ %a@]"
|
|
|
|
pp_print_error err >>= fun () ->
|
|
|
|
worker_loop ()
|
|
|
|
| Ok () ->
|
|
|
|
begin
|
|
|
|
match pending_requests session with
|
|
|
|
| [] -> ()
|
|
|
|
| requests ->
|
|
|
|
let set_context _validator hash context =
|
|
|
|
store session hash context >>= fun _ ->
|
|
|
|
Lwt.return_unit in
|
|
|
|
let timeout =
|
|
|
|
request session
|
|
|
|
~get_context:(fetch session)
|
|
|
|
~set_context requests in
|
|
|
|
if timeout > 0. then
|
|
|
|
Lwt.ignore_result
|
|
|
|
(Lwt_unix.sleep timeout >|= worker_trigger);
|
|
|
|
end ;
|
|
|
|
worker_loop ()
|
|
|
|
in
|
|
|
|
Lwt_utils.worker "validation"
|
|
|
|
~run:worker_loop
|
|
|
|
~cancel:(fun () -> Lwt_utils.Canceler.cancel canceler) in
|
|
|
|
{ session with worker }
|
|
|
|
|
|
|
|
let shutdown { canceler ; worker } =
|
|
|
|
Lwt_utils.Canceler.cancel canceler >>= fun () -> worker
|
2017-02-24 20:17:53 +04:00
|
|
|
|
|
|
|
end
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
|
2017-11-13 17:29:28 +04:00
|
|
|
let create_validator ?parent worker ?max_child_ttl state db net =
|
2017-02-24 20:17:53 +04:00
|
|
|
|
2017-09-29 20:43:13 +04:00
|
|
|
let net_id = State.Net.id net in
|
|
|
|
let net_db = Distributed_db.activate db net in
|
|
|
|
let session = Context_db.create net_db in
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
let queue = Lwt_pipe.create () in
|
2017-09-29 20:43:13 +04:00
|
|
|
Prevalidator.create net_db >>= fun prevalidator ->
|
|
|
|
let new_blocks = ref Lwt.return_unit in
|
2017-02-24 20:17:53 +04:00
|
|
|
|
2017-09-29 20:43:13 +04:00
|
|
|
Distributed_db.set_callback net_db {
|
2017-02-24 20:17:53 +04:00
|
|
|
notify_branch = begin fun gid locator ->
|
|
|
|
Lwt.async (fun () -> Lwt_pipe.push queue (`Branch (gid, locator)))
|
|
|
|
end ;
|
|
|
|
notify_head = begin fun gid block ops ->
|
2017-11-11 06:34:12 +04:00
|
|
|
Lwt.async (fun () -> Lwt_pipe.push queue (`Head (gid, Block_header.hash block, ops))) ;
|
2017-02-24 20:17:53 +04:00
|
|
|
end ;
|
|
|
|
disconnection = (fun _gid -> ()) ;
|
2017-09-29 20:43:13 +04:00
|
|
|
} ;
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
let shutdown () =
|
2017-03-31 15:04:05 +04:00
|
|
|
lwt_log_notice "shutdown %a" Net_id.pp net_id >>= fun () ->
|
2017-02-24 20:17:53 +04:00
|
|
|
Distributed_db.deactivate net_db >>= fun () ->
|
|
|
|
Lwt_pipe.close queue ;
|
2016-09-08 21:13:10 +04:00
|
|
|
Lwt.join [
|
2017-03-02 17:45:23 +04:00
|
|
|
Context_db.shutdown session ;
|
2017-02-24 20:17:53 +04:00
|
|
|
!new_blocks ;
|
2016-09-08 21:13:10 +04:00
|
|
|
Prevalidator.shutdown prevalidator ;
|
|
|
|
]
|
|
|
|
in
|
|
|
|
|
2017-04-19 23:46:10 +04:00
|
|
|
let valid_block_input_for_net = Watcher.create_input () in
|
2017-02-28 03:48:22 +04:00
|
|
|
let new_head_input = Watcher.create_input () in
|
|
|
|
|
|
|
|
let bootstrapped =
|
|
|
|
(* TODO improve by taking current peers count and current
|
|
|
|
locators into account... *)
|
|
|
|
let stream, stopper =
|
2017-04-19 23:46:10 +04:00
|
|
|
Watcher.create_stream valid_block_input_for_net in
|
2017-02-28 03:48:22 +04:00
|
|
|
let rec wait () =
|
|
|
|
Lwt.pick [ ( Lwt_stream.get stream ) ;
|
|
|
|
( Lwt_unix.sleep 30. >|= fun () -> None) ] >>= function
|
2017-04-19 23:46:10 +04:00
|
|
|
| Some block when
|
|
|
|
Time.((State.Block.header block).shell.timestamp < add (Time.now ()) (-60L)) ->
|
|
|
|
wait ()
|
2017-03-04 12:54:16 +04:00
|
|
|
| _ ->
|
2017-04-19 23:46:10 +04:00
|
|
|
Chain.head net >>= fun head ->
|
|
|
|
Chain.genesis net >>= fun genesis ->
|
|
|
|
if State.Block.equal head genesis then
|
2017-03-04 12:54:16 +04:00
|
|
|
wait ()
|
|
|
|
else
|
|
|
|
Lwt.return_unit in
|
2017-09-29 20:43:13 +04:00
|
|
|
let net_validator =
|
2017-02-28 03:48:22 +04:00
|
|
|
wait () >>= fun () ->
|
|
|
|
Watcher.shutdown stopper ;
|
|
|
|
Lwt.return_unit in
|
2017-09-29 20:43:13 +04:00
|
|
|
Lwt.no_cancel net_validator
|
2017-02-28 03:48:22 +04:00
|
|
|
in
|
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
let rec v = {
|
|
|
|
net ;
|
|
|
|
worker ;
|
|
|
|
parent ;
|
|
|
|
child = None ;
|
|
|
|
prevalidator ;
|
2017-02-24 20:17:53 +04:00
|
|
|
net_db ;
|
2016-09-08 21:13:10 +04:00
|
|
|
shutdown ;
|
|
|
|
notify_block ;
|
|
|
|
fetch_block ;
|
|
|
|
create_child ;
|
2017-04-10 23:14:17 +04:00
|
|
|
check_child ;
|
|
|
|
deactivate_child ;
|
2016-09-08 21:13:10 +04:00
|
|
|
test_validator ;
|
2017-02-28 03:48:22 +04:00
|
|
|
bootstrapped ;
|
|
|
|
new_head_input ;
|
2017-04-19 23:46:10 +04:00
|
|
|
valid_block_input_for_net ;
|
2016-09-08 21:13:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
and notify_block hash block =
|
|
|
|
lwt_debug "-> Validator.notify_block %a"
|
|
|
|
Block_hash.pp_short hash >>= fun () ->
|
2017-04-19 23:46:10 +04:00
|
|
|
Chain.head net >>= fun head ->
|
|
|
|
let head_header = State.Block.header head in
|
|
|
|
if Fitness.compare head_header.shell.fitness block.shell.fitness <= 0 then
|
2017-03-02 17:45:23 +04:00
|
|
|
Context_db.prefetch v session hash ;
|
2016-09-08 21:13:10 +04:00
|
|
|
Lwt.return_unit
|
|
|
|
|
|
|
|
and fetch_block hash =
|
2017-03-02 17:45:23 +04:00
|
|
|
Context_db.fetch session v hash
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-04-10 23:14:17 +04:00
|
|
|
and create_child block protocol expiration =
|
|
|
|
if State.Net.allow_forked_network net then begin
|
|
|
|
deactivate_child () >>= fun () ->
|
|
|
|
begin
|
|
|
|
State.Net.get state net_id >>= function
|
|
|
|
| Ok net_store -> return net_store
|
|
|
|
| Error _ ->
|
2017-09-29 20:43:13 +04:00
|
|
|
State.fork_testnet block protocol expiration >>=? fun net_store ->
|
2017-04-19 23:46:10 +04:00
|
|
|
Chain.head net_store >>= fun block ->
|
2017-04-10 23:14:17 +04:00
|
|
|
Watcher.notify v.worker.valid_block_input block ;
|
|
|
|
return net_store
|
|
|
|
end >>=? fun net_store ->
|
|
|
|
worker.activate ~parent:v net_store >>= fun child ->
|
|
|
|
v.child <- Some child ;
|
|
|
|
return ()
|
|
|
|
end else begin
|
|
|
|
(* Ignoring request... *)
|
|
|
|
return ()
|
|
|
|
end
|
|
|
|
|
|
|
|
and deactivate_child () =
|
|
|
|
match v.child with
|
|
|
|
| None -> Lwt.return_unit
|
|
|
|
| Some child ->
|
|
|
|
v.child <- None ;
|
|
|
|
deactivate child
|
|
|
|
|
|
|
|
and check_child genesis protocol expiration current_time =
|
|
|
|
let activated =
|
2016-09-08 21:13:10 +04:00
|
|
|
match v.child with
|
2017-04-10 23:14:17 +04:00
|
|
|
| None -> false
|
2016-09-08 21:13:10 +04:00
|
|
|
| Some child ->
|
2017-04-10 23:14:17 +04:00
|
|
|
Block_hash.equal (State.Net.genesis child.net).block genesis in
|
|
|
|
begin
|
2017-09-29 20:43:13 +04:00
|
|
|
match max_child_ttl with
|
2017-11-08 15:42:09 +04:00
|
|
|
| None -> return expiration
|
2017-04-10 23:14:17 +04:00
|
|
|
| Some ttl ->
|
2017-11-08 15:42:09 +04:00
|
|
|
Distributed_db.Block_header.fetch net_db genesis () >>=? fun genesis ->
|
|
|
|
return
|
2017-04-10 23:14:17 +04:00
|
|
|
(Time.min expiration
|
|
|
|
(Time.add genesis.shell.timestamp (Int64.of_int ttl)))
|
2017-11-08 15:42:09 +04:00
|
|
|
end >>=? fun local_expiration ->
|
2017-04-10 23:14:17 +04:00
|
|
|
let expired = Time.(local_expiration <= current_time) in
|
|
|
|
if expired && activated then
|
|
|
|
deactivate_child () >>= return
|
|
|
|
else if not activated && not expired then
|
|
|
|
fetch_block genesis >>=? fun genesis ->
|
|
|
|
create_child genesis protocol expiration
|
|
|
|
else
|
|
|
|
return ()
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
and test_validator () =
|
|
|
|
match v.child with
|
|
|
|
| None -> None
|
2017-02-24 20:17:53 +04:00
|
|
|
| Some child -> Some (child, child.net_db)
|
2016-09-08 21:13:10 +04:00
|
|
|
in
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
new_blocks := begin
|
|
|
|
let rec loop () =
|
|
|
|
Lwt_pipe.pop queue >>= function
|
|
|
|
| `Branch (_gid, locator) ->
|
2017-11-11 06:34:12 +04:00
|
|
|
let head, hist = (locator : Block_locator.t :> _ * _) in
|
2017-09-29 20:43:13 +04:00
|
|
|
List.iter
|
|
|
|
(Context_db.prefetch v session)
|
2017-11-11 06:34:12 +04:00
|
|
|
(Block_header.hash head :: hist) ;
|
2017-02-24 20:17:53 +04:00
|
|
|
loop ()
|
|
|
|
| `Head (gid, head, ops) ->
|
2017-03-02 17:45:23 +04:00
|
|
|
Context_db.prefetch v session head ;
|
2017-03-04 12:53:44 +04:00
|
|
|
Prevalidator.notify_operations prevalidator gid ops ;
|
2017-02-24 20:17:53 +04:00
|
|
|
loop ()
|
|
|
|
in
|
|
|
|
Lwt.catch loop
|
|
|
|
(function Lwt_pipe.Closed -> Lwt.return_unit
|
|
|
|
| exn -> Lwt.fail exn)
|
|
|
|
end ;
|
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
Lwt.return v
|
|
|
|
|
2017-09-29 20:43:13 +04:00
|
|
|
let create state db =
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-09-29 20:43:13 +04:00
|
|
|
let validators : net_validator Lwt.t Net_id.Table.t =
|
2017-03-31 15:04:05 +04:00
|
|
|
Net_id.Table.create 7 in
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
let valid_block_input = Watcher.create_input () in
|
|
|
|
|
2017-03-31 15:04:05 +04:00
|
|
|
let get_exn net = Net_id.Table.find validators net in
|
2016-09-08 21:13:10 +04:00
|
|
|
let get net =
|
|
|
|
try get_exn net >>= fun v -> return v
|
|
|
|
with Not_found -> fail (State.Unknown_network net) in
|
2017-03-31 15:04:05 +04:00
|
|
|
let remove net = Net_id.Table.remove validators net in
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
let deactivate { net } =
|
|
|
|
let id = State.Net.id net in
|
|
|
|
get id >>= function
|
|
|
|
| Error _ -> Lwt.return_unit
|
|
|
|
| Ok v ->
|
2017-03-31 15:04:05 +04:00
|
|
|
lwt_log_notice "deactivate network %a" Net_id.pp id >>= fun () ->
|
2016-09-08 21:13:10 +04:00
|
|
|
remove id ;
|
|
|
|
v.shutdown ()
|
|
|
|
in
|
|
|
|
|
2017-04-19 21:21:23 +04:00
|
|
|
let notify_block hash (block : Block_header.t) =
|
2016-09-08 21:13:10 +04:00
|
|
|
match get_exn block.shell.net_id with
|
|
|
|
| exception Not_found -> Lwt.return_unit
|
|
|
|
| net ->
|
|
|
|
net >>= fun net ->
|
|
|
|
net.notify_block hash block in
|
|
|
|
|
|
|
|
let cancelation, cancel, _on_cancel = Lwt_utils.canceler () in
|
|
|
|
|
|
|
|
let maintenance_worker =
|
|
|
|
let next_net_maintenance = ref (Time.now ()) in
|
|
|
|
let net_maintenance () =
|
|
|
|
lwt_log_info "net maintenance" >>= fun () ->
|
|
|
|
let time = Time.now () in
|
2017-03-31 15:04:05 +04:00
|
|
|
Net_id.Table.fold
|
2016-09-08 21:13:10 +04:00
|
|
|
(fun _ v acc ->
|
|
|
|
v >>= fun v ->
|
|
|
|
acc >>= fun () ->
|
|
|
|
match State.Net.expiration v.net with
|
|
|
|
| Some eol when Time.(eol <= time) -> deactivate v
|
|
|
|
| Some _ | None -> Lwt.return_unit)
|
|
|
|
validators Lwt.return_unit >>= fun () ->
|
2017-02-24 20:17:53 +04:00
|
|
|
State.Net.all state >>= fun all_net ->
|
2016-09-08 21:13:10 +04:00
|
|
|
Lwt_list.iter_p
|
|
|
|
(fun net ->
|
|
|
|
match State.Net.expiration net with
|
|
|
|
| Some eol when Time.(eol <= time) ->
|
|
|
|
lwt_log_notice "destroy network %a"
|
2017-03-31 15:04:05 +04:00
|
|
|
Net_id.pp (State.Net.id net) >>= fun () ->
|
2017-02-24 20:17:53 +04:00
|
|
|
State.Net.destroy state net
|
2016-09-08 21:13:10 +04:00
|
|
|
| Some _ | None -> Lwt.return_unit)
|
2017-02-24 20:17:53 +04:00
|
|
|
all_net >>= fun () ->
|
2016-09-08 21:13:10 +04:00
|
|
|
next_net_maintenance := Time.add (Time.now ()) (Int64.of_int 55) ;
|
|
|
|
Lwt.return_unit in
|
|
|
|
let next_head_maintenance = ref (Time.now ()) in
|
|
|
|
let head_maintenance () =
|
|
|
|
lwt_log_info "head maintenance" >>= fun () ->
|
|
|
|
(* TODO *)
|
|
|
|
next_head_maintenance := Time.add (Time.now ()) (Int64.of_int 55) ;
|
|
|
|
Lwt.return_unit in
|
|
|
|
let rec worker_loop () =
|
|
|
|
let timeout =
|
|
|
|
let next = min !next_head_maintenance !next_net_maintenance in
|
|
|
|
let delay = Time.(diff next (now ())) in
|
|
|
|
if delay <= 0L then
|
|
|
|
Lwt.return_unit
|
|
|
|
else
|
|
|
|
Lwt_unix.sleep (Int64.to_float delay) in
|
|
|
|
Lwt.pick [(timeout >|= fun () -> `Process);
|
|
|
|
(cancelation () >|= fun () -> `Cancel)] >>= function
|
|
|
|
| `Cancel -> Lwt.return_unit
|
|
|
|
| `Process ->
|
|
|
|
begin
|
|
|
|
if !next_net_maintenance < Time.now () then
|
|
|
|
net_maintenance ()
|
|
|
|
else
|
|
|
|
Lwt.return ()
|
|
|
|
end >>= fun () ->
|
|
|
|
begin
|
|
|
|
if !next_head_maintenance < Time.now () then
|
|
|
|
head_maintenance ()
|
|
|
|
else
|
|
|
|
Lwt.return ()
|
|
|
|
end >>= fun () ->
|
|
|
|
worker_loop ()
|
|
|
|
in
|
|
|
|
Lwt_utils.worker "validator_maintenance" ~run:worker_loop ~cancel in
|
|
|
|
|
|
|
|
let shutdown () =
|
|
|
|
cancel () >>= fun () ->
|
|
|
|
let validators =
|
2017-03-31 15:04:05 +04:00
|
|
|
Net_id.Table.fold
|
2017-09-29 20:43:13 +04:00
|
|
|
(fun _ (v: net_validator Lwt.t) acc -> (v >>= fun v -> v.shutdown ()) :: acc)
|
2016-09-08 21:13:10 +04:00
|
|
|
validators [] in
|
|
|
|
Lwt.join (maintenance_worker :: validators) in
|
|
|
|
|
2017-03-30 15:16:21 +04:00
|
|
|
let inject_block ?(force = false) bytes operations =
|
|
|
|
Distributed_db.inject_block db bytes operations >>=? fun (hash, block) ->
|
2017-02-24 20:17:53 +04:00
|
|
|
get block.shell.net_id >>=? fun net ->
|
|
|
|
let validation =
|
2017-04-19 23:46:10 +04:00
|
|
|
protect
|
|
|
|
~on_error: begin fun err ->
|
2017-06-09 19:54:08 +04:00
|
|
|
Distributed_db.clear_block
|
|
|
|
net.net_db hash (List.length operations) ;
|
2017-04-19 23:46:10 +04:00
|
|
|
Lwt.return (Error err)
|
|
|
|
end
|
|
|
|
begin fun () ->
|
|
|
|
Chain.head net.net >>= fun head ->
|
|
|
|
let head_header = State.Block.header head in
|
|
|
|
if force ||
|
|
|
|
Fitness.compare head_header.shell.fitness block.shell.fitness <= 0
|
|
|
|
then
|
|
|
|
fetch_block net hash
|
|
|
|
else
|
|
|
|
failwith "Fitness is below the current one"
|
|
|
|
end in
|
2017-02-24 20:17:53 +04:00
|
|
|
return (hash, validation) in
|
|
|
|
|
2017-09-29 20:43:13 +04:00
|
|
|
let rec activate ?parent ?max_child_ttl net =
|
2017-03-31 15:04:05 +04:00
|
|
|
let net_id = State.Net.id net in
|
2016-09-08 21:13:10 +04:00
|
|
|
lwt_log_notice "activate network %a"
|
2017-03-31 15:04:05 +04:00
|
|
|
Net_id.pp net_id >>= fun () ->
|
2017-02-24 20:17:53 +04:00
|
|
|
get net_id >>= function
|
2016-09-08 21:13:10 +04:00
|
|
|
| Error _ ->
|
2017-09-29 20:43:13 +04:00
|
|
|
let v = create_validator ?max_child_ttl ?parent worker state db net in
|
2017-03-31 15:04:05 +04:00
|
|
|
Net_id.Table.add validators net_id v ;
|
2016-09-08 21:13:10 +04:00
|
|
|
v
|
|
|
|
| Ok v -> Lwt.return v
|
|
|
|
|
|
|
|
and worker = {
|
|
|
|
get ; get_exn ;
|
|
|
|
activate ; deactivate ;
|
|
|
|
notify_block ;
|
2017-02-24 20:17:53 +04:00
|
|
|
inject_block ;
|
2016-09-08 21:13:10 +04:00
|
|
|
shutdown ;
|
2017-02-24 20:17:53 +04:00
|
|
|
valid_block_input ;
|
2017-03-03 19:43:28 +04:00
|
|
|
db ;
|
2016-09-08 21:13:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
worker
|
|
|
|
|
2017-04-19 23:46:10 +04:00
|
|
|
let new_head_watcher { new_head_input } =
|
2017-02-28 03:48:22 +04:00
|
|
|
Watcher.create_stream new_head_input
|
|
|
|
|
2017-04-19 23:46:10 +04:00
|
|
|
let watcher { valid_block_input_for_net } =
|
|
|
|
Watcher.create_stream valid_block_input_for_net
|
2017-02-28 03:48:22 +04:00
|
|
|
|
2017-09-29 20:43:13 +04:00
|
|
|
let global_watcher ({ valid_block_input } : t) =
|
2017-02-28 03:48:22 +04:00
|
|
|
Watcher.create_stream valid_block_input
|