-
Notifications
You must be signed in to change notification settings - Fork 211
/
FeeSpec.hs
760 lines (695 loc) · 23 KB
/
FeeSpec.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Cardano.Wallet.Primitive.FeeSpec
( spec
) where
import Prelude
import Cardano.Wallet.Primitive.CoinSelection
( CoinSelection (..), changeBalance, inputBalance, outputBalance )
import Cardano.Wallet.Primitive.CoinSelection.LargestFirst
( largestFirst )
import Cardano.Wallet.Primitive.Fee
( ErrAdjustForFee (..)
, Fee (..)
, FeeOptions (..)
, OnDanglingChange (..)
, adjustForFee
, divvyFee
, rebalanceSelection
)
import Cardano.Wallet.Primitive.Types
( Address (..)
, Coin (..)
, Hash (..)
, ShowFmt (..)
, TxIn (..)
, TxOut (..)
, UTxO (..)
)
import Control.Arrow
( left )
import Control.Monad.IO.Class
( liftIO )
import Control.Monad.Trans.Except
( runExceptT )
import Crypto.Random
( SystemDRG, getSystemDRG )
import Crypto.Random.Types
( withDRG )
import Data.Either
( isRight )
import Data.Function
( (&) )
import Data.Functor.Identity
( Identity (runIdentity) )
import Data.List.NonEmpty
( NonEmpty )
import Data.Maybe
( isNothing )
import Data.Word
( Word64 )
import Fmt
( Buildable (..), nameF, pretty, tupleF )
import Test.Hspec
( Spec, SpecWith, before, describe, it, shouldBe, shouldSatisfy )
import Test.QuickCheck
( Arbitrary (..)
, Gen
, NonEmptyList (..)
, Property
, checkCoverage
, choose
, classify
, conjoin
, counterexample
, coverTable
, disjoin
, elements
, expectFailure
, forAllBlind
, frequency
, generate
, oneof
, property
, scale
, tabulate
, vector
, withMaxSuccess
, (===)
, (==>)
)
import Test.QuickCheck.Monadic
( monadicIO )
import qualified Cardano.Wallet.Primitive.CoinSelection as CS
import qualified Data.ByteString as BS
import qualified Data.List.NonEmpty as NE
import qualified Data.Map.Strict as Map
spec :: Spec
spec = do
describe "Fee calculation : unit tests" $ do
-- Change covers fee exactly, single change output
feeUnitTest (FeeFixture
{ fInps = [20]
, fOuts = [17]
, fChngs = [3]
, fUtxo = []
, fFee = 3
, fDust = 0
}) (Right $ FeeOutput
{ csInps = [20]
, csOuts = [17]
, csChngs = []
})
-- Total change covers fee, multiple change outputs
feeUnitTest (FeeFixture
{ fInps = [20,20]
, fOuts = [16,18]
, fChngs = [4,2]
, fUtxo = []
, fFee = 6
, fDust = 0
}) (Right $ FeeOutput
{ csInps = [20,20]
, csOuts = [16,18]
, csChngs = []
})
-- Fee split evenly across change outputs
feeUnitTest (FeeFixture
{ fInps = [20,20]
, fOuts = [18,18]
, fChngs = [2,2]
, fUtxo = []
, fFee = 2
, fDust = 0
}) (Right $ FeeOutput
{ csInps = [20,20]
, csOuts = [18,18]
, csChngs = [1,1]
})
-- Fee split evenly across change outputs
feeUnitTest (FeeFixture
{ fInps = [20,20]
, fOuts = [17,18]
, fChngs = [3,2]
, fUtxo = []
, fFee = 2
, fDust = 0
}) (Right $ FeeOutput
{ csInps = [20,20]
, csOuts = [17,18]
, csChngs = [1,2]
})
-- Fee divvied, dust removed (dust = 0)
feeUnitTest (FeeFixture
{ fInps = [20,20,20]
, fOuts = [14,18,19]
, fChngs = [6,2,1]
, fUtxo = []
, fFee = 3
, fDust = 0
}) (Right $ FeeOutput
{ csInps = [20,20,20]
, csOuts = [14,18,19]
, csChngs = [4,1,1]
})
-- Fee divvied, dust removed (dust = 1)
feeUnitTest (FeeFixture
{ fInps = [20,20,20]
, fOuts = [14,18,19]
, fChngs = [6,2,1]
, fUtxo = []
, fFee = 3
, fDust = 1
}) (Right $ FeeOutput
{ csInps = [20,20,20]
, csOuts = [14,18,19]
, csChngs = [4,1,1]
})
-- Cannot cover fee, no extra inputs
feeUnitTest (FeeFixture
{ fInps = [20]
, fOuts = [17]
, fChngs = [3]
, fUtxo = []
, fFee = 4
, fDust = 0
}) (Left $ ErrCannotCoverFee 1)
-- Cannot cover fee even with an extra (too small) inputs
feeUnitTest (FeeFixture
{ fInps = [10]
, fOuts = [7]
, fChngs = [3]
, fUtxo = [1]
, fFee = 5
, fDust = 0
}) (Left $ ErrCannotCoverFee 1)
-- Can select extra inputs to exactly cover fee, no change back
feeUnitTest (FeeFixture
{ fInps = [10]
, fOuts = [7]
, fChngs = [3]
, fUtxo = [1,1]
, fFee = 5
, fDust = 0
}) (Right $ FeeOutput
{ csInps = [10,1,1]
, csOuts = [7]
, csChngs = []
})
-- Can select extra inputs to cover for fee, and leave a change back
feeUnitTest (FeeFixture
{ fInps = [10]
, fOuts = [7]
, fChngs = [3]
, fUtxo = [3]
, fFee = 5
, fDust = 0
}) (Right $ FeeOutput
{ csInps = [10,3]
, csOuts = [7]
, csChngs = [1]
})
-- Multiple change output, can select extra inputs to cover fee, no change
feeUnitTest (FeeFixture
{ fInps = [10,10]
, fOuts = [7,7]
, fChngs = [3,3]
, fUtxo = [2,2]
, fFee = 10
, fDust = 0
}) (Right $ FeeOutput
{ csInps = [10,10,2,2]
, csOuts = [7,7]
, csChngs = []
})
-- Multiple outputs, extra inputs selected, resulting change
feeUnitTest (FeeFixture
{ fInps = [10,10]
, fOuts = [7,7]
, fChngs = [3,3]
, fUtxo = [3,3]
, fFee = 10
, fDust = 0
}) (Right $ FeeOutput
{ csInps = [10,10,3,3]
, csOuts = [7,7]
, csChngs = [1,1]
})
-- Multiple change outputs, some bigger than actual Dust
feeUnitTest (FeeFixture
{ fInps = [20,20]
, fOuts = [16,18]
, fChngs = [4,2]
, fUtxo = []
, fFee = 6
, fDust = 2
}) (Right $ FeeOutput
{ csInps = [20,20]
, csOuts = [16,18]
, csChngs = []
})
-- Change created when there was no change before
feeUnitTest (FeeFixture
{ fInps = [1]
, fOuts = [1]
, fChngs = []
, fUtxo = [2]
, fFee = 1
, fDust = 0
}) (Right $ FeeOutput
{ csInps = [1,2]
, csOuts = [1]
, csChngs = [1]
})
let c = getCoin maxBound
-- New BIG inputs selected causes change to overflow
feeUnitTest (FeeFixture
{ fInps = [c-1, c-1]
, fOuts = [c-1]
, fChngs = [c-1]
, fUtxo = [c]
, fFee = c
, fDust = 0
}) (Right $ FeeOutput
{ csInps = [c-1, c-1, c]
, csOuts = [c-1]
, csChngs = [c `div` 2 - 1, c `div` 2]
})
feeUnitTest (FeeFixture
{ fInps = []
, fOuts = []
, fChngs = []
, fUtxo = [3]
, fFee = 3
, fDust = 0
}) (Right $ FeeOutput
{ csInps = [3]
, csOuts = []
, csChngs = []
})
feeUnitTest (FeeFixture
{ fInps = []
, fOuts = []
, fChngs = []
, fUtxo = [2,2]
, fFee = 3
, fDust = 0
}) (Right $ FeeOutput
{ csInps = [2,2]
, csOuts = []
, csChngs = [1]
})
feeUnitTest (FeeFixture
{ fInps = []
, fOuts = []
, fChngs = []
, fUtxo = [2,2]
, fFee = 3
, fDust = 1
}) (Right $ FeeOutput
{ csInps = [2,2]
, csOuts = []
, csChngs = [1]
})
describe "Fee Calculation: Generators" $ do
it "Arbitrary CoinSelection" $ property $ \(ShowFmt cs) ->
property $ isValidSelection cs
before getSystemDRG $ describe "Fee Adjustment properties" $ do
it "Fee adjustment is deterministic when there's no extra inputs"
(\_ -> property propDeterministic)
it "Adjusting for fee (/= 0) reduces the change outputs or increase inputs"
(property . propReducedChanges)
describe "divvyFee" $ do
it "Σ fst (divvyFee fee outs) == fee"
(checkCoverage propDivvyFeeSame)
it "snd (divvyFee fee outs) == outs"
(checkCoverage propDivvyFeeOuts)
it "expectFailure: not (any null (fst <$> divvyFee fee outs))"
(expectFailure propDivvyFeeNoNullFee)
it "expectFailure: empty list"
(expectFailure propDivvyFeeInvariantEmptyList)
describe "prop_rebalanceSelection" $ do
it "The fee balancing algorithm converges for any coin selection."
$ property
$ withMaxSuccess 10000
$ forAllBlind genSelection prop_rebalanceSelection
{-------------------------------------------------------------------------------
Fee Adjustment - Properties
-------------------------------------------------------------------------------}
-- Check whether a selection is valid
isValidSelection :: CoinSelection -> Bool
isValidSelection (CoinSelection i o c r) =
let
oAmt = sum $ map (fromIntegral . getCoin . coin) o
cAmt = sum $ map (fromIntegral . getCoin) c
iAmt = sum $ map (fromIntegral . getCoin . coin . snd) i
rAmt = maybe (0 :: Integer) (fromIntegral . getCoin) r
in
iAmt + rAmt >= oAmt + cAmt
-- | Data for running fee calculation properties
data FeeProp = FeeProp
{ selection :: CoinSelection
-- ^ inputs from wich largestFirst can be calculated
, availableUtxo :: UTxO
-- ^ additional UTxO from which fee calculation will pick needed coins
, feeDust :: (Word64, Word64)
-- ^ constant fee and dust threshold
} deriving Show
instance Buildable FeeProp where
build (FeeProp cc utxo opt) = mempty
<> nameF "selection" (build cc)
<> build utxo
<> nameF "options" (tupleF opt)
propDeterministic
:: ShowFmt FeeProp
-> Property
propDeterministic (ShowFmt (FeeProp coinSel _ (fee, dust))) = monadicIO $ liftIO $ do
let feeOpt = feeOptions fee dust
let utxo = mempty
resultOne <- runExceptT $ adjustForFee feeOpt utxo coinSel
resultTwo <- runExceptT $ adjustForFee feeOpt utxo coinSel
resultOne `shouldBe` resultTwo
propReducedChanges
:: SystemDRG
-> ShowFmt FeeProp
-> Property
propReducedChanges drg (ShowFmt (FeeProp coinSel utxo (fee, dust))) = do
withMaxSuccess 1000 $ isRight coinSel' ==> let Right s = coinSel' in prop s
where
prop s = do
let chgs' = sum $ map getCoin $ change s
let chgs = sum $ map getCoin $ change coinSel
let inps' = CS.inputs s
let inps = CS.inputs coinSel
classify (reserve coinSel > Just (Coin fee)) "reserve > fee" $ disjoin
[ chgs' `shouldSatisfy` (< chgs)
, length inps' `shouldSatisfy` (>= length inps)
]
feeOpt = feeOptions fee dust
coinSel' = left show $ fst $ withDRG drg $ runExceptT $
adjustForFee feeOpt utxo coinSel
{-------------------------------------------------------------------------------
divvyFee - Properties
-------------------------------------------------------------------------------}
-- | Helper to re-apply the pre-conditions for divvyFee
propDivvyFee
:: ((Fee, [Coin]) -> Property)
-> (Fee, NonEmptyList Coin)
-> Property
propDivvyFee prop (fee, NonEmpty outs) =
coverTable "properties"
[ ("fee > 0", 50)
, ("nOuts=1", 1)
, ("nOuts=2", 1)
, ("nOuts=2+", 10)
]
$ tabulate "properties"
[ if fee > Fee 0 then "fee > 0" else "fee == 0"
, "nOuts=" <> case length outs of
n | n <= 2 -> show n
_ -> "2+"
]
$ prop (fee, outs)
-- | Sum of the fees divvied over each output is the same as the initial total
-- fee.
propDivvyFeeSame
:: (Fee, NonEmptyList Coin)
-> Property
propDivvyFeeSame = propDivvyFee $ \(fee, outs) ->
sum (getFee . fst <$> divvyFee fee outs) === getFee fee
-- | divvyFee doesn't change any of the outputs
propDivvyFeeOuts
:: (Fee, NonEmptyList Coin)
-> Property
propDivvyFeeOuts = propDivvyFee $ \(fee, outs) ->
(snd <$> divvyFee fee outs) === outs
-- | divvyFee never generates null fees for a given output.
--
-- This is NOT a property. It is here to illustrate that this can happen in
-- practice, and is known as a possible outcome for the divvyFee function
-- (it is fine for one of the output to be assigned no fee). The only reason
-- this would happen is because there would be less outputs than the fee amount
-- which is probably never going to happen in practice...
propDivvyFeeNoNullFee
:: (Fee, [Coin])
-> Property
propDivvyFeeNoNullFee (fee, outs) =
not (null outs) ==> withMaxSuccess 100000 prop
where
prop = property $ Fee 0 `notElem` (fst <$> divvyFee fee outs)
-- | Illustrate the invariant: 'outs' should be an non-empty list
propDivvyFeeInvariantEmptyList
:: (Fee, [Coin])
-> Property
propDivvyFeeInvariantEmptyList (fee, outs) =
withMaxSuccess 100000 prop
where
prop = divvyFee fee outs `seq` True
{-------------------------------------------------------------------------------
Fee Adjustment - properties
-------------------------------------------------------------------------------}
prop_rebalanceSelection
:: CoinSelection
-> OnDanglingChange
-> Property
prop_rebalanceSelection sel onDangling = do
let (sel', fee') = rebalanceSelection opts sel
let selectionIsBalanced = case onDangling of
PayAndBalance ->
fee' /= Fee 0 || Fee (delta sel') == estimateFee opts sel'
SaveMoney ->
fee' /= Fee 0 || Fee (delta sel') >= estimateFee opts sel'
let reserveIsEmpty =
case reserve sel of
Nothing -> isNothing (reserve sel')
Just{} -> reserve sel' == Just (Coin 0)
conjoin
[ property selectionIsBalanced
, property reserveIsEmpty
]
& counterexample (unlines
[ "selection (before):", pretty sel
, "selection (after):", pretty sel'
, "delta (before): " <> show (delta sel)
, "delta (after): " <> show (delta sel')
, "remaining fee: " <> show (getFee fee')
])
& classify (reserveNonNull && feeLargerThanDelta)
"reserve > 0 && fee > delta"
& classify (reserveLargerThanFee && feeLargerThanDelta)
"reserve > fee && fee > delta"
& classify reserveLargerThanFee
"reserve > fee"
& classify feeLargerThanDelta
"fee > delta"
where
delta s = inputBalance s - (outputBalance s + changeBalance s)
opts = FeeOptions
-- NOTE
-- Dummy fee policy but, following a similar rule as the fee policy on
-- Byron / Shelley (bigger transaction cost more) with sensible values.
{ estimateFee = \cs ->
let
size = fromIntegral $ length $ show cs
in
Fee (100000 + 100 * size)
, dustThreshold = minBound
, onDanglingChange = onDangling
}
reserveNonNull =
reserve sel > Just (Coin 0)
reserveLargerThanFee =
reserve sel > Just (Coin $ getFee $ estimateFee opts sel)
feeLargerThanDelta =
getFee (estimateFee opts sel) > delta sel
{-------------------------------------------------------------------------------
Fee Adjustment - Unit Tests
-------------------------------------------------------------------------------}
feeOptions
:: Word64
-> Word64
-> FeeOptions
feeOptions fee dust = FeeOptions
{ estimateFee =
\_ -> Fee fee
, dustThreshold =
Coin dust
, onDanglingChange =
PayAndBalance
}
feeUnitTest
:: FeeFixture
-> Either ErrAdjustForFee FeeOutput
-> SpecWith ()
feeUnitTest (FeeFixture inpsF outsF chngsF utxoF feeF dustF) expected = it title $ do
(utxo, sel) <- setup
result <- runExceptT $ do
(CoinSelection inps outs chngs _rsv) <-
adjustForFee (feeOptions feeF dustF) utxo sel
return $ FeeOutput
{ csInps = map (getCoin . coin . snd) inps
, csOuts = map (getCoin . coin) outs
, csChngs = map getCoin chngs
}
result `shouldBe` expected
where
setup :: IO (UTxO, CoinSelection)
setup = do
utxo <- generate (genUTxO $ Coin <$> utxoF)
inps <- (Map.toList . getUTxO) <$> generate (genUTxO $ Coin <$> inpsF)
outs <- generate (genTxOut $ Coin <$> outsF)
let chngs = map Coin chngsF
pure (utxo, CoinSelection inps outs chngs Nothing)
title :: String
title = mempty
<> "CoinSelection (inps=" <> show inpsF
<> "outs=" <> show outsF
<> "chngs=" <> show chngsF
<> "), UTxO=" <> show utxoF
<> "), fee=" <> show feeF
<> " --> " <> show expected
-- | A fixture for testing the fee calculation
data FeeFixture = FeeFixture
{ fInps :: [Word64]
-- ^ Value (in Lovelace) & number of coins in inputs
, fOuts :: [Word64]
-- ^ Value (in Lovelace) & number of requested outputs
, fChngs :: [Word64]
-- ^ Value (in Lovelace) & number of changes
, fUtxo :: [Word64]
-- ^ Value (in Lovelace) & number of available coins in the UTxO
, fFee :: Word64
-- ^ Value (in Lovelace) of rigid fee
, fDust :: Word64
-- ^ Value (in Lovelace) of dust
} deriving Show
-- | A fee calculation output
data FeeOutput = FeeOutput
{ csInps :: [Word64]
-- ^ Value (in Lovelace) & number of available coins in the UTxO
, csOuts :: [Word64]
-- ^ Value (in Lovelace) & number of requested outputs
, csChngs :: [Word64]
-- ^ Value (in Lovelace) & number of changes
} deriving (Show, Eq)
{-------------------------------------------------------------------------------
Arbitrary Instances
-------------------------------------------------------------------------------}
deriving newtype instance Arbitrary a => Arbitrary (ShowFmt a)
genUTxO :: [Coin] -> Gen UTxO
genUTxO coins = do
let n = length coins
inps <- vector n
outs <- genTxOut coins
return $ UTxO $ Map.fromList $ zip inps outs
genTxOut :: [Coin] -> Gen [TxOut]
genTxOut coins = do
let n = length coins
outs <- vector n
return $ zipWith TxOut outs coins
genSelection :: Gen CoinSelection
genSelection = do
outs <- choose (1, 10) >>= vector >>= genTxOut
genSelectionFor (NE.fromList outs)
genSelectionFor :: NonEmpty TxOut -> Gen CoinSelection
genSelectionFor outs = do
let opts = CS.CoinSelectionOptions (const 100) (const $ pure ())
utxo <- vector (NE.length outs * 3) >>= genUTxO
rsv <- frequency
[ (3, pure Nothing)
, (1, Just . Coin <$> oneof
[ choose (1, 10000)
, choose (500000, 1000000)
]
)
]
case runIdentity $ runExceptT $ largestFirst opts outs utxo of
Left _ -> genSelectionFor outs
Right (s,_) -> pure $ s { reserve = rsv }
instance Arbitrary TxIn where
shrink _ = []
arbitrary = TxIn
<$> arbitrary
<*> scale (`mod` 3) arbitrary -- No need for a high indexes
instance Arbitrary Coin where
shrink (Coin c) = Coin <$> filter (> 0) (shrink $ fromIntegral c)
arbitrary = Coin <$> choose (1, 200000)
instance Arbitrary Fee where
shrink (Fee c) = Fee <$> filter (> 0) (shrink $ fromIntegral c)
arbitrary = Fee . getCoin <$> arbitrary
instance Arbitrary FeeProp where
shrink (FeeProp cs utxo opts) =
case Map.toList $ getUTxO utxo of
[] ->
map (\cs' -> FeeProp cs' utxo opts) (shrink cs)
us ->
concatMap (\cs' ->
[ FeeProp cs' mempty opts
, FeeProp cs' (UTxO $ Map.fromList (drop 1 us)) opts
]
) (shrink cs)
arbitrary = do
cs <- arbitrary
utxo <- choose (0, 50)
>>= vector
>>= genUTxO
fee <- choose (100000, 500000)
dust <- choose (0, 10000)
return $ FeeProp cs utxo (fee, dust)
instance Arbitrary (Hash "Tx") where
shrink _ = []
arbitrary = do
bytes <- BS.pack <$> vector 32
pure $ Hash bytes
instance Arbitrary Address where
shrink _ = []
arbitrary = elements
[ Address "addr-0"
, Address "addr-1"
, Address "addr-2"
, Address "addr-3"
]
instance Arbitrary CoinSelection where
shrink sel@(CoinSelection inps outs chgs rsv) = case (inps, outs, chgs) of
([_], [_], []) ->
[]
_ ->
let
inps' = if length inps > 1 then drop 1 inps else inps
outs' = if length outs > 1 then drop 1 outs else outs
chgs' = drop 1 chgs
in
filter (\s -> s /= sel && isValidSelection s)
[ CoinSelection inps' outs' chgs' rsv
, CoinSelection inps' outs chgs rsv
, CoinSelection inps outs' chgs rsv
, CoinSelection inps outs chgs' rsv
]
arbitrary = do
outs <- choose (1, 10)
>>= vector
>>= genTxOut
genSelectionFor (NE.fromList outs)
instance Arbitrary OnDanglingChange
where
arbitrary = elements [ PayAndBalance, SaveMoney ]
instance Arbitrary FeeOptions where
arbitrary = do
t <- choose (0, 10) -- dust threshold
c <- choose (0, 10) -- price per transaction
a <- choose (0, 10) -- price per input/output
return $ FeeOptions
{ estimateFee =
\s -> Fee
$ fromIntegral
$ c + a * (length (inputs s) + length (outputs s))
, dustThreshold = Coin t
, onDanglingChange = PayAndBalance
}
instance Show FeeOptions where
show (FeeOptions _ dust onDangling) = show (dust, onDangling)