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

Authz #2206

Merged
merged 28 commits into from
Aug 6, 2022
Merged

Authz #2206

merged 28 commits into from
Aug 6, 2022

Conversation

vuong177
Copy link
Contributor

@vuong177 vuong177 commented Jul 23, 2022

Closes: #2099

What is the purpose of the change

Brief Changelog

(for example:)

  • RegisterLegacyAminoCodec in codec.go in every module. This allows the x/authz module to properly serialize and de-serialize MsgExec instances using Amino

Testing and Verifying

  • Add golang test in msgs_test.go. This test confirms authz serialize and de-serializes working right.
  • I created a testnet with the current mainnet state. My ledger can sign authz msg in this testnet.

@github-actions github-actions bot added C:x/gamm Changes, features and bugs related to the gamm module. C:x/incentives C:x/lockup C:x/superfluid labels Jul 23, 2022
@vuong177 vuong177 marked this pull request as ready for review July 24, 2022 22:33
@vuong177 vuong177 requested a review from a team July 24, 2022 22:33
Copy link
Member

@p0mvn p0mvn left a comment

Choose a reason for hiding this comment

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

Awesome work @vuong177 . Thank you so much for the tests, including on the testnet.

Had some minor comments. Please take a look

@@ -867,3 +872,148 @@ func TestMsgExitSwapShareAmountIn(t *testing.T) {
}
}
}

// Test authz
Copy link
Member

Choose a reason for hiding this comment

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

Please elaborate elaborate here that this test is meant more so to test the authz than the gamm message

gammMsg sdk.Msg
}{
{
expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.gamm.v1beta1.MsgExitSwapShareAmountIn"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`,
Copy link
Member

Choose a reason for hiding this comment

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

Is there a way to have this represented in a struct format and the unmarshal to json string if needed?

It's hard to follow this in a string format.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there a way to have this represented in a struct format and the unmarshal to json string if needed?

It's hard to follow this in a string format.

I think it's a bit complex. Why we don't use pretty json in expectedGrantSignByteMsg. Like that

expectedGrantSignByteMsg :  `{
   "account_number":"1",
   "chain_id":"foo",
   "fee":{
      "amount":[
         
      ],
      "gas":"0"
   },
   "memo":"memo",
   "msgs":[
      {
         "type":"cosmos-sdk/MsgGrant",
         "value":{
            "grant":{
               "authorization":{
                  "type":"cosmos-sdk/GenericAuthorization",
                  "value":{
                     "msg":"/osmosis.gamm.v1beta1.MsgExitSwapShareAmountIn"
                  }
               },
               "expiration":"0001-01-01T02:01:01.000000001Z"
            },
            "grantee":"cosmos1def",
            "granter":"cosmos1abc"
         }
      }
   ],
   "sequence":"1",
   "timeout_height":"1"
}`

instead of use one line string

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good, thanks for suggesting the alternative

x/gamm/types/msgs_test.go Show resolved Hide resolved
typeURL := sdk.MsgTypeURL(tc.gammMsg)
grant, err := authz.NewGrant(someDate, authz.NewGenericAuthorization(typeURL), someDate.Add(time.Hour))
require.NoError(t, err)
msgGrant := &authz.MsgGrant{Granter: "cosmos1abc", Grantee: "cosmos1def", Grant: grant}
Copy link
Member

Choose a reason for hiding this comment

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

can we extract cosmos1abc and cosmos1def to constants instead of hardcoding in multiple places, please?

x/superfluid/types/msg_test.go Show resolved Hide resolved
msg sdk.Msg
}{
{
expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.superfluid.MsgLockAndSuperfluidDelegate"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`,
Copy link
Member

Choose a reason for hiding this comment

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

Similar comment about making these structs and unmarshalling, if possible

x/tokenfactory/types/msgs_test.go Show resolved Hide resolved
msg sdk.Msg
}{
{
expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.tokenfactory.v1beta1.MsgCreateDenom"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`,
Copy link
Member

Choose a reason for hiding this comment

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

ditto: structs instead of strings

@p0mvn
Copy link
Member

p0mvn commented Jul 28, 2022

Hi @vuong177 . Have you gotten a chance to look at my comments? Would love to help you get this over the finish line as we are prioritizing this feature for the current sprint completion

@vuong177
Copy link
Contributor Author

vuong177 commented Jul 28, 2022

@p0mvn sorry for my late response. I'm making this test cleaner.

@p0mvn
Copy link
Member

p0mvn commented Jul 28, 2022

@p0mvn sorry for my late response. I'm making this test cleaner.

No problem and no rush, just making sure that we are making progress here 👍

@ValarDragon
Copy link
Member

Woah thanks for this PR!

I think somethings off about having this many test vectors for authz in every module. Whats the exact authz property were trying to test for every message?

What I was hoping would be the case is that we can have a suite.RunStandardMessageTest[M: sdk.Msg](msg M), and then authz having completeness could be tested there. But we don't need to care about the exact sign bytes, we can trust the authz module to get that correct

@vuong177
Copy link
Contributor Author

vuong177 commented Aug 1, 2022

@ValarDragon This test is for checking serialize and de-serialize in MsgExec. Example in a gamm msg :

Current version:

{
  "account_number": "1",
  "chain_id": "foo",
  "fee": {
    "amount": [],
    "gas": "0"
  },
  "memo": "memo",
  "msgs": [
    {
      "type": "cosmos-sdk/MsgExec",
      "value": {
        "grantee": "cosmos1def",
        "msgs": [
          {
            "pool_id": "1",
            "sender": "osmo14kva2pk8xkpkal0v46yp4vsn27pylayq0zdlls",
            "share_in_amount": "100",
            "token_out_denom": "test",
            "token_out_min_amount": "100"
          }
        ]
      }
    }
  ],
  "sequence": "1",
  "timeout_height": "1"
}

This PR :

{
  "account_number": "1",
  "chain_id": "foo",
  "fee": {
    "amount": [],
    "gas": "0"
  },
  "memo": "memo",
  "msgs": [
    {
      "type": "cosmos-sdk/MsgExec",
      "value": {
        "grantee": "cosmos1def",
        "msgs": [
          {
            "type": "osmosis/gamm/exit-swap-share-amount-in",
            "value": {
              "pool_id": "1",
              "sender": "osmo14kva2pk8xkpkal0v46yp4vsn27pylayq0zdlls",
              "share_in_amount": "100",
              "token_out_denom": "test",
              "token_out_min_amount": "100"
            }
          }
        ]
      }
    }
  ],
  "sequence": "1",
  "timeout_height": "1"
}

@ValarDragon
Copy link
Member

What confuses is me is that there should be some generic validation logic for checking this -- if there wasn't, then there wouldn't have been any failures.

Can we just re-use that method (or write it), and then ensure prior_deserialization(correct_authz_serialization(msg)) doesn't return an error?

@vuong177
Copy link
Contributor Author

vuong177 commented Aug 1, 2022

What confuses is me is that there should be some generic validation logic for checking this -- if there wasn't, then there wouldn't have been any failures.

Can we just re-use that method (or write it), and then ensure prior_deserialization(correct_authz_serialization(msg)) doesn't return an error?

Oh great idea. I think we can do that. It makes this test more sense. Thank you @ValarDragon ! I'm going to make that.

@ValarDragon
Copy link
Member

Awesome, thank you so much! That will become much more maintainable as well =)

Comment on lines 971 to 1000
var (
mockMsgGrant authz.MsgGrant
mockMsgRevoke authz.MsgRevoke
mockMsgExec authz.MsgExec
)

// Authz: Grant Msg
typeURL := sdk.MsgTypeURL(tc.gammMsg)
grant, err := authz.NewGrant(someDate, authz.NewGenericAuthorization(typeURL), someDate.Add(time.Hour))
require.NoError(t, err)

msgGrant := authz.MsgGrant{Granter: mockGranter, Grantee: mockGrantee, Grant: grant}
msgGrantBytes := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgGrant)))
err = authzcodec.ModuleCdc.UnmarshalJSON(msgGrantBytes, &mockMsgGrant)
require.NoError(t, err)

// Authz: Revoke Msg
msgRevoke := authz.MsgRevoke{Granter: mockGranter, Grantee: mockGrantee, MsgTypeUrl: typeURL}
msgRevokeByte := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgRevoke)))
err = authzcodec.ModuleCdc.UnmarshalJSON(msgRevokeByte, &mockMsgRevoke)
require.NoError(t, err)

// Authz: Exec Msg
msgAny, _ := cdctypes.NewAnyWithValue(tc.gammMsg)
msgExec := authz.MsgExec{Grantee: mockGrantee, Msgs: []*cdctypes.Any{msgAny}}
execMsgByte := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgExec)))
err = authzcodec.ModuleCdc.UnmarshalJSON(execMsgByte, &mockMsgExec)
require.NoError(t, err)
require.Equal(t, msgExec.Msgs[0].Value, mockMsgExec.Msgs[0].Value)
})
Copy link
Member

@ValarDragon ValarDragon Aug 4, 2022

Choose a reason for hiding this comment

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

Can we move this all to apptesting function, and call it TestMessageSerialization ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure !

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I rename package in msg_test.go from types to types_test ? This is to avoid import cycle error.

Copy link
Member

Choose a reason for hiding this comment

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

ah yeah, this makes sense to do

@github-actions github-actions bot added the C:app-wiring Changes to the app folder label Aug 5, 2022
Copy link
Member

@ValarDragon ValarDragon left a comment

Choose a reason for hiding this comment

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

Ty for doing this! Looks good to me!

Note to others, diff is only large due to the _testing package switch, which makes sense

Lets add a changelog and this is good to go?

@github-actions github-actions bot added the C:docs Improvements or additions to documentation label Aug 5, 2022
@vuong177
Copy link
Contributor Author

vuong177 commented Aug 5, 2022

I added changelog. Thanks for your reviews !

Copy link
Member

@p0mvn p0mvn left a comment

Choose a reason for hiding this comment

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

LGTM overall, nice work @vuong177

I'm wondering though if it makes sense to separate the types package name change so that we can backport it to v11.x. Please let me know what you think

require.NoError(t, err)

// Authz: Exec Msg
msgAny, _ := cdctypes.NewAnyWithValue(msg)
Copy link
Member

Choose a reason for hiding this comment

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

Let's handle the error please

msg.TokenOutMinAmount = sdk.NewInt(0)
return msg
}),
expectPass: false,
},
{
name: "negative amount criteria",
msg: createMsg(func(msg MsgSwapExactAmountIn) MsgSwapExactAmountIn {
msg: createMsg(func(msg gammtypes.MsgSwapExactAmountIn) gammtypes.MsgSwapExactAmountIn {
Copy link
Member

Choose a reason for hiding this comment

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

q: Would it make sense to split up pass-through changes into a separate PR so that we can backport it?

Otherwise, backporting other work can get bottlenecked on this later

cc: @ValarDragon

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 agree with creating a new PR to change package name.

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'm going to create it.

Copy link
Member

@ValarDragon ValarDragon Aug 5, 2022

Choose a reason for hiding this comment

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

ah thats a great point, should also be a really quick review + merge

Ty for doing that vuong!

Copy link
Member

Choose a reason for hiding this comment

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

Thank you @vuong177 , merged #2316

x/gamm/types/msgs_test.go Show resolved Hide resolved
x/gamm/types/msgs_test.go Outdated Show resolved Hide resolved
appParams.SetAddressPrefixes()
pk1 := ed25519.GenPrivKey().PubKey()
addr1 := sdk.AccAddress(pk1.Address()).String()
coin := sdk.NewCoin("stake", sdk.NewInt(1))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
coin := sdk.NewCoin("stake", sdk.NewInt(1))
coin := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(1))

@ValarDragon
Copy link
Member

oh rip merge conflicts, can you try git merge main?

(Thanks for making the other PR off the osmosis repo, once this is on the repo, we can get these last steps handled with async round trip lol)

@vuong177
Copy link
Contributor Author

vuong177 commented Aug 6, 2022

yes. No conflict anymore!

@ValarDragon
Copy link
Member

Thanks for making this!-

@ValarDragon ValarDragon merged commit 9bc52d4 into osmosis-labs:main Aug 6, 2022
@czarcas7ic czarcas7ic mentioned this pull request Aug 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C:app-wiring Changes to the app folder C:docs Improvements or additions to documentation C:x/gamm Changes, features and bugs related to the gamm module. C:x/incentives C:x/lockup C:x/superfluid C:x/tokenfactory
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ensure all messages work with Authz
3 participants