-
Notifications
You must be signed in to change notification settings - Fork 211
/
Chain.hs
124 lines (108 loc) · 3.28 KB
/
Chain.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
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE NoFieldSelectors #-}
{-# LANGUAGE NamedFieldPuns #-}
{- |
Copyright: © 2024 Cardano Foundation
License: Apache-2.0
Data types relating to the consensus about the blockchain.
-}
module Cardano.Wallet.Read.Chain
( -- * ChainPoint
ChainPoint (..)
, getChainPoint
, prettyChainPoint
, chainPointFromChainTip
-- * ChainTip
, ChainTip (..)
, getChainTip
, prettyChainTip
) where
import Prelude
import Cardano.Wallet.Read.Block
( Block
, BlockNo (..)
, RawHeaderHash
, SlotNo (..)
, getEraBlockNo
, getEraHeaderHash
, getEraSlotNo
, getRawHeaderHash
)
import Cardano.Wallet.Read.Eras
( IsEra
)
import GHC.Generics
( Generic
)
import NoThunks.Class
( NoThunks (..)
)
import qualified Cardano.Wallet.Read.Hash as Hash
import qualified Data.Text as T
{-----------------------------------------------------------------------------
ChainPoint
------------------------------------------------------------------------------}
-- | A point (block) on the Cardano blockchain.
data ChainPoint
= GenesisPoint
| BlockPoint
{ slotNo :: !SlotNo
, headerHash :: !RawHeaderHash
}
deriving (Eq, Ord, Show, Generic)
instance NoThunks ChainPoint
{-# INLINABLE getChainPoint #-}
getChainPoint :: IsEra era => Block era -> ChainPoint
getChainPoint block =
BlockPoint
{ slotNo = getEraSlotNo block
, headerHash = getRawHeaderHash $ getEraHeaderHash block
}
-- | Short printed representation of a 'ChainPoint'.
prettyChainPoint :: ChainPoint -> T.Text
prettyChainPoint GenesisPoint =
"[point genesis]"
prettyChainPoint (BlockPoint slot hash) =
"[point " <> hashF hash <> " at slot " <> slotF slot <> "]"
where
hashF = T.take 8 . Hash.hashToTextAsHex
slotF (SlotNo n) = T.pack (show n)
chainPointFromChainTip :: ChainTip -> ChainPoint
chainPointFromChainTip GenesisTip = GenesisPoint
chainPointFromChainTip (BlockTip slot hash _) = BlockPoint slot hash
{-----------------------------------------------------------------------------
Tip
------------------------------------------------------------------------------}
-- | Used in chain-sync protocol to advertise the tip of the server's chain.
-- Records the 'ChainPoint' and the 'BlockNo' of the block.
data ChainTip
= GenesisTip
| BlockTip
{ slotNo :: !SlotNo
, headerHash :: !RawHeaderHash
, blockNo :: !BlockNo
}
deriving (Eq, Ord, Show, Generic)
instance NoThunks ChainTip
{-# INLINABLE getChainTip #-}
getChainTip :: IsEra era => Block era -> ChainTip
getChainTip block =
BlockTip
{ slotNo = getEraSlotNo block
, headerHash = getRawHeaderHash $ getEraHeaderHash block
, blockNo = getEraBlockNo block
}
-- | Short printed representation of a 'ChainPoint'.
prettyChainTip :: ChainTip -> T.Text
prettyChainTip GenesisTip =
"[tip genesis]"
prettyChainTip BlockTip{slotNo,headerHash,blockNo} =
"[tip " <> hashF headerHash
<> " at slot " <> slotNoF slotNo
<> " at blockNo " <> blockNoF blockNo
<> "]"
where
hashF = T.take 8 . Hash.hashToTextAsHex
slotNoF (SlotNo n) = T.pack (show n)
blockNoF (BlockNo n) = T.pack (show n)