@@ -44,10 +44,16 @@ type IndexBuilder struct {
44
44
cancelChan chan interface {}
45
45
timerFactory * prometheustimer.TimerFactory
46
46
dao * blockDAO
47
+ reindex bool
48
+ }
49
+
50
+ type actionDelta struct {
51
+ senderDelta map [hash.Hash160 ]uint64
52
+ recipientDelta map [hash.Hash160 ]uint64
47
53
}
48
54
49
55
// NewIndexBuilder instantiates an index builder
50
- func NewIndexBuilder (chain Blockchain ) (* IndexBuilder , error ) {
56
+ func NewIndexBuilder (chain Blockchain , reindex bool ) (* IndexBuilder , error ) {
51
57
bc , ok := chain .(* blockchain )
52
58
if ! ok {
53
59
log .S ().Panic ("unexpected blockchain implementation" )
@@ -67,6 +73,7 @@ func NewIndexBuilder(chain Blockchain) (*IndexBuilder, error) {
67
73
cancelChan : make (chan interface {}),
68
74
timerFactory : timerFactory ,
69
75
dao : bc .dao ,
76
+ reindex : reindex ,
70
77
}, nil
71
78
}
72
79
@@ -147,14 +154,16 @@ func (ib *IndexBuilder) getStartHeightAndIndex() (startHeight, startIndex uint64
147
154
}
148
155
return
149
156
}
150
- func (ib * IndexBuilder ) commitBatchAndClear (tipIndex , tipHeight uint64 , batch db.KVStoreBatch ) error {
157
+ func (ib * IndexBuilder ) commitBatchAndClear (tipIndex , tipHeight uint64 , batch db.KVStoreBatch , actDelta * actionDelta ) error {
151
158
tipIndexBytes := byteutil .Uint64ToBytes (tipIndex )
152
159
batch .Put (blockActionBlockMappingNS , indexActionsTipIndexKey , tipIndexBytes , "failed to put tip index of actions" )
153
160
tipHeightBytes := byteutil .Uint64ToBytes (tipHeight )
154
161
batch .Put (blockActionBlockMappingNS , indexActionsTipHeightKey , tipHeightBytes , "failed to put tip height" )
155
162
if err := ib .store .Commit (batch ); err != nil {
156
163
return err
157
164
}
165
+ actDelta .senderDelta = make (map [hash.Hash160 ]uint64 )
166
+ actDelta .recipientDelta = make (map [hash.Hash160 ]uint64 )
158
167
return nil
159
168
}
160
169
func (ib * IndexBuilder ) initAndLoadActions () error {
@@ -166,12 +175,19 @@ func (ib *IndexBuilder) initAndLoadActions() error {
166
175
if err != nil {
167
176
return err
168
177
}
169
- startHeight , startIndex , err := ib .getStartHeightAndIndex ()
170
- if err != nil {
171
- return err
178
+ startHeight , startIndex := uint64 (1 ), uint64 (0 )
179
+ if ! ib .reindex {
180
+ startHeight , startIndex , err = ib .getStartHeightAndIndex ()
181
+ if err != nil {
182
+ return err
183
+ }
172
184
}
173
185
zap .L ().Info ("Loading actions" , zap .Uint64 ("startHeight" , startHeight ), zap .Uint64 ("startIndex" , startIndex ))
174
186
batch := db .NewBatch ()
187
+ actDelta := & actionDelta {
188
+ senderDelta : make (map [hash.Hash160 ]uint64 ),
189
+ recipientDelta : make (map [hash.Hash160 ]uint64 ),
190
+ }
175
191
i := startHeight
176
192
for ; i <= tipHeight ; i ++ {
177
193
hash , err := ib .dao .getBlockHash (i )
@@ -185,7 +201,7 @@ func (ib *IndexBuilder) initAndLoadActions() error {
185
201
blk := & block.Block {
186
202
Body : * body ,
187
203
}
188
- err = indexBlockHash (startIndex , hash , ib .store , blk , batch )
204
+ err = indexBlockHash (startIndex , hash , ib .store , blk , batch , actDelta )
189
205
if err != nil {
190
206
return err
191
207
}
@@ -198,7 +214,7 @@ func (ib *IndexBuilder) initAndLoadActions() error {
198
214
startIndex += uint64 (len (blk .Actions ))
199
215
// commit once every 10000 heights
200
216
if i % 10000 == 0 || i == tipHeight {
201
- if err := ib .commitBatchAndClear (startIndex , i , batch ); err != nil {
217
+ if err := ib .commitBatchAndClear (startIndex , i , batch , actDelta ); err != nil {
202
218
return err
203
219
}
204
220
}
@@ -226,7 +242,7 @@ func getNextHeight(store db.KVStore) (uint64, error) {
226
242
return nextHeight , nil
227
243
}
228
244
func indexBlock (store db.KVStore , blk * block.Block , batch db.KVStoreBatch ) error {
229
- hash := blk .HashBlock ()
245
+ hashBlock := blk .HashBlock ()
230
246
// get index that already builded
231
247
startIndex , err := getNextIndex (store )
232
248
if err != nil {
@@ -237,7 +253,11 @@ func indexBlock(store db.KVStore, blk *block.Block, batch db.KVStoreBatch) error
237
253
if err != nil {
238
254
return err
239
255
}
240
- if err = indexBlockHash (startIndex , hash , store , blk , batch ); err != nil {
256
+ actDelta := & actionDelta {
257
+ senderDelta : make (map [hash.Hash160 ]uint64 ),
258
+ recipientDelta : make (map [hash.Hash160 ]uint64 ),
259
+ }
260
+ if err = indexBlockHash (startIndex , hashBlock , store , blk , batch , actDelta ); err != nil {
241
261
return err
242
262
}
243
263
tipIndexBytes := byteutil .Uint64ToBytes (startIndex + uint64 (len (blk .Actions )))
@@ -246,21 +266,20 @@ func indexBlock(store db.KVStore, blk *block.Block, batch db.KVStoreBatch) error
246
266
batch .Put (blockActionBlockMappingNS , indexActionsTipHeightKey , tipHeightBytes , "failed to put tip height" )
247
267
return nil
248
268
}
249
- func indexBlockHash (startActionsNum uint64 , blkHash hash.Hash256 , store db.KVStore , blk * block.Block , batch db.KVStoreBatch ) error {
269
+ func indexBlockHash (startActionsNum uint64 , blkHash hash.Hash256 , store db.KVStore , blk * block.Block , batch db.KVStoreBatch , actDelta * actionDelta ) error {
250
270
for i , elp := range blk .Actions {
251
271
actHash := elp .Hash ()
252
272
batch .Put (blockActionBlockMappingNS , actHash [hashOffset :], blkHash [:], "failed to put action hash %x" , actHash )
253
273
indexActionsBytes := byteutil .Uint64ToBytes (startActionsNum + uint64 (i ))
254
274
batch .Put (blockActionBlockMappingNS , indexActionsBytes , actHash [:], "failed to put index of actions %x" , actHash )
255
275
}
256
276
257
- return putActions (store , blk , batch )
277
+ return putActions (store , blk , batch , actDelta )
258
278
}
259
279
260
- func putActions (store db.KVStore , blk * block.Block , batch db.KVStoreBatch ) error {
261
- senderDelta := make (map [hash.Hash160 ]uint64 )
262
- recipientDelta := make (map [hash.Hash160 ]uint64 )
263
-
280
+ func putActions (store db.KVStore , blk * block.Block , batch db.KVStoreBatch , actDelta * actionDelta ) error {
281
+ senderDelta := actDelta .senderDelta
282
+ recipientDelta := actDelta .recipientDelta
264
283
for _ , selp := range blk .Actions {
265
284
actHash := selp .Hash ()
266
285
callerAddrBytes := hash .BytesToHash160 (selp .SrcPubkey ().Hash ())
0 commit comments