Skip to content

Commit

Permalink
[#530] Apply lint suggestions to the backend project
Browse files Browse the repository at this point in the history
In alignment with the user story's objective to implement code
formatting and checks for the backend to ensure a consistent code style,
this commit includes the application of the lint suggestions on all the
applicable haskell files in backend module.
  • Loading branch information
placek committed Mar 26, 2024
1 parent d3fa55e commit 768e7f4
Show file tree
Hide file tree
Showing 11 changed files with 16 additions and 42 deletions.
6 changes: 1 addition & 5 deletions govtool/backend/app/Main.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
Expand Down Expand Up @@ -39,10 +38,7 @@ import qualified Data.Text.Lazy.Encoding as LazyText
import Database.PostgreSQL.Simple (close, connectPostgreSQL)

import Network.Wai
import Network.Wai (Request, rawPathInfo, requestHeaderHost)
import Network.Wai.Handler.Warp
import Network.Wai.Handler.Warp (defaultOnException, defaultSettings, runSettings,
setOnException, setPort)
import Network.Wai.Middleware.Cors

import Options.Applicative (execParser)
Expand Down Expand Up @@ -151,7 +147,7 @@ recordUpdate Nothing exception record = record
recordUpdate (Just request) exception record =
record
{ srCulprit = Just $ unpack $ rawPathInfo request,
srServerName = fmap unpack $ requestHeaderHost request
srServerName = unpack <$> requestHeaderHost request
}

shouldDisplayException :: SomeException -> Bool
Expand Down
19 changes: 6 additions & 13 deletions govtool/backend/src/VVA/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ import qualified VVA.Epoch as Epoch
import VVA.Network as Network
import qualified VVA.Proposal as Proposal
import qualified VVA.Transaction as Transaction
import qualified VVA.Types as Types
import VVA.Types (App, AppEnv (..), AppError (CriticalError, ValidationError),
CacheEnv (..))
import qualified VVA.Types as Types

type VVAApi =
"drep" :> "list" :> QueryParam "drepView" Text :> Get '[JSON] [DRep]
Expand Down Expand Up @@ -74,7 +74,6 @@ server = drepList
:<|> throw500
:<|> getNetworkMetrics


mapDRepType :: Types.DRepType -> DRepType
mapDRepType Types.DRep = NormalDRep
mapDRepType Types.SoleVoter = SoleVoter
Expand Down Expand Up @@ -104,7 +103,7 @@ drepList mDRepView = do
let filtered = flip filter dreps $ \Types.DRepRegistration {..} ->
case (dRepRegistrationType, mDRepView) of
(Types.SoleVoter, Just x) -> x == dRepRegistrationView
(Types.DRep, Just x) -> isInfixOf x dRepRegistrationView
(Types.DRep, Just x) -> x `isInfixOf` dRepRegistrationView
(Types.DRep, Nothing) -> True
_ -> False
return $ map drepRegistrationToDrep filtered
Expand All @@ -114,7 +113,6 @@ getVotingPower (unHexText -> dRepId) = do
CacheEnv {dRepVotingPowerCache} <- asks vvaCache
cacheRequest dRepVotingPowerCache dRepId $ DRep.getVotingPower dRepId


proposalToResponse :: Types.Proposal -> ProposalResponse
proposalToResponse Types.Proposal {..} =
ProposalResponse
Expand Down Expand Up @@ -142,7 +140,6 @@ voteToResponse Types.Vote {..} =
voteParamsMetadataHash = HexText <$> voteDocHash
}


mapSortAndFilterProposals
:: [GovernanceActionType]
-> Maybe GovernanceActionSortMode
Expand Down Expand Up @@ -177,7 +174,7 @@ getVotes (unHexText -> dRepId) selectedTypes sortMode = do
let processedProposals = mapSortAndFilterProposals selectedTypes sortMode proposals
return $
[ VoteResponse
{ voteResponseVote = voteToResponse (voteMap Map.! (read $ unpack proposalResponseId))
{ voteResponseVote = voteToResponse (voteMap Map.! read (unpack proposalResponseId))
, voteResponseProposal = proposalResponse
}
| proposalResponse@ProposalResponse{proposalResponseId} <- processedProposals
Expand Down Expand Up @@ -207,8 +204,7 @@ getCurrentDelegation (unHexText -> stakeKey) = do
getStakeKeyVotingPower :: App m => HexText -> m Integer
getStakeKeyVotingPower (unHexText -> stakeKey) = do
CacheEnv {adaHolderVotingPowerCache} <- asks vvaCache
cacheRequest adaHolderVotingPowerCache stakeKey $ AdaHolder.getStakeKeyVotingPower $ stakeKey

cacheRequest adaHolderVotingPowerCache stakeKey $ AdaHolder.getStakeKeyVotingPower stakeKey

listProposals
:: App m
Expand All @@ -234,10 +230,7 @@ listProposals selectedTypes sortMode mPage mPageSize mDrepRaw = do
filter
( \ProposalResponse {proposalResponseId} ->
proposalResponseId `notElem` proposalsToRemove
)
<$>
mapSortAndFilterProposals selectedTypes sortMode
<$> cacheRequest proposalListCache () Proposal.listProposals
) . mapSortAndFilterProposals selectedTypes sortMode <$> cacheRequest proposalListCache () Proposal.listProposals

let total = length mappedAndSortedProposals :: Int

Expand All @@ -250,7 +243,7 @@ listProposals selectedTypes sortMode mPage mPageSize mDrepRaw = do
, listProposalsResponseElements = elements
}

getProposal :: App m => GovActionId -> Maybe (HexText) -> m GetProposalResponse
getProposal :: App m => GovActionId -> Maybe HexText -> m GetProposalResponse
getProposal g@(GovActionId govActionTxHash govActionIndex) mDrepId' = do
let mDrepId = unHexText <$> mDrepId'
CacheEnv {getProposalCache} <- asks vvaCache
Expand Down
4 changes: 2 additions & 2 deletions govtool/backend/src/VVA/API/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ newtype HexText

instance FromJSON HexText where
parseJSON (Aeson.String t) = do
if (Text.length t `mod` 2 == 1 || Text.any (not . isHexDigit) t)
if Text.length t `mod` 2 == 1 || Text.any (not . isHexDigit) t
then mzero
else pure $ HexText t

Expand All @@ -71,7 +71,7 @@ instance ToJSON HexText where
-- To use it in routes, we need to be able to parse it from Text:
instance FromHttpApiData HexText where
parseUrlPiece txt
| Text.all isHexDigit txt && (Text.length txt `mod` 2 == 0) = Right (HexText txt)
| Text.all isHexDigit txt && even (Text.length txt) = Right (HexText txt)
| otherwise = Left "Not a valid hex value"


Expand Down
5 changes: 1 addition & 4 deletions govtool/backend/src/VVA/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeOperators #-}

-- | Module : VVA.Config
-- Description : Configuration interface
Expand Down Expand Up @@ -170,7 +167,7 @@ loadVVAConfig configFile = do
getDbSyncConnectionString ::
(Has VVAConfig r, MonadReader r m) =>
m ByteString
getDbSyncConnectionString = encodeUtf8 <$> asks (dbSyncConnectionString . getter)
getDbSyncConnectionString = asks (encodeUtf8 . dbSyncConnectionString . getter)

-- | Access server port.
getServerPort ::
Expand Down
11 changes: 4 additions & 7 deletions govtool/backend/src/VVA/DRep.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Data.FileEmbed (embedFile)
import Data.Foldable (Foldable (sum))
import Data.Has (Has)
import qualified Data.Map as M
import Data.Maybe (fromMaybe)
import Data.Maybe (fromMaybe, isJust, isNothing)
import Data.Scientific
import Data.String (fromString)
import Data.Text (Text, pack, unpack)
Expand All @@ -32,9 +32,6 @@ import qualified VVA.Proposal as Proposal
import VVA.Types (AppError, DRepInfo (..), DRepRegistration (..), DRepStatus (..),
DRepType (..), Proposal (..), Vote (..))




sqlFrom :: ByteString -> SQL.Query
sqlFrom bs = fromString $ unpack $ Text.decodeUtf8 bs

Expand Down Expand Up @@ -66,9 +63,9 @@ listDReps = withPool $ \conn -> do
(_, d) | d < 0 -> Retired
(isActive, d) | d >= 0 && isActive -> Active
| d >= 0 && not isActive -> Inactive
, let drepType | url == Nothing && wasDRep = DRep
| url == Nothing && not wasDRep = SoleVoter
| url /= Nothing = DRep
, let drepType | isNothing url && wasDRep = DRep
| isNothing url && not wasDRep = SoleVoter
| Data.Maybe.isJust url = DRep
]

getVotesSql :: SQL.Query
Expand Down
1 change: 0 additions & 1 deletion govtool/backend/src/VVA/Epoch.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

module VVA.Epoch where


import Control.Monad.Except (MonadError, throwError)
import Control.Monad.Reader

Expand Down
3 changes: 1 addition & 2 deletions govtool/backend/src/VVA/Network.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

module VVA.Network where


import Control.Monad.Except (MonadError, throwError)
import Control.Monad.Reader

Expand Down Expand Up @@ -34,7 +33,7 @@ networkMetrics ::
m NetworkMetrics
networkMetrics = withPool $ \conn -> do
result <- liftIO $ SQL.query_ conn networkMetricsSql
current_time <- liftIO $ getCurrentTime
current_time <- liftIO getCurrentTime
case result of
[( epoch_no
, block_no
Expand Down
1 change: 1 addition & 0 deletions govtool/backend/src/VVA/Pool.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE FlexibleContexts #-}

module VVA.Pool where

import Control.Monad.IO.Class (MonadIO, liftIO)
Expand Down
2 changes: 0 additions & 2 deletions govtool/backend/src/VVA/Proposal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import VVA.Config
import VVA.Pool (ConnectionPool, withPool)
import VVA.Types (AppError (..), Proposal (..))


sqlFrom :: ByteString -> SQL.Query
sqlFrom bs = fromString $ unpack $ Text.decodeUtf8 bs

Expand All @@ -60,7 +59,6 @@ getProposal txHash index = do
[a] -> return a
_ -> throwError $ CriticalError ("Multiple proposal found for id: " <> txHash <> "#" <> pack (show index) <> ". This should never happen")


getProposals ::
(Has ConnectionPool r, Has VVAConfig r, MonadReader r m, MonadIO m, MonadFail m, MonadError AppError m) =>
Maybe [Text] ->
Expand Down
2 changes: 0 additions & 2 deletions govtool/backend/src/VVA/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

module VVA.Transaction where


import Control.Exception (throw)
import Control.Monad.Except (MonadError, throwError)
import Control.Monad.Reader
Expand All @@ -26,7 +25,6 @@ import VVA.Types (AppError (..), TransactionStatus (.
sqlFrom :: ByteString -> SQL.Query
sqlFrom bs = fromString $ unpack $ Text.decodeUtf8 bs


getTransactionStatusSql :: SQL.Query
getTransactionStatusSql = sqlFrom $(embedFile "sql/get-transaction-status.sql")

Expand Down
4 changes: 0 additions & 4 deletions govtool/backend/src/VVA/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ data AppError
| CriticalError Text
deriving (Show)


instance Exception AppError

data Vote
Expand Down Expand Up @@ -108,8 +107,6 @@ data Proposal
}
deriving (Show)



data TransactionStatus = TransactionConfirmed | TransactionUnconfirmed

data CacheEnv
Expand Down Expand Up @@ -139,4 +136,3 @@ data NetworkMetrics
, networkMetricsAlwaysAbstainVotingPower :: Integer
, networkMetricsAlwaysNoConfidenceVotingPower :: Integer
}

0 comments on commit 768e7f4

Please sign in to comment.