-
Notifications
You must be signed in to change notification settings - Fork 211
/
Block.hs
207 lines (181 loc) · 5.66 KB
/
Block.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
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE RecordWildCards #-}
{-# OPTIONS_GHC -Wno-orphans #-}
module Cardano.Wallet.Primitive.Types.Block
( Block (..)
, BlockHeader (..)
, ChainPoint (..)
, Slot
, isGenesisBlockHeader
, compareSlot
, chainPointFromBlockHeader
, chainPointFromBlockHeader'
, toWalletChainPoint
, fromWalletChainPoint
, toSlot
)
where
import Prelude
import Cardano.Slotting.Slot
( SlotNo (..)
, WithOrigin (..)
)
import Cardano.Wallet.Primitive.Types.Certificates
( DelegationCertificate
)
import Cardano.Wallet.Primitive.Types.Hash
( Hash (..)
)
import Cardano.Wallet.Primitive.Types.Tx.Tx
( Tx
)
import Control.DeepSeq
( NFData
)
import Control.Lens
( view
)
import Data.ByteArray.Encoding
( Base (Base16)
, convertToBase
)
import Data.Maybe
( fromJust
, isNothing
)
import Data.Quantity
( Quantity (getQuantity)
)
import Data.Word
( Word32
)
import Fmt
( Buildable (..)
, blockListF
, indentF
, prefixF
, pretty
)
import GHC.Generics
( Generic
)
import NoThunks.Class
( NoThunks
)
import qualified Cardano.Wallet.Read as Read
import qualified Cardano.Wallet.Read.Hash as Hash
import qualified Data.Text.Encoding as T
data Block = Block
{ header
:: !BlockHeader
, transactions
:: ![Tx]
, delegations
:: ![DelegationCertificate]
} deriving (Show, Eq, Ord, Generic)
instance NFData Block
instance Buildable (Block) where
build (Block h txs _) = mempty
<> build h
<> if null txs then " ∅" else "\n" <> indentF 4 (blockListF txs)
data BlockHeader = BlockHeader
{ slotNo
:: SlotNo
, blockHeight
:: Quantity "block" Word32
, headerHash
:: !(Hash "BlockHeader")
, parentHeaderHash
:: !(Maybe (Hash "BlockHeader"))
} deriving (Show, Eq, Ord, Generic)
-- | Check whether a block with a given 'BlockHeader' is the genesis block.
isGenesisBlockHeader :: BlockHeader -> Bool
isGenesisBlockHeader = isNothing . view #parentHeaderHash
instance NFData BlockHeader
instance Buildable BlockHeader where
build BlockHeader{..} =
previous
<> "["
<> current
<> "-"
<> build slotNo
<> "#" <> (build . show . getQuantity) blockHeight
<> "]"
where
toHex = T.decodeUtf8 . convertToBase Base16
current = prefixF 8 $ build $ toHex $ getHash headerHash
previous = case parentHeaderHash of
Nothing -> ""
Just h -> prefixF 8 (build $ toHex $ getHash h) <> "<-"
-- | A point on the blockchain
-- is either the genesis block, or a block with a hash that was
-- created at a particular 'SlotNo'.
--
-- TODO:
--
-- * This type is essentially a copy of the 'Cardano.Api.Block.ChainPoint'
-- type. We want to import it from there when overhauling our types.
-- * That said, using 'WithOrigin' would not be bad.
-- * 'BlockHeader' is also a good type for rerencing points on the chain,
-- but it's less compatible with the types in ouroboros-network.
data ChainPoint
= ChainPointAtGenesis
| ChainPoint !SlotNo !(Hash "BlockHeader")
deriving (Eq, Ord, Show, Generic)
-- | Compare the slot numbers of two 'ChainPoint's,
-- but where the 'ChainPointAtGenesis' comes before all other slot numbers.
compareSlot :: ChainPoint -> ChainPoint -> Ordering
compareSlot pt1 pt2 = compare (toSlot pt1) (toSlot pt2)
-- | Convert a 'BlockHeader' into a 'ChainPoint'.
chainPointFromBlockHeader :: BlockHeader -> ChainPoint
chainPointFromBlockHeader header@(BlockHeader sl _ hash _)
| isGenesisBlockHeader header = ChainPointAtGenesis
| otherwise = ChainPoint sl hash
chainPointFromBlockHeader' :: BlockHeader -> Read.ChainPoint
chainPointFromBlockHeader' =
fromWalletChainPoint . chainPointFromBlockHeader
toWalletChainPoint :: Read.ChainPoint -> ChainPoint
toWalletChainPoint Read.GenesisPoint = ChainPointAtGenesis
toWalletChainPoint (Read.BlockPoint (Read.SlotNo slot) hash) =
ChainPoint
(SlotNo $ fromIntegral slot)
(Hash $ Hash.hashToBytes hash)
fromWalletChainPoint :: ChainPoint -> Read.ChainPoint
fromWalletChainPoint ChainPointAtGenesis = Read.GenesisPoint
fromWalletChainPoint (ChainPoint slot hash) =
Read.BlockPoint
(toReadSlotNo slot)
(fromJust $ Hash.hashFromBytes $ getHash hash)
toReadSlotNo :: SlotNo -> Read.SlotNo
toReadSlotNo (SlotNo n) = Read.SlotNo (fromIntegral n)
instance NFData ChainPoint
instance NoThunks ChainPoint
instance Buildable ChainPoint where
build ChainPointAtGenesis = "[point genesis]"
build (ChainPoint slot hash) =
"[point " <> hashF <> " at slot " <> pretty slot <> "]"
where
hashF = prefixF 8 $ T.decodeUtf8 $ convertToBase Base16 $ getHash hash
-- | A point in (slot) time, which is either genesis ('Origin')
-- or has a slot number ('At').
--
-- In contrast to 'ChainPoint', the type 'Slot' does not refer
-- to a point on an actual chain with valid block hashes,
-- but merely to a timeslot which can hold a single block.
-- This implies:
--
-- * 'Slot' has a linear ordering implemented in the 'Ord' class
-- (where @Origin < At slot@).
-- * Using 'Slot' in QuickCheck testing requires less context
-- (such as an actual simulated chain.)
type Slot = WithOrigin SlotNo
-- | Retrieve the slot of a 'ChainPoint'.
toSlot :: ChainPoint -> Slot
toSlot ChainPointAtGenesis = Origin
toSlot (ChainPoint slot _) = At slot
instance Buildable Slot where
build Origin = "[genesis]"
build (At slot) = "[at slot " <> pretty slot <> "]"