Skip to content

Commit 26f9243

Browse files
committed
refactor(cardano-chain-gen): Add a Query module
1 parent 49e8ca0 commit 26f9243

File tree

3 files changed

+53
-34
lines changed

3 files changed

+53
-34
lines changed

cardano-chain-gen/cardano-chain-gen.cabal

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ library
4444
Cardano.Mock.ChainDB
4545
Cardano.Mock.ChainSync.Server
4646
Cardano.Mock.ChainSync.State
47+
Cardano.Mock.Query
4748
Cardano.Mock.Forging.Crypto
4849
Cardano.Mock.Forging.Interpreter
4950
Cardano.Mock.Forging.Tx.Alonzo
@@ -63,6 +64,7 @@ library
6364
, bytestring
6465
, cardano-binary
6566
, cardano-crypto-class
67+
, cardano-db
6668
, cardano-ledger-allegra
6769
, cardano-ledger-alonzo
6870
, cardano-ledger-babbage
@@ -78,6 +80,7 @@ library
7880
, containers
7981
, contra-tracer
8082
, directory
83+
, esqueleto
8184
, extra
8285
, mtl
8386
, microlens
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{-# LANGUAGE TypeApplications #-}
2+
3+
module Cardano.Mock.Query (
4+
queryVersionMajorFromEpoch,
5+
queryParamProposalFromEpoch,
6+
) where
7+
8+
import qualified Cardano.Db as Db
9+
import Cardano.Prelude hiding (from)
10+
import Database.Esqueleto.Experimental
11+
import Prelude ()
12+
13+
-- | Query protocol parameters from @EpochParam@ by epoch number. Note that epoch
14+
-- parameters are inserted at the beginning of the next epoch.
15+
--
16+
-- TODO[sgillespie]: It would probably be better to return @Db.EpochParam@, but
17+
-- persistent seems to be having trouble with the data:
18+
--
19+
-- PersistMarshalError "Couldn't parse field `govActionLifetime` from table
20+
-- `epoch_param`. Failed to parse Haskell type `Word64`; expected integer from
21+
-- database, but received: PersistRational (0 % 1).
22+
queryVersionMajorFromEpoch ::
23+
MonadIO io =>
24+
Word64 ->
25+
ReaderT SqlBackend io (Maybe Word16)
26+
queryVersionMajorFromEpoch epochNo = do
27+
res <- selectOne $ do
28+
prop <- from $ table @Db.EpochParam
29+
where_ (prop ^. Db.EpochParamEpochNo ==. val epochNo)
30+
pure (prop ^. Db.EpochParamProtocolMajor)
31+
pure $ unValue <$> res
32+
33+
-- | Query protocol parameter proposals from @ParamProposal@ by epoch number.
34+
queryParamProposalFromEpoch ::
35+
MonadIO io =>
36+
Word64 ->
37+
ReaderT SqlBackend io (Maybe Db.ParamProposal)
38+
queryParamProposalFromEpoch epochNo = do
39+
res <- selectOne $ do
40+
prop <- from $ table @Db.ParamProposal
41+
where_ $ prop ^. Db.ParamProposalEpochNo ==. val (Just epochNo)
42+
pure prop
43+
pure $ entityVal <$> res

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/Other.hs

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
{-# LANGUAGE NumericUnderscores #-}
55
{-# LANGUAGE OverloadedStrings #-}
66
{-# LANGUAGE ScopedTypeVariables #-}
7-
{-# LANGUAGE TypeApplications #-}
87

98
module Test.Cardano.Db.Mock.Unit.Conway.Other (
109
-- * Different configs
@@ -33,16 +32,16 @@ import Cardano.Ledger.Credential (StakeCredential ())
3332
import Cardano.Ledger.Crypto (StandardCrypto ())
3433
import Cardano.Ledger.Keys (KeyHash (), KeyRole (..))
3534
import Cardano.Mock.ChainSync.Server (IOManager (), addBlock, rollback)
36-
import Cardano.Mock.Forging.Interpreter (Interpreter (), forgeNext, getCurrentEpoch)
35+
import Cardano.Mock.Forging.Interpreter (forgeNext, getCurrentEpoch)
3736
import qualified Cardano.Mock.Forging.Tx.Babbage as Babbage
3837
import qualified Cardano.Mock.Forging.Tx.Conway as Conway
3938
import Cardano.Mock.Forging.Tx.Generic (resolvePool)
4039
import Cardano.Mock.Forging.Types
40+
import Cardano.Mock.Query (queryParamProposalFromEpoch, queryVersionMajorFromEpoch)
4141
import Cardano.Prelude hiding (from)
4242
import Cardano.SMASH.Server.PoolDataLayer (PoolDataLayer (..), dbToServantPoolId)
4343
import Cardano.SMASH.Server.Types (DBFail (..))
4444
import Data.List (last)
45-
import Database.Esqueleto.Experimental
4645
import Ouroboros.Consensus.Shelley.Eras (StandardConway ())
4746
import Ouroboros.Network.Block (blockPoint)
4847
import Test.Cardano.Db.Mock.Config
@@ -497,35 +496,9 @@ forkParam =
497496
where
498497
testLabel = "conwayForkParam"
499498
configDir = babbageConfigDir
500-
501-
queryCurrentMajVer ::
502-
MonadIO m =>
503-
Interpreter ->
504-
ReaderT SqlBackend m (Maybe Word16)
505-
queryCurrentMajVer interpreter = do
506-
-- Look up current epoch from ledger
507-
EpochNo currentEpoch <- liftIO $ getCurrentEpoch interpreter
508-
509-
-- Query epoch params from database
510-
res <- selectOne $ do
511-
param <- from $ table @Db.EpochParam
512-
where_ (param ^. Db.EpochParamEpochNo ==. val currentEpoch)
513-
pure (param ^. Db.EpochParamProtocolMajor)
514-
515-
pure $ unValue <$> res
516-
517-
queryMajVerProposal ::
518-
MonadIO m =>
519-
Interpreter ->
520-
ReaderT SqlBackend m (Maybe Word16)
499+
queryCurrentMajVer interpreter = queryVersionMajorFromEpoch =<< getEpochNo interpreter
521500
queryMajVerProposal interpreter = do
522-
-- Look up current epoch from ledger
523-
EpochNo currentEpoch <- liftIO $ getCurrentEpoch interpreter
524-
525-
-- Query proposals from database
526-
res <- selectOne $ do
527-
prop <- from $ table @Db.ParamProposal
528-
where_ $ prop ^. Db.ParamProposalEpochNo ==. val (Just currentEpoch)
529-
pure (prop ^. Db.ParamProposalProtocolMajor)
530-
531-
pure $ join (unValue <$> res)
501+
epochNo <- getEpochNo interpreter
502+
prop <- queryParamProposalFromEpoch epochNo
503+
pure (Db.paramProposalProtocolMajor =<< prop)
504+
getEpochNo = fmap unEpochNo . liftIO . getCurrentEpoch

0 commit comments

Comments
 (0)