|
| 1 | +{-# LANGUAGE DataKinds #-} |
| 2 | +{-# LANGUAGE OverloadedStrings #-} |
| 3 | +{-# LANGUAGE PatternSynonyms #-} |
1 | 4 | {-# LANGUAGE ScopedTypeVariables #-} |
2 | 5 | {-# LANGUAGE TypeApplications #-} |
3 | 6 |
|
4 | 7 | module Test.Cardano.Ledger.Alonzo.GoldenSpec (spec) where |
5 | 8 |
|
6 | | -import Cardano.Ledger.Alonzo |
7 | | -import Paths_cardano_ledger_alonzo (getDataFileName) |
8 | | -import qualified Test.Cardano.Ledger.Alonzo.Binary.Golden as Golden |
9 | | -import Test.Cardano.Ledger.Alonzo.Era () |
| 9 | +import Cardano.Ledger.Address (Addr) |
| 10 | +import Cardano.Ledger.Allegra.Scripts ( |
| 11 | + pattern RequireTimeExpire, |
| 12 | + pattern RequireTimeStart, |
| 13 | + ) |
| 14 | +import Cardano.Ledger.Alonzo (AlonzoEra) |
| 15 | +import Cardano.Ledger.Alonzo.Core ( |
| 16 | + EraTxOut (..), |
| 17 | + PParams, |
| 18 | + dataHashTxOutL, |
| 19 | + emptyPParams, |
| 20 | + mkBasicTxOut, |
| 21 | + ppCostModelsL, |
| 22 | + ) |
| 23 | +import Cardano.Ledger.Alonzo.PParams (LangDepView (..), getLanguageView) |
| 24 | +import Cardano.Ledger.Alonzo.TxBody (utxoEntrySize) |
| 25 | +import Cardano.Ledger.BaseTypes (StrictMaybe (..)) |
| 26 | +import Cardano.Ledger.Coin (Coin (..)) |
| 27 | +import Cardano.Ledger.Core (hashScript) |
| 28 | +import Cardano.Ledger.Keys (KeyRole (..)) |
| 29 | +import Cardano.Ledger.Mary (MaryEra) |
| 30 | +import Cardano.Ledger.Mary.Value (AssetName (..), PolicyID (..), valueFromList) |
| 31 | +import Cardano.Ledger.Plutus.Data (Data (..), hashData) |
| 32 | +import Cardano.Ledger.Plutus.Language (Language (..)) |
| 33 | +import Cardano.Ledger.Shelley.Scripts (pattern RequireAllOf) |
| 34 | +import Cardano.Ledger.Slot (SlotNo (..)) |
| 35 | +import qualified Data.ByteString.Base16 as B16 |
| 36 | +import qualified Data.ByteString.Short as SBS |
| 37 | +import qualified Data.Sequence.Strict as StrictSeq |
| 38 | +import Data.Word (Word8) |
| 39 | +import Lens.Micro |
| 40 | +import qualified PlutusLedgerApi.V1 as PV1 |
| 41 | +import qualified Test.Cardano.Ledger.Alonzo.Binary.GoldenSpec as BinaryGoldenSpec |
| 42 | +import qualified Test.Cardano.Ledger.Alonzo.JSON.GoldenSpec as JsonGoldenSpec |
10 | 43 | import Test.Cardano.Ledger.Common |
11 | | -import Test.Cardano.Ledger.Core.JSON (goldenJsonPParamsSpec, goldenJsonPParamsUpdateSpec) |
| 44 | +import Test.Cardano.Ledger.Core.KeyPair (mkAddr, mkKeyPair) |
| 45 | +import Test.Cardano.Ledger.Plutus (zeroTestingCostModels) |
12 | 46 |
|
13 | 47 | spec :: Spec |
14 | 48 | spec = |
15 | 49 | describe "Golden" $ do |
16 | | - describe "JSON files" $ do |
17 | | - beforeAll (getDataFileName "golden/pparams.json") $ |
18 | | - goldenJsonPParamsSpec @AlonzoEra |
19 | | - beforeAll (getDataFileName "golden/pparams-update.json") $ |
20 | | - goldenJsonPParamsUpdateSpec @AlonzoEra |
21 | | - describe "CBOR" $ Golden.spec @AlonzoEra |
| 50 | + describe "JSON" JsonGoldenSpec.spec |
| 51 | + describe "CBOR" BinaryGoldenSpec.spec |
| 52 | + goldenScriptIntegrity |
| 53 | + goldenUTxOEntryMinAda |
| 54 | + |
| 55 | +goldenScriptIntegrity :: Spec |
| 56 | +goldenScriptIntegrity = |
| 57 | + describe "Script integrity hash" $ do |
| 58 | + it "PlutusV1" $ testScriptIntegrityHash exPP PlutusV1 exampleLangDepViewPV1 |
| 59 | + it "PlutusV2" $ testScriptIntegrityHash exPP PlutusV2 exampleLangDepViewPV2 |
| 60 | + where |
| 61 | + testScriptIntegrityHash :: |
| 62 | + HasCallStack => |
| 63 | + PParams AlonzoEra -> |
| 64 | + Language -> |
| 65 | + LangDepView -> |
| 66 | + Expectation |
| 67 | + testScriptIntegrityHash pp lang view = getLanguageView pp lang `shouldBe` view |
| 68 | + |
| 69 | + exampleLangDepViewPV1 :: LangDepView |
| 70 | + exampleLangDepViewPV1 = LangDepView b1 b2 |
| 71 | + where |
| 72 | + b1 = |
| 73 | + fromRightError "invalid hex encoding of the language inside exampleLangDepViewPV1" $ |
| 74 | + B16.decode "4100" |
| 75 | + b2 = |
| 76 | + fromRightError "invalid hex encoding of the cost model inside exampleLangDepViewPV1" $ |
| 77 | + B16.decode $ |
| 78 | + "58a89f0000000000000000000000000000000000000000000000000000000000" |
| 79 | + <> "0000000000000000000000000000000000000000000000000000000000000000" |
| 80 | + <> "0000000000000000000000000000000000000000000000000000000000000000" |
| 81 | + <> "0000000000000000000000000000000000000000000000000000000000000000" |
| 82 | + <> "0000000000000000000000000000000000000000000000000000000000000000" |
| 83 | + <> "000000000000000000ff" |
| 84 | + |
| 85 | + exampleLangDepViewPV2 :: LangDepView |
| 86 | + exampleLangDepViewPV2 = LangDepView b1 b2 |
| 87 | + where |
| 88 | + b1 = |
| 89 | + fromRightError "invalid hex encoding of the language inside exampleLangDepViewPV2" $ |
| 90 | + B16.decode "01" |
| 91 | + b2 = |
| 92 | + fromRightError "invalid hex encoding of the cost model inside exampleLangDepViewPV2" $ |
| 93 | + B16.decode $ |
| 94 | + "98af000000000000000000000000000000000000000000000000000000000000" |
| 95 | + <> "0000000000000000000000000000000000000000000000000000000000000000" |
| 96 | + <> "0000000000000000000000000000000000000000000000000000000000000000" |
| 97 | + <> "0000000000000000000000000000000000000000000000000000000000000000" |
| 98 | + <> "0000000000000000000000000000000000000000000000000000000000000000" |
| 99 | + <> "0000000000000000000000000000000000" |
| 100 | + |
| 101 | + exPP :: PParams AlonzoEra |
| 102 | + exPP = |
| 103 | + emptyPParams |
| 104 | + & ppCostModelsL .~ zeroTestingCostModels [PlutusV1, PlutusV2] |
| 105 | + |
| 106 | + fromRightError :: (HasCallStack, Show a) => String -> Either a b -> b |
| 107 | + fromRightError errorMsg = |
| 108 | + either (\e -> error $ errorMsg ++ ": " ++ show e) id |
| 109 | + |
| 110 | +-- | (heapWords of a DataHash) * coinsPerUTxOWordLocal is 344820 |
| 111 | +goldenUTxOEntryMinAda :: Spec |
| 112 | +goldenUTxOEntryMinAda = |
| 113 | + describe "golden tests - UTxOEntryMinAda" $ do |
| 114 | + it "one policy, one (smallest) name, yes datum hash" $ |
| 115 | + calcMinUTxO |
| 116 | + ( mkBasicTxOut carlAddr (valueFromList (Coin 1407406) [(pid1, smallestName, 1)]) |
| 117 | + & dataHashTxOutL .~ SJust (hashData @AlonzoEra (Data (PV1.List []))) |
| 118 | + ) |
| 119 | + `shouldBe` Coin 1655136 |
| 120 | + it "one policy, one (smallest) name, no datum hash" $ |
| 121 | + calcMinUTxO |
| 122 | + (mkBasicTxOut bobAddr (valueFromList (Coin 1407406) [(pid1, smallestName, 1)])) |
| 123 | + `shouldBe` Coin 1310316 |
| 124 | + it "one policy, one (small) name" $ |
| 125 | + calcMinUTxO |
| 126 | + (mkBasicTxOut aliceAddr (valueFromList (Coin 1444443) [(pid1, smallName 1, 1)])) |
| 127 | + `shouldBe` Coin 1344798 |
| 128 | + it "one policy, three (small) names" $ |
| 129 | + calcMinUTxO |
| 130 | + ( mkBasicTxOut |
| 131 | + aliceAddr |
| 132 | + ( valueFromList |
| 133 | + (Coin 1555554) |
| 134 | + [ (pid1, smallName 1, 1) |
| 135 | + , (pid1, smallName 2, 1) |
| 136 | + , (pid1, smallName 3, 1) |
| 137 | + ] |
| 138 | + ) |
| 139 | + ) |
| 140 | + `shouldBe` Coin 1448244 |
| 141 | + it "one policy, one (largest) name" $ |
| 142 | + calcMinUTxO |
| 143 | + (mkBasicTxOut carlAddr (valueFromList (Coin 1555554) [(pid1, largestName 65, 1)])) |
| 144 | + `shouldBe` Coin 1448244 |
| 145 | + it "one policy, three (largest) name, with hash" $ |
| 146 | + calcMinUTxO |
| 147 | + ( mkBasicTxOut |
| 148 | + carlAddr |
| 149 | + ( valueFromList |
| 150 | + (Coin 1962961) |
| 151 | + [ (pid1, largestName 65, 1) |
| 152 | + , (pid1, largestName 66, 1) |
| 153 | + , (pid1, largestName 67, 1) |
| 154 | + ] |
| 155 | + ) |
| 156 | + & dataHashTxOutL .~ SJust (hashData @AlonzoEra (Data (PV1.Constr 0 [PV1.Constr 0 []]))) |
| 157 | + ) |
| 158 | + `shouldBe` Coin 2172366 |
| 159 | + it "two policies, one (smallest) name" $ |
| 160 | + calcMinUTxO |
| 161 | + ( mkBasicTxOut |
| 162 | + aliceAddr |
| 163 | + (valueFromList (Coin 1592591) [(pid1, smallestName, 1), (pid2, smallestName, 1)]) |
| 164 | + ) |
| 165 | + `shouldBe` Coin 1482726 |
| 166 | + it "two policies, one (smallest) name, with hash" $ |
| 167 | + calcMinUTxO |
| 168 | + ( mkBasicTxOut |
| 169 | + aliceAddr |
| 170 | + (valueFromList (Coin 1592591) [(pid1, smallestName, 1), (pid2, smallestName, 1)]) |
| 171 | + & dataHashTxOutL .~ SJust (hashData @AlonzoEra (Data (PV1.Constr 0 []))) |
| 172 | + ) |
| 173 | + `shouldBe` Coin 1827546 |
| 174 | + it "two policies, two (small) names" $ |
| 175 | + calcMinUTxO |
| 176 | + (mkBasicTxOut bobAddr (valueFromList (Coin 1629628) [(pid1, smallName 1, 1), (pid2, smallName 2, 1)])) |
| 177 | + `shouldBe` Coin 1517208 |
| 178 | + it "three policies, ninety-six (small) names" $ |
| 179 | + calcMinUTxO |
| 180 | + ( mkBasicTxOut |
| 181 | + aliceAddr |
| 182 | + ( let f i c = (i, smallName c, 1) |
| 183 | + in valueFromList |
| 184 | + (Coin 7407400) |
| 185 | + [ f i c |
| 186 | + | (i, cs) <- |
| 187 | + [(pid1, [32 .. 63]), (pid2, [64 .. 95]), (pid3, [96 .. 127])] |
| 188 | + , c <- cs |
| 189 | + ] |
| 190 | + ) |
| 191 | + ) |
| 192 | + `shouldBe` Coin 6896400 |
| 193 | + it "utxo entry size of ada-only" $ |
| 194 | + -- This value, 29, is helpful for comparing the alonzo protocol parameter utxoCostPerWord |
| 195 | + -- with the old parameter minUTxOValue. |
| 196 | + -- If we wish to keep the ada-only, no datum hash, minimum value nearly the same, |
| 197 | + -- we can divide minUTxOValue by 29 and round. |
| 198 | + utxoEntrySize @AlonzoEra (mkBasicTxOut aliceAddr mempty) `shouldBe` 29 |
| 199 | + where |
| 200 | + calcMinUTxO :: TxOut AlonzoEra -> Coin |
| 201 | + calcMinUTxO tout = Coin (utxoEntrySize tout * coinsPerUTxOWordLocal) |
| 202 | + |
| 203 | + -- \| ada cost of storing a word8 of data as a UTxO entry, assuming no change to minUTxOValue |
| 204 | + coinsPerUTxOWordLocal :: Integer |
| 205 | + coinsPerUTxOWordLocal = quot minUTxOValueShelleyMA utxoEntrySizeWithoutValLocal |
| 206 | + where |
| 207 | + utxoEntrySizeWithoutValLocal = 29 |
| 208 | + Coin minUTxOValueShelleyMA = minUTxO |
| 209 | + |
| 210 | +-- Helpers inlined from Test.Cardano.Ledger.Mary.Golden |
| 211 | +-- (cardano-ledger-shelley-ma-test cannot be a dep due to cyclic dependency via cardano-protocol-tpraos) |
| 212 | + |
| 213 | +pid1 :: PolicyID |
| 214 | +pid1 = |
| 215 | + PolicyID $ |
| 216 | + hashScript @MaryEra $ |
| 217 | + RequireAllOf (StrictSeq.fromList []) |
| 218 | + |
| 219 | +pid2 :: PolicyID |
| 220 | +pid2 = |
| 221 | + PolicyID $ |
| 222 | + hashScript @MaryEra $ |
| 223 | + RequireAllOf (StrictSeq.fromList [RequireTimeStart (SlotNo 1)]) |
| 224 | + |
| 225 | +pid3 :: PolicyID |
| 226 | +pid3 = |
| 227 | + PolicyID $ |
| 228 | + hashScript @MaryEra $ |
| 229 | + RequireAllOf (StrictSeq.fromList [RequireTimeExpire (SlotNo 1)]) |
| 230 | + |
| 231 | +smallestName :: AssetName |
| 232 | +smallestName = AssetName $ SBS.pack [] |
| 233 | + |
| 234 | +smallName :: Word8 -> AssetName |
| 235 | +smallName c = AssetName $ SBS.pack [c] |
| 236 | + |
| 237 | +largestName :: Word8 -> AssetName |
| 238 | +largestName c = AssetName . SBS.pack $ c : [1 .. 31] |
| 239 | + |
| 240 | +minUTxO :: Coin |
| 241 | +minUTxO = Coin $ 1000 * 1000 |
| 242 | + |
| 243 | +-- Addresses inlined from Test.Cardano.Ledger.Shelley.Examples.Cast |
| 244 | +-- (cardano-ledger-shelley-test cannot be a dep due to cyclic dependency via cardano-protocol-tpraos) |
| 245 | + |
| 246 | +aliceAddr :: Addr |
| 247 | +aliceAddr = mkAddr (mkKeyPair @Payment 0) (mkKeyPair @Staking 1) |
| 248 | + |
| 249 | +bobAddr :: Addr |
| 250 | +bobAddr = mkAddr (mkKeyPair @Payment 2) (mkKeyPair @Staking 3) |
| 251 | + |
| 252 | +carlAddr :: Addr |
| 253 | +carlAddr = mkAddr (mkKeyPair @Payment 4) (mkKeyPair @Staking 5) |
0 commit comments