{- |
Module      : Singular.State.Refusal
Description : Why a builder of the Singular state declines to build a transaction
License     : Apache-2.0

A builder returns a 'Refusal' instead of a transaction it knows the Singular
state validator, the application's validator or the ledger would refuse. Every
refusal names a fact about the chain or the arguments that the caller can act
on. A failed read of the chain is not a refusal: it throws, as the other
builders' reads do.
-}
module Singular.State.Refusal
    ( Refusal (..)
    , CapacityLimit (..)
    , capacityMessage
    ) where

import Data.ByteString (ByteString)
import Data.Text (Text)
import Data.Text qualified as Text

import Cardano.Ledger.Coin (Coin)
import Cardano.Ledger.Hashes (ScriptHash)
import Cardano.Ledger.TxIn (TxIn)

import Singular.Registry.TrieState (RegistryIdentity, TrieFailure)

-- | Actual work or bytes exceeding the observed protocol limit: used, limit.
data CapacityLimit
    = TransactionSize Integer Integer
    | ExecutionMemory Integer Integer
    | ExecutionSteps Integer Integer
    deriving stock (CapacityLimit -> CapacityLimit -> Bool
(CapacityLimit -> CapacityLimit -> Bool)
-> (CapacityLimit -> CapacityLimit -> Bool) -> Eq CapacityLimit
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CapacityLimit -> CapacityLimit -> Bool
== :: CapacityLimit -> CapacityLimit -> Bool
$c/= :: CapacityLimit -> CapacityLimit -> Bool
/= :: CapacityLimit -> CapacityLimit -> Bool
Eq, Int -> CapacityLimit -> ShowS
[CapacityLimit] -> ShowS
CapacityLimit -> String
(Int -> CapacityLimit -> ShowS)
-> (CapacityLimit -> String)
-> ([CapacityLimit] -> ShowS)
-> Show CapacityLimit
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CapacityLimit -> ShowS
showsPrec :: Int -> CapacityLimit -> ShowS
$cshow :: CapacityLimit -> String
show :: CapacityLimit -> String
$cshowList :: [CapacityLimit] -> ShowS
showList :: [CapacityLimit] -> ShowS
Show)

-- | Name the actual protocol capacity, measured work and limit in plain text.
capacityMessage :: CapacityLimit -> Text
capacityMessage :: CapacityLimit -> Text
capacityMessage CapacityLimit
limit = String -> Text
Text.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ case CapacityLimit
limit of
    TransactionSize Integer
used Integer
allowed ->
        String
"the transaction size "
            String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Integer -> String
forall a. Show a => a -> String
show Integer
used
            String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" bytes exceeds the limit "
            String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Integer -> String
forall a. Show a => a -> String
show Integer
allowed
            String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" bytes"
    ExecutionMemory Integer
used Integer
allowed ->
        String
"the execution memory "
            String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Integer -> String
forall a. Show a => a -> String
show Integer
used
            String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" exceeds the limit "
            String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Integer -> String
forall a. Show a => a -> String
show Integer
allowed
    ExecutionSteps Integer
used Integer
allowed ->
        String
"the execution steps "
            String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Integer -> String
forall a. Show a => a -> String
show Integer
used
            String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" exceed the limit "
            String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Integer -> String
forall a. Show a => a -> String
show Integer
allowed

-- | Why a builder declined.
data Refusal
    = -- | The payer's address holds no ADA-only output for fee and collateral.
      PayerHasNoFunding
    | -- | The payer's payment credential is a script, which cannot sign.
      PayerIsNotAKey
    | -- | The seed is not an unspent output of the payer.
      SeedNotAtPayer TxIn
    | -- | The reference output named for a script is not on the chain.
      ReferenceScriptMissing TxIn
    | {- | The reference output carries no script, or another one: the hash it
      carries, when it carries a script, and the expected hash.
      -}
      ReferenceScriptMismatch TxIn (Maybe ScriptHash) ScriptHash
    | -- | No output holds the state token of the registry.
      NoRegistryState RegistryIdentity
    | -- | Several outputs hold the state token; the count found.
      AmbiguousRegistryState Int
    | -- | The output that holds the state token is not a state of the registry.
      NotARegistryState TxIn
    | -- | The registry's public history cannot be replayed into its trie.
      TrieRefused TrieFailure
    | -- | There is nothing to fold.
      NothingToFold
    | -- | The same pending registration is named twice.
      DuplicateRequest TxIn
    | -- | Two pending registrations name the same key.
      DuplicateKey ByteString
    | -- | The key is already registered: its leaf is not unknown.
      KeyAlreadyRegistered ByteString
    | -- | A termination can only consume an active key.
      KeyNotActive ByteString
    | -- | The output to fold is not on the chain.
      PendingNotFound TxIn
    | {- | The output is not a pending registration the application decodes and
      authenticates: it holds no approval, or another, than the registration
      it carries commits.
      -}
      NotAPendingRegistration TxIn
    | -- | The pending registration belongs to another registry.
      PendingOfAnotherRegistry TxIn
    | {- | The deposit is below the minimum ADA of the holding it commits, so
      no fold could ever deliver it; the deposit and that minimum.
      -}
      DepositBelowHolding TxIn Coin Coin
    | -- | No validity interval at the tip ends before every processing deadline.
      ProcessingWindowClosed
    | -- | The transaction builder itself failed; its message.
      BuildFailed Text
    | -- | The actual built transaction exceeds this protocol capacity.
      CapacityExceeded CapacityLimit
    deriving stock (Refusal -> Refusal -> Bool
(Refusal -> Refusal -> Bool)
-> (Refusal -> Refusal -> Bool) -> Eq Refusal
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Refusal -> Refusal -> Bool
== :: Refusal -> Refusal -> Bool
$c/= :: Refusal -> Refusal -> Bool
/= :: Refusal -> Refusal -> Bool
Eq, Int -> Refusal -> ShowS
[Refusal] -> ShowS
Refusal -> String
(Int -> Refusal -> ShowS)
-> (Refusal -> String) -> ([Refusal] -> ShowS) -> Show Refusal
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Refusal -> ShowS
showsPrec :: Int -> Refusal -> ShowS
$cshow :: Refusal -> String
show :: Refusal -> String
$cshowList :: [Refusal] -> ShowS
showList :: [Refusal] -> ShowS
Show)