Skip to content

Commit

Permalink
Use PlutusScriptSerialised consistently instead of fromPlutusScript (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ch1bo authored Jan 8, 2025
2 parents 6a0f3e3 + 527cc21 commit 2039d7c
Show file tree
Hide file tree
Showing 26 changed files with 64 additions and 86 deletions.
1 change: 0 additions & 1 deletion hydra-cardano-api/hydra-cardano-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ library
Hydra.Cardano.Api.Hash
Hydra.Cardano.Api.Network
Hydra.Cardano.Api.NetworkId
Hydra.Cardano.Api.PlutusScript
Hydra.Cardano.Api.PolicyId
Hydra.Cardano.Api.Prelude
Hydra.Cardano.Api.Pretty
Expand Down
1 change: 0 additions & 1 deletion hydra-cardano-api/src/Hydra/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ import Hydra.Cardano.Api.CtxTx as Extras
import Hydra.Cardano.Api.ExecutionUnits as Extras
import Hydra.Cardano.Api.Hash as Extras
import Hydra.Cardano.Api.NetworkId ()
import Hydra.Cardano.Api.PlutusScript as Extras
import Hydra.Cardano.Api.PolicyId as Extras
import Hydra.Cardano.Api.ReferenceScript as Extras
import Hydra.Cardano.Api.ScriptData as Extras
Expand Down
14 changes: 0 additions & 14 deletions hydra-cardano-api/src/Hydra/Cardano/Api/PlutusScript.hs

This file was deleted.

5 changes: 2 additions & 3 deletions hydra-cardano-api/src/Hydra/Cardano/Api/ReferenceScript.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module Hydra.Cardano.Api.ReferenceScript where

import Hydra.Cardano.Api.Prelude

import Hydra.Cardano.Api.PlutusScript (fromPlutusScript)
import PlutusLedgerApi.V3 qualified as Plutus

-- | Construct a 'ReferenceScript' from any given Plutus script.
Expand All @@ -13,12 +12,12 @@ mkScriptRef =
ReferenceScript babbageBasedEra
. toScriptInAnyLang
. PlutusScript PlutusScriptV3
. fromPlutusScript
. PlutusScriptSerialised

-- | Construct a PlutusV3 'ReferenceScript' from any given Plutus script.
mkScriptRefV3 :: Plutus.SerialisedScript -> ReferenceScript Era
mkScriptRefV3 =
ReferenceScript babbageBasedEra
. toScriptInAnyLang
. PlutusScript PlutusScriptV3
. fromPlutusScript
. PlutusScriptSerialised
2 changes: 0 additions & 2 deletions hydra-cardano-api/src/Hydra/Cardano/Api/ScriptHash.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ module Hydra.Cardano.Api.ScriptHash where

import Hydra.Cardano.Api.Prelude

import Hydra.Cardano.Api.PlutusScript ()

-- * Extras

-- | Like 'hashScript', but for a 'ScriptInAnyLang'.
Expand Down
27 changes: 13 additions & 14 deletions hydra-node/src/Hydra/Chain/Direct/State.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import Hydra.Cardano.Api (
NetworkId (Mainnet, Testnet),
NetworkMagic (NetworkMagic),
PaymentKey,
PlutusScriptV3,
PolicyId,
Quantity (..),
SerialiseAsRawBytes (serialiseToRawBytes),
Expand All @@ -35,7 +34,6 @@ import Hydra.Cardano.Api (
UTxO,
UTxO' (UTxO),
chainPointToSlotNo,
fromPlutusScript,
fromScriptData,
genTxIn,
getTxBody,
Expand All @@ -52,6 +50,7 @@ import Hydra.Cardano.Api (
txOutValue,
txSpendingUTxO,
pattern ByronAddressInEra,
pattern PlutusScriptSerialised,
pattern ShelleyAddressInEra,
pattern TxIn,
pattern TxOut,
Expand Down Expand Up @@ -407,11 +406,11 @@ abort ctx seedTxIn spendableUTxO committedUTxO = do
commits =
UTxO.toMap $ UTxO.filter (isScriptTxOut commitScript) utxoOfThisHead'

commitScript = fromPlutusScript @PlutusScriptV3 commitValidatorScript
commitScript = PlutusScriptSerialised commitValidatorScript

headScript = fromPlutusScript @PlutusScriptV3 Head.validatorScript
headScript = PlutusScriptSerialised Head.validatorScript

initialScript = fromPlutusScript @PlutusScriptV3 initialValidatorScript
initialScript = PlutusScriptSerialised initialValidatorScript

headTokenScript = mkHeadTokenScript seedTxIn

Expand Down Expand Up @@ -443,9 +442,9 @@ collect ctx headId headParameters utxoToCollect spendableUTxO = do
pure $
collectComTx networkId scriptRegistry ownVerificationKey headId headParameters headUTxO commits utxoToCollect
where
headScript = fromPlutusScript @PlutusScriptV3 Head.validatorScript
headScript = PlutusScriptSerialised Head.validatorScript

commitScript = fromPlutusScript @PlutusScriptV3 commitValidatorScript
commitScript = PlutusScriptSerialised commitValidatorScript

ChainContext{networkId, ownVerificationKey, scriptRegistry} = ctx

Expand Down Expand Up @@ -490,8 +489,8 @@ increment ctx spendableUTxO headId headParameters incrementingSnapshot depositTx
Left SnapshotIncrementUTxOIsNull
| otherwise -> Right $ incrementTx scriptRegistry ownVerificationKey headId headParameters headUTxO sn (UTxO.singleton (depositedIn, depositedOut)) upperValiditySlot sigs
where
headScript = fromPlutusScript @PlutusScriptV3 Head.validatorScript
depositScript = fromPlutusScript @PlutusScriptV3 depositValidatorScript
headScript = PlutusScriptSerialised Head.validatorScript
depositScript = PlutusScriptSerialised depositValidatorScript

Snapshot{utxoToCommit} = sn

Expand Down Expand Up @@ -530,7 +529,7 @@ decrement ctx spendableUTxO headId headParameters decrementingSnapshot = do
Left DecrementValueNegative
Right $ decrementTx scriptRegistry ownVerificationKey headId headParameters headUTxO sn sigs
where
headScript = fromPlutusScript @PlutusScriptV3 Head.validatorScript
headScript = PlutusScriptSerialised Head.validatorScript

decommitValue = foldMap txOutValue $ fromMaybe mempty $ utxoToDecommit sn

Expand Down Expand Up @@ -584,7 +583,7 @@ recover ctx headId depositedTxId spendableUTxO lowerValiditySlot = do
then Left InvalidHeadIdInRecover{headId}
else Right $ recoverTx depositedTxId deposited lowerValiditySlot
where
depositScript = fromPlutusScript @PlutusScriptV3 depositValidatorScript
depositScript = PlutusScriptSerialised depositValidatorScript
ChainContext{networkId} = ctx

-- | Construct a close transaction spending the head output in given 'UTxO',
Expand Down Expand Up @@ -627,7 +626,7 @@ close ctx spendableUTxO headId HeadParameters{parties, contestationPeriod} openV
where
Snapshot{utxoToCommit, utxoToDecommit} = getSnapshot confirmedSnapshot

headScript = fromPlutusScript @PlutusScriptV3 Head.validatorScript
headScript = PlutusScriptSerialised Head.validatorScript

ChainContext{ownVerificationKey, scriptRegistry} = ctx

Expand Down Expand Up @@ -699,7 +698,7 @@ contest ctx spendableUTxO headId contestationPeriod openVersion contestingSnapsh

ChainContext{ownVerificationKey, scriptRegistry} = ctx

headScript = fromPlutusScript @PlutusScriptV3 Head.validatorScript
headScript = PlutusScriptSerialised Head.validatorScript

data FanoutTxError
= CannotFindHeadOutputToFanout
Expand Down Expand Up @@ -738,7 +737,7 @@ fanout ctx spendableUTxO seedTxIn utxo utxoToCommit utxoToDecommit deadlineSlotN

ChainContext{scriptRegistry} = ctx

headScript = fromPlutusScript @PlutusScriptV3 Head.validatorScript
headScript = PlutusScriptSerialised Head.validatorScript

checkHeadDatum headUTxO@(_, headOutput) = do
headDatum <-
Expand Down
24 changes: 12 additions & 12 deletions hydra-node/src/Hydra/Chain/Direct/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ observeInitTx tx = do
guard $ isScriptTxOut headScript out
(ix,out,) <$> (fromScriptData =<< txOutScriptData out)

headScript = fromPlutusScript @PlutusScriptV3 Head.validatorScript
headScript = PlutusScriptSerialised Head.validatorScript

indexedOutputs = zip [0 ..] (txOuts' tx)

Expand All @@ -184,7 +184,7 @@ observeInitTx tx = do

isInitial = isScriptTxOut initialScript

initialScript = fromPlutusScript @PlutusScriptV3 initialValidatorScript
initialScript = PlutusScriptSerialised initialValidatorScript

mintedTokenNames pid =
[ assetName
Expand Down Expand Up @@ -262,11 +262,11 @@ observeCommitTx networkId utxo tx = do

initialAddress = mkScriptAddress @PlutusScriptV3 networkId initialScript

initialScript = fromPlutusScript @PlutusScriptV3 initialValidatorScript
initialScript = PlutusScriptSerialised initialValidatorScript

commitAddress = mkScriptAddress networkId commitScript

commitScript = fromPlutusScript @PlutusScriptV3 commitValidatorScript
commitScript = PlutusScriptSerialised commitValidatorScript

data CollectComObservation = CollectComObservation
{ threadOutput :: OpenThreadOutput
Expand Down Expand Up @@ -307,7 +307,7 @@ observeCollectComTx utxo tx = do
}
_ -> Nothing
where
headScript = fromPlutusScript Head.validatorScript
headScript = PlutusScriptSerialised Head.validatorScript
decodeUtxoHash datum =
case fromScriptData datum of
Just (Head.Open Head.OpenDatum{utxoHash}) -> Just $ fromBuiltin utxoHash
Expand Down Expand Up @@ -350,8 +350,8 @@ observeIncrementTx utxo tx = do
_ -> Nothing
_ -> Nothing
where
depositScript = fromPlutusScript depositValidatorScript
headScript = fromPlutusScript Head.validatorScript
depositScript = PlutusScriptSerialised depositValidatorScript
headScript = PlutusScriptSerialised Head.validatorScript

data DecrementObservation = DecrementObservation
{ headId :: HeadId
Expand Down Expand Up @@ -389,7 +389,7 @@ observeDecrementTx utxo tx = do
_ -> Nothing
_ -> Nothing
where
headScript = fromPlutusScript Head.validatorScript
headScript = PlutusScriptSerialised Head.validatorScript

data CloseObservation = CloseObservation
{ threadOutput :: ClosedThreadOutput
Expand Down Expand Up @@ -434,7 +434,7 @@ observeCloseTx utxo tx = do
}
_ -> Nothing
where
headScript = fromPlutusScript Head.validatorScript
headScript = PlutusScriptSerialised Head.validatorScript

data ContestObservation = ContestObservation
{ contestedThreadOutput :: (TxIn, TxOut CtxUTxO)
Expand Down Expand Up @@ -474,7 +474,7 @@ observeContestTx utxo tx = do
}
_ -> Nothing
where
headScript = fromPlutusScript Head.validatorScript
headScript = PlutusScriptSerialised Head.validatorScript

decodeDatum headDatum =
case fromScriptData headDatum of
Expand All @@ -500,7 +500,7 @@ observeFanoutTx utxo tx = do
Head.Fanout{} -> pure FanoutObservation{headId}
_ -> Nothing
where
headScript = fromPlutusScript Head.validatorScript
headScript = PlutusScriptSerialised Head.validatorScript

newtype AbortObservation = AbortObservation {headId :: HeadId} deriving stock (Eq, Show, Generic)

Expand All @@ -519,7 +519,7 @@ observeAbortTx utxo tx = do
Head.Abort -> pure $ AbortObservation headId
_ -> Nothing
where
headScript = fromPlutusScript Head.validatorScript
headScript = PlutusScriptSerialised Head.validatorScript

-- * Cardano specific identifiers

Expand Down
5 changes: 2 additions & 3 deletions hydra-node/test/Hydra/Chain/Direct/StateSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import Hydra.Cardano.Api (
TxOut,
UTxO,
findRedeemerSpending,
fromPlutusScript,
genTxIn,
hashScript,
isScriptTxOut,
Expand Down Expand Up @@ -427,11 +426,11 @@ genCommitTxMutation utxo tx =
fromMaybe (error "not found redeemer") $
findRedeemerSpending @Initial.RedeemerType tx initialTxIn

initialScript = fromPlutusScript @PlutusScriptV3 initialValidatorScript
initialScript = PlutusScriptSerialised initialValidatorScript

fakeScriptAddress = mkScriptAddress @PlutusScriptV3 testNetworkId fakeScript

fakeScript = fromPlutusScript dummyValidatorScript
fakeScript = PlutusScriptSerialised dummyValidatorScript

genAdaOnlyUTxOOnMainnetWithAmountBiggerThanOutLimit :: Gen UTxO
genAdaOnlyUTxOOnMainnetWithAmountBiggerThanOutLimit = do
Expand Down
2 changes: 1 addition & 1 deletion hydra-plutus/exe/inspect-script/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ main = do
serialised =
Aeson.encode $
serialiseToTextEnvelope (Just $ fromString itemName) $
fromPlutusScript @PlutusScriptV3 item
PlutusScriptSerialised item
BL.writeFile itemFile serialised
putTextLn $ " " <> pack itemFile <> ": " <> sizeInKb (serialise item)

Expand Down
10 changes: 5 additions & 5 deletions hydra-plutus/src/Hydra/Contract.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import Data.ByteString.Lazy qualified as BSL
import Hydra.Cardano.Api (
PlutusScriptVersion (PlutusScriptV3),
ScriptHash,
fromPlutusScript,
hashScript,
pattern PlutusScript,
pattern PlutusScriptSerialised,
)
import Hydra.Cardano.Api.Prelude qualified as Api
import Hydra.Contract.Head qualified as Head
Expand Down Expand Up @@ -44,18 +44,18 @@ scriptInfo =
ScriptInfo
{ mintingScriptHash = plutusScriptHash $ HeadTokens.mintingPolicyScript defaultOutRef
, mintingScriptSize = scriptSize $ HeadTokens.mintingPolicyScript defaultOutRef
, initialScriptHash = hashScript $ Api.PlutusScript PlutusScriptV3 $ fromPlutusScript initialValidatorScript
, initialScriptHash = hashScript $ Api.PlutusScript PlutusScriptV3 $ PlutusScriptSerialised initialValidatorScript
, initialScriptSize = scriptSize initialValidatorScript
, commitScriptHash = hashScript $ Api.PlutusScript PlutusScriptV3 $ fromPlutusScript commitValidatorScript
, commitScriptHash = hashScript $ Api.PlutusScript PlutusScriptV3 $ PlutusScriptSerialised commitValidatorScript
, commitScriptSize = scriptSize commitValidatorScript
, headScriptHash = plutusScriptHash Head.validatorScript
, headScriptSize = scriptSize Head.validatorScript
, depositScriptHash = hashScript $ Api.PlutusScript PlutusScriptV3 $ fromPlutusScript depositValidatorScript
, depositScriptHash = hashScript $ Api.PlutusScript PlutusScriptV3 $ PlutusScriptSerialised depositValidatorScript
, depositScriptSize = scriptSize depositValidatorScript
}
where
plutusScriptHash =
hashScript . PlutusScript . fromPlutusScript
hashScript . PlutusScript . PlutusScriptSerialised

scriptSize = BSL.length . serialise

Expand Down
5 changes: 2 additions & 3 deletions hydra-plutus/src/Hydra/Contract/HeadTokens.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ module Hydra.Contract.HeadTokens where
import PlutusTx.Prelude

import Hydra.Cardano.Api (
PlutusScriptV3,
PolicyId,
TxIn,
fromPlutusScript,
scriptPolicyId,
toPlutusTxOutRef,
pattern PlutusScript,
pattern PlutusScriptSerialised,
)
import Hydra.Cardano.Api qualified as Api

Expand Down Expand Up @@ -206,4 +205,4 @@ headPolicyId =
-- | Get the applied head minting policy script given a seed 'TxIn'.
mkHeadTokenScript :: TxIn -> Api.PlutusScript
mkHeadTokenScript =
fromPlutusScript @PlutusScriptV3 . mintingPolicyScript . toPlutusTxOutRef
PlutusScriptSerialised . mintingPolicyScript . toPlutusTxOutRef
4 changes: 2 additions & 2 deletions hydra-plutus/test/Hydra/Plutus/GoldenSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import Hydra.Cardano.Api (
AsType (AsPlutusScriptV3, AsScript),
File (..),
Script,
fromPlutusScript,
hashScript,
readFileTextEnvelope,
writeFileTextEnvelope,
pattern PlutusScript,
pattern PlutusScriptSerialised,
)
import Hydra.Contract.Head qualified as Head
import Hydra.Contract.HeadTokens qualified as HeadTokens
Expand Down Expand Up @@ -49,7 +49,7 @@ spec = do
goldenScript :: String -> Plutus.SerialisedScript -> Golden Script
goldenScript name plutusScript =
Golden
{ output = PlutusScript $ fromPlutusScript plutusScript
{ output = PlutusScript $ PlutusScriptSerialised plutusScript
, encodePretty = show . hashScript
, writeToFile
, readFromFile
Expand Down
Loading

0 comments on commit 2039d7c

Please sign in to comment.