15
15
{-# LANGUAGE UndecidableInstances #-}
16
16
17
17
module Cardano.Ledger.Block (
18
- Block (Block , Block' , UnserialisedBlock , UnsafeUnserialisedBlock ),
18
+ Block (Block ),
19
19
bheader ,
20
20
bbody ,
21
21
neededTxInsForBlock ,
@@ -27,17 +27,13 @@ import Cardano.Ledger.Binary (
27
27
DecCBOR (decCBOR ),
28
28
EncCBOR (.. ),
29
29
EncCBORGroup (.. ),
30
- annotatorSlice ,
31
30
decodeRecordNamed ,
32
31
encodeListLen ,
33
- serialize ,
32
+ toPlainEncoding ,
34
33
)
35
34
import qualified Cardano.Ledger.Binary.Plain as Plain
36
35
import Cardano.Ledger.Core
37
- import Cardano.Ledger.MemoBytes (MemoBytes (Memo ), decodeMemoized )
38
36
import Cardano.Ledger.TxIn (TxIn (.. ))
39
- import qualified Data.ByteString.Lazy as BSL
40
- import qualified Data.ByteString.Short as SBS
41
37
import Data.Foldable (toList )
42
38
import Data.Set (Set )
43
39
import qualified Data.Set as Set
@@ -47,7 +43,7 @@ import Lens.Micro ((^.))
47
43
import NoThunks.Class (NoThunks (.. ))
48
44
49
45
data Block h era
50
- = Block' ! h ! (TxSeq era ) BSL. ByteString
46
+ = Block ! h ! (TxSeq era )
51
47
deriving (Generic )
52
48
53
49
deriving stock instance
@@ -65,111 +61,63 @@ deriving anyclass instance
65
61
) =>
66
62
NoThunks (Block h era )
67
63
68
- pattern Block ::
64
+ instance
69
65
forall era h .
70
66
( Era era
71
67
, EncCBORGroup (TxSeq era )
72
68
, EncCBOR h
73
69
) =>
74
- h ->
75
- TxSeq era ->
76
- Block h era
77
- pattern Block h txns <-
78
- Block' h txns _
79
- where
80
- Block h txns =
81
- let bytes =
82
- serialize (eraProtVerLow @ era ) $
83
- encodeListLen (1 + listLen txns) <> encCBOR h <> encCBORGroup txns
84
- in Block' h txns bytes
85
-
86
- {-# COMPLETE Block #-}
87
-
88
- -- | Access a block without its serialised bytes. This is often useful when
89
- -- we're using a 'BHeaderView' in place of the concrete header.
90
- pattern UnserialisedBlock ::
91
- h ->
92
- TxSeq era ->
93
- Block h era
94
- pattern UnserialisedBlock h txns <- Block' h txns _
95
-
96
- {-# COMPLETE UnserialisedBlock #-}
97
-
98
- -- | Unsafely construct a block without the ability to serialise its bytes.
99
- --
100
- -- Anyone calling this pattern must ensure that the resulting block is never
101
- -- serialised. Any uses of this pattern outside of testing code should be
102
- -- regarded with suspicion.
103
- pattern UnsafeUnserialisedBlock ::
104
- h ->
105
- TxSeq era ->
106
- Block h era
107
- pattern UnsafeUnserialisedBlock h txns <-
108
- Block' h txns _
70
+ EncCBOR (Block h era )
109
71
where
110
- UnsafeUnserialisedBlock h txns =
111
- let bytes = error " `UnsafeUnserialisedBlock` used to construct a block which was later serialised."
112
- in Block' h txns bytes
113
-
114
- {-# COMPLETE UnsafeUnserialisedBlock #-}
115
-
116
- instance (EraTx era , Typeable h ) => EncCBOR (Block h era )
117
-
118
- instance (EraTx era , Typeable h ) => Plain. ToCBOR (Block h era ) where
119
- toCBOR (Block' _ _ blockBytes) = Plain. encodePreEncoded $ BSL. toStrict blockBytes
72
+ encCBOR (Block h txns) =
73
+ encodeListLen (1 + listLen txns) <> encCBOR h <> encCBORGroup txns
120
74
121
75
instance
122
- ( EraSegWits era
123
- , DecCBOR (Annotator h )
124
- , Typeable h
76
+ forall era h .
77
+ ( Era era
78
+ , EncCBORGroup (TxSeq era )
79
+ , EncCBOR h
125
80
) =>
126
- DecCBOR ( Annotator ( Block h era ) )
81
+ Plain. ToCBOR ( Block h era )
127
82
where
128
- decCBOR = annotatorSlice $
129
- decodeRecordNamed " Block" (const blockSize) $ do
130
- header <- decCBOR
131
- txns <- decCBOR
132
- pure $ Block' <$> header <*> txns
133
- where
134
- blockSize =
135
- 1 -- header
136
- + fromIntegral (numSegComponents @ era )
137
-
138
- data BlockRaw h era = BlockRaw ! h ! (TxSeq era )
83
+ toCBOR = toPlainEncoding (eraProtVerLow @ era ) . encCBOR
139
84
140
85
instance
141
86
( EraSegWits era
142
87
, DecCBOR h
143
88
, DecCBOR (TxSeq era )
144
89
) =>
145
- DecCBOR (BlockRaw h era )
90
+ DecCBOR (Block h era )
146
91
where
147
92
decCBOR =
148
93
decodeRecordNamed " Block" (const blockSize) $ do
149
94
header <- decCBOR
150
95
txns <- decCBOR
151
- pure $ BlockRaw header txns
96
+ pure $ Block header txns
152
97
where
153
98
blockSize = 1 + fromIntegral (numSegComponents @ era )
154
99
155
100
instance
156
101
( EraSegWits era
157
- , DecCBOR h
158
- , DecCBOR ( TxSeq era )
102
+ , DecCBOR ( Annotator h )
103
+ , Typeable h
159
104
) =>
160
- DecCBOR (Block h era )
105
+ DecCBOR (Annotator ( Block h era ) )
161
106
where
162
- decCBOR = do
163
- Memo (BlockRaw h txSeq) bs <- decodeMemoized (decCBOR @ (BlockRaw h era ))
164
- pure $ Block' h txSeq (BSL. fromStrict (SBS. fromShort bs))
107
+ decCBOR = decodeRecordNamed " Block" (const blockSize) $ do
108
+ header <- decCBOR
109
+ txns <- decCBOR
110
+ pure $ Block <$> header <*> txns
111
+ where
112
+ blockSize = 1 + fromIntegral (numSegComponents @ era )
165
113
166
114
bheader ::
167
115
Block h era ->
168
116
h
169
- bheader (Block' bh _ _) = bh
117
+ bheader (Block bh _) = bh
170
118
171
119
bbody :: Block h era -> TxSeq era
172
- bbody (Block' _ txs _ ) = txs
120
+ bbody (Block _ txs) = txs
173
121
174
122
-- | The validity of any individual block depends only on a subset
175
123
-- of the UTxO stored in the ledger state. This function returns
@@ -185,7 +133,7 @@ neededTxInsForBlock ::
185
133
EraSegWits era =>
186
134
Block h era ->
187
135
Set TxIn
188
- neededTxInsForBlock (Block' _ txsSeq _ ) = Set. filter isNotNewInput allTxIns
136
+ neededTxInsForBlock (Block _ txsSeq) = Set. filter isNotNewInput allTxIns
189
137
where
190
138
txBodies = map (^. bodyTxL) $ toList $ fromTxSeq txsSeq
191
139
allTxIns = Set. unions $ map (^. allInputsTxBodyF) txBodies
0 commit comments