Skip to content

Commit

Permalink
Assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
nc6 committed Jul 8, 2020
1 parent 8c26630 commit f9e19fa
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 9 deletions.
Expand Up @@ -135,7 +135,11 @@ ledgerTransition = do

dpstate' <-
trans @(DELEGS crypto) $
TRC (DelegsEnv slot txIx pp tx account, dpstate, StrictSeq.getSeq $ _certs $ _body tx)
TRC
( DelegsEnv slot txIx pp tx account,
dpstate,
StrictSeq.getSeq $ _certs $ _body tx
)

let DPState dstate pstate = dpstate
genDelegs = _genDelegs dstate
Expand Down
Expand Up @@ -13,10 +13,26 @@ module Shelley.Spec.Ledger.STS.Pool
)
where

import Cardano.Binary (FromCBOR (..), ToCBOR (..), decodeListLen, decodeWord, encodeListLen, matchSize)
import Cardano.Binary
( FromCBOR (..),
ToCBOR (..),
decodeListLen,
decodeWord,
encodeListLen,
matchSize,
)
import Cardano.Prelude (NoUnexpectedThunks (..))
import Control.Monad.Trans.Reader (asks)
import Control.State.Transition (STS (..), TRC (..), TransitionRule, failBecause, judgmentContext, liftSTS, (?!))
import Control.State.Transition
( Assertion (..),
STS (..),
TRC (..),
TransitionRule,
failBecause,
judgmentContext,
liftSTS,
(?!),
)
import Data.Kind (Type)
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
Expand All @@ -25,13 +41,18 @@ import Data.Word (Word64, Word8)
import GHC.Generics (Generic)
import Shelley.Spec.Ledger.BaseTypes (Globals (..), ShelleyBase, invalidKey)
import Shelley.Spec.Ledger.Coin (Coin)
import Shelley.Spec.Ledger.Core (addpair, haskey, removekey)
import Shelley.Spec.Ledger.Core (addpair, dom, haskey, removekey)
import Shelley.Spec.Ledger.Crypto (Crypto)
import Shelley.Spec.Ledger.Keys (KeyHash (..), KeyRole (..))
import Shelley.Spec.Ledger.LedgerState (PState (..), emptyPState)
import Shelley.Spec.Ledger.PParams (PParams, PParams' (..))
import Shelley.Spec.Ledger.Slot (EpochNo (..), SlotNo, epochInfoEpoch)
import Shelley.Spec.Ledger.TxData (DCert (..), PoolCert (..), PoolParams (..), StakePools (..))
import Shelley.Spec.Ledger.TxData
( DCert (..),
PoolCert (..),
PoolParams (..),
StakePools (..),
)

data POOL (crypto :: Type)

Expand Down Expand Up @@ -66,6 +87,14 @@ instance Typeable crypto => STS (POOL crypto) where

transitionRules = [poolDelegationTransition]

assertions =
[ PreCondition
"_stPools and _pParams must have the same domain"
( \(TRC (_, st, _)) ->
dom (unStakePools $ _stPools st) == dom (_pParams st)
)
]

instance NoUnexpectedThunks (PredicateFailure (POOL crypto))

instance
Expand Down
Expand Up @@ -25,7 +25,8 @@ import Cardano.Binary
)
import Cardano.Prelude (NoUnexpectedThunks (..), asks)
import Control.State.Transition
( Embed,
( Assertion (..),
Embed,
IRC (..),
InitialRule,
STS (..),
Expand All @@ -37,7 +38,7 @@ import Control.State.Transition
wrapFailed,
(?!),
)
import Data.Foldable (toList)
import Data.Foldable (foldl', toList)
import qualified Data.Map.Strict as Map
import Data.Set (Set)
import qualified Data.Set as Set
Expand Down Expand Up @@ -70,8 +71,15 @@ import Shelley.Spec.Ledger.Serialization
)
import Shelley.Spec.Ledger.Slot (SlotNo)
import Shelley.Spec.Ledger.Tx (Tx (..), TxIn, TxOut (..))
import Shelley.Spec.Ledger.TxData (TxBody (..))
import Shelley.Spec.Ledger.UTxO (UTxO (..), totalDeposits, txins, txouts, txup)
import Shelley.Spec.Ledger.TxData (TxBody (..), unWdrl)
import Shelley.Spec.Ledger.UTxO
( UTxO (..),
balance,
totalDeposits,
txins,
txouts,
txup,
)

data UTXO crypto

Expand Down Expand Up @@ -117,6 +125,22 @@ instance
transitionRules = [utxoInductive]
initialRules = [initialLedgerState]

assertions =
[ PostCondition
"UTxO must increase fee pot"
(\(TRC (_, st, _)) st' -> _fees st' >= _fees st),
PostCondition
"Deposit pot must not be negative"
(\_ st' -> _deposited st' >= 0),
let utxoBalance us = _deposited us + _fees us + balance (_utxo us)
withdrawals txb = foldl' (+) (Coin 0) $ unWdrl $ _wdrls txb
in PostCondition
"Should preserve ADA in the UTxO state"
( \(TRC (_, us, tx)) us' ->
utxoBalance us + withdrawals (_body tx) == utxoBalance us'
)
]

instance NoUnexpectedThunks (PredicateFailure (UTXO crypto))

instance
Expand Down

0 comments on commit f9e19fa

Please sign in to comment.