wip int64 timestamp

This commit is contained in:
swrup 2025-12-07 08:51:42 +01:00
parent 32fa5b1fc3
commit 12a384a33a
8 changed files with 49 additions and 28 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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