diff --git a/src/proto_alpha/lib_delegate/client_baking_endorsement.ml b/src/proto_alpha/lib_delegate/client_baking_endorsement.ml index 535fa50fa..7e9888198 100644 --- a/src/proto_alpha/lib_delegate/client_baking_endorsement.ml +++ b/src/proto_alpha/lib_delegate/client_baking_endorsement.ml @@ -12,57 +12,7 @@ open Alpha_context include Logging.Make(struct let name = "client.endorsement" end) -module State : sig - - val get_endorsement: - #Client_context.wallet -> - Signature.Public_key_hash.t -> - Raw_level.t option tzresult Lwt.t - - val record_endorsement: - #Client_context.wallet -> - Signature.Public_key_hash.t -> - Raw_level.t -> - unit tzresult Lwt.t - -end = struct - type t = (string * Raw_level.t) list - - let encoding : t Data_encoding.t = - Data_encoding.assoc Raw_level.encoding - - let name = "endorsements" - - let load (wallet : #Client_context.wallet) = - wallet#load name encoding ~default:[] - - let save (wallet : #Client_context.wallet) list = - wallet#write name list encoding - - let get_endorsement (wallet : #Client_context.wallet) (delegate_key:Signature.public_key_hash) = - wallet#with_lock - (fun () -> - load wallet >>=? fun l -> - return (List.assoc_opt (Signature.Public_key_hash.to_short_b58check delegate_key) l) - ) - - let record_endorsement (wallet : #Client_context.wallet) (delegate:Signature.public_key_hash) (new_lvl:Raw_level.t) = - begin - wallet#with_lock (fun () -> - begin - load wallet >>=? fun l -> - let delegate_key = Signature.Public_key_hash.to_short_b58check delegate - in - let remove_old l = - List.filter - (fun (_, lvl) -> Raw_level.diff new_lvl lvl < 50l (* FIXME: magic constant*)) - l - in - save wallet ((delegate_key, new_lvl):: - List.remove_assoc delegate_key (remove_old l)) - end) - end -end +module State = Daemon_state.Make(struct let name = "endorsements" end) let get_signing_slots cctxt ?(chain = `Main) block delegate level = Alpha_services.Delegate.Endorsing_rights.get cctxt @@ -86,12 +36,12 @@ let inject_endorsement Client_keys.append cctxt src_sk ~watermark:Endorsement bytes >>=? fun signed_bytes -> Shell_services.Injection.operation cctxt ?async ~chain signed_bytes >>=? fun oph -> - State.record_endorsement cctxt pkh level >>=? fun () -> + State.record cctxt pkh level >>=? fun () -> return oph let check_endorsement cctxt level pkh = - State.get_endorsement cctxt pkh >>=? function + State.get cctxt pkh >>=? function | None -> return () | Some recorded_level -> if Raw_level.(level = recorded_level) then @@ -100,7 +50,7 @@ let check_endorsement cctxt level pkh = return () let previously_endorsed_level cctxt pkh new_lvl = - State.get_endorsement cctxt pkh >>=? function + State.get cctxt pkh >>=? function | None -> return false | Some last_lvl -> return (Raw_level.(last_lvl >= new_lvl)) diff --git a/src/proto_alpha/lib_delegate/daemon_state.ml b/src/proto_alpha/lib_delegate/daemon_state.ml new file mode 100644 index 000000000..da35c6ac2 --- /dev/null +++ b/src/proto_alpha/lib_delegate/daemon_state.ml @@ -0,0 +1,56 @@ +open Proto_alpha +open Alpha_context + +module Make(M:sig val name :string end) : sig + + val get: + #Client_context.wallet -> + Signature.Public_key_hash.t -> + Raw_level.t option tzresult Lwt.t + + val record: + #Client_context.wallet -> + Signature.Public_key_hash.t -> + Raw_level.t -> + unit tzresult Lwt.t + +end = struct + type t = (string * Raw_level.t) list + + let encoding : t Data_encoding.t = + Data_encoding.assoc Raw_level.encoding + + let name = M.name + + let load (wallet : #Client_context.wallet) = + wallet#load name encoding ~default:[] + + let save (wallet : #Client_context.wallet) list = + wallet#write name list encoding + + let get (wallet : #Client_context.wallet) (delegate_key:Signature.public_key_hash) = + wallet#with_lock + (fun () -> + load wallet >>=? fun l -> + return (List.assoc_opt (Signature.Public_key_hash.to_short_b58check delegate_key) l) + ) + + let record (wallet : #Client_context.wallet) (delegate:Signature.public_key_hash) (new_lvl:Raw_level.t) = + begin + wallet#with_lock (fun () -> + begin + load wallet >>=? fun l -> + let delegate_key = Signature.Public_key_hash.to_short_b58check delegate + in + let remove_old l = + List.filter + (fun (_, lvl) -> Raw_level.diff new_lvl lvl < 50l (* FIXME: magic constant*)) + l + in + save wallet ((delegate_key, new_lvl):: + List.remove_assoc delegate_key (remove_old l)) + end) + end +end + +