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

[CHORE] Update comparison match logic for opposite amounts #76

Merged
merged 5 commits into from
Jul 22, 2020

Conversation

qiwu7
Copy link
Contributor

@qiwu7 qiwu7 commented Jul 20, 2020

Fixes # .

Motivation

Update comparison match logic for opposite amounts

  • Add more validation on match operations
  • Improve time complexity of comparison logic from O(N^2) -> O(N)

Solution

use a single for loop instead of nested to simply things

Open questions

@qiwu7 qiwu7 changed the title [FIX] Update comparison match logic for opposite amounts [CHORE] Update comparison match logic for opposite amounts Jul 20, 2020
@coveralls
Copy link

coveralls commented Jul 20, 2020

Pull Request Test Coverage Report for Build 2823

  • 9 of 23 (39.13%) changed or added relevant lines in 1 file are covered.
  • 2 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.2%) to 64.22%

Changes Missing Coverage Covered Lines Changed/Added Lines %
parser/match_operations.go 9 23 39.13%
Files with Coverage Reduction New Missed Lines %
parser/match_operations.go 2 79.1%
Totals Coverage Status
Change from base Build 2720: -0.2%
Covered Lines: 1872
Relevant Lines: 2915

💛 - Coveralls

)
}
// check if all operations within the same match slice have the same amount
if len(matchOps) > 1 {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: you can remove this if statement as we assume it must be true (assuming matchIndex is not negative, which it should never be)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

equalAmounts method will return err if length of the operations is less than 1.
https://github.com/coinbase/rosetta-sdk-go/blob/master/parser/match_operations.go#L304-L306

@patrick-ogrady
Copy link
Contributor

@qiwu7 to fix the check-gen error you need to run make shorten-lines I think!

return fmt.Errorf("%w: amounts not opposites", err)
}
}
if err := matchOperationsForOppositeComparisonValid(amountMatch[0], matches); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

On further thought, I suggest we remove matchOperationsForOppositeComparisonValid because we are just wrapping equalAmounts with the conditionals it already contains (which I think is a bad practice).

Suggested change
if err := matchOperationsForOppositeComparisonValid(amountMatch[0], matches); err != nil {
if err := equalAmounts(matches[amountMatch[0]].Operations); err != nil {
return fmt.Errorf("%w: amounts not equal within index %d match operations", err, amountMatch[0])
}

Copy link
Contributor

@patrick-ogrady patrick-ogrady Jul 22, 2020

Choose a reason for hiding this comment

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

If we want to surface better errors in these cases (length 0 vs length 1), I'd suggest modifying equalAmounts itself instead of writing conditionals outside so that we don't hit this: https://github.com/coinbase/rosetta-sdk-go/blob/f929bfc4a262afbbb4053570664be00053a0b657/parser/match_operations.go#L304-L306

Copy link
Contributor

@patrick-ogrady patrick-ogrady Jul 22, 2020

Choose a reason for hiding this comment

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

If we still want to allow len(ops) == 1 to be considered equal in some cases, we could add another arg that specifies if this should be allowed

Copy link
Contributor

Choose a reason for hiding this comment

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

Still thinking if we'd want to consider operation amounts equal if there is only 1 op.

Copy link
Contributor

@patrick-ogrady patrick-ogrady left a comment

Choose a reason for hiding this comment

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

🔥

@heimdall-asguard
Copy link

Review Error for patrick-ogrady @ 2020-07-22 20:05:58 UTC
User failed mfa authentication, see go/mfa-help

@@ -301,8 +301,8 @@ func operationMatch(
// equalAmounts returns an error if a slice of operations do not have
// equal amounts.
func equalAmounts(ops []*types.Operation) error {
if len(ops) <= 1 {
return fmt.Errorf("cannot check equality of %d operations", len(ops))
if len(ops) == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would've expected this test to trigger a test change...can we write a simple one for testing equal amounts with 1 op? @qiwu7

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will do

Copy link
Contributor Author

Choose a reason for hiding this comment

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

actually there already are test cases for equalAmounts with only 1 op, that's why the test was failing previously.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

cool. I can help review that when it's ready :)

@patrick-ogrady patrick-ogrady merged commit 8f3f0d8 into master Jul 22, 2020
@patrick-ogrady patrick-ogrady deleted the fix/comparison_match branch July 22, 2020 22:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants