/
Addresses.hs
252 lines (227 loc) · 9.45 KB
/
Addresses.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
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
module Test.Integration.Scenario.CLI.Shelley.Addresses
( spec
) where
import Prelude
import Cardano.Wallet.Api.Types
( ApiAddress, ApiWallet, DecodeAddress (..), EncodeAddress (..), getApiT )
import Cardano.Wallet.Primitive.AddressDiscovery.Sequential
( defaultAddressPoolGap, getAddressPoolGap )
import Cardano.Wallet.Primitive.Types
( AddressState (..) )
import Control.Monad
( forM_ )
import Data.Generics.Internal.VL.Lens
( view, (^.) )
import Data.Proxy
( Proxy (..) )
import Data.Quantity
( Quantity (..) )
import Data.Text
( Text )
import System.Command
( Exit (..), Stderr (..), Stdout (..) )
import System.Exit
( ExitCode (..) )
import Test.Hspec
( SpecWith, describe )
import Test.Hspec.Expectations.Lifted
( shouldBe, shouldContain )
import Test.Hspec.Extra
( it )
import Test.Integration.Framework.DSL
( Context (..)
, KnownCommand
, deleteWalletViaCLI
, emptyRandomWallet
, emptyWallet
, emptyWalletWith
, eventually
, expectCliField
, expectCliListField
, expectValidJSON
, fixtureWallet
, getWalletViaCLI
, listAddressesViaCLI
, minUTxOValue
, postTransactionViaCLI
, walletId
)
import Test.Integration.Framework.TestData
( errMsg404NoWallet, falseWalletIds )
import qualified Data.Text as T
spec :: forall n t.
( KnownCommand t
, DecodeAddress n
, EncodeAddress n
) => SpecWith (Context t)
spec = describe "SHELLEY_CLI_ADDRESSES" $ do
it "ADDRESS_LIST_01 - Can list addresses - default poolGap" $ \ctx -> do
let g = fromIntegral $ getAddressPoolGap defaultAddressPoolGap
walId <- emptyWallet' ctx
(Exit c, Stdout out, Stderr err) <- listAddressesViaCLI @t ctx [walId]
err `shouldBe` "Ok.\n"
c `shouldBe` ExitSuccess
json <- expectValidJSON (Proxy @[ApiAddress n]) out
length json `shouldBe` g
forM_ [0..(g-1)] $ \addrNum -> do
expectCliListField
addrNum (#state . #getApiT) (`shouldBe` Unused) json
it "ADDRESS_LIST_01 - Can list addresses - non-default poolGap" $ \ctx -> do
let addrPoolGap = 60
walId <- emptyWalletWith' ctx
("This is Wallet, OK?", "cardano-wallet", addrPoolGap)
(Exit c, Stdout out, Stderr err) <- listAddressesViaCLI @t ctx [walId]
err `shouldBe` "Ok.\n"
c `shouldBe` ExitSuccess
json <- expectValidJSON (Proxy @[ApiAddress n]) out
length json `shouldBe` addrPoolGap
forM_ [0..59] $ \addrNum -> do
expectCliListField
addrNum (#state . #getApiT) (`shouldBe` Unused) json
it "ADDRESS_LIST_02 - Can filter used and unused addresses" $ \ctx -> do
let g = fromIntegral $ getAddressPoolGap defaultAddressPoolGap
walId <- fixtureWallet' ctx
(Exit c1, Stdout o1, Stderr e1)
<- listAddressesViaCLI @t ctx ["--state", "used", walId]
e1 `shouldBe` "Ok.\n"
c1 `shouldBe` ExitSuccess
j1 <- expectValidJSON (Proxy @[ApiAddress n]) o1
length j1 `shouldBe` 10
forM_ [0..9] $ \addrNum -> do
expectCliListField
addrNum (#state . #getApiT) (`shouldBe` Used) j1
(Exit c2, Stdout o2, Stderr e2)
<- listAddressesViaCLI @t ctx ["--state", "unused", walId]
e2 `shouldBe` "Ok.\n"
c2 `shouldBe` ExitSuccess
j2 <- expectValidJSON (Proxy @[ApiAddress n]) o2
length j2 `shouldBe` g
forM_ [0..(g-10)] $ \addrNum -> do
expectCliListField
addrNum (#state . #getApiT) (`shouldBe` Unused) j2
it "ADDRESS_LIST_02 - Shows nothing when there are no used addresses"
$ \ctx -> do
walId <- emptyWallet' ctx
(Exit c1, Stdout o1, Stderr e1)
<- listAddressesViaCLI @t ctx ["--state", "used", walId]
e1 `shouldBe` "Ok.\n"
c1 `shouldBe` ExitSuccess
j1 <- expectValidJSON (Proxy @[ApiAddress n]) o1
length j1 `shouldBe` 0
(Exit c2, Stdout o2, Stderr e2)
<- listAddressesViaCLI @t ctx ["--state", "unused", walId]
e2 `shouldBe` "Ok.\n"
c2 `shouldBe` ExitSuccess
j2 <- expectValidJSON (Proxy @[ApiAddress n]) o2
length j2 `shouldBe` 20
forM_ [0..19] $ \addrNum -> do
expectCliListField
addrNum (#state . #getApiT) (`shouldBe` Unused) j2
describe "ADDRESS_LIST_02 - Invalid filters show error message" $ do
let filters =
[ "usedd"
, "uused"
, "unusedd"
, "uunused"
, "USED"
, "UNUSED"
, "-1000"
, "44444444"
, "*"
]
forM_ filters $ \fil -> it ("--state=" <> fil) $ \ctx -> do
walId <- emptyWallet' ctx
(Exit c, Stdout o, Stderr e)
<- listAddressesViaCLI @t ctx ["--state", fil, walId]
let err = "Unable to decode the given value: \"" <> fil <> "\". Please\
\ specify one of the following values: used, unused."
e `shouldContain` err
c `shouldBe` ExitFailure 1
o `shouldBe` ""
it "ADDRESS_LIST_03 - Generates new address pool gap" $ \ctx -> do
let initPoolGap = 10
wSrc <- fixtureWallet' ctx
wDest <- emptyWalletWith ctx ("Wallet", "cardano-wallet", initPoolGap)
let widDest = T.unpack $ wDest ^. walletId
-- make sure all addresses in address_pool_gap are 'Unused'
(Exit c, Stdout o, Stderr e) <- listAddressesViaCLI @t ctx [widDest]
e `shouldBe` "Ok.\n"
c `shouldBe` ExitSuccess
j <- expectValidJSON (Proxy @[ApiAddress n]) o
length j `shouldBe` initPoolGap
forM_ [0..initPoolGap - 1] $ \addrNum -> do
expectCliListField
addrNum (#state . #getApiT) (`shouldBe` Unused) j
-- run 10 transactions to make all addresses `Used`
forM_ [0..initPoolGap - 1] $ \addrNum -> do
let dest = encodeAddress @n (getApiT $ fst $ (j !! addrNum) ^. #id)
let args = [wSrc, "--payment" , (show minUTxOValue) <> "@" <> (T.unpack dest)]
(cTx, _, _) <- postTransactionViaCLI @t ctx "cardano-wallet" args
cTx `shouldBe` ExitSuccess
eventually "all transactions are in ledger" $ do
Stdout o2 <- getWalletViaCLI @t ctx $ T.unpack (wDest ^. walletId)
w <- expectValidJSON (Proxy @ApiWallet) o2
expectCliField
(#balance . #getApiT . #available)
(`shouldBe` Quantity (10 * 1_000_000)) w
-- verify new address_pool_gap has been created
(Exit c1, Stdout o1, Stderr e1) <- listAddressesViaCLI @t ctx [widDest]
e1 `shouldBe` "Ok.\n"
c1 `shouldBe` ExitSuccess
j1 <- expectValidJSON (Proxy @[ApiAddress n]) o1
length j1 `shouldBe` 2*initPoolGap
forM_ [0..initPoolGap - 1] $ \addrNum -> do
expectCliListField
addrNum (#state . #getApiT) (`shouldBe` Used) j1
forM_ [initPoolGap..2*initPoolGap - 1] $ \addrNum -> do
expectCliListField
addrNum (#state . #getApiT) (`shouldBe` Unused) j1
describe "ADDRESS_LIST_04 - False wallet ids" $ do
forM_ falseWalletIds $ \(title, walId) -> it title $ \ctx -> do
(Exit c, Stdout o, Stderr e) <- listAddressesViaCLI @t ctx [walId]
o `shouldBe` ""
c `shouldBe` ExitFailure 1
if (title == "40 chars hex") then
e `shouldContain`
errMsg404NoWallet "1111111111111111111111111111111111111111"
else
e `shouldContain`
"wallet id should be a hex-encoded string of 40 characters"
it "ADDRESS_LIST_04 - 'almost' valid walletId" $ \ctx -> do
wid <- emptyWallet' ctx
(Exit c, Stdout o, Stderr e) <- listAddressesViaCLI @t ctx [wid ++ "0"]
e `shouldContain`
"wallet id should be a hex-encoded string of 40 characters"
o `shouldBe` ""
c `shouldBe` ExitFailure 1
it "ADDRESS_LIST_04 - Deleted wallet" $ \ctx -> do
wid <- emptyWallet' ctx
Exit d <- deleteWalletViaCLI @t ctx wid
d `shouldBe` ExitSuccess
(Exit c, Stdout o, Stderr e) <- listAddressesViaCLI @t ctx [wid]
e `shouldContain` errMsg404NoWallet (T.pack wid)
o `shouldBe` ""
c `shouldBe` ExitFailure 1
it "BYRON_ADDRESS_LIST - Byron wallet on Shelley CLI" $ \ctx -> do
wid <- emptyRandomWallet' ctx
(Exit c, Stdout o, Stderr e) <- listAddressesViaCLI @t ctx [wid]
e `shouldContain` errMsg404NoWallet (T.pack wid)
o `shouldBe` ""
c `shouldBe` ExitFailure 1
where
emptyRandomWallet' :: Context t -> IO String
emptyRandomWallet' = fmap (T.unpack . view walletId) . emptyRandomWallet
emptyWallet' :: Context t -> IO String
emptyWallet' = fmap (T.unpack . view walletId) . emptyWallet
emptyWalletWith' :: Context t -> (Text, Text, Int) -> IO String
emptyWalletWith' ctx (name, pass, pg) =
fmap (T.unpack . view walletId) (emptyWalletWith ctx (name, pass, pg))
fixtureWallet' :: Context t -> IO String
fixtureWallet' = fmap (T.unpack . view walletId) . fixtureWallet