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

Fix error handling of TxIndex.Search #4095

Merged
merged 8 commits into from
Nov 5, 2019

Conversation

hsyis
Copy link
Contributor

@hsyis hsyis commented Oct 31, 2019

This PR fixes error handling for performing a txindex search.

TxIndex.Get returns

  • (txresult, nil) if the transaction is found.
  • (nil, nil) if the transaction is not found.
  • (nil, error) if error is occurred.

Therefore, if res is not nil, I think TxIndex.Search should return (txresult, nil).

Previously, however, this was not a problem because errors.Wrap returns nil if its first argument err is nil.

  • Referenced an issue explaining the need for the change
  • Updated all relevant documentation in docs
  • Updated all code comments where relevant
  • Wrote tests
  • Updated CHANGELOG_PENDING.md

@codecov-io
Copy link

Codecov Report

Merging #4095 into master will increase coverage by 0.02%.
The diff coverage is 50%.

@@            Coverage Diff            @@
##           master   #4095      +/-   ##
=========================================
+ Coverage   66.98%     67%   +0.02%     
=========================================
  Files         247     247              
  Lines       20993   20993              
=========================================
+ Hits        14062   14067       +5     
+ Misses       5886    5883       -3     
+ Partials     1045    1043       -2
Impacted Files Coverage Δ
state/txindex/kv/kv.go 74.9% <50%> (ø) ⬆️
blockchain/v2/reactor.go 59.64% <0%> (-1.76%) ⬇️
consensus/reactor.go 79.19% <0%> (ø) ⬆️
consensus/replay.go 72.54% <0%> (+0.78%) ⬆️
p2p/pex/pex_reactor.go 83.28% <0%> (+1.15%) ⬆️

Copy link
Contributor

@melekes melekes left a comment

Choose a reason for hiding this comment

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

Good catch! See my comment about using a switch below...

@@ -180,9 +180,9 @@ func (txi *TxIndex) Search(q *query.Query) ([]*types.TxResult, error) {
} else if ok {
res, err := txi.Get(hash)
if res == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

what do you think about using a switch here?

switch {
	case err != nil:
		return []*types.TxResult{}, errors.Wrapf(err, "can't get tx %X", hash)
	case res == nil:
		return []*types.TxResult{}, nil
	default:
		return []*types.TxResult{res}, nil

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good comment :) I think it's nice to have explicit error handling for each state.
I'll change to use the switch statement.

@melekes
Copy link
Contributor

melekes commented Nov 4, 2019

Could you add an entry to CHANGELOG_PENDING.md?

- [state] txindex/kv: Return an error if there's one when the user searches for a tx (hash=X) (@hsyis)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants