Skip to content

Commit a2df13b

Browse files
authored
Merge pull request #1021 from IntersectMBO/mgalazyn/test/add-utxorpc-conversion-functions-for-test
gRPC: Add TxOut CBOR representation to `readUtxos` method, fix address serialisation in TxOutput.
2 parents eeb2a3b + e60c6f3 commit a2df13b

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

cardano-rpc/cardano-rpc.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ library
6868
base,
6969
bytestring,
7070
cardano-api >=10.17,
71+
cardano-binary,
7172
cardano-ledger-api,
7273
cardano-ledger-conway,
7374
cardano-ledger-core,

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

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
module Cardano.Rpc.Server.Internal.UtxoRpc.Type
1414
( utxoRpcPParamsToProtocolParams
1515
, utxoToUtxoRpcAnyUtxoData
16+
, anyUtxoDataUtxoRpcToUtxo
1617
, txOutToUtxoRpcTxOutput
1718
, utxoRpcTxOutputToTxOut
1819
, protocolParamsToUtxoRpcPParams
@@ -37,6 +38,7 @@ import Cardano.Api.Value
3738
import Cardano.Rpc.Proto.Api.UtxoRpc.Query qualified as UtxoRpc
3839
import Cardano.Rpc.Server.Internal.Orphans ()
3940

41+
import Cardano.Binary qualified as CBOR
4042
import Cardano.Ledger.Api qualified as L
4143
import Cardano.Ledger.BaseTypes (WithOrigin (..))
4244
import Cardano.Ledger.BaseTypes qualified as L
@@ -430,13 +432,52 @@ scriptDataToUtxoRpcPlutusData = \case
430432
utxoToUtxoRpcAnyUtxoData :: forall era. IsEra era => UTxO era -> [Proto UtxoRpc.AnyUtxoData]
431433
utxoToUtxoRpcAnyUtxoData utxo =
432434
toList utxo <&> \(txIn, txOut) -> do
435+
let era = useEra @era
436+
txOutCbor =
437+
obtainCommonConstraints era $
438+
CBOR.serialize' $
439+
toShelleyTxOut (convert era) txOut
433440
defMessage
434-
& #nativeBytes .~ "" -- TODO where to get that from? run cbor serialisation of utxos list?
441+
& #nativeBytes .~ txOutCbor
435442
& #txoRef .~ inject txIn
436443
& #cardano .~ txOutToUtxoRpcTxOutput txOut
437444

445+
anyUtxoDataUtxoRpcToUtxo
446+
:: forall era m
447+
. HasCallStack
448+
=> MonadThrow m
449+
=> Era era
450+
-> [Proto UtxoRpc.AnyUtxoData]
451+
-> m (UTxO era)
452+
anyUtxoDataUtxoRpcToUtxo era = fmap fromList . foldM f mempty
453+
where
454+
f
455+
:: [(TxIn, TxOut CtxUTxO era)]
456+
-> Proto UtxoRpc.AnyUtxoData
457+
-> m [(TxIn, TxOut CtxUTxO era)]
458+
f acc e = do
459+
txOut <- obtainCommonConstraints era $ utxoRpcTxOutputToTxOut $ e ^. #cardano
460+
txIn <- txoRefUtxoRpcToTxIn $ e ^. #txoRef
461+
pure $ (txIn, txOut) : acc
462+
463+
txoRefUtxoRpcToTxIn
464+
:: forall m
465+
. HasCallStack
466+
=> MonadThrow m
467+
=> Proto UtxoRpc.TxoRef
468+
-> m TxIn
469+
txoRefUtxoRpcToTxIn txoRef = do
470+
txId' <-
471+
liftEitherError $
472+
deserialiseFromRawBytes asType $
473+
txoRef ^. #hash
474+
pure $ TxIn txId' (TxIx . fromIntegral $ txoRef ^. #index)
475+
438476
txOutToUtxoRpcTxOutput
439-
:: forall era. IsEra era => TxOut CtxUTxO era -> Proto UtxoRpc.TxOutput
477+
:: forall era
478+
. IsEra era
479+
=> TxOut CtxUTxO era
480+
-> Proto UtxoRpc.TxOutput
440481
txOutToUtxoRpcTxOutput (TxOut addressInEra txOutValue datum script) = do
441482
let multiAsset =
442483
fromList $
@@ -487,7 +528,8 @@ utxoRpcTxOutputToTxOut txOutput = do
487528
addrUtf8 <- liftEitherError $ T.decodeUtf8' (txOutput ^. #address)
488529
address <-
489530
maybe (throwM . stringException $ "Cannot decode address: " <> T.unpack addrUtf8) pure $
490-
deserialiseAddress (AsAddress AsShelleyAddr) addrUtf8
531+
obtainCommonConstraints era $
532+
deserialiseAddress asType addrUtf8
491533
datum <-
492534
case txOutput ^. #maybe'datum of
493535
Just datumRpc ->
@@ -515,7 +557,7 @@ utxoRpcTxOutputToTxOut txOutput = do
515557
pure (AssetId pId assetName, outCoin <> mintCoin)
516558
pure $
517559
TxOut
518-
(AddressInEra (ShelleyAddressInEra (convert era)) address)
560+
address
519561
( obtainCommonConstraints era $
520562
TxOutValueShelleyBased (convert era) (toMaryValue $ coinValue <> multiAssetValue)
521563
)

0 commit comments

Comments
 (0)