diff --git a/src/config.ml b/src/config.ml index e3c112ee..827fe57f 100644 --- a/src/config.ml +++ b/src/config.ml @@ -128,9 +128,9 @@ module Coin = struct type t = { section_name: string; value: Amount.t; - duration_withdraw: Ptime.Span.t; - duration_spend: Ptime.Span.t; - duration_legal: Ptime.Span.t; + duration_withdraw: Timestamp.span; + duration_spend: Timestamp.span; + duration_legal: Timestamp.span; fee_withdraw: Amount.t; fee_deposit: Amount.t; fee_refresh: Amount.t; diff --git a/src/denomination.ml b/src/denomination.ml index 7b296362..ae561bd9 100644 --- a/src/denomination.ml +++ b/src/denomination.ml @@ -36,16 +36,14 @@ let make Config.Coin.t) = assert (cipher = `RSA); - let stamp_start = Ptime_clock.now () |> Option.some in + let stamp_start = Timestamp.now () in let stamp_expire_withdraw = - Timestamp.add_span_exn stamp_start (Some duration_withdraw) + Timestamp.add_span_exn stamp_start duration_withdraw in let stamp_expire_deposit = - Timestamp.add_span_exn stamp_start (Some duration_spend) - in - let stamp_expire_legal = - Timestamp.add_span_exn stamp_start (Some duration_legal) + Timestamp.add_span_exn stamp_start duration_spend in + let stamp_expire_legal = Timestamp.add_span_exn stamp_start duration_legal in let open Mirage_crypto_pk.Rsa in let priv = generate ~bits:rsa_keysize () in diff --git a/src/management.ml b/src/management.ml index 2f6d19f4..2767e139 100644 --- a/src/management.ml +++ b/src/management.ml @@ -196,15 +196,11 @@ let update_master_signatures ~db_conn ~sm_signkey ~sm_denom match opt with None -> error | Some v -> Ok v in let check = function false -> error | true -> Ok () in - (* TODO time *) - let timestamp_equal a b = - let timestamp_to_int64 = function - | None -> None - | Some v -> v |> Ptime.to_float_s |> Int64.of_float |> Option.some - in - timestamp_to_int64 a = timestamp_to_int64 b + let* () = + check + (Timestamp.to_int64 valid_from + = Timestamp.to_int64 denom.stamp_start) in - let* () = check (timestamp_equal valid_from denom.stamp_start) in let* () = check (coin = denom.value) in let* () = check (fee_refund = denom.fee_refund) in let* () = check (age_mask = denom.age_mask) in diff --git a/src/parse_config.ml b/src/parse_config.ml index 0fd672a3..fb079846 100644 --- a/src/parse_config.ml +++ b/src/parse_config.ml @@ -173,9 +173,7 @@ module Parse_duration = struct (fun acc { number; dunit } -> acc + (number * dunit_to_seconds dunit)) 0 t in - let acc = Int64.of_int acc in - let ptime = ptime_span_of_int64 acc in - ptime + Timestamp.span_of_int64 @@ Int64.of_int acc let parse s : duration_element list = match parse_string ~consume:All duration s with diff --git a/src/pg.ml b/src/pg.ml index d958c6a6..70a1e99a 100644 --- a/src/pg.ml +++ b/src/pg.ml @@ -20,9 +20,10 @@ module Caqti_type = struct Amount.make ~sign:None ~currency:Config.currency ~value ~fraction) (t2 int64 int32) - (* we want to use int64 timestamps, + (* ! do not use ptime + we want to use int64 timestamps, not postgresql built-in timestamp type *) - let ptime : Ptime.t option t = Timestamp.caqti + let ptime : unit t = Caqti_type.unit let time = Timestamp.caqti let age_mask : int t = Caqti_type.int let rsa_public = RsaPublicKey.caqti diff --git a/src/signkey.ml b/src/signkey.ml index 883dfa93..e07eba70 100644 --- a/src/signkey.ml +++ b/src/signkey.ml @@ -12,10 +12,9 @@ type t = { } let make () = - let stamp_start = Ptime_clock.now () |> Option.some in + let stamp_start = Timestamp.now () in let stamp_expire = - Timestamp.add_span_exn stamp_start - (Some Config.Exchange.signkey_legal_duration) + Timestamp.add_span_exn stamp_start Config.Exchange.signkey_legal_duration in let stamp_end = stamp_expire in let priv, pub = Mirage_crypto_ec.Ed25519.generate () in diff --git a/src/timestamp.ml b/src/timestamp.ml index 710ac992..bb061c90 100644 --- a/src/timestamp.ml +++ b/src/timestamp.ml @@ -1,6 +1,8 @@ type t = Ptime.t option type span = Ptime.Span.t option +let now () = Ptime_clock.now () |> Option.some + let diff a b = match (a, b) with | None, _ | _, None -> None @@ -56,9 +58,31 @@ let ptime_of_int64 i = | None -> Fmt.failwith "ptime_of_int64 error: `%Ld` is not a valid ptime" i | Some ts -> ts +let ptime_span_to_int64 span = span |> Ptime.Span.to_float_s |> Int64.of_float + +let ptime_span_of_int64 i = + match Ptime.Span.of_float_s (Int64.to_float i) with + | None -> + Fmt.failwith "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 -> ptime_to_int64 p let decode_int64 i = if i = Int64.max_int then None else Some (ptime_of_int64 i) +let encode_span_int64 = function + | None -> Int64.max_int + | Some p -> ptime_span_to_int64 p + +let decode_span_int64 i = + if i = Int64.max_int then None else Some (ptime_span_of_int64 i) + +let to_int64 = encode_int64 +let of_int64 = decode_int64 +let span_to_int64 = encode_span_int64 +let span_of_int64 = decode_span_int64 + (* UINT64_MAX represents "never". *) let bin = Bin.map Bin.neint64 decode_int64 encode_int64 let bin_nbo = Bin.map Bin.beint64 decode_int64 encode_int64 diff --git a/src/timestamp.mli b/src/timestamp.mli index ae81b3a3..4da82338 100644 --- a/src/timestamp.mli +++ b/src/timestamp.mli @@ -2,15 +2,20 @@ uhuh! need to be int64 for binary/pg round trip *) -type t = Ptime.t option -type span = Ptime.Span.t option +type t +type span +val now : unit -> t val diff : t -> t -> span val add_span_exn : t -> span -> t val of_span_exn : span -> t +val of_int64 : int64 -> t +val to_int64 : t -> int64 +val span_of_int64 : int64 -> span +val span_to_int64 : span -> int64 (* - *) 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