Skip to content

Commit 51cf380

Browse files
coderbradleezjshen14
authored andcommitted
Reduplicative ations with same hash shows in action list 1386 (#1390)
1 parent 6717829 commit 51cf380

File tree

3 files changed

+40
-17
lines changed

3 files changed

+40
-17
lines changed

blockchain/indexbuilder.go

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,16 @@ type IndexBuilder struct {
4444
cancelChan chan interface{}
4545
timerFactory *prometheustimer.TimerFactory
4646
dao *blockDAO
47+
reindex bool
48+
}
49+
50+
type actionDelta struct {
51+
senderDelta map[hash.Hash160]uint64
52+
recipientDelta map[hash.Hash160]uint64
4753
}
4854

4955
// NewIndexBuilder instantiates an index builder
50-
func NewIndexBuilder(chain Blockchain) (*IndexBuilder, error) {
56+
func NewIndexBuilder(chain Blockchain, reindex bool) (*IndexBuilder, error) {
5157
bc, ok := chain.(*blockchain)
5258
if !ok {
5359
log.S().Panic("unexpected blockchain implementation")
@@ -67,6 +73,7 @@ func NewIndexBuilder(chain Blockchain) (*IndexBuilder, error) {
6773
cancelChan: make(chan interface{}),
6874
timerFactory: timerFactory,
6975
dao: bc.dao,
76+
reindex: reindex,
7077
}, nil
7178
}
7279

@@ -147,14 +154,16 @@ func (ib *IndexBuilder) getStartHeightAndIndex() (startHeight, startIndex uint64
147154
}
148155
return
149156
}
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 {
151158
tipIndexBytes := byteutil.Uint64ToBytes(tipIndex)
152159
batch.Put(blockActionBlockMappingNS, indexActionsTipIndexKey, tipIndexBytes, "failed to put tip index of actions")
153160
tipHeightBytes := byteutil.Uint64ToBytes(tipHeight)
154161
batch.Put(blockActionBlockMappingNS, indexActionsTipHeightKey, tipHeightBytes, "failed to put tip height")
155162
if err := ib.store.Commit(batch); err != nil {
156163
return err
157164
}
165+
actDelta.senderDelta = make(map[hash.Hash160]uint64)
166+
actDelta.recipientDelta = make(map[hash.Hash160]uint64)
158167
return nil
159168
}
160169
func (ib *IndexBuilder) initAndLoadActions() error {
@@ -166,12 +175,19 @@ func (ib *IndexBuilder) initAndLoadActions() error {
166175
if err != nil {
167176
return err
168177
}
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+
}
172184
}
173185
zap.L().Info("Loading actions", zap.Uint64("startHeight", startHeight), zap.Uint64("startIndex", startIndex))
174186
batch := db.NewBatch()
187+
actDelta := &actionDelta{
188+
senderDelta: make(map[hash.Hash160]uint64),
189+
recipientDelta: make(map[hash.Hash160]uint64),
190+
}
175191
i := startHeight
176192
for ; i <= tipHeight; i++ {
177193
hash, err := ib.dao.getBlockHash(i)
@@ -185,7 +201,7 @@ func (ib *IndexBuilder) initAndLoadActions() error {
185201
blk := &block.Block{
186202
Body: *body,
187203
}
188-
err = indexBlockHash(startIndex, hash, ib.store, blk, batch)
204+
err = indexBlockHash(startIndex, hash, ib.store, blk, batch, actDelta)
189205
if err != nil {
190206
return err
191207
}
@@ -198,7 +214,7 @@ func (ib *IndexBuilder) initAndLoadActions() error {
198214
startIndex += uint64(len(blk.Actions))
199215
// commit once every 10000 heights
200216
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 {
202218
return err
203219
}
204220
}
@@ -226,7 +242,7 @@ func getNextHeight(store db.KVStore) (uint64, error) {
226242
return nextHeight, nil
227243
}
228244
func indexBlock(store db.KVStore, blk *block.Block, batch db.KVStoreBatch) error {
229-
hash := blk.HashBlock()
245+
hashBlock := blk.HashBlock()
230246
// get index that already builded
231247
startIndex, err := getNextIndex(store)
232248
if err != nil {
@@ -237,7 +253,11 @@ func indexBlock(store db.KVStore, blk *block.Block, batch db.KVStoreBatch) error
237253
if err != nil {
238254
return err
239255
}
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 {
241261
return err
242262
}
243263
tipIndexBytes := byteutil.Uint64ToBytes(startIndex + uint64(len(blk.Actions)))
@@ -246,21 +266,20 @@ func indexBlock(store db.KVStore, blk *block.Block, batch db.KVStoreBatch) error
246266
batch.Put(blockActionBlockMappingNS, indexActionsTipHeightKey, tipHeightBytes, "failed to put tip height")
247267
return nil
248268
}
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 {
250270
for i, elp := range blk.Actions {
251271
actHash := elp.Hash()
252272
batch.Put(blockActionBlockMappingNS, actHash[hashOffset:], blkHash[:], "failed to put action hash %x", actHash)
253273
indexActionsBytes := byteutil.Uint64ToBytes(startActionsNum + uint64(i))
254274
batch.Put(blockActionBlockMappingNS, indexActionsBytes, actHash[:], "failed to put index of actions %x", actHash)
255275
}
256276

257-
return putActions(store, blk, batch)
277+
return putActions(store, blk, batch, actDelta)
258278
}
259279

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
264283
for _, selp := range blk.Actions {
265284
actHash := selp.Hash()
266285
callerAddrBytes := hash.BytesToHash160(selp.SrcPubkey().Hash())

chainservice/chainservice.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func New(
140140

141141
var indexBuilder *blockchain.IndexBuilder
142142
if _, ok := cfg.Plugins[config.GatewayPlugin]; ok && cfg.Chain.EnableAsyncIndexWrite {
143-
if indexBuilder, err = blockchain.NewIndexBuilder(chain); err != nil {
143+
if indexBuilder, err = blockchain.NewIndexBuilder(chain, cfg.Reindex); err != nil {
144144
return nil, errors.Wrap(err, "failed to create index builder")
145145
}
146146
if err := chain.AddSubscriber(indexBuilder); err != nil {

config/config.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func init() {
3535
flag.StringVar(&_secretPath, "secret-path", "", "Secret path")
3636
flag.StringVar(&_subChainPath, "sub-config-path", "", "Sub chain Config path")
3737
flag.Var(&_plugins, "plugin", "Plugin of the node")
38+
flag.BoolVar(&_reindex, "reindex", false, "reindex actions")
3839
}
3940

4041
var (
@@ -44,6 +45,7 @@ var (
4445
_secretPath string
4546
_subChainPath string
4647
_plugins strs
48+
_reindex bool
4749
)
4850

4951
const (
@@ -166,6 +168,7 @@ var (
166168
},
167169
},
168170
Genesis: genesis.Default,
171+
Reindex: false,
169172
}
170173

171174
// ErrInvalidCfg indicates the invalid config value
@@ -351,6 +354,7 @@ type (
351354
Log log.GlobalConfig `yaml:"log"`
352355
SubLogs map[string]log.GlobalConfig `yaml:"subLogs"`
353356
Genesis genesis.Genesis `yaml:"genesis"`
357+
Reindex bool `yaml:"reindex"`
354358
}
355359

356360
// Validate is the interface of validating the config
@@ -379,7 +383,7 @@ func New(validates ...Validate) (Config, error) {
379383
if err := yaml.Get(uconfig.Root).Populate(&cfg); err != nil {
380384
return Config{}, errors.Wrap(err, "failed to unmarshal YAML config to struct")
381385
}
382-
386+
cfg.Reindex = _reindex
383387
// set network master key to private key
384388
if cfg.Network.MasterKey == "" {
385389
cfg.Network.MasterKey = cfg.Chain.ProducerPrivKey

0 commit comments

Comments
 (0)