Skip to content

Commit

Permalink
Merge pull request #876 from Plutonomicon/klntsky/874-costmodels-int-fix
Browse files Browse the repository at this point in the history
Fix `CostModel` representation
  • Loading branch information
ngua committed Aug 12, 2022
2 parents 59ae294 + 96fcc18 commit 37dd169
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 185 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ See https://github.com/cardano-foundation/CIPs/issues/303 for motivation
- `TypedValidator` interface ([#808](https://github.com/Plutonomicon/cardano-transaction-lib/issues/808))
- `Contract.Address.getWalletCollateral` now works with `KeyWallet`.
- Removed unwanted error messages in case `WebSocket` listeners get cancelled ([#827](https://github.com/Plutonomicon/cardano-transaction-lib/issues/827))
- Bug in `CostModel` serialization - incorrect `Int` type ([#874](https://github.com/Plutonomicon/cardano-transaction-lib/issues/874))

## [2.0.0-alpha] - 2022-07-05

Expand Down
2 changes: 1 addition & 1 deletion src/Cardano/Types/Transaction.purs
Original file line number Diff line number Diff line change
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
5 changes: 2 additions & 3 deletions src/Deserialization/Transaction.purs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ import Data.BigInt (BigInt)
import Data.BigInt as BigInt
import Data.Bitraversable (bitraverse)
import Data.Either (Either)
import Data.Int (fromString)
import Data.Map as M
import Data.Maybe (Maybe)
import Data.Newtype (wrap, unwrap)
Expand Down Expand Up @@ -542,9 +541,9 @@ convertCostModel err = map T.CostModel <<< traverse stringToInt <<<
_unpackCostModel
where
stringToInt
:: String -> Either (Variant (fromCslRepError :: String | r)) Int
:: String -> Either (Variant (fromCslRepError :: String | r)) Int.Int
stringToInt s = cslErr (err <> ": string (" <> s <> ") -> int") $
fromString s
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ import Serialization.Types
, ExUnits
, GenesisDelegateHash
, GenesisHash
, Int32
, Ipv4
, Ipv6
, Language
Expand Down Expand Up @@ -144,9 +143,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 @@ -233,9 +233,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 @@ -772,7 +772,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
Original file line number Diff line number Diff line change
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
5 changes: 5 additions & 0 deletions src/Types/BigNum.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ exports.bnMul = maybe => lhs => rhs => {
};

exports._fromString = maybe => str => {
// this is needed because try/catch overuse breaks runtime badly
// https://github.com/Plutonomicon/cardano-transaction-lib/issues/875
if (str[0] == "-") {
return maybe.nothing;
}
try {
return maybe.just(lib.BigNum.from_str(str));
} catch (_) {
Expand Down
27 changes: 24 additions & 3 deletions src/Types/Int.purs
Original file line number Diff line number Diff line change
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 @@ -35,7 +44,11 @@ instance Show Int where
show = _intToStr

instance EncodeAeson Int where
encodeAeson' = encodeAeson' <<< _intToStr
encodeAeson' = encodeAeson' <<< toBigInt

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

fromBigInt :: BigInt.BigInt -> Maybe Int
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
Original file line number Diff line number Diff line change
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 37dd169

Please sign in to comment.