Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PlutusLedgerApi QuickCheck instances #6624

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### Removed

- `Arbitrary` instances from `PlutusLedgerApi.Test.V1.Value` and `PlutusLedgerApi.Test.V3.MintValue`. Import `PlutusLedgerApi.Test.QuickCheck` instead.

### Added

- `PlutusLedgerApi.Test.QuickCheck` module to testlib with quickcheck instances for all ledger types.
24 changes: 24 additions & 0 deletions plutus-ledger-api/plutus-ledger-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ library plutus-ledger-api-testlib
PlutusLedgerApi.Test.Common.EvaluationContext
PlutusLedgerApi.Test.EvaluationEvent
PlutusLedgerApi.Test.Examples
PlutusLedgerApi.Test.QuickCheck
PlutusLedgerApi.Test.Scripts
PlutusLedgerApi.Test.V1.Data.EvaluationContext
PlutusLedgerApi.Test.V1.Data.Value
Expand All @@ -136,6 +137,28 @@ library plutus-ledger-api-testlib
PlutusLedgerApi.Test.V3.EvaluationContext
PlutusLedgerApi.Test.V3.MintValue

other-modules:
PlutusLedgerApi.Test.Common.QuickCheck.Utils
PlutusLedgerApi.Test.Orphans.PlutusTx
PlutusLedgerApi.Test.Orphans.V1
PlutusLedgerApi.Test.Orphans.V1.Address
PlutusLedgerApi.Test.Orphans.V1.Contexts
PlutusLedgerApi.Test.Orphans.V1.Credential
PlutusLedgerApi.Test.Orphans.V1.Crypto
PlutusLedgerApi.Test.Orphans.V1.DCert
PlutusLedgerApi.Test.Orphans.V1.Interval
PlutusLedgerApi.Test.Orphans.V1.Scripts
PlutusLedgerApi.Test.Orphans.V1.Time
PlutusLedgerApi.Test.Orphans.V1.Tx
PlutusLedgerApi.Test.Orphans.V1.Value
PlutusLedgerApi.Test.Orphans.V2
PlutusLedgerApi.Test.Orphans.V2.Contexts
PlutusLedgerApi.Test.Orphans.V2.Tx
PlutusLedgerApi.Test.Orphans.V3
PlutusLedgerApi.Test.Orphans.V3.Contexts
PlutusLedgerApi.Test.Orphans.V3.MintValue
PlutusLedgerApi.Test.Orphans.V3.Tx

build-depends:
, barbies
, base >=4.9 && <5
Expand All @@ -149,6 +172,7 @@ library plutus-ledger-api-testlib
, plutus-tx ^>=1.36
, prettyprinter
, QuickCheck
, quickcheck-instances
, serialise
, text

Expand Down
1 change: 1 addition & 0 deletions plutus-ledger-api/test/Spec/V1/Data/Value.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module Spec.V1.Data.Value where

import PlutusLedgerApi.Test.QuickCheck ()
import PlutusLedgerApi.Test.V1.Data.Value as Value
-- TODO: import a new PlutusLedgerApi.Data.V1 module instead
import PlutusLedgerApi.V1.Data.Value
Expand Down
1 change: 1 addition & 0 deletions plutus-ledger-api/test/Spec/V1/Value.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Spec.V1.Value where

import PlutusLedgerApi.Test.QuickCheck ()
import PlutusLedgerApi.Test.V1.Value as Value
import PlutusLedgerApi.V1
import PlutusLedgerApi.V1.Value
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE RoleAnnotations #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ViewPatterns #-}

module PlutusLedgerApi.Test.Common.QuickCheck.Utils (
SizedByteString (SizedByteString),
unSizedByteString,
AsWord64 (AsWord64),
fromAsWord64,
) where

import Data.ByteString (ByteString)
import Data.ByteString qualified as BS
import Data.Coerce (coerce)
import Data.Proxy (Proxy (Proxy))
import Data.Word (Word64)
import GHC.TypeNats (KnownNat, Nat, natVal)
import Test.QuickCheck (Arbitrary (arbitrary, shrink), CoArbitrary, Function (function),
functionMap, vectorOf)
import Test.QuickCheck.Instances.ByteString ()

{- | Helper for 'ByteString's of a fixed length. We don't expose the
constructor, instead providing a read-only pattern, as well as an accessor
function, to ensure that the size invariant is maintained.
-}
newtype SizedByteString (n :: Nat) = UnsafeSizedByteString ByteString
deriving
(Eq
,Ord
)
via ByteString
deriving stock
(Show
)

type role SizedByteString nominal

instance KnownNat n => Arbitrary (SizedByteString n) where
{-# INLINEABLE arbitrary #-}
arbitrary =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self: when we generate ByteStrings normally we use Text.encodeUtf8 <$> arbitrary. Why? Should we do the same here?

Copy link
Contributor

@kozross kozross Nov 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This guarantees that you get ByteStrings that are UTF-8 encodings of text, which is probably what you often want. However, in a lot of situations here, we're generating arbitrary hashes, which most certainly do not follow this rule.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we should change this rule in our code in the first place. Thanks!

UnsafeSizedByteString . BS.pack <$> do
let !len = fromIntegral . natVal $ Proxy @n
vectorOf len arbitrary
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vectorOf has to force len, so you don't really need a bang here, but it's OK to keep it, I'm only saying it as FYI.

{-# INLINEABLE shrink #-}
shrink =
fmap (UnsafeSizedByteString . BS.pack)
. traverse shrink
. BS.unpack
. unSizedByteString

deriving via ByteString instance CoArbitrary (SizedByteString n)


instance Function (SizedByteString n) where
{-# INLINEABLE function #-}
function = functionMap coerce UnsafeSizedByteString

{- | Read-only pattern for accessing the underlying 'ByteString'. Use it just
like you would use a data constructor in a pattern match.
-}
pattern SizedByteString :: forall (n :: Nat). ByteString -> SizedByteString n
pattern SizedByteString bs <- UnsafeSizedByteString bs

{-# COMPLETE SizedByteString #-}

{- | Get the underlying 'ByteString'. It is guaranteed to have the length
specified in its type.
-}
unSizedByteString ::
forall (n :: Nat).
SizedByteString n ->
ByteString
unSizedByteString = coerce

{- | Plutus' ledger API often has to \'fake\' 'Word64' using the much larger
'Integer' type. This helper is designed to generate 'Integer's that fit into
'Word64'.

We don't expose the constructor directly; instead, we provide a read-only
pattern and an accessor function.
-}
newtype AsWord64 = UnsafeAsWord64 Word64
deriving
(Eq
,Ord
,Arbitrary
,CoArbitrary
)
via Word64
deriving stock
(Show
)

instance Function AsWord64 where
{-# INLINEABLE function #-}
function = functionMap coerce UnsafeAsWord64

{- | Read-only pattern for accessing the underlying 'Integer'. Use it just like
you would use a data constructor in a pattern match.
-}
pattern AsWord64 :: Integer -> AsWord64
pattern AsWord64 i <- (fromAsWord64 -> i)

fromAsWord64 :: AsWord64 -> Integer
fromAsWord64 = fromIntegral . coerce @_ @Word64
Loading