Alpha/Endorser: Refactor State Module

This commit is contained in:
MBourgoin 2018-06-18 12:34:03 +02:00 committed by Grégoire Henry
parent bea2344c3c
commit 7d0c526644
2 changed files with 60 additions and 54 deletions

View File

@ -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))

View File

@ -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