This repository was archived by the owner on Aug 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 631
/
Copy pathJsonLog.hs
46 lines (40 loc) · 1.82 KB
/
JsonLog.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
module Pos.Chain.Block.JsonLog
( jlCreatedBlock
, jlAdoptedBlock
) where
import Universum
import Formatting (sformat)
import Pos.Chain.Block.Blockchain (gbHeader, gbhPrevBlock)
import Pos.Chain.Block.Genesis (genBlockEpoch)
import Pos.Chain.Block.Union (Block, HeaderHash, headerHash,
headerHashF, mainBlockSlot, mainBlockTxPayload)
import Pos.Core (HasConfiguration, SlotId (..), getEpochIndex,
getSlotIndex, mkLocalSlotIndex)
import Pos.Core.JsonLog.LogEvents (JLBlock (..), JLEvent (..))
import Pos.Core.Txp (txpTxs)
import Pos.Crypto (hash, hashHexF)
-- | Return event of created block.
jlCreatedBlock :: HasConfiguration => Block -> JLEvent
jlCreatedBlock block = JLCreatedBlock $ JLBlock {..}
where
jlHash = showHeaderHash $ headerHash block
jlPrevBlock = showHeaderHash $ case block of
Left gB -> view gbhPrevBlock (gB ^. gbHeader)
Right mB -> view gbhPrevBlock (mB ^. gbHeader)
jlSlot = (getEpochIndex $ siEpoch slot, getSlotIndex $ siSlot slot)
jlTxs = case block of
Left _ -> []
Right mB -> map fromTx . toList $ mB ^. mainBlockTxPayload . txpTxs
slot :: SlotId
slot = case block of
Left gB -> let slotZero = case mkLocalSlotIndex 0 of
Right sz -> sz
Left _ -> error "impossible branch"
in SlotId (gB ^. genBlockEpoch) slotZero
Right mB -> mB ^. mainBlockSlot
fromTx = sformat hashHexF . hash
-- | Returns event of created 'Block'.
jlAdoptedBlock :: Block -> JLEvent
jlAdoptedBlock = JLAdoptedBlock . showHeaderHash . headerHash
showHeaderHash :: HeaderHash -> Text
showHeaderHash = sformat headerHashF