Skip to content

Commit

Permalink
Merge pull request #69 from thesixnetwork/develop
Browse files Browse the repository at this point in the history
🐛 validate action and parameter length
  • Loading branch information
dDeedev authored Feb 15, 2023
2 parents ed7f461 + a00e14e commit 33a96d0
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions x/nftmngr/keeper/msg_server_perform_multi_token_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -304,7 +303,7 @@ func (k msgServer) PerformMultiTokenOneAction(goCtx context.Context, msg *types.

// Emit events on metadata change
// Check action with reference exists
refId := msg.RefId + "token_id_" + tokenId
refId := msg.RefId + "_token-id_" + tokenId
if msg.RefId != "" {

_, found := k.Keeper.GetActionByRefId(ctx, refId)
Expand Down Expand Up @@ -347,8 +346,8 @@ func (k msgServer) PerformMultiTokenOneAction(goCtx context.Context, msg *types.
func (k msgServer) PerformMultiTokenMultiAction(goCtx context.Context, msg *types.MsgPerformMultiTokenMultiAction) (*types.MsgPerformMultiTokenMultiActionResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// check action len and parameters len are suitable
if len(msg.Action) != len(msg.Parameters[0]) {
return nil, sdkerrors.Wrap(types.ErrActionAndParametersNotMatch, "Action: "+strconv.Itoa(len(msg.Action))+" Parameters: "+strconv.Itoa(len(msg.Parameters[0])))
if len(msg.Action) != len(msg.Parameters) {
return nil, sdkerrors.Wrap(types.ErrActionAndParametersNotMatch, "Action: "+string(int32(len(msg.Action)))+" Parameters: "+string(int32(len(msg.Parameters))))
}

// ** SCHEMA LAYER **
Expand Down Expand Up @@ -409,7 +408,7 @@ func (k msgServer) PerformMultiTokenMultiAction(goCtx context.Context, msg *type
var actionPrams_ []*types.ActionParameter
err := json.Unmarshal([]byte(params_), &actionPrams_)
if err != nil {
sdkerrors.Wrap(types.ErrInvalidParameter, "Error in Unmarshal: "+err.Error())
sdkerrors.Wrap(types.ErrInvalidParameter, "Error in Unmarshal required parameters ")
}
// Check if action requires parameters
param := mapAction[index].GetParams()
Expand All @@ -426,11 +425,11 @@ func (k msgServer) PerformMultiTokenMultiAction(goCtx context.Context, msg *type
}

for i := 0; i < len(required_param); i++ {
if actionPrams_[index].Name != required_param[i].Name {
if actionPrams_[i].Name != required_param[i].Name {
return nil, sdkerrors.Wrap(types.ErrInvalidParameter, "input paramter name is not match to "+required_param[i].Name)
}
if actionPrams_[index].Value == "" {
actionPrams_[index].Value = required_param[i].DefaultValue
if actionPrams_[i].Value == "" {
actionPrams_[i].Value = required_param[i].DefaultValue
}
}

Expand All @@ -448,7 +447,7 @@ func (k msgServer) PerformMultiTokenMultiAction(goCtx context.Context, msg *type
var actionPrams_ []*types.ActionParameter
err := json.Unmarshal([]byte(msg.Parameters[index]), &actionPrams_)
if err != nil {
sdkerrors.Wrap(types.ErrInvalidParameter, "Error in Unmarshal: "+err.Error())
sdkerrors.Wrap(types.ErrInvalidParameter, "Error in Unmarshal: TOKEN DATA LAYER cannot unmarshal parameters")
}

// Create map of existing attribute in nftdata
Expand Down Expand Up @@ -508,7 +507,7 @@ func (k msgServer) PerformMultiTokenMultiAction(goCtx context.Context, msg *type

// Emit events on metadata change
// Check action with reference exists
refId := msg.RefId + "token_id_" + tokenId
refId := msg.RefId + "_token-id_" + tokenId + "_action-id_" + string(rune(index))
if msg.RefId != "" {

_, found := k.Keeper.GetActionByRefId(ctx, refId)
Expand Down Expand Up @@ -551,8 +550,8 @@ func (k msgServer) PerformMultiTokenMultiAction(goCtx context.Context, msg *type
func (k msgServer) PerformOneTokenMultiAction(goCtx context.Context, msg *types.MsgPerformOneTokenMultiAction) (*types.MsgPerformOneTokenMultiActionResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// check action len and parameters len are suitable
if len(msg.Action) != len(msg.Parameters[0]) {
return nil, sdkerrors.Wrap(types.ErrActionAndParametersNotMatch, "Action: "+strconv.Itoa(len(msg.Action))+" Parameters: "+strconv.Itoa(len(msg.Parameters[0])))
if len(msg.Action) != len(msg.Parameters) {
return nil, sdkerrors.Wrap(types.ErrActionAndParametersNotMatch, "Action: "+string(int32(len(msg.Action)))+" Parameters: "+string(int32(len(msg.Parameters))))
}

// ** SCHEMA LAYER **
Expand Down Expand Up @@ -611,7 +610,7 @@ func (k msgServer) PerformOneTokenMultiAction(goCtx context.Context, msg *types.
var actionPrams_ []*types.ActionParameter
err := json.Unmarshal([]byte(params_), &actionPrams_)
if err != nil {
sdkerrors.Wrap(types.ErrInvalidParameter, "Error in Unmarshal: "+err.Error())
sdkerrors.Wrap(types.ErrInvalidParameter, "Error in Unmarshal parameters index")
}
// Check if action requires parameters
param := mapAction[index].GetParams()
Expand All @@ -628,25 +627,26 @@ func (k msgServer) PerformOneTokenMultiAction(goCtx context.Context, msg *types.
}

for i := 0; i < len(required_param); i++ {
if actionPrams_[index].Name != required_param[i].Name {
if actionPrams_[i].Name != required_param[i].Name {
return nil, sdkerrors.Wrap(types.ErrInvalidParameter, "input paramter name is not match to "+required_param[i].Name)
}
if actionPrams_[index].Value == "" {
actionPrams_[index].Value = required_param[i].DefaultValue
if actionPrams_[i].Value == "" {
actionPrams_[i].Value = required_param[i].DefaultValue
}
}

}

// ** TOKEN DATA LAYER **
// iterate over token ids
count := 0
for index, msg_ := range msg.Action {
_ = msg_ // unused
// unmarshal parameters
var actionPrams_ []*types.ActionParameter
err := json.Unmarshal([]byte(msg.Parameters[index]), &actionPrams_)
if err != nil {
sdkerrors.Wrap(types.ErrInvalidParameter, "Error in Unmarshal: "+err.Error())
sdkerrors.Wrap(types.ErrInvalidParameter, "Error in Unmarshal: TOKEN DATA LAYER index msg.Action")
}

// Create map of existing attribute in nftdata
Expand Down Expand Up @@ -706,7 +706,7 @@ func (k msgServer) PerformOneTokenMultiAction(goCtx context.Context, msg *types.

// Emit events on metadata change
// Check action with reference exists
refId := msg.RefId + mapAction[index].Name + msg.TokenId
refId := msg.RefId + "_action-id_" + string(int32(count))
if msg.RefId != "" {

_, found := k.Keeper.GetActionByRefId(ctx, refId)
Expand Down Expand Up @@ -737,7 +737,7 @@ func (k msgServer) PerformOneTokenMultiAction(goCtx context.Context, msg *types.
sdk.NewAttribute(types.AttributeKeyRunActionChangeList, string(changeList)),
),
)

count++
}

return &types.MsgPerformOneTokenMultiActionResponse{
Expand Down

0 comments on commit 33a96d0

Please sign in to comment.