From d8470543ac2ee9c9051b8549f105077c01886128 Mon Sep 17 00:00:00 2001 From: Paul Date: Tue, 28 Jun 2022 19:05:31 +0800 Subject: [PATCH 1/4] feat: update wasm parser --- cosmwasm/msg_parser.go | 43 +++++++++++++++++++++++++++++++++------- cosmwasm/query_parser.go | 43 +++++++++++++++++++++++++++++++++------- 2 files changed, 72 insertions(+), 14 deletions(-) diff --git a/cosmwasm/msg_parser.go b/cosmwasm/msg_parser.go index 49ab5b60fc..2ff957f854 100644 --- a/cosmwasm/msg_parser.go +++ b/cosmwasm/msg_parser.go @@ -39,21 +39,50 @@ func NewParserRouter() ParserRouter { } type CustomMsg struct { - Route string `json:"route"` - MsgData json.RawMessage `json:"msg_data"` + Profiles *json.RawMessage `json:"profiles"` + Relationships *json.RawMessage `json:"relationships"` + Subspaces *json.RawMessage `json:"subspaces"` + Posts *json.RawMessage `json:"posts"` + Reports *json.RawMessage `json:"reports"` + Reactions *json.RawMessage `json:"reactions"` } func (router ParserRouter) ParseCustom(contractAddr sdk.AccAddress, data json.RawMessage) ([]sdk.Msg, error) { var customMsg CustomMsg err := json.Unmarshal(data, &customMsg) - if err != nil { return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error()) } - - if parser, ok := router.Parsers[customMsg.Route]; ok { - return parser.ParseCustomMsgs(contractAddr, customMsg.MsgData) + // get route and msg from data + var route string + var msg json.RawMessage + if customMsg.Profiles != nil { + route = WasmMsgParserRouteProfiles + msg = *customMsg.Profiles + } + if customMsg.Subspaces != nil { + route = QueryRouteSubspaces + msg = *customMsg.Subspaces + } + if customMsg.Relationships != nil { + route = WasmMsgParserRouteRelationships + msg = *customMsg.Relationships + } + if customMsg.Posts != nil { + route = WasmMsgParserRoutePosts + msg = *customMsg.Posts + } + if customMsg.Reports != nil { + route = WasmMsgParserRouteReports + msg = *customMsg.Reports + } + if customMsg.Reactions != nil { + route = WasmMsgParserRouteReactions + msg = *customMsg.Reactions } - return nil, sdkerrors.Wrap(wasm.ErrInvalidMsg, customMsg.Route) + if parser, ok := router.Parsers[route]; ok { + return parser.ParseCustomMsgs(contractAddr, msg) + } + return nil, sdkerrors.Wrap(wasm.ErrInvalidMsg, "unimplemented route") } diff --git a/cosmwasm/query_parser.go b/cosmwasm/query_parser.go index 31438a7433..12426de137 100644 --- a/cosmwasm/query_parser.go +++ b/cosmwasm/query_parser.go @@ -30,8 +30,12 @@ func NewQuerier(queriers map[string]Querier) QuerierRouter { } type CustomQuery struct { - Route string `json:"route"` - QueryData json.RawMessage `json:"query_data"` + Profiles *json.RawMessage `json:"profiles"` + Relationships *json.RawMessage `json:"relationships"` + Subspaces *json.RawMessage `json:"subspaces"` + Posts *json.RawMessage `json:"posts"` + Reports *json.RawMessage `json:"reports"` + Reactions *json.RawMessage `json:"reactions"` } const ( @@ -46,14 +50,39 @@ const ( func (q QuerierRouter) QueryCustom(ctx sdk.Context, data json.RawMessage) ([]byte, error) { var customQuery CustomQuery err := json.Unmarshal(data, &customQuery) - if err != nil { return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error()) } - - if querier, ok := q.Queriers[customQuery.Route]; ok { - return querier.QueryCustom(ctx, customQuery.QueryData) + // get route and query from data + var route string + var query json.RawMessage + if customQuery.Profiles != nil { + route = QueryRouteProfiles + query = *customQuery.Profiles + } + if customQuery.Subspaces != nil { + route = QueryRouteSubspaces + query = *customQuery.Subspaces + } + if customQuery.Relationships != nil { + route = QueryRouteRelationships + query = *customQuery.Relationships + } + if customQuery.Posts != nil { + route = QueryRoutePosts + query = *customQuery.Posts + } + if customQuery.Reports != nil { + route = QueryRouteReports + query = *customQuery.Reports + } + if customQuery.Reactions != nil { + route = QueryRouteReactions + query = *customQuery.Reactions } - return nil, sdkerrors.Wrap(wasm.ErrQueryFailed, customQuery.Route) + if querier, ok := q.Queriers[route]; ok { + return querier.QueryCustom(ctx, query) + } + return nil, sdkerrors.Wrap(wasm.ErrQueryFailed, "unimplemented route") } From 2edfab9da2235b0bb03d3c5ae11d17a75e4be607 Mon Sep 17 00:00:00 2001 From: Paul Date: Tue, 28 Jun 2022 19:11:16 +0800 Subject: [PATCH 2/4] chore: fix the order of modules --- cosmwasm/msg_parser.go | 2 +- cosmwasm/query_parser.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cosmwasm/msg_parser.go b/cosmwasm/msg_parser.go index 2ff957f854..fd32b35780 100644 --- a/cosmwasm/msg_parser.go +++ b/cosmwasm/msg_parser.go @@ -40,8 +40,8 @@ func NewParserRouter() ParserRouter { type CustomMsg struct { Profiles *json.RawMessage `json:"profiles"` - Relationships *json.RawMessage `json:"relationships"` Subspaces *json.RawMessage `json:"subspaces"` + Relationships *json.RawMessage `json:"relationships"` Posts *json.RawMessage `json:"posts"` Reports *json.RawMessage `json:"reports"` Reactions *json.RawMessage `json:"reactions"` diff --git a/cosmwasm/query_parser.go b/cosmwasm/query_parser.go index 12426de137..956c230a51 100644 --- a/cosmwasm/query_parser.go +++ b/cosmwasm/query_parser.go @@ -31,8 +31,8 @@ func NewQuerier(queriers map[string]Querier) QuerierRouter { type CustomQuery struct { Profiles *json.RawMessage `json:"profiles"` - Relationships *json.RawMessage `json:"relationships"` Subspaces *json.RawMessage `json:"subspaces"` + Relationships *json.RawMessage `json:"relationships"` Posts *json.RawMessage `json:"posts"` Reports *json.RawMessage `json:"reports"` Reactions *json.RawMessage `json:"reactions"` From dc06486dfe86e88dd43a35f78be7803bd91e0588 Mon Sep 17 00:00:00 2001 From: Paul Date: Tue, 28 Jun 2022 21:40:57 +0800 Subject: [PATCH 3/4] Update cosmwasm/msg_parser.go Co-authored-by: Leonardo Bragagnolo --- cosmwasm/msg_parser.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cosmwasm/msg_parser.go b/cosmwasm/msg_parser.go index fd32b35780..bd34357b65 100644 --- a/cosmwasm/msg_parser.go +++ b/cosmwasm/msg_parser.go @@ -61,7 +61,7 @@ func (router ParserRouter) ParseCustom(contractAddr sdk.AccAddress, data json.Ra msg = *customMsg.Profiles } if customMsg.Subspaces != nil { - route = QueryRouteSubspaces + route = WasmMsgParserRouteSubspaces msg = *customMsg.Subspaces } if customMsg.Relationships != nil { From 227a73a4633d721068f96adcbea937e044215fb7 Mon Sep 17 00:00:00 2001 From: Paul Date: Tue, 28 Jun 2022 21:52:05 +0800 Subject: [PATCH 4/4] chore: improve code structure --- cosmwasm/msg_parser.go | 18 +++++++----------- cosmwasm/query_parser.go | 18 +++++++----------- 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/cosmwasm/msg_parser.go b/cosmwasm/msg_parser.go index bd34357b65..8c08b03f26 100644 --- a/cosmwasm/msg_parser.go +++ b/cosmwasm/msg_parser.go @@ -56,27 +56,23 @@ func (router ParserRouter) ParseCustom(contractAddr sdk.AccAddress, data json.Ra // get route and msg from data var route string var msg json.RawMessage - if customMsg.Profiles != nil { + switch { + case customMsg.Profiles != nil: route = WasmMsgParserRouteProfiles msg = *customMsg.Profiles - } - if customMsg.Subspaces != nil { + case customMsg.Subspaces != nil: route = WasmMsgParserRouteSubspaces msg = *customMsg.Subspaces - } - if customMsg.Relationships != nil { + case customMsg.Relationships != nil: route = WasmMsgParserRouteRelationships msg = *customMsg.Relationships - } - if customMsg.Posts != nil { + case customMsg.Posts != nil: route = WasmMsgParserRoutePosts msg = *customMsg.Posts - } - if customMsg.Reports != nil { + case customMsg.Reports != nil: route = WasmMsgParserRouteReports msg = *customMsg.Reports - } - if customMsg.Reactions != nil { + case customMsg.Reactions != nil: route = WasmMsgParserRouteReactions msg = *customMsg.Reactions } diff --git a/cosmwasm/query_parser.go b/cosmwasm/query_parser.go index 956c230a51..7900e52da7 100644 --- a/cosmwasm/query_parser.go +++ b/cosmwasm/query_parser.go @@ -56,27 +56,23 @@ func (q QuerierRouter) QueryCustom(ctx sdk.Context, data json.RawMessage) ([]byt // get route and query from data var route string var query json.RawMessage - if customQuery.Profiles != nil { + switch { + case customQuery.Profiles != nil: route = QueryRouteProfiles query = *customQuery.Profiles - } - if customQuery.Subspaces != nil { + case customQuery.Subspaces != nil: route = QueryRouteSubspaces query = *customQuery.Subspaces - } - if customQuery.Relationships != nil { + case customQuery.Relationships != nil: route = QueryRouteRelationships query = *customQuery.Relationships - } - if customQuery.Posts != nil { + case customQuery.Posts != nil: route = QueryRoutePosts query = *customQuery.Posts - } - if customQuery.Reports != nil { + case customQuery.Reports != nil: route = QueryRouteReports query = *customQuery.Reports - } - if customQuery.Reactions != nil { + case customQuery.Reactions != nil: route = QueryRouteReactions query = *customQuery.Reactions }