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

Reduce overhead of inline datums #2629

Merged
merged 3 commits into from
Jan 26, 2022

Conversation

lehins
Copy link
Collaborator

@lehins lehins commented Jan 25, 2022

Main purpose of this PR is to introduce InlineData, which is a ShortByteString of serialized plutus Data. It is designed with type safety in mind and by hiding type constructor it is impossible to construct InlineData from binary that doesn't deserialize into valid plutus data.

This whole thing is necessary to avoid carrying around both serialized and deserialized version of Data in memory for every inline datum in the utxo.

It also cleans up the rest of usage of HasField "address", which was started in #2616

@lehins lehins requested a review from nc6 January 25, 2022 11:30
Copy link
Contributor

@nc6 nc6 left a comment

Choose a reason for hiding this comment

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

Looks generally good, though I don't understand why the inline datums are in Alonzo rather than Babbage

-- to the memory overhead of the ledger state. Constructor is intentionally not
-- exported, in order to prevent invalid creation of data from arbitrary binary
-- data. Use `makeInlineData` for smart construction.
newtype InlineData era = InlineData ShortByteString
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this not live in the Babbage codebase? We don't have inline datums in Alonzo

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In order to guarantee type safety Data and InlineData have to live in the same module (note both of their constructors aren't exported). I think it is more important than placing types into eras they were introduced in.

However, if it is the name that bothers you, I can rename it to BinaryData and then it will not be tight into Babbage in any way.

Copy link
Contributor

Choose a reason for hiding this comment

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

I sometimes wish Haskell had a concept of "friends", a la C++. But yeah, I wonder whether BinaryData might be a better name, just to avoid confusion.

Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't the usual trick to make Cardano.Ledger.Alonzo.Data.Internal, and re-export in Cardano.Ledger.Alonzo.Data? I'm happy either way.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Using Internal modules is one of the common ways to deal with this, but that doesn't give you the same guarantees as the approach taken in this PR, because Internal modules simply relay responsibility to all programmers, sort of: "if you want to shoot yourself in the foot this is how you do it". While not exporting constructors at all, and instead providing smart constructors (a.k.a Data and makeInlineData in this case) will give hard guarantees that undeserializable InlineDatum cannot be created outside the Data module.

I've renamed it to BinaryData. This will avoid any future confusion and will provide a good insight into what that type means.

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess the nice part about "friends" is that you can pick exactly who all gets to shoot themselves in the foot. :)

@@ -204,12 +211,11 @@ decodeAddress28 stakeRef (Addr28Extra a b c d) = do
addrHash =
hashFromPackedBytes $
PackedBytes28 a b c (fromIntegral (d `shiftR` 32))
pure $ Addr network paymentCred (StakeRefBase stakeRef)
pure $! Addr network paymentCred (StakeRefBase stakeRef)
Copy link
Contributor

Choose a reason for hiding this comment

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

We think this bang could be anything to do with the 1.31 regression?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I doubt, but I was thinking this too. So, who knows, maybe.

@lehins lehins force-pushed the lehins/reduce-overhead-of-inline-datums branch from ca595d7 to 13cc280 Compare January 26, 2022 01:52
@lehins lehins merged commit 8e9b094 into master Jan 26, 2022
@iohk-bors iohk-bors bot deleted the lehins/reduce-overhead-of-inline-datums branch January 26, 2022 16:18
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.

3 participants