-
Notifications
You must be signed in to change notification settings - Fork 483
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
base: master
Are you sure you want to change the base?
Conversation
@effectfully - would you mind taking a look at this? |
It's on my list, but I can't do it this week, sorry, too many commitments. I'll do my best to review it the next week. Thanks for the reminder. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shrinking is broken, coverage is poor, invariants aren't respected. 3k lines of code.
Thank you for opening the PR! We'll process it, split into smaller PRs, fix issues etc.
arbitrary = | ||
UnsafeSizedByteString . BS.pack <$> do | ||
let !len = fromIntegral . natVal $ Proxy @n | ||
vectorOf len arbitrary |
There was a problem hiding this comment.
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.
|
||
instance KnownNat n => Arbitrary (SizedByteString n) where | ||
{-# INLINEABLE arbitrary #-} | ||
arbitrary = |
There was a problem hiding this comment.
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 ByteString
s normally we use Text.encodeUtf8 <$> arbitrary
. Why? Should we do the same here?
There was a problem hiding this comment.
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 ByteString
s 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.
There was a problem hiding this comment.
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!
instance Arbitrary PlutusTx.BuiltinByteString where | ||
{-# INLINEABLE arbitrary #-} | ||
arbitrary = PlutusTx.toBuiltin @ByteString <$> arbitrary | ||
{-# INLINEABLE shrink #-} | ||
shrink = fmap (PlutusTx.toBuiltin @ByteString) . shrink . PlutusTx.fromBuiltin |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Unisay once you're done figuring out what we're supposed to do with ByteString
, could you just suggest what we should do here? Is it already fine?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this instance delegates to the Arbitrary ByteString
instance.
Assuming the latter is fine, this one should be also fine.
{- | This is a very general instance, able to produce 'PlutusTx.BuiltinData' of | ||
basically any shape. You probably want something more focused than this. | ||
-} | ||
instance Arbitrary PlutusTx.BuiltinData where |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just a reimplementation (a likely worse one) of the already existing Arbitrary Data
instance.
this can have zero values, and does not treat the Ada symbol or token name | ||
specially. | ||
-} | ||
instance Arbitrary Value where |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have that one as well, except more fine-grained.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we instead use instance that was in plutus-ledger-api/testlib/PlutusLedgerApi/Test/V1/Value.hs
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should merge ours with yours as both have good and bad things about them. I assume MLabs isn't paid by IOG to do this work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, current Plutarch work is part of Catalyst project
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good to know, thanks. It'll probably take us a lot of time to adopt all of this code tbh, due to other things being considered higher priority. We'll tag you on relevant PRs if we have any improvement suggestions over what you have here. Thanks again for creating this PR, we'll keep it open for now and we'll see how it goes with adopting your code.
2. It is difficult to work within the size limit (32 bytes) when generating | ||
UTF-8. | ||
-} | ||
instance Arbitrary TokenName where |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one we definitely need to steal.
plutus-ledger-api/testlib/PlutusLedgerApi/Test/Orphans/V1/Value.hs
Outdated
Show resolved
Hide resolved
described above: failing to do so means all guarantees of this type are off | ||
the table. | ||
-} | ||
newtype MintValue = MintValue Value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We now have our own MintValue
type with its generator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you point me to that please? This is also "old" mint value, i.e. for V1 and V2 that has zero ADA entry, our V3 MintValue has that omitted
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PlutusLedgerApi.V3.MintValue.MintValue
and the generator is in PlutusLedgerApi.Test.V3.MintValue
. Ours is probably broken though, we need to fix it.
arbitrary = error "No such 'Arbitrary' instance" | ||
shrink _ = [] | ||
|
||
instance Arbitrary Value where |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's bold haha.
Such intervals are provided by cardano-node, see: <https://github.com/IntersectMBO/plutus/blob/c082e28591990a14a9f4f91700b684590ac252be/plutus-ledger-api/src/PlutusLedgerApi/V1/Interval.hs#L369-L393>
This is a backport of <Plutonomicon/plutarch-plutus@6c8db66>
Closes #6210
Add
Arbitrary
,CoArbitrary
andFunction
instances fromQuickCheck
for everyPlutusLedgerApi
type. This is a port of instances from https://github.com/Plutonomicon/plutarch-plutusPre-submit checklist: