Skip to content

Commit

Permalink
Adapt cardano-ledger-test to MultiAsset mint field
Browse files Browse the repository at this point in the history
  • Loading branch information
teodanciu committed Aug 8, 2022
1 parent 0d64169 commit d425d76
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 47 deletions.
Expand Up @@ -76,6 +76,7 @@ import Cardano.Ledger.Keys
hashKey,
hashVerKeyVRF,
)
import Cardano.Ledger.Mary.Value (MaryValue (..), MultiAsset (..))
import Cardano.Ledger.Pretty
import Cardano.Ledger.Pretty.Babbage ()
import Cardano.Ledger.SafeHash (hashAnnotated)
Expand Down Expand Up @@ -783,11 +784,11 @@ utxoStEx6 pf = smartUTxOState (utxoEx6 pf) (Coin 0) (Coin 5) def
-- Example 7: Process a MINT transaction with a succeeding Plutus script.
-- =============================================================================

mintEx7 :: forall era. (Scriptic era, HasTokens era) => Proof era -> Value era
mintEx7 :: forall era. (Scriptic era, HasTokens era) => Proof era -> MultiAsset (Crypto era)
mintEx7 pf = forge @era 1 (always 2 pf)

outEx7 :: (HasTokens era, EraTxOut era, Scriptic era) => Proof era -> TxOut era
outEx7 pf = newTxOut pf [Address (someAddr pf), Amount (mintEx7 pf <+> inject (Coin 995))]
outEx7 :: (HasTokens era, EraTxOut era, Scriptic era, Value era ~ MaryValue (Crypto era)) => Proof era -> TxOut era
outEx7 pf = newTxOut pf [Address (someAddr pf), Amount (MaryValue 0 (mintEx7 pf) <+> inject (Coin 995))]

redeemerExample7 :: Data era
redeemerExample7 = Data (Plutus.I 42)
Expand All @@ -798,7 +799,7 @@ validatingRedeemersEx7 =
Map.singleton (RdmrPtr Tag.Mint 0) (redeemerExample7, ExUnits 5000 5000)

validatingBodyWithMint ::
(HasTokens era, EraTxBody era, Scriptic era) =>
(HasTokens era, EraTxBody era, Scriptic era, Value era ~ MaryValue (Crypto era)) =>
Proof era ->
TxBody era
validatingBodyWithMint pf =
Expand All @@ -817,7 +818,8 @@ validatingTxWithMint ::
( Scriptic era,
HasTokens era,
EraTx era,
GoodCrypto (Crypto era)
GoodCrypto (Crypto era),
Value era ~ MaryValue (Crypto era)
) =>
Proof era ->
Tx era
Expand All @@ -832,12 +834,12 @@ validatingTxWithMint pf =
]
]

utxoEx7 :: forall era. (HasTokens era, EraTxBody era, PostShelley era) => Proof era -> UTxO era
utxoEx7 :: forall era. (HasTokens era, EraTxBody era, PostShelley era, Value era ~ MaryValue (Crypto era)) => Proof era -> UTxO era
utxoEx7 pf = expectedUTxO' pf (ExpectSuccess (validatingBodyWithMint pf) (outEx7 pf)) 7

utxoStEx7 ::
forall era.
(Default (State (EraRule "PPUP" era)), PostShelley era, EraTxBody era, HasTokens era) =>
(Default (State (EraRule "PPUP" era)), PostShelley era, EraTxBody era, HasTokens era, Value era ~ MaryValue (Crypto era)) =>
Proof era ->
UTxOState era
utxoStEx7 pf = smartUTxOState (utxoEx7 pf) (Coin 0) (Coin 5) def
Expand All @@ -846,11 +848,11 @@ utxoStEx7 pf = smartUTxOState (utxoEx7 pf) (Coin 0) (Coin 5) def
-- Example 8: Process a MINT transaction with a failing Plutus script.
-- ==============================================================================

mintEx8 :: forall era. (Scriptic era, HasTokens era) => Proof era -> Value era
mintEx8 :: forall era. (Scriptic era, HasTokens era) => Proof era -> MultiAsset (Crypto era)
mintEx8 pf = forge @era 1 (never 1 pf)

outEx8 :: (HasTokens era, EraTxOut era, Scriptic era) => Proof era -> TxOut era
outEx8 pf = newTxOut pf [Address (someAddr pf), Amount (mintEx8 pf <+> inject (Coin 995))]
outEx8 :: (HasTokens era, EraTxOut era, Scriptic era, Value era ~ MaryValue (Crypto era)) => Proof era -> TxOut era
outEx8 pf = newTxOut pf [Address (someAddr pf), Amount ((MaryValue 0 (mintEx8 pf)) <+> inject (Coin 995))]

redeemerExample8 :: Data era
redeemerExample8 = Data (Plutus.I 0)
Expand All @@ -861,7 +863,7 @@ notValidatingRedeemersEx8 =
Map.singleton (RdmrPtr Tag.Mint 0) (redeemerExample8, ExUnits 5000 5000)

notValidatingBodyWithMint ::
(HasTokens era, EraTxBody era, Scriptic era) =>
(HasTokens era, EraTxBody era, Scriptic era, Value era ~ MaryValue (Crypto era)) =>
Proof era ->
TxBody era
notValidatingBodyWithMint pf =
Expand All @@ -880,7 +882,8 @@ notValidatingTxWithMint ::
( Scriptic era,
HasTokens era,
EraTx era,
GoodCrypto (Crypto era)
GoodCrypto (Crypto era),
Value era ~ MaryValue (Crypto era)
) =>
Proof era ->
Tx era
Expand Down Expand Up @@ -918,22 +921,22 @@ validatingRedeemersEx9 =
(RdmrPtr Tag.Mint 1, (Data (Plutus.I 104), ExUnits 5000 5000))
]

mintEx9 :: forall era. (PostShelley era, EraTxOut era, HasTokens era) => Proof era -> Value era
mintEx9 pf = forge @era 1 (always 2 pf) <+> forge @era 1 (timelockScript 1 pf)
mintEx9 :: forall era. (PostShelley era, HasTokens era) => Proof era -> MultiAsset (Crypto era)
mintEx9 pf = forge @era 1 (always 2 pf) <> forge @era 1 (timelockScript 1 pf)

outEx9 :: (HasTokens era, EraTxOut era, PostShelley era) => Proof era -> TxOut era
outEx9 :: (HasTokens era, EraTxOut era, PostShelley era, Value era ~ MaryValue (Crypto era)) => Proof era -> TxOut era
outEx9 pf =
newTxOut
pf
[ Address (someAddr pf),
Amount (mintEx9 pf <+> inject (Coin 4996))
Amount ((MaryValue 0 (mintEx9 pf)) <+> inject (Coin 4996))
]

timelockStakeCred :: PostShelley era => Proof era -> StakeCredential (Crypto era)
timelockStakeCred pf = ScriptHashObj (timelockHash 2 pf)

validatingBodyManyScripts ::
(HasTokens era, EraTxBody era, PostShelley era) =>
(HasTokens era, EraTxBody era, PostShelley era, Value era ~ MaryValue (Crypto era)) =>
Proof era ->
TxBody era
validatingBodyManyScripts pf =
Expand Down Expand Up @@ -964,7 +967,8 @@ validatingTxManyScripts ::
( PostShelley era,
HasTokens era,
EraTxBody era,
GoodCrypto (Crypto era)
GoodCrypto (Crypto era),
Value era ~ MaryValue (Crypto era)
) =>
Proof era ->
Tx era
Expand All @@ -989,7 +993,7 @@ validatingTxManyScripts pf =
]
]

utxoEx9 :: forall era. (EraTxBody era, PostShelley era, HasTokens era) => Proof era -> UTxO era
utxoEx9 :: forall era. (EraTxBody era, PostShelley era, HasTokens era, Value era ~ MaryValue (Crypto era)) => Proof era -> UTxO era
utxoEx9 pf = UTxO utxo
where
utxo =
Expand All @@ -1000,7 +1004,7 @@ utxoEx9 pf = UTxO utxo

utxoStEx9 ::
forall era.
(EraTxBody era, Default (State (EraRule "PPUP" era)), PostShelley era, HasTokens era) =>
(EraTxBody era, Default (State (EraRule "PPUP" era)), PostShelley era, HasTokens era, Value era ~ MaryValue (Crypto era)) =>
Proof era ->
UTxOState era
utxoStEx9 pf = smartUTxOState (utxoEx9 pf) (Coin 0) (Coin 5) def
Expand Down Expand Up @@ -1258,7 +1262,8 @@ missing1phaseScriptWitnessTx ::
( PostShelley era,
HasTokens era,
EraTxBody era,
GoodCrypto (Crypto era)
GoodCrypto (Crypto era),
Value era ~ MaryValue (Crypto era)
) =>
Proof era ->
Tx era
Expand Down Expand Up @@ -1288,7 +1293,8 @@ missing2phaseScriptWitnessTx ::
( PostShelley era,
HasTokens era,
EraTx era,
GoodCrypto (Crypto era)
GoodCrypto (Crypto era),
Value era ~ MaryValue (Crypto era)
) =>
Proof era ->
Tx era
Expand Down Expand Up @@ -1385,7 +1391,8 @@ phase1FailureTx ::
( PostShelley era,
HasTokens era,
EraTx era,
GoodCrypto (Crypto era)
GoodCrypto (Crypto era),
Value era ~ MaryValue (Crypto era)
) =>
Proof era ->
Tx era
Expand Down Expand Up @@ -1786,7 +1793,8 @@ testAlonzoBlock ::
( GoodCrypto (Crypto era),
HasTokens era,
Scriptic era,
EraSegWits era
EraSegWits era,
Value era ~ MaryValue (Crypto era)
) =>
Proof era ->
Block (BHeaderView (Crypto era)) era
Expand All @@ -1811,7 +1819,8 @@ example1UTxO ::
( GoodCrypto (Crypto era),
HasTokens era,
PostShelley era,
EraTxBody era
EraTxBody era,
Value era ~ MaryValue (Crypto era)
) =>
Proof era ->
UTxO era
Expand Down Expand Up @@ -1841,7 +1850,8 @@ example1UtxoSt ::
GoodCrypto (Crypto era),
HasTokens era,
PostShelley era,
Default (State (EraRule "PPUP" era))
Default (State (EraRule "PPUP" era)),
Value era ~ MaryValue (Crypto era)
) =>
Proof era ->
UTxOState era
Expand All @@ -1852,7 +1862,8 @@ example1BBodyState ::
HasTokens era,
PostShelley era,
Default (State (EraRule "PPUP" era)),
EraTxBody era
EraTxBody era,
Value era ~ MaryValue (Crypto era)
) =>
Proof era ->
BbodyState era
Expand Down Expand Up @@ -2083,7 +2094,8 @@ alonzoUTXOWexamplesB ::
Scriptic era,
Default (State (EraRule "PPUP" era)),
EraTx era,
PostShelley era -- MAYBE WE CAN REPLACE THIS BY GoodCrypto
PostShelley era, -- MAYBE WE CAN REPLACE THIS BY GoodCrypto,
Value era ~ MaryValue (Crypto era)
) =>
Proof era ->
TestTree
Expand Down Expand Up @@ -2417,6 +2429,7 @@ alonzoBBODYexamplesP ::
HasTokens era,
Default (State (EraRule "PPUP" era)),
PostShelley era,
Value era ~ MaryValue (Crypto era),
EraSegWits era
) =>
Proof era ->
Expand Down
Expand Up @@ -61,7 +61,7 @@ import Cardano.Ledger.Core
import Cardano.Ledger.Credential (Credential (..), StakeReference (..))
import Cardano.Ledger.Keys (KeyHash, KeyPair (..), KeyRole (..), hashKey)
import Cardano.Ledger.Keys.Bootstrap (BootstrapWitness (..))
import Cardano.Ledger.Mary.Value (MaryValue (..))
import Cardano.Ledger.Mary.Value (MaryValue (..), MultiAsset (..))
import Cardano.Ledger.Serialization (sizedValue)
import qualified Cardano.Ledger.Shelley.PParams as PP (Update)
import Cardano.Ledger.Shelley.Tx (ShelleyTx (..), ShelleyTxOut (..), pattern WitnessSet)
Expand Down Expand Up @@ -118,7 +118,7 @@ data TxBodyField era
| TTL SlotNo
| Update (StrictMaybe (PP.Update era))
| ReqSignerHashes (Set (KeyHash 'Witness (Crypto era)))
| Mint (Value era)
| Mint (MultiAsset (Crypto era))
| WppHash (StrictMaybe (ScriptIntegrityHash (Crypto era)))
| AdHash (StrictMaybe (AuxiliaryDataHash (Crypto era)))
| Txnetworkid (StrictMaybe Network)
Expand Down Expand Up @@ -241,8 +241,8 @@ initValue = MaryValue 0 mempty

initialTxBody :: Era era => Proof era -> TxBody era
initialTxBody (Shelley _) = ShelleyTxBody Set.empty Seq.empty Seq.empty initWdrl (Coin 0) (SlotNo 0) SNothing SNothing
initialTxBody (Allegra _) = MATxBody Set.empty Seq.empty Seq.empty initWdrl (Coin 0) initVI SNothing SNothing (Coin 0)
initialTxBody (Mary _) = MATxBody Set.empty Seq.empty Seq.empty initWdrl (Coin 0) initVI SNothing SNothing initValue
initialTxBody (Allegra _) = MATxBody Set.empty Seq.empty Seq.empty initWdrl (Coin 0) initVI SNothing SNothing mempty
initialTxBody (Mary _) = MATxBody Set.empty Seq.empty Seq.empty initWdrl (Coin 0) initVI SNothing SNothing mempty
initialTxBody (Alonzo _) =
AlonzoTxBody
Set.empty
Expand All @@ -254,7 +254,7 @@ initialTxBody (Alonzo _) =
initVI
SNothing
Set.empty
initValue
mempty
SNothing
SNothing
SNothing
Expand All @@ -272,7 +272,7 @@ initialTxBody (Babbage _) =
initVI
SNothing
Set.empty
initValue
mempty
SNothing
SNothing
SNothing
Expand Down
Expand Up @@ -818,7 +818,7 @@ txBodyFieldSummary txb = case txb of
(Txfee c) -> [("Fee", ppCoin c)]
(Update (SJust _)) -> [("Collateral Return", ppString "?")]
(ReqSignerHashes x) -> [("Required Signer hashes", ppInt (Set.size x))]
(Mint v) -> [("Mint", ppInteger (Val.size v) <> ppString " bytes")]
(Mint ma) -> [("Mint", ppInteger (Val.size (MaryValue 0 ma)) <> ppString " bytes")]
(WppHash (SJust _)) -> [("WppHash", ppString "?")]
(AdHash (SJust _)) -> [("AdHash", ppString "?")]
(Txnetworkid (SJust x)) -> [("Network id", ppNetwork x)]
Expand Down Expand Up @@ -891,9 +891,12 @@ plutusDataSummary (Plutus.List xs) = ppList plutusDataSummary xs
plutusDataSummary (Plutus.I n) = ppInteger n
plutusDataSummary (Plutus.B bs) = trim (ppLong bs)

multiAssetSummary :: MultiAsset c -> PDoc
multiAssetSummary (MultiAsset m) = ppString ("num tokens = " ++ show (Map.size m))

vSummary :: MaryValue c -> PDoc
vSummary (MaryValue n (MultiAsset m)) =
ppSexp "Value" [ppInteger n, ppString ("num tokens = " ++ show (Map.size m))]
vSummary (MaryValue n ma) =
ppSexp "Value" [ppInteger n, multiAssetSummary ma]

scriptSummary :: forall era. Proof era -> Script era -> PDoc
scriptSummary p@(Babbage _) script = plutusSummary p script
Expand Down Expand Up @@ -1224,7 +1227,7 @@ pcTxBodyField proof x = case x of
Update SNothing -> []
Update (SJust _) -> [("update", ppString "UPDATE")]
ReqSignerHashes s -> [("required hashes", ppSet pcKeyHash s)]
Mint v -> [("minted", pcCoreValue proof v)]
Mint v -> [("minted", multiAssetSummary v)]
WppHash SNothing -> []
WppHash (SJust h) -> [("integrity hash", trim (ppSafeHash h))]
AdHash SNothing -> []
Expand Down
Expand Up @@ -14,7 +14,7 @@ import Cardano.Ledger.Alonzo.Scripts (AlonzoScript (..))
import Cardano.Ledger.Core
import qualified Cardano.Ledger.Crypto as CC (Crypto)
import Cardano.Ledger.Keys (KeyHash, KeyRole (..))
import Cardano.Ledger.Mary.Value (AssetName (..), MaryValue (..), MultiAsset (..), PolicyID (..))
import Cardano.Ledger.Mary.Value (AssetName (..), MultiAsset (..), PolicyID (..))
import qualified Cardano.Ledger.Shelley.Scripts as Multi
import Cardano.Ledger.ShelleyMA.Timelocks (Timelock (..))
import Cardano.Slotting.Slot (SlotNo (..))
Expand Down Expand Up @@ -45,7 +45,7 @@ class Scriptic era => PostShelley era where
after :: Int -> Proof era -> Script era

class HasTokens era where
forge :: Integer -> Script era -> Value era
forge :: Integer -> Script era -> MultiAsset (Crypto era)

instance CC.Crypto c => Scriptic (ShelleyEra c) where
never _ (Shelley _) = Multi.RequireAnyOf mempty -- always False
Expand Down Expand Up @@ -87,19 +87,19 @@ instance CC.Crypto c => PostShelley (MaryEra c) where
after n (Mary _) = RequireTimeExpire (theSlot n)

instance forall c. CC.Crypto c => HasTokens (MaryEra c) where
forge n s = MaryValue 0 $ MultiAsset $ Map.singleton pid (Map.singleton an n)
forge n s = MultiAsset $ Map.singleton pid (Map.singleton an n)
where
pid = PolicyID (hashScript @(MaryEra c) s)
an = AssetName "an"

instance forall c. CC.Crypto c => HasTokens (AlonzoEra c) where
forge n s = MaryValue 0 $ MultiAsset $ Map.singleton pid (Map.singleton an n)
forge n s = MultiAsset $ Map.singleton pid (Map.singleton an n)
where
pid = PolicyID (hashScript @(AlonzoEra c) s)
an = AssetName "an"

instance forall c. CC.Crypto c => HasTokens (BabbageEra c) where
forge n s = MaryValue 0 $ MultiAsset $ Map.singleton pid (Map.singleton an n)
forge n s = MultiAsset $ Map.singleton pid (Map.singleton an n)
where
pid = PolicyID (hashScript @(BabbageEra c) s)
an = AssetName "an"
Expand Down
Expand Up @@ -24,6 +24,7 @@ import qualified Cardano.Ledger.Alonzo.Tx as Alonzo
import Cardano.Ledger.Alonzo.TxBody (AlonzoTxBody (AlonzoTxBody), AlonzoTxOut (AlonzoTxOut))
import qualified Cardano.Ledger.Alonzo.TxWitness as Alonzo
import Cardano.Ledger.Crypto (DSIGN, KES)
import Cardano.Ledger.Mary.Value (MaryValue (..))
import Cardano.Ledger.Shelley.API.Genesis (initialState)
import Cardano.Ledger.Shelley.API.Mempool (ApplyTxError (..))
import qualified Cardano.Ledger.Shelley.LedgerState as LedgerState
Expand Down Expand Up @@ -124,13 +125,14 @@ instance
Alonzo.txvldt = ValidityInterval SNothing $ SJust (1 + maxTTL),
Alonzo.txUpdates = SNothing,
Alonzo.reqSignerHashes = Set.empty,
Alonzo.mint = mint,
Alonzo.mint = ma,
Alonzo.scriptIntegrityHash = redeemers >>= uncurry (Alonzo.hashScriptIntegrity langViews),
Alonzo.adHash = SNothing,
Alonzo.txnetworkid = SNothing -- SJust Testnet
}
where
langViews = Set.singleton $ Alonzo.getLanguageView (LedgerState.esPp . LedgerState.nesEs $ nes) PlutusV1
MaryValue _ ma = mint

makeTx _ realTxBody (TxWitnessArguments wits (SupportsScript ScriptFeatureTag_PlutusV1 scripts) (SupportsPlutus (rdmr, dats)) (SupportsPlutus isValid)) =
let witSet =
Expand Down

0 comments on commit d425d76

Please sign in to comment.