RLP (empty) → 1.0.0
raw patch · 9 files changed
+779/−0 lines, 9 filesdep +basedep +binarydep +bytestringsetup-changed
Dependencies added: base, binary, bytestring, hspec
Files
- ChangeLog.md +5/−0
- LICENSE +165/−0
- README.md +12/−0
- RLP.cabal +47/−0
- Setup.hs +2/−0
- src/Data/Serialize/RLP.hs +228/−0
- src/Data/Serialize/RLP/Internal.hs +188/−0
- test/Data/Serialize/RLPSpec.hs +131/−0
- test/Spec.hs +1/−0
+ ChangeLog.md view
@@ -0,0 +1,5 @@+# Revision history for RLP++## 1.0.0 -- 2018-12-01++* First version.
+ LICENSE view
@@ -0,0 +1,165 @@+ GNU LESSER GENERAL PUBLIC LICENSE+ Version 3, 29 June 2007++ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>+ Everyone is permitted to copy and distribute verbatim copies+ of this license document, but changing it is not allowed.+++ This version of the GNU Lesser General Public License incorporates+the terms and conditions of version 3 of the GNU General Public+License, supplemented by the additional permissions listed below.++ 0. Additional Definitions.++ As used herein, "this License" refers to version 3 of the GNU Lesser+General Public License, and the "GNU GPL" refers to version 3 of the GNU+General Public License.++ "The Library" refers to a covered work governed by this License,+other than an Application or a Combined Work as defined below.++ An "Application" is any work that makes use of an interface provided+by the Library, but which is not otherwise based on the Library.+Defining a subclass of a class defined by the Library is deemed a mode+of using an interface provided by the Library.++ A "Combined Work" is a work produced by combining or linking an+Application with the Library. The particular version of the Library+with which the Combined Work was made is also called the "Linked+Version".++ The "Minimal Corresponding Source" for a Combined Work means the+Corresponding Source for the Combined Work, excluding any source code+for portions of the Combined Work that, considered in isolation, are+based on the Application, and not on the Linked Version.++ The "Corresponding Application Code" for a Combined Work means the+object code and/or source code for the Application, including any data+and utility programs needed for reproducing the Combined Work from the+Application, but excluding the System Libraries of the Combined Work.++ 1. Exception to Section 3 of the GNU GPL.++ You may convey a covered work under sections 3 and 4 of this License+without being bound by section 3 of the GNU GPL.++ 2. Conveying Modified Versions.++ If you modify a copy of the Library, and, in your modifications, a+facility refers to a function or data to be supplied by an Application+that uses the facility (other than as an argument passed when the+facility is invoked), then you may convey a copy of the modified+version:++ a) under this License, provided that you make a good faith effort to+ ensure that, in the event an Application does not supply the+ function or data, the facility still operates, and performs+ whatever part of its purpose remains meaningful, or++ b) under the GNU GPL, with none of the additional permissions of+ this License applicable to that copy.++ 3. Object Code Incorporating Material from Library Header Files.++ The object code form of an Application may incorporate material from+a header file that is part of the Library. You may convey such object+code under terms of your choice, provided that, if the incorporated+material is not limited to numerical parameters, data structure+layouts and accessors, or small macros, inline functions and templates+(ten or fewer lines in length), you do both of the following:++ a) Give prominent notice with each copy of the object code that the+ Library is used in it and that the Library and its use are+ covered by this License.++ b) Accompany the object code with a copy of the GNU GPL and this license+ document.++ 4. Combined Works.++ You may convey a Combined Work under terms of your choice that,+taken together, effectively do not restrict modification of the+portions of the Library contained in the Combined Work and reverse+engineering for debugging such modifications, if you also do each of+the following:++ a) Give prominent notice with each copy of the Combined Work that+ the Library is used in it and that the Library and its use are+ covered by this License.++ b) Accompany the Combined Work with a copy of the GNU GPL and this license+ document.++ c) For a Combined Work that displays copyright notices during+ execution, include the copyright notice for the Library among+ these notices, as well as a reference directing the user to the+ copies of the GNU GPL and this license document.++ d) Do one of the following:++ 0) Convey the Minimal Corresponding Source under the terms of this+ License, and the Corresponding Application Code in a form+ suitable for, and under terms that permit, the user to+ recombine or relink the Application with a modified version of+ the Linked Version to produce a modified Combined Work, in the+ manner specified by section 6 of the GNU GPL for conveying+ Corresponding Source.++ 1) Use a suitable shared library mechanism for linking with the+ Library. A suitable mechanism is one that (a) uses at run time+ a copy of the Library already present on the user's computer+ system, and (b) will operate properly with a modified version+ of the Library that is interface-compatible with the Linked+ Version.++ e) Provide Installation Information, but only if you would otherwise+ be required to provide such information under section 6 of the+ GNU GPL, and only to the extent that such information is+ necessary to install and execute a modified version of the+ Combined Work produced by recombining or relinking the+ Application with a modified version of the Linked Version. (If+ you use option 4d0, the Installation Information must accompany+ the Minimal Corresponding Source and Corresponding Application+ Code. If you use option 4d1, you must provide the Installation+ Information in the manner specified by section 6 of the GNU GPL+ for conveying Corresponding Source.)++ 5. Combined Libraries.++ You may place library facilities that are a work based on the+Library side by side in a single library together with other library+facilities that are not Applications and are not covered by this+License, and convey such a combined library under terms of your+choice, if you do both of the following:++ a) Accompany the combined library with a copy of the same work based+ on the Library, uncombined with any other library facilities,+ conveyed under the terms of this License.++ b) Give prominent notice with the combined library that part of it+ is a work based on the Library, and explaining where to find the+ accompanying uncombined form of the same work.++ 6. Revised Versions of the GNU Lesser General Public License.++ The Free Software Foundation may publish revised and/or new versions+of the GNU Lesser General Public License from time to time. Such new+versions will be similar in spirit to the present version, but may+differ in detail to address new problems or concerns.++ Each version is given a distinguishing version number. If the+Library as you received it specifies that a certain numbered version+of the GNU Lesser General Public License "or any later version"+applies to it, you have the option of following the terms and+conditions either of that published version or of any later version+published by the Free Software Foundation. If the Library as you+received it does not specify a version number of the GNU Lesser+General Public License, you may choose any version of the GNU Lesser+General Public License ever published by the Free Software Foundation.++ If the Library as you received it specifies that a proxy can decide+whether future versions of the GNU Lesser General Public License shall+apply, that proxy's public statement of acceptance of any version is+permanent authorization for you to choose that version for the+Library.
+ README.md view
@@ -0,0 +1,12 @@+# RLP: Recursive Length Prefix++[](https://travis-ci.org/Jasagredo/RLP)+++This package implements the Recursive Length Prefix protocol defined+in the [Ethereum Yellow Paper](https://ethereum.github.io/yellowpaper/paper.pdf)+used for serializing and deserializing structures into an array of bytes.++For more information, please check the haddock documentation (can be generated running `cabal haddock` on the root of the repo).++Feel free to contribute.
+ RLP.cabal view
@@ -0,0 +1,47 @@+Name: RLP+Version: 1.0.0+Author: Javier Sagredo <jasagredo@gmail.com>+Maintainer: Javier Sagredo <jasagredo@gmail.com>+License: LGPL-3+License-File: LICENSE+Homepage: https://github.com/jasagredo/RLP+Synopsis: RLP serialization as defined in Ethereum Yellow Paper+Description: RLP serialization as defined in Ethereum Yellow Paper+ allowing the encoding of arbitrary data and the later+ decoding of such data. Intended for use with Ethereum+ transactions or data over the network.+Category: Data, Parsing+Cabal-Version: >= 1.10+Build-Type: Simple+Extra-Source-Files: README.md, ChangeLog.md+Tested-With: GHC == 8.4.1, GHC == 8.6.1++Library+ Default-Language: Haskell2010+ HS-Source-Dirs: src+ GHC-Options: -Wall+ Exposed-Modules: Data.Serialize.RLP+ Other-Modules: Data.Serialize.RLP.Internal+ Build-Depends: base >= 4 && < 5+ , binary >= 0.8 && < 0.9+ , bytestring >= 0.10 && < 0.11 ++Test-Suite spec+ Type: exitcode-stdio-1.0+ Default-Language: Haskell2010+ Hs-Source-Dirs: src+ , test+ Ghc-Options: -Wall+ Main-Is: Spec.hs+ Other-Modules: Data.Serialize.RLPSpec+ , Data.Serialize.RLP+ , Data.Serialize.RLP.Internal+ Build-Depends: base+ , hspec >= 2.6 && < 2.7+ , binary+ , bytestring+++Source-Repository head+ Type: git+ Location: git://github.com/jasagredo/RLP.git
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/Data/Serialize/RLP.hs view
@@ -0,0 +1,228 @@+-- |+-- Module : Data.Serialize.RLP+-- License : LGPL-3 (see LICENSE)+--+-- Maintainer : Javier Sagredo <jasataco@gmail.com>+-- Stability : stable+--+-- An implementation of the Recursive Length Prefix method+-- as described in the Yellow Paper <https://ethereum.github.io/yellowpaper/paper.pdf>.+--+-- To actually use this module, the type that is going to+-- be encoded has to be instance of RLPSerialize defining+-- 'toRLP' and 'fromRLP'.+--------------------------------------------------------------------------------++module Data.Serialize.RLP (+ -- * The RLP Type+ RLPT(..),+ -- ** Subtleties+ -- $subtleties++ -- * Helper Int functions+ toBigEndian,+ toBigEndianS,+ fromBigEndian,+ fromBigEndianS,++ -- * Helper String functions+ toByteString,+ toByteStringS,+ fromByteString,+ fromByteStringS,+ + -- * The RLPSerialize class+ RLPSerialize(..)+ -- * Example+ -- $example+ +) where++import Data.Serialize.RLP.Internal++import qualified Data.ByteString as DBS+import qualified Data.ByteString.Lazy as DBSL++--------------------------------------------------------------------------------++-- $subtleties+-- The idea of transforming a custom type into RLPT is to+-- preserve the original structure as far as possible. For example,+-- suppose we have a data structure:+--+-- > data Name = (String, String) -- represents the first and last name of a Person+-- > data Person = Person Name Int -- represents the whole name of a Person and its age+--+-- Then the desired output of the transformation of a Person value to RLPT should be (pseudocode):+--+-- > RLPL [ RLPL [ RLPB, RLPB ], RLPB ]+--+-- This way the structure is clearly preserved. Eventhough this does not have+-- to be true as the transformation to RLPL is defined by the user and a custom+-- process can be implemented, it is advised to follow this guideline for better+-- understanding of the generated code.+--+-- It is important to remark that although it can't be imposed, it doesn't make sense to try+-- to transform to RLP types with more than one constructor. The transformation should encode+-- a way to find out which of the constructors belongs to the data so not only data is being+-- encoded in the result, also information about the structure futher than the actual length+-- prefixes. That's why it only makes sense to transform to RLP types with just one constructor.++--------------------------------------------------------------------------------++-- | The 'RLPSerialize' class provides functions for transforming values to RLPT structures.+-- For encoding and decoding values with the RLP protocol, 'toRLP' and 'fromRLP' have to+-- be implemented.+--+-- Instances of RLPSerialize have to satisfy the following property:+--+-- > fromRLP . toRLP == id+--+-- In such case, it can be assured with the default definition that:+--+-- > rlpDecode . rlpEncode == id+--+-- RLPSerialize makes use of the Get and Put classes together with a set of+-- custom serializations for encoding and decoding RLPT data.+class RLPSerialize a where+ -- | Transform a value to the 'RLPT' structure that best fits its internal structure+ toRLP :: a -> RLPT+ -- | Transform an 'RLPT' structure back into the value it represents+ fromRLP :: RLPT -> a++ -- | Transform a value to an 'RLPT' structure and then encode it following the+ -- RLP standard.+ rlpEncode :: a -> DBSL.ByteString+ rlpEncode = rlpEncodeI . toRLP++ -- | Transform a ByteString to an 'RLPT' structure following the RLP standard and+ -- then transform it to the original type.+ rlpDecode :: DBSL.ByteString -> Maybe a+ rlpDecode x = maybe Nothing fromRLP $ rlpDecodeI x++ {-# MINIMAL toRLP, fromRLP #-}++-- RLPT values don't have to be transformed as they already are RLPT+instance RLPSerialize RLPT where+ toRLP = id++ fromRLP = id++-- ByteStrings just have to be encapsulated+-- Also, it only makes sense to disencapsulate from a ByteString+instance RLPSerialize DBS.ByteString where+ toRLP = RLPB++ fromRLP (RLPB b) = b+ fromRLP _ = undefined++-- Ints have to be transformed into its Big-endian form+-- and then they are treated as ByteStrings.+-- The same applies for the inverse transformation. They+-- are treated as ByteStrings and then interpreted as a+-- Big-endian encoded Int.+instance RLPSerialize Int where+ toRLP = toRLP . toBigEndianS++ fromRLP = fromBigEndianS . (fromRLP :: RLPT -> DBS.ByteString)++-- Serializing lists implies making a list with the serialization+-- of each element+instance RLPSerialize a => RLPSerialize [a] where+ toRLP = RLPL . map toRLP++ fromRLP (RLPL x) = map fromRLP x+ fromRLP _ = undefined++-- Bools are serialized as [0] or [1] in a ByteArray+-- THIS IS AN ASUMPTION considering Bool equivalent to+-- integers in the range 0..1+instance RLPSerialize Bool where+ toRLP True = RLPB $ toByteStringS "\SOH"+ toRLP False = RLPB $ toByteStringS "\NUL"++ fromRLP x+ | x == toRLP True = True+ | otherwise = False++-- Chars are just length-one strings+instance RLPSerialize Char where+ toRLP = RLPB . toByteStringS . (: [])++ fromRLP (RLPB x) = head $ fromByteStringS x+ fromRLP _ = undefined++-- Tuples are transformed into Lists+instance (RLPSerialize a, RLPSerialize b) => RLPSerialize (a, b) where+ toRLP (x, y) = RLPL [toRLP x, toRLP y]++ fromRLP (RLPL [x, y]) = (fromRLP x, fromRLP y)+ fromRLP _ = undefined++instance (RLPSerialize a, RLPSerialize b, RLPSerialize c) => RLPSerialize (a, b, c) where+ toRLP (x, y, z) = RLPL [toRLP x, toRLP y, toRLP z]++ fromRLP (RLPL [x, y, z]) = (fromRLP x, fromRLP y, fromRLP z)+ fromRLP _ = undefined++instance (RLPSerialize a, RLPSerialize b, RLPSerialize c, RLPSerialize d) => RLPSerialize (a, b, c, d) where+ toRLP (a1, a2, a3, a4) = RLPL [toRLP a1, toRLP a2, toRLP a3, toRLP a4]++ fromRLP (RLPL [a1, a2, a3, a4]) = (fromRLP a1, fromRLP a2, fromRLP a3, fromRLP a4)+ fromRLP _ = undefined++instance (RLPSerialize a, RLPSerialize b, RLPSerialize c, RLPSerialize d, RLPSerialize e) => RLPSerialize (a, b, c, d, e) where+ toRLP (a1, a2, a3, a4, a5) = RLPL [toRLP a1, toRLP a2, toRLP a3, toRLP a4, toRLP a5]++ fromRLP (RLPL [a1, a2, a3, a4, a5]) = (fromRLP a1, fromRLP a2, fromRLP a3, fromRLP a4, fromRLP a5)+ fromRLP _ = undefined++instance (RLPSerialize a, RLPSerialize b, RLPSerialize c, RLPSerialize d, RLPSerialize e, RLPSerialize f) => RLPSerialize (a, b, c, d, e, f) where+ toRLP (a1, a2, a3, a4, a5, a6) = RLPL [toRLP a1, toRLP a2, toRLP a3, toRLP a4, toRLP a5, toRLP a6]++ fromRLP (RLPL [a1, a2, a3, a4, a5, a6]) = (fromRLP a1, fromRLP a2, fromRLP a3, fromRLP a4, fromRLP a5, fromRLP a6)+ fromRLP _ = undefined++-- Needed by the default rlpDecode implementation+instance RLPSerialize a => RLPSerialize (Maybe a) where+ toRLP = undefined++ fromRLP = Just . fromRLP++--------------------------------------------------------------------------------++-- $example+-- For a full example, we reproduce the implementation of the Person type as in the+-- subtleties section.+--+-- First of all, we define the type:+--+-- > type Name = (String, String)+-- > data Person = Person {+-- > name :: Name,+-- > age :: Int+-- > } deriving (Show)+--+-- Then we have to make it an instance of RLPSerialize:+-- +-- > instance RLPSerialize Person where+-- > toRLP p = RLPL [+-- > RLPL [+-- > toRLP . toByteStringS . fst . name $ p,+-- > toRLP . toByteStringS . snd . name $ p+-- > ],+-- > toRLP . age $ p]+-- > +-- > fromRLP (RLPL [ RLPL [ RLPB a, RLPB b ], RLPB c ]) =+-- > Person (fromByteStringS a, fromByteStringS b) (fromBigEndianS c :: Int)+-- +-- This way, if the decoding gives rise to other structure than the expected, a runtime+-- exception will be thrown by the pattern matching. We can now use our decoder and encoder+-- with our custom type:+--+-- > p = Person ("John", "Snow") 33+-- > e = rlpEncode p+-- > -- "\204\202\132John\132Snow!" ~ [204,202,132,74,111,104,110,132,83,110,111,119,33]+-- > rlpDecode e :: Maybe Person+-- > -- Just (Person {name = ("John","Snow"), age = 33})+--
+ src/Data/Serialize/RLP/Internal.hs view
@@ -0,0 +1,188 @@+module Data.Serialize.RLP.Internal (+ RLPEncodeable(..),++ -- * Helper Int functions+ toBigEndian,+ toBigEndianS,+ fromBigEndian,+ fromBigEndianS,++ -- * Helper String functions+ toByteString,+ toByteStringS,+ fromByteString,+ fromByteStringS,+ + RLPT(..)+ ) where++import Data.Binary.Get+import Data.Binary.Put++import qualified Data.ByteString as DBS+import qualified Data.ByteString.Char8 as DBSC+import qualified Data.ByteString.Lazy as DBSL+import qualified Data.ByteString.Lazy.Char8 as DBSLC++--------------------------------------------------------------------------------++-- | The 'RLPT' type represents the result of transforming the+-- initial data into its byte-array representation, taking in+-- account the structure of the fields.+--+-- Fields that can't be directly transformed into a ByteString (such+-- as a type with several fields) should generate a list with the+-- representations of its fields (using the RLPL constructor).+--+-- RLPT represents the T type defined in the Ethereum Yellowpaper for+-- defining the RLP protocol.+data RLPT = RLPL [RLPT] | RLPB DBS.ByteString+ deriving (Show, Eq) -- just for understanding pourposes and for checking with hspec++--------------------------------------------------------------------------------++toBigEndian :: Int -> DBSL.ByteString+toBigEndian = DBSLC.dropWhile (== '\NUL') . runPut . putInt64be . fromIntegral++-- | Strict version of 'toBigEndian'+toBigEndianS :: Int -> DBS.ByteString+toBigEndianS = DBSL.toStrict . toBigEndian++fromBigEndian :: DBSL.ByteString -> Int+fromBigEndian bs = fromIntegral . runGet getInt64be $ bs'+ where bs' = case () of+ _ | DBSL.length bs >= 8 -> bs+ | otherwise -> DBSLC.append (DBSLC.pack $ b) bs+ where b = take (8 - (fromIntegral . DBSL.length $ bs)) (repeat '\NUL')+ +-- | Strict version of 'fromBigEndian'+fromBigEndianS :: DBS.ByteString -> Int+fromBigEndianS = fromBigEndian . DBSL.fromStrict++toByteString :: String -> DBSL.ByteString+toByteString = DBSLC.pack++-- | Strict version of 'toByteString'+toByteStringS :: String -> DBS.ByteString+toByteStringS = DBSC.pack++fromByteString :: DBSL.ByteString -> String+fromByteString = DBSLC.unpack++-- | Strict version of 'fromByteString'+fromByteStringS :: DBS.ByteString -> String+fromByteStringS = DBSC.unpack++-- | Internal function for spliting the array in chunks of bytes+rlpSplit :: DBSL.ByteString -> [DBSL.ByteString]+rlpSplit x+ | DBSL.null x = []+ | DBSL.head x < 192 =+ case () of+ _ | DBSL.head x < 128 -> (DBSL.singleton . DBSL.head $ x) : (rlpSplit $ DBSL.tail x)+ | DBSL.head x < 183 ->+ let size = (fromIntegral $ DBSL.head x) - 128 :: Int in+ let total = size + 1 in+ (DBSL.take (fromIntegral total) x) : (rlpSplit $ DBSL.drop (fromIntegral total) x)+ | otherwise ->+ let sizeSize = (fromIntegral $ DBSL.head x) - 183 :: Int in+ let size = fromBigEndian . DBSL.take (fromIntegral sizeSize) . DBSL.tail $ x :: Int in+ let total = sizeSize + size + 1 :: Int in + (DBSL.take (fromIntegral total) x) : (rlpSplit $ DBSL.drop (fromIntegral total) x)+ | DBSL.head x == 192 = (DBSL.singleton $ DBSL.head x) : (rlpSplit $ DBSL.tail x)+ | DBSL.head x < 247 =+ let size = (fromIntegral $ DBSL.head x) - 192 :: Int in+ let total = size + 1 in+ (DBSL.take (fromIntegral total) x) : (rlpSplit $ DBSL.drop (fromIntegral total) x)+ | otherwise =+ let sizeSize = (fromIntegral $ DBSL.head x) - 247 :: Int in+ let size = fromBigEndian . DBSL.take (fromIntegral sizeSize) . DBSL.tail $ x :: Int in+ let total = sizeSize + size + 1 :: Int in + (DBSL.take (fromIntegral total) x) : (rlpSplit $ DBSL.drop (fromIntegral total) x)++--------------------------------------------------------------------------------++-- | The 'RLPEncodeable' class groups the RLPT, ByteString and Int types+-- for transforming them into ByteStrings.+--+-- This class defines only the functions for the types explicitly shown on the+-- Yellow Paper. This class intends to be internal and not be used outside the+-- RLPSerialize class.+class RLPEncodeable a where+ -- Use Put to encode the structure+ rlpEncodeI' :: a -> Put++ -- Mainly run rlpEncodeI'+ rlpEncodeI :: a -> DBSL.ByteString+ rlpEncodeI = runPut . rlpEncodeI'++ -- Use Get to parse the structure+ rlpDecodeI' :: Get a++ -- Mainly run rlpDecodeI'+ rlpDecodeI :: DBSL.ByteString -> Maybe a+ rlpDecodeI x = let r = runGetOrFail rlpDecodeI' x in+ case r of+ Left _ -> Nothing+ Right (_, _, s) -> Just s++--------------------------------------------------------------------------------+-- Instances++instance RLPEncodeable RLPT where+ rlpEncodeI' (RLPB bs) = rlpEncodeI' bs+ rlpEncodeI' (RLPL t) = case () of+ _ | DBSL.length dat < 56 -> (putWord8 . fromIntegral $ 192 + DBSL.length dat)+ <> (putLazyByteString dat) + | otherwise -> (putWord8 . fromIntegral $ 247 + DBSL.length l)+ <> (putLazyByteString l)+ <> (putLazyByteString dat)+ where l = toBigEndian . fromIntegral . DBSL.length $ dat+ where dat = DBSL.concat . map rlpEncodeI $ t++ rlpDecodeI' = do+ i <- getWord8+ case () of + _ | i < 192 -> do -- ByteArray+ ls <- getRemainingLazyByteString+ return . RLPB . (\(Just x) -> x) . rlpDecodeI $ DBSL.cons i ls+ | i == 192 -> do -- Empty list+ return $ RLPL []+ | i < 247 -> do -- Small list+ ls <- getLazyByteString . fromIntegral $ i - 192+ return $ RLPL . map ((\(Just x) -> x) . rlpDecodeI) . rlpSplit $ ls+ | otherwise -> do -- Big List+ ls <- getLazyByteString . fromIntegral $ i - 247+ let k = fromBigEndian ls+ ls' <- getLazyByteString . fromIntegral $ k+ return $ RLPL . map ((\(Just x) -> x) . rlpDecodeI) . rlpSplit $ ls'++instance RLPEncodeable DBS.ByteString where+ rlpEncodeI' bs+ | (DBS.length bs == 1) && (DBS.head bs < (fromIntegral (128 :: Integer))) = putByteString bs+ | DBS.length bs < 56 = (putWord8 . fromIntegral $ 128 + DBS.length bs)+ <> (putByteString bs)+ | otherwise = (putWord8 . fromIntegral $ 183 + DBSL.length l)+ <> (putLazyByteString l)+ <> (putByteString bs)+ where l = toBigEndian . DBS.length $ bs++ rlpDecodeI' = do+ i <- getWord8+ case () of+ _ | i < 128 -> return $ DBS.singleton i+ | i < 183 -> do+ ls <- getByteString . fromIntegral $ i - 128+ return ls+ | i < 192 -> do+ sbe <- getLazyByteString . fromIntegral $ i - 183+ ls <- getByteString . fromBigEndian $ sbe+ return ls+ | otherwise -> undefined++instance RLPEncodeable Int where+ rlpEncodeI' = rlpEncodeI' . DBSL.toStrict . toBigEndian++ rlpDecodeI' = do+ b <- rlpDecodeI' :: Get DBS.ByteString+ return . fromBigEndian . DBSL.fromStrict $ b
+ test/Data/Serialize/RLPSpec.hs view
@@ -0,0 +1,131 @@+module Data.Serialize.RLPSpec (main, spec) where++import Test.Hspec++import Data.Serialize.RLP++import Data.ByteString as DBS+import Data.ByteString.Lazy as DBSL++main :: IO ()+main = hspec spec++spec :: Spec+spec = do+ context "when encoding _empty_ values" $ do+ it "should encode the null string" $ do+ (DBSL.unpack . rlpEncode . toByteStringS $ "") `shouldBe` [ 0x80 ]++ it "should decode it back" $ do+ (rlpDecode . rlpEncode . toByteStringS $ "" :: Maybe DBS.ByteString) `shouldBe` (Just $ toByteStringS "")++ it "should encode the empty list" $ do+ (DBSL.unpack . rlpEncode . RLPL $ []) `shouldBe` [ 0xc0 ]++ it "should decode it back" $ do+ (rlpDecode . rlpEncode . RLPL $ [] :: Maybe RLPT) `shouldBe` (Just $ RLPL []) ++ it "should encode the integer Zero" $ do+ (DBSL.unpack . rlpEncode . toRLP $ (0 :: Int)) `shouldBe` [ 0x80 ]++ it "should decode it back" $ do+ (maybe Nothing fromRLP $ rlpDecode . rlpEncode . toRLP $ (0 :: Int) :: Maybe Int) `shouldBe` (Just 0)++ context "when encoding ByteArrays" $ do+ it "should encode an array of length 1 and small value" $ do+ (DBSL.unpack . rlpEncode . toByteStringS $ "\NUL") `shouldBe` [ 0x00 ]++ it "should decode it back" $ do+ (rlpDecode . rlpEncode . toByteStringS $ "\NUL" :: Maybe DBS.ByteString) `shouldBe` (Just $ toByteStringS "\NUL")++ it "should encode an array of length 1 and big value" $ do+ (DBSL.unpack . rlpEncode . toByteStringS $ "\150") `shouldBe` [ 0x81, 0x96 ]++ it "should decode it back" $ do+ (rlpDecode . rlpEncode . toByteStringS $ "\150" :: Maybe DBS.ByteString) `shouldBe` (Just $ toByteStringS "\150")++ it "should encode an array of length bigger than one and smaller than 56" $ do+ (DBSL.unpack . rlpEncode . toByteStringS $ "\SOH\SOH") `shouldBe` [ 0x82, 0x01, 0x01 ]++ it "should decode it back" $ do+ (rlpDecode . rlpEncode . toByteStringS $ "\SOH\SOH" :: Maybe DBS.ByteString) `shouldBe` (Just $ toByteStringS "\SOH\SOH")++ it "should encode an array of length bigger than 56 with _small_ length" $ do+ (DBSL.unpack . rlpEncode . toByteStringS . Prelude.map toEnum . Prelude.take 60 . Prelude.repeat $ 1)+ `shouldBe` ([ 0xb8, 0x3c ] ++ (Prelude.take 60 . Prelude.repeat $ 0x01))++ it "should decode it back" $ do+ (rlpDecode . rlpEncode . toByteStringS . Prelude.map toEnum . Prelude.take 60 . Prelude.repeat $ 1 :: Maybe DBS.ByteString)+ `shouldBe` (Just $ toByteStringS . Prelude.map toEnum . Prelude.take 60 . Prelude.repeat $ 0x01)++ it "should encode an array of length bigger than 56 with _big_ length" $ do+ (DBSL.unpack . rlpEncode . toByteStringS . Prelude.map toEnum . Prelude.take 43520 . Prelude.repeat $ 1)+ `shouldBe` ([ 0xb9, 0xaa, 0x00 ] ++ (Prelude.take 43520 . Prelude.repeat $ 0x01))++ it "should decode it back" $ do+ (rlpDecode . rlpEncode . toByteStringS . Prelude.map toEnum . Prelude.take 43520 . Prelude.repeat $ 1 :: Maybe DBS.ByteString)+ `shouldBe` (Just $ toByteStringS . Prelude.map toEnum . Prelude.take 43520 . Prelude.repeat $ 0x01)++ context "when encoding RLP structures" $ do+ it "should encode RLP structures with less than 56 bytes and one element" $ do+ (DBSL.unpack . rlpEncode $ RLPL [ RLPB . toByteStringS $ "\NUL" ]) `shouldBe` [ 0xc1, 0x00 ]+ + it "should decode it back" $ do+ (rlpDecode . rlpEncode $ RLPL [ RLPB . toByteStringS $ "\NUL" ] :: Maybe RLPT)+ `shouldBe` (Just $ RLPL [ RLPB . toByteStringS $ "\NUL" ])++ it "should encode RLP structures with less than 56 bytes and more than one element" $ do+ (DBSL.unpack . rlpEncode $ RLPL [ RLPB . toByteStringS $ "\NUL" , RLPL [ RLPB . toByteStringS $ "\NUL" ] ])+ `shouldBe` [ 0xc3, 0x00, 0xc1, 0x00 ]++ it "should decode it back" $ do+ (rlpDecode . rlpEncode $ RLPL [ RLPB . toByteStringS $ "\NUL" , RLPL [ RLPB . toByteStringS $ "\NUL" ] ] :: Maybe RLPT)+ `shouldBe` (Just $ RLPL [ RLPB . toByteStringS $ "\NUL" , RLPL [ RLPB . toByteStringS $ "\NUL" ] ])++ it "should encode RLP structures with more than 56 bytes and one element" $ do+ (DBSL.unpack . rlpEncode $ RLPL [ RLPB . toByteStringS . Prelude.map toEnum . Prelude.take 60 . Prelude.repeat $ 1 ])+ `shouldBe` ([ 0xf8, 0x3e, 0xb8, 0x3c ] ++ (Prelude.map toEnum . Prelude.take 60 . Prelude.repeat $ 0x01))++ it "should decode it back" $ do+ (rlpDecode . rlpEncode $ RLPL [ RLPB . toByteStringS . Prelude.map toEnum . Prelude.take 60 . Prelude.repeat $ 1 ] :: Maybe RLPT)+ `shouldBe` (Just $ RLPL [ RLPB . toByteStringS . Prelude.map toEnum . Prelude.take 60 . Prelude.repeat $ 1 ])++ it "should encode RLP structures with more than 56 bytes and more than one element" $ do+ (DBSL.unpack . rlpEncode $ RLPL [ RLPB . toByteStringS $ "\NUL" , RLPL [ RLPB . toByteStringS . Prelude.map toEnum . Prelude.take 60 . Prelude.repeat $ 1 ] ])+ `shouldBe` ([ 0xf8, 0x41, 0x00, 0xf8, 0x3e, 0xb8, 0x3c ] ++ (Prelude.map toEnum . Prelude.take 60 . Prelude.repeat $ 0x01))++ it "should decode it back" $ do+ (rlpDecode . rlpEncode $ RLPL [ RLPB . toByteStringS $ "\NUL" , RLPL [ RLPB . toByteStringS . Prelude.map toEnum . Prelude.take 60 . Prelude.repeat $ 1 ] ] :: Maybe RLPT)+ `shouldBe` (Just $ RLPL [ RLPB . toByteStringS $ "\NUL" , RLPL [ RLPB . toByteStringS . Prelude.map toEnum . Prelude.take 60 . Prelude.repeat $ 1 ] ])+++ context "when running Ethereum examples" $ do+ it "should encode dog" $ do+ (DBSL.unpack . rlpEncode . toByteStringS $ "dog") `shouldBe` [ 0x83, 0x64, 0x6f, 0x67 ]++ it "should decode it back" $ do+ (rlpDecode . rlpEncode . toByteStringS $ "dog" :: Maybe DBS.ByteString) `shouldBe` (Just $ toByteStringS "dog")++ it "should encode [ cat, dog ]" $ do+ (DBSL.unpack . rlpEncode . RLPL . Prelude.map (RLPB . toByteStringS) $ [ "cat", "dog" ])+ `shouldBe` [ 0xc8, 0x83, 0x63, 0x61, 0x74, 0x83, 0x64, 0x6f, 0x67 ]++ it "should decode it back" $ do+ (rlpDecode . rlpEncode . RLPL . Prelude.map (RLPB . toByteStringS) $ [ "cat", "dog" ] :: Maybe RLPT)+ `shouldBe` (Just $ RLPL . Prelude.map (RLPB . toByteStringS) $ [ "cat", "dog" ])++ it "should encode the set theoretical representation" $ do+ (DBSL.unpack . rlpEncode . toRLP $ RLPL [ RLPL [] , RLPL [ RLPL [] ] , RLPL [ RLPL [] , RLPL [ RLPL [] ] ] ])+ `shouldBe` [ 0xc7, 0xc0, 0xc1, 0xc0, 0xc3, 0xc0, 0xc1, 0xc0 ]++ it "should decode it back" $ do+ (rlpDecode . rlpEncode . toRLP $ RLPL [ RLPL [] , RLPL [ RLPL [] ] , RLPL [ RLPL [] , RLPL [ RLPL [] ] ] ] :: Maybe RLPT)+ `shouldBe` (Just $ RLPL [ RLPL [] , RLPL [ RLPL [] ] , RLPL [ RLPL [] , RLPL [ RLPL [] ] ] ])++ it "should encode LOREM IPSUM" $ do+ (DBSL.unpack . rlpEncode . toByteStringS $ "Lorem ipsum dolor sit amet, consectetur adipisicing elit")+ `shouldBe` [ 0xb8, 0x38, 0x4c, 0x6f, 0x72, 0x65, 0x6d, 0x20, 0x69, 0x70, 0x73, 0x75, 0x6d, 0x20, 0x64, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x73, 0x69, 0x74, 0x20, 0x61, 0x6d, 0x65, 0x74, 0x2c, 0x20, 0x63, 0x6f, 0x6e, 0x73, 0x65, 0x63, 0x74, 0x65, 0x74, 0x75, 0x72, 0x20, 0x61, 0x64, 0x69, 0x70, 0x69, 0x73, 0x69, 0x63, 0x69, 0x6e, 0x67, 0x20, 0x65, 0x6c, 0x69, 0x74 ]++ it "should decode it back" $ do+ (rlpDecode . rlpEncode . toByteStringS $ "Lorem ipsum dolor sit amet, consectetur adipisicing elit" :: Maybe DBS.ByteString)+ `shouldBe` (Just $ toByteStringS $ "Lorem ipsum dolor sit amet, consectetur adipisicing elit")
+ test/Spec.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}