Skip to content

Commit

Permalink
Alonzo UTXOW examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Jared Corduan committed Apr 19, 2021
1 parent 5aa639f commit d227aa9
Show file tree
Hide file tree
Showing 12 changed files with 823 additions and 28 deletions.
10 changes: 9 additions & 1 deletion alonzo/impl/cardano-ledger-alonzo.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ library
shelley-spec-ledger,
small-steps,
strict-containers,
text,
transformers
hs-source-dirs:
src
Expand All @@ -91,7 +92,8 @@ library test
plutus-tx,
QuickCheck,
shelley-spec-ledger-test,
shelley-spec-ledger
shelley-spec-ledger,
text,
hs-source-dirs:
test/lib

Expand All @@ -105,6 +107,7 @@ test-suite cardano-ledger-alonzo-test
other-modules:
Test.Cardano.Ledger.Alonzo.Golden
Test.Cardano.Ledger.Alonzo.Serialisation.Tripping
Test.Cardano.Ledger.Alonzo.Examples.Utxow
Test.Cardano.Ledger.Alonzo.Serialisation.CDDL
build-depends:
base16-bytestring,
Expand All @@ -115,8 +118,13 @@ test-suite cardano-ledger-alonzo-test
cardano-ledger-core,
cardano-ledger-shelley-ma-test,
containers,
data-default-class,
plutus-core,
plutus-tx,
plutus-ledger-api,
QuickCheck,
small-steps,
small-steps-test,
shelley-spec-ledger,
shelley-spec-ledger-test,
strict-containers,
Expand Down
2 changes: 1 addition & 1 deletion alonzo/impl/cddl-files/alonzo.cddl
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ language = 0 ; Plutus v1

costmdls = { * language => cost_model } ; New

cost_model = { * bytes => integer } ; New
cost_model = { * text => integer } ; New

transaction_metadatum =
{ * transaction_metadatum => transaction_metadatum }
Expand Down
2 changes: 1 addition & 1 deletion alonzo/impl/src/Cardano/Ledger/Alonzo/Data.hs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ import qualified Data.Set as Set
import Data.Typeable (Typeable)
import Data.Word (Word64)
import GHC.Generics (Generic)
import qualified Language.PlutusTx as Plutus
import NoThunks.Class (InspectHeapNamed (..), NoThunks)
import qualified PlutusTx as Plutus
import Shelley.Spec.Ledger.BaseTypes (StrictMaybe (..))
import Shelley.Spec.Ledger.Metadata (Metadatum)
import Shelley.Spec.Ledger.Serialization (mapFromCBOR)
Expand Down
11 changes: 6 additions & 5 deletions alonzo/impl/src/Cardano/Ledger/Alonzo/FakePlutus.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ module Cardano.Ledger.Alonzo.FakePlutus where
import qualified Data.ByteString as BS (ByteString)
import Data.Map (Map)
import Data.Word (Word64)
import Language.PlutusTx (Data (..))
import Language.PlutusTx.IsData.Class (IsData (..))
import Numeric.Natural (Natural)
import Plutus.V1.Ledger.Ada (adaSymbol, adaToken)
import Plutus.V1.Ledger.Contexts (TxOutInfo)
import Plutus.V1.Ledger.Crypto (PubKeyHash (..))
import Plutus.V1.Ledger.Interval
( Extended (..),
Expand All @@ -21,9 +18,11 @@ import Plutus.V1.Ledger.Interval
)
import Plutus.V1.Ledger.Scripts (Datum (..), DatumHash (..), MonetaryPolicyHash (..), ValidatorHash (..))
import Plutus.V1.Ledger.Slot (SlotRange)
import Plutus.V1.Ledger.Tx (TxOutRef (..), TxOutType (..))
import Plutus.V1.Ledger.Tx (TxOutRef (..))
import Plutus.V1.Ledger.TxId (TxId (..))
import Plutus.V1.Ledger.Value (CurrencySymbol (..), TokenName (..), Value (..), singleton, unionWith)
import PlutusTx.Data (Data (..))
import PlutusTx.IsData.Class (IsData (..))

-- ========================================================

Expand Down Expand Up @@ -109,5 +108,7 @@ data ScriptPurpose
data Context = Context TxInfo ScriptPurpose

instance IsData Context where
toData (Context _ _) = undefined
-- toData will be implemented in the Plutus library,
-- this is just a FakePlutus hack.
toData (Context _ _) = I 0
fromData _ctxdata = Nothing
5 changes: 2 additions & 3 deletions alonzo/impl/src/Cardano/Ledger/Alonzo/Rules/Utxow.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module Cardano.Ledger.Alonzo.Rules.Utxow where
-- import Shelley.Spec.Ledger.UTxO(UTxO(..))

import Cardano.Binary (FromCBOR (..), ToCBOR (..))
import Cardano.Ledger.Alonzo.Data (Data, DataHash)
import Cardano.Ledger.Alonzo.Data (DataHash)
import Cardano.Ledger.Alonzo.PParams (PParams)
import Cardano.Ledger.Alonzo.PlutusScriptApi (checkScriptData, language, scriptsNeeded)
import Cardano.Ledger.Alonzo.Rules.Utxo (AlonzoUTXO)
Expand Down Expand Up @@ -187,7 +187,6 @@ type ShelleyStyleWitnessNeeds era =
-- (in addition to ShelleyStyleWitnessNeeds)
type AlonzoStyleAdditions era =
( HasField "datahash" (Core.TxOut era) (StrictMaybe (DataHash (Crypto era))), -- BE SURE AND ADD THESE INSTANCES
HasField "txdatahash" (Core.Tx era) (Map.Map (DataHash (Crypto era)) (Data era)),
HasField "wppHash" (Core.TxBody era) (StrictMaybe (WitnessPPDataHash (Crypto era))),
HasField "txnetworkid" (Core.TxBody era) (StrictMaybe Network)
)
Expand Down Expand Up @@ -254,7 +253,7 @@ alonzoStyleWitness = do
SJust h <- [getField @"datahash" output],
isTwoPhaseScriptAddress @era tx (getField @"address" output)
]
txHashes = domain (getField @"txdatahash" tx)
txHashes = domain (txdats . wits' $ tx)
inputHashes = Set.fromList utxoHashes
txHashes == inputHashes ?! DataHashSetsDontAgree txHashes inputHashes

Expand Down
10 changes: 5 additions & 5 deletions alonzo/impl/src/Cardano/Ledger/Alonzo/Scripts.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ import Cardano.Ledger.Pretty
PrettyA (..),
ppCoin,
ppInteger,
ppLong,
ppMap,
ppRecord,
ppSexp,
ppString,
ppWord64,
text,
)
import Cardano.Ledger.SafeHash
( HashWithCrypto (..),
Expand All @@ -60,11 +60,11 @@ import Cardano.Ledger.SafeHash
import Cardano.Ledger.ShelleyMA.Timelocks
import Cardano.Ledger.Val (Val ((<+>), (<×>)))
import Control.DeepSeq (NFData (..))
import Data.ByteString (ByteString)
import Data.ByteString.Short (ShortByteString, fromShort)
import Data.Coders
import Data.Map (Map)
import Data.MemoBytes
import Data.Text (Text)
import Data.Typeable
import Data.Word (Word64, Word8)
import GHC.Generics (Generic)
Expand Down Expand Up @@ -144,7 +144,7 @@ pointWiseExUnits oper (ExUnits m1 s1) (ExUnits m2 s2) = (m1 `oper` m2) && (s1 `o
-- Cost Model needs to preserve its serialization bytes as
-- it is going to be hashed. Thus we make it a newtype around a MemoBytes

newtype CostModel = CostModelConstr (MemoBytes (Map ByteString Integer))
newtype CostModel = CostModelConstr (MemoBytes (Map Text Integer))
deriving (Eq, Generic, Show, Ord)
deriving newtype (SafeToHash)

Expand All @@ -153,7 +153,7 @@ newtype CostModel = CostModelConstr (MemoBytes (Map ByteString Integer))

instance HashWithCrypto CostModel CostModel

pattern CostModel :: Map ByteString Integer -> CostModel
pattern CostModel :: Map Text Integer -> CostModel
pattern CostModel m <-
CostModelConstr (Memo m _)
where
Expand Down Expand Up @@ -274,7 +274,7 @@ instance PrettyA ExUnits where prettyA = ppExUnits

ppCostModel :: CostModel -> PDoc
ppCostModel (CostModelConstr (Memo m _)) =
ppSexp "CostModel" [ppMap ppLong ppInteger m]
ppSexp "CostModel" [ppMap text ppInteger m]

instance PrettyA CostModel where prettyA = ppCostModel

Expand Down
6 changes: 4 additions & 2 deletions alonzo/impl/src/Cardano/Ledger/Alonzo/TxBody.hs
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,13 @@ deriving stock instance
Eq (TxOut era)

instance
( Show (Core.Value era)
( Era era,
Show (Core.Value era)
) =>
Show (TxOut era)
where
show = error "Not yet implemented"
show (TxOut addr vl dh) =
"TxOut (" <> show addr <> " " <> show vl <> " " <> show dh <> ")"

deriving via InspectHeapNamed "TxOut" (TxOut era) instance NoThunks (TxOut era)

Expand Down
17 changes: 11 additions & 6 deletions alonzo/impl/src/Cardano/Ledger/Alonzo/TxInfo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,13 @@ import Data.Maybe (mapMaybe)
import qualified Data.Set as Set
import Data.Typeable (Typeable)
import GHC.Records (HasField (..))
import qualified Language.PlutusCore.Evaluation.Machine.ExMemory as P (ExCPU (..), ExMemory (..))
import qualified Language.PlutusTx as P (Data (..))
import qualified Language.PlutusTx.IsData.Class as P (IsData (..))
import qualified Plutus.V1.Ledger.Ada as P (adaSymbol, adaToken)
import qualified Plutus.V1.Ledger.Api as P
( CostModelParameters,
( CostModel,
ExBudget (..),
VerboseMode (..),
evaluateScriptRestricting,
validateAndCreateCostModel,
validateScript,
)
import qualified Plutus.V1.Ledger.Crypto as P (PubKeyHash (..))
Expand All @@ -80,6 +78,10 @@ import qualified Plutus.V1.Ledger.Slot as P (SlotRange)
import qualified Plutus.V1.Ledger.Tx as P (TxOutRef (..))
import qualified Plutus.V1.Ledger.TxId as P (TxId (..))
import qualified Plutus.V1.Ledger.Value as P (CurrencySymbol (..), TokenName (..), Value (..), singleton, unionWith)
import qualified PlutusCore.Evaluation.Machine.ExBudgetingDefaults as P (defaultCostModel)
import qualified PlutusCore.Evaluation.Machine.ExMemory as P (ExCPU (..), ExMemory (..))
import qualified PlutusTx as P (Data (..))
import qualified PlutusTx.IsData.Class as P (IsData (..))
import Shelley.Spec.Ledger.Address (Addr (..), RewardAcnt (..))
import Shelley.Spec.Ledger.BaseTypes (StrictMaybe (..))
import Shelley.Spec.Ledger.Credential (Credential (KeyHashObj, ScriptHashObj), Ptr (..), StakeReference (..))
Expand Down Expand Up @@ -257,8 +259,11 @@ getWitVKeyHash = P.PubKeyHash . fromShort . (\(UnsafeHash x) -> x) . (\(KeyHash
transDataPair :: (DataHash c, Data era) -> (P.DatumHash, P.Datum)
transDataPair (x, y) = (transDataHash' x, P.Datum (getPlutusData y))

transCostModel :: CostModel -> P.CostModelParameters
transCostModel (CostModel mp) = Map.foldlWithKey' (\ans bytes n -> Map.insert (show bytes) n ans) Map.empty mp
transCostModel :: CostModel -> P.CostModel
transCostModel (CostModel mp) =
case P.validateAndCreateCostModel mp of
Nothing -> P.defaultCostModel -- TODO validation should be before this
Just cm -> cm

transExUnits :: ExUnits -> P.ExBudget
transExUnits (ExUnits mem steps) = P.ExBudget (P.ExCPU (fromIntegral steps)) (P.ExMemory (fromIntegral mem))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ import Data.Map (Map)
import qualified Data.Map as Map
import Data.Maybe (mapMaybe)
import qualified Data.Set as Set
import qualified Language.PlutusTx as Plutus
import qualified Data.Text as T (pack)
import qualified PlutusTx as Plutus
import Test.Cardano.Ledger.ShelleyMA.Serialisation.Generators (genMintValues)
import Test.QuickCheck
import Test.Shelley.Spec.Ledger.ConcreteCryptoTypes (Mock)
Expand Down Expand Up @@ -192,7 +193,7 @@ instance Arbitrary Prices where
arbitrary = Prices <$> arbitrary <*> arbitrary

instance Arbitrary CostModel where
arbitrary = CostModel <$> arbitrary
arbitrary = (CostModel . (Map.mapKeys T.pack)) <$> arbitrary

instance Arbitrary (PParams era) where
arbitrary =
Expand Down
Loading

0 comments on commit d227aa9

Please sign in to comment.