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(vbank): ensure that multiple balance updates are sorted #3435

Merged
merged 2 commits into from
Jun 30, 2021

Conversation

michaelfig
Copy link
Member

@michaelfig michaelfig commented Jun 30, 2021

Fixes #3425

@mhofman identified another nondeterminism in x/vbank: balance update messages sent to JS that contained more than 1 update were giving an updates array whose order was determined by the Golang iteration order randomizer.

This change sorts the updates array and tests for regressions.

@michaelfig michaelfig added the cosmic-swingset package: cosmic-swingset label Jun 30, 2021
@michaelfig michaelfig self-assigned this Jun 30, 2021
@michaelfig michaelfig requested a review from JimLarson June 30, 2021 03:52
Copy link
Contributor

@JimLarson JimLarson left a comment

Choose a reason for hiding this comment

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

Great! Just some cleanup stuff to do.

@@ -99,6 +103,7 @@ func Test_marshalBalanceUpdate(t *testing.T) {
addressToBalance map[string]sdk.Coins
want balances
wantErr bool
encoded []byte
Copy link
Contributor

Choose a reason for hiding this comment

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

This field seems to be unused.

@@ -82,7 +82,11 @@ func decodeBalances(encoded []byte) (balances, uint64, error) {
return nil, 0, fmt.Errorf("bad balance update type: %s", balanceUpdate.Type)
}
b := newBalances()
for _, u := range balanceUpdate.Updated {
fmt.Printf("updated balances %v\n", balanceUpdate.Updated)
Copy link
Contributor

Choose a reason for hiding this comment

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

The non-determinism of update order is what led to the following commit in the PR that created these tests: 854fe21

In the long run, that commit should be undone and this file should switch from want values in the the map-of-maps representation - elegant as it is - to the vbankBalanceUpdate structure, or perhaps the raw encoded JSON - depending on whether differences in encoded JSON, e.g. different whitespace, would trigger a nondeterminism failure.

That could possibly be delayed to a follow-on PR. If so, then for now please abstract this plus the Type field check above into a balanceUpdateValid(vbu vbankBalanceUpdate) bool utility function, and file an issue for going back to determinism-sensitive want values.

Copy link
Member Author

Choose a reason for hiding this comment

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

I introduced validateBalanceUpdate, which returns an error if there's a problem, nil otherwise.

I'm not sure I want determinism-sensitive want values. I like the fact that the test creates random private/pubkeys, and it would make the test less clear to write dynamic want values (since their update order depends on the lexicographic ordering of the pubkey addresses).

@@ -495,7 +502,7 @@ func Test_EndBlock_Events(t *testing.T) {
}
gotMsg, gotNonce, err := decodeBalances([]byte(msgsSent[0]))
if err != nil {
t.Errorf("decode balances error = %v", err)
t.Fatalf("decode balances error = %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Cool! Please note that this PR fixes #3425.

@michaelfig michaelfig mentioned this pull request Jun 30, 2021
@michaelfig michaelfig linked an issue Jun 30, 2021 that may be closed by this pull request
@michaelfig michaelfig merged commit 6e4cf0c into xsnap-isready Jun 30, 2021
@michaelfig michaelfig deleted the mfig-vbank-sort-updates branch June 30, 2021 04:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cosmic-swingset package: cosmic-swingset
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use Fatalf when Golang tests should abort
2 participants