Skip to content

Commit

Permalink
move scripts map to auth and call from handler
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoyangLiu committed Jul 1, 2019
1 parent 2bb80c7 commit 118cb73
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
17 changes: 17 additions & 0 deletions x/auth/script.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package auth

import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

type Script func(ctx sdk.Context, tx sdk.Msg) sdk.Error

var scriptsCenter = map[string][]Script{}

func RegisterScripts(msgType string, scripts...Script) {
scriptsCenter[msgType] = append(scriptsCenter[msgType], scripts...)
}

func GetRegisteredScripts(msgType string) []Script {
return scriptsCenter[msgType]
}
9 changes: 9 additions & 0 deletions x/bank/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bank

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
)

// NewHandler returns a handler for "bank" type messages.
Expand All @@ -20,6 +21,14 @@ func NewHandler(k Keeper) sdk.Handler {
// Handle MsgSend.
func handleMsgSend(ctx sdk.Context, k Keeper, msg MsgSend) sdk.Result {
// NOTE: totalIn == totalOut should already have been checked
for _, script := range auth.GetRegisteredScripts(msg.Type()) {
if script == nil {
continue
}
if err := script(ctx, msg); err != nil {
return err.Result()
}
}

tags, err := k.InputOutputCoins(ctx, msg.Inputs, msg.Outputs)
if err != nil {
Expand Down

0 comments on commit 118cb73

Please sign in to comment.