|
| 1 | +{-# LANGUAGE RecordWildCards #-} |
| 2 | + |
| 3 | +module Spec.HydraAuctionOnchain.QuickCheck.Gen |
| 4 | + ( KeyPair (..) |
| 5 | + , genKeyPair |
| 6 | + , genTxInfoTemplate |
| 7 | + , genValidAuctionTerms |
| 8 | + , genValidBidState |
| 9 | + , genValidNewBidState |
| 10 | + ) where |
| 11 | + |
| 12 | +import Control.Monad (liftM3) |
| 13 | +import Crypto.PubKey.Ed25519 (PublicKey, SecretKey, generateSecretKey, sign, toPublic) |
| 14 | +import Crypto.Random (drgNewTest, withDRG) |
| 15 | +import Data.ByteArray (ByteArrayAccess, convert) |
| 16 | +import Data.ByteString (ByteString) |
| 17 | +import Data.Functor ((<&>)) |
| 18 | +import Plutarch.Test.QuickCheck.Instances () |
| 19 | +import PlutusLedgerApi.V1.Interval qualified as Interval (always) |
| 20 | +import PlutusLedgerApi.V1.Value qualified as Value (singleton) |
| 21 | +import PlutusLedgerApi.V2 |
| 22 | + ( BuiltinByteString |
| 23 | + , CurrencySymbol |
| 24 | + , POSIXTime (POSIXTime) |
| 25 | + , PubKeyHash |
| 26 | + , TxId |
| 27 | + , TxInfo (..) |
| 28 | + , adaSymbol |
| 29 | + , adaToken |
| 30 | + , toBuiltin |
| 31 | + ) |
| 32 | +import PlutusTx.AssocMap qualified as AMap (empty) |
| 33 | +import Spec.HydraAuctionOnchain.Helpers (hashVerificationKey, serialise) |
| 34 | +import Spec.HydraAuctionOnchain.QuickCheck.Modifiers (GenNonAdaValue (GenNonAdaValue)) |
| 35 | +import Spec.HydraAuctionOnchain.Types.AuctionTerms (AuctionTerms (..)) |
| 36 | +import Spec.HydraAuctionOnchain.Types.BidTerms (BidTerms (..)) |
| 37 | +import Spec.HydraAuctionOnchain.Types.BidderInfo (BidderInfo (..)) |
| 38 | +import Spec.HydraAuctionOnchain.Types.StandingBidState (StandingBidState (StandingBidState)) |
| 39 | +import Test.QuickCheck |
| 40 | + ( Arbitrary (arbitrary) |
| 41 | + , Gen |
| 42 | + , NonNegative (NonNegative) |
| 43 | + , Positive (Positive) |
| 44 | + , arbitraryBoundedRandom |
| 45 | + , chooseInt |
| 46 | + , chooseInteger |
| 47 | + , liftArbitrary |
| 48 | + , suchThat |
| 49 | + , vector |
| 50 | + ) |
| 51 | + |
| 52 | +data KeyPair = KeyPair {skey :: SecretKey, vkey :: PublicKey} |
| 53 | + deriving stock (Show, Eq) |
| 54 | + |
| 55 | +signUsingKeyPair :: ByteArrayAccess ba => KeyPair -> ba -> BuiltinByteString |
| 56 | +signUsingKeyPair KeyPair {skey, vkey} message = |
| 57 | + toBuiltin @ByteString $ convert $ sign skey vkey message |
| 58 | + |
| 59 | +genKeyPair :: Gen KeyPair |
| 60 | +genKeyPair = |
| 61 | + arbitraryBoundedRandom <&> \seed -> |
| 62 | + let (skey, _) = withDRG (drgNewTest seed) generateSecretKey |
| 63 | + in KeyPair skey $ toPublic skey |
| 64 | + |
| 65 | +genTxInfoTemplate :: Gen TxInfo |
| 66 | +genTxInfoTemplate = do |
| 67 | + txInfoFeeAda <- arbitrary @Integer |
| 68 | + txInfoId <- arbitrary @TxId |
| 69 | + pure $ |
| 70 | + TxInfo |
| 71 | + { txInfoInputs = mempty |
| 72 | + , txInfoReferenceInputs = mempty |
| 73 | + , txInfoOutputs = mempty |
| 74 | + , txInfoFee = Value.singleton adaSymbol adaToken txInfoFeeAda |
| 75 | + , txInfoMint = mempty |
| 76 | + , txInfoDCert = mempty |
| 77 | + , txInfoWdrl = AMap.empty |
| 78 | + , txInfoValidRange = Interval.always |
| 79 | + , txInfoSignatories = mempty |
| 80 | + , txInfoRedeemers = AMap.empty |
| 81 | + , txInfoData = AMap.empty |
| 82 | + , txInfoId |
| 83 | + } |
| 84 | + |
| 85 | +genValidBidTerms :: CurrencySymbol -> AuctionTerms -> KeyPair -> KeyPair -> Gen BidTerms |
| 86 | +genValidBidTerms auctionCs auctionTerms sellerKeys bidderKeys = do |
| 87 | + let (bi'BidderVk, bi'BidderPkh) = hashVerificationKey $ vkey bidderKeys |
| 88 | + let bt'Bidder = BidderInfo {..} |
| 89 | + bt'BidPrice <- arbitrary @Integer `suchThat` (\x -> x > at'StartingBid auctionTerms) |
| 90 | + let bt'SellerSignature = sellerSignature bi'BidderVk |
| 91 | + let bt'BidderSignature = bidderSignature bt'BidPrice bi'BidderPkh |
| 92 | + pure BidTerms {..} |
| 93 | + where |
| 94 | + sellerSignature :: BuiltinByteString -> BuiltinByteString |
| 95 | + sellerSignature bidderVkey = |
| 96 | + signUsingKeyPair sellerKeys $ |
| 97 | + (serialise auctionCs <> serialise bidderVkey) |
| 98 | + |
| 99 | + bidderSignature :: Integer -> PubKeyHash -> BuiltinByteString |
| 100 | + bidderSignature bidPrice bidderPkh = |
| 101 | + signUsingKeyPair bidderKeys $ |
| 102 | + (serialise auctionCs <> serialise bidderPkh <> serialise bidPrice) |
| 103 | + |
| 104 | +genValidBidState |
| 105 | + :: CurrencySymbol |
| 106 | + -> AuctionTerms |
| 107 | + -> KeyPair |
| 108 | + -> KeyPair |
| 109 | + -> Gen StandingBidState |
| 110 | +genValidBidState auctionCs auctionTerms sellerKeys bidderKeys = |
| 111 | + StandingBidState |
| 112 | + <$> liftArbitrary |
| 113 | + (genValidBidTerms auctionCs auctionTerms sellerKeys bidderKeys) |
| 114 | + |
| 115 | +genValidNewBidState |
| 116 | + :: StandingBidState |
| 117 | + -> CurrencySymbol |
| 118 | + -> AuctionTerms |
| 119 | + -> KeyPair |
| 120 | + -> KeyPair |
| 121 | + -> Gen StandingBidState |
| 122 | +genValidNewBidState oldBidState auctionCs auctionTerms sellerKeys bidderKeys = |
| 123 | + case oldBidState of |
| 124 | + StandingBidState Nothing -> |
| 125 | + genValidBidState auctionCs auctionTerms sellerKeys bidderKeys |
| 126 | + StandingBidState (Just oldBidTerms) -> do |
| 127 | + newBidPrice <- arbitrary @Integer `suchThat` (\x -> x >= at'MinBidIncrement auctionTerms) |
| 128 | + pure . StandingBidState . Just $ |
| 129 | + oldBidTerms |
| 130 | + { bt'BidPrice = newBidPrice |
| 131 | + } |
| 132 | + |
| 133 | +genValidAuctionTerms :: PublicKey -> Gen AuctionTerms |
| 134 | +genValidAuctionTerms vkey = do |
| 135 | + GenNonAdaValue @Positive at'AuctionLot <- arbitrary |
| 136 | + let (at'SellerVk, at'SellerPkh) = hashVerificationKey vkey |
| 137 | + at'Delegates <- vector @PubKeyHash =<< chooseInt (0, 10) |
| 138 | + |
| 139 | + let chooseInterval = POSIXTime <$> chooseInteger (1, 604_800_000) -- up to 1 week in msec |
| 140 | + (biddingPeriod, purchasePeriod, penaltyPeriod) <- |
| 141 | + liftM3 (,,) chooseInterval chooseInterval chooseInterval |
| 142 | + at'BiddingStart <- arbitrary @POSIXTime |
| 143 | + let at'BiddingEnd = at'BiddingStart + biddingPeriod |
| 144 | + let at'PurchaseDeadline = at'BiddingEnd + purchasePeriod |
| 145 | + let at'Cleanup = at'PurchaseDeadline + penaltyPeriod |
| 146 | + |
| 147 | + let minAuctionFeePerDelegate = 2_000_000 |
| 148 | + at'AuctionFeePerDelegate <- arbitrary @Integer `suchThat` (\x -> x > minAuctionFeePerDelegate) |
| 149 | + |
| 150 | + let minStartingBid = at'AuctionFeePerDelegate * fromIntegral (length at'Delegates) |
| 151 | + at'StartingBid <- arbitrary @Integer `suchThat` (\x -> x > minStartingBid) |
| 152 | + |
| 153 | + Positive at'MinBidIncrement <- arbitrary @(Positive Integer) |
| 154 | + NonNegative at'MinDepositAmount <- arbitrary @(NonNegative Integer) |
| 155 | + pure AuctionTerms {..} |
0 commit comments