Skip to content

Commit

Permalink
Remove dependency on cardano-prelude (IntersectMBO#2992)
Browse files Browse the repository at this point in the history
Update `plutus` and `cardano-base` dependencies. Remove usage of `cardano-prelude`:

* Update nix shell to prebuild packages and make them available to ghc-pkg so that
  running cabal from within the shell doesn't have to (which sometimes fails).

* Update plutus version and adjust to API changes

* Update cardano-base to a version compatible with plutus and without cardano-prelude

* Allow newer version of `hashable`, since plutus brings in version 1.4.1.0,
  but our dependency Unique is restricting it to 1.4.

* Regenerate `PlutusScript` using `plutus-preprocessor`

* Fix Plutus protocol version in test, which was wrong, but only started 
  failing with the new plutus version

* Replace `panic` function from `cardano-prelude` with `error`

* Use HeapWords from cardano-base instead of cardano-prelude

* Remove dependency on `cardano-prelude` from everywhere except byron

Co-authored-by: Alexey Kuleshevich <[email protected]>
  • Loading branch information
teodanciu and lehins authored Sep 17, 2022
1 parent 3f5123c commit 4d85002
Show file tree
Hide file tree
Showing 44 changed files with 248 additions and 238 deletions.
12 changes: 7 additions & 5 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ test-show-details: streaming
source-repository-package
type: git
location: https://github.com/input-output-hk/cardano-base
tag: 631cb6cf1fa01ab346233b610a38b3b4cba6e6ab
--sha256: 0944wg2nqazmhlmsynwgdwxxj6ay0hb9qig9l128isb2cjia0hlp
tag: 7e3ddba98a61900181fe63cdd4c9ed9708d1a6a7
--sha256: 1ci0aa8zkcld0d4i6xldvffhppf8bg0ys8y2jys912r7i5sxq5pa
subdir:
base-deriving-via
binary
binary/test
cardano-crypto-class
cardano-crypto-praos
heapwords
measures
slotting
strict-containers
Expand Down Expand Up @@ -83,8 +84,8 @@ source-repository-package
source-repository-package
type: git
location: https://github.com/input-output-hk/plutus
tag: 8ab4c3355c5fdf67dcf6acc1f5a14668d5e6f0a9
--sha256: coD/Kpl7tutwXb6ukQCH5XojBjquYkW7ob0BWZtdpok=
tag: ecbfc46d3302f438a31c4f2f8f30cd1a87dab271
--sha256: 1r16a9xk49bw3r9wihmf49p2f8pfqqymrpgf85lzccyjrxdvxd7r
subdir:
plutus-ledger-api
plutus-tx
Expand All @@ -110,6 +111,7 @@ source-repository-package
allow-newer:
monoidal-containers:aeson,
size-based:template-haskell,
Unique:hashable

constraints:
-- bizarre issue: in earlier versions they define their own 'GEq', in newer
Expand All @@ -118,7 +120,7 @@ constraints:
, dependent-sum > 0.6.2.0

-- Plutus dependency
, algebraic-graphs < 0.7
, algebraic-graphs >= 0.7

, protolude >= 0.3.2

Expand Down
2 changes: 1 addition & 1 deletion eras/alonzo/impl/cardano-ledger-alonzo.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ library
cardano-crypto-class,
cardano-ledger-core,
cardano-ledger-shelley-ma,
cardano-prelude,
cardano-slotting,
containers,
data-default,
deepseq,
heapwords,
measures,
mtl,
microlens,
Expand Down
4 changes: 2 additions & 2 deletions eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Data.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import Cardano.Binary
withSlice,
)
import Cardano.Crypto.Hash.Class (HashAlgorithm)
import Cardano.HeapWords (HeapWords (..), heapWords0, heapWords1)
import Cardano.Ledger.Alonzo.Era
import Cardano.Ledger.Alonzo.Language (Language (..))
import Cardano.Ledger.Alonzo.Scripts (AlonzoScript (..), validScript)
Expand All @@ -72,7 +73,6 @@ import Cardano.Ledger.SafeHash
)
import Cardano.Ledger.Serialization (mapFromCBOR)
import Cardano.Ledger.Shelley.Metadata (Metadatum, validMetadatum)
import Cardano.Prelude (HeapWords (..), heapWords0, heapWords1)
import qualified Codec.Serialise as Cborg (Serialise (..))
import Control.DeepSeq (NFData)
import Data.ByteString.Lazy (fromStrict)
Expand All @@ -87,7 +87,7 @@ import Data.Typeable (Typeable)
import Data.Word (Word64)
import GHC.Generics (Generic)
import NoThunks.Class (InspectHeapNamed (..), NoThunks)
import qualified Plutus.V1.Ledger.Api as Plutus
import qualified PlutusLedgerApi.V1 as Plutus

-- =====================================================================
-- Plutus.Data is the type that Plutus expects as data.
Expand Down
20 changes: 9 additions & 11 deletions eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Genesis.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import Cardano.Ledger.Alonzo.Scripts
ExUnits (..),
ExUnits',
Prices (..),
costModelParamsNames,
getCostModelParams,
mkCostModel,
)
Expand All @@ -42,15 +43,12 @@ import Data.Aeson.Types (FromJSONKey (..), ToJSONKey (..), toJSONKeyText)
import Data.Coders
import Data.Map (Map)
import qualified Data.Map.Strict as Map
import Data.Maybe (mapMaybe)
import Data.Scientific (fromRationalRepetendLimited)
import qualified Data.Set as Set
import Data.Text (Text)
import Data.Word (Word64)
import GHC.Generics (Generic)
import NoThunks.Class (NoThunks)
import Numeric.Natural (Natural)
import Plutus.V1.Ledger.Api as PV1 (costModelParamNames)

data AlonzoGenesis = AlonzoGenesis
{ coinsPerUTxOWord :: !Coin,
Expand Down Expand Up @@ -213,8 +211,8 @@ instance ToJSONKey Language where
instance FromJSONKey Language where
fromJSONKey = Aeson.FromJSONKeyTextParser languageFromText

validateCostModel :: MonadFail m => (Language, Map Text Integer) -> m (Language, CostModel)
validateCostModel (lang, cmps) = case mkCostModel lang cmps of
validateCostModel :: MonadFail m => Language -> Map Text Integer -> m (Language, CostModel)
validateCostModel lang cmps = case mkCostModel lang cmps of
Left err -> fail $ show err
Right cm -> pure (lang, cm)

Expand All @@ -223,19 +221,19 @@ validateCostModel (lang, cmps) = case mkCostModel lang cmps of
-- Therefore we just replace (in order) the new keys for the old ones.
translateLegacyV1paramNames :: Map Text Integer -> Map Text Integer
translateLegacyV1paramNames cmps =
Map.fromList $
zipWith (\(_, v) k2 -> (k2, v)) (Map.toList cmps) (Set.toList PV1.costModelParamNames)
Map.fromList $ zip (costModelParamsNames PlutusV1) (Map.elems cmps)

instance FromJSON CostModels where
parseJSON = Aeson.withObject "CostModels" $ \o -> do
plutusV1 <- o .:? "PlutusV1"
plutusV2 <- o .:? "PlutusV2"
let plutusV1' = translateLegacyV1paramNames <$> plutusV1
cms <- mapM validateCostModel $ mapMaybe f [(PlutusV1, plutusV1'), (PlutusV2, plutusV2)]
cms <-
sequence
[ validateCostModel lang cm
| (lang, Just cm) <- [(PlutusV1, plutusV1'), (PlutusV2, plutusV2)]
]
pure . CostModels . Map.fromList $ cms
where
f (_, Nothing) = Nothing
f (a, Just b) = Just (a, b)

instance FromJSON AlonzoGenesis where
parseJSON = Aeson.withObject "Alonzo Genesis" $ \o -> do
Expand Down
74 changes: 53 additions & 21 deletions eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Scripts.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,20 @@ module Cardano.Ledger.Alonzo.Scripts
-- * Cost Model
CostModel,
mkCostModel,
costModelParamsNames,
costModelParamsNamesSet,
getCostModelLanguage,
getCostModelParams,
getEvaluationContext,
ExUnits (ExUnits, exUnitsMem, exUnitsSteps, ..),
ExUnits',
Prices (..),
hashCostModel,
assertWellFormedCostModelParams,
PV1.assertWellFormedCostModelParams,
decodeCostModelMap,
decodeCostModel,
CostModels (..),
CostModelApplyError (..),
PV1.CostModelApplyError (..),
contentsEq,
)
where
Expand All @@ -65,6 +67,7 @@ import Cardano.Ledger.ShelleyMA.Timelocks (Timelock)
import qualified Cardano.Ledger.ShelleyMA.Timelocks as Timelocks
import Control.DeepSeq (NFData (..), deepseq, rwhnf)
import Control.Monad (when)
import Control.Monad.Trans.Writer (WriterT (runWriterT))
import Data.ByteString.Short (ShortByteString, fromShort)
import Data.Coders
( Annotator,
Expand All @@ -83,20 +86,34 @@ import Data.Coders
(<*!),
)
import Data.DerivingVia (InstantiatedAt (..))
import Data.Either (isRight)
import Data.Int (Int64)
import Data.Map (Map)
import qualified Data.Map.Strict as Map
import Data.Measure (BoundedMeasure, Measure)
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Text (Text)
import qualified Data.Text as Text
import Data.Typeable (Proxy (..), Typeable)
import Data.Word (Word64, Word8)
import GHC.Generics (Generic)
import NoThunks.Class (InspectHeapNamed (..), NoThunks)
import Numeric.Natural (Natural)
import Plutus.V1.Ledger.Api as PV1 hiding (Map, Script)
import Plutus.V2.Ledger.Api as PV2 (costModelParamNames, isScriptWellFormed, mkEvaluationContext)
import PlutusCore.Evaluation.Machine.CostModelInterface (CostModelApplyWarn)
import PlutusLedgerApi.Common (showParamName)
import qualified PlutusLedgerApi.V1 as PV1
( CostModelApplyError (..),
EvaluationContext,
ParamName,
ProtocolVersion (ProtocolVersion),
ScriptDecodeError,
assertScriptWellFormed,
assertWellFormedCostModelParams,
mkEvaluationContext,
)
import qualified PlutusLedgerApi.V2 as PV2 (ParamName, assertScriptWellFormed, mkEvaluationContext)
import PlutusPrelude (enumerate)

-- | Marker indicating the part of a transaction for which this script is acting
-- as a validator.
Expand Down Expand Up @@ -224,7 +241,7 @@ pointWiseExUnits oper (ExUnits m1 s1) (ExUnits m2 s2) = (m1 `oper` m2) && (s1 `o
-- =====================================

-- | A language dependent cost model for the Plutus evaluator.
-- Note that the `EvaluationContext` is entirely dependent on the
-- Note that the `PV1.EvaluationContext` is entirely dependent on the
-- cost model parameters (ie the `Map` `Text` `Integer`) and that
-- this type uses the smart constructor `mkCostModel`
-- to hide the evaluation context.
Expand Down Expand Up @@ -270,15 +287,18 @@ instance NFData CostModel where

-- | Convert cost model parameters to a cost model, making use of the
-- conversion function mkEvaluationContext from the Plutus API.
mkCostModel :: Language -> Map Text Integer -> Either CostModelApplyError CostModel
mkCostModel PlutusV1 cm =
case PV1.mkEvaluationContext cm of
Right evalCtx -> Right (CostModel PlutusV1 cm evalCtx)
Left e -> Left e
mkCostModel PlutusV2 cm =
case PV2.mkEvaluationContext cm of
Right evalCtx -> Right (CostModel PlutusV2 cm evalCtx)
mkCostModel :: Language -> Map Text Integer -> Either PV1.CostModelApplyError CostModel
mkCostModel lang cm =
case eCostModel of
Right (evalCtx, _) -> Right (CostModel lang cm evalCtx)
Left e -> Left e
where
mkEvaluationContext =
case lang of
PlutusV1 -> PV1.mkEvaluationContext
PlutusV2 -> PV2.mkEvaluationContext
eCostModel :: Either PV1.CostModelApplyError (PV1.EvaluationContext, [CostModelApplyWarn])
eCostModel = runWriterT (mkEvaluationContext (Map.elems cm))

getCostModelLanguage :: CostModel -> Language
getCostModelLanguage (CostModel lang _ _) = lang
Expand All @@ -291,14 +311,19 @@ decodeCostModelMap = decodeMapByKey fromCBOR decodeCostModel

decodeCostModel :: Language -> Decoder s CostModel
decodeCostModel lang = do
let keys = costModelParamsNamesSet lang
checked <- mkCostModel lang <$> decodeArrayAsMap keys fromCBOR
case checked of
Left e -> fail $ show e
Right cm -> pure cm
where
keys = case lang of
PlutusV1 -> PV1.costModelParamNames
PlutusV2 -> PV2.costModelParamNames

costModelParamsNames :: Language -> [Text]
costModelParamsNames = \case
PlutusV1 -> Text.pack . showParamName <$> enumerate @PV1.ParamName
PlutusV2 -> Text.pack . showParamName <$> enumerate @PV2.ParamName

costModelParamsNamesSet :: Language -> Set.Set Text
costModelParamsNamesSet = Set.fromList . costModelParamsNames

decodeArrayAsMap :: Ord a => Set a -> Decoder s b -> Decoder s (Map a b)
decodeArrayAsMap keys decodeValue = do
Expand Down Expand Up @@ -426,10 +451,17 @@ instance Era era => FromCBOR (Annotator (Script era)) where
-- Run deepseq to see that there are no infinite computations and that
-- every Plutus Script unflattens into a real PV1.Script
validScript :: ProtVer -> Script era -> Bool
validScript pv scrip = case scrip of
TimelockScript sc -> deepseq sc True
PlutusScript PlutusV1 bytes -> PV1.isScriptWellFormed (transProtocolVersion pv) bytes
PlutusScript PlutusV2 bytes -> PV2.isScriptWellFormed (transProtocolVersion pv) bytes
validScript pv script =
case script of
TimelockScript sc -> deepseq sc True
PlutusScript lang bytes ->
let assertScriptWellFormed =
case lang of
PlutusV1 -> PV1.assertScriptWellFormed
PlutusV2 -> PV2.assertScriptWellFormed
eWellFormed :: Either PV1.ScriptDecodeError ()
eWellFormed = assertScriptWellFormed (transProtocolVersion pv) bytes
in isRight eWellFormed

transProtocolVersion :: ProtVer -> PV1.ProtocolVersion
transProtocolVersion (ProtVer major minor) =
Expand Down
4 changes: 2 additions & 2 deletions eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Tools.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ import qualified Data.Set as Set
import Data.Text (Text)
import GHC.Records (HasField (..))
import Lens.Micro
import qualified Plutus.V1.Ledger.Api as PV1
import qualified Plutus.V2.Ledger.Api as PV2
import qualified PlutusLedgerApi.V1 as PV1
import qualified PlutusLedgerApi.V2 as PV2

-- | Script failures that can be returned by 'evaluateTransactionExecutionUnits'.
data TransactionScriptFailure c
Expand Down
17 changes: 6 additions & 11 deletions eras/alonzo/impl/src/Cardano/Ledger/Alonzo/TxInfo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ module Cardano.Ledger.Alonzo.TxInfo
transDataHash,
transDataHash',
transKeyHash,
transScriptHash,
transSafeHash,
transHash,
txInfoId,
Expand Down Expand Up @@ -147,9 +146,9 @@ import GHC.Records (HasField (..))
import Lens.Micro
import NoThunks.Class (NoThunks)
import Numeric.Natural (Natural)
import qualified Plutus.V1.Ledger.Api as PV1
import Plutus.V1.Ledger.Contexts ()
import qualified Plutus.V2.Ledger.Api as PV2
import qualified PlutusLedgerApi.V1 as PV1
import PlutusLedgerApi.V1.Contexts ()
import qualified PlutusLedgerApi.V2 as PV2
import Prettyprinter (Pretty (..))

-- =========================================================
Expand Down Expand Up @@ -227,9 +226,6 @@ transDataHash' safe = PV1.DatumHash (transSafeHash safe)
transKeyHash :: KeyHash d c -> PV1.PubKeyHash
transKeyHash (KeyHash h) = PV1.PubKeyHash (PV1.toBuiltin (hashToBytes h))

transScriptHash :: ScriptHash c -> PV1.ValidatorHash
transScriptHash (ScriptHash h) = PV1.ValidatorHash (PV1.toBuiltin (hashToBytes h))

transSafeHash :: SafeHash c i -> PV1.BuiltinByteString
transSafeHash = PV1.toBuiltin . hashToBytes . extractHash

Expand All @@ -240,8 +236,8 @@ txInfoId :: TxId c -> PV1.TxId
txInfoId (TxId safe) = PV1.TxId (transSafeHash safe)

transStakeCred :: Credential kr c -> PV1.Credential
transStakeCred (ScriptHashObj (ScriptHash kh)) =
PV1.ScriptCredential (PV1.ValidatorHash (PV1.toBuiltin (hashToBytes kh)))
transStakeCred (ScriptHashObj (ScriptHash sh)) =
PV1.ScriptCredential (PV1.ScriptHash (PV1.toBuiltin (hashToBytes sh)))
transStakeCred (KeyHashObj (KeyHash kh)) =
PV1.PubKeyCredential (PV1.PubKeyHash (PV1.toBuiltin (hashToBytes kh)))

Expand All @@ -256,8 +252,7 @@ transStakeReference StakeRefNull = Nothing
transCred :: Credential kr c -> PV1.Credential
transCred (KeyHashObj (KeyHash kh)) =
PV1.PubKeyCredential (PV1.PubKeyHash (PV1.toBuiltin (hashToBytes kh)))
transCred (ScriptHashObj (ScriptHash kh)) =
PV1.ScriptCredential (PV1.ValidatorHash (PV1.toBuiltin (hashToBytes kh)))
transCred (ScriptHashObj (ScriptHash sh)) = PV1.ScriptCredential (PV1.ScriptHash (PV1.toBuiltin (hashToBytes sh)))

transAddr :: Addr c -> Maybe PV1.Address
transAddr (Addr _net object stake) = Just (PV1.Address (transCred object) (transStakeReference stake))
Expand Down
7 changes: 4 additions & 3 deletions eras/alonzo/test-suite/cardano-ledger-alonzo-test.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ library
cborg,
containers,
data-default-class,
hashable,
hashable >= 1.4.1.0,
plutus-core,
plutus-tx,
plutus-ledger-api,
plutus-ledger-api:{plutus-ledger-api, plutus-ledger-api-testlib} ^>=1.0,
QuickCheck,
cardano-ledger-shelley-test,
cardano-ledger-shelley,
Expand Down Expand Up @@ -111,7 +112,7 @@ test-suite cardano-ledger-alonzo-test
containers,
data-default-class,
plutus-core,
plutus-ledger-api,
plutus-ledger-api:{plutus-ledger-api, plutus-ledger-api-testlib} ^>=1.0,
QuickCheck,
small-steps,
small-steps-test,
Expand Down
Loading

0 comments on commit 4d85002

Please sign in to comment.