Skip to content

Commit

Permalink
Merge #691
Browse files Browse the repository at this point in the history
691: display call stack for unsuccessful unsafe calls r=piotr-iohk a=KtorZ

# Issue Number

<!-- Put here a reference to the issue this PR relates to and which requirements it tackles -->


# Overview

<!-- Detail in a few bullet points the work accomplished in this PR -->

- [x] I have added "stack traces" for unsafe calls when they fail.

# Comments

<!-- Additional comments or screenshots to attach if any -->

It's not much, but it turns:

```
       uncaught exception: ErrorCall
       TextDecodingError {getTextDecodingError = "Unable to decode Address: encoding is neither Bech32 nor Base58."}
       CallStack (from HasCallStack):
         error, called at src/Cardano/Wallet/Unsafe.hs:56:13 in cardano-wallet-core-2019.7.24-vtyOM1MPP4BB3M0K9gXCg:Cardano.Wallet.Unsafe
```


into

```
       uncaught exception: ErrorCall
       TextDecodingError {getTextDecodingError = "Unable to decode Address: encoding is neither Bech32 nor Base58."}
       CallStack (from HasCallStack):
         error, called at src/Cardano/Wallet/Unsafe.hs:58:13 in cardano-wallet-core-2019.7.24-vtyOM1MPP4BB3M0K9gXCg:Cardano.Wallet.Unsafe
         unsafeDecodeAddress, called at test/unit/Cardano/Wallet/Jormungandr/CompatibilitySpec.hs:121:21 in main:Cardano.Wallet.Jormungandr.CompatibilitySpec
```

which makes it easier to locate the point of failure.

<!-- 
Don't forget to:

 ✓ Self-review your changes to make sure nothing unexpected slipped through
 ✓ Assign yourself to the PR
 ✓ Assign one or several reviewer(s)
 ✓ Once created, link this PR to its corresponding ticket
 ✓ Acknowledge any changes required to the Wiki
-->


Co-authored-by: KtorZ <matthias.benkort@gmail.com>
  • Loading branch information
iohk-bors[bot] and KtorZ committed Sep 11, 2019
2 parents f52aee8 + 9d6e22b commit bcc06c3
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/core/src/Cardano/Wallet/Unsafe.hs
@@ -1,3 +1,4 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
Expand Down Expand Up @@ -38,6 +39,8 @@ import Data.Proxy
( Proxy )
import Data.Text
( Text )
import GHC.Stack
( HasCallStack )

import qualified Cardano.Crypto.Wallet as CC
import qualified Codec.CBOR.Decoding as CBOR
Expand All @@ -46,29 +49,30 @@ import qualified Data.ByteString.Lazy as BL


-- | Decode an hex-encoded 'ByteString' into raw bytes, or fail.
unsafeFromHex :: ByteString -> ByteString
unsafeFromHex :: HasCallStack => ByteString -> ByteString
unsafeFromHex =
either (error . show) id . convertFromBase @ByteString @ByteString Base16

-- | Decode a bech32-encoded 'Text' into an 'Address', or fail.
unsafeDecodeAddress :: DecodeAddress t => Proxy t -> Text -> Address
unsafeDecodeAddress :: (HasCallStack, DecodeAddress t) => Proxy t -> Text -> Address
unsafeDecodeAddress proxy =
either (error . show ) id . decodeAddress proxy

-- | Run a decoder on a hex-encoded 'ByteString', or fail.
unsafeDecodeHex :: Get a -> ByteString -> a
unsafeDecodeHex :: HasCallStack => Get a -> ByteString -> a
unsafeDecodeHex get = runGet get . BL.fromStrict . unsafeFromHex

-- | Build a 'XPrv' from an hex-encoded bytestring
unsafeXPrv :: ByteString -> XPrv
unsafeXPrv :: HasCallStack => ByteString -> XPrv
unsafeXPrv hex =
case convertFromBase @_ @ByteString Base16 hex >>= CC.xprv of
Left e -> error $ "unsafeXPrv: " <> e
Right a -> a

-- | Build 'Mnemonic' from literals
unsafeMkMnemonic
:: forall mw n csz. (ConsistentEntropy n mw csz, EntropySize mw ~ n)
:: forall mw n csz
. (ConsistentEntropy n mw csz, EntropySize mw ~ n, HasCallStack)
=> [Text]
-> Mnemonic mw
unsafeMkMnemonic m =
Expand All @@ -88,7 +92,8 @@ unsafeRunExceptT = runExceptT >=> \case

-- | CBOR deserialise without error handling - handy for prototypes or testing.
unsafeDeserialiseCbor
:: (forall s. CBOR.Decoder s a)
:: HasCallStack
=> (forall s. CBOR.Decoder s a)
-> BL.ByteString
-> a
unsafeDeserialiseCbor decoder bytes = either
Expand Down

0 comments on commit bcc06c3

Please sign in to comment.