This repository has been archived by the owner on Mar 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
DSL.hs
927 lines (798 loc) · 27.4 KB
/
DSL.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
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE RankNTypes #-}
module Test.Integration.Framework.DSL
(
-- * Scenario
scenario
, xscenario
, pendingWith
, Scenarios
, Context(..)
-- * Steps
, Setup(..)
, setup
, request
, request_
, successfulRequest
, unsafeRequest
, verify
-- * Requests (Only API types)
, AddressPoolGap
, AssuranceLevel(..)
, DestinationChoice(..)
, EosWallet(..)
, FilterOperations(..)
, NewAccount (..)
, NewAddress(..)
, NewEosWallet (..)
, NewWallet (..)
, PasswordUpdate (..)
, Payment (..)
, RawPassword (..)
, Redemption (..)
, ShieldedRedemptionCode (..)
, SortOperations(..)
, WalletOperation(..)
, WalletUpdate(..)
, defaultAccountId
, defaultAssuranceLevel
, defaultDistribution
, defaultAddressPoolGap
, customDistribution
, defaultGroupingPolicy
, defaultPage
, defaultPerPage
, defaultSetup
, defaultSource
, defaultSpendingPassword
, defaultWalletName
, mkSpendingPassword
, mkPassword
, noRedemptionMnemonic
, noSpendingPassword
, noAddressPoolGap
-- * Expectations
, WalletError(..)
, ErrNotEnoughMoney(..)
, TransactionStatus(..)
, expectAddressInIndexOf
, expectListSizeEqual
, expectListItemFieldEqual
, expectEqual
, expectError
, expectFieldEqual
, expectFieldDiffer
, expectJSONError
, expectSuccess
, expectTxInHistoryOf
, expectTxStatusEventually
, expectTxStatusNever
, expectWalletError
, expectWalletEventuallyRestored
, expectWalletUTxO
-- * Helpers
, ($-)
, (</>)
, (!!)
, address
, addresses
, addressPoolGap
, amount
, assuranceLevel
, backupPhrase
, createdAt
, externallyOwnedAccounts
, failures
, fromWalletId
, initialCoins
, mnemonicWords
, rawAddressPoolGap
, rawMnemonicPassword
, rawPassword
, spendingPassword
, spendingPasswordLastUpdate
, syncState
, totalSuccess
, walAddresses
, wallet
, walletId
, walletName
, wallets
, json
, hasSpendingPassword
, mkAddress
, mkBackupPhrase
) where
import Universum hiding (getArgs, second)
import Control.Concurrent (threadDelay)
import Control.Concurrent.Async (race)
import Crypto.Hash (hash)
import Crypto.Hash.Algorithms (Blake2b_256)
import Data.Aeson.QQ (aesonQQ)
import qualified Data.ByteArray as ByteArray
import Data.Default (Default (..))
import qualified Data.Foldable as F
import Data.Generics.Internal.VL.Lens (lens)
import Data.Generics.Product.Fields (field)
import Data.Generics.Product.Positions (HasPosition, position)
import Data.Generics.Product.Typed (HasType, typed)
import Data.List ((!!))
import qualified Data.List.NonEmpty as NonEmpty
import qualified Data.Map.Strict as Map
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import Data.Typeable (typeOf, typeRepTyCon)
import Formatting (build, sformat)
import Language.Haskell.TH.Quote (QuasiQuoter)
import Test.Hspec.Core.Spec (SpecM, it, xit)
import qualified Test.Hspec.Core.Spec as H
import Test.Hspec.Expectations.Lifted
import Test.QuickCheck (arbitrary, generate)
import Web.HttpApiData (ToHttpApiData (..))
import Cardano.Mnemonic (mkMnemonic, mnemonicToSeed)
import Cardano.Wallet.API.Request.Filter (FilterOperations (..))
import Cardano.Wallet.API.Request.Pagination (Page, PerPage)
import Cardano.Wallet.API.Request.Sort (SortOperations (..))
import Cardano.Wallet.API.Response (JSONValidationError (..))
import Cardano.Wallet.API.V1.Types
import Cardano.Wallet.Client.Http (BaseUrl, ClientError (..), Manager,
WalletClient)
import qualified Cardano.Wallet.Client.Http as Client
import Cardano.Wallet.Kernel.AddressPoolGap (AddressPoolGap,
mkAddressPoolGap)
import Cardano.Wallet.Kernel.Ed25519Bip44 (deriveAccountPrivateKey,
derivePublicKey, genEncryptedSecretKey)
import Pos.Chain.Txp (TxIn (..), TxOut (..), TxOutAux (..))
import Pos.Core (Coin, IsBootstrapEraAddr (..), deriveLvl2KeyPair,
mkCoin, unsafeGetCoin)
import Pos.Core.NetworkMagic (NetworkMagic (..))
import Pos.Crypto (ShouldCheckPassphrase (..),
safeDeterministicKeyGen)
import Test.Integration.Framework.Request (HasHttpClient, request,
request_, successfulRequest, unsafeRequest, ($-))
import Test.Integration.Framework.Scenario (Scenario)
--
-- SCENARIO
--
-- Prior to starting integration tests, we setup a global context
-- and "prepare" a few faucet wallets which all contains some funds.
-- This helps speed up testing and isolate them.
data Context = Context
{ _faucets
:: [FilePath]
-- Already funded faucet wallets
, _client
:: WalletClient IO
-- A handle to the underlying wallet backend server
, _manager
:: (BaseUrl, Manager)
-- The underlying BaseUrl and Manager used by the Wallet Client
} deriving (Generic)
-- | Just a type-alias to 'SpecM', like 'scenario'. Ultimately, everything is
-- made in such way that we can use normal (albeit lifted) HSpec functions and
-- utilities if needed (and rely on its CLI as well when needed).
type Scenarios ctx = SpecM (MVar ctx) ()
-- | Just a slightly-specialized alias for 'it' to help lil'GHC. Also, makes
-- sure the wallet has been cleared out completely before running the scenario.
scenario
:: String
-> Scenario Context IO ()
-> Scenarios Context
scenario title spec = it title (successfulRequest Client.resetWalletState >> spec)
xscenario
:: String
-> Scenario Context IO ()
-> Scenarios Context
xscenario = xit
-- | Lifted version of `H.pendingWith` allowing for temporarily skipping
-- scenarios from execution with a reason, like:
--
-- scenario title $ do
-- pendingWith "This test fails due to bug #213"
-- test
pendingWith
:: (MonadIO m, MonadFail m)
=> String
-> m ()
pendingWith = liftIO . H.pendingWith
--
-- TYPES
--
data DestinationChoice
= RandomDestination
| LockedDestination
deriving (Show, Generic)
newtype RawPassword = RawPassword { getRawPassword :: Text }
deriving stock (Show, Generic)
deriving newtype (Monoid, Semigroup)
instance IsString RawPassword where
fromString = RawPassword . T.pack
--
-- STEPS
--
data Setup = Setup
{ _initialCoins
:: [Coin]
, _walletName
:: Text
, _assuranceLevel
:: AssuranceLevel
, _mnemonicWords
:: [Text]
, _rawPassword
:: RawPassword
, _rawMnemonicPassword
:: RawPassword
, _rawAddressPoolGap
:: Word8
} deriving (Show, Generic)
data Fixture = Fixture
{ _wallet
:: Wallet
, _destinations
:: NonEmpty Address
, _backupPhrase
:: BackupPhrase
, _spendingPassword
:: SpendingPassword
, _eoAccounts
:: [AccountPublicKeyWithIx]
-- ^ WARNING We rely on lazyness here, so don't make this strict
, _addressPoolGap
:: AddressPoolGap
} deriving (Show, Generic)
-- | Setup a wallet with the given parameters.
setup
:: Setup
-> Scenario Context IO Fixture
setup args = do
phrase <- if null (args ^. mnemonicWords)
then liftIO $ generate arbitrary
else mkBackupPhrase (args ^. mnemonicWords)
let walPwd@(WalletPassPhrase pwd) = mkPassword (args ^. rawPassword)
wal <- setupWallet args phrase walPwd
addrs <- forM (RandomDestination :| []) setupDestination
let accs = genExternallyOwnedAccounts (phrase, args ^. rawMnemonicPassword) pwd
gap <- unsafeMkAddressPoolGap (args ^. rawAddressPoolGap)
return $ Fixture wal addrs phrase walPwd accs gap
-- | Apply 'a' to all actions in sequence
verify :: (Monad m) => a -> [a -> m ()] -> m ()
verify a = mapM_ (a &)
--
-- DEFAULT VALUES
--
defaultAccountId :: AccountIndex
defaultAccountId = minBound
defaultAddressPoolGap :: AddressPoolGap
defaultAddressPoolGap = def
defaultAssuranceLevel :: AssuranceLevel
defaultAssuranceLevel = NormalAssurance
defaultDistribution
:: HasType (NonEmpty Address) s
=> Word64
-> s
-> NonEmpty PaymentDistribution
defaultDistribution c s = pure $
PaymentDistribution (WalAddress $ head $ s ^. typed) (WalletCoin $ mkCoin c)
customDistribution
:: NonEmpty (Account,Word64)
-> NonEmpty PaymentDistribution
customDistribution payees =
let recepientWalAddresses = NonEmpty.fromList
$ map (view walAddresses)
$ concatMap (view addresses . fst)
$ payees
in NonEmpty.zipWith
PaymentDistribution
recepientWalAddresses
(map ((\coin -> WalletCoin $ mkCoin coin) . snd) payees)
defaultGroupingPolicy :: Maybe WalletInputSelectionPolicy
defaultGroupingPolicy = Nothing
defaultPage :: Maybe Page
defaultPage = Nothing
defaultPerPage :: Maybe PerPage
defaultPerPage = Nothing
defaultSetup :: Setup
defaultSetup = Setup
{ _initialCoins = []
, _walletName = defaultWalletName
, _assuranceLevel = defaultAssuranceLevel
, _mnemonicWords = []
, _rawPassword = mempty
, _rawMnemonicPassword = mempty
, _rawAddressPoolGap = 20
}
defaultSource
:: HasType Wallet s
=> s
-> PaymentSource
defaultSource s =
PaymentSource (s ^. wallet . walletId) defaultAccountId
defaultSpendingPassword :: SpendingPassword
defaultSpendingPassword = mempty
defaultWalletName :: Text
defaultWalletName = "Fixture Wallet"
noRedemptionMnemonic :: Maybe RedemptionMnemonic
noRedemptionMnemonic = Nothing
noSpendingPassword :: Maybe SpendingPassword
noSpendingPassword = Nothing
noAddressPoolGap :: Maybe AddressPoolGap
noAddressPoolGap = Nothing
--
-- HELPERS
--
json :: QuasiQuoter
json = aesonQQ
infixr 5 </>
(</>) :: ToHttpApiData a => Text -> a -> Text
base </> next = mconcat [base, "/", toQueryParam next]
address :: HasType WalAddress s => Lens' s WalAddress
address = typed
addressPoolGap :: HasType AddressPoolGap s => Lens' s AddressPoolGap
addressPoolGap = typed
amount :: HasType WalletCoin s => Lens' s Word64
amount =
lens _get _set
where
_get :: HasType WalletCoin s => s -> Word64
_get = unsafeGetCoin . unWalletCoin . view typed
_set :: HasType WalletCoin s => (s, Word64) -> s
_set (s, v) = set typed (WalletCoin $ mkCoin v) s
assuranceLevel :: HasType AssuranceLevel s => Lens' s AssuranceLevel
assuranceLevel = typed
backupPhrase :: HasType BackupPhrase s => Lens' s BackupPhrase
backupPhrase = typed
createdAt :: HasPosition 6 s s WalletTimestamp WalletTimestamp => Lens' s WalletTimestamp
createdAt = position @6
externallyOwnedAccounts :: HasType [AccountPublicKeyWithIx] s => Lens' s [AccountPublicKeyWithIx]
externallyOwnedAccounts = typed
failures :: Lens' (BatchImportResult a) [a]
failures = field @"aimFailures"
faucets :: HasType [FilePath] s => Lens' s [FilePath]
faucets = typed
initialCoins
:: HasType [Coin] s
=> Lens' s [Word64]
initialCoins =
lens _get _set
where
_get :: HasType [Coin] s => s -> [Word64]
_get = map unsafeGetCoin . view typed
_set :: HasType [Coin] s => (s, [Word64]) -> s
_set (s, v) = set typed (map mkCoin v) s
mnemonicWords :: HasType [Text] s => Lens' s [Text]
mnemonicWords = typed
hasSpendingPassword :: HasType Bool s => Lens' s Bool
hasSpendingPassword = typed
rawAddressPoolGap :: Lens' Setup Word8
rawAddressPoolGap = field @"_rawAddressPoolGap"
rawPassword :: Lens' Setup RawPassword
rawPassword = field @"_rawPassword"
rawMnemonicPassword :: Lens' Setup RawPassword
rawMnemonicPassword = field @"_rawMnemonicPassword"
spendingPassword :: HasType SpendingPassword s => Lens' s SpendingPassword
spendingPassword = typed
syncState :: HasType SyncState s => Lens' s SyncState
syncState = typed
totalSuccess :: Lens' (BatchImportResult a) Natural
totalSuccess = field @"aimTotalSuccess"
wallet :: HasType Wallet s => Lens' s Wallet
wallet = typed
wallets :: HasType [Wallet] s => Lens' s [Wallet]
wallets = typed
walletId :: HasType WalletId s => Lens' s WalletId
walletId = typed
walletName :: HasType Text s => Lens' s Text
walletName = typed
spendingPasswordLastUpdate :: Lens' Wallet WalletTimestamp
spendingPasswordLastUpdate = field @"walSpendingPasswordLastUpdate"
addresses :: HasType [WalletAddress] s => Lens' s [WalletAddress]
addresses = typed
walAddresses :: HasType WalAddress s => Lens' s WalAddress
walAddresses = typed
--
-- EXPECTATIONS
--
-- | Expects data list returned by the API to be of certain length
expectListSizeEqual
:: (MonadIO m, MonadFail m, Foldable xs)
=> Int
-> Either ClientError (xs a)
-> m ()
expectListSizeEqual l = \case
Left e -> wantedSuccessButError e
Right xs -> length (F.toList xs) `shouldBe` l
-- | Expects that returned data list's particular item field matches the expected value
--
-- e.g.
-- verify response
-- [ expectDataListItemFieldEqual 0 walletName "first"
-- , expectDataListItemFieldEqual 1 walletName "second"
-- ]
expectListItemFieldEqual
:: (MonadIO m, MonadFail m, Show a, Eq a)
=> Int
-> Lens' s a
-> a
-> Either ClientError [s]
-> m ()
expectListItemFieldEqual i getter a = \case
Left e -> wantedSuccessButError e
Right s
| length s > i -> expectFieldEqual getter a (Right (s !! i))
| otherwise -> fail $
"expectListItemFieldEqual: trying to access the #" <> show i <>
" element from a list but there's none! "
-- | The type signature is more scary than it seems. This drills into a given
-- `a` type through the provided lens and sees whether field matches.
--
-- e.g.
-- do
-- expectFieldEqual #walAssuranceLevel AssuranceStrict response
-- expectFieldEqual #walName "My Wallet" response
expectFieldEqual
:: (MonadIO m, MonadFail m, Show a, Eq a)
=> Lens' s a
-> a
-> Either ClientError s
-> m ()
expectFieldEqual getter a = \case
Left e -> wantedSuccessButError e
Right s -> view getter s `shouldBe` a
-- | The opposite to 'expectFieldEqual'.
expectFieldDiffer
:: (MonadIO m, MonadFail m, Show a, Eq a)
=> Lens' s a
-> a
-> Either ClientError s
-> m ()
expectFieldDiffer getter a = \case
Left e -> wantedSuccessButError e
Right s -> view getter s `shouldNotBe` a
-- | Expects entire equality of two types
expectEqual
:: (MonadIO m, MonadFail m, Show a, Eq a)
=> a
-> Either ClientError a
-> m ()
expectEqual =
expectFieldEqual id
-- | Expect an errored response, without any further assumptions
expectError
:: (MonadIO m, MonadFail m, Show a)
=> Either ClientError a
-> m ()
expectError = \case
Left _ -> return ()
Right a -> wantedErrorButSuccess a
-- | Expect a successful response, without any further assumptions
expectSuccess
:: (MonadIO m, MonadFail m, Show a)
=> Either ClientError a
-> m ()
expectSuccess = \case
Left e -> wantedSuccessButError e
Right _ -> return ()
-- | Expect a transaction to be part of a wallet history.
expectTxInHistoryOf
:: (MonadIO m, MonadFail m, MonadReader ctx m, HasHttpClient ctx)
=> Wallet
-> Either ClientError Transaction
-> m ()
expectTxInHistoryOf w = \case
Left e -> wantedSuccessButError e
Right txn -> tryNextPage (on (==) txId txn) 1
where
tryNextPage predicate i = do
txns <- successfulRequest $ Client.getTransactionIndexFilterSorts
$- Just (walId w)
$- Nothing
$- Nothing
$- Just (fromInteger i)
$- Just 50
$- NoFilters
$- NoSorts
when (null txns) $
fail "expectTxInHistoryOf: couldn't find transaction in history"
case find predicate txns of
Nothing -> tryNextPage predicate (i + 1)
Just _ -> return ()
-- | Expect an address to be part of the global index
expectAddressInIndexOf
:: (MonadIO m, MonadFail m, MonadReader ctx m, HasHttpClient ctx)
=> Either ClientError WalletAddress
-> m ()
expectAddressInIndexOf = \case
Left e -> wantedSuccessButError e
Right addr -> tryNextPage ((==) addr) 1
where
tryNextPage predicate i = do
addrs <- successfulRequest $ Client.getAddressIndexPaginated
$- Just (fromInteger i)
$- Just 50
when (null addrs) $
fail "expectAddressInIndexOf: couldn't find address in history"
case find predicate addrs of
Nothing -> tryNextPage predicate (i + 1)
Just _ -> return ()
-- | Wait for a transaction to reach one of the given status. Fails after 60
-- seconds if not.
expectTxStatusEventually
:: (MonadIO m, MonadFail m, MonadReader ctx m, HasHttpClient ctx)
=> [TransactionStatus]
-> Either ClientError Transaction
-> m ()
expectTxStatusEventually statuses = \case
Left e -> wantedSuccessButError e
Right txn -> do
result <- ask >>= \ctx -> timeout (120 * second) (waitForTxStatus ctx statuses txn)
case result of
Nothing -> fail "expectTxStatusEventually: waited too long for statuses."
Just _ -> return ()
-- | Checks that a transacton "never" reaches one of the given status. Never
-- really means 60 seconds, you know...
expectTxStatusNever
:: (MonadIO m, MonadFail m, MonadReader ctx m, HasHttpClient ctx)
=> [TransactionStatus]
-> Either ClientError Transaction
-> m ()
expectTxStatusNever statuses = \case
Left e -> wantedSuccessButError e
Right txn -> do
result <- ask >>= \ctx -> timeout (120 * second) (waitForTxStatus ctx statuses txn)
case result of
Nothing -> return ()
Just _ -> fail "expectTxStatusNever: reached one of the provided statuses."
-- | Wait until a wallet is restored, up to a certain point.
expectWalletEventuallyRestored
:: (MonadIO m, MonadFail m, MonadReader ctx m, HasHttpClient ctx)
=> Either ClientError Wallet
-> m ()
expectWalletEventuallyRestored = \case
Left e -> wantedSuccessButError e
Right w -> do
result <- ask >>= \ctx -> timeout (120 * second) (waitForRestored ctx w)
case result of
Nothing -> fail "expectWalletEventuallyRestored: waited too long for restoration."
Just _ -> return ()
expectWalletError
:: (MonadIO m, MonadFail m, Show a)
=> WalletError
-> Either ClientError a
-> m ()
expectWalletError e' = \case
Right a -> wantedErrorButSuccess a
Left (ClientWalletError e) -> constructorOf e `shouldBe` constructorOf e'
Left e -> e `shouldBe` (ClientWalletError e')
where
constructorOf = typeRepTyCon . typeOf
-- | Verifies that the response is errored from a failed JSON validation
-- matching part of the given message.
expectJSONError
:: (MonadIO m, MonadFail m, Show a)
=> String
-> Either ClientError a
-> m ()
expectJSONError excerpt = \case
Right a -> wantedErrorButSuccess a
Left (ClientJSONError (JSONValidationFailed msg)) ->
T.unpack msg `shouldContain` excerpt
Left e ->
fail $ "expectJSONError: got something else than a JSON validation failure: " <> show e
expectWalletUTxO
:: (MonadIO m, MonadFail m)
=> [Word64]
-> Either ClientError UtxoStatistics
-> m ()
expectWalletUTxO coins = \case
Left e -> wantedSuccessButError e
Right stats -> do
addr <- liftIO $ generate arbitrary
let constructUtxoEntry input coin =
( TxInUnknown input "arbitrary input"
, TxOutAux (TxOut addr (mkCoin coin))
)
let utxo = Map.fromList $ zipWith constructUtxoEntry [0..] coins
computeUtxoStatistics log10 [utxo] `shouldBe` stats
--
-- INTERNALS
--
fromWalletId :: Client.WalletId -> Text
fromWalletId (Client.WalletId a) = a
wantedSuccessButError
:: (MonadFail m, Show e)
=> e
-> m void
wantedSuccessButError =
fail . ("expected a successful response but got an error: " <>) . show
wantedErrorButSuccess
:: (MonadFail m, Show a)
=> a
-> m void
wantedErrorButSuccess =
fail . ("expected an error but got a successful response: " <>) . show
timeout :: (MonadIO m) => Int -> IO a -> m (Maybe a)
timeout maxWaitingTime action = liftIO $ do
race (threadDelay maxWaitingTime) action >>= \case
Left _ -> return Nothing
Right a -> return (Just a)
second :: Int
second = 1000000
-- | Wait until the given transaction reaches the given status. Potentially
-- loop ad infinitum; Caller is expected to cancel the thread at some point.
waitForTxStatus
:: HasHttpClient ctx
=> ctx
-> [TransactionStatus]
-> Transaction
-> IO ()
waitForTxStatus ctx statuses txn = do
-- NOTE
-- A bit tricky here, we can't just fire async operation on anything else
-- but plain `IO`. Hence the explicit context passing here.
txns <- flip runReaderT ctx $ successfulRequest $ Client.getTransactionIndex
$- Nothing
$- Nothing
$- Nothing
let tx = find (on (==) txId txn) txns
if ((fmap txStatus tx) `elem` (fmap Just statuses)) then
return ()
else
threadDelay (5 * second) >> waitForTxStatus ctx statuses txn
-- | Wait until the given wallet is restored.
waitForRestored
:: HasHttpClient ctx
=> ctx
-> Wallet
-> IO ()
waitForRestored ctx w = do
response <- flip runReaderT ctx $ successfulRequest $ Client.getWallet
$- w ^. walletId
case walSyncState response of
Synced -> return ()
_ -> threadDelay (5 * second) >> waitForRestored ctx w
-- | Make a backup phrase from a raw list of words.
mkBackupPhrase
:: (MonadIO m, MonadFail m)
=> [Text]
-> m BackupPhrase
mkBackupPhrase ws = either onError onSuccess (mkMnemonic ws)
where
onError err =
fail $ "Invalid BackupPhrase provided: " <> show ws <> ". We expect 12\
\ valid english mnemonic words but the following error has occured:"
<> show err
onSuccess =
return . BackupPhrase
-- | Create a Base16-encoded spending password from raw text
mkPassword
:: RawPassword
-> SpendingPassword
mkPassword (RawPassword txt)
| null txt = mempty
| otherwise = txt
& T.encodeUtf8
& hash @ByteString @Blake2b_256
& ByteArray.convert
& WalletPassPhrase
mkAddress
:: (MonadIO m, MonadFail m)
=> BackupPhrase
-> AccountIndex
-> Word32
-> m WalAddress
mkAddress (BackupPhrase mnemonic) (accId) ix = do
let (_, esk) = safeDeterministicKeyGen
(mnemonicToSeed mnemonic)
mempty
let maddr = fst <$> deriveLvl2KeyPair
NetworkMainOrStage
(IsBootstrapEraAddr True)
(ShouldCheckPassphrase False)
mempty
esk
(getAccIndex accId)
ix
case maddr of
Nothing ->
fail "The impossible happened: failed to generate a\
\ random address. This can only happened if you\
\ provided a derivation index that is out-of-bound!"
Just addr ->
return (WalAddress addr)
-- | Execute the given setup action with using the next faucet wallet. It fails
-- hard if there's no more faucet wallet available.
withNextFaucet
:: (Wallet -> Scenario Context IO c)
-> Scenario Context IO c
withNextFaucet actionWithFaucet = do
ctx <- get
when (null $ ctx ^. faucets) $ fail $
"\nFailed to setup new scenario: there's no more available faucet wallets!\
\\nWe import a faucet wallet for each scenario but only have a limited\
\ number of them. This can be modified directly in the configuration file,\
\ by default in: \n\n\ttest/integration/configuration.yaml\
\\n\ntry increasing the number of available 'poors' wallets\
\\n\nspec: &default_core_genesis_spec\
\\n\tinitializer:\
\\n\t\ttestBalance:\
\\n\t\t\tpoors: ???\n"
let acquireFaucet = do
let (key:rest) = ctx ^. faucets
put (ctx & faucets .~ rest)
successfulRequest $ Client.importWallet $- WalletImport Nothing key
let releaseFaucet faucet = do
successfulRequest (Client.deleteWallet $- walId faucet)
bracket acquireFaucet releaseFaucet actionWithFaucet
-- | Setup a given wallet and pre-fill it with given coins.
setupWallet
:: Setup
-> BackupPhrase
-> SpendingPassword
-> Scenario Context IO Wallet
setupWallet args phrase password = do
wal <- successfulRequest $ Client.postWallet $- NewWallet
phrase
(Just $ password)
(args ^. assuranceLevel)
(args ^. walletName)
CreateWallet
unless (null $ args ^. initialCoins) $ withNextFaucet $ \faucet -> do
let paymentSource = PaymentSource (walId faucet) minBound
let paymentDist (addr, coin) = pure $ PaymentDistribution (addrId addr) (WalletCoin coin)
forM_ (args ^. initialCoins) $ \coin -> do
-- NOTE
-- Making payments to a different address each time to cope with
-- grouping policy. That's actually a behavior we might want to
-- test in the future. So, we'll need to do something smarter here.
addr <- successfulRequest $ Client.postAddress $- NewAddress
(Just $ password)
minBound
(walId wal)
txn <- request $ Client.postTransaction $- Payment
paymentSource
(paymentDist (addr, mkCoin coin))
Nothing
Nothing
expectTxStatusEventually [InNewestBlocks, Persisted] txn
return wal
-- | Generate some destinations for payments.
--
-- - RandomDestination generates fake addresses going nowhere (hopefully :) ...)
-- - LockedDestination generates addresses that points to an asset-locked wallet
setupDestination
:: DestinationChoice
-> Scenario Context IO Address
setupDestination = \case
RandomDestination -> do
bp <- liftIO (generate arbitrary)
unWalAddress <$> mkAddress bp defaultAccountId 1
LockedDestination ->
fail "Asset-locked destination aren't yet implemented. This\
\ requires slightly more work than it seems and will be\
\ implemented later."
-- | Lazily generate an "infinite" list of accounts (PubKey, Index)
-- from a mnemonic using the new address derivation scheme V2 (BIP-44 with
-- Ed25519 elliptic curve)
genExternallyOwnedAccounts
:: (BackupPhrase, RawPassword)
-> PassPhrase
-> [AccountPublicKeyWithIx]
genExternallyOwnedAccounts (BackupPhrase mnemonic, RawPassword mnePassphrase) passphrase =
let
esk = genEncryptedSecretKey (mnemonic, T.encodeUtf8 mnePassphrase) passphrase
accountXPrv = fmap derivePublicKey . deriveAccountPrivateKey passphrase esk
in
catMaybes
[ AccountPublicKeyWithIx <$> accountXPrv (getAccIndex ix) <*> pure ix
| ix <- [minBound..maxBound]
]
-- | Create an AddressPoolGap from a raw Word32 and throw in IO if anything
-- goes wrong.
unsafeMkAddressPoolGap
:: (MonadIO m, MonadFail m)
=> Word8
-> m AddressPoolGap
unsafeMkAddressPoolGap g = either onError return $ mkAddressPoolGap g
where
onError = fail . toString . sformat build