-
Notifications
You must be signed in to change notification settings - Fork 155
/
CompactAddress.hs
751 lines (670 loc) · 27.2 KB
/
CompactAddress.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
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE BinaryLiterals #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module Cardano.Ledger.CompactAddress
( compactAddr,
decompactAddr,
CompactAddr (..),
substring,
isPayCredScriptCompactAddr,
isBootstrapCompactAddr,
-- Faster Address deserialization
decodeAddr,
decodeAddrShort,
decodeAddrEither,
decodeAddrShortEither,
fromCborAddr,
fromCborBothAddr,
fromCborCompactAddr,
fromCborBackwardsBothAddr,
decodeRewardAcnt,
fromCborRewardAcnt,
-- * Exported for benchmarking only
fromCborCompactAddrOld,
decompactAddrLazy,
)
where
import Cardano.Binary
( Decoder,
DecoderError (..),
FromCBOR (..),
ToCBOR (..),
decodeFull',
)
import qualified Cardano.Crypto.Hash.Class as Hash
import Cardano.Ledger.Address
( Addr (..),
BootstrapAddress (..),
RewardAcnt (..),
Word7 (..),
byron,
getAddr,
isEnterpriseAddr,
notBaseAddr,
payCredIsScript,
serialiseAddr,
stakeCredIsScript,
toWord7,
word7sToWord64,
)
import Cardano.Ledger.BaseTypes (CertIx (..), Network (..), TxIx (..), word8ToNetwork)
import Cardano.Ledger.Credential
( Credential (KeyHashObj, ScriptHashObj),
PaymentCredential,
Ptr (..),
StakeReference (..),
)
import Cardano.Ledger.Crypto (ADDRHASH)
import qualified Cardano.Ledger.Crypto as CC (Crypto)
import Cardano.Ledger.Hashes (ScriptHash (..))
import Cardano.Ledger.Keys (KeyHash (..))
import Cardano.Ledger.Slot (SlotNo (..))
import Control.DeepSeq (NFData)
import Control.Monad (ap, guard, unless, when)
import qualified Control.Monad.Fail
import Control.Monad.Trans.State (StateT, evalStateT, get, modify', state)
import qualified Data.Binary.Get as B
import Data.Bits (Bits, clearBit, shiftL, testBit, (.&.), (.|.))
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BSL
import Data.ByteString.Short as SBS (fromShort, index, length, toShort)
import Data.ByteString.Short.Internal as SBS (ShortByteString (SBS), unsafeIndex)
import qualified Data.ByteString.Unsafe as BS (unsafeDrop, unsafeIndex)
import Data.Coders (cborError)
import Data.Maybe (fromMaybe)
import qualified Data.Primitive.ByteArray as BA
import Data.Proxy (Proxy (..))
import Data.String (fromString)
import Data.Text (Text)
import Data.Word (Word16, Word32, Word64, Word8)
import GHC.Show (intToDigit)
import Numeric (showIntAtBase)
newtype CompactAddr crypto = UnsafeCompactAddr ShortByteString
deriving (Eq, Ord, NFData)
instance CC.Crypto c => Show (CompactAddr c) where
show c = show (decompactAddr c)
compactAddr :: Addr crypto -> CompactAddr crypto
compactAddr = UnsafeCompactAddr . SBS.toShort . serialiseAddr
{-# INLINE compactAddr #-}
decompactAddr :: forall crypto. CC.Crypto crypto => CompactAddr crypto -> Addr crypto
decompactAddr (UnsafeCompactAddr sbs) =
case decodeAddrShort sbs of
Just addr -> addr
Nothing -> decompactAddrOld sbs
{-# INLINE decompactAddr #-}
decompactAddrOld :: CC.Crypto crypto => ShortByteString -> Addr crypto
decompactAddrOld short = snd . unwrap "CompactAddr" $ runGetShort getShortAddr 0 short
where
-- The reason failure is impossible here is that the only way to call this code
-- is using a CompactAddr, which can only be constructed using compactAddr.
-- compactAddr serializes an Addr, so this is guaranteed to work.
unwrap :: forall a. Text -> Maybe a -> a
unwrap name = fromMaybe (error $ "Impossible failure when decoding " <> name)
{-# NOINLINE decompactAddrOld #-}
------------------------------------------------------------------------------------------
-- Fast Address Serializer ---------------------------------------------------------------
------------------------------------------------------------------------------------------
fromCborAddr :: forall crypto s. CC.Crypto crypto => Decoder s (Addr crypto)
fromCborAddr = do
sbs <- fromCBOR
decodeAddrShort @crypto sbs
{-# INLINE fromCborAddr #-}
fromCborBothAddr :: forall crypto s. CC.Crypto crypto => Decoder s (Addr crypto, CompactAddr crypto)
fromCborBothAddr = do
sbs <- fromCBOR
addr <- decodeAddrShort @crypto sbs
pure (addr, UnsafeCompactAddr sbs)
{-# INLINE fromCborBothAddr #-}
fromCborCompactAddr :: forall crypto s. CC.Crypto crypto => Decoder s (CompactAddr crypto)
fromCborCompactAddr = do
-- Ensure bytes can be decoded as Addr
(_addr, cAddr) <- fromCborBothAddr
pure cAddr
{-# INLINE fromCborCompactAddr #-}
-- This is a fallback deserializer that preserves old behavior. It will almost never be
-- invoked, that is why it is not inlined.
fromCborAddrFallback :: CC.Crypto crypto => ShortByteString -> Decoder s (Addr crypto)
fromCborAddrFallback sbs =
case B.runGetOrFail getAddr $ BSL.fromStrict $ SBS.fromShort sbs of
Right (_remaining, _offset, value) -> pure value
Left (_remaining, _offset, message) ->
cborError (DecoderErrorCustom "Addr" $ fromString message)
{-# NOINLINE fromCborAddrFallback #-}
fromCborCompactAddrOld :: forall s crypto. CC.Crypto crypto => Decoder s (CompactAddr crypto)
fromCborCompactAddrOld = do
sbs <- fromCBOR
UnsafeCompactAddr sbs <$ fromCborAddrFallback @crypto sbs
{-# INLINE fromCborCompactAddrOld #-}
fromCborBackwardsBothAddr ::
forall crypto s.
CC.Crypto crypto =>
Decoder s (Addr crypto, CompactAddr crypto)
fromCborBackwardsBothAddr = do
sbs <- fromCBOR
addr <-
case decodeAddrShortEither @crypto sbs of
Right a -> pure a
Left _err -> fromCborAddrFallback sbs
pure (addr, UnsafeCompactAddr sbs)
{-# INLINE fromCborBackwardsBothAddr #-}
class AddressBuffer b where
bufLength :: b -> Int
bufUnsafeIndex :: b -> Int -> Word8
bufToByteString :: b -> BS.ByteString
bufGetHash :: Hash.HashAlgorithm h => b -> Int -> Maybe (Hash.Hash h a)
instance AddressBuffer ShortByteString where
bufLength = SBS.length
{-# INLINE bufLength #-}
bufUnsafeIndex = SBS.unsafeIndex
{-# INLINE bufUnsafeIndex #-}
bufToByteString = SBS.fromShort
{-# INLINE bufToByteString #-}
bufGetHash = Hash.hashFromOffsetBytesShort
{-# INLINE bufGetHash #-}
instance AddressBuffer BS.ByteString where
bufLength = BS.length
{-# INLINE bufLength #-}
bufUnsafeIndex = BS.unsafeIndex
{-# INLINE bufUnsafeIndex #-}
bufToByteString = id
{-# INLINE bufToByteString #-}
bufGetHash bs offset = do
guard (offset >= 0 && offset < BS.length bs)
Hash.hashFromBytes (BS.unsafeDrop offset bs)
{-# INLINE bufGetHash #-}
-- | Address header byte truth table:
newtype Header = Header Word8
deriving (Eq, Ord, Bits, Num)
instance Show Header where
show (Header header) = ("0b" ++) . showIntAtBase 2 intToDigit header $ ""
-- | Every Byron address starts with @[TkListLen 2]@, which encodes as 130 (or 0x80)
headerByron :: Header
headerByron = 0b10000010 -- 0x80
isByronAddress :: Header -> Bool
isByronAddress = (== headerByron)
{-# INLINE isByronAddress #-}
headerNonShelleyBits :: Header
headerNonShelleyBits = headerByron .|. 0b00001100
headerNetworkId :: Header -> Network
headerNetworkId header
| header `testBit` 0 = Mainnet
| otherwise = Testnet
{-# INLINE headerNetworkId #-}
headerIsPaymentScript :: Header -> Bool
headerIsPaymentScript = (`testBit` 4)
{-# INLINE headerIsPaymentScript #-}
headerIsEnterpriseAddr :: Header -> Bool
headerIsEnterpriseAddr = (`testBit` 5)
{-# INLINE headerIsEnterpriseAddr #-}
headerIsStakingScript :: Header -> Bool
headerIsStakingScript = (`testBit` 5)
{-# INLINE headerIsStakingScript #-}
headerIsBaseAddress :: Header -> Bool
headerIsBaseAddress = not . (`testBit` 6)
{-# INLINE headerIsBaseAddress #-}
newtype Fail a = Fail {runFail :: Either String a}
deriving (Functor, Applicative, Monad)
instance MonadFail Fail where
fail = Fail . Left
decodeAddrEither ::
forall crypto.
CC.Crypto crypto =>
BS.ByteString ->
Either String (Addr crypto)
decodeAddrEither sbs = runFail $ evalStateT (decodeAddrStateT sbs) 0
{-# INLINE decodeAddrEither #-}
decodeAddrShortEither ::
forall crypto.
CC.Crypto crypto =>
ShortByteString ->
Either String (Addr crypto)
decodeAddrShortEither sbs = runFail $ evalStateT (decodeAddrStateT sbs) 0
{-# INLINE decodeAddrShortEither #-}
decodeAddrShort ::
forall crypto m.
(CC.Crypto crypto, MonadFail m) =>
ShortByteString ->
m (Addr crypto)
decodeAddrShort sbs = evalStateT (decodeAddrStateT sbs) 0
{-# INLINE decodeAddrShort #-}
decodeAddr ::
forall crypto m.
(CC.Crypto crypto, MonadFail m) =>
BS.ByteString ->
m (Addr crypto)
decodeAddr sbs = evalStateT (decodeAddrStateT sbs) 0
{-# INLINE decodeAddr #-}
-- | While decoding an Addr the header (the first byte in the buffer) is
-- expected to be in a certain format. Here are the meaning of all the bits:
--
-- @@@
--
-- ┏━━━━━━━━━━━━━━━━┳━┯━┯━┯━┯━┯━┯━┯━┓
-- ┃ Byron Address ┃1┊0┊0┊0┊0┊0┊1┊0┃
-- ┣━━━━━━━━━━━━━━━━╋━┿━┿━┿━┿━┿━┿━┿━┫
-- ┃Shelley Address ┃0┊x┊x┊x┊0┊0┊0┊x┃
-- ┗━━━━━━━━━━━━━━━━╋━┿━┿━┿━┿━┿━┿━┿━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
-- ┃0┊0┊0┊0┊0┊0┊0┊0┃ Testnet PaymentKey StakingKey ┃
-- ┃0┊0┊0┊0┊0┊0┊0┊1┃ Mainnet PaymentKey StakingKey ┃
-- ┃0┊0┊0┊1┊0┊0┊0┊0┃ Testnet PaymentScript StakingKey ┃
-- ┃0┊0┊0┊1┊0┊0┊0┊1┃ Mainnet PaymentScript StakingKey ┃
-- ┃0┊0┊1┊0┊0┊0┊0┊0┃ Testnet PaymentKey StakingScript ┃
-- ┃0┊0┊1┊0┊0┊0┊0┊1┃ Mainnet PaymentKey StakingScript ┃
-- ┃0┊0┊1┊1┊0┊0┊0┊0┃ Testnet PaymentScript StakingScript ┃
-- ┃0┊0┊1┊1┊0┊0┊0┊1┃ Mainnet PaymentScript StakingScript ┃
-- ┃0┊1┊0┊0┊0┊0┊0┊0┃ Testnet PaymentKey StakingPtr ┃
-- ┃0┊1┊0┊0┊0┊0┊0┊1┃ Mainnet PaymentKey StakingPtr ┃
-- ┃0┊1┊0┊1┊0┊0┊0┊0┃ Testnet PaymentScript StakingPtr ┃
-- ┃0┊1┊0┊1┊0┊0┊0┊1┃ Mainnet PaymentScript StakingPtr ┃
-- ┃0┊1┊1┊0┊0┊0┊0┊0┃ Testnet PaymentKey StakingNull ┃
-- ┃0┊1┊1┊0┊0┊0┊0┊1┃ Mainnet PaymentKey StakingNull ┃
-- ┃0┊1┊1┊1┊0┊0┊0┊0┃ Testnet PaymentScript StakingNull ┃
-- ┃0┊1┊1┊1┊0┊0┊0┊1┃ Mainnet PaymentScript StakingNull ┃
-- ┗━┷━┷━┷━┷━┷━┷━┷━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
-- \ \ \ \
-- \ \ \ `Is Mainnet Address
-- \ \ `Payment Credential is a Script
-- \ `Staking Credential is a Script / No Staking Credential
-- `Not a Base Address
-- @@@
decodeAddrStateT ::
(CC.Crypto crypto, MonadFail m, AddressBuffer b) =>
b ->
StateT Int m (Addr crypto)
decodeAddrStateT buf = do
guardLength "Header" 1 buf
let header = Header $ bufUnsafeIndex buf 0
addr <-
if isByronAddress header
then AddrBootstrap <$> decodeBootstrapAddress buf
else do
-- Ensure there are no unexpected bytes in the header
unless (header .&. headerNonShelleyBits == 0) $
failDecoding
"Shelley Address"
$ "Invalid header. Unused bits are not suppose to be set: " <> show header
-- Advance one byte for the consumed header
modify' (+ 1)
payment <- decodePaymentCredential header buf
staking <- decodeStakeReference header buf
pure $ Addr (headerNetworkId header) payment staking
addr <$ ensureBufIsConsumed "Addr" buf
{-# INLINE decodeAddrStateT #-}
-- | Checks that the current offset is exactly at the end of the buffer.
ensureBufIsConsumed ::
forall m b.
(MonadFail m, AddressBuffer b) =>
-- | Name for error reporting
String ->
-- | Buffer that should have been consumed.
b ->
StateT Int m ()
ensureBufIsConsumed name buf = do
lastOffset <- get
let len = bufLength buf
unless (lastOffset == len) $
failDecoding name $ "Left over bytes: " ++ show (len - lastOffset)
{-# INLINE ensureBufIsConsumed #-}
-- | This decoder assumes the whole `ShortByteString` is occupied by the `BootstrapAddress`
decodeBootstrapAddress ::
forall crypto m b.
(MonadFail m, AddressBuffer b) =>
b ->
StateT Int m (BootstrapAddress crypto)
decodeBootstrapAddress buf =
case decodeFull' $ bufToByteString buf of
Left e -> fail $ show e
Right addr -> BootstrapAddress addr <$ modify' (+ bufLength buf)
{-# INLINE decodeBootstrapAddress #-}
decodePaymentCredential ::
(CC.Crypto crypto, MonadFail m, AddressBuffer b) =>
Header ->
b ->
StateT Int m (PaymentCredential crypto)
decodePaymentCredential header buf
| headerIsPaymentScript header = ScriptHashObj <$> decodeScriptHash buf
| otherwise = KeyHashObj <$> decodeKeyHash buf
{-# INLINE decodePaymentCredential #-}
decodeStakeReference ::
(CC.Crypto crypto, MonadFail m, AddressBuffer b) =>
Header ->
b ->
StateT Int m (StakeReference crypto)
decodeStakeReference header buf
| headerIsBaseAddress header =
if headerIsStakingScript header
then StakeRefBase . ScriptHashObj <$> decodeScriptHash buf
else StakeRefBase . KeyHashObj <$> decodeKeyHash buf
| otherwise =
if headerIsEnterpriseAddr header
then pure StakeRefNull
else StakeRefPtr <$> decodePtr buf
{-# INLINE decodeStakeReference #-}
decodeKeyHash ::
(CC.Crypto crypto, MonadFail m, AddressBuffer b) =>
b ->
StateT Int m (KeyHash kr crypto)
decodeKeyHash buf = KeyHash <$> decodeHash buf
{-# INLINE decodeKeyHash #-}
decodeScriptHash ::
(CC.Crypto crypto, MonadFail m, AddressBuffer b) =>
b ->
StateT Int m (ScriptHash crypto)
decodeScriptHash buf = ScriptHash <$> decodeHash buf
{-# INLINE decodeScriptHash #-}
decodeHash ::
forall a h m b.
(Hash.HashAlgorithm h, MonadFail m, AddressBuffer b) =>
b ->
StateT Int m (Hash.Hash h a)
decodeHash buf = do
offset <- get
case bufGetHash buf offset of
Just h -> h <$ modify' (+ hashLen)
Nothing
| offset >= 0 ->
failDecoding "Hash" $
"Not enough bytes supplied: "
++ show (bufLength buf - offset)
++ ". Expected: "
++ show hashLen
Nothing -> fail "Impossible: Negative offset"
where
hashLen :: Int
hashLen = fromIntegral (Hash.sizeHash (Proxy :: Proxy h))
{-# INLINE decodeHash #-}
decodePtr ::
(MonadFail m, AddressBuffer b) =>
b ->
StateT Int m Ptr
decodePtr buf =
Ptr
<$> (SlotNo . (fromIntegral :: Word32 -> Word64) <$> decodeVariableLengthWord32 "SlotNo" buf)
<*> (TxIx . (fromIntegral :: Word16 -> Word64) <$> decodeVariableLengthWord16 "TxIx" buf)
<*> (CertIx . (fromIntegral :: Word16 -> Word64) <$> decodeVariableLengthWord16 "CertIx" buf)
{-# INLINE decodePtr #-}
guardLength ::
(MonadFail m, AddressBuffer b) =>
-- | Name for what is being decoded for the error message
String ->
Int ->
b ->
StateT Int m ()
guardLength name expectedLength buf = do
offset <- get
when (offset > bufLength buf - expectedLength) $
failDecoding name "Not enough bytes for decoding"
{-# INLINE guardLength #-}
-- | Decode a variable length integral value that is encoded with 7 bits of data
-- and the most significant bit (MSB), the 8th bit is set whenever there are
-- more bits following. Continuation style allows us to avoid
-- rucursion. Removing loops is good for performance.
decode7BitVarLength ::
(Num a, Bits a, AddressBuffer b, MonadFail m) =>
-- | Name of what is being decoded for error reporting
String ->
-- | Buffer that contains encoded number
b ->
-- | Continuation that will be invoked if MSB is set
(a -> StateT Int m a) ->
-- | Accumulator
a ->
StateT Int m a
decode7BitVarLength name buf cont !acc = do
guardLength name 1 buf
offset <- state (\off -> (off, off + 1))
let b8 = bufUnsafeIndex buf offset
if b8 `testBit` 7
then cont (acc `shiftL` 7 .|. fromIntegral (b8 `clearBit` 7))
else pure (acc `shiftL` 7 .|. fromIntegral b8)
{-# INLINE decode7BitVarLength #-}
failDecoding :: MonadFail m => String -> String -> m a
failDecoding name msg = fail $ "Decoding " ++ name ++ ": " ++ msg
{-# NOINLINE failDecoding #-}
decodeVariableLengthWord16 ::
forall m b.
(MonadFail m, AddressBuffer b) =>
String ->
b ->
StateT Int m Word16
decodeVariableLengthWord16 name buf = do
off0 <- get
let d7 = decode7BitVarLength name buf
d7last :: Word16 -> StateT Int m Word16
d7last acc = do
res <- decode7BitVarLength name buf (\_ -> failDecoding name "too many bytes.") acc
-- Only while decoding the last 7bits we check if there was too many
-- bits supplied at the beginning.
unless (bufUnsafeIndex buf off0 .&. 0b01111100 == 0) $
failDecoding name "More than 16bits was supplied"
pure res
d7 (d7 d7last) 0
{-# INLINE decodeVariableLengthWord16 #-}
decodeVariableLengthWord32 ::
forall m b.
(MonadFail m, AddressBuffer b) =>
String ->
b ->
StateT Int m Word32
decodeVariableLengthWord32 name buf = do
off0 <- get
let d7 = decode7BitVarLength name buf
d7last :: Word32 -> StateT Int m Word32
d7last acc = do
res <- decode7BitVarLength name buf (\_ -> failDecoding name "too many bytes.") acc
-- Only while decoding the last 7bits we check if there was too many
-- bits supplied at the beginning.
unless (bufUnsafeIndex buf off0 .&. 0b01110000 == 0) $
failDecoding name "More than 32bits was supplied"
pure res
d7 (d7 (d7 (d7 d7last))) 0
{-# INLINE decodeVariableLengthWord32 #-}
------------------------------------------------------------------------------------------
-- Reward Account Deserializer -----------------------------------------------------------
------------------------------------------------------------------------------------------
decodeRewardAcnt ::
forall crypto b m.
(CC.Crypto crypto, AddressBuffer b, MonadFail m) =>
b ->
m (RewardAcnt crypto)
decodeRewardAcnt buf = evalStateT (decodeRewardAccountT buf) 0
{-# INLINE decodeRewardAcnt #-}
fromCborRewardAcnt :: forall crypto s. CC.Crypto crypto => Decoder s (RewardAcnt crypto)
fromCborRewardAcnt = do
sbs :: ShortByteString <- fromCBOR
decodeRewardAcnt @crypto sbs
{-# INLINE fromCborRewardAcnt #-}
headerIsRewardAccount :: Header -> Bool
headerIsRewardAccount header = header .&. 0b11101110 == 0b11100000
{-# INLINE headerIsRewardAccount #-}
headerRewardAccountIsScript :: Header -> Bool
headerRewardAccountIsScript = (`testBit` 4)
{-# INLINE headerRewardAccountIsScript #-}
-- | Reward Account Header.
--
-- @@@
--
-- ┏━━━━━━━━━━━━━━━━┳━┯━┯━┯━┯━┯━┯━┯━┓
-- ┃ Reward Account ┃1┊1┊1┊x┊0┊0┊0┊x┃
-- ┗━━━━━━━━━━━━━━━━╋━┿━┿━┿━┿━┿━┿━┿━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
-- ┃1┊1┊1┊0┊0┊0┊0┊0┃ Testnet PaymentKey StakingKey ┃
-- ┃1┊1┊1┊0┊0┊0┊0┊1┃ Mainnet PaymentKey StakingKey ┃
-- ┃1┊1┊1┊1┊0┊0┊0┊0┃ Testnet PaymentScript StakingKey ┃
-- ┃1┊1┊1┊1┊0┊0┊0┊1┃ Mainnet PaymentScript StakingKey ┃
-- ┗━┷━┷━┷━┷━┷━┷━┷━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
-- \ \
-- \ `Is Mainnet Address
-- `Account Credential is a Script
-- @@@
decodeRewardAccountT ::
(MonadFail m, CC.Crypto crypto, AddressBuffer b) =>
b ->
StateT Int m (RewardAcnt crypto)
decodeRewardAccountT buf = do
guardLength "Header" 1 buf
modify' (+ 1)
let header = Header $ bufUnsafeIndex buf 0
unless (headerIsRewardAccount header) $
fail $ "Invalid header for the reward account: " <> show header
account <-
if headerRewardAccountIsScript header
then ScriptHashObj <$> decodeScriptHash buf
else KeyHashObj <$> decodeKeyHash buf
ensureBufIsConsumed "RewardsAcnt" buf
pure $! RewardAcnt (headerNetworkId header) account
{-# INLINE decodeRewardAccountT #-}
------------------------------------------------------------------------------------------
-- Old Address Deserializer --------------------------------------------------------------
------------------------------------------------------------------------------------------
-- | This lazy deserializer is kept around purely for benchmarking, so we can
-- verify that new deserializer `decodeAddrStateT` is doing the work lazily.
decompactAddrLazy :: forall crypto. CC.Crypto crypto => CompactAddr crypto -> Addr crypto
decompactAddrLazy (UnsafeCompactAddr bytes) =
if testBit header byron
then AddrBootstrap $ run "byron address" 0 bytes getBootstrapAddress
else Addr addrNetId paycred stakecred
where
run :: forall a. Text -> Int -> ShortByteString -> GetShort a -> a
run name i sbs g = snd . unwrap name $ runGetShort g i sbs
-- The reason failure is impossible here is that the only way to call this code
-- is using a CompactAddr, which can only be constructed using compactAddr.
-- compactAddr serializes an Addr, so this is guaranteed to work.
unwrap :: forall a. Text -> Maybe a -> a
unwrap name = fromMaybe (error $ "Impossible failure when decoding " <> name)
header = run "address header" 0 bytes getWord
addrNetId =
unwrap "address network id" $
word8ToNetwork $ header .&. 0x0F -- 0b00001111 is the mask for the network id
-- The address format is
-- header | pay cred | stake cred
-- where the header is 1 byte
-- the pay cred is (sizeHash (ADDRHASH crypto)) bytes
-- and the stake cred can vary
paycred = run "payment credential" 1 bytes (getPayCred header)
stakecred = run "staking credential" 1 bytes $ do
skipHash ([] @(ADDRHASH crypto))
getStakeReference header
skipHash :: forall proxy h. Hash.HashAlgorithm h => proxy h -> GetShort ()
skipHash p = skip . fromIntegral $ Hash.sizeHash p
skip :: Int -> GetShort ()
skip n = GetShort $ \i sbs ->
let offsetStop = i + n
in if offsetStop <= SBS.length sbs
then Just (offsetStop, ())
else Nothing
instance CC.Crypto crypto => ToCBOR (CompactAddr crypto) where
toCBOR (UnsafeCompactAddr bytes) = toCBOR bytes
instance CC.Crypto crypto => FromCBOR (CompactAddr crypto) where
fromCBOR = do
(_addr, cAddr) <- fromCborBackwardsBothAddr
pure cAddr
{-# INLINE fromCBOR #-}
newtype GetShort a = GetShort {runGetShort :: Int -> ShortByteString -> Maybe (Int, a)}
deriving (Functor)
instance Applicative GetShort where
pure a = GetShort $ \i _sbs -> Just (i, a)
(<*>) = ap
instance Monad GetShort where
(GetShort g) >>= f = GetShort $ \i sbs ->
case g i sbs of
Nothing -> Nothing
Just (i', x) -> runGetShort (f x) i' sbs
instance Control.Monad.Fail.MonadFail GetShort where
fail _ = GetShort $ \_ _ -> Nothing
getShortAddr :: CC.Crypto crypto => GetShort (Addr crypto)
getShortAddr = do
header <- peekWord8
if testBit header byron
then AddrBootstrap <$> getBootstrapAddress
else do
_ <- getWord -- read past the header byte
let addrNetId = header .&. 0x0F -- 0b00001111 is the mask for the network id
case word8ToNetwork addrNetId of
Just n -> do
c <- getPayCred header
h <- getStakeReference header
pure (Addr n c h)
Nothing ->
fail $
concat
["Address with unknown network Id. (", show addrNetId, ")"]
getBootstrapAddress :: GetShort (BootstrapAddress crypto)
getBootstrapAddress = do
bs <- getRemainingAsByteString
case decodeFull' bs of
Left e -> fail $ show e
Right r -> pure $ BootstrapAddress r
getWord :: GetShort Word8
getWord = GetShort $ \i sbs ->
if i < SBS.length sbs
then Just (i + 1, SBS.index sbs i)
else Nothing
peekWord8 :: GetShort Word8
peekWord8 = GetShort peek
where
peek i sbs = if i < SBS.length sbs then Just (i, SBS.index sbs i) else Nothing
getRemainingAsByteString :: GetShort BS.ByteString
getRemainingAsByteString = GetShort $ \i sbs ->
let l = SBS.length sbs
in if i < l
then Just (l, SBS.fromShort $ substring sbs i l)
else Nothing
getHash :: forall a h. Hash.HashAlgorithm h => GetShort (Hash.Hash h a)
getHash = GetShort $ \i sbs ->
let hashLen = Hash.sizeHash ([] @h)
offsetStop = i + fromIntegral hashLen
in if offsetStop <= SBS.length sbs
then do
hash <- Hash.hashFromBytesShort $ substring sbs i offsetStop
Just (offsetStop, hash)
else Nothing
-- start is the first index copied
-- stop is the index after the last index copied
substring :: ShortByteString -> Int -> Int -> ShortByteString
substring (SBS ba) start stop =
case BA.cloneByteArray (BA.ByteArray ba) start (stop - start) of
BA.ByteArray ba' -> SBS ba'
getWord7s :: GetShort [Word7]
getWord7s = do
next <- getWord
-- is the high bit set?
if testBit next 7
then -- if so, grab more words
(:) (toWord7 next) <$> getWord7s
else -- otherwise, this is the last one
pure [Word7 next]
getVariableLengthWord64 :: GetShort Word64
getVariableLengthWord64 = word7sToWord64 <$> getWord7s
getPtr :: GetShort Ptr
getPtr =
Ptr <$> (SlotNo <$> getVariableLengthWord64)
<*> (TxIx . fromIntegral <$> getVariableLengthWord64)
<*> (CertIx . fromIntegral <$> getVariableLengthWord64)
getKeyHash :: CC.Crypto crypto => GetShort (Credential kr crypto)
getKeyHash = KeyHashObj . KeyHash <$> getHash
getScriptHash :: CC.Crypto crypto => GetShort (Credential kr crypto)
getScriptHash = ScriptHashObj . ScriptHash <$> getHash
getStakeReference :: CC.Crypto crypto => Word8 -> GetShort (StakeReference crypto)
getStakeReference header = case testBit header notBaseAddr of
True -> case testBit header isEnterpriseAddr of
True -> pure StakeRefNull
False -> StakeRefPtr <$> getPtr
False -> case testBit header stakeCredIsScript of
True -> StakeRefBase <$> getScriptHash
False -> StakeRefBase <$> getKeyHash
getPayCred :: CC.Crypto crypto => Word8 -> GetShort (PaymentCredential crypto)
getPayCred header = case testBit header payCredIsScript of
True -> getScriptHash
False -> getKeyHash
-- | Efficiently check whether compated adddress is an address with a credential
-- that is a payment script.
isPayCredScriptCompactAddr :: CompactAddr crypto -> Bool
isPayCredScriptCompactAddr (UnsafeCompactAddr bytes) =
testBit (SBS.index bytes 0) payCredIsScript
-- | Efficiently check whether compated adddress is a Byron address.
isBootstrapCompactAddr :: CompactAddr crypto -> Bool
isBootstrapCompactAddr (UnsafeCompactAddr bytes) = testBit (SBS.index bytes 0) byron