Skip to content

Commit

Permalink
Adapt cardano-ledger-shelley-ma-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 93a87dd commit 5d70dc2
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 89 deletions.
Expand Up @@ -61,7 +61,7 @@ exampleTxBodyMA value =
(ValidityInterval (SJust (SlotNo 2)) (SJust (SlotNo 4)))
(SJust (Update exampleProposedPPUpdates (EpochNo 0)))
(SJust auxiliaryDataHash)
value
mempty
where
-- Dummy hash to decouple from the auxiliary data in 'exampleTx'.
auxiliaryDataHash :: AuxiliaryDataHash (Crypto era)
Expand Down
Expand Up @@ -41,7 +41,7 @@ import Cardano.Ledger.ShelleyMA.TxBody
ValidityInterval (ValidityInterval),
)
import Cardano.Ledger.TxIn (TxIn)
import Cardano.Ledger.Val (Val (zero), (<+>))
import Cardano.Ledger.Val ((<+>))
import Cardano.Slotting.Slot (SlotNo (SlotNo))
import Control.Monad (replicateM)
import Data.Hashable (hash)
Expand Down Expand Up @@ -106,7 +106,7 @@ genTxBody ::
Gen (MATxBody era, [Timelock (Crypto era)])
genTxBody slot ins outs cert wdrl fee upd ad = do
validityInterval <- genValidityInterval slot
let mint = zero -- the mint field is always empty for an Allegra TxBody
let mint = mempty -- the mint field is always empty for an Allegra TxBody
pure
( MATxBody
ins
Expand Down
23 changes: 12 additions & 11 deletions eras/shelley-ma/test-suite/src/Test/Cardano/Ledger/MaryEraGen.hs
Expand Up @@ -20,9 +20,10 @@ import qualified Cardano.Ledger.Crypto as CC
import Cardano.Ledger.Mary.Value
( AssetName (..),
MaryValue (..),
MultiAsset,
PolicyID (..),
multiAssetFromList,
policies,
valueFromList,
)
import Cardano.Ledger.Shelley.PParams (ShelleyPParams, ShelleyPParamsHKD (..), Update)
import Cardano.Ledger.Shelley.Tx
Expand Down Expand Up @@ -150,10 +151,10 @@ redCoinId = PolicyID $ hashScript @(MaryEra c) redCoins
red :: AssetName
red = AssetName "red"

genRed :: CC.Crypto c => Gen (MaryValue c)
genRed :: CC.Crypto c => Gen (MultiAsset c)
genRed = do
n <- genInteger coloredCoinMinMint coloredCoinMaxMint
pure $ valueFromList 0 [(redCoinId, red, n)]
pure $ multiAssetFromList [(redCoinId, red, n)]

--------------------------------------------------------
-- Blue Coins --
Expand All @@ -175,11 +176,11 @@ maxBlueMint = 5
-- current coin selection algorithm does not prevent creating
-- a multi-asset that is too large.

genBlue :: CC.Crypto c => Gen (MaryValue c)
genBlue :: CC.Crypto c => Gen (MultiAsset c)
genBlue = do
as <- QC.resize maxBlueMint $ QC.listOf genSingleBlue
-- the transaction size gets too big if we mint too many assets
pure $ valueFromList 0 (map (\(asset, count) -> (blueCoinId, asset, count)) as)
pure $ multiAssetFromList (map (\(asset, count) -> (blueCoinId, asset, count)) as)
where
genSingleBlue = do
n <- genInteger coloredCoinMinMint coloredCoinMaxMint
Expand All @@ -202,11 +203,11 @@ yellowCoinId = PolicyID $ hashScript @(MaryEra c) yellowCoins
yellowNumAssets :: Int
yellowNumAssets = 5

genYellow :: CC.Crypto c => Gen (MaryValue c)
genYellow :: CC.Crypto c => Gen (MultiAsset c)
genYellow = do
xs <- QC.sublistOf [0 .. yellowNumAssets]
as <- mapM genSingleYellow xs
pure $ valueFromList 0 (map (\(asset, count) -> (yellowCoinId, asset, count)) as)
pure $ multiAssetFromList (map (\(asset, count) -> (yellowCoinId, asset, count)) as)
where
genSingleYellow x = do
y <- genInteger coloredCoinMinMint coloredCoinMaxMint
Expand Down Expand Up @@ -240,10 +241,10 @@ blueFreq = 1
yellowFreq :: Int
yellowFreq = 20

genBundle :: Int -> Gen (MaryValue c) -> Gen (MaryValue c)
genBundle :: Int -> Gen (MultiAsset c) -> Gen (MultiAsset c)
genBundle freq g = QC.frequency [(freq, g), (100 - freq, pure mempty)]

genMint :: CC.Crypto c => Gen (MaryValue c)
genMint :: CC.Crypto c => Gen (MultiAsset c)
genMint = do
r <- genBundle redFreq genRed
b <- genBundle blueFreq genBlue
Expand All @@ -266,14 +267,14 @@ addTokens ::
Proxy era ->
StrictSeq (TxOut era) -> -- This is an accumuating parameter
PParams era ->
MaryValue (Crypto era) ->
MultiAsset (Crypto era) ->
StrictSeq (TxOut era) ->
Maybe (StrictSeq (TxOut era))
addTokens proxy tooLittleLovelace pparams ts (txOut :<| os) =
let v = txOut ^. valueTxOutL
in if Val.coin v < scaledMinDeposit v (getField @"_minUTxOValue" pparams)
then addTokens proxy (txOut :<| tooLittleLovelace) pparams ts os
else Just $ tooLittleLovelace >< addValToTxOut @era ts txOut <| os
else Just $ tooLittleLovelace >< addValToTxOut @era (MaryValue 0 ts) txOut <| os
addTokens _proxy _ _ _ StrictSeq.Empty = Nothing

-- | This function is only good in the Mary Era
Expand Down
Expand Up @@ -161,7 +161,10 @@ instance Mock c => Arbitrary (MultiAsset c) where
arbitrary = MultiAsset <$> arbitrary

instance Mock c => Arbitrary (MaryValue c) where
arbitrary = valueFromListBounded @Word64 <$> arbitrary <*> arbitrary
arbitrary = MaryValue <$> (fromIntegral <$> positives) <*> (multiAssetFromListBounded <$> triples)
where
triples = arbitrary :: Gen [(PolicyID c, AssetName, Word64)]
positives = arbitrary :: Gen Word64

shrink (MaryValue ada assets) =
concat
Expand All @@ -177,21 +180,20 @@ instance Mock c => Arbitrary (MaryValue c) where
--
-- - Fix the ADA value to 0
-- - Allow both positive and negative quantities
genMintValues :: forall c. Mock c => Gen (MaryValue c)
genMintValues = valueFromListBounded @Int64 0 <$> arbitrary
genMintValues :: forall c. Mock c => Gen (MultiAsset c)
genMintValues = multiAssetFromListBounded @Int64 <$> arbitrary

-- | Variant on @valueFromList@ that makes sure that generated values stay
-- | Variant on @multiAssetFromList@ that makes sure that generated values stay
-- bounded within the range of a given integral type.
valueFromListBounded ::
multiAssetFromListBounded ::
forall i crypto.
(Bounded i, Integral i) =>
i ->
[(PolicyID crypto, AssetName, i)] ->
MaryValue crypto
valueFromListBounded (fromIntegral -> ada) =
MultiAsset crypto
multiAssetFromListBounded =
foldr
(\(p, n, fromIntegral -> i) ans -> ConcreteValue.insert comb p n i ans)
(MaryValue ada mempty)
mempty
where
comb :: Integer -> Integer -> Integer
comb a b =
Expand Down Expand Up @@ -220,7 +222,7 @@ instance Mock c => Arbitrary (MATxBody (AllegraEra c)) where
<*> arbitrary
<*> arbitrary
<*> arbitrary
<*> pure (Coin 0)
<*> pure mempty

instance Mock c => Arbitrary (Timelock c) where
arbitrary = sizedTimelock maxTimelockDepth
Expand Down
Expand Up @@ -20,7 +20,7 @@ import Cardano.Ledger.BaseTypes (StrictMaybe (SJust, SNothing))
import Cardano.Ledger.Coin (Coin (..))
import Cardano.Ledger.Core
import Cardano.Ledger.Mary (MaryEra)
import Cardano.Ledger.Mary.Value (AssetName (..), MaryValue (..), MultiAsset (..), PolicyID (..))
import Cardano.Ledger.Mary.Value (AssetName (..), MultiAsset (..), PolicyID (..))
import Cardano.Ledger.Shelley.TxBody (ShelleyEraTxBody (..), Wdrl (..))
import Cardano.Ledger.ShelleyMA.Timelocks (Timelock (..), ValidityInterval (..))
import Cardano.Ledger.ShelleyMA.TxBody (MATxBody (..), ShelleyMAEraTxBody (..))
Expand Down Expand Up @@ -65,8 +65,8 @@ txM =
SNothing
testmint

testmint :: MaryValue TestCrypto
testmint = MaryValue 0 $ MultiAsset $ Map.singleton policyId (Map.singleton aname 2)
testmint :: MultiAsset TestCrypto
testmint = MultiAsset $ Map.singleton policyId (Map.singleton aname 2)
where
policyId = PolicyID . hashScript @TestEra . RequireAnyOf $ fromList []
aname = AssetName $ fromString "asset name"
Expand Down
Expand Up @@ -80,7 +80,7 @@ bootstrapTxId = txid txb
unboundedInterval
SNothing
SNothing
(Val.inject (Coin 0))
mempty

initUTxO :: UTxO MaryTest
initUTxO =
Expand Down Expand Up @@ -111,7 +111,7 @@ makeTxb ::
[TxIn TestCrypto] ->
[ShelleyTxOut MaryTest] ->
ValidityInterval ->
MaryValue TestCrypto ->
MultiAsset TestCrypto ->
MATxBody MaryTest
makeTxb ins outs interval minted =
MATxBody
Expand Down Expand Up @@ -159,17 +159,16 @@ amethyst = AssetName "amethyst"
-- Mint Purple Tokens --
------------------------

mintSimpleEx1 :: MaryValue TestCrypto
mintSimpleEx1 :: MultiAsset TestCrypto
mintSimpleEx1 =
MaryValue 0 $
MultiAsset $
Map.singleton purplePolicyId (Map.fromList [(plum, 13), (amethyst, 2)])
MultiAsset $
Map.singleton purplePolicyId (Map.fromList [(plum, 13), (amethyst, 2)])

aliceCoinSimpleEx1 :: Coin
aliceCoinSimpleEx1 = aliceInitCoin <-> feeEx

tokensSimpleEx1 :: MaryValue TestCrypto
tokensSimpleEx1 = mintSimpleEx1 <+> Val.inject aliceCoinSimpleEx1
tokensSimpleEx1 = (MaryValue 0 mintSimpleEx1) <+> Val.inject aliceCoinSimpleEx1

-- Mint a purple token bundle, consisting of thirteen plums and two amethysts.
-- Give the bundle to Alice.
Expand Down Expand Up @@ -230,7 +229,7 @@ txbodySimpleEx2 =
ShelleyTxOut Cast.bobAddr bobTokensSimpleEx2
]
unboundedInterval
Val.zero
mempty

txSimpleEx2 :: ShelleyTx MaryTest
txSimpleEx2 =
Expand Down Expand Up @@ -286,17 +285,16 @@ tokenTimeEx = AssetName "tokenTimeEx"
-- Mint Bounded Time Range Tokens --
------------------------------------

mintTimeEx1 :: MaryValue TestCrypto
mintTimeEx1 :: MultiAsset TestCrypto
mintTimeEx1 =
MaryValue 0 $
MultiAsset $
Map.singleton boundedTimePolicyId (Map.singleton tokenTimeEx 1)
MultiAsset $
Map.singleton boundedTimePolicyId (Map.singleton tokenTimeEx 1)

aliceCoinsTimeEx1 :: Coin
aliceCoinsTimeEx1 = aliceInitCoin <-> feeEx

tokensTimeEx1 :: MaryValue TestCrypto
tokensTimeEx1 = mintTimeEx1 <+> Val.inject aliceCoinsTimeEx1
tokensTimeEx1 = (MaryValue 0 mintTimeEx1) <+> Val.inject aliceCoinsTimeEx1

-- Mint tokens
txbodyTimeEx1 :: StrictMaybe SlotNo -> StrictMaybe SlotNo -> MATxBody MaryTest
Expand Down Expand Up @@ -368,7 +366,7 @@ txbodyTimeEx2 =
ShelleyTxOut Cast.bobAddr bobTokensTimeEx2
]
unboundedInterval
Val.zero
mempty

txTimeEx2 :: ShelleyTx MaryTest
txTimeEx2 =
Expand Down Expand Up @@ -409,17 +407,16 @@ tokenSingWitEx1 = AssetName "tokenSingWitEx1"
-- Mint Alice Tokens --
-----------------------

mintSingWitEx1 :: MaryValue TestCrypto
mintSingWitEx1 :: MultiAsset TestCrypto
mintSingWitEx1 =
MaryValue 0 $
MultiAsset $
Map.singleton alicePolicyId (Map.singleton tokenSingWitEx1 17)
MultiAsset $
Map.singleton alicePolicyId (Map.singleton tokenSingWitEx1 17)

bobCoinsSingWitEx1 :: Coin
bobCoinsSingWitEx1 = bobInitCoin <-> feeEx

tokensSingWitEx1 :: MaryValue TestCrypto
tokensSingWitEx1 = mintSingWitEx1 <+> Val.inject bobCoinsSingWitEx1
tokensSingWitEx1 = (MaryValue 0 mintSingWitEx1) <+> Val.inject bobCoinsSingWitEx1

-- Bob pays the fees, but only alice can witness the minting
txbodySingWitEx1 :: MATxBody MaryTest
Expand Down Expand Up @@ -470,11 +467,10 @@ txSingWitEx1Invalid =
------------------------

-- Mint negative valued tokens
mintNegEx1 :: MaryValue TestCrypto
mintNegEx1 :: MultiAsset TestCrypto
mintNegEx1 =
MaryValue 0 $
MultiAsset $
Map.singleton purplePolicyId (Map.singleton plum (-8))
MultiAsset $
Map.singleton purplePolicyId (Map.singleton plum (-8))

aliceTokensNegEx1 :: MaryValue TestCrypto
aliceTokensNegEx1 =
Expand Down Expand Up @@ -516,11 +512,10 @@ expectedUTxONegEx1 =
-- Now attempt to produce negative outputs
--

mintNegEx2 :: MaryValue TestCrypto
mintNegEx2 :: MultiAsset TestCrypto
mintNegEx2 =
MaryValue 0 $
MultiAsset $
Map.singleton purplePolicyId (Map.singleton plum (-9))
MultiAsset $
Map.singleton purplePolicyId (Map.singleton plum (-9))

aliceTokensNegEx2 :: MaryValue TestCrypto
aliceTokensNegEx2 =
Expand Down Expand Up @@ -551,37 +546,35 @@ testNegEx2 = do
minUtxoBigEx :: Coin
minUtxoBigEx = Coin 50000

smallValue :: MaryValue TestCrypto
smallValue :: MultiAsset TestCrypto
smallValue =
MaryValue 0 $
MultiAsset $
Map.singleton purplePolicyId (Map.fromList [(plum, 13), (amethyst, 2)])
MultiAsset $
Map.singleton purplePolicyId (Map.fromList [(plum, 13), (amethyst, 2)])

smallOut :: ShelleyTxOut MaryTest
smallOut =
ShelleyTxOut Cast.aliceAddr $ smallValue <+> Val.inject (aliceInitCoin <-> (feeEx <+> minUtxoBigEx))
ShelleyTxOut Cast.aliceAddr $ (MaryValue 0 smallValue) <+> Val.inject (aliceInitCoin <-> (feeEx <+> minUtxoBigEx))

numAssets :: Int
numAssets = 1000

bigValue :: MaryValue TestCrypto
bigValue :: MultiAsset TestCrypto
bigValue =
MaryValue 0 $
MultiAsset $
Map.singleton
purplePolicyId
(Map.fromList $ map (\x -> (AssetName . fromString $ show x, 1)) [1 .. numAssets])
MultiAsset $
Map.singleton
purplePolicyId
(Map.fromList $ map (\x -> (AssetName . fromString $ show x, 1)) [1 .. numAssets])

bigOut :: ShelleyTxOut MaryTest
bigOut = ShelleyTxOut Cast.aliceAddr $ bigValue <+> Val.inject minUtxoBigEx
bigOut = ShelleyTxOut Cast.aliceAddr $ (MaryValue 0 bigValue) <+> Val.inject minUtxoBigEx

txbodyWithBigValue :: MATxBody MaryTest
txbodyWithBigValue =
makeTxb
[mkTxInPartial bootstrapTxId 0]
[smallOut, bigOut]
unboundedInterval
(bigValue <+> smallValue)
(bigValue <> smallValue)

txBigValue :: ShelleyTx MaryTest
txBigValue =
Expand Down

0 comments on commit 5d70dc2

Please sign in to comment.