-
Notifications
You must be signed in to change notification settings - Fork 158
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
Serialisation generators for Mary/Allegra #1966
Conversation
|
🙏 |
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.
There is now a bunch of overlap and duplication between Test.Cardano.Ledger.ShelleyMA.Serialisation.Generators
and Test.Shelley.Spec.Ledger.Serialisation.Generators
. For example, compare genBlock
in the former to the Arbitrary (Block era)
instance in the latter, it's almost a verbatim copy.
Moreover, the instances overlap, e.g., Arbitrary (Block (AllegraEra C_Crypto))
and Arbitrary (Block era)
.
This approach doesn't work well together with consensus, as we want our generators to be parametric in the era, just like all our Shelley-code. We don't want to duplicate our generators for all eras. In consensus they would be identical copies.
Fortunately, I think there is a good solution:
- Move all the
Arbitrary
instances and generator functions that can be parametric in the era toTest.Shelley.Spec.Ledger.Serialisation.EraIndepGenerators
. Don't impose any constraints that can only be satisfied by a single era, e.g.,ShelleyTest
hasCore.TxBody era ~ TxBody era
, that's a no go. These generators will require constraints likeArbitrary (Core.TxBody era)
, etc. You can bundle them inShelleyBasedTest
for convenience. - In
Test.Shelley.Spec.Ledger.Serialisation.Generators
, define theArbitrary
instances for the Shelley era specific types, i.e., the constraints you had to add toShelleyBasedTest
. - In
Test.Cardano.Ledger.ShelleyMA.Serialisation.Generators
, define theArbitrary
instances for the Mary and Allegra era specific types.
The good thing about this approach is that it should eliminate almost all duplication. Adding an extra era will only require writing Arbitrary
instances for the TxBody
, Script
, ... types that are different in that era. Moreover, consensus will be able to use these generators, even when new eras are added in the future (as long as the era-specific instances are provided).
where | ||
genScript = genMAScript | ||
|
||
instance Arbitrary (MA.TxBody (MaryEra C_Crypto)) 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.
Why are these all fixed to C_Crypto
? (Same for Allegra) Consensus can't use C_Crypto
.
Fortunately, you can generalise these all to Mock c => .. c
, I have tried it out myself 🙂.
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.
sure, I'll go with Mock c
- actually started with that and then specialised ;/
Hasn't this happened before? |
47d28ee
to
16fe142
Compare
FYI, consensus will remain blocked until this is done. |
👍 |
still finding that out - but this PR has nothing to do with it I'm sure |
b5f2519
to
dc7bfd5
Compare
@mrBliss I've made these changes
From your side you'd now have to import both
How does this work for you? |
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.
Thanks @uroboros for working on this!
We're getting closer, but it's still not quite what consensus needs 🙂.
The diamond we talked about:
1. shared
/ | \
/ | \
2. Shelley Allegra Mary
\ | /
\ | /
3. top-level shared
- The
Arbitrary
instances that are shared among all eras. For exampleArbitrary (HashHeader crypto)
,Arbitrary (Update era)
, etc. These live inEraIndepGenerators
. - The
Arbitrary
instances for the types that differ from era to era, i.e.,Value
,TxBody
,Script
. So theArbitrary (MA.TxBody (MaryEra c))
, ... instances inShelleyMA/../Generators
and theArbitrary (TxBody (ShelleyEra c))
, ... instances inShelley/../Generators
. - The top-level
Arbitrary
instances forBlock era
,Tx era
,LedgersPredicateFailure era
, ..., that consensus wants. Note that these are parameterised by the era. These should live inEraIndepGenerators
.
In the current state of the PR, 1 and 2 are done 👍. But 3 is not yet done, as there are still separate Arbitrary (Block (ShelleyEra c))
and Arbitrary (Block (MaryEra c))
instances (same for Tx
, ...), instead of a single Arbitrary (Block era)
instance. And it is exactly this what consensus needs, as our code is parametric in the era.
where | ||
arbitrary = genericArbitraryU | ||
|
||
-- we don't have a shrinker for Value, so we do not shrink this | ||
-- predicate failure, as its constructor contains Value | ||
shrink pf = [pf] |
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.
BTW, if you can't shrink, return []
. I think the current code might cause an infinite shrink.
febb953
to
ec7640b
Compare
@mrBliss in this episode! ...
What I could not quite get for you is the "top of the diamond" - but in effect
|
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.
@mrBliss in this episode! ...
* the following are now era parametrised in `EraIndep.hs` * `Block era, Tx era` instances * all `PredicateFail` instances (except the UTXO one, which is specialised per era) * `EraIndep` no longer refs (or is referenced by) `Shelley.Generators` and `Shelley.MAGenerators` - they are independent
Excellent!
What I could not quite get for you is the "top of the diamond" - but in effect
EraIndep.hs
becomes your entry point, and must be imported alongside the specialised ones:import Test.Shelley.Spec.Ledger.Serialisation.EraIndepGenerators () -- shelley-spec-ledger-test import Test.Shelley.Spec.Ledger.Serialisation.Generators () -- shelley-spec-ledger-test import Test.Cardano.Ledger.ShelleyMA.Serialisation.Generators () -- cardano-ledger-shelley-ma-test
That's fine!
I'll try it out in consensus right now, to see whether it all works out as expected.
Arbitrary (StrictSeq (TxOut era)), | ||
Arbitrary (StrictSeq (DCert era)), | ||
Arbitrary (Wdrl era), | ||
Arbitrary Coin, | ||
Arbitrary (StrictMaybe (Update era)), | ||
Arbitrary (StrictMaybe (MetaDataHash era)), | ||
Arbitrary (StrictMaybe SlotNo) |
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.
I think you can simplify these constraints, e.g., Arbitrary (StrictSeq (TxOut era))
-> Arbitrary (TxOut era)
and Arbitrary (StrictMaybe a)
-> Arbitrary a
. Enabling the Wredundant-constraints
or -Wsimplifiable-class-constraints
constraints should warn you about this. The same is true for a bunch of other instances.
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.
also here, the StrictSeq
is defined in EraIndep which I don't ref here, which prevents the simplifying to Arbitrary (TxOut era)
Arbitrary SlotNo, | ||
Arbitrary Coin, | ||
Arbitrary ByteString, | ||
Arbitrary Network, | ||
Arbitrary DeltaCoin |
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.
Don't these exist already?
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.
these do exist in EraIndep
- but I'm avoiding to reference that here so that we can use EraIndep as an entry point
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.
What's your objection to depending on EraIndep
here? I would understand objecting to the other direction.
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 point! I'll do that and mop up the constraints
Arbitrary (Core.Value era), | ||
Arbitrary (Core.Script era) | ||
) => | ||
Arbitrary (Tx era) |
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.
👍
Arbitrary (Core.Value era), | ||
Arbitrary (Core.Script era) | ||
) => | ||
Arbitrary (Block era) |
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.
👍
I have just tried it out in consensus, and everything works 🎉 |
If you simplify these constraints (see inline comments), this is ready to merge for me. Thanks again @uroboros. |
yay! |
1780d94
to
761b96e
Compare
Resolves CAD-1845 * Extract EraIndepGenerators from the ShelleyEra serialisation generators so that we can re-use EraIndep as a base for ShelleyMAEra serialisation generators * Specialise Shelley Era generators to ShelleyEra (rather than just the `ShelleyTest` constraint) - otherwise we'd have Overlapping Instances between Shelley/MA * ShelleyMAEra generators are fixed to MaryEra C_Crypto and AllegraEra C_Crypto Note: To import Shelley + ShelleyMA arb instances, you need ``` import Test.Shelley.Spec.Ledger.Serialisation.EraIndepGenerators () -- shelley-spec-ledger-test import Test.Shelley.Spec.Ledger.Serialisation.Generators () -- shelley-spec-ledger-test import Test.Cardano.Ledger.ShelleyMA.Serialisation.Generators () -- cardano-ledger-shelley-ma-test ```
761b96e
to
ec12be7
Compare
The ledger state format changed because of IntersectMBO/cardano-ledger#1968 The decoder is backwards-compatible, so the ledger doesn't break binary compatibility. The other visible change is IntersectMBO/cardano-ledger#1966
Resolves CAD-1845