Skip to content

Commit

Permalink
fix network id type for mempool endpoints (#879)
Browse files Browse the repository at this point in the history
* fix network id type for mempool endpoints

* fix parsing of network id

Co-authored-by: Doug Beardsley <[email protected]>
  • Loading branch information
larskuhtz and mightybyte authored Jan 12, 2020
1 parent 36ed4cd commit b4220fc
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/Chainweb/RestAPI/NetworkID.hs
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,23 @@ data NetworkId
deriving (Show, Eq, Ord, Generic)
deriving anyclass (Hashable, NFData)

-- | Textual representation of NetworkId.
--
-- This is expected to match the respective routes generated for
-- 'NetworkEndpointApi'.
--
networkIdToText :: NetworkId -> T.Text
networkIdToText CutNetwork = "cut"
networkIdToText (ChainNetwork cid) = "chain/" <> chainIdToText cid
networkIdToText (MempoolNetwork cid) = "mempool/" <> chainIdToText cid
networkIdToText (MempoolNetwork cid) = "chain/" <> chainIdToText cid <> "/mempool"
{-# INLINE networkIdToText #-}

networkIdFromText :: MonadThrow m => T.Text -> m NetworkId
networkIdFromText "cut" = return CutNetwork
networkIdFromText t = case T.break (== '/') t of
(a, b)
| a == "chain" -> ChainNetwork <$> chainIdFromText (T.drop 1 b)
| a == "mempool" -> MempoolNetwork <$> chainIdFromText (T.drop 1 b)
| T.null b -> throwM . TextFormatException $ "missing '/' in network id: \"" <> t <> "\"."
| otherwise -> throwM $ TextFormatException $ "unrecognized network id: \"" <> t <> "\"."
networkIdFromText t = case T.split (== '/') t of
["cut"] -> return CutNetwork
["chain", a, "mempool"] -> MempoolNetwork <$> chainIdFromText a
["chain", a] -> ChainNetwork <$> chainIdFromText a
_ -> throwM $ TextFormatException $ "unrecognized network id: \"" <> t <> "\"."

unsafeNetworkIdFromText :: HasCallStack => T.Text -> NetworkId
unsafeNetworkIdFromText = fromJuste . networkIdFromText
Expand Down

0 comments on commit b4220fc

Please sign in to comment.