-
Notifications
You must be signed in to change notification settings - Fork 213
/
TransactionSpec.hs
394 lines (362 loc) · 14.9 KB
/
TransactionSpec.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
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Cardano.Wallet.Jormungandr.TransactionSpec
( spec
) where
import Prelude
import Cardano.Wallet.Jormungandr.Binary
( putSignedTx, runPut )
import Cardano.Wallet.Jormungandr.Compatibility
( Jormungandr )
import Cardano.Wallet.Jormungandr.Environment
( KnownNetwork (..), Network (..) )
import Cardano.Wallet.Jormungandr.Transaction
( newTransactionLayer )
import Cardano.Wallet.Primitive.AddressDerivation
( Depth (..)
, Key
, Passphrase (..)
, XPrv
, keyToAddress
, publicKey
, unsafeGenerateKeyFromSeed
)
import Cardano.Wallet.Primitive.CoinSelection
( CoinSelection (..) )
import Cardano.Wallet.Primitive.CoinSelection.LargestFirst
( largestFirst )
import Cardano.Wallet.Primitive.Types
( Address (..)
, Coin (..)
, Hash (..)
, ShowFmt (..)
, TxIn (..)
, TxOut (..)
, UTxO (..)
)
import Cardano.Wallet.Transaction
( TransactionLayer (..) )
import Cardano.Wallet.Unsafe
( unsafeFromHex )
import Control.Monad.Trans.Except
( runExceptT )
import Data.ByteArray.Encoding
( Base (Base16), convertToBase )
import Data.ByteString
( ByteString )
import Data.Functor.Identity
( Identity (runIdentity) )
import Data.List.NonEmpty
( NonEmpty )
import Data.Proxy
( Proxy (..) )
import Data.Quantity
( Quantity (..) )
import Test.Hspec
( Spec, SpecWith, describe, it, shouldBe )
import Test.QuickCheck
( Arbitrary (..)
, Gen
, Property
, choose
, property
, scale
, vectorOf
, withMaxSuccess
, (===)
)
import qualified Cardano.Wallet.Primitive.CoinSelection as CS
import qualified Data.ByteArray as BA
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL
import qualified Data.List.NonEmpty as NE
import qualified Data.Map as Map
spec :: Spec
spec = do
estimateSizeSpec
mkStdTxSpec
{-------------------------------------------------------------------------------
Size Estimation
-------------------------------------------------------------------------------}
estimateSizeSpec :: Spec
estimateSizeSpec = do
describe "estimateSize" $ do
it "Estimated size is zero"
(withMaxSuccess 2500 $ property $ propSizeEstimation $ Proxy @'Mainnet)
it "Estimated size is zero"
(withMaxSuccess 2500 $ property $ propSizeEstimation $ Proxy @'Testnet)
propSizeEstimation
:: forall n. (KnownNetwork n)
=> Proxy n
-> (ShowFmt CoinSelection)
-> Property
propSizeEstimation _ (ShowFmt sel) =
let
blockHash =
Hash {getHash = "\216OY\rX\199\234\188.<O\\\244Y\211\210\254\224`i\216\DC3\167\132\139\154\216\161T\174\247\155"}
tl = newTransactionLayer blockHash :: TransactionLayer (Jormungandr n)
calcSize = estimateSize tl sel
in calcSize === Quantity 0
instance Arbitrary CoinSelection where
shrink sel@(CoinSelection inps outs chgs) = case (inps, outs, chgs) of
([_], [_], []) ->
[]
_ ->
let
inps' = take (max 1 (length inps `div` 2)) inps
outs' = take (max 1 (length outs `div` 2)) outs
chgs' = take (length chgs `div` 2) chgs
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'
, CoinSelection inps' outs chgs
, CoinSelection inps outs chgs'
, CoinSelection inps outs' chgs
, CoinSelection inps'' outs'' chgs''
, CoinSelection inps'' outs chgs
, CoinSelection inps outs'' chgs
, CoinSelection inps outs chgs''
]
arbitrary = do
outs <- choose (1, 10)
>>= \n -> vectorOf n arbitrary
>>= genTxOut
genSelection (NE.fromList outs)
deriving instance Arbitrary a => Arbitrary (ShowFmt a)
-- Check whether a selection is valid
isValidSelection :: CoinSelection -> Bool
isValidSelection (CoinSelection i o c) =
let
oAmt = sum $ map (fromIntegral . getCoin . coin) o
cAmt = sum $ map (fromIntegral . getCoin) c
iAmt = sum $ map (fromIntegral . getCoin . coin . snd) i
in
(iAmt :: Integer) >= (oAmt + cAmt)
genTxOut :: [Coin] -> Gen [TxOut]
genTxOut coins = do
let n = length coins
outs <- vectorOf n arbitrary
return $ zipWith TxOut outs coins
genSelection :: NonEmpty TxOut -> Gen CoinSelection
genSelection outs = do
let opts = CS.CoinSelectionOptions 100
utxo <- vectorOf (NE.length outs * 3) arbitrary >>= genUTxO
case runIdentity $ runExceptT $ largestFirst opts outs utxo of
Left _ -> genSelection outs
Right (s,_) -> return s
genUTxO :: [Coin] -> Gen UTxO
genUTxO coins = do
let n = length coins
inps <- vectorOf n arbitrary
outs <- genTxOut coins
return $ UTxO $ Map.fromList $ zip inps outs
instance Arbitrary Coin where
shrink (Coin c) = Coin <$> shrink (fromIntegral c)
arbitrary = Coin <$> choose (1, 200000)
instance Arbitrary Address where
shrink _ = []
arbitrary =
pure $ Address "\131\&3$\195xi\193\"h\154\&5\145}\245:O\"\148\163\165/h^\ENQ\245\248\229;\135\231\234E/"
instance Arbitrary TxIn where
shrink _ = []
arbitrary = TxIn
<$> arbitrary
<*> scale (`mod` 3) arbitrary -- No need for a high indexes
instance Arbitrary (Hash "Tx") where
shrink _ = []
arbitrary = do
bytes <- BS.pack <$> vectorOf 32 arbitrary
pure $ Hash bytes
{-------------------------------------------------------------------------------
mkStdTx
-------------------------------------------------------------------------------}
mkStdTxSpec :: Spec
mkStdTxSpec = do
let (xprv0, pwd0) =
xprvFromSeed "arbitrary-seed-0"
-- ^ 30c8422fd3cbaf54449df9f627a1f88cf85d4fee84083a91cdb6f0dbdb09c24d
-- ed29409c6a8275a643a2e79d280d97a898a3366706f3f677434b75c9d98680d0
let (xprv1, pwd1) =
xprvFromSeed "arbitrary-seed-1"
-- ^ b81e217576bf2683f3359f50d0bf938ca3c61fdf7a2d0c1b2b35f7fb174dc042
-- 6bb7377c9ea9cb481b4c3df601379fbf69033add18c1d272d7975c43682afc48
let (xprv2, pwd2) =
xprvFromSeed "arbitrary-seed-2"
-- ^ e01bfb39e3e595fce0b9b19e386a82816e0cef8aa823c75a32bc0f39bcf7c14e
-- 8a03d255df0440b6d0fcf5d5199a582d1df7d858bd7556d4941ebf6223fa66d1
let txin0 = Hash $ unsafeFromHex
"666984dec4bc0ff1888be97bfe0694a96b35c58d025405ead51d5cc72a3019f4"
let txin1 = Hash $ unsafeFromHex
"1323856bc91c49e928f6f30f4e8d665d53eb4ab6028bd0ac971809d514c92db1"
let block0 = Hash $ unsafeFromHex
"13c3d835c53a198f7c8513b04d99eeb23c745c0a73364c2f0e802fa38eec9dba"
describe "mkStdTx 'Mainnet" $ do
let proxy = Proxy @(Jormungandr 'Mainnet)
let keyToAddress' = keyToAddress @(Jormungandr 'Mainnet)
let addr0 = keyToAddress' (publicKey xprv0)
-- ^ ca1qvk32hg8rppc0wn0lzpkq996pd3xkxqguel8tharwrpdch6czu2luh3truq
let addr1 = keyToAddress' (publicKey xprv1)
-- ^ ca1qwedmnalvvgqqgt2dczppejvyrn2lydmk2pxya4dd076wal8v6eykzfapdx
let addr2 = keyToAddress' (publicKey xprv2)
-- ^ ca1qvp2m296efkn4zy63y769x4g52g5vrt7e9dnvszt7z2vrfe0ya66vznknfn
let keystore = mkKeystore
[ (addr0, (xprv0, pwd0))
, (addr1, (xprv1, pwd1))
, (addr2, (xprv2, pwd2))
]
-- jcli transaction new \
-- | jcli transaction add-input $TXIN0 0 10000 \
-- | jcli transaction add-output $ADDR1 14 \
-- | jcli transaction finalize $ADDR2 \
-- > tx
--
-- echo $XPRV0 \
-- | jcli key from-bytes --type ed25519Extended \
-- | jcli transaction make-witness $(jcli transaction id --staging tx) \
-- --genesis-block-hash $BLOCK0 --type utxo > wit0.bin
--
-- cat tx \
-- | jcli transaction add-witness wit0.bin \
-- | jcli transaction seal \
-- | jcli transaction to-message
goldenTestStdTx proxy keystore block0
[ (TxIn txin0 0, TxOut addr0 (Coin 10000)) ]
[ TxOut addr1 (Coin 14)
, TxOut addr2 (Coin 9986)
]
"00bf020102000000000000002710666984dec4bc0ff1888be97bfe0694a96b35c5\
\8d025405ead51d5cc72a3019f403b2ddcfbf631000216a6e0410e64c20e6af91bb\
\b2826276ad6bfda777e766b24b000000000000000e0302ada8baca6d3a889a893d\
\a29aa8a291460d7ec95b36404bf094c1a72f2775a6000000000000270201bf524c\
\8f2be5b1adb7f4b6de2cfd90e226f48fa1d1943bda8e6f0140f8cf7f3a6f4b3797\
\649b2bc92fa7e3cb6c32f2299899ba7cbd18abb8252786cd212a6f08"
-- jcli transaction new \
-- | jcli transaction add-input $TXIN0 0 10000 \
-- | jcli transaction add-input $TXIN1 1 999999999 \
-- | jcli transaction add-output $ADDR0 99999 \
-- | jcli transaction add-output $ADDR1 42 \
-- | jcli transaction add-output $ADDR2 1337 \
-- | jcli transaction finalize \
-- > tx
--
-- echo $XPRV0 \
-- | jcli key from-bytes --type ed25519Extended \
-- | jcli transaction make-witness $(jcli transaction id --staging tx) \
-- --genesis-block-hash $BLOCK0 --type utxo > wit0.bin
--
-- echo $XPRV1 \
-- | jcli key from-bytes --type ed25519Extended \
-- | jcli transaction make-witness $(jcli transaction id --staging tx) \
-- --genesis-block-hash $BLOCK0 --type utxo > wit1.bin
--
-- cat tx \
-- | jcli transaction add-witness wit0.bin \
-- | jcli transaction add-witness wit1.bin \
-- | jcli transaction seal \
-- | jcli transaction to-message
goldenTestStdTx proxy keystore block0
[ (TxIn txin0 0, TxOut addr0 (Coin 10000))
, (TxIn txin1 1, TxOut addr1 (Coin 999999999))
]
[ TxOut addr0 (Coin 99999)
, TxOut addr1 (Coin 42)
, TxOut addr2 (Coin 1337)
]
"0152020203000000000000002710666984dec4bc0ff1888be97bfe0694a96b35c5\
\8d025405ead51d5cc72a3019f401000000003b9ac9ff1323856bc91c49e928f6f3\
\0f4e8d665d53eb4ab6028bd0ac971809d514c92db1032d155d07184387ba6ff883\
\6014ba0b626b1808e67e75dfa370c2dc5f581715fe000000000001869f03b2ddcf\
\bf631000216a6e0410e64c20e6af91bbb2826276ad6bfda777e766b24b00000000\
\0000002a0302ada8baca6d3a889a893da29aa8a291460d7ec95b36404bf094c1a7\
\2f2775a600000000000005390141a47dc3e1246215cd4f78b82444a623c507a1de\
\d0eb74f641cc77879fc99d6464ac64b63c6ea3a678da99e0028e6de97ad72c5f92\
\19624f873c7ca4fdb15209012399890d409217c80744dd7a1409d5d53ca735e4bc\
\044b712c59e612858993b95b47ec7d94ded00044a34fdd9686fe3dea3a62bbf7eb\
\1ddc2a576bd5e1133207"
describe "mkStdTx 'Testnet" $ do
let proxy = Proxy @(Jormungandr 'Testnet)
let keyToAddress' = keyToAddress @(Jormungandr 'Testnet)
let addr0 = keyToAddress' (publicKey xprv0)
-- ^ ta1svk32hg8rppc0wn0lzpkq996pd3xkxqguel8tharwrpdch6czu2luue5k36
let addr1 = keyToAddress' (publicKey xprv1)
-- ^ ta1swedmnalvvgqqgt2dczppejvyrn2lydmk2pxya4dd076wal8v6eykfpz5qu
let addr2 = keyToAddress' (publicKey xprv2)
-- ^ ta1svp2m296efkn4zy63y769x4g52g5vrt7e9dnvszt7z2vrfe0ya66vfmfxyf
let keystore = mkKeystore
[ (addr0, (xprv0, pwd0))
, (addr1, (xprv1, pwd1))
, (addr2, (xprv2, pwd2))
]
-- See 'Mainnet description
goldenTestStdTx proxy keystore block0
[ (TxIn txin0 0, TxOut addr0 (Coin 10000)) ]
[ TxOut addr1 (Coin 14)
, TxOut addr2 (Coin 9986)
]
"00bf020102000000000000002710666984dec4bc0ff1888be97bfe0694a96b35c5\
\8d025405ead51d5cc72a3019f483b2ddcfbf631000216a6e0410e64c20e6af91bb\
\b2826276ad6bfda777e766b24b000000000000000e8302ada8baca6d3a889a893d\
\a29aa8a291460d7ec95b36404bf094c1a72f2775a60000000000002702017eeb55\
\f63214db6bed534d2a623e2b6c2d9b8e54d68c8d77b0328d84d2e7de816046e3ae\
\56d252a9284f96b512159593f469b771e211348e1c1b4deee987b606"
-- See 'Mainnet description
goldenTestStdTx proxy keystore block0
[ (TxIn txin0 0, TxOut addr0 (Coin 10000))
, (TxIn txin1 1, TxOut addr1 (Coin 999999999))
]
[ TxOut addr0 (Coin 99999)
, TxOut addr1 (Coin 42)
, TxOut addr2 (Coin 1337)
]
"0152020203000000000000002710666984dec4bc0ff1888be97bfe0694a96b35c5\
\8d025405ead51d5cc72a3019f401000000003b9ac9ff1323856bc91c49e928f6f3\
\0f4e8d665d53eb4ab6028bd0ac971809d514c92db1832d155d07184387ba6ff883\
\6014ba0b626b1808e67e75dfa370c2dc5f581715fe000000000001869f83b2ddcf\
\bf631000216a6e0410e64c20e6af91bbb2826276ad6bfda777e766b24b00000000\
\0000002a8302ada8baca6d3a889a893da29aa8a291460d7ec95b36404bf094c1a7\
\2f2775a60000000000000539012334a13af31af13490fa3da215f0aa14731ccac7\
\2573f28d0778525e847b3700824548dfa2cdddc0a71376de9b7a4867ac93a4dfcf\
\d51516e63cbf23fa59810c01ecad1eec04b45f8a0405b38e242f05868d339ca91d\
\9cebdc18021656f17413e35efed90cc701c5066ea423c16a2e2b85ad2fc314ad6b\
\ff21f5f9ebc2d2a5e201"
goldenTestStdTx
:: forall (n :: Network). ()
=> Proxy (Jormungandr n)
-> (Address -> Maybe (Key 'AddressK XPrv, Passphrase "encryption"))
-> Hash "Genesis"
-> [(TxIn, TxOut)]
-> [TxOut]
-> ByteString
-> SpecWith ()
goldenTestStdTx _ keystore block0 inps outs bytes' = it title $ do
let tx = mkStdTx tl keystore inps outs
let bytes = hex . BL.toStrict . runPut . putSignedTx <$> tx
bytes `shouldBe` Right bytes'
where
tl = newTransactionLayer @n block0
title = "golden test mkStdTx: " <> show inps <> show outs
xprvFromSeed
:: ByteString
-> (Key depth XPrv, Passphrase "encryption")
xprvFromSeed seed =
( unsafeGenerateKeyFromSeed (Passphrase (BA.convert seed), mempty) pwd
, pwd
)
where
pwd = mempty
mkKeystore :: Ord k => [(k,v)] -> (k -> Maybe v)
mkKeystore pairs k =
Map.lookup k (Map.fromList pairs)
hex :: ByteString -> ByteString
hex = convertToBase Base16