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

Fix TxMetaText generators. #2862

Merged
merged 5 commits into from
Sep 1, 2021

Conversation

jonathanknowles
Copy link
Contributor

@jonathanknowles jonathanknowles commented Aug 31, 2021

Issue Number

#2861

Comments

This PR:

  • fixes generators for TxMetadata so that they only produce TxMetaText values where the UTF-8-serialized length does not exceed 64 bytes.
  • renames generators to genSimpleTxMetadata and genNestedTxMetadata to emphasise the difference in generated values (simple vs nested).
  • uses UnicodeString to generate TxMetaText content.
  • adds round-trip persistence tests for genSimpleTxMetadata and genNestedTxMetadata.

These tests fail, as the generator for `TxMetaText` values occasionally
produces values that when UTF-8 encoded are longer than 64 bytes, which
is disallowed.
The length of a `TxMetaText`, when UTF-8 encoded, must not be greater
than 64 bytes.
Copy link
Contributor

@rvl rvl left a comment

Choose a reason for hiding this comment

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

Thanks

(T.pack . take 32 . getPrintableString <$> arbitrary) `suchThat` guardText
genTxMetaText :: Gen Text
genTxMetaText =
(T.pack . take 32 . getPrintableString <$> arbitrary)
Copy link
Contributor

Choose a reason for hiding this comment

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

We could make this variable-length, with a hard maximum of 64 unicode characters, which is then filtered by guardTxMetaText.

Suggested change
(T.pack . take 32 . getPrintableString <$> arbitrary)
(T.pack . getUnicodeString <$> scale (min 64) arbitrary)

Also I believe it's fine to use UnicodeString instead of PrintableString.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 58cc2af.

Comment on lines 234 to 237
shrinkTxMetaText t
| n <= 1 = []
| otherwise = filter guardText [ T.take (n `div` 2) t, T.drop (n `div` 2) t ]
| otherwise = filter guardTxMetaTextPrefix
[ T.take (n `div` 2) t, T.drop (n `div` 2) t ]
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm pretty sure guardText isn't needed anymore - a relic of a previous JSON encoding of TxMetadata. So we could just re-use the UnicodeString shrinker.

shrinkTxMetaText t = [T.pack s | u <- shrink (UnicodeString $ T.unpack t), let s = getUnicodeString u, length s > 0]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in ab6f53b and 58cc2af.

newtype Large a = Large { unLarge :: a } deriving (Eq, Show)

instance Arbitrary (Small TxMetadata) where
arbitrary = Small <$> genSmallTxMetadata
Copy link
Contributor

Choose a reason for hiding this comment

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

I looked at the definitions of genSmallTxMetadata and genTxMetadata to try and see why they couldn't be replaced with Test.QuickCheck.scale.

The distinction seems to be that "small" generates non-recursive metadata values from the 3 primitive metadata types, and non-small generates metadata values of any depth.

So perhaps we could fix the naming. So remove Large and rename Small to something like Primitive or Simple.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed (hopefully) in dc70900.

@jonathanknowles
Copy link
Contributor Author

bors r+

@iohk-bors
Copy link
Contributor

iohk-bors bot commented Sep 1, 2021

Build succeeded:

@iohk-bors iohk-bors bot merged commit 15e582b into master Sep 1, 2021
@iohk-bors iohk-bors bot deleted the jonathanknowles/fix-tx-meta-text-generators branch September 1, 2021 04:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants