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

Add UTs for PreExecutor.PreExecute() #1858

Merged
merged 19 commits into from
Jan 20, 2025
Merged

Add UTs for PreExecutor.PreExecute() #1858

merged 19 commits into from
Jan 20, 2025

Conversation

RodrigoVillar
Copy link
Contributor

@RodrigoVillar RodrigoVillar commented Jan 9, 2025

This PR adds UTs for the PreExecute() method of PreExecutor.

@RodrigoVillar RodrigoVillar marked this pull request as ready for review January 10, 2025 19:05
@RodrigoVillar RodrigoVillar marked this pull request as draft January 13, 2025 21:15
@RodrigoVillar RodrigoVillar marked this pull request as ready for review January 14, 2025 15:04
Comment on lines 91 to 93
name: "raw fee doesn't exist",
err: database.ErrNotFound,
},
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we include a valid transaction for a test case like this where it will error before dereferencing the transaction?

If the code were to change the order of verification, but the raw fee was still missing, ideally the test case would still pass. By writing the test this way, it makes it brittle and it will fail if we make any ordering changes to the code itself.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Same comment for remaining test cases. To avoid duplicate code, we could construct a single valid tx above the creation of the slice of test cases.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we also add a more specific error, so that we won't confuse database.ErrNotFound from another part of the code with failing to fetch the raw fee?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment on lines 39 to 40
isRepeatError error
setBits []int
Copy link
Collaborator

Choose a reason for hiding this comment

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

can we replace setBits with a value of type set.Bits ? Not sure there's any advantage to handling this within the mocked IsRepeat instead of leaving to the caller.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@RodrigoVillar RodrigoVillar linked an issue Jan 14, 2025 that may be closed by this pull request
Comment on lines 45 to 50
if err != nil && err != database.ErrNotFound {
return err
}
if err == database.ErrNotFound {
return ErrFeeNotFound
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why differentiate these two errors? In this case, we can treat both the value not being present or any other error identically (no need to special case database.ErrNotFound here. Could we instead use fmt.Errorf("%w: %w", ErrFailedToFetchFee, err) so that we include the specific error we will want to test for and whatever error actually came from the view?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment on lines 37 to 40
type mockValidityWindow struct {
isRepeatError error
setBits set.Bits
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we use the general purpose mock validity window added in #1875 ? This way we do not re-create the same mock at each test site.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment on lines 183 to 196
db, err := merkledb.New(
ctx,
memdb.New(),
merkledb.Config{
BranchFactor: merkledb.BranchFactor16,
Tracer: trace.Noop,
},
)
r.NoError(err)

for k, v := range tt.state {
r.NoError(db.Put([]byte(k), v))
}
r.NoError(db.CommitToDB(ctx))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need to create a merkledb instance here? I think we can just the state type taken by the pre-executor to state.Immutable so that we can simply use state.ImmutableStorage instead

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Switched the PreExecutor from taking in state.View to taking in state.Immutable

ruleFactory := genesis.ImmutableRuleFactory{
Rules: testRules,
}
validTX := &chain.Transaction{
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we camel case this ie. validTx ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@aaronbuchwald aaronbuchwald merged commit 296bad6 into main Jan 20, 2025
17 checks passed
@aaronbuchwald aaronbuchwald deleted the preexecutor-ut branch January 20, 2025 23:46
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.

Add UTs for PreExecutor.PreExecute()
2 participants