2016-09-08 21:13:10 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
2018-02-06 00:17:03 +04:00
|
|
|
(* Copyright (c) 2014 - 2018. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
2018-01-29 04:06:47 +04:00
|
|
|
open Proto_alpha
|
|
|
|
open Tezos_context
|
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
open Logging.Client.Endorsement
|
|
|
|
|
|
|
|
module State : sig
|
|
|
|
|
|
|
|
val get_endorsement:
|
2017-11-07 20:38:11 +04:00
|
|
|
#Client_commands.wallet ->
|
2016-09-08 21:13:10 +04:00
|
|
|
Raw_level.t ->
|
|
|
|
int ->
|
|
|
|
(Block_hash.t * Operation_hash.t) option tzresult Lwt.t
|
|
|
|
|
|
|
|
val record_endorsement:
|
2017-11-07 20:38:11 +04:00
|
|
|
#Client_commands.wallet ->
|
2016-09-08 21:13:10 +04:00
|
|
|
Raw_level.t ->
|
|
|
|
Block_hash.t ->
|
|
|
|
int -> Operation_hash.t -> unit tzresult Lwt.t
|
|
|
|
|
|
|
|
end = struct
|
|
|
|
|
|
|
|
module LevelMap = Map.Make(Raw_level)
|
|
|
|
|
|
|
|
type t = (int * Block_hash.t * Operation_hash.t) list LevelMap.t
|
|
|
|
let encoding : t Data_encoding.t =
|
|
|
|
let open Data_encoding in
|
|
|
|
conv
|
|
|
|
(fun x -> LevelMap.bindings x)
|
2017-02-15 20:20:10 +04:00
|
|
|
(fun l ->
|
|
|
|
List.fold_left
|
|
|
|
(fun x (y, z) -> LevelMap.add y z x)
|
|
|
|
LevelMap.empty l)
|
2016-09-08 21:13:10 +04:00
|
|
|
(list (obj2
|
|
|
|
(req "level" Raw_level.encoding)
|
|
|
|
(req "endorsement"
|
|
|
|
(list (obj3
|
|
|
|
(req "slot" int31)
|
|
|
|
(req "block" Block_hash.encoding)
|
|
|
|
(req "operation" Operation_hash.encoding))))))
|
|
|
|
|
2017-11-07 20:38:11 +04:00
|
|
|
let name =
|
|
|
|
"endorsements"
|
|
|
|
|
|
|
|
let load (wallet : #Client_commands.wallet) =
|
|
|
|
wallet#load name encoding ~default:LevelMap.empty
|
|
|
|
|
|
|
|
let save (wallet : #Client_commands.wallet) map =
|
|
|
|
wallet#write name encoding map
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
let lock = Lwt_mutex.create ()
|
|
|
|
|
2017-11-07 20:38:11 +04:00
|
|
|
let get_endorsement (wallet : #Client_commands.wallet) level slot =
|
2016-09-08 21:13:10 +04:00
|
|
|
Lwt_mutex.with_lock lock
|
|
|
|
(fun () ->
|
2017-11-07 20:38:11 +04:00
|
|
|
load wallet >>=? fun map ->
|
2016-09-08 21:13:10 +04:00
|
|
|
try
|
|
|
|
let _, block, op =
|
|
|
|
LevelMap.find level map
|
|
|
|
|> List.find (fun (slot',_,_) -> slot = slot') in
|
|
|
|
return (Some (block, op))
|
|
|
|
with Not_found -> return None)
|
|
|
|
|
2017-11-07 20:38:11 +04:00
|
|
|
let record_endorsement (wallet : #Client_commands.wallet) level hash slot oph =
|
2016-09-08 21:13:10 +04:00
|
|
|
Lwt_mutex.with_lock lock
|
|
|
|
(fun () ->
|
2017-11-07 20:38:11 +04:00
|
|
|
load wallet >>=? fun map ->
|
2016-09-08 21:13:10 +04:00
|
|
|
let previous =
|
|
|
|
try LevelMap.find level map
|
|
|
|
with Not_found -> [] in
|
2017-11-07 20:38:11 +04:00
|
|
|
wallet#write name
|
|
|
|
(LevelMap.add level ((slot, hash, oph) :: previous) map)
|
|
|
|
encoding)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let get_signing_slots cctxt ?max_priority block delegate level =
|
|
|
|
Client_proto_rpcs.Helpers.Rights.endorsement_rights_for_delegate cctxt
|
2016-09-08 21:13:10 +04:00
|
|
|
?max_priority ~first_level:level ~last_level:level
|
|
|
|
block delegate () >>=? fun possibilities ->
|
|
|
|
let slots =
|
2017-03-14 19:32:01 +04:00
|
|
|
List.map (fun (_,slot) -> slot)
|
|
|
|
@@ List.filter (fun (l, _) -> l = level) possibilities in
|
2016-09-08 21:13:10 +04:00
|
|
|
return slots
|
|
|
|
|
2017-11-07 20:38:11 +04:00
|
|
|
let inject_endorsement (cctxt : Client_commands.full_context)
|
2017-11-23 19:39:33 +04:00
|
|
|
block level ?async
|
2016-09-08 21:13:10 +04:00
|
|
|
src_sk source slot =
|
2017-11-20 06:37:06 +04:00
|
|
|
let block = Client_rpcs.last_baked_block block in
|
2018-02-08 13:51:02 +04:00
|
|
|
Block_services.info cctxt block >>=? fun bi ->
|
2017-11-07 20:38:11 +04:00
|
|
|
Client_proto_rpcs.Helpers.Forge.Delegate.endorsement cctxt
|
2016-09-08 21:13:10 +04:00
|
|
|
block
|
2017-04-20 10:49:14 +04:00
|
|
|
~branch:bi.hash
|
2016-09-08 21:13:10 +04:00
|
|
|
~source
|
2017-04-20 10:26:43 +04:00
|
|
|
~block:bi.hash
|
2016-09-08 21:13:10 +04:00
|
|
|
~slot:slot
|
|
|
|
() >>=? fun bytes ->
|
2018-02-01 20:31:08 +04:00
|
|
|
Client_keys.append src_sk bytes >>=? fun signed_bytes ->
|
2018-02-08 13:51:02 +04:00
|
|
|
Shell_services.inject_operation
|
2017-11-23 19:39:33 +04:00
|
|
|
cctxt ?async ~net_id:bi.net_id signed_bytes >>=? fun oph ->
|
2017-04-20 10:26:43 +04:00
|
|
|
State.record_endorsement cctxt level bi.hash slot oph >>=? fun () ->
|
2016-09-08 21:13:10 +04:00
|
|
|
return oph
|
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let previously_endorsed_slot cctxt level slot =
|
|
|
|
State.get_endorsement cctxt level slot >>=? function
|
2016-09-08 21:13:10 +04:00
|
|
|
| None -> return false
|
|
|
|
| Some _ -> return true
|
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let check_endorsement cctxt level slot =
|
|
|
|
State.get_endorsement cctxt level slot >>=? function
|
2016-09-08 21:13:10 +04:00
|
|
|
| None -> return ()
|
|
|
|
| Some (block, _) ->
|
2016-12-03 16:05:02 +04:00
|
|
|
Error_monad.failwith
|
2016-09-08 21:13:10 +04:00
|
|
|
"Already signed block %a at level %a, slot %d"
|
|
|
|
Block_hash.pp_short block Raw_level.pp level slot
|
|
|
|
|
|
|
|
|
2017-11-07 20:38:11 +04:00
|
|
|
let forge_endorsement (cctxt : Client_commands.full_context)
|
2017-11-23 19:39:33 +04:00
|
|
|
block
|
2016-09-08 21:13:10 +04:00
|
|
|
~src_sk ?slot ?max_priority src_pk =
|
2017-11-20 06:37:06 +04:00
|
|
|
let block = Client_rpcs.last_baked_block block in
|
2017-02-28 05:56:40 +04:00
|
|
|
let src_pkh = Ed25519.Public_key.hash src_pk in
|
2017-11-07 20:38:11 +04:00
|
|
|
Client_proto_rpcs.Context.next_level cctxt block >>=? fun { level } ->
|
2016-09-08 21:13:10 +04:00
|
|
|
begin
|
|
|
|
match slot with
|
|
|
|
| Some slot -> return slot
|
|
|
|
| None ->
|
2017-02-15 20:20:10 +04:00
|
|
|
get_signing_slots
|
2017-11-07 20:38:11 +04:00
|
|
|
cctxt ?max_priority block src_pkh level >>=? function
|
2016-09-08 21:13:10 +04:00
|
|
|
| slot::_ -> return slot
|
2017-11-07 20:38:11 +04:00
|
|
|
| [] -> cctxt#error "No slot found at level %a" Raw_level.pp level
|
2016-09-08 21:13:10 +04:00
|
|
|
end >>=? fun slot ->
|
2017-11-23 19:39:33 +04:00
|
|
|
check_endorsement cctxt level slot >>=? fun () ->
|
2016-12-03 16:05:02 +04:00
|
|
|
inject_endorsement cctxt
|
2017-11-23 19:39:33 +04:00
|
|
|
block level
|
2016-09-08 21:13:10 +04:00
|
|
|
src_sk src_pk slot
|
|
|
|
|
|
|
|
|
|
|
|
(** Worker *)
|
|
|
|
|
|
|
|
type state = {
|
|
|
|
delegates: public_key_hash list ;
|
2017-11-01 15:07:33 +04:00
|
|
|
mutable best: Client_baking_blocks.block_info ;
|
2016-09-08 21:13:10 +04:00
|
|
|
mutable to_endorse: endorsement list ;
|
|
|
|
delay: int64;
|
|
|
|
}
|
|
|
|
and endorsement = {
|
|
|
|
time: Time.t ;
|
|
|
|
delegate: public_key_hash ;
|
2017-11-01 15:07:33 +04:00
|
|
|
block: Client_baking_blocks.block_info ;
|
2016-09-08 21:13:10 +04:00
|
|
|
slot: int;
|
|
|
|
}
|
|
|
|
|
2017-02-15 23:39:38 +04:00
|
|
|
let create_state delegates best delay =
|
2016-09-08 21:13:10 +04:00
|
|
|
{ delegates ;
|
2017-02-15 23:39:38 +04:00
|
|
|
best ;
|
2016-09-08 21:13:10 +04:00
|
|
|
to_endorse = [] ;
|
|
|
|
delay ;
|
|
|
|
}
|
|
|
|
|
|
|
|
let rec insert ({time} as e) = function
|
|
|
|
| [] -> [e]
|
|
|
|
| ({time = time'} :: _) as l when Time.compare time time' < 0 ->
|
|
|
|
e :: l
|
|
|
|
| e' :: l -> e' :: insert e l
|
|
|
|
|
2017-02-28 11:18:06 +04:00
|
|
|
let get_delegates cctxt state =
|
|
|
|
match state.delegates with
|
2017-04-05 03:02:10 +04:00
|
|
|
| [] ->
|
|
|
|
Client_keys.get_keys cctxt >>=? fun keys ->
|
|
|
|
return (List.map (fun (_,pkh,_,_) -> pkh) keys)
|
|
|
|
| _ :: _ as delegates ->
|
|
|
|
return delegates
|
2017-02-28 11:18:06 +04:00
|
|
|
|
2017-02-15 23:39:38 +04:00
|
|
|
let drop_old_endorsement ~before state =
|
|
|
|
state.to_endorse <-
|
|
|
|
List.filter
|
|
|
|
(fun { block } -> Fitness.compare before block.fitness <= 0)
|
|
|
|
state.to_endorse
|
|
|
|
|
2017-11-07 20:38:11 +04:00
|
|
|
let schedule_endorsements (cctxt : Client_commands.full_context) state bis =
|
2017-11-01 15:07:33 +04:00
|
|
|
let may_endorse (block: Client_baking_blocks.block_info) delegate time =
|
2017-04-05 03:02:10 +04:00
|
|
|
Client_keys.Public_key_hash.name cctxt delegate >>=? fun name ->
|
2016-09-08 21:13:10 +04:00
|
|
|
lwt_log_info "May endorse block %a for %s"
|
|
|
|
Block_hash.pp_short block.hash name >>= fun () ->
|
|
|
|
let b = `Hash block.hash in
|
|
|
|
let level = Raw_level.succ block.level.level in
|
2017-11-07 20:38:11 +04:00
|
|
|
get_signing_slots cctxt b delegate level >>=? fun slots ->
|
2016-09-08 21:13:10 +04:00
|
|
|
lwt_debug "Found slots for %a/%s (%d)"
|
|
|
|
Block_hash.pp_short block.hash name (List.length slots) >>= fun () ->
|
|
|
|
iter_p
|
|
|
|
(fun slot ->
|
2017-02-15 23:39:38 +04:00
|
|
|
if Fitness.compare state.best.fitness block.fitness < 0 then begin
|
|
|
|
state.best <- block ;
|
|
|
|
drop_old_endorsement ~before:block.fitness state ;
|
|
|
|
end ;
|
2016-12-03 16:05:02 +04:00
|
|
|
previously_endorsed_slot cctxt level slot >>=? function
|
2016-09-08 21:13:10 +04:00
|
|
|
| true ->
|
|
|
|
lwt_debug "slot %d: previously endorsed." slot >>= fun () ->
|
|
|
|
return ()
|
|
|
|
| false ->
|
|
|
|
try
|
|
|
|
let same_slot e =
|
|
|
|
e.block.level = block.level && e.slot = slot in
|
|
|
|
let old = List.find same_slot state.to_endorse in
|
2017-02-15 20:20:10 +04:00
|
|
|
if Fitness.compare old.block.fitness block.fitness < 0
|
|
|
|
then begin
|
2016-09-08 21:13:10 +04:00
|
|
|
lwt_log_info
|
|
|
|
"Schedule endorsement for block %a \
|
2017-11-13 19:34:00 +04:00
|
|
|
\ (level %a, slot %d, time %a) (replace block %a)"
|
2016-09-08 21:13:10 +04:00
|
|
|
Block_hash.pp_short block.hash
|
|
|
|
Raw_level.pp level
|
|
|
|
slot
|
|
|
|
Time.pp_hum time
|
|
|
|
Block_hash.pp_short old.block.hash
|
|
|
|
>>= fun () ->
|
|
|
|
state.to_endorse <-
|
|
|
|
insert
|
|
|
|
{ time ; delegate ; block ; slot }
|
2017-02-15 20:20:10 +04:00
|
|
|
(List.filter
|
|
|
|
(fun e -> not (same_slot e))
|
|
|
|
state.to_endorse) ;
|
2016-09-08 21:13:10 +04:00
|
|
|
return ()
|
|
|
|
end else begin
|
2017-02-15 20:20:10 +04:00
|
|
|
lwt_debug
|
|
|
|
"slot %d: better pending endorsement"
|
|
|
|
slot >>= fun () ->
|
2016-09-08 21:13:10 +04:00
|
|
|
return ()
|
|
|
|
end
|
|
|
|
with Not_found ->
|
|
|
|
lwt_log_info
|
|
|
|
"Schedule endorsement for block %a \
|
|
|
|
\ (level %a, slot %d, time %a)"
|
|
|
|
Block_hash.pp_short block.hash
|
|
|
|
Raw_level.pp level
|
|
|
|
slot
|
|
|
|
Time.pp_hum time >>= fun () ->
|
|
|
|
state.to_endorse <-
|
|
|
|
insert { time ; delegate ; block ; slot } state.to_endorse ;
|
|
|
|
return ())
|
|
|
|
slots in
|
|
|
|
let time = Time.(add (now ()) state.delay) in
|
2017-04-05 03:02:10 +04:00
|
|
|
get_delegates cctxt state >>=? fun delegates ->
|
2016-09-08 21:13:10 +04:00
|
|
|
iter_p
|
|
|
|
(fun delegate ->
|
|
|
|
iter_p
|
|
|
|
(fun bi -> may_endorse bi delegate time)
|
|
|
|
bis)
|
2017-04-05 03:02:10 +04:00
|
|
|
delegates
|
|
|
|
|
2017-11-07 20:38:11 +04:00
|
|
|
let schedule_endorsements (cctxt : Client_commands.full_context) state bis =
|
2017-04-05 03:02:10 +04:00
|
|
|
schedule_endorsements cctxt state bis >>= function
|
2016-09-08 21:13:10 +04:00
|
|
|
| Error exns ->
|
|
|
|
lwt_log_error
|
|
|
|
"@[<v 2>Error(s) while scheduling endorsements@,%a@]"
|
|
|
|
pp_print_error exns
|
|
|
|
| Ok () -> Lwt.return_unit
|
|
|
|
|
|
|
|
let pop_endorsements state =
|
|
|
|
let now = Time.now () in
|
|
|
|
let rec pop acc = function
|
|
|
|
| [] -> List.rev acc, []
|
|
|
|
| {time} :: _ as slots when Time.compare now time <= 0 ->
|
|
|
|
List.rev acc, slots
|
|
|
|
| slot :: slots -> pop (slot :: acc) slots in
|
|
|
|
let to_endorse, future_endorsement = pop [] state.to_endorse in
|
|
|
|
state.to_endorse <- future_endorsement ;
|
|
|
|
to_endorse
|
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let endorse cctxt state =
|
2016-09-08 21:13:10 +04:00
|
|
|
let to_endorse = pop_endorsements state in
|
|
|
|
iter_p
|
2017-02-15 23:39:38 +04:00
|
|
|
(fun { delegate ; block ; slot } ->
|
2016-09-08 21:13:10 +04:00
|
|
|
let hash = block.hash in
|
|
|
|
let b = `Hash hash in
|
|
|
|
let level = Raw_level.succ block.level.level in
|
2016-12-03 16:05:02 +04:00
|
|
|
previously_endorsed_slot cctxt level slot >>=? function
|
2016-09-08 21:13:10 +04:00
|
|
|
| true -> return ()
|
|
|
|
| false ->
|
2016-12-03 16:05:02 +04:00
|
|
|
Client_keys.get_key cctxt delegate >>=? fun (name, pk, sk) ->
|
2016-09-08 21:13:10 +04:00
|
|
|
lwt_debug "Endorsing %a for %s (slot %d)!"
|
|
|
|
Block_hash.pp_short hash name slot >>= fun () ->
|
2016-12-03 16:05:02 +04:00
|
|
|
inject_endorsement cctxt
|
2017-11-23 19:39:33 +04:00
|
|
|
b level ~async:true
|
2016-09-08 21:13:10 +04:00
|
|
|
sk pk slot >>=? fun oph ->
|
2017-11-07 20:38:11 +04:00
|
|
|
cctxt#message
|
2016-09-08 21:13:10 +04:00
|
|
|
"Injected endorsement for block '%a' \
|
|
|
|
\ (level %a, slot %d, contract %s) '%a'"
|
|
|
|
Block_hash.pp_short hash
|
|
|
|
Raw_level.pp level
|
|
|
|
slot name
|
2016-11-22 20:59:09 +04:00
|
|
|
Operation_hash.pp_short oph >>= fun () ->
|
2016-09-08 21:13:10 +04:00
|
|
|
return ())
|
|
|
|
to_endorse
|
|
|
|
|
|
|
|
let compute_timeout state =
|
|
|
|
match state.to_endorse with
|
|
|
|
| [] -> Lwt_utils.never_ending
|
|
|
|
| {time} :: _ ->
|
|
|
|
let delay = (Time.diff time (Time.now ())) in
|
|
|
|
if delay <= 0L then
|
|
|
|
Lwt.return_unit
|
|
|
|
else
|
|
|
|
Lwt_unix.sleep (Int64.to_float delay)
|
|
|
|
|
2017-11-07 20:38:11 +04:00
|
|
|
let create (cctxt : Client_commands.full_context) ~delay contracts block_stream =
|
2016-09-08 21:13:10 +04:00
|
|
|
lwt_log_info "Starting endorsement daemon" >>= fun () ->
|
|
|
|
Lwt_stream.get block_stream >>= function
|
2017-04-05 01:35:41 +04:00
|
|
|
| None | Some (Ok []) | Some (Error _) ->
|
2017-11-07 20:38:11 +04:00
|
|
|
cctxt#error "Can't fetch the current block head."
|
2017-04-05 01:35:41 +04:00
|
|
|
| Some (Ok (bi :: _ as initial_heads)) ->
|
2016-09-08 21:13:10 +04:00
|
|
|
let last_get_block = ref None in
|
|
|
|
let get_block () =
|
|
|
|
match !last_get_block with
|
|
|
|
| None ->
|
|
|
|
let t = Lwt_stream.get block_stream in
|
|
|
|
last_get_block := Some t ;
|
|
|
|
t
|
|
|
|
| Some t -> t in
|
2017-02-15 23:39:38 +04:00
|
|
|
let state = create_state contracts bi (Int64.of_int delay) in
|
2016-09-08 21:13:10 +04:00
|
|
|
let rec worker_loop () =
|
|
|
|
let timeout = compute_timeout state in
|
|
|
|
Lwt.choose [ (timeout >|= fun () -> `Timeout) ;
|
|
|
|
(get_block () >|= fun b -> `Hash b) ] >>= function
|
2017-04-05 01:35:41 +04:00
|
|
|
| `Hash (None | Some (Error _)) ->
|
2016-09-08 21:13:10 +04:00
|
|
|
Lwt.return_unit
|
2017-04-05 01:35:41 +04:00
|
|
|
| `Hash (Some (Ok bis)) ->
|
2016-09-08 21:13:10 +04:00
|
|
|
Lwt.cancel timeout ;
|
|
|
|
last_get_block := None ;
|
2016-12-03 16:05:02 +04:00
|
|
|
schedule_endorsements cctxt state bis >>= fun () ->
|
2016-09-08 21:13:10 +04:00
|
|
|
worker_loop ()
|
|
|
|
| `Timeout ->
|
|
|
|
begin
|
2016-12-03 16:05:02 +04:00
|
|
|
endorse cctxt state >>= function
|
2016-09-08 21:13:10 +04:00
|
|
|
| Ok () -> Lwt.return_unit
|
|
|
|
| Error errs ->
|
|
|
|
lwt_log_error "Error while endorsing:\n%a"
|
|
|
|
pp_print_error
|
|
|
|
errs >>= fun () ->
|
|
|
|
Lwt.return_unit
|
|
|
|
end >>= fun () ->
|
|
|
|
worker_loop () in
|
2016-12-03 16:05:02 +04:00
|
|
|
schedule_endorsements cctxt state initial_heads >>= fun () ->
|
2016-09-08 21:13:10 +04:00
|
|
|
worker_loop ()
|