From a3e3b8fd33a3895d3a48cdafc092b5c70f1110bc Mon Sep 17 00:00:00 2001 From: swrup Date: Thu, 11 Dec 2025 07:23:51 +0100 Subject: [PATCH] --- src/bin_sig.ml | 8 +------- src/bin_type.ml | 25 +++++++++++++------------ src/pg.ml | 7 ++----- src/timestamp.ml | 26 +++++++++++++++++++++++++- src/timestamp.mli | 10 +++++++++- 5 files changed, 50 insertions(+), 26 deletions(-) diff --git a/src/bin_sig.ml b/src/bin_sig.ml index 89e97930..45cdeb93 100644 --- a/src/bin_sig.ml +++ b/src/bin_sig.ml @@ -5,12 +5,6 @@ open Bin_type (* TODO keep this? some of those are actuall ecdhe, or union of eddsa|ecdhe *) module Aliases = struct - module TimestampNBO : Time_S = struct - type t = Timestamp.t - - let bin = Timestamp.bin_nbo - end - module AmountNBO = struct type t = Amount.t @@ -397,7 +391,7 @@ module GlobalFees = struct }) |+ Purpose.field purpose |+ field TimeAbsoluteNBO.bin (fun t -> t.start_date) - |+ field TimeRelativeNBO.bin (fun t -> t.end_date) + |+ field TimeAbsoluteNBO.bin (fun t -> t.end_date) |+ field TimeRelativeNBO.bin (fun t -> t.purse_timeout) |+ field TimeRelativeNBO.bin (fun t -> t.kyc_timeout) |+ field TimeRelativeNBO.bin (fun t -> t.history_expiration) diff --git a/src/bin_type.ml b/src/bin_type.ml index 7370141a..e2a82283 100644 --- a/src/bin_type.ml +++ b/src/bin_type.ml @@ -58,28 +58,29 @@ module Bytes_64 = struct end (* -- Time -- *) -module type Time_S = sig - type t = Timestamp.t - - val bin : t Bin.t -end - -module TIME : Time_S = struct +module TimeAbsolute = struct type t = Timestamp.t let bin = Timestamp.bin end -module TIME_NBO : Time_S = struct +module TimeAbsoluteNBO = struct type t = Timestamp.t let bin = Timestamp.bin_nbo end -module TimeAbsolute : Time_S = TIME -module TimeAbsoluteNBO : Time_S = TIME_NBO -module TimeRelative : Time_S = TIME -module TimeRelativeNBO : Time_S = TIME_NBO +module TimeRelative = struct + type t = Timestamp.Span.t + + let bin = Timestamp.Span.bin +end + +module TimeRelativeNBO = struct + type t = Timestamp.Span.t + + let bin = Timestamp.Span.bin_nbo +end (* -- Cryptographic primitives -- *) diff --git a/src/pg.ml b/src/pg.ml index aa913e5c..1eb7ea30 100644 --- a/src/pg.ml +++ b/src/pg.ml @@ -277,15 +277,12 @@ let insert_global_fee = history_fee; account_fee; purse_fee; - history_expiration= _; + history_expiration; purse_account_limit; - purse_timeout= _; + purse_timeout; master_sig; } -> - (* TODO time *) - let purse_timeout = None in - let history_expiration = None in Conn.exec insert_global_fee ( start_date, end_date, diff --git a/src/timestamp.ml b/src/timestamp.ml index d01bf83c..53371473 100644 --- a/src/timestamp.ml +++ b/src/timestamp.ml @@ -48,7 +48,6 @@ let jsont = |> Jsont.Object.mem "t_s" number_or_never_jsont ~enc:Fun.id |> Jsont.Object.finish -(* TODO exn *) let ptime_to_int64 ptime = ptime |> Ptime.to_float_s |> Int64.of_float let ptime_of_int64 i = @@ -77,3 +76,28 @@ let compare a b = let b = ptime_to_int64 b in let c = Int64.compare a b in Some c + +module Span = struct + type t = span + + let to_int64 ptime = ptime |> Ptime.Span.to_float_s |> Int64.of_float + + let of_int64 i = + match Ptime.Span.of_float_s (Int64.to_float i) with + | None -> + Fmt.failwith + "ptime_span_of_int64 error: `%Ld` is not a valid ptime span" i + | Some ts -> ts + + let encode_int64 = function None -> Int64.max_int | Some p -> to_int64 p + let decode_int64 i = if i = Int64.max_int then None else Some (of_int64 i) + + (* UINT64_MAX represents "forever" *) + let bin = Bin.map Bin.neint64 decode_int64 encode_int64 + let bin_nbo = Bin.map Bin.beint64 decode_int64 encode_int64 + + let caqti : t Caqti_type.t = + let encode v = Ok (encode_int64 v) in + let decode v = Ok (decode_int64 v) in + Caqti_type.custom ~encode ~decode Caqti_type.int64 +end diff --git a/src/timestamp.mli b/src/timestamp.mli index d5d10c5b..26d21661 100644 --- a/src/timestamp.mli +++ b/src/timestamp.mli @@ -7,6 +7,14 @@ type t = Ptime.t option type span = Ptime.Span.t option +module Span : sig + type t = span + + val bin : t Bin.t + val bin_nbo : t Bin.t + val caqti : t Caqti_type.t +end + val diff : t -> t -> span val add_span_exn : t -> span -> t val of_span_exn : span -> t @@ -16,4 +24,4 @@ val compare : t -> t -> int option val jsont : t Jsont.t val bin : t Bin.t val bin_nbo : t Bin.t -val caqti : Ptime.t option Caqti_type.t +val caqti : t Caqti_type.t