Skip to content

Commit

Permalink
feat: add x/authz message parser (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
MonikaCat committed Jun 20, 2023
1 parent 232efea commit 3c8b1d8
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 17 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
uses: actions/checkout@v3

- name: Compute diff 📜
uses: technote-space/get-diff-action@v6.1.0
uses: technote-space/get-diff-action@v6.1.2
with:
SUFFIX_FILTER: |
.go
Expand All @@ -27,9 +27,9 @@ jobs:
- name: Setup Go 🧰
if: "env.GIT_DIFF != ''"
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: 1.19
go-version: "1.20"

- name: Run lint ✅
if: "env.GIT_DIFF != ''"
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ jobs:
uses: actions/checkout@v3

- name: Setup Go 🧰
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: 1.19
go-version: "1.20"

- name: Compute diff 📜
uses: technote-space/get-diff-action@v6.1.0
Expand Down
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
## Unreleased
### Changes
- ([\#97](https://github.com/forbole/juno/pull/97)) Add `x/authz` message parser

## v5.1.0
### Changes
- Bumped Go version to `1.20`
- Fixed a bug that caused messages type urls to be empty in the database
- Implemented a better default `EncodingConfigBuilder`
- Added the ability to set `DatabaseConfig` field values using setter methods
- ([\#96](https://github.com/forbole/juno/pull/96)) Export `Codec` and `LegacyAmino` from the `Database` implementation

## v5.0.0
### Changes
- Updated Cosmos SDK to `v0.47.2`

### Database
- ([\#96](https://github.com/forbole/juno/pull/96)) Exported Codec and LegacyAmino codec inside Database wrapper

## v4.2.0
### Changes
- ([\#93](https://github.com/forbole/juno/pull/93)) Decode IBC transfer data to JSON for `/ibc.core.channel.v1.MsgRecvPacket` message
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.19
require (
github.com/cometbft/cometbft v0.37.1
github.com/cosmos/cosmos-sdk v0.46.7
github.com/cosmos/gogoproto v1.4.2
github.com/cosmos/ibc-go/v5 v5.1.0
github.com/go-co-op/gocron v1.13.0
github.com/gogo/protobuf v1.3.3
Expand Down Expand Up @@ -69,7 +70,6 @@ require (
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/cosmos/cosmos-proto v1.0.0-alpha7 // indirect
github.com/cosmos/go-bip39 v1.0.0 // indirect
github.com/cosmos/gogoproto v1.4.2 // indirect
github.com/cosmos/gorocksdb v1.2.0 // indirect
github.com/cosmos/iavl v0.19.4 // indirect
github.com/cosmos/ledger-cosmos-go v0.12.1 // indirect
Expand Down
140 changes: 129 additions & 11 deletions modules/messages/account_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ package messages

import (
"fmt"
"strings"

"github.com/gogo/protobuf/proto"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
"github.com/cosmos/gogoproto/proto"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
authztypes "github.com/cosmos/cosmos-sdk/x/authz"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
crisistypes "github.com/cosmos/cosmos-sdk/x/crisis/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/types"
govtypesv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govtypesv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
ibctransfertypes "github.com/cosmos/ibc-go/v5/modules/apps/transfer/types"
Expand Down Expand Up @@ -51,10 +53,12 @@ var CosmosMessageAddressesParser = JoinMessageParsers(
CrisisMessagesParser,
DistributionMessagesParser,
EvidenceMessagesParser,
GovMessagesParser,
GovV1Beta1MessagesParser,
GovV1MessageParser,
IBCTransferMessagesParser,
SlashingMessagesParser,
StakingMessagesParser,
AuthzMessageParser,
DefaultMessagesParser,
)

Expand Down Expand Up @@ -137,32 +141,53 @@ func EvidenceMessagesParser(_ codec.Codec, cosmosMsg sdk.Msg) ([]string, error)
return nil, MessageNotSupported(cosmosMsg)
}

// GovMessagesParser returns the list of all the accounts involved in the given
// message if it's related to the x/gov module
func GovMessagesParser(cdc codec.Codec, cosmosMsg sdk.Msg) ([]string, error) {
// GovV1Beta1MessagesParser returns the list of all the accounts involved in the given
// message if it's related to the x/gov v1beta1 module
func GovV1Beta1MessagesParser(cdc codec.Codec, cosmosMsg sdk.Msg) ([]string, error) {
switch msg := cosmosMsg.(type) {

case *govtypesv1beta1.MsgSubmitProposal:
case *govv1beta1.MsgSubmitProposal:
addresses := []string{msg.Proposer}

var content govtypesv1beta1.Content
var content govv1beta1.Content
err := cdc.UnpackAny(msg.Content, &content)
if err != nil {
return nil, err
}

// Get addresses from contents
switch content := content.(type) {
//nolint:staticcheck // Let's keep this for the time being
case *distrtypes.CommunityPoolSpendProposal:
addresses = append(addresses, content.Recipient)
}

return addresses, nil

case *govtypesv1.MsgDeposit:
case *govv1beta1.MsgDeposit:
return []string{msg.Depositor}, nil

case *govtypesv1.MsgVote:
case *govv1beta1.MsgVote:
return []string{msg.Voter}, nil

}

return nil, MessageNotSupported(cosmosMsg)
}

// GovV1MessageParser returns the list of all the accounts involved in the given
// message if it's related to the x/gov v1 module
func GovV1MessageParser(cdc codec.Codec, cosmosMsg sdk.Msg) ([]string, error) {
switch msg := cosmosMsg.(type) {

case *govv1.MsgSubmitProposal:
addresses := []string{msg.Proposer}
return addresses, nil

case *govv1.MsgDeposit:
return []string{msg.Depositor}, nil

case *govv1.MsgVote:
return []string{msg.Voter}, nil

}
Expand Down Expand Up @@ -230,3 +255,96 @@ func StakingMessagesParser(_ codec.Codec, cosmosMsg sdk.Msg) ([]string, error) {

return nil, MessageNotSupported(cosmosMsg)
}

// AuthzMessageParser returns the list of all the accounts involved in the given
// message if it's related to the x/authz module
func AuthzMessageParser(c codec.Codec, cosmosMsg sdk.Msg) ([]string, error) {
switch msg := cosmosMsg.(type) {
case *authztypes.MsgGrant:
return []string{msg.Grantee, msg.Granter}, nil
case *authztypes.MsgRevoke:
return []string{msg.Grantee, msg.Granter}, nil
case *authztypes.MsgExec:
for _, index := range msg.Msgs {
var executedMsg sdk.Msg
if err := c.UnpackAny(index, &executedMsg); err != nil {
return nil, fmt.Errorf("error while unpacking message from authz: %s", err)
}

switch {
case strings.Contains(index.TypeUrl, "bank"):
addresses, err := BankMessagesParser(c, executedMsg)
if err != nil {
return nil, MessageNotSupported(executedMsg)
}
addresses = append(addresses, msg.Grantee)
return addresses, nil
case strings.Contains(index.TypeUrl, "crisis"):
addresses, err := CrisisMessagesParser(c, executedMsg)
if err != nil {
return nil, MessageNotSupported(executedMsg)
}
addresses = append(addresses, msg.Grantee)
return addresses, nil
case strings.Contains(index.TypeUrl, "distribution"):
addresses, err := DistributionMessagesParser(c, executedMsg)
if err != nil {
return nil, MessageNotSupported(executedMsg)
}
addresses = append(addresses, msg.Grantee)
return addresses, nil
case strings.Contains(index.TypeUrl, "evidence"):
addresses, err := EvidenceMessagesParser(c, executedMsg)
if err != nil {
return nil, MessageNotSupported(executedMsg)
}
addresses = append(addresses, msg.Grantee)
return addresses, nil
case strings.Contains(index.TypeUrl, "gov.v1beta1"):
addresses, err := GovV1Beta1MessagesParser(c, executedMsg)
if err != nil {
return nil, MessageNotSupported(executedMsg)
}
addresses = append(addresses, msg.Grantee)
return addresses, nil
case strings.Contains(index.TypeUrl, "gov.v1."):
addresses, err := GovV1MessageParser(c, executedMsg)
if err != nil {
return nil, MessageNotSupported(executedMsg)
}
addresses = append(addresses, msg.Grantee)
return addresses, nil
case strings.Contains(index.TypeUrl, "ibc"):
addresses, err := IBCTransferMessagesParser(c, executedMsg)
if err != nil {
return nil, MessageNotSupported(executedMsg)
}
addresses = append(addresses, msg.Grantee)
return addresses, nil
case strings.Contains(index.TypeUrl, "slashing"):
addresses, err := SlashingMessagesParser(c, executedMsg)
if err != nil {
return nil, MessageNotSupported(executedMsg)
}
addresses = append(addresses, msg.Grantee)
return addresses, nil
case strings.Contains(index.TypeUrl, "staking"):
addresses, err := StakingMessagesParser(c, executedMsg)
if err != nil {
return nil, MessageNotSupported(executedMsg)
}
addresses = append(addresses, msg.Grantee)
return addresses, nil
default:
addresses, err := DefaultMessagesParser(c, executedMsg)
if err != nil {
return nil, MessageNotSupported(executedMsg)
}
addresses = append(addresses, msg.Grantee)
return addresses, nil
}
}
}

return nil, MessageNotSupported(cosmosMsg)
}

0 comments on commit 3c8b1d8

Please sign in to comment.