Add Flat encoding test coverage across all packages#7678
Open
Add Flat encoding test coverage across all packages#7678
Conversation
Pay down test coverage debt for Flat serialization instances. This ensures encoding stability is verified by tests before we attempt to fix the Generic derivation bug in a follow-up PR. Adds roundtrip and stable byte encoding tests for: - Flat library types (Maybe, Either, NonEmpty, Complex, Ratio, Set, Tree, Map, Seq, DList, Filler, PreAligned, and monoid/semigroup wrappers) - TPLC types (Version, Name, Kind, DeBruijn, NamedDeBruijn, SrcSpan, SrcSpans, DefaultFun, DefaultUni, and all newtype wrappers including TyName, Unique, TyDeBruijn, etc.) - PIR types (Recursivity, Strictness) - UPLC types (Binder variants, FakeNamedDeBruijn, minimal Program encoding) - Value types (K encodes as ByteString, Quantity as Integer) Also adds a standalone encoding generator executable (cabal run flat-encoding-generator) for reproducing expected byte constants.
zliu41
reviewed
Mar 19, 2026
| -- | Stable byte encoding tests for flat library container/composite types. | ||
| -- Wrapper types (Identity, All, Any, Dual, etc.) only have roundtrip tests | ||
| -- since their encoding stability is not critical (they are never on-chain). | ||
| testNewEncodings = testGroup "stable byte encodings" |
Member
There was a problem hiding this comment.
We are not introducing any new encodings, so this should just be called "testEncodings"
| -- | Reproducible encoding generator for Flat test coverage. | ||
| -- Run: cabal run flat-encoding-generator | ||
| -- Prints expected byte sequences for all Flat test values. | ||
| executable flat-encoding-generator |
Member
There was a problem hiding this comment.
What's the purpose of this, and in what scenario would someone want to use this executable?
|
|
||
| ghc-options: | ||
| -Wno-missing-signatures -Wno-missing-deriving-strategies | ||
| -Wno-unused-packages |
Member
There was a problem hiding this comment.
Why do you need these flags? Can you just fix the warnings?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
Approach
Added two categories of tests for Flat instances:
Stable byte encoding tests pin the exact byte representation of on-chain-critical types (Version, DeBruijn, DefaultFun, DefaultUni, Binder variants, UPLC Program, PIR Recursivity/Strictness). These detect any accidental encoding changes that would break on-chain script compatibility.
Roundtrip property tests verify
unflat (flat x) == Right xfor all Flat instances including library-internal types (monoid/semigroup wrappers, containers, etc.) where encoding stability is less critical but correctness still matters.A standalone encoding generator executable (
cabal run flat-encoding-generator) is included so anyone can reproduce the expected byte constants without GHCi sessions.BLS12_381 types are deliberately skipped (their encode/decode implementations are
error/fail).Changes
Flat library tests (
flat/test/Spec.hs)TPLC tests (
plutus-core/test/Flat/Spec.hs— new module)UPLC tests (
untyped-plutus-core/testlib/Flat/Spec.hs)(program 1.1.0 (con integer 0))test_flatinto the UPLC test entry point (was previously defined but not imported)PIR tests (
plutus-ir/test/PlutusIR/Core/Tests.hs)Value tests (
plutus-core/test/Value/Spec.hs)Infrastructure
plutus-core/tools/GenerateEncodings.hs)FOURMOLU_DISABLEtoflat/test/Spec.hs(CPP-heavy file that fourmolu cannot parse)