Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helper: use finalized for receipt calculation #1058

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 27 additions & 9 deletions helper/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,20 +497,38 @@ func (c *ContractCaller) GetConfirmedTxReceipt(tx common.Hash, requiredConfirmat
receipt, _ = receiptCache.(*ethTypes.Receipt)
}

Logger.Debug("Tx included in block", "block", receipt.BlockNumber.Uint64(), "tx", tx)
receiptBlockNumber := receipt.BlockNumber.Uint64()

// get main chain block
latestBlk, err := c.GetMainChainBlock(nil)
Logger.Debug("Tx included in block", "block", receiptBlockNumber, "tx", tx)

// fetch the last finalized main chain block (available post-merge)
latestFinalizedBlock, err := c.GetMainChainFinalizedBlock()
if err != nil {
Logger.Error("error getting latest block from main chain", "error", err)
return nil, err
Logger.Error("error getting latest finalized block from main chain", "error", err)
}

Logger.Debug("Latest block on main chain obtained", "Block", latestBlk.Number.Uint64())
// If latest finalized block is available, use it to check if receipt is finalized or not.
// Else, fallback to the `requiredConfirmations` value
if latestFinalizedBlock != nil {
Logger.Debug("Latest finalized block on main chain obtained", "Block", latestFinalizedBlock.Number.Uint64(), "receipt block", receiptBlockNumber)

if receiptBlockNumber > latestFinalizedBlock.Number.Uint64() {
return nil, errors.New("not enough confirmations")
}
} else {
// get current/latest main chain block
latestBlk, err := c.GetMainChainBlock(nil)
if err != nil {
Logger.Error("error getting latest block from main chain", "error", err)
return nil, err
}

Logger.Debug("Latest block on main chain obtained", "Block", latestBlk.Number.Uint64(), "receipt block", receiptBlockNumber)

diff := latestBlk.Number.Uint64() - receipt.BlockNumber.Uint64()
if diff < requiredConfirmations {
return nil, errors.New("not enough confirmations")
diff := latestBlk.Number.Uint64() - receiptBlockNumber
if diff < requiredConfirmations {
return nil, errors.New("not enough confirmations")
}
}

return receipt, nil
Expand Down