Alpha/Endorser: Refactor State Module
This commit is contained in:
parent
bea2344c3c
commit
7d0c526644
@ -12,57 +12,7 @@ open Alpha_context
|
|||||||
|
|
||||||
include Logging.Make(struct let name = "client.endorsement" end)
|
include Logging.Make(struct let name = "client.endorsement" end)
|
||||||
|
|
||||||
module State : sig
|
module State = Daemon_state.Make(struct let name = "endorsements" end)
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
let get_signing_slots cctxt ?(chain = `Main) block delegate level =
|
let get_signing_slots cctxt ?(chain = `Main) block delegate level =
|
||||||
Alpha_services.Delegate.Endorsing_rights.get cctxt
|
Alpha_services.Delegate.Endorsing_rights.get cctxt
|
||||||
@ -86,12 +36,12 @@ let inject_endorsement
|
|||||||
Client_keys.append cctxt
|
Client_keys.append cctxt
|
||||||
src_sk ~watermark:Endorsement bytes >>=? fun signed_bytes ->
|
src_sk ~watermark:Endorsement bytes >>=? fun signed_bytes ->
|
||||||
Shell_services.Injection.operation cctxt ?async ~chain signed_bytes >>=? fun oph ->
|
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
|
return oph
|
||||||
|
|
||||||
|
|
||||||
let check_endorsement cctxt level pkh =
|
let check_endorsement cctxt level pkh =
|
||||||
State.get_endorsement cctxt pkh >>=? function
|
State.get cctxt pkh >>=? function
|
||||||
| None -> return ()
|
| None -> return ()
|
||||||
| Some recorded_level ->
|
| Some recorded_level ->
|
||||||
if Raw_level.(level = recorded_level) then
|
if Raw_level.(level = recorded_level) then
|
||||||
@ -100,7 +50,7 @@ let check_endorsement cctxt level pkh =
|
|||||||
return ()
|
return ()
|
||||||
|
|
||||||
let previously_endorsed_level cctxt pkh new_lvl =
|
let previously_endorsed_level cctxt pkh new_lvl =
|
||||||
State.get_endorsement cctxt pkh >>=? function
|
State.get cctxt pkh >>=? function
|
||||||
| None -> return false
|
| None -> return false
|
||||||
| Some last_lvl ->
|
| Some last_lvl ->
|
||||||
return (Raw_level.(last_lvl >= new_lvl))
|
return (Raw_level.(last_lvl >= new_lvl))
|
||||||
|
56
src/proto_alpha/lib_delegate/daemon_state.ml
Normal file
56
src/proto_alpha/lib_delegate/daemon_state.ml
Normal 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
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user