Skip to content

Commit 77d1ec0

Browse files
committed
Add HasCallStack constraints to functions containing error calls
Add HasCallStack to functions that call error directly or indirectly (via fromMaybe impossible), so that call stacks are captured in error messages. For class methods (castVerificationKey, deterministicSigningKey), the constraint is placed on individual instance methods that actually call error, rather than on the class method itself. Also improve Dijkstra-era placeholder error messages to be more descriptive.
1 parent 1c39fba commit 77d1ec0

11 files changed

Lines changed: 44 additions & 16 deletions

File tree

cardano-api/src/Cardano/Api/Byron/Internal/Key.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import Data.ByteString.Lazy qualified as LB
5555
import Data.Either.Combinators
5656
import Data.Text qualified as Text
5757
import Formatting (build, formatToString)
58+
import GHC.Stack (HasCallStack)
5859

5960
-- | Byron-era payment keys. Used for Byron addresses and witnessing
6061
-- transactions that spend from these addresses.
@@ -195,7 +196,8 @@ instance Key ByronKeyLegacy where
195196
deriving newtype (ToCBOR, FromCBOR)
196197
deriving anyclass SerialiseAsCBOR
197198

198-
deterministicSigningKey :: AsType ByronKeyLegacy -> Crypto.Seed -> SigningKey ByronKeyLegacy
199+
deterministicSigningKey
200+
:: HasCallStack => AsType ByronKeyLegacy -> Crypto.Seed -> SigningKey ByronKeyLegacy
199201
deterministicSigningKey _ _ = error "Please generate a non legacy Byron key instead"
200202

201203
deterministicSigningKeySeedSize :: AsType ByronKeyLegacy -> Word

cardano-api/src/Cardano/Api/Certificate/Internal.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ getAnchorDataFromCertificate c =
826826
Ledger.RetirePoolTxCert _ _ -> return Nothing
827827
Ledger.GenesisDelegTxCert{} -> return Nothing
828828
Ledger.MirTxCert _ -> return Nothing
829-
_ -> error "dijkstra"
829+
_ -> error "getAnchorDataFromCertificate: Dijkstra era not supported"
830830
ConwayCertificate ceo ccert ->
831831
conwayEraOnwardsConstraints ceo $
832832
case ccert of
@@ -843,7 +843,7 @@ getAnchorDataFromCertificate c =
843843
Ledger.UpdateDRepTxCert _ mAnchor -> return $ Ledger.strictMaybeToMaybe mAnchor
844844
Ledger.AuthCommitteeHotKeyTxCert _ _ -> return Nothing
845845
Ledger.ResignCommitteeColdTxCert _ mAnchor -> return $ Ledger.strictMaybeToMaybe mAnchor
846-
_ -> error "dijkstra"
846+
_ -> error "getAnchorDataFromCertificate: Dijkstra era not supported"
847847
where
848848
anchorDataFromPoolMetadata
849849
:: MonadError AnchorDataFromCertificateError m

cardano-api/src/Cardano/Api/Experimental/Tx/Internal/AnyWitness.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ getPlutusDatum
125125
getPlutusDatum L.SPlutusV1 (SpendingScriptDatum d) = Just d
126126
getPlutusDatum L.SPlutusV2 (SpendingScriptDatum d) = Just d
127127
getPlutusDatum L.SPlutusV3 (SpendingScriptDatum d) = d
128-
getPlutusDatum L.SPlutusV4 (SpendingScriptDatum _d) = error "dijkstra"
128+
getPlutusDatum L.SPlutusV4 (SpendingScriptDatum _d) = error "getPlutusDatum: Dijkstra era not supported"
129129
getPlutusDatum _ InlineDatum = Nothing
130130
getPlutusDatum _ NoScriptDatum = Nothing
131131

cardano-api/src/Cardano/Api/Key/Internal.hs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ import Data.ByteString qualified as BS
8181
import Data.Either.Combinators (maybeToRight)
8282
import Data.Maybe
8383
import Data.String (IsString (..))
84+
import GHC.Stack (HasCallStack)
8485

8586
--
8687
-- Shelley payment keys
@@ -330,6 +331,8 @@ instance HasTextEnvelope (SigningKey PaymentExtendedKey) where
330331
textEnvelopeType _ = "PaymentExtendedSigningKeyShelley_ed25519_bip32"
331332

332333
instance CastVerificationKeyRole PaymentExtendedKey PaymentKey where
334+
castVerificationKey
335+
:: HasCallStack => VerificationKey PaymentExtendedKey -> VerificationKey PaymentKey
333336
castVerificationKey (PaymentExtendedVerificationKey vk) =
334337
PaymentVerificationKey
335338
. Shelley.VKey
@@ -580,6 +583,7 @@ instance HasTextEnvelope (SigningKey StakeExtendedKey) where
580583
textEnvelopeType _ = "StakeExtendedSigningKeyShelley_ed25519_bip32"
581584

582585
instance CastVerificationKeyRole StakeExtendedKey StakeKey where
586+
castVerificationKey :: HasCallStack => VerificationKey StakeExtendedKey -> VerificationKey StakeKey
583587
castVerificationKey (StakeExtendedVerificationKey vk) =
584588
StakeVerificationKey
585589
. Shelley.VKey
@@ -1033,6 +1037,8 @@ instance SerialiseAsBech32 (SigningKey CommitteeColdExtendedKey) where
10331037
bech32PrefixesPermitted _ = unsafeHumanReadablePartFromText <$> ["cc_cold_xsk"]
10341038

10351039
instance CastVerificationKeyRole CommitteeColdExtendedKey CommitteeColdKey where
1040+
castVerificationKey
1041+
:: HasCallStack => VerificationKey CommitteeColdExtendedKey -> VerificationKey CommitteeColdKey
10361042
castVerificationKey (CommitteeColdExtendedVerificationKey vk) =
10371043
CommitteeColdVerificationKey
10381044
. Shelley.VKey
@@ -1168,6 +1174,8 @@ instance SerialiseAsBech32 (SigningKey CommitteeHotExtendedKey) where
11681174
bech32PrefixesPermitted _ = unsafeHumanReadablePartFromText <$> ["cc_hot_xsk"]
11691175

11701176
instance CastVerificationKeyRole CommitteeHotExtendedKey CommitteeHotKey where
1177+
castVerificationKey
1178+
:: HasCallStack => VerificationKey CommitteeHotExtendedKey -> VerificationKey CommitteeHotKey
11711179
castVerificationKey (CommitteeHotExtendedVerificationKey vk) =
11721180
CommitteeHotVerificationKey
11731181
. Shelley.VKey
@@ -1307,6 +1315,8 @@ instance HasTextEnvelope (SigningKey GenesisExtendedKey) where
13071315
textEnvelopeType _ = "GenesisExtendedSigningKey_ed25519_bip32"
13081316

13091317
instance CastVerificationKeyRole GenesisExtendedKey GenesisKey where
1318+
castVerificationKey
1319+
:: HasCallStack => VerificationKey GenesisExtendedKey -> VerificationKey GenesisKey
13101320
castVerificationKey (GenesisExtendedVerificationKey vk) =
13111321
GenesisVerificationKey
13121322
. Shelley.VKey
@@ -1556,6 +1566,8 @@ instance HasTextEnvelope (SigningKey GenesisDelegateExtendedKey) where
15561566
textEnvelopeType _ = "GenesisDelegateExtendedSigningKey_ed25519_bip32"
15571567

15581568
instance CastVerificationKeyRole GenesisDelegateExtendedKey GenesisDelegateKey where
1569+
castVerificationKey
1570+
:: HasCallStack => VerificationKey GenesisDelegateExtendedKey -> VerificationKey GenesisDelegateKey
15591571
castVerificationKey (GenesisDelegateExtendedVerificationKey vk) =
15601572
GenesisDelegateVerificationKey
15611573
. Shelley.VKey
@@ -1966,6 +1978,8 @@ instance FromJSON (Hash StakePoolExtendedKey) where
19661978
Right h -> pure h
19671979

19681980
instance CastVerificationKeyRole StakePoolExtendedKey StakePoolKey where
1981+
castVerificationKey
1982+
:: HasCallStack => VerificationKey StakePoolExtendedKey -> VerificationKey StakePoolKey
19691983
castVerificationKey (StakePoolExtendedVerificationKey vk) =
19701984
StakePoolVerificationKey
19711985
. Shelley.VKey
@@ -2225,6 +2239,7 @@ instance SerialiseAsBech32 (SigningKey DRepExtendedKey) where
22252239
bech32PrefixesPermitted _ = unsafeHumanReadablePartFromText <$> ["drep_xsk"]
22262240

22272241
instance CastVerificationKeyRole DRepExtendedKey DRepKey where
2242+
castVerificationKey :: HasCallStack => VerificationKey DRepExtendedKey -> VerificationKey DRepKey
22282243
castVerificationKey (DRepExtendedVerificationKey vk) =
22292244
DRepVerificationKey
22302245
. Shelley.VKey

cardano-api/src/Cardano/Api/Key/Internal/Class.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import Cardano.Crypto.Seed qualified as Crypto
2222

2323
import Control.Monad.IO.Class
2424
import Data.Kind (Type)
25+
import GHC.Stack (HasCallStack)
2526
import System.Random (StdGen)
2627
import System.Random qualified as Random
2728

@@ -77,7 +78,8 @@ generateSigningKey keytype = do
7778
seedSize = deterministicSigningKeySeedSize keytype
7879

7980
generateInsecureSigningKey
80-
:: MonadIO m
81+
:: HasCallStack
82+
=> MonadIO m
8183
=> Key keyrole
8284
=> SerialiseAsRawBytes (SigningKey keyrole)
8385
=> StdGen

cardano-api/src/Cardano/Api/LedgerState.hs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ import Data.Word
270270
import Data.Yaml qualified as Yaml
271271
import Formatting.Buildable (build)
272272
import GHC.Exts (IsList (..))
273+
import GHC.Stack (HasCallStack)
273274
import Lens.Micro
274275
import Network.Mux qualified as Mux
275276
import Network.TypedProtocol.Core (Nat (..))
@@ -458,7 +459,7 @@ data FoldStatus
458459
-- the node's tip where @k@ is the security parameter.
459460
foldBlocks
460461
:: forall a t m
461-
. ()
462+
. HasCallStack
462463
=> Show a
463464
=> MonadIOTransError FoldBlocksError t m
464465
=> NodeConfigFile 'In
@@ -715,7 +716,8 @@ foldBlocks nodeConfigFilePath socketPath validationMode state0 accumulate = hand
715716
-- | Wrap a 'ChainSyncClient' with logic that tracks the ledger state.
716717
chainSyncClientWithLedgerState
717718
:: forall m a
718-
. Monad m
719+
. HasCallStack
720+
=> Monad m
719721
=> Env
720722
-> LedgerState
721723
-- ^ Initial ledger state
@@ -859,7 +861,8 @@ chainSyncClientWithLedgerState env ledgerState0 validationMode (CS.ChainSyncClie
859861
-- | See 'chainSyncClientWithLedgerState'.
860862
chainSyncClientPipelinedWithLedgerState
861863
:: forall m a
862-
. Monad m
864+
. HasCallStack
865+
=> Monad m
863866
=> Env
864867
-> LedgerState
865868
-> ValidationMode
@@ -2308,7 +2311,8 @@ getLedgerTablesUTxOValues sbe tbs =
23082311
-- provide a termination epoch otherwise blocks would be applied indefinitely.
23092312
foldEpochState
23102313
:: forall t m s
2311-
. MonadIOTransError FoldBlocksError t m
2314+
. HasCallStack
2315+
=> MonadIOTransError FoldBlocksError t m
23122316
=> NodeConfigFile 'In
23132317
-- ^ Path to the cardano-node config file (e.g. <path to cardano-node project>/configuration/cardano/mainnet-config.json)
23142318
-> SocketPath

cardano-api/src/Cardano/Api/Network/Internal/NetworkId.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import Cardano.Crypto.ProtocolMagic qualified as Byron
2525
import Cardano.Ledger.BaseTypes qualified as Shelley (Network (..))
2626
import Ouroboros.Network.Magic (NetworkMagic (..))
2727

28+
import GHC.Stack (HasCallStack)
29+
2830
-- ----------------------------------------------------------------------------
2931
-- NetworkId type
3032
--
@@ -74,7 +76,7 @@ toShelleyNetwork :: NetworkId -> Shelley.Network
7476
toShelleyNetwork Mainnet = Shelley.Mainnet
7577
toShelleyNetwork (Testnet _) = Shelley.Testnet
7678

77-
fromShelleyNetwork :: Shelley.Network -> NetworkMagic -> NetworkId
79+
fromShelleyNetwork :: HasCallStack => Shelley.Network -> NetworkMagic -> NetworkId
7880
fromShelleyNetwork Shelley.Testnet nm = Testnet nm
7981
fromShelleyNetwork Shelley.Mainnet nm
8082
| nm == mainnetNetworkMagic = Mainnet

cardano-api/src/Cardano/Api/Plutus/Internal/Script.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,7 +1416,8 @@ toShelleyMultiSig = go
14161416
go _ = Left MultiSigErrorTimelockNotsupported
14171417

14181418
-- | Conversion for the 'Shelley.MultiSig' language used by the Shelley era.
1419-
fromShelleyMultiSig :: Shelley.MultiSig (ShelleyLedgerEra ShelleyEra) -> SimpleScript
1419+
fromShelleyMultiSig
1420+
:: Shelley.MultiSig (ShelleyLedgerEra ShelleyEra) -> SimpleScript
14201421
fromShelleyMultiSig = go
14211422
where
14221423
go (Shelley.RequireSignature kh) =
@@ -1425,7 +1426,7 @@ fromShelleyMultiSig = go
14251426
go (Shelley.RequireAllOf s) = RequireAllOf (map go $ toList s)
14261427
go (Shelley.RequireAnyOf s) = RequireAnyOf (map go $ toList s)
14271428
go (Shelley.RequireMOf m s) = RequireMOf m (map go $ toList s)
1428-
go _ = error ""
1429+
go _ = error "fromShelleyMultiSig: Dijkstra era not supported"
14291430

14301431
-- | Conversion for the 'Timelock.Timelock' language that is shared between the
14311432
-- Allegra and Mary eras.
@@ -1459,7 +1460,7 @@ fromAllegraTimelock = go
14591460
go (Shelley.RequireAllOf s) = RequireAllOf (map go (toList s))
14601461
go (Shelley.RequireAnyOf s) = RequireAnyOf (map go (toList s))
14611462
go (Shelley.RequireMOf i s) = RequireMOf i (map go (toList s))
1462-
go _ = error "dijkstra"
1463+
go _ = error "fromAllegraTimelock: Dijkstra era not supported"
14631464

14641465
type family ToLedgerPlutusLanguage lang where
14651466
ToLedgerPlutusLanguage PlutusScriptV1 = Plutus.PlutusV1

cardano-api/src/Cardano/Api/Query/Internal/Type/QueryInMode.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ toConsensusQuery QueryLedgerConfig = Some Consensus.DebugLedgerConfig
572572

573573
toConsensusQueryShelleyBased
574574
:: forall era protocol block result
575-
. ()
575+
. HasCallStack
576576
=> ConsensusBlockForEra era ~ Consensus.ShelleyBlock protocol (ShelleyLedgerEra era)
577577
=> Consensus.CardanoBlock StandardCrypto ~ block
578578
=> L.EraGov (ShelleyLedgerEra era)

cardano-api/src/Cardano/Api/Tx/Internal/Body.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,7 @@ setTxTreasuryDonation
11851185
:: Maybe (Featured ConwayEraOnwards era L.Coin) -> TxBodyContent build era -> TxBodyContent build era
11861186
setTxTreasuryDonation v txBodyContent = txBodyContent{txTreasuryDonation = v}
11871187

1188-
getTxIdByron :: Byron.ATxAux ByteString -> TxId
1188+
getTxIdByron :: HasCallStack => Byron.ATxAux ByteString -> TxId
11891189
getTxIdByron (Byron.ATxAux{Byron.aTaTx = txbody}) =
11901190
TxId
11911191
. fromMaybe impossible

0 commit comments

Comments
 (0)