Skip to content

Commit 147cb62

Browse files
committed
wip
1 parent bc51380 commit 147cb62

File tree

3 files changed

+69
-35
lines changed

3 files changed

+69
-35
lines changed

cardano-rpc/src/Cardano/Rpc/Server/Internal/Orphans.hs

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -78,40 +78,6 @@ instance Inject TxIn (Proto UtxoRpc.TxoRef) where
7878
& #hash .~ serialiseToRawBytes txId'
7979
& #index .~ fromIntegral txIx
8080

81-
instance Inject ScriptData (Proto UtxoRpc.PlutusData) where
82-
inject = \case
83-
ScriptDataBytes bs ->
84-
defMessage & #boundedBytes .~ bs
85-
ScriptDataNumber int
86-
| int <= fromIntegral (maxBound @Int64)
87-
&& int >= fromIntegral (minBound @Int64) ->
88-
defMessage & #bigInt . #int .~ fromIntegral int
89-
| int < 0 ->
90-
-- https://www.rfc-editor.org/rfc/rfc8949.html#name-bignums see 3.4.3 for negative integers
91-
defMessage & #bigInt . #bigNInt .~ serialiseToRawBytes (fromIntegral @_ @Natural (-1 - int))
92-
| otherwise ->
93-
defMessage & #bigInt . #bigUInt .~ serialiseToRawBytes (fromIntegral @_ @Natural int)
94-
ScriptDataList sds ->
95-
defMessage & #array . #items .~ map inject sds
96-
ScriptDataMap elements -> do
97-
let pairs =
98-
elements <&> \(k, v) ->
99-
defMessage
100-
& #key .~ inject k
101-
& #value .~ inject v
102-
defMessage & #map . #pairs .~ pairs
103-
ScriptDataConstructor tag args -> do
104-
-- Details of plutus tag serialisation:
105-
-- https://github.com/IntersectMBO/plutus/blob/fc78c36b545ee287ae8796a0c1a7d04cf31f4cee/plutus-core/plutus-core/src/PlutusCore/Data.hs#L72
106-
let constr =
107-
defMessage
108-
& ( if tag <= fromIntegral (maxBound @Word32)
109-
then #tag .~ fromIntegral tag
110-
else (#tag .~ 102) . (#anyConstructor .~ fromIntegral @_ @Word64 tag)
111-
)
112-
& #fields .~ map inject args
113-
defMessage & #constr .~ constr
114-
11581
instance L.ConwayEraPParams lera => Inject (L.PParams lera) (Proto UtxoRpc.PParams) where
11682
inject pparams = do
11783
let pparamsCostModels :: Map L.Language [Int64] =

cardano-rpc/src/Cardano/Rpc/Server/Internal/UtxoRpc/Type.hs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{-# LANGUAGE DerivingStrategies #-}
22
{-# LANGUAGE FlexibleContexts #-}
33
{-# LANGUAGE FlexibleInstances #-}
4+
{-# LANGUAGE GADTs #-}
45
{-# LANGUAGE LambdaCase #-}
56
{-# LANGUAGE MultiParamTypeClasses #-}
67
{-# LANGUAGE OverloadedLabels #-}
@@ -268,6 +269,40 @@ referenceScriptToUtxoRpcScript (ReferenceScript _ (ScriptInAnyLang _ script)) =
268269
PlutusScript PlutusScriptV3 ps ->
269270
defMessage & #plutusV3 .~ serialiseToRawBytes ps
270271

272+
scriptDataToUtxoRpcPlutusData :: ScriptData -> Proto UtxoRpc.PlutusData
273+
scriptDataToUtxoRpcPlutusData = \case
274+
ScriptDataBytes bs ->
275+
defMessage & #boundedBytes .~ bs
276+
ScriptDataNumber int
277+
| int <= fromIntegral (maxBound @Int64)
278+
&& int >= fromIntegral (minBound @Int64) ->
279+
defMessage & #bigInt . #int .~ fromIntegral int
280+
| int < 0 ->
281+
-- https://www.rfc-editor.org/rfc/rfc8949.html#name-bignums see 3.4.3 for negative integers
282+
defMessage & #bigInt . #bigNInt .~ serialiseToRawBytes (fromIntegral @_ @Natural (-1 - int))
283+
| otherwise ->
284+
defMessage & #bigInt . #bigUInt .~ serialiseToRawBytes (fromIntegral @_ @Natural int)
285+
ScriptDataList sds ->
286+
defMessage & #array . #items .~ map scriptDataToUtxoRpcPlutusData sds
287+
ScriptDataMap elements -> do
288+
let pairs =
289+
elements <&> \(k, v) ->
290+
defMessage
291+
& #key .~ scriptDataToUtxoRpcPlutusData k
292+
& #value .~ scriptDataToUtxoRpcPlutusData v
293+
defMessage & #map . #pairs .~ pairs
294+
ScriptDataConstructor tag args -> do
295+
-- Details of plutus tag serialisation:
296+
-- https://github.com/IntersectMBO/plutus/blob/fc78c36b545ee287ae8796a0c1a7d04cf31f4cee/plutus-core/plutus-core/src/PlutusCore/Data.hs#L72
297+
let constr =
298+
defMessage
299+
& ( if tag <= fromIntegral (maxBound @Word32)
300+
then #tag .~ fromIntegral tag
301+
else (#tag .~ 102) . (#anyConstructor .~ fromIntegral @_ @Word64 tag)
302+
)
303+
& #fields .~ map scriptDataToUtxoRpcPlutusData args
304+
defMessage & #constr .~ constr
305+
271306
utxoToUtxoRpcAnyUtxoData :: forall era. IsCardanoEra era => UTxO era -> [Proto UtxoRpc.AnyUtxoData]
272307
utxoToUtxoRpcAnyUtxoData utxo =
273308
toList utxo <&> \(txIn, TxOut addressInEra txOutValue datum script) -> do
@@ -296,7 +331,7 @@ utxoToUtxoRpcAnyUtxoData utxo =
296331
TxOutDatumInline _ hashableScriptData ->
297332
defMessage
298333
& #hash .~ serialiseToRawBytes (hashScriptDataBytes hashableScriptData)
299-
& #payload .~ inject (getScriptData hashableScriptData)
334+
& #payload .~ scriptDataToUtxoRpcPlutusData (getScriptData hashableScriptData)
300335
& #originalCbor .~ getOriginalScriptDataBytes hashableScriptData
301336

302337
protoTxOut =
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{-# LANGUAGE OverloadedLists #-}
2+
{-# LANGUAGE RankNTypes #-}
3+
{-# LANGUAGE ScopedTypeVariables #-}
4+
{-# LANGUAGE TypeApplications #-}
5+
6+
module Test.Cardano.Rpc.TxOutput where
7+
8+
import Cardano.Api.Experimental.Era
9+
import Cardano.Api.ProtocolParameters
10+
import Cardano.Rpc.Server.Internal.UtxoRpc.Type
11+
12+
import Cardano.Ledger.Api qualified as L
13+
import Cardano.Ledger.BaseTypes qualified as L
14+
import Cardano.Ledger.Coin qualified as L
15+
import Cardano.Ledger.Conway.PParams qualified as L
16+
import Cardano.Ledger.Plutus qualified as L
17+
18+
import RIO
19+
20+
import Data.Bits
21+
import Data.Map.Strict qualified as M
22+
import Data.Ratio
23+
import GHC.IsList
24+
25+
import Test.Gen.Cardano.Api.Typed (genValidProtocolParameters)
26+
27+
import Hedgehog
28+
import Hedgehog qualified as H
29+
30+
-- | Test if protocol parameters roundtrip between ledger and proto representation
31+
hprop_roundtrip_tx_output :: Property
32+
hprop_roundtrip_tx_output = H.property $ do
33+
pure ()

0 commit comments

Comments
 (0)