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: fix bug when updating allowance inside AllowedMsgAllowance #10564

Merged
merged 16 commits into from
Jan 5, 2022

Conversation

0Tech
Copy link
Contributor

@0Tech 0Tech commented Nov 17, 2021

Description

Closes: #10563

By the fix, AllowedMsgAllowance updates its allowance after the change in Accept().
To catch the corresponding issue, the fix also adds new unit tests.


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • followed the guidelines for building modules
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed ! in the type prefix if API or client breaking change
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic
  • reviewed API design and naming
  • reviewed documentation is accurate
  • reviewed tests and test coverage
  • manually tested (if applicable)

Copy link
Contributor Author

@0Tech 0Tech left a comment

Choose a reason for hiding this comment

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

I have reviewed all of my changes, and added some comments.

return allowance.Accept(ctx, fee, msgs)
remove, reserr := allowance.Accept(ctx, fee, msgs)
if !remove {
a.Allowance, err = types.NewAnyWithValue(allowance.(proto.Message))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Assign the changed allowance to the old one, so the change could be applied.
There is no need to do this job if remove is true apparently.

@@ -0,0 +1,208 @@
package feegrant_test
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 test has been copied from basic_fee_test.go.

Comment on lines 48 to 52
"msg contained": {
allowance: &feegrant.BasicAllowance{},
msgs: []string{"/cosmos.feegrant.v1beta1.MsgRevokeAllowance"},
accept: true,
},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is the simplest case. In the test, the called message would be /cosmos.feegrant.v1beta1.MsgRevokeAllowance, so it must be accepted.

Comment on lines 53 to 57
"msg not contained": {
allowance: &feegrant.BasicAllowance{},
msgs: []string{"/cosmos.feegrant.v1beta1.MsgGrantAllowance"},
accept: false,
},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added this case to confirm that Accept() rejects the message if it is not in its list.
Other than this case, these are just borrowed cases from basic_fee_test.go.

Comment on lines 180 to 196
updatedGrant := func(granter, grantee sdk.AccAddress,
allowance feegrant.FeeAllowanceI) feegrant.Grant {
newGrant, err := feegrant.NewGrant(
granter,
grantee,
allowance)
require.NoError(t, err)

cdc := simapp.MakeTestEncodingConfig().Codec
bz, err := cdc.Marshal(&newGrant)
require.NoError(t, err)

var loaded feegrant.Grant
err = cdc.Unmarshal(bz, &loaded)
require.NoError(t, err)
return loaded
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mimic the logic as best as I can.
After Accept() has been called, the updated grant should be saved, so I added Marshal().
To check the values are all correct, we must load the grant again, so I added Unmarshal().

require.NoError(t, err)
feeAllowance, err := newAllowance.(*feegrant.AllowedMsgAllowance).GetAllowance()
require.NoError(t, err)
assert.Equal(t, tc.remains, feeAllowance.(*feegrant.BasicAllowance).SpendLimit)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Finally, the assert against SpendLimit has been made.

@0Tech 0Tech marked this pull request as ready for review November 17, 2021 10:42
@atheeshp atheeshp self-assigned this Nov 19, 2021
@atheeshp
Copy link
Contributor

The idea sounds fine to me, what are your thoughts on this @anilcse , @AmauryM , @blushi

@amaury1093 amaury1093 self-assigned this Nov 25, 2021
@alexanderbez
Copy link
Contributor

Is spend_limit meant to be mutated though?

@0Tech
Copy link
Contributor Author

0Tech commented Dec 1, 2021

Is spend_limit meant to be mutated though?

According to the documents and implementation, yes, it has to be mutated.

// spend_limit specifies the maximum amount of tokens that can be spent
// by this allowance and will be updated as tokens are spent. If it is
// empty, there is no spend limit and any amount of coins can be spent.
repeated cosmos.base.v1beta1.Coin spend_limit = 1

func (a *BasicAllowance) Accept(ctx sdk.Context, fee sdk.Coins, _ []sdk.Msg) (bool, error) {
if a.Expiration != nil && a.Expiration.Before(ctx.BlockTime()) {
return true, sdkerrors.Wrap(ErrFeeLimitExpired, "basic allowance")
}
if a.SpendLimit != nil {
left, invalid := a.SpendLimit.SafeSub(fee)
if invalid {
return false, sdkerrors.Wrap(ErrFeeLimitExceeded, "basic allowance")
}
a.SpendLimit = left
return left.IsZero(), nil
}
return false, nil
}

The modification I'm adding is just saving the change into the state correctly.

Copy link
Contributor

@alexanderbez alexanderbez left a comment

Choose a reason for hiding this comment

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

Changes LGTM, but I just don't have enough context about this module to give it an ACK. @AmauryM who designed or implemented the feegrant module?

Copy link
Contributor

@amaury1093 amaury1093 left a comment

Choose a reason for hiding this comment

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

Thanks @0Tech! the fix in x/feegrant/filtered_fee.go LGTM

Could you clean up the tests a little bit? they are really hard to follow

CHANGELOG.md Outdated Show resolved Hide resolved
x/feegrant/filtered_fee_test.go Outdated Show resolved Hide resolved
x/feegrant/filtered_fee_test.go Outdated Show resolved Hide resolved
x/feegrant/filtered_fee_test.go Outdated Show resolved Hide resolved
x/feegrant/filtered_fee_test.go Outdated Show resolved Hide resolved
x/feegrant/filtered_fee_test.go Outdated Show resolved Hide resolved
x/feegrant/filtered_fee_test.go Outdated Show resolved Hide resolved
@0Tech
Copy link
Contributor Author

0Tech commented Dec 20, 2021

Thanks @0Tech! the fix in x/feegrant/filtered_fee.go LGTM

Could you clean up the tests a little bit? they are really hard to follow

Thank you for your kind feedback @AmauryM! I have applied all the feedback you made 😄

Copy link
Contributor

@amaury1093 amaury1093 left a comment

Choose a reason for hiding this comment

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

actually, let's make the test simpler before merging.

x/feegrant/filtered_fee_test.go Outdated Show resolved Hide resolved
Comment on lines 168 to 190
if !removed {
// save the new grant
newGrant, err := feegrant.NewGrant(
sdk.AccAddress(grant.Granter),
sdk.AccAddress(grant.Grantee),
allowance)
require.NoError(t, err)

cdc := simapp.MakeTestEncodingConfig().Codec
bz, err := cdc.Marshal(&newGrant)
require.NoError(t, err)

// load the grant
var loadedGrant feegrant.Grant
err = cdc.Unmarshal(bz, &loadedGrant)
require.NoError(t, err)

newAllowance, err := newGrant.GetGrant()
require.NoError(t, err)
feeAllowance, err := newAllowance.(*feegrant.AllowedMsgAllowance).GetAllowance()
require.NoError(t, err)
assert.Equal(t, tc.remains, feeAllowance.(*feegrant.BasicAllowance).SpendLimit)
}
Copy link
Contributor

@amaury1093 amaury1093 Jan 3, 2022

Choose a reason for hiding this comment

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

I actually don't understand this block so well, still.

Can we simply do:

			if !removed {
				assert.Equal(t, tc.remains, allowance.Allowance.GetCachedValue().(*feegrant.BasicAllowance).SpendLimit)
			}

Copy link
Contributor Author

@0Tech 0Tech Jan 4, 2022

Choose a reason for hiding this comment

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

This block is the whole point of this fix. With the simplified version, we cannot check whether the allowance has been updated correctly, because the cached value was correct even before the fix.
This patch is forcing the cached value applied to the state. We must marshal & unmarshal it, which simulates the actual process.
That was the last redundant func() meant for. If you don't mind, I can wrap it with func() again (or a block with curly brackets suffice?), providing meaningful name and comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, there was a mistake during the modification. I think this has confused you.
newGrant at line 185 must be loadedGrant.
I will also apply the fix after your response.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK Understood. Yes loadedGrant on L185 sounds already clearer.

Could you add some comments, e.g. around marshaling to simulate the actual process of putting in block?

If you don't mind, I can wrap it with func() again (or a block with curly brackets suffice?), providing meaningful name and comments.

I don't think that's idiomatic go. Using newlines and comments should be enough to describe what you're achieving.

Copy link
Contributor

Choose a reason for hiding this comment

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

if !removed {
	assert.Equal(t, tc.remains, allowance.GetAllowance().(*feegrant.BasicAllowance).SpendLimit)
}

I align with @AmauryM here, this thing works I guess, can you verify that this assert should be fail with the earlier code and passes with the changes in this PR. if that works no need of creating the Grant and marshals.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I verified that the simplified version does not fail with both of the earlier code and the new code. And I also verified that the longer version with marshaling fails with the earlier code.

Copy link
Contributor

@atheeshp atheeshp left a comment

Choose a reason for hiding this comment

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

looks good, few nits!

x/feegrant/filtered_fee_test.go Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
}{
"msg contained": {
allowance: &feegrant.BasicAllowance{},
msgs: []string{sdk.MsgTypeURL(&call)},
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
msgs: []string{sdk.MsgTypeURL(&call)},
msgs: []string{sdk.MsgTypeURL(&banktypes.MsgSend{})},

we can remove call if this works.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it could be more straight-forward to use the variable call, because we don't need to compare the msgs in the cases with the actual calling msg in the loop. To be more pedantic, I have to make sure that the msg type of "msg not contained" case is not identical to that of call, but I think it's not necessary.

Comment on lines 66 to 69
a.Allowance, err = types.NewAnyWithValue(allowance.(proto.Message))
if err != nil {
return false, sdkerrors.Wrapf(sdkerrors.ErrPackAny, "cannot proto marshal %T", allowance)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you separate this into a separate method AllowedMsgAllowance.SetAllowance(allowance)

Comment on lines 168 to 190
if !removed {
// save the new grant
newGrant, err := feegrant.NewGrant(
sdk.AccAddress(grant.Granter),
sdk.AccAddress(grant.Grantee),
allowance)
require.NoError(t, err)

cdc := simapp.MakeTestEncodingConfig().Codec
bz, err := cdc.Marshal(&newGrant)
require.NoError(t, err)

// load the grant
var loadedGrant feegrant.Grant
err = cdc.Unmarshal(bz, &loadedGrant)
require.NoError(t, err)

newAllowance, err := newGrant.GetGrant()
require.NoError(t, err)
feeAllowance, err := newAllowance.(*feegrant.AllowedMsgAllowance).GetAllowance()
require.NoError(t, err)
assert.Equal(t, tc.remains, feeAllowance.(*feegrant.BasicAllowance).SpendLimit)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

if !removed {
	assert.Equal(t, tc.remains, allowance.GetAllowance().(*feegrant.BasicAllowance).SpendLimit)
}

I align with @AmauryM here, this thing works I guess, can you verify that this assert should be fail with the earlier code and passes with the changes in this PR. if that works no need of creating the Grant and marshals.

Comment on lines +156 to +157
grant, err := feegrant.NewGrant(granter, grantee, allowance)
require.NoError(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
grant, err := feegrant.NewGrant(granter, grantee, allowance)
require.NoError(t, err)

@0Tech 0Tech changed the title fix: update allowance inside AllowedMsgAllowance fix: fix bug when updating allowance inside AllowedMsgAllowance Jan 4, 2022
@amaury1093 amaury1093 added the A:automerge Automatically merge PR once all prerequisites pass. label Jan 5, 2022
@mergify mergify bot merged commit 651ef9e into cosmos:master Jan 5, 2022
@assafmo assafmo mentioned this pull request May 11, 2023
19 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A:automerge Automatically merge PR once all prerequisites pass. C:x/feegrant
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AllowedMsgAllowance does not subtract fees from spend_limit
4 participants