-
Notifications
You must be signed in to change notification settings - Fork 44
/
Debug.hs
106 lines (85 loc) · 3.14 KB
/
Debug.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
-- | Functions for passing serialised data from Plutus scripts and then decoding them.
{-# LANGUAGE TupleSections #-}
module Plutus.Debug (
-- * Debugging
debug
, debugIfTrue
, debugIfFalse
, debugError
-- * Decoding
, recoverFromData
, recoverPlutusData
) where
import Codec.Serialise (deserialise)
import Plutus.V1.Ledger.Api (ToData (..), unsafeFromBuiltinData)
import PlutusTx (Data, FromData (..), fromData)
import PlutusTx.Prelude (BuiltinString, decodeUtf8, encodeUtf8, error, fromBuiltin, trace)
import Prelude hiding (error)
import qualified Data.ByteString.Base16 as Base16 (decode)
import qualified Data.ByteString.Char8 as BS8 (pack)
import qualified Data.ByteString.Lazy as LBS (fromStrict)
import qualified Data.Text as T (unpack)
{-# INLINE debug #-}
-- | Write a message and serialised data to the trace log.
debug :: ToData a
=> BuiltinString -- ^ A message.
-> a -- ^ The data.
-> b -- ^ The value to be returned.
-> b -- ^ The vaule returned.
debug message =
trace
. decodeUtf8
. unsafeFromBuiltinData
. toBuiltinData
. (encodeUtf8 message, )
-- | Write a message and serialised data to the trace log, if a condition holds.
debugIfTrue :: ToData a
=> BuiltinString -- ^ The message.
-> a -- ^ The data.
-> Bool -- ^ The condition to be returned.
-> Bool -- ^ The condition.
debugIfTrue message x condition =
if condition
then debug message x True
else False
-- | Write a message and serialised data to the trace log, if a condition does not hold.
debugIfFalse :: ToData a
=> BuiltinString -- ^ The message.
-> a -- ^ The data.
-> Bool -- ^ The condition to be returned.
-> Bool -- ^ The condition.
debugIfFalse message x condition =
if condition
then True
else debug message x False
-- | Raise an error after writing a message and serialised data to the trace log.
debugError :: ToData a
=> BuiltinString -- ^ The message.
-> a -- ^ The data.
-> b -- ^ The type to be returned.
debugError message x = error $ debug message x ()
-- | Recover serialised debugging data.
recoverPlutusData :: String -- ^ The base-16 encoding of the data.
-> Either String Data -- ^ The Plutus data.
recoverPlutusData raw =
do
bytes <- Base16.decode . BS8.pack $ raw
pure
. deserialise
. LBS.fromStrict
$ bytes
-- | Recover serialised debugging data.
recoverFromData :: FromData a
=> String -- ^ The base-16 encoding of the data.
-> Either String (String, a) -- ^ The original message and data.
recoverFromData raw =
do
raw' <- recoverPlutusData raw
(message, x) <-
maybe (Left "Failed decoding Plutus data.") Right
$ fromData raw'
pure
(
T.unpack . fromBuiltin . decodeUtf8 $ message
, x
)