Skip to content

Commit

Permalink
Reorganize
Browse files Browse the repository at this point in the history
  • Loading branch information
Anviking committed May 15, 2019
1 parent 1fad123 commit c967040
Showing 1 changed file with 43 additions and 37 deletions.
80 changes: 43 additions & 37 deletions lib/jormungandr/src/Cardano/Wallet/Binary/Jormungandr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,6 @@ import Data.ByteString
import Data.Word
( Word16, Word32, Word64, Word8 )


-- | Messages are what the block body consists of.
--
-- Every message is prefixed with a message header.
--
-- Following, as closely as possible:
-- https://github.com/input-output-hk/rust-cardano/blob/e0616f13bebd6b908320bddb1c1502dea0d3305a/chain-impl-mockchain/src/message/mod.rs#L22-L29
data Message
= Initial [ConfigParam]
| OldUtxoDeclaration TODO
| Transaction TODO
| Certificate TODO
| UpdateProposal SignedUpdateProposal
| UpdateVote SignedVote
| UnimplementedMessage Int -- For development. Remove later.
deriving (Eq, Show)


data BlockHeader = BlockHeader
{ version :: Word16
, contentSize :: Word32
Expand All @@ -87,12 +69,6 @@ data TODO = TODO
deriving (Eq, Show)
data SignedVote = SignedVote
deriving (Eq, Show)
newtype Milli = Milli Word64
deriving (Eq, Show)
newtype LeaderId = LeaderId ByteString
deriving (Eq, Show)
data LinearFee = LinearFee Word64 Word64 Word64
deriving (Eq, Show)


{-# ANN getBlockHeader ("HLint: ignore Use <$>" :: String) #-}
Expand Down Expand Up @@ -125,6 +101,26 @@ getBlock = do
$ whileM (not <$> isEmpty) getMessage
return $ Block header msgs

{-------------------------------------------------------------------------------
Messages
-------------------------------------------------------------------------------}

-- | Messages are what the block body consists of.
--
-- Every message is prefixed with a message header.
--
-- Following, as closely as possible:
-- https://github.com/input-output-hk/rust-cardano/blob/e0616f13bebd6b908320bddb1c1502dea0d3305a/chain-impl-mockchain/src/message/mod.rs#L22-L29
data Message
= Initial [ConfigParam]
| OldUtxoDeclaration TODO
| Transaction TODO
| Certificate TODO
| UpdateProposal SignedUpdateProposal
| UpdateVote SignedVote
| UnimplementedMessage Int -- For development. Remove later.
deriving (Eq, Show)

getMessage :: Get Message
getMessage = do
size <- fromIntegral <$> getWord16be
Expand All @@ -140,6 +136,10 @@ getMessage = do
5 -> unimpl
other -> fail $ "Unexpected content type tag " ++ show other

getInitial :: Get [ConfigParam]
getInitial = do
len <- fromIntegral <$> getWord16be
replicateM len getConfigParam

{-------------------------------------------------------------------------------
Config Parameters
Expand Down Expand Up @@ -180,12 +180,6 @@ getTagLen = do
, len = fromIntegral $ w .&. (63) -- 0b111111
}


getInitial :: Get [ConfigParam]
getInitial = do
len <- fromIntegral <$> getWord16be
replicateM len getConfigParam

getConfigParam :: Get ConfigParam
getConfigParam = do
TagLen tag len <- getTagLen
Expand All @@ -205,17 +199,20 @@ getConfigParam = do
14 -> ConfigLinearFee <$> getLinearFee
15 -> ProposalExpiration <$> getWord32be
a -> fail $ "Invalid config param with tag " ++ show a
where
getBool = getWord8 >>= \case
1 -> return True
0 -> return False
other -> fail $ "Unexpected boolean integer: " ++ show other

getLinearFee = LinearFee <$> getWord64be <*> getWord64be <*> getWord64be

data Discrimination = Production | Test
deriving (Eq, Show)

newtype Milli = Milli Word64
deriving (Eq, Show)

newtype LeaderId = LeaderId ByteString
deriving (Eq, Show)

data LinearFee = LinearFee Word64 Word64 Word64
deriving (Eq, Show)


getDiscrimination :: Get Discrimination
getDiscrimination = getWord8 >>= \case
1 -> return Production
Expand All @@ -228,6 +225,15 @@ getMilli = Milli <$> getWord64be
getLeaderId :: Get LeaderId
getLeaderId = LeaderId <$> getByteString 32

getLinearFee :: Get LinearFee
getLinearFee = LinearFee <$> getWord64be <*> getWord64be <*> getWord64be

getBool :: Get Bool
getBool = getWord8 >>= \case
1 -> return True
0 -> return False
other -> fail $ "Unexpected boolean integer: " ++ show other

{-------------------------------------------------------------------------------
Helpers
-------------------------------------------------------------------------------}
Expand Down

0 comments on commit c967040

Please sign in to comment.