-
Notifications
You must be signed in to change notification settings - Fork 213
/
BinarySpec.hs
319 lines (287 loc) · 10.4 KB
/
BinarySpec.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
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RankNTypes #-}
module Cardano.Wallet.BinarySpec
( spec
-- * Helpers
, unsafeDeserialiseFromBytes
) where
import Prelude
import Cardano.Wallet.Binary
( TxWitness (PublicKeyWitness)
, decodeBlock
, decodeBlockHeader
, decodeSignedTx
, decodeTx
, decodeTxWitness
, encodeSignedTx
, encodeTx
, encodeTxWitness
, txId
)
import Cardano.Wallet.Primitive.Types
( Address (..)
, Block (..)
, BlockHeader (..)
, Coin (..)
, Hash (..)
, SlotId (..)
, Tx (..)
, TxIn (..)
, TxOut (..)
)
import Data.ByteArray.Encoding
( Base (Base16), convertFromBase )
import Data.ByteString
( ByteString )
import Data.ByteString.Base58
( bitcoinAlphabet, decodeBase58 )
import Data.Either
( isLeft )
import Test.Hspec
( Expectation, HasCallStack, Spec, describe, it, shouldBe, shouldSatisfy )
import qualified Codec.CBOR.Decoding as CBOR
import qualified Codec.CBOR.Encoding as CBOR
import qualified Codec.CBOR.Read as CBOR
import qualified Codec.CBOR.Write as CBOR
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString.Lazy.Char8 as L8
import qualified Data.Set as Set
{-# ANN spec ("HLint: ignore Use head" :: String) #-}
spec :: Spec
spec = do
describe "Decoding blocks" $ do
it "should decode a block header" $ do
bs <- L8.readFile
"test/data/Cardano/Wallet/BinarySpec-block-header-1"
let decoded = unsafeDeserialiseFromBytes decodeBlockHeader bs
decoded `shouldBe` blockHeader1
it "should decode a block without transactions" $ do
bs <- L8.readFile "test/data/Cardano/Wallet/BinarySpec-block-1"
let decoded = unsafeDeserialiseFromBytes decodeBlock bs
decoded `shouldBe` block1
it "should decode a block with transactions" $ do
bs <- L8.readFile "test/data/Cardano/Wallet/BinarySpec-block-2"
let decoded = unsafeDeserialiseFromBytes decodeBlock bs
decoded `shouldBe` block2
it "should decode a testnet block with a transaction" $ do
bs <- L8.readFile "test/data/Cardano/Wallet/BinarySpec-block-3"
let decoded = unsafeDeserialiseFromBytes decodeBlock bs
decoded `shouldBe` block3
it "should decode a block with many transactions" $ do
bs <- L8.readFile "test/data/Cardano/Wallet/BinarySpec-block-4"
let decoded = unsafeDeserialiseFromBytes decodeBlock bs
decoded `shouldBe` block4
it "should fail to decode a junk block" $ do
let junk = mconcat (replicate 100 "junk")
decoded = CBOR.deserialiseFromBytes decodeBlock junk
decoded `shouldSatisfy` isLeft
it "should fail to decode a block with block header data" $ do
bs <- L8.readFile
"test/data/Cardano/Wallet/BinarySpec-block-header-1"
let decoded = CBOR.deserialiseFromBytes decodeBlock bs
decoded `shouldBe`
Left (CBOR.DeserialiseFailure 3 "expected list of length 3")
describe "Encoding Tx" $ do
let txs = Set.toList (transactions block2 <> transactions block3)
let roundTripTx tx = do
let bytes = CBOR.toLazyByteString (encodeTx tx)
let tx' = unsafeDeserialiseFromBytes decodeTx bytes
tx `shouldBe` tx'
it "encode . decode = pure (1)" $ do
roundTripTx (txs !! 0)
it "encode . decode = pure (2)" $ do
roundTripTx (txs !! 1)
it "should compute correct txId (1)" $ do
let hash = txId (txs !! 0)
let hash' = hash16
"c470563001e448e61ff1268c2a6eb458\
\ace1d04011a02cb262b6d709d66c23d0"
hash `shouldBe` hash'
it "should compute correct txId (2)" $ do
let hash = txId (txs !! 1)
let hash' = hash16
"d30d37f1f8674c6c33052826fdc5bc19\
\8e3e95c150364fd775d4bc663ae6a9e6"
hash `shouldBe` hash'
let pkWit = PublicKeyWitness "Trust me"
describe "Encoding Tx Witness" $ do
it "(encode . decode) = pure" $ do
cborRoundtrip
decodeTxWitness
encodeTxWitness
pkWit
describe "Encoding Signed Tx" $ do
let txs = Set.toList (transactions block2 <> transactions block3)
it "(encode . decode) = pure" $ do
cborRoundtrip
decodeSignedTx
encodeSignedTx
(txs !! 0, [pkWit])
cborRoundtrip
:: (HasCallStack, Show a, Eq a)
=> (forall s. CBOR.Decoder s a)
-> (a -> CBOR.Encoding)
-> a
-> Expectation
cborRoundtrip decode encode a = do
let bytes = CBOR.toLazyByteString $ encode a
let a' = unsafeDeserialiseFromBytes decode bytes
a `shouldBe` a'
-- A mainnet block header
blockHeader1 :: BlockHeader
blockHeader1 = BlockHeader
{ slotId = SlotId 105 9520
, prevBlockHash = hash16
"9f3c67b575bf2c5638291949694849d6ce5d29efa1f2eb3ed0beb6dac262e9e0"
}
-- A mainnet block
block1 :: Block
block1 = Block
{ header = BlockHeader
{ slotId = SlotId 105 9519
, prevBlockHash = prevBlockHash0
}
, transactions = mempty
}
where
prevBlockHash0 = hash16
"4d97da40fb62bec847d6123762e82f9325f11d0c8e89deee0c7dbb598ed5f0cf"
-- A mainnet block with a transaction
block2 :: Block
block2 = Block
{ header = BlockHeader
{ slotId = SlotId 105 9876
, prevBlockHash = prevBlockHash0
}
, transactions = Set.fromList
[ Tx
{ inputs =
[ TxIn { inputId = inputId0, inputIx = 3 } ]
, outputs =
[ TxOut { address = address0, coin = Coin 285000000 }
, TxOut { address = address1, coin = Coin 1810771919 } ]
}
]
}
where
prevBlockHash0 = hash16
"da73001193ab3e6a43921385941c5f96b8f56de3908e78fae06f038b91dadd9d"
inputId0 = hash16
"60dbb2679ee920540c18195a3d92ee9be50aee6ed5f891d92d51db8a76b02cd2"
address0 = addr58
"DdzFFzCqrhsug8jKBMV5Cr94hKY4DrbJtkUpqptoGEkovR2QSkcA\
\cRgjnUyegE689qBX6b2kyxyNvCL6mfqiarzRB9TRq8zwJphR31pr"
address1 = addr58
"DdzFFzCqrhsmxmuQpgjUrvRwF5ZKnyQ7pGrS4q53u5B516wcc26m\
\aHz9M4myYAkQVc5m9E4DKJjRDjPxuDdK3ZsHb1Dnqf3XorZ1PnzX"
-- A testnet block with a transaction
block3 :: Block
block3 = Block
{ header = BlockHeader
{ slotId = SlotId 30 9278
, prevBlockHash = prevBlockHash0
}
, transactions = Set.fromList
[ Tx
{ inputs =
[ TxIn { inputId = inputId0, inputIx = 1 }
, TxIn { inputId = inputId1, inputIx = 0 } ]
, outputs =
[ TxOut { address = address0, coin = Coin 1404176490 }
, TxOut { address = address1, coin = Coin 1004099328 } ]
}
]
}
where
prevBlockHash0 = hash16
"b065b5fe97bec5fd130e7a639189499c9d0b1fcf9348c5c19f7a22700da7a35e"
inputId0 = hash16
"6967e2b5c3ad5ae07a9bd8d888f1836195a04f7a1cb4b6d083261870068fab1b"
inputId1 = hash16
"7064addc0968bccd7d57d2e7aa1e9c2f666d8387042483fc1e87200cfb96c8f1"
address0 = addr58
"37btjrVyb4KBsw2f3V76ntfwqDPgyf3QmmdsrTSmCnuTGYtS9JgVXzxeQ\
\EsKjgWurKoyw9BDNEtLxWtU9znK49SC8bLTirk6YqcAESFxXJkSyXhQKL"
address1 = addr58
"37btjrVyb4KD5Ne4yvGAHGbQuHUYQX1VPsXh85rBh3UrGSMWdRSFxBYQ9\
\RQRHCMezN6AMLd3uYTC5hbeVTUiPzfQUTCEogg2HrSJKQUjAgsoYZHwT3"
-- A mainnet block with multiple transactions
block4 :: Block
block4 = Block
{ header = BlockHeader
{ slotId = SlotId 14 18
, prevBlockHash = prevBlockHash0
}
, transactions = Set.fromList
[ Tx
{ inputs =
[ TxIn
{ inputId = inputId0
, inputIx = 0
}
]
, outputs =
[ TxOut
{ address = addr0
, coin = Coin 3841254542346
}
, TxOut
{ address = addr1
, coin = Coin 2700667457
}
]
}
, Tx
{ inputs =
[ TxIn
{ inputId = inputId1
, inputIx = 0
}
]
, outputs =
[ TxOut
{ address = addr2
, coin = Coin 3832107959251
}
, TxOut
{ address = addr3
, coin = Coin 11823271860
}
]
}
]
}
where
prevBlockHash0 = hash16
"f4283844eb78ca6f6333b007f5a735d71499d6ce7cc816846a033a36784bd299"
inputId0 = hash16
"f91292301d4bb1b6e040cecdff4030959b49c95e7dae087782dd558bebb6668a"
addr0 = addr58
"DdzFFzCqrhss1h6EV6KqcSkJNoC2UdtnuP1gGsoxT5Gv8GPfsReX\
\8QhVZWXeZLTidYPi3Fu5ZXG4gvfq3zbwD4nboD1HxoCPCFJLWpMc"
addr1 = addr58
"DdzFFzCqrhszy7cUVphbFGaPBG7yn5nNPzjGDYaPsPuEG84KRjtK\
\RhhNhwjNENx8LT9XGbYfAJzptwotbp7ySho5LGeCc2ALq3cQX2JM"
inputId1 = hash16
"96e170491afb6ebd579fd57c76c684f22436f8cc3a912397ddb1c9c51b86fb53"
addr2 = addr58
"DdzFFzCqrht5ZoBwGocznhhpr4yXRWy1RaMqguNBPjVGhwZcNMnh\
\sDUtEETzjQVVc1TBuL3en6yA8JcKVXx1cuoea5rozjcaFid3pkV7"
addr3 = addr58
"DdzFFzCqrhsoLchqT8AwxFH9srQzvH78dUAD1BwHneqGHvA7BV89\
\Mj87XFPDSU2tJCMiWpi7vf1U5CqE835Xz2kpnyzFTuYQLkZev4qw"
-- * Helpers
-- | Make a Hash from a Base16 encoded string, without error handling.
hash16 :: ByteString -> Hash a
hash16 = either bomb Hash . convertFromBase Base16
where
bomb msg = error ("Could not decode test string: " <> msg)
-- | Make an Address from a Base58 encoded string, without error handling.
addr58 :: ByteString -> Address
addr58 = maybe (error "addr58: Could not decode") Address
. decodeBase58 bitcoinAlphabet
-- | CBOR deserialise without error handling - handy for prototypes or testing.
unsafeDeserialiseFromBytes :: (forall s. CBOR.Decoder s a) -> BL.ByteString -> a
unsafeDeserialiseFromBytes decoder bytes =
either (\e -> error $ "unsafeDeserialiseFromBytes: " <> show e) snd $
CBOR.deserialiseFromBytes decoder bytes