-
Notifications
You must be signed in to change notification settings - Fork 158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mary minimal property tests #2088
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,20 +12,30 @@ | |
module Test.Cardano.Ledger.Mary () where -- export the EraGen instance for MaryEra | ||
|
||
import Cardano.Ledger.AuxiliaryData (AuxiliaryDataHash) | ||
import qualified Cardano.Ledger.Core as Core (AuxiliaryData, Value) | ||
import qualified Cardano.Ledger.Crypto as CryptoClass | ||
import Cardano.Ledger.Era (Crypto) | ||
import Cardano.Ledger.Mary.Value (Value (..)) | ||
import Cardano.Ledger.Mary.Value | ||
( AssetName (..), | ||
PolicyID (..), | ||
Value (..), | ||
policies, | ||
) | ||
import Cardano.Ledger.Shelley.Constraints (UsesAuxiliary, UsesValue) | ||
import Cardano.Ledger.ShelleyMA.Timelocks (Timelock (..)) | ||
import Cardano.Ledger.ShelleyMA.TxBody (StrictMaybe, TxBody (..)) | ||
import qualified Cardano.Ledger.Val as Val | ||
import Cardano.Slotting.Slot (SlotNo) | ||
import qualified Data.ByteString.Char8 as BS | ||
import Data.Map (Map) | ||
import qualified Data.Map as Map | ||
import Data.Sequence.Strict (StrictSeq) | ||
import Data.Sequence.Strict (StrictSeq (..), (<|)) | ||
import qualified Data.Sequence.Strict as StrictSeq | ||
import qualified Data.Set as Set | ||
import Shelley.Spec.Ledger.BaseTypes (StrictMaybe (..)) | ||
import Shelley.Spec.Ledger.Coin (Coin (..)) | ||
import Shelley.Spec.Ledger.PParams (Update) | ||
import Shelley.Spec.Ledger.Tx (TxIn, TxOut) | ||
import Shelley.Spec.Ledger.Tx (TxIn, TxOut (..), hashScript) | ||
import Shelley.Spec.Ledger.TxBody (DCert, Wdrl) | ||
import Test.Cardano.Ledger.Allegra | ||
( genValidityInterval, | ||
|
@@ -34,10 +44,11 @@ import Test.Cardano.Ledger.Allegra | |
unQuantifyTL, | ||
) | ||
import Test.Cardano.Ledger.EraBuffet (MaryEra) | ||
import Test.QuickCheck (Gen) | ||
import Test.QuickCheck (Gen, arbitrary, frequency) | ||
import qualified Test.QuickCheck as QC | ||
import Test.Shelley.Spec.Ledger.ConcreteCryptoTypes (Mock) | ||
import Test.Shelley.Spec.Ledger.Generator.Constants (Constants (..)) | ||
import Test.Shelley.Spec.Ledger.Generator.Core (GenEnv (..)) | ||
import Test.Shelley.Spec.Ledger.Generator.Core (GenEnv (..), genInteger) | ||
import Test.Shelley.Spec.Ledger.Generator.EraGen (EraGen (..)) | ||
import Test.Shelley.Spec.Ledger.Generator.ScriptClass | ||
( ScriptClass (..), | ||
|
@@ -67,13 +78,167 @@ instance (CryptoClass.Crypto c, Mock c) => EraGen (MaryEra c) where | |
genGenesisValue (GenEnv _ Constants {minGenesisOutputVal, maxGenesisOutputVal}) = | ||
Val.inject . Coin <$> exponential minGenesisOutputVal maxGenesisOutputVal | ||
genEraTxBody _ge = genTxBody | ||
genEraAuxiliaryData = error "TODO @uroboros - implement genAuxiliaryData for Mary" | ||
updateEraTxBody (TxBody _in _out cert wdrl _txfee vi upd meta forge) fee ins outs = | ||
TxBody ins outs cert wdrl fee vi upd meta forge | ||
genEraAuxiliaryData = genAuxiliaryData | ||
updateEraTxBody (TxBody _in _out cert wdrl _txfee vi upd meta mint) fee ins outs = | ||
TxBody ins outs cert wdrl fee vi upd meta mint | ||
|
||
genAuxiliaryData :: | ||
Mock crypto => | ||
Constants -> | ||
Gen (StrictMaybe (Core.AuxiliaryData (MaryEra crypto))) | ||
genAuxiliaryData Constants {frequencyTxWithMetadata} = | ||
frequency | ||
[ (frequencyTxWithMetadata, SJust <$> arbitrary), | ||
(100 - frequencyTxWithMetadata, pure SNothing) | ||
] | ||
|
||
-------------------------------------------------------- | ||
-- Permissionless Tokens -- | ||
-- -- | ||
-- We introduce three token bundles, each which has a -- | ||
-- permissionless minting policy and each which has a -- | ||
-- different minting behavior (use of asset names). -- | ||
-------------------------------------------------------- | ||
|
||
-- | An infinite indexed collection of trivial policies. | ||
-- They are trivial in the sense that they require no | ||
-- signature and can be submitted at any time. | ||
trivialPolicy :: CryptoClass.Crypto c => Int -> Timelock c | ||
trivialPolicy i | i == 0 = RequireAllOf (StrictSeq.fromList []) | ||
trivialPolicy i | otherwise = RequireAllOf (StrictSeq.fromList [trivialPolicy (i -1)]) | ||
|
||
-------------------------------------------------------- | ||
-- Red Coins -- | ||
-- -- | ||
-- These tokens are always minted with the same asset -- | ||
-- name, "red". -- | ||
-------------------------------------------------------- | ||
|
||
redCoins :: CryptoClass.Crypto c => Timelock c | ||
redCoins = trivialPolicy 0 | ||
|
||
redCoinId :: forall c. CryptoClass.Crypto c => PolicyID c | ||
redCoinId = PolicyID $ hashScript @(MaryEra c) redCoins | ||
|
||
red :: AssetName | ||
red = AssetName $ BS.pack "redCoin" | ||
|
||
genRed :: CryptoClass.Crypto c => Gen (Value c) | ||
genRed = do | ||
n <- genInteger 1 1000000 | ||
pure $ Value 0 (Map.singleton redCoinId (Map.singleton red n)) | ||
|
||
-------------------------------------------------------- | ||
-- Blue Coins -- | ||
-- -- | ||
-- These tokens are (nearly) always minted with a new -- | ||
-- asset name. | ||
-------------------------------------------------------- | ||
|
||
blueCoins :: CryptoClass.Crypto c => Timelock c | ||
blueCoins = trivialPolicy 1 | ||
|
||
blueCoinId :: forall c. CryptoClass.Crypto c => PolicyID c | ||
blueCoinId = PolicyID $ hashScript @(MaryEra c) blueCoins | ||
|
||
maxBlueMint :: Int | ||
maxBlueMint = 10 | ||
|
||
genBlue :: CryptoClass.Crypto c => Gen (Value c) | ||
genBlue = do | ||
as <- QC.resize maxBlueMint $ QC.listOf genSingleBlue | ||
-- the transaction size gets too big if we mint too many assets | ||
pure $ Value 0 (Map.singleton blueCoinId (Map.fromList as)) | ||
where | ||
genSingleBlue = do | ||
n <- genInteger 1 1000000 | ||
a <- arbitrary | ||
pure $ (AssetName a, n) | ||
|
||
-------------------------------------------------------- | ||
-- Yellow Coins -- | ||
-- -- | ||
-- These tokens are minted with a small variety of -- | ||
-- asset names. -- | ||
-------------------------------------------------------- | ||
|
||
yellowCoins :: CryptoClass.Crypto c => Timelock c | ||
yellowCoins = trivialPolicy 2 | ||
|
||
yellowCoinId :: forall c. CryptoClass.Crypto c => PolicyID c | ||
yellowCoinId = PolicyID $ hashScript @(MaryEra c) yellowCoins | ||
|
||
yellowNumAssets :: Int | ||
yellowNumAssets = 5 | ||
|
||
genYellow :: CryptoClass.Crypto c => Gen (Value c) | ||
genYellow = do | ||
xs <- QC.sublistOf [0 .. yellowNumAssets] | ||
as <- mapM genSingleYellow xs | ||
pure $ Value 0 (Map.singleton yellowCoinId (Map.fromList as)) | ||
where | ||
genSingleYellow x = do | ||
y <- genInteger 1 1000000 | ||
let an = AssetName . BS.pack $ "yellow" <> show x | ||
pure $ (an, y) | ||
|
||
-- | This map allows us to lookup a minting policy by the policy ID. | ||
policyIndex :: CryptoClass.Crypto c => Map (PolicyID c) (Timelock c) | ||
policyIndex = | ||
Map.fromList | ||
[ (redCoinId, redCoins), | ||
(blueCoinId, blueCoins), | ||
(yellowCoinId, yellowCoins) | ||
] | ||
|
||
-------------------------------------------------------- | ||
-- Minting Frequencies -- | ||
-- -- | ||
-- The frequencies represent a percent chance of any -- | ||
-- given transaction to mint one of the three token -- | ||
-- bundles. -- | ||
-------------------------------------------------------- | ||
|
||
redFreq :: Int | ||
redFreq = 30 | ||
|
||
blueFreq :: Int | ||
blueFreq = 5 | ||
|
||
yellowFreq :: Int | ||
yellowFreq = 50 | ||
|
||
genBundle :: Int -> Gen (Value c) -> Gen (Value c) | ||
genBundle freq g = QC.frequency [(freq, g), (100 - freq, pure mempty)] | ||
|
||
genMint :: CryptoClass.Crypto c => Gen (Value c) | ||
genMint = do | ||
r <- genBundle redFreq genRed | ||
b <- genBundle blueFreq genBlue | ||
y <- genBundle yellowFreq genYellow | ||
pure $ r <> b <> y | ||
|
||
------------------------------- | ||
-- END Permissionless Tokens -- | ||
------------------------------- | ||
|
||
-- | Add tokens to a non-empty list of transaction outputs. | ||
-- NOTE: this function will raise an error if given an empty sequence. | ||
addTokensToFirstOutput :: | ||
( Core.Value era ~ Value (Crypto era), | ||
EraGen era | ||
) => | ||
Value (Crypto era) -> | ||
StrictSeq (TxOut era) -> | ||
StrictSeq (TxOut era) | ||
addTokensToFirstOutput ts ((TxOut a v) :<| os) = TxOut a (v <> ts) <| os | ||
addTokensToFirstOutput _ StrictSeq.Empty = | ||
error "addTokensToFirstOutput was given an empty sequence" | ||
|
||
genTxBody :: | ||
forall era. | ||
( UsesValue era, | ||
Core.Value era ~ Value (Crypto era), | ||
UsesAuxiliary era, | ||
EraGen era | ||
) => | ||
|
@@ -85,21 +250,25 @@ genTxBody :: | |
Coin -> | ||
StrictMaybe (Update era) -> | ||
StrictMaybe (AuxiliaryDataHash (Crypto era)) -> | ||
Gen (TxBody era) | ||
Gen (TxBody era, [Timelock (Crypto era)]) | ||
genTxBody slot ins outs cert wdrl fee upd meta = do | ||
validityInterval <- genValidityInterval slot | ||
let mint = error "TODO @uroboros mint some Mary era tokens" | ||
mint <- genMint | ||
let outs' = addTokensToFirstOutput (mint) outs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (mint) |
||
ps = map (\p -> (Map.!) policyIndex p) (Set.toList $ policies mint) | ||
pure $ | ||
TxBody | ||
ins | ||
outs | ||
cert | ||
wdrl | ||
fee | ||
validityInterval | ||
upd | ||
meta | ||
mint | ||
( TxBody | ||
ins | ||
outs' | ||
cert | ||
wdrl | ||
fee | ||
validityInterval | ||
upd | ||
meta | ||
mint, | ||
ps -- These additional scripts are for the minting policies. | ||
) | ||
|
||
instance Split (Value era) where | ||
vsplit (Value n _) 0 = ([], Coin n) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can wrap
genMint
with agenMintWithScripts
that produces the policy scripts present in it's generated token bundles