Skip to content

Commit

Permalink
Use Types.Int instead of Data.Int in CostModel
Browse files Browse the repository at this point in the history
  • Loading branch information
klntsky committed Aug 10, 2022
1 parent 6cdbad6 commit 767677d
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 184 deletions.
2 changes: 1 addition & 1 deletion src/Cardano/Types/Transaction.purs
Expand Up @@ -402,7 +402,7 @@ instance EncodeAeson Language where
encodeAeson' = case _ of
PlutusV1 -> encodeAeson' $ encodeTagged' "PlutusV1" {}

newtype CostModel = CostModel (Array Int)
newtype CostModel = CostModel (Array Int.Int)

derive instance Newtype CostModel _
derive newtype instance Eq CostModel
Expand Down
6 changes: 3 additions & 3 deletions src/Deserialization/Transaction.purs
Expand Up @@ -542,9 +542,9 @@ convertCostModel err = map T.CostModel <<< traverse stringToInt <<<
_unpackCostModel
where
stringToInt
:: String -> Either (Variant (fromCslRepError :: String | r)) Int
stringToInt s = cslErr (err <> ": string (" <> s <> ") -> int") $
fromString s
:: String -> Either (Variant (fromCslRepError :: String | r)) Int.Int
stringToInt s = cslErr ("string (" <> s <> ") -> int") $
Int.fromBigInt =<< BigInt.fromString s

convertAuxiliaryData
:: forall (r :: Row Type). Csl.AuxiliaryData -> Err r T.AuxiliaryData
Expand Down
333 changes: 167 additions & 166 deletions src/QueryM/Ogmios.purs

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions src/Serialization.js
Expand Up @@ -129,8 +129,6 @@ exports.costModelSetCost = cm => op => cost => () => cm.set(op, cost);

exports.newPlutusV1 = () => lib.Language.new_plutus_v1();

exports.newInt32 = x => () => lib.Int.new_i32(x);

exports._hashScriptData = rs => cms => ds => () => {
const list = lib.PlutusList.new();
ds.forEach(d => list.add(d));
Expand Down
10 changes: 5 additions & 5 deletions src/Serialization.purs
Expand Up @@ -92,7 +92,6 @@ import Serialization.Types
, ExUnits
, GenesisDelegateHash
, GenesisHash
, Int32
, Ipv4
, Ipv6
, Language
Expand Down Expand Up @@ -142,9 +141,10 @@ import Types.BigNum (BigNum)
import Types.BigNum (fromBigInt, fromStringUnsafe, toString) as BigNum
import Types.ByteArray (ByteArray)
import Types.CborBytes (CborBytes)
import Types.RawBytes (RawBytes)
import Types.Int as Csl
import Types.Int as Int
import Types.PlutusData as PlutusData
import Types.RawBytes (RawBytes)
import Types.TokenName (getTokenName) as TokenName
import Types.Transaction (TransactionInput(TransactionInput)) as T
import Untagged.Union (type (|+|), UndefinedOr, maybeToUor)
Expand Down Expand Up @@ -231,9 +231,9 @@ foreign import costmdlsSetCostModel
:: Costmdls -> Language -> CostModel -> Effect Unit

foreign import newCostModel :: Effect CostModel
foreign import costModelSetCost :: CostModel -> Int -> Int32 -> Effect Unit
foreign import costModelSetCost :: CostModel -> Int -> Csl.Int -> Effect Unit
foreign import newPlutusV1 :: Effect Language
foreign import newInt32 :: Int -> Effect Int32

foreign import _hashScriptData
:: Redeemers -> Costmdls -> Array PlutusData -> Effect ScriptDataHash

Expand Down Expand Up @@ -770,7 +770,7 @@ convertCostmdls (T.Costmdls cs) = do
$ Map.lookup T.PlutusV1 cs
costModel <- newCostModel
forWithIndex_ costs $ \operation cost ->
costModelSetCost costModel operation =<< newInt32 cost
costModelSetCost costModel operation cost
costmdls <- newCostmdls
plutusV1 <- newPlutusV1
costmdlsSetCostModel costmdls plutusV1 costModel
Expand Down
2 changes: 0 additions & 2 deletions src/Serialization/Types.purs
Expand Up @@ -21,7 +21,6 @@ module Serialization.Types
, GenesisDelegateHash
, GenesisHash
, GenesisKeyDelegation
, Int32
, Ipv4
, Ipv6
, Language
Expand Down Expand Up @@ -111,7 +110,6 @@ foreign import data GeneralTransactionMetadata :: Type
foreign import data GenesisDelegateHash :: Type
foreign import data GenesisHash :: Type
foreign import data GenesisKeyDelegation :: Type
foreign import data Int32 :: Type
foreign import data Ipv4 :: Type
foreign import data Ipv6 :: Type
foreign import data Language :: Type
Expand Down
25 changes: 23 additions & 2 deletions src/Types/Int.purs
Expand Up @@ -5,19 +5,28 @@ module Types.Int
, newNegative
, fromBigInt
, toBigInt
, fromInt
, toInt
) where

import Prelude

import Aeson (class EncodeAeson, encodeAeson')
import Aeson
( class DecodeAeson
, class EncodeAeson
, JsonDecodeError(TypeMismatch)
, decodeAeson
, encodeAeson'
)
import Control.Alternative ((<|>))
import Data.BigInt as BigInt
import Data.Either (note)
import Data.Function (on)
import Data.Maybe (Maybe, fromJust)
import Partial.Unsafe (unsafePartial)
import Prim as Prim
import Types.BigNum (BigNum)
import Types.BigNum (fromBigInt) as BigNum
import Types.BigNum (fromInt, fromBigInt) as BigNum

foreign import data Int :: Prim.Type

Expand All @@ -37,6 +46,10 @@ instance Show Int where
instance EncodeAeson Int where
encodeAeson' = encodeAeson' <<< _intToStr

instance DecodeAeson Int where
decodeAeson aeson =
decodeAeson aeson >>= note (TypeMismatch "Int") <<< fromBigInt

fromBigInt :: BigInt.BigInt -> Maybe Int
fromBigInt bi =
(newPositive <$> BigNum.fromBigInt bi) <|>
Expand All @@ -46,3 +59,11 @@ toBigInt :: Int -> BigInt.BigInt
toBigInt int =
-- Assuming every Int can be represented as BigInt
unsafePartial $ fromJust $ BigInt.fromString $ _intToStr int

fromInt :: Prim.Int -> Int
fromInt n
| n < 0 = newNegative $ BigNum.fromInt n
| otherwise = newPositive $ BigNum.fromInt n

toInt :: Int -> Maybe Prim.Int
toInt = toBigInt >>> BigInt.toInt
7 changes: 4 additions & 3 deletions test/Fixtures/CostModels.purs
Expand Up @@ -4,18 +4,19 @@ module Test.Fixtures.CostModels

import Prelude

import Data.Map as Map
import Data.Tuple.Nested ((/\))
import Cardano.Types.Transaction
( CostModel(CostModel)
, Costmdls(Costmdls)
, Language(PlutusV1)
)
import Data.Map as Map
import Data.Tuple.Nested ((/\))
import Types.Int as Int

costModelsFixture1 :: Costmdls
costModelsFixture1 = Costmdls $ Map.fromFoldable
[ PlutusV1 /\
( CostModel
( CostModel $ map Int.fromInt
[ 197209
, 0
, 1
Expand Down

0 comments on commit 767677d

Please sign in to comment.