Skip to content

Commit

Permalink
Add function genTxInLargeRange.
Browse files Browse the repository at this point in the history
Function `genTxInLargeRange` generates transaction inputs chose from a
large range, to minimize collisions.
  • Loading branch information
jonathanknowles committed Jan 14, 2021
1 parent 38e8bd2 commit f2fa053
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion lib/core/src/Cardano/Wallet/Primitive/Types/Tx/Gen.hs
Expand Up @@ -4,6 +4,7 @@ module Cardano.Wallet.Primitive.Types.Tx.Gen
( genTxHashSmallRange
, genTxIndexSmallRange
, genTxInSmallRange
, genTxInLargeRange
, genTxOutSmallRange
, shrinkTxHashSmallRange
, shrinkTxIndexSmallRange
Expand All @@ -22,17 +23,20 @@ import Cardano.Wallet.Primitive.Types.TokenBundle.Gen
( genTokenBundleSmallRange, shrinkTokenBundleSmallRange )
import Cardano.Wallet.Primitive.Types.Tx
( TxIn (..), TxOut (..) )
import Control.Monad
( replicateM )
import Data.Either
( fromRight )
import Data.Text.Class
( FromText (..) )
import Data.Word
( Word32 )
import Test.QuickCheck
( Gen, elements )
( Gen, arbitrary, elements )
import Test.QuickCheck.Extra
( shrinkInterleaved )

import qualified Data.ByteString.Char8 as B8
import qualified Data.Text as T

--------------------------------------------------------------------------------
Expand All @@ -48,6 +52,13 @@ shrinkTxHashSmallRange hash = filter (< hash) txHashes
txHashes :: [Hash "Tx"]
txHashes = mkTxHash <$> ['0' .. '7']

--------------------------------------------------------------------------------
-- Transaction hashes chosen from a large range (to minimize collisions)
--------------------------------------------------------------------------------

genTxHashLargeRange :: Gen (Hash "Tx")
genTxHashLargeRange = Hash . B8.pack <$> replicateM 32 arbitrary

--------------------------------------------------------------------------------
-- Transaction indices chosen from a small range (to allow collisions)
--------------------------------------------------------------------------------
Expand Down Expand Up @@ -75,6 +86,17 @@ shrinkTxInSmallRange (TxIn h i) = uncurry TxIn <$> shrinkInterleaved
(h, shrinkTxHashSmallRange)
(i, shrinkTxIndexSmallRange)

--------------------------------------------------------------------------------
-- Transaction inputs chosen from a large range (to minimize collisions)
--------------------------------------------------------------------------------

genTxInLargeRange :: Gen TxIn
genTxInLargeRange = TxIn
<$> genTxHashLargeRange
-- Note that we don't need to choose indices from a large range, as hashes
-- are already chosen from a large range:
<*> genTxIndexSmallRange

--------------------------------------------------------------------------------
-- Transaction outputs chosen from a small range (to allow collisions)
--------------------------------------------------------------------------------
Expand Down

0 comments on commit f2fa053

Please sign in to comment.