-
Notifications
You must be signed in to change notification settings - Fork 199
/
internalBlock.go
337 lines (276 loc) · 10.4 KB
/
internalBlock.go
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
package blockAPI
import (
"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/data"
"github.com/multiversx/mx-chain-core-go/data/block"
"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/dataRetriever"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/state"
)
type internalBlockProcessor struct {
*baseAPIBlockProcessor
}
// newInternalBlockProcessor will create a new instance of internal block processor
func newInternalBlockProcessor(arg *ArgAPIBlockProcessor, emptyReceiptsHash []byte) *internalBlockProcessor {
hasDbLookupExtensions := arg.HistoryRepo.IsEnabled()
return &internalBlockProcessor{
baseAPIBlockProcessor: &baseAPIBlockProcessor{
hasDbLookupExtensions: hasDbLookupExtensions,
selfShardID: arg.SelfShardID,
store: arg.Store,
marshalizer: arg.Marshalizer,
uint64ByteSliceConverter: arg.Uint64ByteSliceConverter,
historyRepo: arg.HistoryRepo,
apiTransactionHandler: arg.APITransactionHandler,
txStatusComputer: arg.StatusComputer,
hasher: arg.Hasher,
addressPubKeyConverter: arg.AddressPubkeyConverter,
emptyReceiptsHash: emptyReceiptsHash,
enableEpochsHandler: arg.EnableEpochsHandler,
},
}
}
// GetInternalShardBlockByNonce wil return a shard block by nonce
func (ibp *internalBlockProcessor) GetInternalShardBlockByNonce(format common.ApiOutputFormat, nonce uint64) (interface{}, error) {
if ibp.selfShardID == core.MetachainShardId {
return nil, ErrShardOnlyEndpoint
}
storerUnit := dataRetriever.ShardHdrNonceHashDataUnit + dataRetriever.UnitType(ibp.selfShardID)
nonceToByteSlice := ibp.uint64ByteSliceConverter.ToByteSlice(nonce)
headerHash, err := ibp.store.Get(storerUnit, nonceToByteSlice)
if err != nil {
return nil, err
}
blockBytes, err := ibp.getFromStorer(dataRetriever.BlockHeaderUnit, headerHash)
if err != nil {
return nil, err
}
return ibp.convertShardBlockBytesByOutputFormat(format, blockBytes)
}
// GetInternalShardBlockByHash wil return a shard block by hash
func (ibp *internalBlockProcessor) GetInternalShardBlockByHash(format common.ApiOutputFormat, hash []byte) (interface{}, error) {
if ibp.selfShardID == core.MetachainShardId {
return nil, ErrShardOnlyEndpoint
}
blockBytes, err := ibp.getFromStorer(dataRetriever.BlockHeaderUnit, hash)
if err != nil {
return nil, err
}
return ibp.convertShardBlockBytesByOutputFormat(format, blockBytes)
}
// GetInternalShardBlockByRound wil return a shard block by round
func (ibp *internalBlockProcessor) GetInternalShardBlockByRound(format common.ApiOutputFormat, round uint64) (interface{}, error) {
if ibp.selfShardID == core.MetachainShardId {
return nil, ErrShardOnlyEndpoint
}
_, blockBytes, err := ibp.getBlockHeaderHashAndBytesByRound(round, dataRetriever.BlockHeaderUnit)
if err != nil {
return nil, err
}
return ibp.convertShardBlockBytesByOutputFormat(format, blockBytes)
}
func (ibp *internalBlockProcessor) convertShardBlockBytesToInternalBlock(blockBytes []byte) (interface{}, error) {
return process.UnmarshalShardHeader(ibp.marshalizer, blockBytes)
}
func (ibp *internalBlockProcessor) convertShardBlockBytesByOutputFormat(format common.ApiOutputFormat, blockBytes []byte) (interface{}, error) {
switch format {
case common.ApiOutputFormatJSON:
return ibp.convertShardBlockBytesToInternalBlock(blockBytes)
case common.ApiOutputFormatProto:
return blockBytes, nil
default:
return nil, ErrInvalidOutputFormat
}
}
// GetInternalMetaBlockByNonce wil return a meta block by nonce
func (ibp *internalBlockProcessor) GetInternalMetaBlockByNonce(format common.ApiOutputFormat, nonce uint64) (interface{}, error) {
if ibp.selfShardID != core.MetachainShardId {
return nil, ErrMetachainOnlyEndpoint
}
storerUnit := dataRetriever.MetaHdrNonceHashDataUnit
nonceToByteSlice := ibp.uint64ByteSliceConverter.ToByteSlice(nonce)
headerHash, err := ibp.store.Get(storerUnit, nonceToByteSlice)
if err != nil {
return nil, err
}
blockBytes, err := ibp.getFromStorer(dataRetriever.MetaBlockUnit, headerHash)
if err != nil {
return nil, err
}
return ibp.convertMetaBlockBytesByOutputFormat(format, blockBytes)
}
// GetInternalMetaBlockByHash wil return a meta block by hash
func (ibp *internalBlockProcessor) GetInternalMetaBlockByHash(format common.ApiOutputFormat, hash []byte) (interface{}, error) {
if ibp.selfShardID != core.MetachainShardId {
return nil, ErrMetachainOnlyEndpoint
}
blockBytes, err := ibp.getFromStorer(dataRetriever.MetaBlockUnit, hash)
if err != nil {
return nil, err
}
return ibp.convertMetaBlockBytesByOutputFormat(format, blockBytes)
}
// GetInternalMetaBlockByRound wil return a meta block by round
func (ibp *internalBlockProcessor) GetInternalMetaBlockByRound(format common.ApiOutputFormat, round uint64) (interface{}, error) {
if ibp.selfShardID != core.MetachainShardId {
return nil, ErrMetachainOnlyEndpoint
}
_, blockBytes, err := ibp.getBlockHeaderHashAndBytesByRound(round, dataRetriever.MetaBlockUnit)
if err != nil {
return nil, err
}
return ibp.convertMetaBlockBytesByOutputFormat(format, blockBytes)
}
// GetInternalStartOfEpochMetaBlock wil return the epoch start meta block for the provided epoch
func (ibp *internalBlockProcessor) GetInternalStartOfEpochMetaBlock(format common.ApiOutputFormat, epoch uint32) (interface{}, error) {
if ibp.selfShardID != core.MetachainShardId {
return nil, ErrMetachainOnlyEndpoint
}
storer, err := ibp.store.GetStorer(dataRetriever.MetaBlockUnit)
if err != nil {
return nil, err
}
epochStartIdentifier := core.EpochStartIdentifier(epoch)
blockBytes, err := storer.GetFromEpoch([]byte(epochStartIdentifier), epoch)
if err != nil {
return nil, err
}
return ibp.convertMetaBlockBytesByOutputFormat(format, blockBytes)
}
// GetInternalStartOfEpochValidatorsInfo wil return the epoch start validators info for the provided epoch
func (ibp *internalBlockProcessor) GetInternalStartOfEpochValidatorsInfo(epoch uint32) ([]*state.ShardValidatorInfo, error) {
if ibp.selfShardID != core.MetachainShardId {
return nil, ErrMetachainOnlyEndpoint
}
storer, err := ibp.store.GetStorer(dataRetriever.MetaBlockUnit)
if err != nil {
return nil, err
}
epochStartIdentifier := core.EpochStartIdentifier(epoch)
blockBytes, err := storer.GetFromEpoch([]byte(epochStartIdentifier), epoch)
if err != nil {
return nil, err
}
metaBlock := &block.MetaBlock{}
err = ibp.marshalizer.Unmarshal(metaBlock, blockBytes)
if err != nil {
return nil, err
}
validatorsInfo, err := ibp.getAllValidatorsInfo(metaBlock)
if err != nil {
return nil, err
}
return validatorsInfo, nil
}
func (ibp *internalBlockProcessor) getAllValidatorsInfo(metaBlock data.HeaderHandler) ([]*state.ShardValidatorInfo, error) {
allValidatorInfo := make([]*state.ShardValidatorInfo, 0)
for _, miniBlockHeader := range metaBlock.GetMiniBlockHeaderHandlers() {
hash := miniBlockHeader.GetHash()
miniBlock, err := ibp.getMiniBlockByHash(hash, metaBlock.GetEpoch())
if err != nil {
return nil, err
}
if miniBlock.Type != block.PeerBlock {
continue
}
validatorInfo, err := ibp.getValidatorsInfo(miniBlock, metaBlock.GetEpoch())
if err != nil {
return nil, err
}
allValidatorInfo = append(allValidatorInfo, validatorInfo...)
}
return allValidatorInfo, nil
}
func (ibp *internalBlockProcessor) getValidatorsInfo(
miniBlock *block.MiniBlock,
epoch uint32,
) ([]*state.ShardValidatorInfo, error) {
validatorsInfoBytes := make([][]byte, 0)
if epoch >= ibp.enableEpochsHandler.RefactorPeersMiniBlocksEnableEpoch() {
validatorsInfoBuff, err := ibp.store.GetAll(dataRetriever.UnsignedTransactionUnit, miniBlock.TxHashes)
if err != nil {
return nil, err
}
for _, validatorInfoBuff := range validatorsInfoBuff {
validatorsInfoBytes = append(validatorsInfoBytes, validatorInfoBuff)
}
} else {
validatorsInfoBytes = miniBlock.TxHashes
}
validatorsInfo := make([]*state.ShardValidatorInfo, 0)
for _, validatorInfoBytes := range validatorsInfoBytes {
shardValidatorInfo := &state.ShardValidatorInfo{}
err := ibp.marshalizer.Unmarshal(shardValidatorInfo, validatorInfoBytes)
if err != nil {
return nil, err
}
validatorsInfo = append(validatorsInfo, shardValidatorInfo)
}
return validatorsInfo, nil
}
func (ibp *internalBlockProcessor) getMiniBlockByHash(hash []byte, epoch uint32) (*block.MiniBlock, error) {
storer, err := ibp.store.GetStorer(dataRetriever.MiniBlockUnit)
if err != nil {
return nil, err
}
blockBytes, err := storer.GetFromEpoch(hash, epoch)
if err != nil {
return nil, err
}
miniBlock := &block.MiniBlock{}
err = ibp.marshalizer.Unmarshal(miniBlock, blockBytes)
if err != nil {
return nil, err
}
return miniBlock, nil
}
func (ibp *internalBlockProcessor) convertMetaBlockBytesToInternalBlock(blockBytes []byte) (*block.MetaBlock, error) {
blockHeader := &block.MetaBlock{}
err := ibp.marshalizer.Unmarshal(blockHeader, blockBytes)
if err != nil {
return nil, err
}
return blockHeader, nil
}
func (ibp *internalBlockProcessor) convertMetaBlockBytesByOutputFormat(format common.ApiOutputFormat, blockBytes []byte) (interface{}, error) {
switch format {
case common.ApiOutputFormatJSON:
return ibp.convertMetaBlockBytesToInternalBlock(blockBytes)
case common.ApiOutputFormatProto:
return blockBytes, nil
default:
return nil, ErrInvalidOutputFormat
}
}
// GetInternalMiniBlock will return the miniblock based on the hash
func (ibp *internalBlockProcessor) GetInternalMiniBlock(format common.ApiOutputFormat, hash []byte, epoch uint32) (interface{}, error) {
storer, err := ibp.store.GetStorer(dataRetriever.MiniBlockUnit)
if err != nil {
return nil, err
}
blockBytes, err := storer.GetFromEpoch(hash, epoch)
if err != nil {
return nil, err
}
return ibp.convertMiniBlockBytesByOutportFormat(format, blockBytes)
}
func (ibp *internalBlockProcessor) convertMiniBlockBytesByOutportFormat(format common.ApiOutputFormat, blockBytes []byte) (interface{}, error) {
switch format {
case common.ApiOutputFormatJSON:
miniBlock := &block.MiniBlock{}
err := ibp.marshalizer.Unmarshal(miniBlock, blockBytes)
if err != nil {
return nil, err
}
return miniBlock, nil
case common.ApiOutputFormatProto:
return blockBytes, nil
default:
return nil, ErrInvalidOutputFormat
}
}
// IsInterfaceNil returns true if underlying object is nil
func (ibp *internalBlockProcessor) IsInterfaceNil() bool {
return ibp == nil
}