Skip to content

Commit

Permalink
ormulu
Browse files Browse the repository at this point in the history
  • Loading branch information
uroboros committed Nov 6, 2020
1 parent c2676c1 commit febb953
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,58 +9,58 @@
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE RankNTypes #-}
{-# OPTIONS_GHC -Wno-orphans #-}

module Test.Cardano.Ledger.ShelleyMA.Serialisation.Generators
(sizedTimelock,
maxTimelockDepth)
where
( sizedTimelock,
maxTimelockDepth,
)
where

import Cardano.Binary
( toCBOR,
)
import Cardano.Crypto.Hash (HashAlgorithm, hashWithSerialiser)
import qualified Cardano.Crypto.Hash as Hash
import Cardano.Ledger.Allegra (AllegraEra)
import qualified Cardano.Ledger.Core as Abstract
import Cardano.Ledger.Era (Era)
import Cardano.Ledger.Mary (MaryEra)
import qualified Cardano.Ledger.Mary.Value as Mary (AssetID (..), PolicyID (..), Value (..))
import Cardano.Ledger.Shelley (ShelleyBased)
import qualified Cardano.Ledger.ShelleyMA.Rules.Utxo as MA.STS
import qualified Cardano.Ledger.ShelleyMA.Scripts as MA (Timelock (..))
import Cardano.Ledger.ShelleyMA.Timelocks (Timelock (..), ValidityInterval)
import qualified Cardano.Ledger.ShelleyMA.TxBody as MA (TxBody (..))
import Data.ByteString.Char8 (ByteString)
import Data.Coerce (coerce)
import Data.Sequence.Strict (StrictSeq, fromList)
import Generic.Random (genericArbitraryU)
import Shelley.Spec.Ledger.API hiding (SignedDSIGN, TxBody (..))
import Shelley.Spec.Ledger.Coin (DeltaCoin)
import Shelley.Spec.Ledger.MetaData (MetaDataHash)
import qualified Shelley.Spec.Ledger.STS.Ledger as STS
import qualified Shelley.Spec.Ledger.STS.Ledgers as STS
import qualified Shelley.Spec.Ledger.STS.Ppup as STS
import qualified Shelley.Spec.Ledger.STS.Utxow as STS
import Shelley.Spec.Ledger.Slot (SlotNo)
import Test.QuickCheck
( Arbitrary,
arbitrary,
choose,
genericShrink,
oneof,
shrink,
listOf,
oneof,
resize,
choose
shrink,
)
import Test.Shelley.Spec.Ledger.ConcreteCryptoTypes (Mock)
import Test.Tasty.QuickCheck (Gen)
import Data.Sequence.Strict (StrictSeq)
import Shelley.Spec.Ledger.MetaData (MetaDataHash)
import Data.ByteString.Char8 (ByteString)
import qualified Shelley.Spec.Ledger.STS.Ppup as STS
import Shelley.Spec.Ledger.Slot (SlotNo)
import Shelley.Spec.Ledger.Coin (DeltaCoin)
import Data.Sequence.Strict (fromList)
import qualified Cardano.Ledger.ShelleyMA.Scripts as MA (Timelock (..))
import Cardano.Crypto.Hash (HashAlgorithm, hashWithSerialiser)
import qualified Cardano.Crypto.Hash as Hash
import Data.Coerce (coerce)
import Cardano.Binary
( toCBOR,
)

{-------------------------------------------------------------------------------
ShelleyMAEra Generators
Expand Down Expand Up @@ -104,19 +104,21 @@ maxTimelockListLens :: Int
maxTimelockListLens = 5

sizedTimelock ::
(Era era,
( Era era,
Arbitrary (StrictMaybe SlotNo)
) =>
Int -> Gen (Timelock era)
Int ->
Gen (Timelock era)
sizedTimelock 0 = (MA.RequireSignature . KeyHash . mkDummyHash) <$> arbitrary
sizedTimelock n =
oneof
[ (MA.RequireSignature . KeyHash . mkDummyHash) <$> arbitrary,
MA.RequireAllOf <$> (fromList <$> resize maxTimelockListLens (listOf (sizedTimelock (n -1)))),
MA.RequireAnyOf <$> (fromList <$> resize maxTimelockListLens (listOf (sizedTimelock (n -1)))),
do subs <- resize maxTimelockListLens (listOf (sizedTimelock (n -1)))
let i = length subs
MA.RequireMOf <$> choose (0,i) <*> pure (fromList subs),
do
subs <- resize maxTimelockListLens (listOf (sizedTimelock (n -1)))
let i = length subs
MA.RequireMOf <$> choose (0, i) <*> pure (fromList subs),
RequireTimeStart <$> arbitrary,
RequireTimeExpire <$> arbitrary
]
Expand All @@ -143,10 +145,10 @@ instance
arbitrary = genTxBody

instance
(Mock c,
PraosCrypto c,
Arbitrary (StrictMaybe SlotNo),
Arbitrary (MultiSig (MaryEra c))
( Mock c,
PraosCrypto c,
Arbitrary (StrictMaybe SlotNo),
Arbitrary (MultiSig (MaryEra c))
) =>
Arbitrary (Timelock (MaryEra c))
where
Expand Down Expand Up @@ -258,10 +260,10 @@ instance
arbitrary = genTxBody

instance
(Mock c,
PraosCrypto c,
Arbitrary (StrictMaybe SlotNo),
Arbitrary (MultiSig (AllegraEra c))
( Mock c,
PraosCrypto c,
Arbitrary (StrictMaybe SlotNo),
Arbitrary (MultiSig (AllegraEra c))
) =>
Arbitrary (Timelock (AllegraEra c))
where
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# OPTIONS_GHC -Wno-orphans #-}

module Test.Cardano.Ledger.ShelleyMA.Serialisation.Timelocks
Expand All @@ -14,41 +14,38 @@ module Test.Cardano.Ledger.ShelleyMA.Serialisation.Timelocks
)
where

import Cardano.Binary( Annotator (..), FromCBOR(..), ToCBOR(..) )
import Cardano.Binary (Annotator (..), FromCBOR (..), ToCBOR (..))
import Cardano.Ledger.Era (PreviousEra)
import Cardano.Ledger.ShelleyMA.Timelocks
( Timelock(..),
pattern Timelock,
translate,
( Timelock (..),
showTimelock,
translate,
pattern Timelock,
)
import Cardano.Slotting.Slot (SlotNo (..))
import qualified Data.ByteString.Lazy as Lazy
import Data.MemoBytes (MemoBytes (Memo))
import Data.Sequence.Strict (fromList)
import Shelley.Spec.Ledger.BaseTypes (StrictMaybe (SJust, SNothing))
import Shelley.Spec.Ledger.Scripts (MultiSig, getMultiSigBytes)
import Test.Cardano.Ledger.ShelleyMA.Serialisation.Coders (embedTripAnn, roundTripAnn)
import Test.Cardano.Ledger.ShelleyMA.Serialisation.Generators (maxTimelockDepth, sizedTimelock)
import Test.Cardano.Ledger.ShelleyMA.TxBody (TestEra)
import Test.Tasty
import Test.Tasty.QuickCheck ( testProperty, Arbitrary, arbitrary )
import Shelley.Spec.Ledger.Scripts(MultiSig,getMultiSigBytes)
import Test.Shelley.Spec.Ledger.Serialisation.EraIndepGenerators ()
import Test.Shelley.Spec.Ledger.Serialisation.Generators()
import Test.Cardano.Ledger.ShelleyMA.Serialisation.Generators(sizedTimelock, maxTimelockDepth)
import Test.Cardano.Ledger.ShelleyMA.Serialisation.Coders(roundTripAnn, embedTripAnn)
import Data.MemoBytes(MemoBytes(Memo))
import Cardano.Ledger.Era(PreviousEra)

import Test.Shelley.Spec.Ledger.Serialisation.Generators ()
import Test.Tasty
import Test.Tasty.QuickCheck (Arbitrary, arbitrary, testProperty)

instance
Arbitrary (Timelock TestEra)
where
instance Arbitrary (Timelock TestEra) where
arbitrary = sizedTimelock maxTimelockDepth

-- ================================================================

s1 :: Timelock TestEra
s1 = RequireAllOf (fromList [ RequireTimeStart (SJust (SlotNo 12)), RequireTimeExpire SNothing])
s1 = RequireAllOf (fromList [RequireTimeStart (SJust (SlotNo 12)), RequireTimeExpire SNothing])

s2 :: Timelock TestEra
s2 = RequireAllOf (fromList [ RequireTimeStart (SJust (SlotNo 12)), RequireTimeExpire (SJust (SlotNo 23))])
s2 = RequireAllOf (fromList [RequireTimeStart (SJust (SlotNo 12)), RequireTimeExpire (SJust (SlotNo 23))])

s4 :: Timelock TestEra
s4 = RequireAllOf (fromList [s1, s2])
Expand All @@ -63,29 +60,29 @@ checkOne nm t = testProperty ("RoundTrip: " ++ nm) $

checkAnn :: Timelock TestEra -> Bool
checkAnn t =
case roundTripAnn t of
Right _ -> True
Left s -> error (show s)
case roundTripAnn t of
Right _ -> True
Left s -> error (show s)

checkEmbed :: MultiSig TestEra -> Bool
checkEmbed multi =
case embedTripAnn @(Timelock TestEra) multi of
Right (left,_) | left == Lazy.empty -> True
Right (left,_) -> error("Bytes left over: "++show left)
Right (left, _) | left == Lazy.empty -> True
Right (left, _) -> error ("Bytes left over: " ++ show left)
Left s -> error (show s)

-- The translate tests depend upon translating from a previous era
-- to the current era. We arbitrarily set the TestEra to be its own
-- PreviousEra. TestEra is only used in Serialisations tests, so
-- this should not have any wider effect.

type instance PreviousEra(TestEra) = TestEra
type instance PreviousEra (TestEra) = TestEra

checkTranslate :: MultiSig TestEra -> Bool
checkTranslate multi = bytes == bytes2
where bytes = getMultiSigBytes multi
(Timelock (Memo _ bytes2)) = translate @TestEra multi

where
bytes = getMultiSigBytes multi
(Timelock (Memo _ bytes2)) = translate @TestEra multi

timelockTests :: TestTree
timelockTests =
Expand All @@ -97,4 +94,4 @@ timelockTests =
testProperty "roundtripTimelock" checkAnn,
testProperty "MultiSig deserialises as Timelock" checkEmbed,
testProperty "Translate preserves bytes" checkTranslate
]
]
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ module BenchUTxOAggregate where
import Cardano.Ledger.Compactible (toCompact)
import Cardano.Ledger.Era (Era (Crypto))
import qualified Cardano.Ledger.Val as Val
import Control.SetAlgebra (Bimap, biMapFromList, dom, (▷), (◁))
import Control.Iterate.SetAlgebra (compile, compute, run)
import Control.SetAlgebra (Bimap, biMapFromList, dom, (▷), (◁))
import Data.Map (Map)
import qualified Data.Map as Map
import qualified Data.Sequence as Seq
import Shelley.Spec.Ledger.Address
( Addr (..),
)
import Shelley.Spec.Ledger.CompactAddr (compactAddr)
import Shelley.Spec.Ledger.Coin (Coin (..))
import Shelley.Spec.Ledger.CompactAddr (compactAddr)
import Shelley.Spec.Ledger.Credential
( Credential (..),
Ptr (..),
Expand All @@ -30,6 +30,7 @@ import Shelley.Spec.Ledger.LedgerState
InstantaneousRewards (..),
PState (..),
)
import Shelley.Spec.Ledger.Scripts ()
import Shelley.Spec.Ledger.TxBody
( PoolParams (..),
TxId (..),
Expand All @@ -44,7 +45,6 @@ import Test.Shelley.Spec.Ledger.ConcreteCryptoTypes (C, C_Crypto)
import Test.Shelley.Spec.Ledger.Examples.Cast (alicePoolParams)
import Test.Shelley.Spec.Ledger.Serialisation.EraIndepGenerators (mkDummyHash)
import Test.Shelley.Spec.Ledger.Serialisation.Generators ()
import Shelley.Spec.Ledger.Scripts ()

genTestCase ::
Int -> -- The size of the utxo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module Test.Shelley.Spec.Ledger.Serialisation.EraIndepGenerators
maxTxWits,
)
where

import Cardano.Binary
( ToCBOR (..),
toCBOR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@ module Test.Shelley.Spec.Ledger.Serialisation.Generators () where

import qualified Cardano.Ledger.Core as Core
import Cardano.Ledger.Shelley (ShelleyEra)
import Data.Sequence.Strict (StrictSeq)
import Generic.Random (genericArbitraryU)
import Shelley.Spec.Ledger.API hiding (SignedDSIGN)
import Shelley.Spec.Ledger.Coin (DeltaCoin)
import Shelley.Spec.Ledger.MetaData (MetaDataHash)
import qualified Shelley.Spec.Ledger.STS.Deleg as STS
import qualified Shelley.Spec.Ledger.STS.Ledger as STS
import qualified Shelley.Spec.Ledger.STS.Ledgers as STS
import qualified Shelley.Spec.Ledger.STS.Ppup as STS
import qualified Shelley.Spec.Ledger.STS.Utxo as STS
import qualified Shelley.Spec.Ledger.STS.Utxow as STS
import qualified Shelley.Spec.Ledger.STS.Ppup as STS
import Shelley.Spec.Ledger.Slot (SlotNo)
import Test.QuickCheck
( Arbitrary,
arbitrary,
genericShrink,
shrink,
)
import Test.Shelley.Spec.Ledger.ConcreteCryptoTypes (Mock)
import Data.Sequence.Strict (StrictSeq)
import Shelley.Spec.Ledger.MetaData (MetaDataHash)
import Shelley.Spec.Ledger.Slot (SlotNo)
import Shelley.Spec.Ledger.Coin (DeltaCoin)

{-------------------------------------------------------------------------------
ShelleyEra Generators
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Shelley.Spec.Ledger.Address (Addr (..), serialiseAddr)
import qualified Shelley.Spec.Ledger.CompactAddr as CA
import Shelley.Spec.Ledger.Credential
( PaymentCredential,
StakeReference (..)
StakeReference (..),
)
import Test.QuickCheck
import Test.Shelley.Spec.Ledger.ConcreteCryptoTypes (Mock)
Expand Down Expand Up @@ -50,10 +50,12 @@ propDecompactShelleyLazyAddr ::
(Era era, Mock (Crypto era)) =>
Gen Bool
propDecompactShelleyLazyAddr = do
stakeRef <- oneof
[ StakeRefBase <$> arbitrary
, StakeRefPtr <$> arbitrary
] :: Gen (StakeReference era)
stakeRef <-
oneof
[ StakeRefBase <$> arbitrary,
StakeRefPtr <$> arbitrary
] ::
Gen (StakeReference era)
addr <- Addr <$> arbitrary <*> arbitrary <*> pure stakeRef
let keyHash0 = unsafeGetHash addr
keyHash1 = unsafeGetHash . CA.decompactAddr . mangle . CA.compactAddr $ addr
Expand Down

0 comments on commit febb953

Please sign in to comment.