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

43 lines
911 B
Go
Raw Normal View History

package pow
import (
"reflect"
sdk "github.com/cosmos/cosmos-sdk/types"
)
2018-03-24 13:41:26 -07:00
func (pk Keeper) Handler(ctx sdk.Context, msg sdk.Msg) sdk.Result {
switch msg := msg.(type) {
case MineMsg:
return handleMineMsg(ctx, pk, msg)
default:
errMsg := "Unrecognized pow Msg type: " + reflect.TypeOf(msg).Name()
return sdk.ErrUnknownRequest(errMsg).Result()
}
}
2018-03-24 13:41:26 -07:00
func handleMineMsg(ctx sdk.Context, pk Keeper, msg MineMsg) 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-04-05 06:16:54 -07:00
// commented for now, makes testing difficult
// TODO figure out a better test method that allows early CheckTx return
/*
if ctx.IsCheckTx() {
return sdk.Result{} // TODO
}
*/
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{}
}