{- |
Module      : Singular.Registry.FoldAdmission
Description : Pure fold admission and within-batch checks (#519)
License     : Apache-2.0

Pull request 3 of #519 promotes the fold's window rule out of the command
line into the registry library, beside the within-batch rule the model
states. Both functions are pure over POSIX milliseconds: no clock, no
provider, no command line, no open datum. They mirror the model's names —
'foldAdmission' and 'batchConsumesCreated' of @Singular.Model_ — so the
builder, the selection and the command line call one predicate each, and
the conformance unit suite checks the library against the model at the
boundary and one past it.

A request's deadline is its submission time plus the registry's processing
time, never the clock. A fold is admitted when its finite validity upper
bound, excluded as the ledger hands it to a script, is at or before the
deadline of every request it includes; equality admits. A batch that
terminates a key an earlier insertion of the same batch created is refused
@token-missing@: the termination must burn the active token from an input,
and the insertion delivers it to an output of the same transaction.
-}
module Singular.Registry.FoldAdmission
    ( BatchEdge (..)
    , AdmissionRefusal (..)
    , BatchConsumption (..)
    , foldAdmission
    , batchConsumesCreated
    , admissionReason
    , admissionSource
    , consumptionReason
    , consumptionSource
    , mintReason
    , mintSource
    ) where

{- | The edge a batched request takes, as far as the within-batch check
reads it: an insertion creates the key's active holding, a termination
burns one, any other edge is neither.
-}
data BatchEdge
    = BatchInsert
    | BatchTerminate
    | BatchOther
    deriving stock (BatchEdge -> BatchEdge -> Bool
(BatchEdge -> BatchEdge -> Bool)
-> (BatchEdge -> BatchEdge -> Bool) -> Eq BatchEdge
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BatchEdge -> BatchEdge -> Bool
== :: BatchEdge -> BatchEdge -> Bool
$c/= :: BatchEdge -> BatchEdge -> Bool
/= :: BatchEdge -> BatchEdge -> Bool
Eq, Int -> BatchEdge -> ShowS
[BatchEdge] -> ShowS
BatchEdge -> String
(Int -> BatchEdge -> ShowS)
-> (BatchEdge -> String)
-> ([BatchEdge] -> ShowS)
-> Show BatchEdge
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BatchEdge -> ShowS
showsPrec :: Int -> BatchEdge -> ShowS
$cshow :: BatchEdge -> String
show :: BatchEdge -> String
$cshowList :: [BatchEdge] -> ShowS
showList :: [BatchEdge] -> ShowS
Show)

{- | Why a fold is not admitted: the first included request, in input
order, whose deadline the upper bound passes, with its submission time,
its deadline and the bound. The reason is 'admissionReason' with source
'admissionSource'.
-}
data AdmissionRefusal ref = AdmissionRefusal
    { forall ref. AdmissionRefusal ref -> ref
refusalRef :: ref
    , forall ref. AdmissionRefusal ref -> Integer
refusalSubmittedAt :: Integer
    , forall ref. AdmissionRefusal ref -> Integer
refusalDeadline :: Integer
    , forall ref. AdmissionRefusal ref -> Integer
refusalBound :: Integer
    }
    deriving stock (AdmissionRefusal ref -> AdmissionRefusal ref -> Bool
(AdmissionRefusal ref -> AdmissionRefusal ref -> Bool)
-> (AdmissionRefusal ref -> AdmissionRefusal ref -> Bool)
-> Eq (AdmissionRefusal ref)
forall ref.
Eq ref =>
AdmissionRefusal ref -> AdmissionRefusal ref -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall ref.
Eq ref =>
AdmissionRefusal ref -> AdmissionRefusal ref -> Bool
== :: AdmissionRefusal ref -> AdmissionRefusal ref -> Bool
$c/= :: forall ref.
Eq ref =>
AdmissionRefusal ref -> AdmissionRefusal ref -> Bool
/= :: AdmissionRefusal ref -> AdmissionRefusal ref -> Bool
Eq, Int -> AdmissionRefusal ref -> ShowS
[AdmissionRefusal ref] -> ShowS
AdmissionRefusal ref -> String
(Int -> AdmissionRefusal ref -> ShowS)
-> (AdmissionRefusal ref -> String)
-> ([AdmissionRefusal ref] -> ShowS)
-> Show (AdmissionRefusal ref)
forall ref. Show ref => Int -> AdmissionRefusal ref -> ShowS
forall ref. Show ref => [AdmissionRefusal ref] -> ShowS
forall ref. Show ref => AdmissionRefusal ref -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall ref. Show ref => Int -> AdmissionRefusal ref -> ShowS
showsPrec :: Int -> AdmissionRefusal ref -> ShowS
$cshow :: forall ref. Show ref => AdmissionRefusal ref -> String
show :: AdmissionRefusal ref -> String
$cshowList :: forall ref. Show ref => [AdmissionRefusal ref] -> ShowS
showList :: [AdmissionRefusal ref] -> ShowS
Show)

{- | Why a batch consumes what it creates: the terminating request and the
key. The reason is 'consumptionReason' with source 'consumptionSource'.
-}
data BatchConsumption ref key = BatchConsumption
    { forall ref key. BatchConsumption ref key -> ref
consumptionRef :: ref
    , forall ref key. BatchConsumption ref key -> key
consumptionKey :: key
    }
    deriving stock (BatchConsumption ref key -> BatchConsumption ref key -> Bool
(BatchConsumption ref key -> BatchConsumption ref key -> Bool)
-> (BatchConsumption ref key -> BatchConsumption ref key -> Bool)
-> Eq (BatchConsumption ref key)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall ref key.
(Eq ref, Eq key) =>
BatchConsumption ref key -> BatchConsumption ref key -> Bool
$c== :: forall ref key.
(Eq ref, Eq key) =>
BatchConsumption ref key -> BatchConsumption ref key -> Bool
== :: BatchConsumption ref key -> BatchConsumption ref key -> Bool
$c/= :: forall ref key.
(Eq ref, Eq key) =>
BatchConsumption ref key -> BatchConsumption ref key -> Bool
/= :: BatchConsumption ref key -> BatchConsumption ref key -> Bool
Eq, Int -> BatchConsumption ref key -> ShowS
[BatchConsumption ref key] -> ShowS
BatchConsumption ref key -> String
(Int -> BatchConsumption ref key -> ShowS)
-> (BatchConsumption ref key -> String)
-> ([BatchConsumption ref key] -> ShowS)
-> Show (BatchConsumption ref key)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall ref key.
(Show ref, Show key) =>
Int -> BatchConsumption ref key -> ShowS
forall ref key.
(Show ref, Show key) =>
[BatchConsumption ref key] -> ShowS
forall ref key.
(Show ref, Show key) =>
BatchConsumption ref key -> String
$cshowsPrec :: forall ref key.
(Show ref, Show key) =>
Int -> BatchConsumption ref key -> ShowS
showsPrec :: Int -> BatchConsumption ref key -> ShowS
$cshow :: forall ref key.
(Show ref, Show key) =>
BatchConsumption ref key -> String
show :: BatchConsumption ref key -> String
$cshowList :: forall ref key.
(Show ref, Show key) =>
[BatchConsumption ref key] -> ShowS
showList :: [BatchConsumption ref key] -> ShowS
Show)

{- | The model's refusal word for a fold outside its window, reported
verbatim.
-}
admissionReason :: String
admissionReason :: String
admissionReason = String
"not-phase1"

{- | Where the window refusal comes from: the model, never processor
policy.
-}
admissionSource :: String
admissionSource :: String
admissionSource = String
"model-refusal"

{- | The model's refusal word for a batch that terminates a key it just
inserted, reported verbatim.
-}
consumptionReason :: String
consumptionReason :: String
consumptionReason = String
"token-missing"

{- | The model's refusal word for a batch whose claimed mint differs from
what its edges mint at any key, reported verbatim.
-}
mintReason :: String
mintReason :: String
mintReason = String
"net-mint-mismatch"

{- | Where the keyed-mint refusal comes from: the model, never processor
policy.
-}
mintSource :: String
mintSource :: String
mintSource = String
"model-refusal"

{- | Where the within-batch refusal comes from: the model, never processor
policy.
-}
consumptionSource :: String
consumptionSource :: String
consumptionSource = String
"model-refusal"

{- | The fold admission over POSIX milliseconds: the registry's processing
time, the fold's finite excluded upper bound, and the included requests as
output reference with submission time, in input order. Admits when the
bound is at or before every request's deadline; otherwise refuses with the
first request in input order whose deadline the bound passes.
-}
foldAdmission
    :: Integer -> Integer -> [(ref, Integer)] -> Maybe (AdmissionRefusal ref)
foldAdmission :: forall ref.
Integer
-> Integer -> [(ref, Integer)] -> Maybe (AdmissionRefusal ref)
foldAdmission Integer
processTime Integer
upperBound = [(ref, Integer)] -> Maybe (AdmissionRefusal ref)
forall {ref}. [(ref, Integer)] -> Maybe (AdmissionRefusal ref)
go
  where
    go :: [(ref, Integer)] -> Maybe (AdmissionRefusal ref)
go [] = Maybe (AdmissionRefusal ref)
forall a. Maybe a
Nothing
    go ((ref
ref, Integer
submittedAt) : [(ref, Integer)]
rest)
        | Integer
upperBound Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Integer
submittedAt Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
processTime = [(ref, Integer)] -> Maybe (AdmissionRefusal ref)
go [(ref, Integer)]
rest
        | Bool
otherwise =
            AdmissionRefusal ref -> Maybe (AdmissionRefusal ref)
forall a. a -> Maybe a
Just
                ( ref -> Integer -> Integer -> Integer -> AdmissionRefusal ref
forall ref.
ref -> Integer -> Integer -> Integer -> AdmissionRefusal ref
AdmissionRefusal
                    ref
ref
                    Integer
submittedAt
                    (Integer
submittedAt Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
processTime)
                    Integer
upperBound
                )

{- | The within-batch check over the included requests in order, each as
output reference with key and edge. Refuses with the terminating request
and the key when a termination names a key an earlier insertion of the
same batch created; anything else is the law's business, not this check's.
-}
batchConsumesCreated
    :: (Eq key)
    => [(ref, key, BatchEdge)] -> Maybe (BatchConsumption ref key)
batchConsumesCreated :: forall key ref.
Eq key =>
[(ref, key, BatchEdge)] -> Maybe (BatchConsumption ref key)
batchConsumesCreated = [key]
-> [(ref, key, BatchEdge)] -> Maybe (BatchConsumption ref key)
forall {key} {ref}.
Eq key =>
[key]
-> [(ref, key, BatchEdge)] -> Maybe (BatchConsumption ref key)
go []
  where
    go :: [key]
-> [(ref, key, BatchEdge)] -> Maybe (BatchConsumption ref key)
go [key]
_ [] = Maybe (BatchConsumption ref key)
forall a. Maybe a
Nothing
    go [key]
created ((ref
ref, key
key, BatchEdge
edge) : [(ref, key, BatchEdge)]
rest) = case BatchEdge
edge of
        BatchEdge
BatchTerminate
            | key
key key -> [key] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [key]
created -> BatchConsumption ref key -> Maybe (BatchConsumption ref key)
forall a. a -> Maybe a
Just (ref -> key -> BatchConsumption ref key
forall ref key. ref -> key -> BatchConsumption ref key
BatchConsumption ref
ref key
key)
            | Bool
otherwise -> [key]
-> [(ref, key, BatchEdge)] -> Maybe (BatchConsumption ref key)
go [key]
created [(ref, key, BatchEdge)]
rest
        BatchEdge
BatchInsert -> [key]
-> [(ref, key, BatchEdge)] -> Maybe (BatchConsumption ref key)
go (key
key key -> [key] -> [key]
forall a. a -> [a] -> [a]
: [key]
created) [(ref, key, BatchEdge)]
rest
        BatchEdge
BatchOther -> [key]
-> [(ref, key, BatchEdge)] -> Maybe (BatchConsumption ref key)
go [key]
created [(ref, key, BatchEdge)]
rest