Skip to content

Commit 6fb9d5b

Browse files
committed
wip
1 parent b5686e2 commit 6fb9d5b

File tree

4 files changed

+93
-19
lines changed

4 files changed

+93
-19
lines changed

cardano-rpc/cardano-rpc.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,4 @@ test-suite cardano-rpc-test
135135
other-modules:
136136
Test.Cardano.Rpc.ProtocolParameters
137137
Test.Cardano.Rpc.TxOutput
138+
Test.Cardano.Rpc.Type

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
{-# LANGUAGE OverloadedLabels #-}
55
{-# LANGUAGE RankNTypes #-}
66
{-# LANGUAGE ScopedTypeVariables #-}
7+
{-# LANGUAGE TypeApplications #-}
78
{-# OPTIONS_GHC -Wno-orphans #-}
89

910
module Cardano.Rpc.Server.Internal.Orphans where
@@ -63,6 +64,23 @@ instance Inject TxIn (Proto UtxoRpc.TxoRef) where
6364
instance Message a => Default (Proto a) where
6465
def = defMessage
6566

67+
instance Inject Integer (Proto UtxoRpc.BigInt) where
68+
inject int
69+
| int <= fromIntegral (maxBound @Int64)
70+
&& int >= fromIntegral (minBound @Int64) =
71+
inject @Int64 $ fromIntegral int
72+
| int < 0 =
73+
-- https://www.rfc-editor.org/rfc/rfc8949.html#name-bignums see 3.4.3 for negative integers
74+
defMessage & #bigNInt .~ serialiseToRawBytes (fromIntegral @_ @Natural (-1 - int))
75+
| otherwise =
76+
defMessage & #bigUInt .~ serialiseToRawBytes (fromIntegral @_ @Natural int)
77+
78+
instance Inject Int64 (Proto UtxoRpc.BigInt) where
79+
inject int = defMessage & #int .~ int
80+
81+
instance Inject L.Coin (Proto UtxoRpc.BigInt) where
82+
inject = inject . fromIntegral @_ @Integer
83+
6684
-----------
6785
-- Errors
6886
-----------

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

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module Cardano.Rpc.Server.Internal.UtxoRpc.Type
1818
, utxoRpcTxOutputToTxOut
1919
, protocolParamsToUtxoRpcPParams
2020
, simpleScriptToUtxoRpcNativeScript
21+
, utxoRpcBigIntToInteger
2122
, mkChainPointMsg
2223
)
2324
where
@@ -70,20 +71,20 @@ protocolParamsToUtxoRpcPParams era pparams = obtainCommonConstraints era $ do
7071
drepVotingThresholds :: L.DRepVotingThresholds =
7172
pparams ^. L.ppDRepVotingThresholdsL
7273
def
73-
& #coinsPerUtxoByte .~ pparams ^. L.ppCoinsPerUTxOByteL . to L.unCoinPerByte . to fromIntegral
74+
& #coinsPerUtxoByte .~ pparams ^. L.ppCoinsPerUTxOByteL . to L.unCoinPerByte . to inject
7475
& #maxTxSize .~ pparams ^. L.ppMaxTxSizeL . to fromIntegral
75-
& #minFeeCoefficient .~ pparams ^. L.ppMinFeeBL . to fromIntegral
76-
& #minFeeConstant .~ pparams ^. L.ppMinFeeAL . to fromIntegral
76+
& #minFeeCoefficient .~ pparams ^. L.ppMinFeeBL . to inject
77+
& #minFeeConstant .~ pparams ^. L.ppMinFeeAL . to inject
7778
& #maxBlockBodySize .~ pparams ^. L.ppMaxBBSizeL . to fromIntegral
7879
& #maxBlockHeaderSize .~ pparams ^. L.ppMaxBHSizeL . to fromIntegral
79-
& #stakeKeyDeposit .~ pparams ^. L.ppKeyDepositL . to fromIntegral
80-
& #poolDeposit .~ pparams ^. L.ppPoolDepositL . to fromIntegral
80+
& #stakeKeyDeposit .~ pparams ^. L.ppKeyDepositL . to inject
81+
& #poolDeposit .~ pparams ^. L.ppPoolDepositL . to inject
8182
& #poolRetirementEpochBound .~ pparams ^. L.ppEMaxL . to L.unEpochInterval . to fromIntegral
8283
& #desiredNumberOfPools .~ pparams ^. L.ppNOptL . to fromIntegral
8384
& #poolInfluence .~ pparams ^. L.ppA0L . to L.unboundRational . to inject
8485
& #monetaryExpansion .~ pparams ^. L.ppRhoL . to L.unboundRational . to inject
8586
& #treasuryExpansion .~ pparams ^. L.ppTauL . to L.unboundRational . to inject
86-
& #minPoolCost .~ pparams ^. L.ppMinPoolCostL . to fromIntegral
87+
& #minPoolCost .~ pparams ^. L.ppMinPoolCostL . to inject
8788
& #protocolVersion . #major .~ pparams ^. L.ppProtocolVersionL . to L.pvMajor . to L.getVersion
8889
& #protocolVersion . #minor .~ pparams ^. L.ppProtocolVersionL . to L.pvMinor . to fromIntegral
8990
& #maxValueSize .~ pparams ^. L.ppMaxValSizeL . to fromIntegral
@@ -129,8 +130,8 @@ protocolParamsToUtxoRpcPParams era pparams = obtainCommonConstraints era $ do
129130
.~ pparams ^. L.ppCommitteeMaxTermLengthL . to L.unEpochInterval . to fromIntegral
130131
& #governanceActionValidityPeriod
131132
.~ pparams ^. L.ppGovActionLifetimeL . to L.unEpochInterval . to fromIntegral
132-
& #governanceActionDeposit .~ pparams ^. L.ppGovActionDepositL . to fromIntegral
133-
& #drepDeposit .~ pparams ^. L.ppDRepDepositL . to fromIntegral
133+
& #governanceActionDeposit .~ pparams ^. L.ppGovActionDepositL . to inject
134+
& #drepDeposit .~ pparams ^. L.ppDRepDepositL . to inject
134135
& #drepInactivityPeriod .~ pparams ^. L.ppDRepActivityL . to L.unEpochInterval . to fromIntegral
135136

136137
utxoRpcPParamsToProtocolParams
@@ -140,8 +141,10 @@ utxoRpcPParamsToProtocolParams
140141
utxoRpcPParamsToProtocolParams era pp = conwayEraOnwardsConstraints (convert era) $ do
141142
def
142143
& appFuns
143-
[ pure
144-
. (L.ppCoinsPerUTxOByteL .~ pp ^. #coinsPerUtxoByte . to fromIntegral . to L.Coin . to L.CoinPerByte)
144+
[ \r -> do
145+
coinsPerUtxoByte <-
146+
pp ^. #coinsPerUtxoByte . to utxoRpcBigIntToInteger ?! "Invalid coinsPerUtxoByte"
147+
pure $ set L.ppCoinsPerUTxOByteL (L.CoinPerByte $ L.Coin coinsPerUtxoByte) r
145148
, pure . (L.ppMaxTxSizeL .~ pp ^. #maxTxSize . to fromIntegral)
146149
, pure . (L.ppMinFeeBL .~ pp ^. #minFeeCoefficient . to fromIntegral)
147150
, pure . (L.ppMinFeeAL .~ pp ^. #minFeeConstant . to fromIntegral)
@@ -399,15 +402,7 @@ scriptDataToUtxoRpcPlutusData :: ScriptData -> Proto UtxoRpc.PlutusData
399402
scriptDataToUtxoRpcPlutusData = \case
400403
ScriptDataBytes bs ->
401404
defMessage & #boundedBytes .~ bs
402-
ScriptDataNumber int
403-
| int <= fromIntegral (maxBound @Int64)
404-
&& int >= fromIntegral (minBound @Int64) ->
405-
defMessage & #bigInt . #int .~ fromIntegral int
406-
| int < 0 ->
407-
-- https://www.rfc-editor.org/rfc/rfc8949.html#name-bignums see 3.4.3 for negative integers
408-
defMessage & #bigInt . #bigNInt .~ serialiseToRawBytes (fromIntegral @_ @Natural (-1 - int))
409-
| otherwise ->
410-
defMessage & #bigInt . #bigUInt .~ serialiseToRawBytes (fromIntegral @_ @Natural int)
405+
ScriptDataNumber int -> defMessage & #bigInt .~ inject int
411406
ScriptDataList sds ->
412407
defMessage & #array . #items .~ map scriptDataToUtxoRpcPlutusData sds
413408
ScriptDataMap elements -> do
@@ -563,3 +558,18 @@ utxoRpcTxOutputToTxOut txOutput = do
563558
)
564559
datum
565560
referenceScript
561+
562+
utxoRpcBigIntToInteger
563+
:: forall m
564+
. HasCallStack
565+
=> MonadThrow m
566+
=> Proto UtxoRpc.BigInt
567+
-> m Integer
568+
utxoRpcBigIntToInteger bigInt
569+
| Just int <- bigInt ^. #maybe'int = fromIntegral int
570+
| Just bytes <- bigInt ^. #maybe'bigNInt = do
571+
n <- fmap fromIntegral . liftEitherError $ deserialiseFromRawBytes AsNatural bytes
572+
pure $ -n - 1
573+
| Just bytes <- bigInt ^. #maybe'bigUInt =
574+
map fromIntegral . liftEitherError $ deserialiseFromRawBytes AsNatural bytes
575+
| otherwise = pure 0 -- assume default value
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{-# LANGUAGE RankNTypes #-}
2+
{-# LANGUAGE ScopedTypeVariables #-}
3+
{-# LANGUAGE TypeApplications #-}
4+
5+
module Test.Cardano.Rpc.Type where
6+
7+
import Cardano.Api.Experimental.Era
8+
import Cardano.Rpc.Server.Internal.UtxoRpc.Type
9+
10+
import RIO
11+
12+
import Test.Gen.Cardano.Api.Typed
13+
( genTxOutUTxOContext
14+
)
15+
16+
import Hedgehog as H
17+
import Hedgehog.Extras qualified as H
18+
import Hedgehog.Gen as H
19+
import Hedgehog.Range as H
20+
21+
-- | Test that @BigInt@ protobuf type conversion to/from Integer roundtrips
22+
hprop_roundtrip_bigint :: Property
23+
hprop_roundtrip_bigint = do
24+
int <- genLargeInteger
25+
let bigInt = inject int
26+
27+
int' <- utxoRpcBigIntToInteger bigInt
28+
H.note_ "Check that Integer -> BigInt -> Integer preserves the value"
29+
int === int'
30+
31+
let bigInt' = inject int'
32+
H.note_ "Check that BigInt -> Integer -> BigInt preserves the value"
33+
bitInt === bigInt'
34+
35+
-- generate integer for each of the BigInt proto type constructors
36+
genLargeInteger :: Gen Integer
37+
genLargeInteger =
38+
H.choice
39+
[ Gen.integral $ H.linearFrom 0 (maxI64 + 1) (2 ^ 128) -- large positive - bigUInt
40+
, Gen.integral $ H.linearFrom 0 - (2 ^ 128) (minI64 - 1) -- large negative - bigNInt
41+
, Gen.integral $ H.linearFrom 0 minI64 maxI64 -- within Int64 size - int
42+
]
43+
where
44+
minI64 = fromIntegral $ minBound @Int64 :: Integer
45+
maxI64 = fromIntegral $ maxBound @Int64 :: Integer

0 commit comments

Comments
 (0)