-
Notifications
You must be signed in to change notification settings - Fork 199
/
shardBlock.go
241 lines (201 loc) · 8.75 KB
/
shardBlock.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
package blockAPI
import (
"encoding/hex"
"time"
"github.com/multiversx/mx-chain-core-go/data/alteredAccount"
"github.com/multiversx/mx-chain-core-go/data/api"
"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/node/filters"
"github.com/multiversx/mx-chain-go/process"
)
type shardAPIBlockProcessor struct {
*baseAPIBlockProcessor
}
// newShardApiBlockProcessor will create a new instance of shard api block processor
func newShardApiBlockProcessor(arg *ArgAPIBlockProcessor, emptyReceiptsHash []byte) *shardAPIBlockProcessor {
hasDbLookupExtensions := arg.HistoryRepo.IsEnabled()
return &shardAPIBlockProcessor{
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,
logsFacade: arg.LogsFacade,
receiptsRepository: arg.ReceiptsRepository,
alteredAccountsProvider: arg.AlteredAccountsProvider,
accountsRepository: arg.AccountsRepository,
scheduledTxsExecutionHandler: arg.ScheduledTxsExecutionHandler,
enableEpochsHandler: arg.EnableEpochsHandler,
},
}
}
// GetBlockByNonce will return a shard APIBlock by nonce
func (sbp *shardAPIBlockProcessor) GetBlockByNonce(nonce uint64, options api.BlockQueryOptions) (*api.Block, error) {
storerUnit := dataRetriever.ShardHdrNonceHashDataUnit + dataRetriever.UnitType(sbp.selfShardID)
nonceToByteSlice := sbp.uint64ByteSliceConverter.ToByteSlice(nonce)
headerHash, err := sbp.store.Get(storerUnit, nonceToByteSlice)
if err != nil {
return nil, err
}
// if genesis block, get the nonce key corresponding to the altered block
if nonce == 0 {
nonceToByteSlice = append(nonceToByteSlice, []byte(common.GenesisStorageSuffix)...)
}
alteredHeaderHash, err := sbp.store.Get(storerUnit, nonceToByteSlice)
if err != nil {
return nil, err
}
blockBytes, err := sbp.getFromStorer(dataRetriever.BlockHeaderUnit, alteredHeaderHash)
if err != nil {
return nil, err
}
return sbp.convertShardBlockBytesToAPIBlock(headerHash, blockBytes, options)
}
// GetBlockByHash will return a shard APIBlock by hash
func (sbp *shardAPIBlockProcessor) GetBlockByHash(hash []byte, options api.BlockQueryOptions) (*api.Block, error) {
blockBytes, err := sbp.getFromStorer(dataRetriever.BlockHeaderUnit, hash)
if err != nil {
return nil, err
}
blockHeader, err := process.UnmarshalShardHeader(sbp.marshalizer, blockBytes)
if err != nil {
return nil, err
}
// if genesis block, get the altered block bytes
if blockHeader.GetNonce() == 0 {
alteredHash := createAlteredBlockHash(hash)
blockBytes, err = sbp.getFromStorer(dataRetriever.BlockHeaderUnit, alteredHash)
if err != nil {
return nil, err
}
}
blockAPI, err := sbp.convertShardBlockBytesToAPIBlock(hash, blockBytes, options)
if err != nil {
return nil, err
}
storerUnit := dataRetriever.ShardHdrNonceHashDataUnit + dataRetriever.UnitType(sbp.selfShardID)
return sbp.computeStatusAndPutInBlock(blockAPI, storerUnit)
}
// GetBlockByRound will return a shard APIBlock by round
func (sbp *shardAPIBlockProcessor) GetBlockByRound(round uint64, options api.BlockQueryOptions) (*api.Block, error) {
headerHash, blockBytes, err := sbp.getBlockHeaderHashAndBytesByRound(round, dataRetriever.BlockHeaderUnit)
if err != nil {
return nil, err
}
return sbp.convertShardBlockBytesToAPIBlock(headerHash, blockBytes, options)
}
// GetAlteredAccountsForBlock will return the altered accounts for the desired shard block
func (sbp *shardAPIBlockProcessor) GetAlteredAccountsForBlock(options api.GetAlteredAccountsForBlockOptions) ([]*alteredAccount.AlteredAccount, error) {
headerHash, blockBytes, err := sbp.getHashAndBlockBytesFromStorer(options.GetBlockParameters)
if err != nil {
return nil, err
}
apiBlock, err := sbp.convertShardBlockBytesToAPIBlock(headerHash, blockBytes, api.BlockQueryOptions{WithTransactions: true, WithLogs: true})
if err != nil {
return nil, err
}
return sbp.apiBlockToAlteredAccounts(apiBlock, options)
}
func (sbp *shardAPIBlockProcessor) getHashAndBlockBytesFromStorer(params api.GetBlockParameters) ([]byte, []byte, error) {
switch params.RequestType {
case api.BlockFetchTypeByHash:
return sbp.getHashAndBlockBytesFromStorerByHash(params)
case api.BlockFetchTypeByNonce:
return sbp.getHashAndBlockBytesFromStorerByNonce(params)
default:
return nil, nil, errUnknownBlockRequestType
}
}
func (sbp *shardAPIBlockProcessor) getHashAndBlockBytesFromStorerByHash(params api.GetBlockParameters) ([]byte, []byte, error) {
headerBytes, err := sbp.getFromStorer(dataRetriever.BlockHeaderUnit, params.Hash)
return params.Hash, headerBytes, err
}
func (sbp *shardAPIBlockProcessor) getHashAndBlockBytesFromStorerByNonce(params api.GetBlockParameters) ([]byte, []byte, error) {
storerUnit := dataRetriever.ShardHdrNonceHashDataUnit + dataRetriever.UnitType(sbp.selfShardID)
nonceToByteSlice := sbp.uint64ByteSliceConverter.ToByteSlice(params.Nonce)
headerHash, err := sbp.store.Get(storerUnit, nonceToByteSlice)
if err != nil {
return nil, nil, err
}
headerBytes, err := sbp.getFromStorer(dataRetriever.BlockHeaderUnit, headerHash)
return headerHash, headerBytes, err
}
func (sbp *shardAPIBlockProcessor) convertShardBlockBytesToAPIBlock(hash []byte, blockBytes []byte, options api.BlockQueryOptions) (*api.Block, error) {
blockHeader, err := process.UnmarshalShardHeader(sbp.marshalizer, blockBytes)
if err != nil {
return nil, err
}
numOfTxs := uint32(0)
miniblocks := make([]*api.MiniBlock, 0)
for _, mb := range blockHeader.GetMiniBlockHeaderHandlers() {
if block.Type(mb.GetTypeInt32()) == block.PeerBlock {
continue
}
numOfTxs += mb.GetTxCount()
miniblockAPI := &api.MiniBlock{
Hash: hex.EncodeToString(mb.GetHash()),
Type: block.Type(mb.GetTypeInt32()).String(),
SourceShard: mb.GetSenderShardID(),
DestinationShard: mb.GetReceiverShardID(),
ProcessingType: block.ProcessingType(mb.GetProcessingType()).String(),
ConstructionState: block.MiniBlockState(mb.GetConstructionState()).String(),
IndexOfFirstTxProcessed: mb.GetIndexOfFirstTxProcessed(),
IndexOfLastTxProcessed: mb.GetIndexOfLastTxProcessed(),
}
if options.WithTransactions {
miniBlockCopy := mb
err = sbp.getAndAttachTxsToMb(miniBlockCopy, blockHeader, miniblockAPI, options)
if err != nil {
return nil, err
}
}
miniblocks = append(miniblocks, miniblockAPI)
}
intraMb, err := sbp.getIntrashardMiniblocksFromReceiptsStorage(blockHeader, hash, options)
if err != nil {
return nil, err
}
miniblocks = append(miniblocks, intraMb...)
miniblocks = filterOutDuplicatedMiniblocks(miniblocks)
statusFilters := filters.NewStatusFilters(sbp.selfShardID)
statusFilters.ApplyStatusFilters(miniblocks)
apiBlock := &api.Block{
Nonce: blockHeader.GetNonce(),
Round: blockHeader.GetRound(),
Epoch: blockHeader.GetEpoch(),
Shard: blockHeader.GetShardID(),
Hash: hex.EncodeToString(hash),
PrevBlockHash: hex.EncodeToString(blockHeader.GetPrevHash()),
NumTxs: numOfTxs,
MiniBlocks: miniblocks,
AccumulatedFees: blockHeader.GetAccumulatedFees().String(),
DeveloperFees: blockHeader.GetDeveloperFees().String(),
Timestamp: time.Duration(blockHeader.GetTimeStamp()),
Status: BlockStatusOnChain,
StateRootHash: hex.EncodeToString(blockHeader.GetRootHash()),
PubKeyBitmap: hex.EncodeToString(blockHeader.GetPubKeysBitmap()),
Signature: hex.EncodeToString(blockHeader.GetSignature()),
LeaderSignature: hex.EncodeToString(blockHeader.GetLeaderSignature()),
ChainID: string(blockHeader.GetChainID()),
SoftwareVersion: hex.EncodeToString(blockHeader.GetSoftwareVersion()),
ReceiptsHash: hex.EncodeToString(blockHeader.GetReceiptsHash()),
Reserved: blockHeader.GetReserved(),
RandSeed: hex.EncodeToString(blockHeader.GetRandSeed()),
PrevRandSeed: hex.EncodeToString(blockHeader.GetPrevRandSeed()),
}
addScheduledInfoInBlock(blockHeader, apiBlock)
return apiBlock, nil
}
// IsInterfaceNil returns true if underlying object is nil
func (sbp *shardAPIBlockProcessor) IsInterfaceNil() bool {
return sbp == nil
}