Skip to content

Commit 791da67

Browse files
committed
removal of panic and replacing with Exceptions
1 parent ee4688f commit 791da67

File tree

5 files changed

+88
-68
lines changed

5 files changed

+88
-68
lines changed

cardano-db-sync/src/Cardano/DbSync/Config.hs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,9 @@ import Cardano.DbSync.Config.Cardano
3030
import Cardano.DbSync.Config.Node
3131
import Cardano.DbSync.Config.Shelley
3232
import Cardano.DbSync.Config.Types
33-
import Cardano.DbSync.Util
33+
import Cardano.DbSync.Error (runOrThrowIO)
3434
import Cardano.Prelude
35-
import qualified Data.ByteString.Char8 as BS
36-
import qualified Data.Text as Text
37-
import qualified Data.Yaml as Yaml
3835
import System.FilePath (takeDirectory, (</>))
39-
import Cardano.DbSync.Error (runOrThrowIO)
4036

4137
configureLogging :: SyncNodeParams -> Text -> IO (Trace IO Text)
4238
configureLogging params loggingName = do
@@ -49,16 +45,11 @@ configureLogging params loggingName = do
4945

5046
readSyncNodeConfig :: ConfigFile -> IO SyncNodeConfig
5147
readSyncNodeConfig (ConfigFile fp) = do
52-
pcfg <- adjustNodeFilePath . parseSyncPreConfig <$> readByteString fp "DbSync"
53-
ncfg <- runOrThrowIO . parseNodeConfig =<< readByteString (pcNodeConfigFilePath pcfg) "node"
54-
coalesceConfig pcfg ncfg (mkAdjustPath pcfg)
48+
pcfg <- runOrThrowIO . parseSyncPreConfig =<< runOrThrowIO (readByteStringFromFile fp "DbSync")
49+
let afp = adjustNodeFilePath pcfg
50+
ncfg <- runOrThrowIO . parseNodeConfig =<< runOrThrowIO (readByteStringFromFile (pcNodeConfigFilePath afp) "node")
51+
coalesceConfig afp ncfg (mkAdjustPath pcfg)
5552
where
56-
parseSyncPreConfig :: ByteString -> SyncPreConfig
57-
parseSyncPreConfig bs =
58-
case Yaml.decodeEither' bs of
59-
Left err -> panic $ "readSyncNodeConfig: Error parsing config: " <> textShow err
60-
Right res -> res
61-
6253
adjustNodeFilePath :: SyncPreConfig -> SyncPreConfig
6354
adjustNodeFilePath cfg =
6455
cfg {pcNodeConfigFile = adjustNodeConfigFilePath (takeDirectory fp </>) (pcNodeConfigFile cfg)}
@@ -98,8 +89,3 @@ coalesceConfig pcfg ncfg adjustGenesisPath = do
9889

9990
mkAdjustPath :: SyncPreConfig -> (FilePath -> FilePath)
10091
mkAdjustPath cfg fp = takeDirectory (pcNodeConfigFilePath cfg) </> fp
101-
102-
readByteString :: FilePath -> Text -> IO ByteString
103-
readByteString fp cfgType =
104-
catch (BS.readFile fp) $ \(_ :: IOException) ->
105-
panic $ mconcat ["Cannot find the ", cfgType, " configuration file at : ", Text.pack fp]

cardano-db-sync/src/Cardano/DbSync/Config/Node.hs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
module Cardano.DbSync.Config.Node (
99
NodeConfig (..),
1010
parseNodeConfig,
11+
parseSyncPreConfig,
12+
readByteStringFromFile,
1113
) where
1214

1315
-- Node really should export something like this, but doesn't and it actually seemed to
@@ -20,9 +22,10 @@ import Cardano.Prelude
2022
import Data.Aeson (FromJSON (..), Object, (.:), (.:?))
2123
import qualified Data.Aeson as Aeson
2224
import Data.Aeson.Types (Parser)
25+
import qualified Data.ByteString.Char8 as BS
26+
import Data.String (String)
2327
import qualified Data.Yaml as Yaml
2428
import qualified Ouroboros.Consensus.Cardano.CanHardFork as Shelley
25-
import Data.String (String)
2629

2730
data NodeConfig = NodeConfig
2831
{ ncProtocol :: !SyncProtocol
@@ -49,8 +52,10 @@ data NodeConfig = NodeConfig
4952
ncConwayHardFork :: !Shelley.TriggerHardFork
5053
}
5154

52-
newtype NodeConfigParseError =
53-
NodeConfigParseError String
55+
data NodeConfigParseError
56+
= NodeConfigParseError String
57+
| ParseSyncPreConfigError String
58+
| ReadByteStringFromFileError String
5459
deriving (Show)
5560

5661
instance Exception NodeConfigParseError
@@ -61,6 +66,19 @@ parseNodeConfig bs =
6166
Left err -> pure $ Left $ NodeConfigParseError ("Error parsing node config: " <> show err)
6267
Right nc -> pure $ Right nc
6368

69+
parseSyncPreConfig :: ByteString -> IO (Either NodeConfigParseError SyncPreConfig)
70+
parseSyncPreConfig bs =
71+
case Yaml.decodeEither' bs of
72+
Left err -> pure $ Left $ ParseSyncPreConfigError ("Error parseSyncPreConfig: Error parsing config: " <> show err)
73+
Right res -> pure $ Right res
74+
75+
readByteStringFromFile :: FilePath -> Text -> IO (Either NodeConfigParseError ByteString)
76+
readByteStringFromFile fp cfgType = do
77+
result <- try (BS.readFile fp) :: IO (Either SomeException BS.ByteString)
78+
case result of
79+
Left _ -> pure $ Left $ ReadByteStringFromFileError ("Error Cannot find the " <> show cfgType <> " configuration file at : " <> show fp)
80+
Right res -> pure $ Right res
81+
6482
-- -------------------------------------------------------------------------------------------------
6583

6684
instance FromJSON NodeConfig where
@@ -70,15 +88,15 @@ instance FromJSON NodeConfig where
7088
parse :: Object -> Parser NodeConfig
7189
parse o =
7290
NodeConfig
73-
<$> o .: "Protocol"
74-
<*> o .:? "PBftSignatureThreshold"
91+
<$> (o .: "Protocol")
92+
<*> (o .:? "PBftSignatureThreshold")
7593
<*> fmap GenesisFile (o .: "ByronGenesisFile")
7694
<*> fmap GenesisHashByron (o .: "ByronGenesisHash")
7795
<*> fmap GenesisFile (o .: "ShelleyGenesisFile")
7896
<*> fmap GenesisHashShelley (o .: "ShelleyGenesisHash")
7997
<*> fmap GenesisFile (o .: "AlonzoGenesisFile")
8098
<*> fmap GenesisHashAlonzo (o .: "AlonzoGenesisHash")
81-
<*> o .: "RequiresNetworkMagic"
99+
<*> (o .: "RequiresNetworkMagic")
82100
<*> parseByronProtocolVersion o
83101
<*> parseShelleyHardForkEpoch o
84102
<*> parseAllegraHardForkEpoch o
@@ -90,9 +108,9 @@ instance FromJSON NodeConfig where
90108
parseByronProtocolVersion :: Object -> Parser Byron.ProtocolVersion
91109
parseByronProtocolVersion o =
92110
Byron.ProtocolVersion
93-
<$> o .: "LastKnownBlockVersion-Major"
94-
<*> o .: "LastKnownBlockVersion-Minor"
95-
<*> o .: "LastKnownBlockVersion-Alt"
111+
<$> (o .: "LastKnownBlockVersion-Major")
112+
<*> (o .: "LastKnownBlockVersion-Minor")
113+
<*> (o .: "LastKnownBlockVersion-Alt")
96114

97115
parseShelleyHardForkEpoch :: Object -> Parser Shelley.TriggerHardFork
98116
parseShelleyHardForkEpoch o =

cardano-db-sync/src/Cardano/DbSync/Era/Byron/Genesis.hs

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -171,39 +171,49 @@ validateGenesisDistribution prunes tracer networkName cfg bid =
171171

172172
-- -----------------------------------------------------------------------------
173173

174-
insertTxOuts :: (MonadBaseControl IO m, MonadIO m) => Bool -> DB.BlockId -> (Byron.Address, Byron.Lovelace) -> ReaderT SqlBackend m ()
174+
insertTxOuts ::
175+
(MonadBaseControl IO m, MonadIO m) =>
176+
Bool ->
177+
DB.BlockId ->
178+
(Byron.Address, Byron.Lovelace) ->
179+
ReaderT SqlBackend m (Either SyncNodeError ())
175180
insertTxOuts hasConsumed blkId (address, value) = do
176-
-- Each address/value pair of the initial coin distribution comes from an artifical transaction
177-
-- with a hash generated by hashing the address.
178-
txId <-
179-
DB.insertTx $
180-
DB.Tx
181-
{ DB.txHash = Byron.unTxHash $ txHashOfAddress address
182-
, DB.txBlockId = blkId
183-
, DB.txBlockIndex = 0
184-
, DB.txOutSum = DB.DbLovelace (Byron.unsafeGetLovelace value)
185-
, DB.txFee = DB.DbLovelace 0
186-
, DB.txDeposit = 0
187-
, DB.txSize = 0 -- Genesis distribution address to not have a size.
188-
, DB.txInvalidHereafter = Nothing
189-
, DB.txInvalidBefore = Nothing
190-
, DB.txValidContract = True
191-
, DB.txScriptSize = 0
192-
}
193-
void . DB.insertTxOutPlex hasConsumed $
194-
DB.TxOut
195-
{ DB.txOutTxId = txId
196-
, DB.txOutIndex = 0
197-
, DB.txOutAddress = Text.decodeUtf8 $ Byron.addrToBase58 address
198-
, DB.txOutAddressRaw = Binary.serialize' address
199-
, DB.txOutAddressHasScript = False
200-
, DB.txOutPaymentCred = Nothing
201-
, DB.txOutStakeAddressId = Nothing
202-
, DB.txOutValue = DB.DbLovelace (Byron.unsafeGetLovelace value)
203-
, DB.txOutDataHash = Nothing
204-
, DB.txOutInlineDatumId = Nothing
205-
, DB.txOutReferenceScriptId = Nothing
206-
}
181+
case txHashOfAddress address of
182+
Left err -> pure $ Left err
183+
Right val -> do
184+
-- Each address/value pair of the initial coin distribution comes from an artifical transaction
185+
-- with a hash generated by hashing the address.
186+
txId <-
187+
DB.insertTx $
188+
DB.Tx
189+
{ DB.txHash = Byron.unTxHash val
190+
, DB.txBlockId = blkId
191+
, DB.txBlockIndex = 0
192+
, DB.txOutSum = DB.DbLovelace (Byron.unsafeGetLovelace value)
193+
, DB.txFee = DB.DbLovelace 0
194+
, DB.txDeposit = 0
195+
, DB.txSize = 0 -- Genesis distribution address to not have a size.
196+
, DB.txInvalidHereafter = Nothing
197+
, DB.txInvalidBefore = Nothing
198+
, DB.txValidContract = True
199+
, DB.txScriptSize = 0
200+
}
201+
void $
202+
DB.insertTxOutPlex hasConsumed $
203+
DB.TxOut
204+
{ DB.txOutTxId = txId
205+
, DB.txOutIndex = 0
206+
, DB.txOutAddress = Text.decodeUtf8 $ Byron.addrToBase58 address
207+
, DB.txOutAddressRaw = Binary.serialize' address
208+
, DB.txOutAddressHasScript = False
209+
, DB.txOutPaymentCred = Nothing
210+
, DB.txOutStakeAddressId = Nothing
211+
, DB.txOutValue = DB.DbLovelace (Byron.unsafeGetLovelace value)
212+
, DB.txOutDataHash = Nothing
213+
, DB.txOutInlineDatumId = Nothing
214+
, DB.txOutReferenceScriptId = Nothing
215+
}
216+
pure $ Right ()
207217

208218
-- -----------------------------------------------------------------------------
209219

@@ -231,9 +241,13 @@ genesisTxos config =
231241
nonAvvmBalances =
232242
Map.toList $ Byron.unGenesisNonAvvmBalances (Byron.configNonAvvmBalances config)
233243

234-
txHashOfAddress :: Byron.Address -> Crypto.Hash Byron.Tx
235-
txHashOfAddress =
236-
fromMaybe (panic "Cardano.DbSync.Era.Byron.Genesis.txHashOfAddress")
237-
. Crypto.abstractHashFromBytes
238-
. Crypto.abstractHashToBytes
239-
. Crypto.serializeCborHash
244+
txHashOfAddress :: Byron.Address -> Either SyncNodeError (Crypto.Hash Byron.Tx)
245+
txHashOfAddress ba = do
246+
case hashFromBS of
247+
Just res -> Right res
248+
Nothing -> Left $ SNErrInsertGenesis "Cardano.DbSync.Era.Byron.Genesis.txHashOfAddress"
249+
where
250+
hashFromBS =
251+
Crypto.abstractHashFromBytes
252+
. Crypto.abstractHashToBytes
253+
$ Crypto.serializeCborHash ba

cardano-db/src/Cardano/Db/Multiplex.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ import Cardano.Db.Insert
1616
import qualified Cardano.Db.Migration.Extra.CosnumedTxOut.Queries as ExtraCons
1717
import qualified Cardano.Db.Migration.Extra.CosnumedTxOut.Schema as ExtraCons
1818
import Cardano.Db.Schema
19-
import Cardano.Prelude (panic)
2019
import Control.Monad.IO.Class (MonadIO, liftIO)
2120
import Control.Monad.Trans.Control (MonadBaseControl)
2221
import Control.Monad.Trans.Reader (ReaderT)
2322
import Data.Text (Text)
2423
import Data.Word (Word64)
2524
import Database.Persist.Sql (SqlBackend, ToBackendKey (..))
25+
import Control.Exception (throwIO)
2626

2727
insertTxOutPlex ::
2828
(MonadBaseControl IO m, MonadIO m) =>
@@ -85,7 +85,7 @@ runExtraMigrations trce blockNoDiff consumed pruned = do
8585
liftIO $ logInfo trce "Extra migration consumed_tx_out already executed"
8686
(True, False, False) -> do
8787
liftIO $ logError trce migratedButNotSet
88-
panic migratedButNotSet
88+
liftIO $ throwIO $ userError (show migratedButNotSet)
8989
(False, True, False) -> do
9090
liftIO $ logInfo trce "Running extra migration consumed_tx_out"
9191
ExtraCons.migrateTxOut $ Just trce

doc/validation.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ the fees associated with the transaction are recorded in the database.
7272

7373
## Runtime Reward Validations
7474

75+
-- TODO: Vince - probably too late unless we deprecate, but could this be a command line argument?
76+
7577
Due to recent issues with reward related tables, extra validation was added. When that validation
7678
fails, there are now two options. `db-sync` can abort or continue running. The behaviour is
7779
specified by the presence or absence of the `DbSyncAbortOnPanic` environemnt variable. If the

0 commit comments

Comments
 (0)