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

ci: add workflow for reporting possible state-changing diffs #13774

Merged
merged 2 commits into from
Nov 27, 2022

Conversation

elias-orijtech
Copy link
Contributor

@elias-orijtech elias-orijtech commented Nov 5, 2022

This PR implements a tool for detecting PRs that may touch state code. Example PR and run: https://github.com/elias-orijtech/cosmos-sdk/actions/runs/3439195320/jobs/5736196835

Fixes #13518

CC @odeke-em

@elias-orijtech elias-orijtech requested a review from a team as a code owner November 5, 2022 00:02
@elias-orijtech elias-orijtech marked this pull request as draft November 5, 2022 00:02
Comment on lines 126 to 128
for _, pkg := range pkg.Imports {
addPkg(pkg)
}

Check failure

Code scanning / gosec

the value in the range statement should be _ unless copying a map: want: for key := range m

the value in the range statement should be _ unless copying a map: want: for key := range m

func main() {
flag.Parse()
*dir, _ = filepath.Abs(*dir)

Check warning

Code scanning / gosec

Returned error is not propagated up the stack.

Returned error is not propagated up the stack.
@codecov
Copy link

codecov bot commented Nov 5, 2022

Codecov Report

Merging #13774 (5e179fb) into main (2739f83) will increase coverage by 0.40%.
The diff coverage is n/a.

❗ Current head 5e179fb differs from pull request most recent head dd59995. Consider uploading reports for the commit dd59995 to get more accurate results

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #13774      +/-   ##
==========================================
+ Coverage   56.25%   56.66%   +0.40%     
==========================================
  Files         667      636      -31     
  Lines       56576    54552    -2024     
==========================================
- Hits        31829    30910     -919     
+ Misses      22165    21136    -1029     
+ Partials     2582     2506      -76     
Impacted Files Coverage Δ
x/auth/keeper/genesis.go 0.00% <0.00%> (-50.00%) ⬇️
x/auth/keeper/account.go 53.33% <0.00%> (-18.34%) ⬇️
store/cache/cache.go 79.62% <0.00%> (-16.67%) ⬇️
store/mem/store.go 82.35% <0.00%> (-11.77%) ⬇️
store/types/store.go 67.44% <0.00%> (-11.63%) ⬇️
store/internal/proofs/create.go 80.95% <0.00%> (-9.53%) ⬇️
x/group/internal/math/dec.go 41.53% <0.00%> (-7.00%) ⬇️
x/gov/abci.go 90.29% <0.00%> (-6.86%) ⬇️
server/config/config.go 69.87% <0.00%> (-2.65%) ⬇️
x/auth/types/account.go 76.83% <0.00%> (-2.50%) ⬇️
... and 103 more

Comment on lines 126 to 128
for _, pkg := range pkg.Imports {
addPkg(pkg)
}

Check warning

Code scanning / CodeQL

Iteration over map

Iteration over map may be a possible source of non-determinism
Comment on lines 134 to 136
for n := range rootFuncs {
missing = append(missing, n.typ+"."+n.fun)
}

Check warning

Code scanning / CodeQL

Iteration over map

Iteration over map may be a possible source of non-determinism
cmd/statediff/main.go Outdated Show resolved Hide resolved
@elias-orijtech elias-orijtech changed the title cmd/statediff: add tool for reporting possible state-changing diffs ci: add workflow for reporting possible state-changing diffs Nov 10, 2022
@elias-orijtech elias-orijtech force-pushed the main branch 4 times, most recently from 86b83ab to 7d7a1f6 Compare November 10, 2022 18:04
@elias-orijtech elias-orijtech marked this pull request as ready for review November 10, 2022 18:04
@elias-orijtech
Copy link
Contributor Author

This is now ready for review.

Copy link
Collaborator

@odeke-em odeke-em left a comment

Choose a reason for hiding this comment

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

LGTM, thank you @elias-orijtech! Just one comment though.

Kindly cc-ing @ebuchman too

.github/workflows/statediff.yml Outdated Show resolved Hide resolved
@elias-orijtech elias-orijtech force-pushed the main branch 2 times, most recently from 52a167f to d02a4f9 Compare November 10, 2022 22:03
@odeke-em
Copy link
Collaborator

@elias-orijtech please update the commit messages to link to the issue to be fixed:

Fixes #13518

So that it gets auto closed and a related PR is linked.

@tac0turtle
Copy link
Member

does this post a comment in the pr or only fail in ci?

@elias-orijtech
Copy link
Contributor Author

does this post a comment in the pr or only fail in ci?

It didn't, but now it does :) See elias-orijtech#106 (comment) for an example comment it posts (once pr PR).

- uses: actions/checkout@v3
- uses: orijtech/statediff@main
with:
roots: 'github.com/cosmos/cosmos-sdk/baseapp.BaseApp.DeliverTx,github.com/cosmos/cosmos-sdk/baseapp.BaseApp.BeginBlock,github.com/cosmos/cosmos-sdk/baseapp.BaseApp.EndBlock,github.com/cosmos/cosmos-sdk/baseapp.BaseApp.Commit'
Copy link
Member

Choose a reason for hiding this comment

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

could you explain what this does?

Copy link
Member

Choose a reason for hiding this comment

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

oh I read what this does. I don't think this solves the issue described above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The linked issue says

"State machine relevant code should basically be anything reachable from BeginBlock/DeliverTx/EndBlock/Commit."

This tool implements the basic reachability analysis: function or method calls. With the GitHub and testing machinery in place, the tool can easily be expanded. For example, what makes code such as x/staking/keeper/msg_server.go state code? Called by a root function/method, implementing a certain interface, something else? If you come up with the rule, I can add it to the tool.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@ebuchman could you please chime in too? @elias-orijtech implemented checking code reachable from the desired roots. Please chime in with suggestions for the use cases if it doesn't fit what he implemented. Thanks @tac0turtle @alexanderbez for the co-reviews!

Copy link
Member

@tac0turtle tac0turtle left a comment

Choose a reason for hiding this comment

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

If we make a state machine breaking change in x/staking/keeper/msg_server.go it wouldn't fire. The tool may be to simple for what is wanted.

could you touch on how this tool works?

@elias-orijtech
Copy link
Contributor Author

If we make a state machine breaking change in x/staking/keeper/msg_server.go it wouldn't fire. The tool may be to simple for what is wanted.

For now, maybe, but tool can be expanded to detect more cases. I can change "Fixes #13518" to "Update #13518" to reflect that, if you like. This PR is mostly about getting the ball rolling with a MVP tool and all the machinery for integrating with GitHub.

could you touch on how this tool works?

There is a bit of explanation in https://github.com/orijtech/statediff. Basically, the tool detect whether a PR touches any line that belongs to the graph of function or methods called from the roots.

@alexanderbez
Copy link
Contributor

So this job mainly just checks if code was modified/added/removed in an execution flow that is regarded as part of the state machine? If so, how exactly does it determine that? Can there be false negatives? What about false positives?

@elias-orijtech
Copy link
Contributor Author

So this job mainly just checks if code was modified/added/removed in an execution flow that is regarded as part of the state machine? If so, how exactly does it determine that? Can there be false negatives? What about false positives?

False negatives can happen if a state function or method is unreachable through direct function or method calls from a root. For example, through an interface method call.

False positives can happen if a PR touches a state function or method, but the change is not actually state changing. For example a comment or a performance optimization.

@alexanderbez
Copy link
Contributor

I see. Ok, then I don't have any strong objections to this. I'm happy to give an ACK. I'm not convinced how much it will really buy us, but at least we can experiment with it.

Let's keep this as a non-required job.

@amaury1093 amaury1093 self-assigned this Nov 16, 2022
The workflow uses the new github.com/orijtech/statediff tool that
builds a callgraph from a set of root methods and functions, and
checks whether a patch touches it.

Fixes cosmos#13518

Signed-off-by: Elias Naur <elias@orijtech.com>
@elias-orijtech
Copy link
Contributor Author

If we make a state machine breaking change in x/staking/keeper/msg_server.go it wouldn't fire. The tool may be to simple for what is wanted.

Can you clarify what you mean by too simple? Just adding roots (say, github.com/cosmos/cosmos-sdk/x/staking/keeper.msgServer.Delegate) will trigger on any changes to Delegate or one of its callees.

I've tried a few implementations to catch changes to msg_server.go automatically, but to make productive progress can you say what makes code such as that in msg_server.go state-sensitive? For example "any code called by the gRPC endpoints", or "implementations of interface X/Y/Z...".

Copy link
Member

@tac0turtle tac0turtle left a comment

Choose a reason for hiding this comment

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

lets merge it and see how it works

@tac0turtle tac0turtle enabled auto-merge (squash) November 27, 2022 11:48
@tac0turtle tac0turtle merged commit 60fdbd0 into cosmos:main Nov 27, 2022
@tac0turtle
Copy link
Member

Can you clarify what you mean by too simple? Just adding roots (say, github.com/cosmos/cosmos-sdk/x/staking/keeper.msgServer.Delegate) will trigger on any changes to Delegate or one of its callees.

I actually need to see it work, I could be wrong with my assumption.

I've tried a few implementations to catch changes to msg_server.go automatically, but to make productive progress can you say what makes code such as that in msg_server.go state-sensitive? For example "any code called by the gRPC endpoints", or "implementations of interface X/Y/Z...".

This is a good question and not something we have well defined I believe. There is the unspoken things we shouldn't touch, but its better to write them down

@odeke-em
Copy link
Collaborator

Thank you for the reviews @alexanderbez @tac0turtle plus merge and for the good work @elias-orijtech

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.

tool to check SDK diffs and highlight just state-machine relevant code
6 participants