Skip to content

Commit

Permalink
Add Default instances for some Shelley states.
Browse files Browse the repository at this point in the history
Shelley states (most of which can be found in `module
Shelley.Spec.Ledger.LedgerState`) have now a `Default` instance, which means
that they have a default value. This default value is what it was previously
know as "empty" state. Furthermore, the STS class provides a `default`
definition for initial rules of STS whose state has a `Default` instance:

```
default initialRules :: Default (State a) => [InitialRule a]
initialRules = [pure def]
```
  • Loading branch information
nc6 authored and dnadales committed Jan 13, 2021
1 parent d104b97 commit 009b449
Show file tree
Hide file tree
Showing 31 changed files with 135 additions and 204 deletions.
1 change: 1 addition & 0 deletions semantics/executable-spec/small-steps.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ library
, cborg
, containers
, cryptonite
, data-default-class
, deepseq
, free
, mtl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleContexts #-}
Expand Down Expand Up @@ -63,6 +64,7 @@ import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.State.Strict (modify, runStateT)
import qualified Control.Monad.Trans.State.Strict as MonadState
import Data.Data (Data, Typeable)
import Data.Default.Class (Default, def)
import Data.Foldable (find, traverse_)
import Data.Functor ((<&>))
import Data.Kind (Type)
Expand Down Expand Up @@ -180,6 +182,8 @@ class

-- | Rules governing transition under this system.
initialRules :: [InitialRule a]
default initialRules :: Default (State a) => [InitialRule a]
initialRules = [pure def]

transitionRules :: [TransitionRule a]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ test-suite cardano-ledger-shelley-ma-test
"-with-rtsopts=-K4m -M250m"
build-depends:
cardano-ledger-shelley-ma,
data-default-class,
shelley-spec-ledger-test,
cardano-ledger-shelley-ma-test,
base >=4.9 && <4.15,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Shelley.Spec.Ledger.UTxO (txid)
import Test.Tasty (TestTree)
import Test.Tasty.HUnit (testCase)
import Test.Shelley.Spec.Ledger.Utils (runShelleyBase, applySTSTest)
import Shelley.Spec.Ledger.LedgerState (emptyUTxOState, emptyDPState)
import Shelley.Spec.Ledger.LedgerState ()
import Control.State.Transition.Extended (TRC (..))
import Control.Monad.Except (runExcept)
import Shelley.Spec.Ledger.Tx (hashScript, scriptWits)
Expand All @@ -29,6 +29,7 @@ import Test.Cardano.Ledger.EraBuffet
ShelleyEra,
StandardCrypto,
)
import Data.Default.Class (def)

type Allegra = AllegraEra StandardCrypto

Expand Down Expand Up @@ -73,7 +74,7 @@ testScriptPostTranslation = testCase
(S.TxIn bootstrapTxId 0)
(S.TxOut addr (Val.inject (S.Coin 1)))
env = S.LedgerEnv (SlotNo 0) 0 emptyPParams (S.AccountState (S.Coin 0) (S.Coin 0))
utxoStShelley = emptyUTxOState {S._utxo = utxo}
utxoStShelley = def {S._utxo = utxo}
utxoStAllegra = fromRight . runExcept $ translateEra @Allegra () utxoStShelley
txb =
S.TxBody
Expand All @@ -90,7 +91,7 @@ testScriptPostTranslation = testCase
txa = fromRight . runExcept $ translateEra @Allegra () txs
result = runShelleyBase $
applySTSTest @(S.LEDGER Allegra)
(TRC (env, (utxoStAllegra, emptyDPState), txa))
(TRC (env, (utxoStAllegra, def), txa))
in
case result of
Left e -> error $ show e
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ import GHC.Records
import Shelley.Spec.Ledger.API (LEDGER, LedgerEnv (..))
import Shelley.Spec.Ledger.LedgerState
( DPState (..),
UTxOState (..),
emptyDPState,
emptyUTxOState,
UTxOState (..)
)
import Shelley.Spec.Ledger.Tx (Tx (..))
import Shelley.Spec.Ledger.UTxO (UTxO)
import Test.Cardano.Ledger.EraBuffet (TestCrypto)
import Test.Shelley.Spec.Ledger.Utils (applySTSTest, runShelleyBase)
import Test.Tasty.HUnit (Assertion, (@?=))
import Data.Default.Class (def)

type MaryTest = MaryEra TestCrypto

Expand All @@ -42,10 +41,10 @@ testMaryNoDelegLEDGER ::
Assertion
testMaryNoDelegLEDGER utxo tx env (Right expectedUTxO) = do
checkTrace @(LEDGER MaryTest) runShelleyBase env $
pure (emptyUTxOState {_utxo = utxo}, emptyDPState) .- tx .-> expectedSt'
pure (def {_utxo = utxo}, def) .- tx .-> expectedSt'
where
txFee = getField @"txfee" (_body tx)
expectedSt' = (emptyUTxOState {_utxo = expectedUTxO, _fees = txFee}, emptyDPState)
expectedSt' = (def {_utxo = expectedUTxO, _fees = txFee}, def)
testMaryNoDelegLEDGER utxo tx env predicateFailure@(Left _) = do
let st = runShelleyBase $ applySTSTest @(LEDGER MaryTest) (TRC (env, (emptyUTxOState {_utxo = utxo}, emptyDPState), tx))
let st = runShelleyBase $ applySTSTest @(LEDGER MaryTest) (TRC (env, (def {_utxo = utxo}, def), tx))
(ignoreAllButUTxO st) @?= predicateFailure
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import qualified Cardano.Ledger.Crypto as CC
import Cardano.Ledger.Shelley (ShelleyEra)
import Cardano.Ledger.Val ((<->))
import qualified Data.ByteString.Short as SBS
import Data.Default.Class (def)
import qualified Data.Map.Strict as Map
import Data.Maybe (fromMaybe)
import GHC.Stack (HasCallStack)
Expand All @@ -30,8 +31,6 @@ import Shelley.Spec.Ledger.API.Types
import Shelley.Spec.Ledger.Coin (CompactForm (CompactCoin))
import Shelley.Spec.Ledger.CompactAddr (CompactAddr (UnsafeCompactAddr))
import Shelley.Spec.Ledger.EpochBoundary
import Shelley.Spec.Ledger.LedgerState
import Shelley.Spec.Ledger.Rewards
import Shelley.Spec.Ledger.STS.Chain (pparamsToChainChecksData)
import Shelley.Spec.Ledger.Slot

Expand Down Expand Up @@ -130,7 +129,7 @@ translateToShelleyLedgerState genesisShelley epochNo cvs =
esLState = ledgerState,
esPrevPp = pparams,
esPp = pparams,
esNonMyopic = emptyNonMyopic
esNonMyopic = def
}

utxoByron :: Byron.UTxO
Expand All @@ -147,12 +146,12 @@ translateToShelleyLedgerState genesisShelley epochNo cvs =
{ _utxo = utxoShelley,
_deposited = Coin 0,
_fees = Coin 0,
_ppups = emptyPPUPState
_ppups = def
},
_delegationState =
DPState
{ _dstate = emptyDState {_genDelegs = genDelegs},
_pstate = emptyPState
{ _dstate = def {_genDelegs = genDelegs},
_pstate = def
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import qualified Cardano.Ledger.Val as Val
import Control.DeepSeq (NFData)
import Control.SetAlgebra (dom, eval, setSingleton, (▷), (◁))
import Data.Aeson
import Data.Default.Class (Default, def)
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Ratio ((%))
Expand Down Expand Up @@ -230,6 +231,9 @@ instance
f <- fromCBOR
pure $ SnapShots mark set go f

instance Default (SnapShots crypto) where
def = emptySnapShots

emptySnapShot :: SnapShot crypto
emptySnapShot = SnapShot (Stake Map.empty) Map.empty Map.empty

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,12 @@ module Shelley.Spec.Ledger.LedgerState
RewardUpdate (..),
UTxOState (..),
depositPoolChange,
emptyAccount,
emptyDPState,
emptyDState,
emptyEpochState,
emptyInstantaneousRewards,
emptyLedgerState,
emptyPPUPState,
emptyPState,
emptyRewardUpdate,
emptyUTxOState,
pvCanFollow,
reapRewards,
totalInstantaneousReservesRewards,
updatePpup,

-- * state transitions
emptyDelegation,

-- * Genesis State
genesisState,

Expand Down Expand Up @@ -129,6 +117,7 @@ import Control.SetAlgebra (Bimap, biMapEmpty, dom, eval, forwards, range, (∈),
import qualified Data.ByteString.Lazy as BSL (length)
import Data.Coerce (coerce)
import Data.Constraint (Constraint)
import Data.Default.Class (Default, def)
import Data.Foldable (fold, toList)
import Data.Group (invert)
import Data.Kind (Type)
Expand Down Expand Up @@ -181,7 +170,6 @@ import Shelley.Spec.Ledger.EpochBoundary
SnapShots (..),
Stake (..),
aggregateUtxoCoinByCredential,
emptySnapShots,
)
import Shelley.Spec.Ledger.Hashing (hashAnnotated)
import Shelley.Spec.Ledger.Keys
Expand All @@ -202,7 +190,6 @@ import Shelley.Spec.Ledger.PParams
ProtVer (..),
Update (..),
emptyPPPUpdates,
emptyPParams,
)
import Shelley.Spec.Ledger.RewardProvenance (Desirability (..), RewardProvenance (..))
import Shelley.Spec.Ledger.Rewards
Expand All @@ -211,7 +198,6 @@ import Shelley.Spec.Ledger.Rewards
PerformanceEstimate (..),
applyDecay,
desirability,
emptyNonMyopic,
percentile',
reward,
)
Expand Down Expand Up @@ -452,7 +438,8 @@ instance
pure $ RewardUpdate dt (invert dr) rw (invert df) nm

emptyRewardUpdate :: RewardUpdate crypto
emptyRewardUpdate = RewardUpdate (DeltaCoin 0) (DeltaCoin 0) Map.empty (DeltaCoin 0) emptyNonMyopic
emptyRewardUpdate =
RewardUpdate (DeltaCoin 0) (DeltaCoin 0) Map.empty (DeltaCoin 0) def

data AccountState = AccountState
{ _treasury :: !Coin,
Expand Down Expand Up @@ -521,49 +508,6 @@ instance
n <- fromCBOR
pure $ EpochState a s l r p n

emptyPPUPState :: PPUPState era
emptyPPUPState = PPUPState emptyPPPUpdates emptyPPPUpdates

emptyUTxOState :: UTxOState era
emptyUTxOState = UTxOState (UTxO Map.empty) (Coin 0) (Coin 0) emptyPPUPState

emptyEpochState :: EpochState era
emptyEpochState =
EpochState emptyAccount emptySnapShots emptyLedgerState emptyPParams emptyPParams emptyNonMyopic

emptyLedgerState :: LedgerState era
emptyLedgerState =
LedgerState
emptyUTxOState
emptyDelegation

emptyAccount :: AccountState
emptyAccount = AccountState (Coin 0) (Coin 0)

emptyDelegation :: DPState crypto
emptyDelegation =
DPState emptyDState emptyPState

emptyInstantaneousRewards :: InstantaneousRewards crypto
emptyInstantaneousRewards = InstantaneousRewards Map.empty Map.empty

emptyDState :: DState crypto
emptyDState =
DState
Map.empty
Map.empty
biMapEmpty
Map.empty
(GenDelegs Map.empty)
emptyInstantaneousRewards

emptyPState :: PState crypto
emptyPState =
PState Map.empty Map.empty Map.empty

emptyDPState :: DPState crypto
emptyDPState = DPState emptyDState emptyPState

data PPUPState era = PPUPState
{ proposals :: !(ProposedPPUpdates era),
futureProposals :: !(ProposedPPUpdates era)
Expand Down Expand Up @@ -762,11 +706,11 @@ genesisState genDelegs0 utxo0 =
utxo0
(Coin 0)
(Coin 0)
emptyPPUPState
def
)
(DPState dState emptyPState)
(DPState dState def)
where
dState = emptyDState {_genDelegs = GenDelegs genDelegs0}
dState = def {_genDelegs = GenDelegs genDelegs0}

-- | Implementation of abstract transaction size
txsize :: Tx era -> Integer
Expand Down Expand Up @@ -1275,3 +1219,42 @@ returnRedeemAddrsToReserves es = es {esAccountState = acnt', esLState = ls'}
}
us' = us {_utxo = UTxO nonredeemers :: UTxO era}
ls' = ls {_utxoState = us'}

--------------------------------------------------------------------------------
-- Default instances
--------------------------------------------------------------------------------

instance Default (PPUPState era) where
def = PPUPState emptyPPPUpdates emptyPPPUpdates

instance Default (UTxOState era) where
def = UTxOState (UTxO Map.empty) (Coin 0) (Coin 0) def

instance Default (EpochState era) where
def = EpochState def def def def def def

instance Default (LedgerState era) where
def = LedgerState def def

instance Default (DPState crypto) where
def = DPState def def

instance Default (InstantaneousRewards crypto) where
def = InstantaneousRewards Map.empty Map.empty

instance Default (DState crypto) where
def =
DState
Map.empty
Map.empty
biMapEmpty
Map.empty
(GenDelegs Map.empty)
def

instance Default (PState crypto) where
def =
PState Map.empty Map.empty Map.empty

instance Default AccountState where
def = AccountState (Coin 0) (Coin 0)
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import Control.DeepSeq (NFData)
import Control.Monad (unless)
import Data.Aeson (FromJSON (..), ToJSON (..), (.!=), (.:), (.:?), (.=))
import qualified Data.Aeson as Aeson
import Data.Default.Class (Default, def)
import Data.Foldable (fold)
import Data.Functor.Identity (Identity)
import Data.List (nub)
Expand Down Expand Up @@ -300,6 +301,9 @@ instance FromJSON (PParams era) where
<*> obj .:? "minUTxOValue" .!= mempty
<*> obj .:? "minPoolCost" .!= mempty

instance Default (PParams era) where
def = emptyPParams

-- | Returns a basic "empty" `PParams` structure with all zero values.
emptyPParams :: PParams era
emptyPParams =
Expand Down
Loading

0 comments on commit 009b449

Please sign in to comment.