cosmos-sdk/examples/democoin/x/pow/handler.go

36 lines
738 B
Go
Raw Normal View History

package pow
import (
"reflect"
sdk "github.com/cosmos/cosmos-sdk/types"
)
2018-04-18 21:49:24 -07:00
// POW handler
2018-03-24 13:41:26 -07:00
func (pk Keeper) Handler(ctx sdk.Context, msg sdk.Msg) sdk.Result {
switch msg := msg.(type) {
2018-04-18 21:49:24 -07:00
case MsgMine:
return handleMsgMine(ctx, pk, msg)
2018-03-24 13:41:26 -07:00
default:
errMsg := "Unrecognized pow Msg type: " + reflect.TypeOf(msg).Name()
return sdk.ErrUnknownRequest(errMsg).Result()
}
}
2018-04-18 21:49:24 -07:00
func handleMsgMine(ctx sdk.Context, pk Keeper, msg MsgMine) sdk.Result {
// precondition: msg has passed ValidateBasic
2018-03-24 13:41:26 -07:00
newDiff, newCount, err := pk.CheckValid(ctx, msg.Difficulty, msg.Count)
if err != nil {
2018-03-24 13:41:26 -07:00
return err.Result()
}
2018-03-24 13:41:26 -07:00
err = pk.ApplyValid(ctx, msg.Sender, newDiff, newCount)
if err != nil {
return err.Result()
}
return sdk.Result{}
}