diff --git a/.weeder.yaml b/.weeder.yaml index 4d2384a2efa..df9afcc93c4 100644 --- a/.weeder.yaml +++ b/.weeder.yaml @@ -34,3 +34,13 @@ - name: Module not compiled - module: Cardano.Launcher.Windows +- package: + - name: cardano-wallet-jormungandr + - section: + - name: test:unit + - message: + - name: Weeds exported + - module: + - name: Spec + - identifier: main + diff --git a/lib/jormungandr/cardano-wallet-jormungandr.cabal b/lib/jormungandr/cardano-wallet-jormungandr.cabal index d8113ba15ab..4d42b5d5e35 100644 --- a/lib/jormungandr/cardano-wallet-jormungandr.cabal +++ b/lib/jormungandr/cardano-wallet-jormungandr.cabal @@ -34,12 +34,12 @@ library build-depends: base , cardano-wallet-core - , binary - , bytestring - , cardano-crypto - , cryptonite - , digest - , memory + , fmt +-- , binary +-- , bytestring +-- , cardano-crypto +-- , cryptonite +-- , digest , text , text-class hs-source-dirs: @@ -49,8 +49,6 @@ library Cardano.Wallet.Binary.Jormungandr Cardano.Wallet.Compatibility.Jormungandr Cardano.Wallet.Transaction.Jormungandr - Data.Packfile - Servant.Extra.ContentTypes test-suite unit default-language: @@ -67,23 +65,11 @@ test-suite unit -Werror build-depends: base - , aeson - , base58-bytestring , bytestring - , cardano-crypto , cardano-wallet-core , cardano-wallet-jormungandr - , cborg - , containers - , digest - , fmt - , generic-arbitrary - , hspec - , hspec-golden-aeson , memory - , QuickCheck - , text-class - , transformers + , hspec type: exitcode-stdio-1.0 hs-source-dirs: diff --git a/lib/jormungandr/src/Cardano/Wallet/Binary/Jormungandr.hs b/lib/jormungandr/src/Cardano/Wallet/Binary/Jormungandr.hs index 66f58fa25d8..b41fec73eb7 100644 --- a/lib/jormungandr/src/Cardano/Wallet/Binary/Jormungandr.hs +++ b/lib/jormungandr/src/Cardano/Wallet/Binary/Jormungandr.hs @@ -10,5 +10,3 @@ module Cardano.Wallet.Binary.Jormungandr ( ) where - - diff --git a/lib/jormungandr/src/Data/Packfile.hs b/lib/jormungandr/src/Data/Packfile.hs deleted file mode 100644 index 9eb811ee5fb..00000000000 --- a/lib/jormungandr/src/Data/Packfile.hs +++ /dev/null @@ -1,93 +0,0 @@ --- | --- Copyright: © 2018-2019 IOHK --- License: MIT --- --- Decoder for the rust-cardano packfile format. --- --- A pack file is a collection of bytestring blobs. --- --- The reference implementation is in --- . - -module Data.Packfile - ( decodePackfile - , PackfileError (..) - ) where - -import Prelude - -import Control.Monad - ( guard, when ) -import Data.Binary.Get - ( Get, getByteString, getInt32be, isEmpty, runGetOrFail ) -import Data.Int - ( Int32 ) - -import qualified Data.ByteString as BS -import qualified Data.ByteString.Lazy as BL - --- | Things related to the file format that can go wrong when decoding a pack --- file. -data PackfileError - = MissingMagicError - | WrongFileTypeError - | VersionTooOldError - | VersionTooNewError - | BlobDecodeError String - deriving (Show, Eq) - --- | Decode a Cardano version 1 pack file. The blobs are returned as a --- list. Decoding is not incremental, and all data is stored in memory. -decodePackfile :: BL.ByteString -> Either PackfileError [BS.ByteString] -decodePackfile pf = case runGetOrFail getHeader pf of - Left _ -> Left MissingMagicError - Right (rest, _, hdr) -> case checkHeader hdr of - Left e -> Left e - Right () -> case runGetOrFail getBlobs rest of - Left (_, _, msg) -> Left (BlobDecodeError msg) - Right (_, _, res) -> Right res - -data Header = Header !BS.ByteString !Int - -getHeader :: Get Header -getHeader = do - magic <- getByteString 8 - guard (magic == "\254CARDANO") - fileType <- getByteString 4 - version <- getInt32be - pure $! Header fileType (fromIntegral version) - --- The current version of a rust-cardano packfile is 1, and we do not know how --- to decode any other version. -checkHeader :: Header -> Either PackfileError () -checkHeader = checkHeader' "PACK" 1 1 - -checkHeader' :: BS.ByteString -> Int -> Int -> Header -> Either PackfileError () -checkHeader' expFileType minVersion maxVersion (Header fileType version) - | fileType /= expFileType = Left WrongFileTypeError - | version < minVersion = Left VersionTooOldError - | version > maxVersion = Left VersionTooNewError - | otherwise = Right () - -getBlobs :: Get [BS.ByteString] -getBlobs = do - empty <- isEmpty - if empty - then pure [] - else do - blob <- getBlob - blobs <- getBlobs - pure (blob:blobs) - -getBlob :: Get BS.ByteString -getBlob = do - size <- getInt32be - when (size >= 20000000) $ - -- limit blob size to 20MB to avoid consuming all memory - fail ("read block of size: " <> show size) - bytes <- getByteString (fromIntegral size) - _pad <- getByteString (npad size) - pure bytes - where - npad :: Int32 -> Int - npad n = -((fromIntegral n) `mod` (-4)) diff --git a/lib/jormungandr/src/Servant/Extra/ContentTypes.hs b/lib/jormungandr/src/Servant/Extra/ContentTypes.hs deleted file mode 100644 index 914e1501a1a..00000000000 --- a/lib/jormungandr/src/Servant/Extra/ContentTypes.hs +++ /dev/null @@ -1,98 +0,0 @@ -{-# LANGUAGE DataKinds #-} -{-# LANGUAGE FlexibleInstances #-} -{-# LANGUAGE MultiParamTypeClasses #-} -{-# LANGUAGE ScopedTypeVariables #-} - --- | Extra content types for Servant. --- -module Servant.Extra.ContentTypes - ( ComputeHash - , CBOR - , FromCBOR (..) - , Hash (..) - , Packed - , WithHash (..) - ) where - -import Prelude - -import Crypto.Hash - ( Digest, hashWith ) -import Crypto.Hash.IO - ( HashAlgorithm (..) ) -import Data.ByteArray.Encoding - ( Base (Base16), convertToBase ) -import Data.Packfile - ( decodePackfile ) -import Data.Proxy - ( Proxy (..) ) -import Data.Text.Encoding - ( decodeUtf8 ) -import Network.HTTP.Media - ( (//) ) -import Servant.API - ( Accept (..), MimeUnrender (..), ToHttpApiData (..) ) - -import qualified Codec.CBOR.Decoding as CBOR -import qualified Codec.CBOR.Read as CBOR -import qualified Data.ByteString.Lazy as BL - --- | Represents a CBOR (Concise Binary Object Representation) object. --- --- See RFC 7049 (http://cbor.io/) for further details. --- -data CBOR - --- | The class of types that can be converted to from CBOR. --- -class FromCBOR a where - fromCBOR :: CBOR.Decoder s a - -instance Accept CBOR where - contentType _ = "text" // "plain" - -instance FromCBOR a => MimeUnrender CBOR a where - mimeUnrender _ bl = either - (Left . show) - (Right . snd) - (CBOR.deserialiseFromBytes fromCBOR bl) - --- | Represents a piece of binary data for which a hash value should be --- calculated before performing any further deserialization. --- -data ComputeHash algorithm a - --- | Represents the result of hashing a piece of data. --- -newtype Hash algorithm a = Hash (Digest algorithm) - -instance ToHttpApiData (Hash algorithm a) where - toUrlPiece (Hash digest) = decodeUtf8 $ convertToBase Base16 digest - --- | Represents a piece of data with an accompanying hash value. -data WithHash algorithm a = WithHash - { getHash :: Digest algorithm - , getValue :: a - } deriving Show - -instance Accept a => Accept (ComputeHash algorithm a) where - contentType _ = contentType (Proxy :: Proxy a) - -instance forall a b alg . (MimeUnrender a b, HashAlgorithm alg) => - MimeUnrender (ComputeHash alg a) (WithHash alg b) where - mimeUnrender _ bl = - WithHash (hashWith (undefined :: alg) $ BL.toStrict bl) - <$> mimeUnrender (Proxy :: Proxy a) bl - --- | Represents something that has been packed with the Cardano packfile format. --- -data Packed a - -instance Accept a => Accept (Packed a) where - contentType _ = "text" // "plain" - -instance forall a b . MimeUnrender a b => MimeUnrender (Packed a) [b] where - mimeUnrender _ bs = either - (Left . show) - (traverse $ mimeUnrender (Proxy :: Proxy a) . BL.fromStrict) - (decodePackfile bs) diff --git a/lib/jormungandr/test/data/Cardano/Wallet/Binary/PackfileSpec-epoch-mainnet-104 b/lib/jormungandr/test/data/Cardano/Wallet/Binary/PackfileSpec-epoch-mainnet-104 deleted file mode 100644 index 2ca825935b9..00000000000 Binary files a/lib/jormungandr/test/data/Cardano/Wallet/Binary/PackfileSpec-epoch-mainnet-104 and /dev/null differ diff --git a/lib/jormungandr/test/unit/Cardano/Wallet/Binary/JormungandrSpec.hs b/lib/jormungandr/test/unit/Cardano/Wallet/Binary/JormungandrSpec.hs index cf51a3e8501..2a5eb1fc889 100644 --- a/lib/jormungandr/test/unit/Cardano/Wallet/Binary/JormungandrSpec.hs +++ b/lib/jormungandr/test/unit/Cardano/Wallet/Binary/JormungandrSpec.hs @@ -4,15 +4,14 @@ module Cardano.Wallet.Binary.JormungandrSpec ( spec - - -- * Helpers - , unsafeDeserialiseFromBytes ) where import Prelude import Cardano.Wallet.Binary.Jormungandr () +import Data.ByteString + ( ByteString ) import Cardano.Wallet.Primitive.Types ( BlockHeader (..), Hash (..), SlotId (..) ) @@ -22,13 +21,6 @@ import Data.ByteArray.Encoding import Test.Hspec ( Spec, describe, shouldBe, xit ) -import qualified Codec.CBOR.Decoding as CBOR ---import qualified Codec.CBOR.Encoding as CBOR -import qualified Codec.CBOR.Read as CBOR ---import qualified Codec.CBOR.Write as CBOR -import qualified Data.ByteString as BS -import qualified Data.ByteString.Lazy as BL - {-# ANN spec ("HLint: ignore Use head" :: String) #-} spec :: Spec spec = do @@ -38,11 +30,11 @@ spec = do `shouldBe` BlockHeader (SlotId 0 0) (Hash "?") where + unsafeDeserialiseFromBytes = undefined decodeGenesisBlock = error "TODO: import from Binary.Jormungandr" -genesisBlock :: BL.ByteString -genesisBlock = - either error BL.fromStrict $ convertFromBase @BS.ByteString Base16 +genesisBlock :: ByteString +genesisBlock = either error id $ convertFromBase @ByteString Base16 "005200000000009f000000000000000000000000ffadebfecd59d9eaa12e903a\ \d58100f7c1e35899739c3d05d022835c069d2b4f000000000000000000000000\ \00000000000000000000000000000000000000000047000048000000005cc1c2\ @@ -51,9 +43,3 @@ genesisBlock = \0001000000ff0005000006000000000000000000000000000000000000000000\ \0000000000002c020001833324c37869c122689a35917df53a4f2294a3a52f68\ \5e05f5f8e53b87e7ea452f000000000000000e" - --- | CBOR deserialise without error handling - handy for prototypes or testing. -unsafeDeserialiseFromBytes :: (forall s. CBOR.Decoder s a) -> BL.ByteString -> a -unsafeDeserialiseFromBytes decoder bytes = - either (\e -> error $ "unsafeDeserialiseFromBytes: " <> show e) snd $ - CBOR.deserialiseFromBytes decoder bytes