Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Instead of using the UTxOIndex generators, and converting back to UTxOs, this commits adds generators for UTxO. 1. Mimic the generators in UTxOIndex.Gen 2. Make UTxOIndex.Gen use UTxO.Gen 3. Use UTxO.Gen in ServerSpec In response to the suggestion #2684 (comment)
- Loading branch information
Showing
4 changed files
with
90 additions
and
35 deletions.
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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
module Cardano.Wallet.Primitive.Types.UTxO.Gen | ||
( genUTxOSmall | ||
, genUTxOLarge | ||
, genUTxOLargeN | ||
, shrinkUTxOSmall | ||
) where | ||
|
||
import Prelude | ||
|
||
import Cardano.Wallet.Primitive.Types.Tx | ||
( TxIn, TxOut ) | ||
import Cardano.Wallet.Primitive.Types.Tx.Gen | ||
( genTxInLargeRange | ||
, genTxInSmallRange | ||
, genTxOutSmallRange | ||
, shrinkTxInSmallRange | ||
, shrinkTxOutSmallRange | ||
) | ||
import Cardano.Wallet.Primitive.Types.UTxO | ||
( UTxO (..) ) | ||
import Control.Monad | ||
( replicateM ) | ||
import Test.QuickCheck | ||
( Gen, choose, frequency, shrinkList ) | ||
import Test.QuickCheck.Extra | ||
( shrinkInterleaved ) | ||
|
||
import qualified Data.Map as Map | ||
|
||
-------------------------------------------------------------------------------- | ||
-- Small indices | ||
-------------------------------------------------------------------------------- | ||
|
||
genUTxOSmall :: Gen UTxO | ||
genUTxOSmall = do | ||
entryCount <- frequency | ||
[ (1, pure 0) | ||
, (1, pure 1) | ||
, (32, choose (2, 64)) | ||
] | ||
UTxO . Map.fromList <$> replicateM entryCount genEntrySmallRange | ||
|
||
shrinkUTxOSmall :: UTxO -> [UTxO] | ||
shrinkUTxOSmall | ||
= take 16 | ||
. fmap (UTxO . Map.fromList) | ||
. shrinkList shrinkEntrySmallRange | ||
. Map.toList | ||
. getUTxO | ||
|
||
genEntrySmallRange :: Gen (TxIn, TxOut) | ||
genEntrySmallRange = (,) | ||
<$> genTxInSmallRange | ||
<*> genTxOutSmallRange | ||
|
||
shrinkEntrySmallRange :: (TxIn, TxOut) -> [(TxIn, TxOut)] | ||
shrinkEntrySmallRange (i, o) = uncurry (,) <$> shrinkInterleaved | ||
(i, shrinkTxInSmallRange) | ||
(o, shrinkTxOutSmallRange) | ||
|
||
-------------------------------------------------------------------------------- | ||
-- Large indices | ||
-------------------------------------------------------------------------------- | ||
|
||
genUTxOLarge :: Gen UTxO | ||
genUTxOLarge = do | ||
entryCount <- choose (1024, 4096) | ||
genUTxOLargeN entryCount | ||
|
||
genUTxOLargeN :: Int -> Gen UTxO | ||
genUTxOLargeN entryCount = do | ||
UTxO . Map.fromList <$> replicateM entryCount genEntryLargeRange | ||
|
||
genEntryLargeRange :: Gen (TxIn, TxOut) | ||
genEntryLargeRange = (,) | ||
<$> genTxInLargeRange | ||
-- Note that we don't need to choose outputs from a large range, as inputs | ||
-- are already chosen from a large range: | ||
<*> genTxOutSmallRange |
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