From 0ef8381bacb2cffa78364045b34844bcbfb399fd Mon Sep 17 00:00:00 2001 From: Philipp Kant Date: Tue, 14 Jul 2020 16:47:38 +0200 Subject: [PATCH 1/8] Start optimising the memory footprint of the UTxO. This changes the memory representation of TxIn and TxOut, to reduce overhead. In particular, we use unpacked Word64 for coin values and txout indices, and use the serialisation of addresses as ShortByteString. This does not yet change the representation of TxId. The changes are nearly opaque, in that we provide view patterns that takes the place of the old constructors. We do need to add a Crypto constraint wherever we use a TxOut, however, since the view pattern relies on serialisation/deserialisation. --- .../src/Shelley/Spec/Ledger/API/Wallet.hs | 1 + .../src/Shelley/Spec/Ledger/EpochBoundary.hs | 2 +- .../src/Shelley/Spec/Ledger/LedgerState.hs | 2 +- .../src/Shelley/Spec/Ledger/STS/Chain.hs | 4 +- .../src/Shelley/Spec/Ledger/STS/Epoch.hs | 12 ++-- .../src/Shelley/Spec/Ledger/STS/Snap.hs | 5 +- .../src/Shelley/Spec/Ledger/TxData.hs | 55 ++++++++++++++++--- .../src/Shelley/Spec/Ledger/UTxO.hs | 3 +- 8 files changed, 63 insertions(+), 21 deletions(-) diff --git a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/API/Wallet.hs b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/API/Wallet.hs index c6a2e20087e..5ebefad5760 100644 --- a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/API/Wallet.hs +++ b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/API/Wallet.hs @@ -107,6 +107,7 @@ getUTxO = _utxo . _utxoState . esLState . nesEs -- | Get the UTxO filtered by address. getFilteredUTxO :: + Crypto crypto => ShelleyState crypto -> Set (Addr crypto) -> UTxO crypto diff --git a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/EpochBoundary.hs b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/EpochBoundary.hs index 801a20e6989..637fb4fde27 100644 --- a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/EpochBoundary.hs +++ b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/EpochBoundary.hs @@ -80,7 +80,7 @@ getStakeHK :: Addr crypto -> Maybe (Credential 'Staking crypto) getStakeHK (Addr _ _ (StakeRefBase hk)) = Just hk getStakeHK _ = Nothing -aggregateOuts :: UTxO crypto -> Map (Addr crypto) Coin +aggregateOuts :: Crypto crypto => UTxO crypto -> Map (Addr crypto) Coin aggregateOuts (UTxO u) = Map.fromListWith (+) (map (\(_, TxOut a c) -> (a, c)) $ Map.toList u) diff --git a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/LedgerState.hs b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/LedgerState.hs index 59b3682f3e2..7207cdd2b87 100644 --- a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/LedgerState.hs +++ b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/LedgerState.hs @@ -924,7 +924,7 @@ reapRewards dStateRewards withdrawals = -- | Stake distribution stakeDistr :: - forall crypto. + forall crypto. Crypto crypto => UTxO crypto -> DState crypto -> PState crypto -> diff --git a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/STS/Chain.hs b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/STS/Chain.hs index 08b9c3eb1fc..36b2ebcc71a 100644 --- a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/STS/Chain.hs +++ b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/STS/Chain.hs @@ -363,7 +363,7 @@ data AdaPots = AdaPots deriving (Show, Eq) -- | Calculate the total ada pots in the chain state -totalAdaPots :: ChainState crypto -> AdaPots +totalAdaPots :: Crypto crypto => ChainState crypto -> AdaPots totalAdaPots (ChainState nes _ _ _ _ _ _) = AdaPots { treasuryAdaPot = treasury_, @@ -381,7 +381,7 @@ totalAdaPots (ChainState nes _ _ _ _ _ _) = circulation = balance u -- | Calculate the total ada in the chain state -totalAda :: ChainState crypto -> Coin +totalAda :: Crypto crypto => ChainState crypto -> Coin totalAda cs = treasuryAdaPot + reservesAdaPot + rewardsAdaPot + utxoAdaPot + depositsAdaPot + feesAdaPot where diff --git a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/STS/Epoch.hs b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/STS/Epoch.hs index 9aeb4979536..414c7a7577e 100644 --- a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/STS/Epoch.hs +++ b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/STS/Epoch.hs @@ -21,6 +21,7 @@ import qualified Data.Map.Strict as Map import Data.Typeable (Typeable) import GHC.Generics (Generic) import Shelley.Spec.Ledger.BaseTypes (Globals (..), ShelleyBase) +import Shelley.Spec.Ledger.Crypto (Crypto) import Shelley.Spec.Ledger.EpochBoundary (emptySnapShots) import Shelley.Spec.Ledger.LedgerState ( EpochState, @@ -49,7 +50,7 @@ import Shelley.Spec.Ledger.Slot (EpochNo) data EPOCH crypto -instance Typeable crypto => STS (EPOCH crypto) where +instance (Crypto crypto, Typeable crypto) => STS (EPOCH crypto) where type State (EPOCH crypto) = EpochState crypto type Signal (EPOCH crypto) = EpochNo type Environment (EPOCH crypto) = () @@ -93,7 +94,8 @@ votedValuePParams (ProposedPPUpdates ppup) pps quorumN = 1 -> (Just . updatePParams pps . fst . head . Map.toList) consensus _ -> Nothing -epochTransition :: forall crypto. Typeable crypto => TransitionRule (EPOCH crypto) +epochTransition :: forall crypto. Crypto crypto => + TransitionRule (EPOCH crypto) epochTransition = do TRC ( _, @@ -139,11 +141,11 @@ epochTransition = do pp' nm -instance Typeable crypto => Embed (SNAP crypto) (EPOCH crypto) where +instance (Crypto crypto, Typeable crypto) => Embed (SNAP crypto) (EPOCH crypto) where wrapFailed = SnapFailure -instance Typeable crypto => Embed (POOLREAP crypto) (EPOCH crypto) where +instance (Crypto crypto, Typeable crypto) => Embed (POOLREAP crypto) (EPOCH crypto) where wrapFailed = PoolReapFailure -instance Typeable crypto => Embed (NEWPP crypto) (EPOCH crypto) where +instance (Crypto crypto, Typeable crypto) => Embed (NEWPP crypto) (EPOCH crypto) where wrapFailed = NewPpFailure diff --git a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/STS/Snap.hs b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/STS/Snap.hs index 13fc2a35a47..14d45d6de00 100644 --- a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/STS/Snap.hs +++ b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/STS/Snap.hs @@ -20,6 +20,7 @@ import Control.State.Transition import Data.Typeable (Typeable) import GHC.Generics (Generic) import Shelley.Spec.Ledger.BaseTypes +import Shelley.Spec.Ledger.Crypto (Crypto) import Shelley.Spec.Ledger.EpochBoundary import Shelley.Spec.Ledger.LedgerState ( DPState (..), @@ -30,7 +31,7 @@ import Shelley.Spec.Ledger.LedgerState data SNAP crypto -instance Typeable crypto => STS (SNAP crypto) where +instance (Crypto crypto, Typeable crypto) => STS (SNAP crypto) where type State (SNAP crypto) = SnapShots crypto type Signal (SNAP crypto) = () type Environment (SNAP crypto) = LedgerState crypto @@ -43,7 +44,7 @@ instance Typeable crypto => STS (SNAP crypto) where instance NoUnexpectedThunks (PredicateFailure (SNAP crypto)) -snapTransition :: TransitionRule (SNAP crypto) +snapTransition :: Crypto crypto => TransitionRule (SNAP crypto) snapTransition = do TRC (lstate, s, ()) <- judgmentContext diff --git a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/TxData.hs b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/TxData.hs index d16c2a6216e..0f81f3e59a9 100644 --- a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/TxData.hs +++ b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/TxData.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE ViewPatterns #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE DeriveAnyClass #-} {-# LANGUAGE DeriveGeneric #-} @@ -46,8 +47,8 @@ module Shelley.Spec.Ledger.TxData extraSize ), TxId (..), - TxIn (..), - TxOut (..), + TxIn (TxIn), pattern TxInCompact, + TxOut (TxOut), Url, Wdrl (..), WitVKey (WitVKey, wvkBytes), @@ -101,6 +102,7 @@ import qualified Data.ByteString as BS import qualified Data.ByteString.Base16 as Base16 import qualified Data.ByteString.Char8 as Char8 import qualified Data.ByteString.Lazy as BSL +import qualified Data.ByteString.Short as BSS import Data.Foldable (fold) import Data.IP (IPv4, IPv6) import Data.Int (Int64) @@ -118,7 +120,8 @@ import Data.Word (Word8) import GHC.Generics (Generic) import Numeric.Natural (Natural) import Quiet -import Shelley.Spec.Ledger.Address (Addr (..), RewardAcnt (..)) +import Shelley.Spec.Ledger.Address (deserialiseAddr, Addr (..), RewardAcnt (..), + serialiseAddr) import Shelley.Spec.Ledger.BaseTypes ( DnsName, Port, @@ -129,7 +132,7 @@ import Shelley.Spec.Ledger.BaseTypes maybeToStrictMaybe, strictMaybeToMaybe, ) -import Shelley.Spec.Ledger.Coin (Coin (..)) +import Shelley.Spec.Ledger.Coin (word64ToCoin, Coin (..)) import Shelley.Spec.Ledger.Core (Relation (..)) import Shelley.Spec.Ledger.Credential ( Credential (..), @@ -375,17 +378,51 @@ deriving newtype instance Crypto crypto => ToCBOR (TxId crypto) deriving newtype instance Crypto crypto => FromCBOR (TxId crypto) -- | The input of a UTxO. -data TxIn crypto - = TxIn !(TxId crypto) !Natural -- TODO use our own Natural type - deriving (Show, Eq, Generic, Ord, NFData) +data TxIn crypto = TxInCompact !(TxId crypto) {-# UNPACK #-} !Word64 + deriving (Show, Eq, Generic, Ord, NFData) +-- TODO: We will also want to have the TxId be compact, but the representation +-- depends on the crypto. + +pattern TxIn :: Crypto crypto => + TxId crypto -> + Natural -> -- TODO We might want to change this to Word64 generally + TxIn crypto +pattern TxIn addr index <- + TxInCompact addr (fromIntegral -> index) + where + TxIn addr index = + TxInCompact addr (fromIntegral index) + +{-# COMPLETE TxIn #-} instance NoUnexpectedThunks (TxIn crypto) -- | The output of a UTxO. -data TxOut crypto - = TxOut !(Addr crypto) !Coin +data TxOut crypto = + TxOutCompact + {-# UNPACK #-} !BSS.ShortByteString + {-# UNPACK #-} !Word64 deriving (Show, Eq, Generic, Ord, NFData) +pattern TxOut :: Crypto crypto => + Addr crypto -> + Coin -> + TxOut crypto +pattern TxOut addr coin <- (viewCompactTxOut -> (addr, coin)) + where + TxOut addr (Coin coin) = + TxOutCompact (BSS.toShort $ serialiseAddr addr) (fromIntegral coin) + +{-# COMPLETE TxOut #-} + +viewCompactTxOut :: forall crypto . Crypto crypto => TxOut crypto -> (Addr crypto, Coin) +viewCompactTxOut (TxOutCompact bs c) = (addr, coin) + where + addr = case deserialiseAddr (BSS.fromShort bs) of + Nothing -> panic "viewCompactTxOut: impossible" + Just (a :: Addr crypto) -> a + coin = word64ToCoin c + instance NoUnexpectedThunks (TxOut crypto) data DelegCert crypto diff --git a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/UTxO.hs b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/UTxO.hs index 8dca38469aa..5af292aa6aa 100644 --- a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/UTxO.hs +++ b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/UTxO.hs @@ -225,7 +225,7 @@ makeWitnessesFromScriptKeys txbodyHash hashKeyMap scriptHashes = in makeWitnessesVKey txbodyHash (Map.elems witKeys) -- | Determine the total balance contained in the UTxO. -balance :: UTxO crypto -> Coin +balance :: Crypto crypto => UTxO crypto -> Coin balance (UTxO utxo) = foldr addCoins 0 utxo where addCoins (TxOut _ a) b = a + b @@ -300,6 +300,7 @@ scriptsNeeded u tx = -- | Compute the subset of inputs of the set 'txInps' for which each input is -- locked by a script in the UTxO 'u'. txinsScript :: + Crypto crypto => Set (TxIn crypto) -> UTxO crypto -> Set (TxIn crypto) From 9dbe100efd8cc413155badb04ee0d059cc575834 Mon Sep 17 00:00:00 2001 From: Philipp Kant Date: Tue, 14 Jul 2020 20:03:14 +0200 Subject: [PATCH 2/8] Run ormolu --- .../src/Shelley/Spec/Ledger/LedgerState.hs | 7 +-- .../src/Shelley/Spec/Ledger/STS/Chain.hs | 14 ++--- .../src/Shelley/Spec/Ledger/STS/Epoch.hs | 10 ++-- .../src/Shelley/Spec/Ledger/TxData.hs | 52 +++++++++++-------- .../src/Shelley/Spec/Ledger/UTxO.hs | 1 - 5 files changed, 48 insertions(+), 36 deletions(-) diff --git a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/LedgerState.hs b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/LedgerState.hs index 7207cdd2b87..8fd70a67f58 100644 --- a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/LedgerState.hs +++ b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/LedgerState.hs @@ -184,12 +184,12 @@ import Shelley.Spec.Ledger.Rewards ) import Shelley.Spec.Ledger.Serialization (mapFromCBOR, mapToCBOR) import Shelley.Spec.Ledger.Slot - ( Duration (..), + ( (+*), + Duration (..), EpochNo (..), SlotNo (..), epochInfoFirst, epochInfoSize, - (+*), ) import Shelley.Spec.Ledger.Tx ( Tx (..), @@ -924,7 +924,8 @@ reapRewards dStateRewards withdrawals = -- | Stake distribution stakeDistr :: - forall crypto. Crypto crypto => + forall crypto. + Crypto crypto => UTxO crypto -> DState crypto -> PState crypto -> diff --git a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/STS/Chain.hs b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/STS/Chain.hs index 36b2ebcc71a..2cc71141399 100644 --- a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/STS/Chain.hs +++ b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/STS/Chain.hs @@ -90,12 +90,12 @@ import Shelley.Spec.Ledger.LedgerState OBftSlot, PState (..), UTxOState (..), + _genDelegs, emptyDState, emptyPPUPState, emptyPState, getGKeys, updateNES, - _genDelegs, ) import Shelley.Spec.Ledger.OCert (OCertSignable) import Shelley.Spec.Ledger.PParams @@ -231,12 +231,12 @@ chainChecks :: m () chainChecks maxpv pp bh = do unless (m <= maxpv) $ throwError (ObsoleteNodeCHAIN m maxpv) - unless (fromIntegral (bHeaderSize bh) <= _maxBHSize pp) $ - throwError $ - HeaderSizeTooLargeCHAIN (fromIntegral $ bHeaderSize bh) (_maxBHSize pp) - unless (hBbsize (bhbody bh) <= _maxBBSize pp) $ - throwError $ - BlockSizeTooLargeCHAIN (hBbsize (bhbody bh)) (_maxBBSize pp) + unless (fromIntegral (bHeaderSize bh) <= _maxBHSize pp) + $ throwError + $ HeaderSizeTooLargeCHAIN (fromIntegral $ bHeaderSize bh) (_maxBHSize pp) + unless (hBbsize (bhbody bh) <= _maxBBSize pp) + $ throwError + $ BlockSizeTooLargeCHAIN (hBbsize (bhbody bh)) (_maxBBSize pp) where (ProtVer m _) = _protocolVersion pp diff --git a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/STS/Epoch.hs b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/STS/Epoch.hs index 414c7a7577e..50ce86b415e 100644 --- a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/STS/Epoch.hs +++ b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/STS/Epoch.hs @@ -27,6 +27,9 @@ import Shelley.Spec.Ledger.LedgerState ( EpochState, PPUPState (..), PState (..), + _delegationState, + _ppups, + _utxoState, emptyAccount, emptyLedgerState, esAccountState, @@ -35,9 +38,6 @@ import Shelley.Spec.Ledger.LedgerState esPp, esPrevPp, esSnapshots, - _delegationState, - _ppups, - _utxoState, pattern DPState, pattern EpochState, ) @@ -94,7 +94,9 @@ votedValuePParams (ProposedPPUpdates ppup) pps quorumN = 1 -> (Just . updatePParams pps . fst . head . Map.toList) consensus _ -> Nothing -epochTransition :: forall crypto. Crypto crypto => +epochTransition :: + forall crypto. + Crypto crypto => TransitionRule (EPOCH crypto) epochTransition = do TRC diff --git a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/TxData.hs b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/TxData.hs index 0f81f3e59a9..5aea802973b 100644 --- a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/TxData.hs +++ b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/TxData.hs @@ -1,4 +1,3 @@ -{-# LANGUAGE ViewPatterns #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE DeriveAnyClass #-} {-# LANGUAGE DeriveGeneric #-} @@ -17,6 +16,7 @@ {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TypeApplications #-} {-# LANGUAGE TypeFamilies #-} +{-# LANGUAGE ViewPatterns #-} module Shelley.Spec.Ledger.TxData ( DCert (..), @@ -47,7 +47,8 @@ module Shelley.Spec.Ledger.TxData extraSize ), TxId (..), - TxIn (TxIn), pattern TxInCompact, + TxIn (TxIn), + pattern TxInCompact, TxOut (TxOut), Url, Wdrl (..), @@ -94,7 +95,7 @@ import Cardano.Prelude ) import Control.Iterate.SetAlgebra (BaseRep (MapR), Embed (..), Exp (Base), HasExp (toExp)) import Control.Monad (unless) -import Data.Aeson (FromJSON (..), ToJSON (..), (.!=), (.:), (.:?), (.=)) +import Data.Aeson ((.!=), (.:), (.:?), (.=), FromJSON (..), ToJSON (..)) import qualified Data.Aeson as Aeson import Data.Aeson.Types (explicitParseField) import Data.ByteString (ByteString) @@ -120,8 +121,12 @@ import Data.Word (Word8) import GHC.Generics (Generic) import Numeric.Natural (Natural) import Quiet -import Shelley.Spec.Ledger.Address (deserialiseAddr, Addr (..), RewardAcnt (..), - serialiseAddr) +import Shelley.Spec.Ledger.Address + ( Addr (..), + RewardAcnt (..), + deserialiseAddr, + serialiseAddr, + ) import Shelley.Spec.Ledger.BaseTypes ( DnsName, Port, @@ -132,7 +137,7 @@ import Shelley.Spec.Ledger.BaseTypes maybeToStrictMaybe, strictMaybeToMaybe, ) -import Shelley.Spec.Ledger.Coin (word64ToCoin, Coin (..)) +import Shelley.Spec.Ledger.Coin (Coin (..), word64ToCoin) import Shelley.Spec.Ledger.Core (Relation (..)) import Shelley.Spec.Ledger.Credential ( Credential (..), @@ -379,11 +384,13 @@ deriving newtype instance Crypto crypto => FromCBOR (TxId crypto) -- | The input of a UTxO. data TxIn crypto = TxInCompact !(TxId crypto) {-# UNPACK #-} !Word64 - deriving (Show, Eq, Generic, Ord, NFData) + deriving (Show, Eq, Generic, Ord, NFData) + -- TODO: We will also want to have the TxId be compact, but the representation -- depends on the crypto. -pattern TxIn :: Crypto crypto => +pattern TxIn :: + Crypto crypto => TxId crypto -> Natural -> -- TODO We might want to change this to Word64 generally TxIn crypto @@ -398,24 +405,26 @@ pattern TxIn addr index <- instance NoUnexpectedThunks (TxIn crypto) -- | The output of a UTxO. -data TxOut crypto = - TxOutCompact - {-# UNPACK #-} !BSS.ShortByteString - {-# UNPACK #-} !Word64 +data TxOut crypto + = TxOutCompact + {-# UNPACK #-} !BSS.ShortByteString + {-# UNPACK #-} !Word64 deriving (Show, Eq, Generic, Ord, NFData) -pattern TxOut :: Crypto crypto => +pattern TxOut :: + Crypto crypto => Addr crypto -> Coin -> TxOut crypto -pattern TxOut addr coin <- (viewCompactTxOut -> (addr, coin)) +pattern TxOut addr coin <- + (viewCompactTxOut -> (addr, coin)) where TxOut addr (Coin coin) = TxOutCompact (BSS.toShort $ serialiseAddr addr) (fromIntegral coin) {-# COMPLETE TxOut #-} -viewCompactTxOut :: forall crypto . Crypto crypto => TxOut crypto -> (Addr crypto, Coin) +viewCompactTxOut :: forall crypto. Crypto crypto => TxOut crypto -> (Addr crypto, Coin) viewCompactTxOut (TxOutCompact bs c) = (addr, coin) where addr = case deserialiseAddr (BSS.fromShort bs) of @@ -772,10 +781,10 @@ instance FromCBOR (Annotator (WitVKey crypto kr)) where fromCBOR = - annotatorSlice $ - decodeRecordNamed "WitVKey" (const 2) $ - fmap pure $ - mkWitVKey <$> fromCBOR <*> decodeSignedDSIGN + annotatorSlice + $ decodeRecordNamed "WitVKey" (const 2) + $ fmap pure + $ mkWitVKey <$> fromCBOR <*> decodeSignedDSIGN where mkWitVKey k sig = WitVKey' k sig (asWitness $ hashKey k) @@ -827,8 +836,9 @@ instance unless (null missingFields) (fail $ "missing required transaction component(s): " <> show missingFields) - pure $ - Annotator $ \fullbytes bytes -> + pure + $ Annotator + $ \fullbytes bytes -> (foldr ($) basebody (flip runAnnotator fullbytes . snd <$> mapParts)) {bodyBytes = bytes} where f :: diff --git a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/UTxO.hs b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/UTxO.hs index 5af292aa6aa..78f0b38d8cb 100644 --- a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/UTxO.hs +++ b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/UTxO.hs @@ -292,7 +292,6 @@ scriptsNeeded u tx = where unTxOut (TxOut a _) = a withdrawals = unWdrl $ _wdrls $ _body tx - UTxO u'' = (txinsScript (txins $ _body tx) u) ◁ u -- u'' = Map.restrictKeys v (txinsScript (txins $ _body tx) u) TODO certificates = (toList . _certs . _body) tx From bb1a72795c21aec48b113f064f4f9b3b41917e38 Mon Sep 17 00:00:00 2001 From: Philipp Kant Date: Tue, 14 Jul 2020 20:12:05 +0200 Subject: [PATCH 3/8] Adjusting constraints in tests, to make them compile --- .../Test/Shelley/Spec/Ledger/Generator/Core.hs | 16 ++++++++-------- .../Test/Shelley/Spec/Ledger/Generator/Utxo.hs | 2 +- .../Spec/Ledger/NonTraceProperties/Generator.hs | 6 +++--- .../Spec/Ledger/NonTraceProperties/Mutator.hs | 8 ++++---- .../Spec/Ledger/SerializationProperties.hs | 2 +- .../test/Test/Shelley/Spec/Ledger/Shrinkers.hs | 4 ++-- 6 files changed, 19 insertions(+), 19 deletions(-) diff --git a/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/Generator/Core.hs b/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/Generator/Core.hs index c6413f8b567..027f56e062b 100644 --- a/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/Generator/Core.hs +++ b/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/Generator/Core.hs @@ -60,7 +60,7 @@ import Control.Monad (replicateM) import Control.Monad.Trans.Reader (asks) import Data.Coerce (coerce) import Data.List (foldl') -import qualified Data.List as List (find, findIndex, (\\)) +import qualified Data.List as List ((\\), find, findIndex) import Data.Map.Strict (Map) import qualified Data.Map.Strict as Map import Data.Ratio ((%)) @@ -110,8 +110,6 @@ import Shelley.Spec.Ledger.Keys ) import Shelley.Spec.Ledger.LedgerState ( AccountState (..), - depositPoolChange, - reapRewards, _delegationState, _deposited, _dstate, @@ -119,6 +117,8 @@ import Shelley.Spec.Ledger.LedgerState _rewards, _utxo, _utxoState, + depositPoolChange, + reapRewards, ) import Shelley.Spec.Ledger.OCert (KESPeriod (..), OCertSignable (..), pattern OCert) import Shelley.Spec.Ledger.PParams @@ -132,11 +132,11 @@ import Shelley.Spec.Ledger.Scripts pattern RequireSignature, ) import Shelley.Spec.Ledger.Slot - ( BlockNo (..), + ( (*-), + BlockNo (..), Duration (..), SlotNo (..), epochInfoFirst, - (*-), ) import Shelley.Spec.Ledger.Tx ( hashScript, @@ -147,9 +147,9 @@ import Shelley.Spec.Ledger.Tx ) import qualified Shelley.Spec.Ledger.Tx as Ledger import Shelley.Spec.Ledger.TxData - ( unWdrl, - _txfee, + ( _txfee, _wdrls, + unWdrl, pattern Wdrl, ) import Shelley.Spec.Ledger.UTxO @@ -448,7 +448,7 @@ pickStakeKey keys = vKey . snd <$> QC.elements keys -- Note: we need to keep the initial utxo coin sizes large enough so that -- when we simulate sequences of transactions, we have enough funds available -- to include certificates that require deposits. -genTxOut :: HasCallStack => Constants -> [Addr h] -> Gen [TxOut h] +genTxOut :: (HasCallStack, HashAlgorithm h) => Constants -> [Addr h] -> Gen [TxOut h] genTxOut Constants {maxGenesisOutputVal, minGenesisOutputVal} addrs = do ys <- genCoinList minGenesisOutputVal maxGenesisOutputVal (length addrs) (length addrs) return (uncurry TxOut <$> zip addrs ys) diff --git a/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/Generator/Utxo.hs b/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/Generator/Utxo.hs index 904f5d47f0a..fdb0ef5169d 100644 --- a/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/Generator/Utxo.hs +++ b/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/Generator/Utxo.hs @@ -353,7 +353,7 @@ genTxBody inputs outputs certs wdrls update fee slotWithTTL = do -- The idea is to have an specified spending balance and fees that must be paid -- by the selected addresses. calcOutputsFromBalance :: - HasCallStack => + (HasCallStack, HashAlgorithm h) => Coin -> [Addr h] -> Coin -> diff --git a/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/NonTraceProperties/Generator.hs b/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/NonTraceProperties/Generator.hs index bffc74d70cc..877b28315dc 100644 --- a/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/NonTraceProperties/Generator.hs +++ b/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/NonTraceProperties/Generator.hs @@ -47,13 +47,13 @@ import Shelley.Spec.Ledger.Credential (pattern KeyHashObj, pattern StakeRefBase) import Shelley.Spec.Ledger.Keys (KeyRole (..), hashKey, vKey) import Shelley.Spec.Ledger.LedgerState ( AccountState (..), - genesisState, _delegationState, _dstate, _genDelegs, _stkCreds, _utxo, _utxoState, + genesisState, ) import Shelley.Spec.Ledger.PParams (PParams, emptyPParams) import Shelley.Spec.Ledger.STS.Delegs @@ -175,7 +175,7 @@ genCoinList minCoin maxCoin lower upper = do -- | Generator for a list of 'TxOut' where for each 'Addr' of 'addrs' one Coin -- value is generated. -genTxOut :: [Addr h] -> Gen [TxOut h] +genTxOut :: HashAlgorithm h => [Addr h] -> Gen [TxOut h] genTxOut addrs = do ys <- genCoinList 100 10000 (length addrs) (length addrs) return (uncurry TxOut <$> zip addrs ys) @@ -322,7 +322,7 @@ findStakeKeyPair (KeyHashObj hk) keyList = findStakeKeyPair _ _ = undefined -- TODO treat script case -- | Returns the hashed 'addr' part of a 'TxOut'. -getTxOutAddr :: TxOut h -> Addr h +getTxOutAddr :: HashAlgorithm h => TxOut h -> Addr h getTxOutAddr (TxOut addr _) = addr -- | Generator for arbitrary valid ledger state, discarding any generated diff --git a/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/NonTraceProperties/Mutator.hs b/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/NonTraceProperties/Mutator.hs index 371df5d1cad..47c9c5bbfd7 100644 --- a/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/NonTraceProperties/Mutator.hs +++ b/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/NonTraceProperties/Mutator.hs @@ -120,7 +120,7 @@ mutateTxBody tx = do SNothing -- | Mutator for a list of 'TxIn'. -mutateInputs :: [TxIn h] -> Gen [TxIn h] +mutateInputs :: HashAlgorithm h => [TxIn h] -> Gen [TxIn h] mutateInputs [] = pure [] mutateInputs (txin : txins) = do mtxin <- mutateInput txin @@ -130,13 +130,13 @@ mutateInputs (txin : txins) = do -- | Mutator for a single 'TxIn', which mutates the index of the output to -- spend. -mutateInput :: TxIn h -> Gen (TxIn h) +mutateInput :: HashAlgorithm h => TxIn h -> Gen (TxIn h) mutateInput (TxIn idx index) = do index' <- mutateNat 0 100 index pure $ TxIn idx index' -- | Mutator for a list of 'TxOut'. -mutateOutputs :: StrictSeq (TxOut h) -> Gen (StrictSeq (TxOut h)) +mutateOutputs :: HashAlgorithm h => StrictSeq (TxOut h) -> Gen (StrictSeq (TxOut h)) mutateOutputs StrictSeq.Empty = pure StrictSeq.Empty mutateOutputs (txout :<| txouts) = do mtxout <- mutateOutput txout @@ -146,7 +146,7 @@ mutateOutputs (txout :<| txouts) = do -- | Mutator for a single 'TxOut' which mutates the associated 'Coin' value of -- the output. -mutateOutput :: TxOut h -> Gen (TxOut h) +mutateOutput :: HashAlgorithm h => TxOut h -> Gen (TxOut h) mutateOutput (TxOut addr c) = do c' <- mutateCoin 0 100 c pure $ TxOut addr c' diff --git a/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/SerializationProperties.hs b/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/SerializationProperties.hs index b9348355cd0..3a13107b498 100644 --- a/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/SerializationProperties.hs +++ b/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/SerializationProperties.hs @@ -302,7 +302,7 @@ instance Crypto c => Arbitrary (TxIn c) where <$> (TxId <$> genHash (Proxy @c)) <*> arbitrary -instance HashAlgorithm h => Arbitrary (Mock.TxOut h) where +instance Arbitrary (Mock.TxOut h) where arbitrary = genericArbitraryU shrink = genericShrink diff --git a/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/Shrinkers.hs b/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/Shrinkers.hs index febb4af5558..c29cd4b4e14 100644 --- a/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/Shrinkers.hs +++ b/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/Shrinkers.hs @@ -53,13 +53,13 @@ shrinkTxBody (TxBody is os cs ws tf tl tu md) = -- [ TxBody is os cs ws tf tl tu' | tu' <- shrinkUpdate tu ] outBalance = outputBalance os -outputBalance :: StrictSeq (TxOut crypto) -> Coin +outputBalance :: Crypto crypto => StrictSeq (TxOut crypto) -> Coin outputBalance = foldl' (\v (TxOut _ c) -> v + c) (Coin 0) shrinkTxIn :: TxIn crypto -> [TxIn crypto] shrinkTxIn = const [] -shrinkTxOut :: TxOut crypto -> [TxOut crypto] +shrinkTxOut :: Crypto crypto => TxOut crypto -> [TxOut crypto] shrinkTxOut (TxOut addr coin) = TxOut addr <$> shrinkCoin coin From 431031f61e09c0e5a9bee9bc03e012f12371fe28 Mon Sep 17 00:00:00 2001 From: Philipp Kant Date: Tue, 14 Jul 2020 20:33:07 +0200 Subject: [PATCH 4/8] Run ormolu --- .../executable-spec/src/Shelley/Spec/Ledger/API/Wallet.hs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/API/Wallet.hs b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/API/Wallet.hs index 5ebefad5760..6c1e7f1f421 100644 --- a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/API/Wallet.hs +++ b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/API/Wallet.hs @@ -30,15 +30,15 @@ import Shelley.Spec.Ledger.Delegation.Certificates (unPoolDistr) import Shelley.Spec.Ledger.EpochBoundary (SnapShot (..), Stake (..), poolStake) import Shelley.Spec.Ledger.Keys (KeyHash, KeyRole (..), SignKeyVRF) import Shelley.Spec.Ledger.LedgerState - ( esLState, + ( _utxo, + _utxoState, + esLState, esNonMyopic, esPp, nesEL, nesEs, nesOsched, nesPd, - _utxo, - _utxoState, ) import Shelley.Spec.Ledger.Rewards ( NonMyopic (..), From eeca97096e42c7ba347369ec442040e117ea9912 Mon Sep 17 00:00:00 2001 From: Philipp Kant Date: Tue, 14 Jul 2020 20:57:44 +0200 Subject: [PATCH 5/8] Running ormolu again --- .../src/Shelley/Spec/Ledger/API/Wallet.hs | 6 +++--- .../src/Shelley/Spec/Ledger/LedgerState.hs | 4 ++-- .../src/Shelley/Spec/Ledger/STS/Chain.hs | 14 +++++++------- .../src/Shelley/Spec/Ledger/STS/Epoch.hs | 6 +++--- .../src/Shelley/Spec/Ledger/TxData.hs | 18 +++++++++--------- .../Test/Shelley/Spec/Ledger/Generator/Core.hs | 14 +++++++------- .../Ledger/NonTraceProperties/Generator.hs | 2 +- 7 files changed, 32 insertions(+), 32 deletions(-) diff --git a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/API/Wallet.hs b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/API/Wallet.hs index 6c1e7f1f421..5ebefad5760 100644 --- a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/API/Wallet.hs +++ b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/API/Wallet.hs @@ -30,15 +30,15 @@ import Shelley.Spec.Ledger.Delegation.Certificates (unPoolDistr) import Shelley.Spec.Ledger.EpochBoundary (SnapShot (..), Stake (..), poolStake) import Shelley.Spec.Ledger.Keys (KeyHash, KeyRole (..), SignKeyVRF) import Shelley.Spec.Ledger.LedgerState - ( _utxo, - _utxoState, - esLState, + ( esLState, esNonMyopic, esPp, nesEL, nesEs, nesOsched, nesPd, + _utxo, + _utxoState, ) import Shelley.Spec.Ledger.Rewards ( NonMyopic (..), diff --git a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/LedgerState.hs b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/LedgerState.hs index 8fd70a67f58..5e4b932f605 100644 --- a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/LedgerState.hs +++ b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/LedgerState.hs @@ -184,12 +184,12 @@ import Shelley.Spec.Ledger.Rewards ) import Shelley.Spec.Ledger.Serialization (mapFromCBOR, mapToCBOR) import Shelley.Spec.Ledger.Slot - ( (+*), - Duration (..), + ( Duration (..), EpochNo (..), SlotNo (..), epochInfoFirst, epochInfoSize, + (+*), ) import Shelley.Spec.Ledger.Tx ( Tx (..), diff --git a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/STS/Chain.hs b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/STS/Chain.hs index 2cc71141399..36b2ebcc71a 100644 --- a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/STS/Chain.hs +++ b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/STS/Chain.hs @@ -90,12 +90,12 @@ import Shelley.Spec.Ledger.LedgerState OBftSlot, PState (..), UTxOState (..), - _genDelegs, emptyDState, emptyPPUPState, emptyPState, getGKeys, updateNES, + _genDelegs, ) import Shelley.Spec.Ledger.OCert (OCertSignable) import Shelley.Spec.Ledger.PParams @@ -231,12 +231,12 @@ chainChecks :: m () chainChecks maxpv pp bh = do unless (m <= maxpv) $ throwError (ObsoleteNodeCHAIN m maxpv) - unless (fromIntegral (bHeaderSize bh) <= _maxBHSize pp) - $ throwError - $ HeaderSizeTooLargeCHAIN (fromIntegral $ bHeaderSize bh) (_maxBHSize pp) - unless (hBbsize (bhbody bh) <= _maxBBSize pp) - $ throwError - $ BlockSizeTooLargeCHAIN (hBbsize (bhbody bh)) (_maxBBSize pp) + unless (fromIntegral (bHeaderSize bh) <= _maxBHSize pp) $ + throwError $ + HeaderSizeTooLargeCHAIN (fromIntegral $ bHeaderSize bh) (_maxBHSize pp) + unless (hBbsize (bhbody bh) <= _maxBBSize pp) $ + throwError $ + BlockSizeTooLargeCHAIN (hBbsize (bhbody bh)) (_maxBBSize pp) where (ProtVer m _) = _protocolVersion pp diff --git a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/STS/Epoch.hs b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/STS/Epoch.hs index 50ce86b415e..a2ab0e5521b 100644 --- a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/STS/Epoch.hs +++ b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/STS/Epoch.hs @@ -27,9 +27,6 @@ import Shelley.Spec.Ledger.LedgerState ( EpochState, PPUPState (..), PState (..), - _delegationState, - _ppups, - _utxoState, emptyAccount, emptyLedgerState, esAccountState, @@ -38,6 +35,9 @@ import Shelley.Spec.Ledger.LedgerState esPp, esPrevPp, esSnapshots, + _delegationState, + _ppups, + _utxoState, pattern DPState, pattern EpochState, ) diff --git a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/TxData.hs b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/TxData.hs index 5aea802973b..66fc9e36e04 100644 --- a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/TxData.hs +++ b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/TxData.hs @@ -95,7 +95,7 @@ import Cardano.Prelude ) import Control.Iterate.SetAlgebra (BaseRep (MapR), Embed (..), Exp (Base), HasExp (toExp)) import Control.Monad (unless) -import Data.Aeson ((.!=), (.:), (.:?), (.=), FromJSON (..), ToJSON (..)) +import Data.Aeson (FromJSON (..), ToJSON (..), (.!=), (.:), (.:?), (.=)) import qualified Data.Aeson as Aeson import Data.Aeson.Types (explicitParseField) import Data.ByteString (ByteString) @@ -781,10 +781,10 @@ instance FromCBOR (Annotator (WitVKey crypto kr)) where fromCBOR = - annotatorSlice - $ decodeRecordNamed "WitVKey" (const 2) - $ fmap pure - $ mkWitVKey <$> fromCBOR <*> decodeSignedDSIGN + annotatorSlice $ + decodeRecordNamed "WitVKey" (const 2) $ + fmap pure $ + mkWitVKey <$> fromCBOR <*> decodeSignedDSIGN where mkWitVKey k sig = WitVKey' k sig (asWitness $ hashKey k) @@ -836,10 +836,10 @@ instance unless (null missingFields) (fail $ "missing required transaction component(s): " <> show missingFields) - pure - $ Annotator - $ \fullbytes bytes -> - (foldr ($) basebody (flip runAnnotator fullbytes . snd <$> mapParts)) {bodyBytes = bytes} + pure $ + Annotator $ + \fullbytes bytes -> + (foldr ($) basebody (flip runAnnotator fullbytes . snd <$> mapParts)) {bodyBytes = bytes} where f :: Int -> diff --git a/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/Generator/Core.hs b/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/Generator/Core.hs index 027f56e062b..8052363ea6e 100644 --- a/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/Generator/Core.hs +++ b/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/Generator/Core.hs @@ -60,7 +60,7 @@ import Control.Monad (replicateM) import Control.Monad.Trans.Reader (asks) import Data.Coerce (coerce) import Data.List (foldl') -import qualified Data.List as List ((\\), find, findIndex) +import qualified Data.List as List (find, findIndex, (\\)) import Data.Map.Strict (Map) import qualified Data.Map.Strict as Map import Data.Ratio ((%)) @@ -110,6 +110,8 @@ import Shelley.Spec.Ledger.Keys ) import Shelley.Spec.Ledger.LedgerState ( AccountState (..), + depositPoolChange, + reapRewards, _delegationState, _deposited, _dstate, @@ -117,8 +119,6 @@ import Shelley.Spec.Ledger.LedgerState _rewards, _utxo, _utxoState, - depositPoolChange, - reapRewards, ) import Shelley.Spec.Ledger.OCert (KESPeriod (..), OCertSignable (..), pattern OCert) import Shelley.Spec.Ledger.PParams @@ -132,11 +132,11 @@ import Shelley.Spec.Ledger.Scripts pattern RequireSignature, ) import Shelley.Spec.Ledger.Slot - ( (*-), - BlockNo (..), + ( BlockNo (..), Duration (..), SlotNo (..), epochInfoFirst, + (*-), ) import Shelley.Spec.Ledger.Tx ( hashScript, @@ -147,9 +147,9 @@ import Shelley.Spec.Ledger.Tx ) import qualified Shelley.Spec.Ledger.Tx as Ledger import Shelley.Spec.Ledger.TxData - ( _txfee, + ( unWdrl, + _txfee, _wdrls, - unWdrl, pattern Wdrl, ) import Shelley.Spec.Ledger.UTxO diff --git a/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/NonTraceProperties/Generator.hs b/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/NonTraceProperties/Generator.hs index 877b28315dc..1a631be6933 100644 --- a/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/NonTraceProperties/Generator.hs +++ b/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/NonTraceProperties/Generator.hs @@ -47,13 +47,13 @@ import Shelley.Spec.Ledger.Credential (pattern KeyHashObj, pattern StakeRefBase) import Shelley.Spec.Ledger.Keys (KeyRole (..), hashKey, vKey) import Shelley.Spec.Ledger.LedgerState ( AccountState (..), + genesisState, _delegationState, _dstate, _genDelegs, _stkCreds, _utxo, _utxoState, - genesisState, ) import Shelley.Spec.Ledger.PParams (PParams, emptyPParams) import Shelley.Spec.Ledger.STS.Delegs From 3c5789f6ca93d25481b807784789639516d83a73 Mon Sep 17 00:00:00 2001 From: Philipp Kant Date: Wed, 15 Jul 2020 21:20:41 +0200 Subject: [PATCH 6/8] Properly generate arbitrary UTxO values Previously, we had relied on Generic, which was using the CompactUTxO construnctor directly (so the bytestring which was supposed to be a serialised address was just a random bystestring). --- .../src/Shelley/Spec/Ledger/TxData.hs | 12 ++++++++---- .../Shelley/Spec/Ledger/SerializationProperties.hs | 6 +++--- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/TxData.hs b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/TxData.hs index 66fc9e36e04..5bd135260b1 100644 --- a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/TxData.hs +++ b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/TxData.hs @@ -86,8 +86,9 @@ import Cardano.Binary import Cardano.Prelude ( AllowThunksIn (..), LByteString, - NFData (), + NFData (rnf), NoUnexpectedThunks (..), + UseIsNormalFormNamed (..), Word64, asum, catMaybes, @@ -409,7 +410,12 @@ data TxOut crypto = TxOutCompact {-# UNPACK #-} !BSS.ShortByteString {-# UNPACK #-} !Word64 - deriving (Show, Eq, Generic, Ord, NFData) + deriving (Show, Eq, Ord) + +instance NFData (TxOut crypto) where + rnf = (`seq` ()) + +deriving via UseIsNormalFormNamed "TxOut" (TxOut crypto) instance NoUnexpectedThunks (TxOut crypto) pattern TxOut :: Crypto crypto => @@ -432,8 +438,6 @@ viewCompactTxOut (TxOutCompact bs c) = (addr, coin) Just (a :: Addr crypto) -> a coin = word64ToCoin c -instance NoUnexpectedThunks (TxOut crypto) - data DelegCert crypto = -- | A stake key registration certificate. RegKey !(StakeCredential crypto) diff --git a/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/SerializationProperties.hs b/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/SerializationProperties.hs index 3a13107b498..3e3867ae57c 100644 --- a/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/SerializationProperties.hs +++ b/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/SerializationProperties.hs @@ -118,6 +118,7 @@ import Shelley.Spec.Ledger.TxData StakePoolRelay, TxId (TxId), TxIn (TxIn), + TxOut (TxOut), ) import Test.Cardano.Prelude (genBytes) import Test.QuickCheck @@ -302,9 +303,8 @@ instance Crypto c => Arbitrary (TxIn c) where <$> (TxId <$> genHash (Proxy @c)) <*> arbitrary -instance Arbitrary (Mock.TxOut h) where - arbitrary = genericArbitraryU - shrink = genericShrink +instance HashAlgorithm h => Arbitrary (Mock.TxOut h) where + arbitrary = TxOut <$> arbitrary <*> arbitrary instance Arbitrary Nonce where arbitrary = From 6aeb5a8e62153ab4e99207dfe245f4a2471f1672 Mon Sep 17 00:00:00 2001 From: Philipp Kant Date: Wed, 15 Jul 2020 21:53:53 +0200 Subject: [PATCH 7/8] Added roundtrip test for TxOut --- .../test/Test/Shelley/Spec/Ledger/Serialization.hs | 2 ++ .../test/Test/Shelley/Spec/Ledger/SerializationProperties.hs | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/Serialization.hs b/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/Serialization.hs index 1b75445f004..9e12805b70f 100644 --- a/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/Serialization.hs +++ b/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/Serialization.hs @@ -220,6 +220,7 @@ import Test.Shelley.Spec.Ledger.SerializationProperties prop_roundtrip_RewardAcnt, prop_roundtrip_Tx, prop_roundtrip_TxId, + prop_roundtrip_TxOut, ) import Test.Shelley.Spec.Ledger.Utils import Test.Tasty (TestTree, testGroup) @@ -538,6 +539,7 @@ serializationPropertyTests = QC.testProperty "roundtrip Tx" prop_roundtrip_Tx, QC.testProperty "roundtrip Bootstrap Witness" prop_roundtrip_BootstrapWitness, QC.testProperty "roundtrip TxId" prop_roundtrip_TxId, + QC.testProperty "roundtrip TxOut" prop_roundtrip_TxOut, QC.testProperty "roundtrip LEDGER Predicate Failures" prop_roundtrip_LEDGER_PredicateFails, QC.testProperty "roundtrip Protocol State" prop_roundtrip_PrtclState, QC.testProperty "roundtrip Ledger State" prop_roundtrip_LedgerState, diff --git a/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/SerializationProperties.hs b/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/SerializationProperties.hs index 3e3867ae57c..2ab2341f8bd 100644 --- a/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/SerializationProperties.hs +++ b/shelley/chain-and-ledger/executable-spec/test/Test/Shelley/Spec/Ledger/SerializationProperties.hs @@ -21,6 +21,7 @@ module Test.Shelley.Spec.Ledger.SerializationProperties prop_roundtrip_BlockHeaderHash, prop_roundtrip_Tx, prop_roundtrip_TxId, + prop_roundtrip_TxOut, prop_roundtrip_LEDGER_PredicateFails, prop_roundtrip_PrtclState, prop_roundtrip_LedgerState, @@ -212,6 +213,9 @@ prop_roundtrip_Tx = roundtrip' toCBOR ((. Full) . runAnnotator <$> fromCBOR) prop_roundtrip_TxId :: Mock.TxId Monomorphic.ShortHash -> Property prop_roundtrip_TxId = roundtrip toCBOR fromCBOR +prop_roundtrip_TxOut :: Mock.TxOut Monomorphic.ShortHash -> Property +prop_roundtrip_TxOut = roundtrip toCBOR fromCBOR + prop_roundtrip_BootstrapWitness :: Mock.BootstrapWitness Monomorphic.ShortHash -> Property prop_roundtrip_BootstrapWitness = roundtrip' toCBOR ((. Full) . runAnnotator <$> fromCBOR) From 1af62c82fab3500214dbd1616ba0f6f3ef1139e0 Mon Sep 17 00:00:00 2001 From: Jared Corduan Date: Wed, 15 Jul 2020 18:13:27 -0400 Subject: [PATCH 8/8] unpack the TxId in the TxIn Co-authored-by: Duncan Coutts --- .../executable-spec/src/Shelley/Spec/Ledger/TxData.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/TxData.hs b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/TxData.hs index 5bd135260b1..8b3a2a52254 100644 --- a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/TxData.hs +++ b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/TxData.hs @@ -384,7 +384,7 @@ deriving newtype instance Crypto crypto => ToCBOR (TxId crypto) deriving newtype instance Crypto crypto => FromCBOR (TxId crypto) -- | The input of a UTxO. -data TxIn crypto = TxInCompact !(TxId crypto) {-# UNPACK #-} !Word64 +data TxIn crypto = TxInCompact {-# UNPACK #-} !(TxId crypto) {-# UNPACK #-} !Word64 deriving (Show, Eq, Generic, Ord, NFData) -- TODO: We will also want to have the TxId be compact, but the representation