Skip to content
New issue

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

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

Already on GitHub? # to your account

Unit tests #21

Merged
merged 18 commits into from
Sep 22, 2023
Merged

Unit tests #21

merged 18 commits into from
Sep 22, 2023

Conversation

minghinmatthewlam
Copy link

@minghinmatthewlam minghinmatthewlam commented Aug 30, 2023

Why this should be merged

Adding test coverage for relayer repo and fixes #11

cc @geoff-vball for prior contribution to unit tests as well.

How this works

  • Replaces assert with require checks, since require fails on the first error and makes it easier to debug
  • mockgen binary to generate mocks for interfaces

How this was tested

  • CI

How this was documented

README update

Copy link
Collaborator

@cam-schultz cam-schultz left a comment

Choose a reason for hiding this comment

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

A handful of comments, but this generally looks good. Great to see mocks being leveraged in the unit tests 👍

messages/teleporter/config_test.go Outdated Show resolved Hide resolved
utils/utils.go Outdated Show resolved Hide resolved
scaledTotalWeight := new(big.Int).SetUint64(totalWeight)
scaledTotalWeight.Mul(scaledTotalWeight, new(big.Int).SetUint64(DefaultQuorumNumerator))
scaledSigWeight := new(big.Int).Mul(accumulatedSignatureWeight, new(big.Int).SetUint64(DefaultQuorumDenominator))
totalWeightBI := new(big.Int).SetUint64(totalWeight)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a specific reason to introduce the new variable totalWeightBI? When compared to operating on scaledTotalWeight in place, there's an additional allocation incurred.

Copy link
Contributor

Choose a reason for hiding this comment

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

I made this change to make the code a little more clear. Previously we were assigning scaledTotalWeight := new(big.Int).SetUint64(totalWeight), but it didn't actually represent the scaled total weight until the operation on the next line.

If we think the performance hit of the allocation is a big deal, we can change it back. We could also eliminate another allocation on line 48 by re-using scaledSigWeight.

Copy link
Author

Choose a reason for hiding this comment

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

I think the extra allocation for clarify in variable naming is okay

utils/utils.go Outdated
@@ -58,16 +57,18 @@ func CheckStakeWeightExceedsThreshold(accumulatedSignatureWeight *big.Int, total

// BigToHashSafe ensures that a bignum value is able to fit into a 32 byte buffer before converting it to a common.Hash
// Returns an error if overflow/truncation would occur by trying to perfom this operation.
// TODO is this function still needed? It works for negative numbers now.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looking at common.BigToHash, I think we can remove this function. big.Int doesn't guarantee the internal bytes will be representable as a uint256 (though the getters will truncate to the requested type), so we may want to add redundant validity checks at the callsites.

Copy link
Author

Choose a reason for hiding this comment

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

(though the getters will truncate to the requested type), so we may want to add redundant validity checks at the callsites.

can you clarify this, didn't get it. We aren't using BigToHashSafe anywhere anyways, so I think we can remove if we don't imagine using it.

Copy link
Author

Choose a reason for hiding this comment

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

hmm rereading this comment, I actually prefer leaving this utils function. Right now we don't use common.BigTohash or the BigToHashSafe, but want to leave for future use, since common.BigToHash will just truncate it to 32 bytes, I think it's better to check for error.

Copy link
Contributor

@geoff-vball geoff-vball left a comment

Choose a reason for hiding this comment

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

Just a few small comments. This looks good! Thanks for taking this over 🙏

config/config_test.go Outdated Show resolved Hide resolved
messages/teleporter/message_test.go Outdated Show resolved Hide resolved
messages/teleporter/message_test.go Outdated Show resolved Hide resolved
vms/evm/contract_message_test.go Outdated Show resolved Hide resolved
config/config_test.go Outdated Show resolved Hide resolved
utils/utils_test.go Outdated Show resolved Hide resolved
vms/evm/contract_message_test.go Outdated Show resolved Hide resolved
vms/evm/contract_message_test.go Outdated Show resolved Hide resolved
vms/evm/destination_client.go Show resolved Hide resolved
gwen917
gwen917 previously approved these changes Sep 22, 2023
Copy link
Contributor

@gwen917 gwen917 left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Collaborator

@michaelkaplan13 michaelkaplan13 left a comment

Choose a reason for hiding this comment

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

All of the tests and mocks added here LGTM. My only question would be if we want to include test coverage reports/requirements such that we can track our test coverage going forward and ensure it's at a level we feel comfortable with.

README.md Outdated Show resolved Hide resolved
geoff-vball
geoff-vball previously approved these changes Sep 22, 2023
Copy link
Contributor

@geoff-vball geoff-vball left a comment

Choose a reason for hiding this comment

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

LGTM! 👍

@minghinmatthewlam
Copy link
Author

All of the tests and mocks added here LGTM. My only question would be if we want to include test coverage reports/requirements such that we can track our test coverage going forward and ensure it's at a level we feel comfortable with.

I'm definitely for printing out a report on our test coverage, not sure fi we'd want to require it to above a minimum threshold. Right now our % of test coverage is still low, and want to avoid tackling it all in this PR. Would be good with first printing out the test coverage %, thoughts @michaelkaplan13 ?

@michaelkaplan13
Copy link
Collaborator

All of the tests and mocks added here LGTM. My only question would be if we want to include test coverage reports/requirements such that we can track our test coverage going forward and ensure it's at a level we feel comfortable with.

I'm definitely for printing out a report on our test coverage, not sure fi we'd want to require it to above a minimum threshold. Right now our % of test coverage is still low, and want to avoid tackling it all in this PR. Would be good with first printing out the test coverage %, thoughts @michaelkaplan13 ?

Yeah I agree, it's best left for a separate PR either way. 👍

@minghinmatthewlam
Copy link
Author

Created new issue to address test coverage: #44

Copy link
Contributor

@gwen917 gwen917 left a comment

Choose a reason for hiding this comment

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

LGTM👍

@minghinmatthewlam minghinmatthewlam merged commit 88471d3 into main Sep 22, 2023
@michaelkaplan13 michaelkaplan13 deleted the unit-tests branch December 7, 2023 15:26
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Add unit tests
5 participants