Skip to content

Commit

Permalink
tests: Add tests for decoding actual pack file
Browse files Browse the repository at this point in the history
  • Loading branch information
rvl committed Mar 7, 2019
1 parent 77cd4bd commit 8a85090
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion test/unit/Cardano/Wallet/PackfileSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@ module Cardano.Wallet.PackfileSpec (spec) where

import Prelude

import qualified Data.ByteString.Char8 as B8
import qualified Data.ByteString.Lazy.Char8 as L8

import Data.Either
( fromRight, isRight )
import Test.Hspec
( Spec, describe, it, shouldBe )
( Spec, describe, it, shouldBe, shouldSatisfy )

import Cardano.Wallet.Primitive (Block(..), BlockHeader(..))
import Cardano.Wallet.Binary.Packfile
(decodePackfile, PackfileError (..))
import Cardano.Wallet.Binary (decodeBlock)
import Cardano.Wallet.BinaryHelpers (unsafeDeserialiseFromBytes)

-- version 1 header
packFileHeader :: L8.ByteString
Expand All @@ -23,6 +30,11 @@ testTwoBlobs = packFileHeader
<> "\0\0\0\11first block\0"
<> "\0\0\0\12second block"

-- Get this file from cardano-http-bridge with:
-- wget -O test/data/epoch-mainnet-104 http://localhost:8080/mainnet/epoch/104
testPackfile :: FilePath
testPackfile = "test/data/epoch-mainnet-104"

spec :: Spec
spec = do
describe "Decoding pack file" $ do
Expand Down Expand Up @@ -56,3 +68,33 @@ spec = do
it "should reject extra data" $ do
let decoded = decodePackfile (packFileHeader <> "a")
decoded `shouldBe` Left (BlobDecodeError "not enough bytes")

describe "Decoding a mainnet pack file" $ do
it "should decode pack file for mainnet epoch 104" $ do
bs <- L8.readFile testPackfile
let decoded = decodePackfile bs
decoded `shouldSatisfy` isRight
length (fromRight [] decoded) `shouldBe` 21600

it "should decode the entire blocks" $ do
bs <- L8.readFile testPackfile
let Right (first:second:_) = decodePackfile bs
B8.length first `shouldBe` 648092
B8.length second `shouldBe` 1181

it "should decode correct blocks" $ do
bs <- L8.readFile testPackfile
let (ebb:first:second:_) = map header $ unsafeDeserialiseEpoch bs
epochIndex ebb `shouldBe` 104
epochIndex first `shouldBe` 104
slotNumber ebb `shouldBe` 0 -- epoch genesis block
slotNumber first `shouldBe` 0 -- first block
slotNumber second `shouldBe` 1 -- second block

-- | Decode all blocks in a pack file, without error handling
unsafeDeserialiseEpoch :: L8.ByteString -> [Block]
unsafeDeserialiseEpoch = either giveUp decodeBlocks . decodePackfile
where
decodeBlocks = map decodeBlob
decodeBlob = unsafeDeserialiseFromBytes decodeBlock . L8.fromStrict
giveUp err = error ("Could not decode pack file: " <> show err)

0 comments on commit 8a85090

Please sign in to comment.