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 Cli_entries
|
|
|
|
|
|
|
|
(* TODO locking... *)
|
|
|
|
|
|
|
|
type t = (Block_hash.t * Nonce.t) list
|
|
|
|
|
|
|
|
let encoding : t Data_encoding.t =
|
|
|
|
let open Data_encoding in
|
|
|
|
list
|
|
|
|
(obj2
|
|
|
|
(req "block" Block_hash.encoding)
|
|
|
|
(req "nonce" Nonce.encoding))
|
|
|
|
|
|
|
|
let filename () =
|
|
|
|
Client_config.(base_dir#get // "nonces")
|
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let load cctxt =
|
2016-09-08 21:13:10 +04:00
|
|
|
let filename = filename () in
|
|
|
|
if not (Sys.file_exists filename) then
|
|
|
|
Lwt.return []
|
|
|
|
else
|
2016-12-01 21:27:53 +04:00
|
|
|
Data_encoding_ezjsonm.read_file filename >>= function
|
2016-12-03 16:05:02 +04:00
|
|
|
| None -> cctxt.Client_commands.error "couldn't to read the nonces file"
|
2016-09-08 21:13:10 +04:00
|
|
|
| Some json ->
|
|
|
|
match Data_encoding.Json.destruct encoding json with
|
|
|
|
| exception _ -> (* TODO print_error *)
|
2016-12-03 16:05:02 +04:00
|
|
|
cctxt.Client_commands.error "didn't understand the nonces file"
|
2016-09-08 21:13:10 +04:00
|
|
|
| list ->
|
|
|
|
Lwt.return list
|
|
|
|
|
|
|
|
let check_dir dirname =
|
|
|
|
if not (Sys.file_exists dirname) then
|
2016-12-01 21:27:53 +04:00
|
|
|
Lwt_utils.create_dir dirname
|
2016-09-08 21:13:10 +04:00
|
|
|
else
|
|
|
|
Lwt.return ()
|
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let save cctxt list =
|
2016-09-08 21:13:10 +04:00
|
|
|
Lwt.catch
|
|
|
|
(fun () ->
|
|
|
|
let dirname = Client_config.base_dir#get in
|
|
|
|
check_dir dirname >>= fun () ->
|
|
|
|
let filename = filename () in
|
|
|
|
let json = Data_encoding.Json.construct encoding list in
|
2016-12-01 21:27:53 +04:00
|
|
|
Data_encoding_ezjsonm.write_file filename json >>= function
|
2016-09-08 21:13:10 +04:00
|
|
|
| false -> failwith "Json.write_file"
|
|
|
|
| true -> return ())
|
|
|
|
(fun exn ->
|
2016-12-03 16:05:02 +04:00
|
|
|
cctxt.Client_commands.error
|
|
|
|
"could not write the nonces file: %s." (Printexc.to_string exn))
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let mem cctxt block_hash =
|
|
|
|
load cctxt >|= fun data ->
|
2016-09-08 21:13:10 +04:00
|
|
|
List.mem_assoc block_hash data
|
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let find cctxt block_hash =
|
|
|
|
load cctxt >|= fun data ->
|
2016-09-08 21:13:10 +04:00
|
|
|
try Some (List.assoc block_hash data)
|
|
|
|
with Not_found -> None
|
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let add cctxt block_hash nonce =
|
|
|
|
load cctxt >>= fun data ->
|
|
|
|
save cctxt ((block_hash, nonce) ::
|
2016-09-08 21:13:10 +04:00
|
|
|
List.remove_assoc block_hash data)
|
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let del cctxt block_hash =
|
|
|
|
load cctxt >>= fun data ->
|
|
|
|
save cctxt (List.remove_assoc block_hash data)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let dels cctxt hashes =
|
|
|
|
load cctxt >>= fun data ->
|
|
|
|
save cctxt @@
|
2016-09-08 21:13:10 +04:00
|
|
|
List.fold_left
|
|
|
|
(fun data hash -> List.remove_assoc hash data)
|
|
|
|
data hashes
|