-
Notifications
You must be signed in to change notification settings - Fork 487
Vendor the flat package
#7308
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
Merged
Merged
Vendor the flat package
#7308
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f64eed4
Vendor the flat package: use a custom fork instead of the official fr…
zeme-wana ce0480b
Embed flat directly
zeme-wana 78c78a4
Done
zeme-wana 64ca50f
done
zeme-wana a9a2ca6
wip
zeme-wana a376b04
wip
zeme-wana 51d2845
wip
zeme-wana 3b36254
wip
zeme-wana 3f60044
Applied requested changed
zeme-wana 1d3272e
Applied requested changed
zeme-wana 2cc762f
wip
zeme-wana File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| Copyright (c) 2016, Pasqualino `Titto` Assini | ||
|
|
||
| All rights reserved. | ||
|
|
||
| Redistribution and use in source and binary forms, with or without | ||
| modification, are permitted provided that the following conditions are met: | ||
|
|
||
| * Redistributions of source code must retain the above copyright | ||
| notice, this list of conditions and the following disclaimer. | ||
|
|
||
| * Redistributions in binary form must reproduce the above | ||
| copyright notice, this list of conditions and the following | ||
| disclaimer in the documentation and/or other materials provided | ||
| with the distribution. | ||
|
|
||
| * Neither the name of the copyright holder nor the names of other | ||
| contributors may be used to endorse or promote products derived | ||
| from this software without specific prior written permission. | ||
|
|
||
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # `flat` (vendored) | ||
|
|
||
| This is a vendored copy of the `flat` Haskell library, included for use within the Plutus repository. | ||
|
|
||
| It is not intended for standalone use or external distribution. Original source available at: https://github.com/Quid2/flat. | ||
|
|
||
| Modifications (if any) from upstream should be documented below. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| {-| | ||
| Haskell implementation of <http://quid2.org/docs/Flat.pdf Flat>, a principled, portable and efficient binary data format. | ||
|
|
||
| -} | ||
| module PlutusCore.Flat | ||
| ( module PlutusCore.Flat.Class | ||
| , module PlutusCore.Flat.Filler | ||
| , module X | ||
| , Decoded | ||
| , DecodeException(..) | ||
| ) | ||
| where | ||
|
|
||
| import PlutusCore.Flat.AsBin as X | ||
| import PlutusCore.Flat.AsSize as X | ||
| import PlutusCore.Flat.Class | ||
| import PlutusCore.Flat.Decoder | ||
| import PlutusCore.Flat.Filler | ||
| import PlutusCore.Flat.Instances as X | ||
| import PlutusCore.Flat.Run as X | ||
| import PlutusCore.Flat.Types () |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| {-# LANGUAGE InstanceSigs #-} | ||
| {-# LANGUAGE NoMonomorphismRestriction #-} | ||
| {-# LANGUAGE ScopedTypeVariables #-} | ||
|
|
||
| {- | Wrapper type to decode a value to its flat serialisation. | ||
|
|
||
| See <../test/Big.hs> for an example of use. | ||
|
|
||
| See also 'Flat.Decoder.listTDecoder' and "Flat.AsSize" for other ways to handle large decoded values. | ||
|
|
||
| In 0.5.X this type was called @Repr@. | ||
|
|
||
| @since 0.6 | ||
| -} | ||
| module PlutusCore.Flat.AsBin(AsBin,unbin) where | ||
|
|
||
| import Data.ByteString qualified as B | ||
| import Foreign (plusPtr) | ||
| import PlutusCore.Flat.Bits (bits, fromBools, toBools) | ||
| import PlutusCore.Flat.Class (Flat (..)) | ||
| import PlutusCore.Flat.Decoder.Prim (binOf) | ||
| import PlutusCore.Flat.Decoder.Types (Get (Get, runGet), GetResult (GetResult), | ||
| S (S, currPtr, usedBits)) | ||
| import PlutusCore.Flat.Run (unflatRawWithOffset) | ||
| import Text.PrettyPrint.HughesPJClass (Doc, Pretty (pPrint), prettyShow, text) | ||
|
|
||
| -- $setup | ||
| -- >>> :set -XScopedTypeVariables | ||
| -- >>> import PlutusCore.Flat.Instances.Base | ||
| -- >>> import PlutusCore.Flat.Instances.Text | ||
| -- >>> import PlutusCore.Flat.Decoder.Types | ||
| -- >>> import PlutusCore.Flat.Types | ||
| -- >>> import PlutusCore.Flat.Run | ||
| -- >>> import Data.Word | ||
| -- >>> import qualified Data.Text as T | ||
| -- >>> import Text.PrettyPrint.HughesPJClass | ||
|
|
||
| {- | | ||
|
|
||
| When the flat serialisation of a value takes a lot less memory than the value itself, it can be convenient to keep the value in its encoded representation and decode it on demand. | ||
|
|
||
| To do so, just decode a value `a` as a `AsBin a`. | ||
|
|
||
| Examples: | ||
|
|
||
| Encode a list of Ints and then decode it to a list of AsBin Int: | ||
|
|
||
| >>> unflat (flat [1::Int .. 3]) :: Decoded ([AsBin Int]) | ||
| Right [AsBin {repr = "\129A", offsetBits = 1},AsBin {repr = "A ", offsetBits = 2},AsBin {repr = " \193", offsetBits = 3}] | ||
|
|
||
| To decode an `AsBin a` to an `a`, use `unbin`: | ||
|
|
||
| >>> unbin <$> (unflat (flat 'a') :: Decoded (AsBin Char)) | ||
| Right 'a' | ||
|
|
||
| Keep the values of a list of Ints encoded and decode just one on demand: | ||
|
|
||
| >>> let Right l :: Decoded [AsBin Int] = unflat (flat [1..5]) in unbin (l !! 2) | ||
| 3 | ||
|
|
||
| Show exactly how values are encoded: | ||
|
|
||
| >>> let Right t :: Decoded (AsBin Bool,AsBin Word8,AsBin Bool) = unflat (flat (False,3:: Word64,True)) in prettyShow t | ||
| "(0, _0000001 1, _1)" | ||
|
|
||
| Ten bits in total spread over two bytes: | ||
|
|
||
| @ | ||
| 0 | ||
| _0000001 1 | ||
| _1 | ||
| = | ||
| 00000001 11 | ||
| @ | ||
|
|
||
| Tests: | ||
|
|
||
| >>> unflat (flat ()) :: Decoded (AsBin ()) | ||
| Right (AsBin {repr = "", offsetBits = 0}) | ||
|
|
||
| >>> unflat (flat (False,True)) :: Decoded (Bool,AsBin Bool) | ||
| Right (False,AsBin {repr = "A", offsetBits = 1}) | ||
|
|
||
| >>> unflat (flat (False,False,255 :: Word8)) :: Decoded (Bool,Bool,AsBin Word8) | ||
| Right (False,False,AsBin {repr = "?\193", offsetBits = 2}) | ||
|
|
||
| >>> let Right (b0,b1,rw,b3) :: Decoded (Bool,Bool,AsBin Word8,Bool) = unflat (flat (False,False,255 :: Word8,True)) in (b0,b1,unbin rw,b3) | ||
| (False,False,255,True) | ||
| -} | ||
|
|
||
| data AsBin a = AsBin { | ||
| repr :: B.ByteString -- ^ Flat encoding of the value (encoding starts after offset bits in the first byte and ends in an unspecified position in the last byte) | ||
| ,offsetBits :: Int -- ^ First byte offset: number of unused most significant bits in the first byte | ||
| } deriving Show | ||
|
|
||
| instance Flat a => Pretty (AsBin a) where | ||
| pPrint :: AsBin a -> Doc | ||
| pPrint r = let n = replicate (offsetBits r) in text $ n '_' ++ (drop (offsetBits r) . prettyShow . fromBools . (n False ++) . toBools . bits $ unbin r) | ||
|
|
||
| -- | Decode a value | ||
| unbin :: Flat a => AsBin a -> a | ||
| unbin a = | ||
| case unflatRawWithOffset dec (repr a) (offsetBits a) of | ||
| Right a -> a | ||
| Left e -> error (show e) -- impossible, as it is a valid encoding | ||
| where | ||
| dec = Get $ \end s -> do | ||
| GetResult s' a <- runGet decode end s | ||
| let s'' = S (currPtr s' `plusPtr` if usedBits s' == 0 then 0 else 1) 0 | ||
| return $ GetResult s'' a | ||
|
|
||
| instance Flat a => Flat (AsBin a) where | ||
| size = error "unused" | ||
|
|
||
| encode = error "unused" | ||
|
|
||
| decode :: Flat a => Get (AsBin a) | ||
| decode = uncurry AsBin <$> binOf (decode :: Get a) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| {-# LANGUAGE InstanceSigs #-} | ||
| {-# LANGUAGE NoMonomorphismRestriction #-} | ||
| {-# LANGUAGE ScopedTypeVariables #-} | ||
|
|
||
| {- | | ||
| Wrapper type to decode a value to its size in bits. | ||
|
|
||
| See also "Flat.AsBin". | ||
|
|
||
| In 0.5.X this type was called @SizeOf@. | ||
|
|
||
| @since 0.6 | ||
| -} | ||
| module PlutusCore.Flat.AsSize(AsSize(..)) where | ||
|
|
||
| import PlutusCore.Flat.Class (Flat (..)) | ||
| import PlutusCore.Flat.Decoder.Prim (sizeOf) | ||
| import PlutusCore.Flat.Decoder.Types (Get) | ||
| import PlutusCore.Flat.Types (NumBits) | ||
|
|
||
| -- $setup | ||
| -- >>> :set -XScopedTypeVariables | ||
| -- >>> import PlutusCore.Flat.Instances.Base | ||
| -- >>> import PlutusCore.Flat.Instances.Text | ||
| -- >>> import PlutusCore.Flat.Decoder.Types | ||
| -- >>> import PlutusCore.Flat.Types | ||
| -- >>> import PlutusCore.Flat.Run | ||
| -- >>> import Data.Word | ||
| -- >>> import qualified Data.Text as T | ||
|
|
||
| {- | | ||
| Useful to skip unnecessary values and to check encoding sizes. | ||
|
|
||
| Examples: | ||
|
|
||
| Ignore the second and fourth component of a tuple: | ||
|
|
||
| >>> let v = flat ('a',"abc",'z',True) in unflat v :: Decoded (Char,AsSize String,Char,AsSize Bool) | ||
| Right ('a',AsSize 28,'z',AsSize 1) | ||
|
|
||
| Notice the variable size encoding of Words: | ||
|
|
||
| >>> unflat (flat (1::Word16,1::Word64)) :: Decoded (AsSize Word16,AsSize Word64) | ||
| Right (AsSize 8,AsSize 8) | ||
|
|
||
| Text: | ||
|
|
||
| >>> unflat (flat (T.pack "",T.pack "a",T.pack "主",UTF8Text $ T.pack "主",UTF16Text $ T.pack "主",UTF16Text $ T.pack "a")) :: Decoded (AsSize T.Text,AsSize T.Text,AsSize T.Text,AsSize UTF8Text,AsSize UTF16Text,AsSize UTF16Text) | ||
| Right (AsSize 16,AsSize 32,AsSize 48,AsSize 48,AsSize 40,AsSize 40) | ||
|
|
||
| Various encodings: | ||
|
|
||
| >>> unflat (flat (False,[T.pack "",T.pack "a",T.pack "主"],'a')) :: Decoded (AsSize Bool,AsSize [T.Text],AsSize Char) | ||
| Right (AsSize 1,AsSize 96,AsSize 8) | ||
| -} | ||
| newtype AsSize a = AsSize NumBits deriving (Eq,Ord,Show) | ||
|
|
||
| instance Flat a => Flat (AsSize a) where | ||
| size :: Flat a => AsSize a -> NumBits -> NumBits | ||
| size = error "unused" | ||
|
|
||
| encode = error "unused" | ||
|
|
||
| decode :: Flat a => Get (AsSize a) | ||
| decode = AsSize <$> sizeOf (decode :: Get a) |
Oops, something went wrong.
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.
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 wonder if we should remove this license if we're going to start modifying the code. Maybe there's some company policy about this kind of situation? I see that the license does say that we're supposed to retain it, but what happens if we change the source code?
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.
The license says that ridistribution is permitted so long as the original LICENSE file is retained, AFAIU.
But we should get a second opinion.