Skip to content

Commit

Permalink
Merge pull request #207 from oasisprotocol/pro-wh/bugfix/txreplace-main
Browse files Browse the repository at this point in the history
indexer: replace failed tx
  • Loading branch information
ptrus committed Mar 9, 2022
2 parents d21107e + 50693b1 commit 2eabfef
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions indexer/utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package indexer

import (
"database/sql"
"encoding/hex"
"errors"
"fmt"
"math/big"

Expand Down Expand Up @@ -302,8 +304,24 @@ func (ib *indexBackend) StoreBlockData(oasisBlock *block.Block, txResults []*cli
return err
}
default:
if err = s.Insert(ib.ctx, tx); err != nil {
return err
earlierTx, earlierTxErr := s.GetTransaction(ib.ctx, tx.Hash)
if earlierTxErr != nil {
if !errors.Is(earlierTxErr, sql.ErrNoRows) {
return err
}
// First encounter of this tx, continue to upsert.
} else {
if earlierTx.Status != uint(ethtypes.ReceiptStatusFailed) {
ib.logger.Error("duplicate tx",
"earlier_tx", earlierTx,
"tx", tx,
)
return fmt.Errorf("duplicate tx hash %s in rounds %d and %d", tx.Hash, earlierTx.Round, tx.Round)
}
// Replacing a failed encounter of this tx, continue to upsert.
}
if upsertErr := s.Upsert(ib.ctx, tx); upsertErr != nil {
return upsertErr
}
}
}
Expand All @@ -320,8 +338,24 @@ func (ib *indexBackend) StoreBlockData(oasisBlock *block.Block, txResults []*cli
return err
}
default:
if err = s.Insert(ib.ctx, receipt); err != nil {
return err
earlierReceipt, earlierReceiptErr := s.GetTransactionReceipt(ib.ctx, receipt.TransactionHash)
if earlierReceiptErr != nil {
if !errors.Is(earlierReceiptErr, sql.ErrNoRows) {
return err
}
// First encounter of this receipt, continue to upsert.
} else {
if earlierReceipt.Status != uint(ethtypes.ReceiptStatusFailed) {
ib.logger.Error("duplicate receipt",
"earlier_receipt", earlierReceipt,
"receipt", receipt,
)
return fmt.Errorf("duplicate receipt tx hash %s in rounds %d and %d", receipt.TransactionHash, earlierReceipt.Round, receipt.Round)
}
// Replacing a failed encounter of this receipt, continue to upsert.
}
if upsertErr := s.Upsert(ib.ctx, receipt); upsertErr != nil {
return upsertErr
}
}
}
Expand Down

0 comments on commit 2eabfef

Please sign in to comment.