Skip to content

Commit

Permalink
Introduction of safe time management
Browse files Browse the repository at this point in the history
  • Loading branch information
CryptoPascal31 committed Nov 11, 2023
1 parent 9c79436 commit 888ca46
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
42 changes: 33 additions & 9 deletions pact/contracts/util-time.pact
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,21 @@
(enforce-keyset "free.util-lib"))

(use util-chain-data [block-time block-height])
(use util-math [between])
(use util-math [between pow10])

(defconst EPOCH (time "1970-01-01T00:00:00Z"))
(defconst EPOCH:time (time "1970-01-01T00:00:00Z"))


(defconst HASKELL-EPOCH:time (time "1858-11-17T00:00:00Z"))

(defconst GENESIS:time (time "2019-10-30T00:01:00Z"))

(defconst SAFE-DELTA:decimal (- (/ (^ 2.0 62.0) (pow10 6)) 1.0))

(defconst MIN-SAFE-TIME:time (add-time HASKELL-EPOCH (- SAFE-DELTA)))

(defconst MAX-SAFE-TIME:time (add-time HASKELL-EPOCH SAFE-DELTA))

(defconst GENESIS (time "2019-10-30T00:01:00Z"))

(defconst BLOCK-TIME 30.0)

Expand All @@ -41,6 +51,18 @@
"Returns the current time"
(block-time))

(defun --enforce-safe-time:bool (in:time)
(enforce (time-between MIN-SAFE-TIME MAX-SAFE-TIME in) "Time out of safe bounds"))

(defun --enforce-safe-delta:bool (in:decimal)
(enforce (between (- SAFE-DELTA) SAFE-DELTA in) "Delta out of safe bounds"))

(defun add-time-safe:time (in:time delta:decimal)
(--enforce-safe-time in)
(--enforce-safe-delta delta)
(add-time in delta)
)

(defun tomorrow:time ()
"Returns current time + 24 hours"
(from-now (days 1))
Expand All @@ -53,6 +75,7 @@

(defun from-now:time (delta:decimal)
"Returns the delta time taking now as a reference"
(--enforce-safe-delta delta)
(add-time (now) delta)
)

Expand All @@ -63,15 +86,13 @@

(defun to-timestamp:decimal (in:time)
"Computes an Unix timestamp of the input date"
(--enforce-safe-time in)
(diff-time in (epoch))
)

(defconst TIMESTAMP-LIMIT:decimal 3155695200000.0)

(defun from-timestamp:time (timestamp:decimal)
"Computes a time from an Unix timestamp"
; Since add-time is not safe for big numbers we enforce a min/max of 100kyears
(enforce (between (- TIMESTAMP-LIMIT) TIMESTAMP-LIMIT timestamp) "Timestamp out of bounds")
(--enforce-safe-delta timestamp)
(add-time (epoch) timestamp)
)

Expand Down Expand Up @@ -112,15 +133,18 @@

(defun est-height-at-time:integer (target-time:time)
"Estimates the block height at a target-time"
(--enforce-safe-time target-time)
(let* ((delta (diff-time target-time (now)))
(est-block (+ (block-height) (round (/ delta BLOCK-TIME)))))
(if (> est-block 0 ) est-block 0))
)

(defun est-time-at-height:time (target-block:integer)
"Estimates the time of the target-block height"
(let ((delta (- target-block (block-height))))
(add-time (now) (* BLOCK-TIME (dec delta))))
(let* ((delta-blocks (- target-block (block-height)))
(delta (* BLOCK-TIME (dec delta-blocks))))
(--enforce-safe-delta delta)
(add-time (now) delta))
)

;; Diff time functions
Expand Down
4 changes: 2 additions & 2 deletions pact/tests_repl/util-time-test.repl
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
;;; (from-timestamp)
(expect "from-timestamp must be UNIX EPOCH for ZERO" (epoch) (from-timestamp 0.0))
(expect "from-timestamp must be accurate for this example" (time "2022-12-05T00:08:53Z") (from-timestamp 1670198933.0))
(expect-failure "Out of bounds timestamp" "Timestamp out of bounds" (from-timestamp 6311390400000.0))
(expect-failure "Out of bounds timestamp" "Timestamp out of bounds" (from-timestamp -6311390400000.0))
(expect-failure "Out of bounds timestamp" "Delta out of safe bounds" (from-timestamp 6311390400000.0))
(expect-failure "Out of bounds timestamp" "Delta out of safe bounds" (from-timestamp -6311390400000.0))

;;; (earliest ...)
(expect "Test earliest" (time "2022-12-04T14:44:24Z") (earliest (time "2022-12-04T14:54:24Z") (time "2022-12-04T14:44:24Z")))
Expand Down

0 comments on commit 888ca46

Please sign in to comment.