Skip to content

Commit 1b1ff38

Browse files
authored
update txindex condition to hit committed tip instead of parent of committed tip (#1458)
* update txindex condition to hit committed tip instead of parent of committed tip * fix nil pointer in error when failing to fetch block to attach * only info glog when no more blocks exists
1 parent 56529b1 commit 1b1ff38

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

lib/txindex.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,10 @@ func (txi *TXIndex) Update() error {
288288
if !exists || err != nil {
289289
return fmt.Errorf("Update: Problem getting block at height %d: %v", txindexTipNode.Height+1, err)
290290
}
291-
for !blockToAttach.Hash.IsEqual(blockTipNode.Hash) {
291+
for blockToAttach != nil &&
292+
blockToAttach.Header != nil &&
293+
blockToAttach.Header.PrevBlockHash != nil &&
294+
!blockToAttach.Header.PrevBlockHash.IsEqual(blockTipNode.Hash) {
292295
if txi.killed {
293296
glog.Infof(CLog(Yellow, "TxIndex: Update: Killed while attaching blocks"))
294297
break
@@ -300,7 +303,8 @@ func (txi *TXIndex) Update() error {
300303
glog.V(2).Infof("Update: Attaching block (height: %d, hash: %v)",
301304
blockToAttach.Height, blockToAttach.Hash)
302305

303-
blockMsg, err := GetBlock(blockToAttach.Hash, txi.CoreChain.DB(), nil)
306+
var blockMsg *MsgDeSoBlock
307+
blockMsg, err = GetBlock(blockToAttach.Hash, txi.CoreChain.DB(), nil)
304308
if err != nil {
305309
return fmt.Errorf("Update: Problem fetching attach block "+
306310
"with hash %v: %v", blockToAttach.Hash, err)
@@ -330,19 +334,19 @@ func (txi *TXIndex) Update() error {
330334
// - Compute its mapping values, which may include custom metadata fields
331335
// - add all its mappings to the db.
332336
for txnIndexInBlock, txn := range blockMsg.Txns {
333-
txnMeta, err := ConnectTxnAndComputeTransactionMetadata(
337+
txnMeta, innerErr := ConnectTxnAndComputeTransactionMetadata(
334338
txn, utxoView, blockToAttach.Hash, blockToAttach.Height,
335339
blockToAttach.Header.TstampNanoSecs, uint64(txnIndexInBlock))
336-
if err != nil {
340+
if innerErr != nil {
337341
return fmt.Errorf("Update: Problem connecting txn %v to txindex: %v",
338-
txn, err)
342+
txn, innerErr)
339343
}
340344

341-
err = DbPutTxindexTransactionMappingsWithTxn(dbTxn, nil, blockMsg.Header.Height,
345+
innerErr = DbPutTxindexTransactionMappingsWithTxn(dbTxn, nil, blockMsg.Header.Height,
342346
txn, txi.Params, txnMeta, txi.CoreChain.eventManager)
343-
if err != nil {
347+
if innerErr != nil {
344348
return fmt.Errorf("Update: Problem adding txn %v to txindex: %v",
345-
txn, err)
349+
txn, innerErr)
346350
}
347351
}
348352
return nil
@@ -358,10 +362,14 @@ func (txi *TXIndex) Update() error {
358362
return fmt.Errorf("Update: Problem attaching block %v: %v",
359363
blockToAttach, err)
360364
}
361-
var exists bool
365+
prevBlockToAttachHeight := blockToAttach.Height
362366
blockToAttach, exists, err = txi.CoreChain.GetBlockFromBestChainByHeight(uint64(blockToAttach.Height+1), false)
363-
if !exists || err != nil {
364-
return fmt.Errorf("Update: Problem getting block at height %d: %v", blockToAttach.Height+1, err)
367+
if err != nil {
368+
return fmt.Errorf("Update: Problem getting block at height %d: %v", prevBlockToAttachHeight+1, err)
369+
}
370+
if !exists {
371+
glog.Infof("Update: No more blocks to attach to txindex, exiting loop")
372+
break
365373
}
366374
}
367375

0 commit comments

Comments
 (0)