Skip to content

Commit 22f8005

Browse files
authored
Merge pull request #947 from IntersectMBO/mgalazyn/feature/rpc-add-plutusdata-support
gRPC: Add decoded PlutusData and NativeScript in proto definition
2 parents f62733d + adcef0d commit 22f8005

File tree

16 files changed

+5459
-2320
lines changed

16 files changed

+5459
-2320
lines changed

.hlint.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
- ignore: {name: Use camelCase, within: [Test.Cardano.Api.**, Test.Golden.Cardano.Api.**]}
6262

6363
# Ignore all files in cardano-rpc/gen (generated code)
64-
- ignore: {within: [Proto.Cardano, Proto.Utxorpc]}
64+
- ignore: {within: [Proto.Cardano.**, Proto.Utxorpc.**]}
6565

6666
- ignore: {name: Eta reduce}
6767
- ignore: {name: Use + directly}

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

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
module Cardano.Api.Error
99
( Error (..)
1010
, throwErrorAsException
11+
, liftEitherError
1112
, failEitherError
1213
, ErrorAsException (..)
1314
, FileError (..)
@@ -20,7 +21,8 @@ where
2021
import Cardano.Api.Monad.Error
2122
import Cardano.Api.Pretty
2223

23-
import Control.Exception (Exception (..), IOException, throwIO)
24+
import Control.Exception.Safe
25+
import GHC.Stack
2426
import System.Directory (doesFileExist)
2527
import System.IO (Handle)
2628

@@ -32,26 +34,49 @@ instance Error () where
3234

3335
-- | The preferred approach is to use 'Except' or 'ExceptT', but you can if
3436
-- necessary use IO exceptions.
35-
throwErrorAsException :: Error e => e -> IO a
36-
throwErrorAsException e = throwIO (ErrorAsException e)
37-
38-
failEitherError :: MonadFail m => Error e => Either e a -> m a
37+
throwErrorAsException
38+
:: HasCallStack
39+
=> MonadThrow m
40+
=> Typeable e
41+
=> Error e
42+
=> e
43+
-> m a
44+
throwErrorAsException e = withFrozenCallStack $ throwM $ ErrorAsException e
45+
46+
-- | Pretty print 'Error e' and 'fail' if 'Left'.
47+
failEitherError
48+
:: MonadFail m
49+
=> Error e
50+
=> Either e a
51+
-> m a
3952
failEitherError = failEitherWith displayError
4053

54+
-- | Pretty print 'Error e' and 'throwM' it wrapped in 'ErrorAsException' when 'Left'.
55+
liftEitherError
56+
:: HasCallStack
57+
=> MonadThrow m
58+
=> Typeable e
59+
=> Error e
60+
=> Either e a
61+
-> m a
62+
liftEitherError = withFrozenCallStack $ either throwErrorAsException pure
63+
64+
-- | An exception wrapping any 'Error e', attaching a call stack from the construction place to it.
4165
data ErrorAsException where
42-
ErrorAsException :: Error e => e -> ErrorAsException
66+
ErrorAsException :: (HasCallStack, Typeable e, Error e) => e -> ErrorAsException
67+
68+
instance Exception ErrorAsException
4369

70+
-- | Pretty print the error inside the exception
4471
instance Error ErrorAsException where
4572
prettyError (ErrorAsException e) =
4673
prettyError e
4774

75+
-- | Pretty print the error inside the exception followed by the call stack pointing to the place where 'Error e' was
76+
-- wrapped in 'ErrorAsException'
4877
instance Show ErrorAsException where
4978
show (ErrorAsException e) =
50-
docToString $ prettyError e
51-
52-
instance Exception ErrorAsException where
53-
displayException (ErrorAsException e) =
54-
docToString $ prettyError e
79+
docToString (prettyError e) <> "\n" <> prettyCallStack callStack
5580

5681
displayError :: Error a => a -> String
5782
displayError = docToString . prettyError

cardano-api/src/Cardano/Api/Experimental/Era.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,9 @@ type EraCommonConstraints era =
309309
, L.EraTxCert (LedgerEra era)
310310
, L.EraTxOut (LedgerEra era)
311311
, L.EraUTxO (LedgerEra era)
312+
, L.Value (LedgerEra era) ~ L.MaryValue
312313
, FromCBOR (ChainDepState (ConsensusProtocol era))
313-
, -- , FromCBOR (L.TxCert (LedgerEra era))
314-
L.NativeScript (LedgerEra era) ~ L.Timelock (LedgerEra era)
314+
, L.NativeScript (LedgerEra era) ~ L.Timelock (LedgerEra era)
315315
, PraosProtocolSupportsNode (ConsensusProtocol era)
316316
, ShelleyLedgerEra era ~ LedgerEra era
317317
, ToJSON (ChainDepState (ConsensusProtocol era))

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ module Cardano.Api.HasTypeProxy
1313
where
1414

1515
import Data.ByteString qualified as BS
16+
import Data.ByteString.Lazy qualified as BSL
1617
import Data.Kind (Constraint, Type)
1718
import Data.Proxy (Proxy (..))
1819
import Data.Typeable (Typeable)
1920
import Data.Word (Word16, Word8)
21+
import Numeric.Natural (Natural)
2022

2123
class Typeable t => HasTypeProxy t where
2224
-- | A family of singleton types used in this API to indicate which type to
@@ -35,10 +37,18 @@ instance HasTypeProxy Word16 where
3537
data AsType Word16 = AsWord16
3638
proxyToAsType _ = AsWord16
3739

40+
instance HasTypeProxy Natural where
41+
data AsType Natural = AsNatural
42+
proxyToAsType _ = AsNatural
43+
3844
instance HasTypeProxy BS.ByteString where
3945
data AsType BS.ByteString = AsByteString
4046
proxyToAsType _ = AsByteString
4147

48+
instance HasTypeProxy BSL.ByteString where
49+
data AsType BSL.ByteString = AsByteStringLazy
50+
proxyToAsType _ = AsByteStringLazy
51+
4252
data FromSomeType (c :: Type -> Constraint) b where
4353
FromSomeType :: c a => AsType a -> (a -> b) -> FromSomeType c b
4454

cardano-api/src/Cardano/Api/Ledger/Internal/Reexport.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ module Cardano.Api.Ledger.Internal.Reexport
5959
, TxId (..)
6060
, TxIn (..)
6161
, Value
62+
, MaryValue (..)
6263
, MultiAsset (..)
6364
, addDeltaCoin
6465
, castSafeHash
@@ -343,7 +344,7 @@ import Cardano.Ledger.Keys
343344
, hashWithSerialiser
344345
, toVRFVerKeyHash
345346
)
346-
import Cardano.Ledger.Mary.Value (MultiAsset (..))
347+
import Cardano.Ledger.Mary.Value (MaryValue (..), MultiAsset (..))
347348
import Cardano.Ledger.Plutus.Data (Data (..), unData)
348349
import Cardano.Ledger.Plutus.Language (Language, Plutus, languageToText, plutusBinary)
349350
import Cardano.Ledger.Shelley.API

cardano-api/src/Cardano/Api/Serialise/Raw.hs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,17 @@ import Data.Bits (Bits (..))
2626
import Data.ByteString qualified as BS
2727
import Data.ByteString.Base16 qualified as Base16
2828
import Data.ByteString.Builder qualified as BSB
29-
import Data.ByteString.Char8 as BSC
29+
import Data.ByteString.Char8 (ByteString)
30+
import Data.ByteString.Char8 qualified as BSC
31+
import Data.ByteString.Lazy qualified as BSL
3032
import Data.Data (typeRep)
33+
import Data.Foldable qualified as F
3134
import Data.Text (Text)
3235
import Data.Text qualified as Text
3336
import Data.Text.Encoding qualified as Text
3437
import Data.Typeable (TypeRep, Typeable)
3538
import Data.Word (Word16, Word8)
39+
import Numeric.Natural (Natural)
3640

3741
class (HasTypeProxy a, Typeable a) => SerialiseAsRawBytes a where
3842
serialiseToRawBytes :: a -> ByteString
@@ -60,10 +64,24 @@ instance SerialiseAsRawBytes Word16 where
6064
throwError . SerialiseAsRawBytesError $
6165
"Cannot decode Word16 from (hex): " <> BSC.unpack (Base16.encode bs)
6266

67+
-- | Convert the number into binary value
68+
instance SerialiseAsRawBytes Natural where
69+
serialiseToRawBytes 0 = BS.singleton 0x00
70+
serialiseToRawBytes n = BS.toStrict . BSB.toLazyByteString $ go n mempty
71+
where
72+
go 0 acc = acc
73+
go x acc = go (x `shiftR` 8) (BSB.word8 (fromIntegral (x .&. 0xFF)) <> acc)
74+
deserialiseFromRawBytes AsNatural "\x00" = pure 0
75+
deserialiseFromRawBytes AsNatural input = pure . F.foldl' (\acc byte -> acc `shiftL` 8 .|. fromIntegral byte) 0 $ BS.unpack input
76+
6377
instance SerialiseAsRawBytes BS.ByteString where
6478
serialiseToRawBytes = id
6579
deserialiseFromRawBytes AsByteString = pure
6680

81+
instance SerialiseAsRawBytes BSL.ByteString where
82+
serialiseToRawBytes = BSL.toStrict
83+
deserialiseFromRawBytes AsByteStringLazy = pure . BSL.fromStrict
84+
6785
serialiseToRawBytesHex :: SerialiseAsRawBytes a => a -> ByteString
6886
serialiseToRawBytesHex = Base16.encode . serialiseToRawBytes
6987

cardano-api/src/Cardano/Api/Serialise/SerialiseUsing.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ import Cardano.Api.Serialise.Json
1717
import Cardano.Api.Serialise.Raw
1818

1919
import Data.Aeson.Types qualified as Aeson
20+
import Data.ByteString qualified as B
2021
import Data.Text.Encoding qualified as Text
2122
import Data.Typeable (tyConName, typeRep, typeRepTyCon)
23+
import Numeric (showBin)
2224

2325
-- | For use with @deriving via@, to provide 'ToCBOR' and 'FromCBOR' instances,
2426
-- based on the 'SerialiseAsRawBytes' instance.
@@ -39,6 +41,10 @@ instance SerialiseAsRawBytes a => FromCBOR (UsingRawBytes a) where
3941
ttoken = proxyToAsType (Proxy :: Proxy a)
4042
tname = (tyConName . typeRepTyCon . typeRep) (Proxy :: Proxy a)
4143

44+
-- | Prints the representation in binary format, quoted
45+
instance SerialiseAsRawBytes a => Show (UsingRawBytes a) where
46+
showsPrec _ (UsingRawBytes x) = showChar '"' . mconcat (map showBin . B.unpack $ serialiseToRawBytes x) . showChar '"'
47+
4248
-- | For use with @deriving via@, to provide instances for any\/all of 'Show',
4349
-- 'ToJSON', 'FromJSON', 'ToJSONKey', FromJSONKey' using a hex
4450
-- encoding, based on the 'SerialiseAsRawBytes' instance.

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ import Cardano.Ledger.Api qualified as L
8484
import Cardano.Ledger.Coin qualified as L
8585
import Cardano.Ledger.Conway.Governance qualified as L
8686
import Cardano.Ledger.Credential as Ledger (Credential)
87-
import Cardano.Ledger.Mary.Value qualified as L
8887
import Cardano.Ledger.Plutus.Language qualified as Plutus
8988
import Cardano.Ledger.Val qualified as L
9089
import Ouroboros.Consensus.HardFork.History qualified as Consensus

cardano-rpc/cardano-rpc.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ library
6969
bytestring,
7070
cardano-api >=10.17,
7171
cardano-ledger-api,
72-
cardano-ledger-binary,
7372
cardano-ledger-conway,
7473
cardano-ledger-core,
7574
cardano-rpc:gen,
@@ -132,3 +131,4 @@ test-suite cardano-rpc-test
132131
build-tool-depends: tasty-discover:tasty-discover
133132
other-modules:
134133
Test.Cardano.Rpc.ProtocolParameters
134+
Test.Cardano.Rpc.TxOutput

0 commit comments

Comments
 (0)