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

client/eth: Get receipt before checking status. #1599

Merged
merged 1 commit into from
May 6, 2022

Conversation

JoeGruffins
Copy link
Member

@JoeGruffins JoeGruffins commented Apr 25, 2022

checkTxStatus would sometimes return a receipt on error, which caused a
panic in tests sometimes. Get the receipt separately to avoid this.

closes #1577

@chappjc
Copy link
Member

chappjc commented Apr 25, 2022

I got the impression this was used in upcoming work: #1523 (comment)
@buck54321 you gave the thumbs up, but it wasn't clear if you meant "yeah, will be used" or "right, will remove".
EDIT: oh, yeah, used: https://github.com/decred/dcrdex/pull/1399/files#diff-671406d18db2f8e1a281545be05fcdefd789b63614398d678d38e2b2814cf788R2526

@chappjc
Copy link
Member

chappjc commented Apr 25, 2022

NVM, I see it used in #1399

@JoeGruffins
Copy link
Member Author

Oh shoot. Well... I'll go comment over there and convert this to draft for the time being.

checkTxStatus would sometimes return a receipt on error, which caused a
panic in tests sometimes. Get the receipt separately to avoid this.
@JoeGruffins JoeGruffins changed the title client/eth: Move checkTxStatus to test file. client/eth: Get receipt before checking status. May 5, 2022
@JoeGruffins JoeGruffins marked this pull request as ready for review May 5, 2022 06:48
@JoeGruffins
Copy link
Member Author

Forgot to comment on this. Fixing here.

Copy link
Member

@chappjc chappjc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, but could you point out the panic. At a glance I don't see what incorrect logic was there.

@JoeGruffins
Copy link
Member Author

JoeGruffins commented May 6, 2022

could you point out the panic.

func (n *nodeClient) checkTxStatus(ctx context.Context, tx *types.Transaction, txOpts *bind.TransactOpts) (*types.Receipt, error) {
// It appears the receipt is only accessible after the tx is mined.
receipt, err := n.transactionReceipt(ctx, tx.Hash())
if err != nil {
return nil, fmt.Errorf("error getting transaction receipt: %v", err)
}
if receipt.Status != types.ReceiptStatusSuccessful {
return receipt, fmt.Errorf("transaction status failed")
}
if receipt.GasUsed > txOpts.GasLimit {
return receipt, fmt.Errorf("gas used, %d, appears to have exceeded limit of %d", receipt.GasUsed, txOpts.GasLimit)
}
return receipt, nil
}

Imagine n.transactionReceipt fails here, as it usually does when testing on testnet. We get an error and nil receipt return, which is good. The second two errors will return a populated receipt and an error.

Now here in tests:

receipt, err := ethClient.checkTxStatus(ctx, tx, txOpts)
if err != nil && test.success {
spew.Dump(tx)
spew.Dump(receipt)
t.Fatalf("%s: failed transaction status: %v", test.name, err)
} else if receipt.GasUsed > expGas {
t.Fatalf("%s: gas used %d exceeds expected max %d", test.name, receipt.GasUsed, expGas)
}

So if a test where test.success was false and there was an error it goes to the second condition and tries to take the gas used which causes the panic because the receipt was nil.

The test was expected to fail, but failed for an unexpected reason. I think that simply sticking to the "rule" that if error expect bad returns is best and solves the problem.

@chappjc chappjc merged commit e912a08 into decred:master May 6, 2022
@chappjc chappjc added this to the 0.5 milestone May 18, 2022
@chappjc chappjc added the ETH label Aug 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

client/eth: checkTxStatus returns a receipt on error sometimes.
2 participants