Skip to content

Commit

Permalink
delete last block height
Browse files Browse the repository at this point in the history
  • Loading branch information
sh-cha committed Dec 4, 2024
1 parent 9877511 commit 9b459f1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
28 changes: 14 additions & 14 deletions node/broadcaster/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,56 +21,56 @@ func IsTxNotFoundErr(err error, txHash string) bool {
}

// CheckPendingTx query tx info to check if pending tx is processed.
func (b *Broadcaster) CheckPendingTx(ctx types.Context, pendingTx btypes.PendingTxInfo) (*rpccoretypes.ResultTx, time.Time, int64, error) {
func (b *Broadcaster) CheckPendingTx(ctx types.Context, pendingTx btypes.PendingTxInfo) (*rpccoretypes.ResultTx, time.Time, error) {
txHash, err := hex.DecodeString(pendingTx.TxHash)
if err != nil {
return nil, time.Time{}, 0, err
}
lastHeader, err := b.rpcClient.Header(ctx, nil)
if err != nil {
return nil, time.Time{}, 0, err
return nil, time.Time{}, err
}

res, txerr := b.rpcClient.QueryTx(ctx, txHash)
if txerr != nil && IsTxNotFoundErr(txerr, pendingTx.TxHash) {
// if the tx is not found, it means the tx is not processed yet
// or the tx is not indexed by the node in rare cases.

pendingTxTime := time.Unix(0, pendingTx.Timestamp)

lastHeader, err := b.rpcClient.Header(ctx, nil)
if err != nil {
return nil, time.Time{}, err
}

// before timeout
if lastHeader.Header.Time.Before(pendingTxTime.Add(b.cfg.TxTimeout)) {
ctx.Logger().Debug("failed to query tx", zap.String("tx_hash", pendingTx.TxHash), zap.String("error", txerr.Error()))
return nil, time.Time{}, 0, types.ErrTxNotFound
return nil, time.Time{}, types.ErrTxNotFound
} else {
// timeout case
account, err := b.AccountByAddress(pendingTx.Sender)
if err != nil {
return nil, time.Time{}, 0, err
return nil, time.Time{}, err
}
accountSequence, err := account.GetLatestSequence(ctx)
if err != nil {
return nil, time.Time{}, 0, err
return nil, time.Time{}, err
}

// if sequence is larger than the sequence of the pending tx,
// handle it as the tx has already been processed
if pendingTx.Sequence < accountSequence {
return nil, time.Time{}, 0, nil
return nil, time.Time{}, nil
}
panic(fmt.Errorf("something wrong, pending txs are not processed for a long time; current block time: %s, pending tx processing time: %s", time.Now().UTC().String(), pendingTxTime.UTC().String()))
}
} else if txerr != nil {
return nil, time.Time{}, 0, txerr
return nil, time.Time{}, txerr
} else if res.TxResult.Code != 0 {
panic(fmt.Errorf("tx failed, tx hash: %s, code: %d, log: %s; you might need to check gas adjustment config or balance", pendingTx.TxHash, res.TxResult.Code, res.TxResult.Log))
}

header, err := b.rpcClient.Header(ctx, &res.Height)
if err != nil {
return nil, time.Time{}, 0, err
return nil, time.Time{}, err
}
return res, header.Header.Time, lastHeader.Header.Height, nil
return res, header.Header.Time, nil
}

// RemovePendingTx remove pending tx from local pending txs.
Expand Down
4 changes: 2 additions & 2 deletions node/tx_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (n *Node) txChecker(ctx types.Context, enableEventHandler bool) error {

height := int64(0)

res, blockTime, lastBlockHeight, err := n.broadcaster.CheckPendingTx(ctx, pendingTx)
res, blockTime, err := n.broadcaster.CheckPendingTx(ctx, pendingTx)
if errors.Is(err, types.ErrTxNotFound) {
// tx not found
continue
Expand All @@ -64,7 +64,7 @@ func (n *Node) txChecker(ctx types.Context, enableEventHandler bool) error {
default:
}

err := n.handleEvent(ctx, res.Height, blockTime, lastBlockHeight, res.Tx, types.MustUint64ToInt64(uint64(res.Index)), event)
err := n.handleEvent(ctx, res.Height, blockTime, 0, res.Tx, types.MustUint64ToInt64(uint64(res.Index)), event)
if err != nil {
ctx.Logger().Error("failed to handle event", zap.String("tx_hash", pendingTx.TxHash), zap.Int("event_index", eventIndex), zap.String("error", err.Error()))
break
Expand Down

0 comments on commit 9b459f1

Please sign in to comment.