2018-03-22 10:47:13 -07:00
|
|
|
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-22 10:47:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-24 13:41:26 -07:00
|
|
|
func handleMineMsg(ctx sdk.Context, pk Keeper, msg MineMsg) sdk.Result {
|
2018-03-22 10:47:13 -07:00
|
|
|
|
|
|
|
// precondition: msg has passed ValidateBasic
|
|
|
|
|
2018-03-24 13:41:26 -07:00
|
|
|
newDiff, newCount, err := pk.CheckValid(ctx, msg.Difficulty, msg.Count)
|
2018-03-22 10:47:13 -07:00
|
|
|
if err != nil {
|
2018-03-24 13:41:26 -07:00
|
|
|
return err.Result()
|
2018-03-22 10:47:13 -07:00
|
|
|
}
|
|
|
|
|
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-22 10:47:13 -07:00
|
|
|
|
2018-03-24 13:41:26 -07:00
|
|
|
err = pk.ApplyValid(ctx, msg.Sender, newDiff, newCount)
|
|
|
|
if err != nil {
|
|
|
|
return err.Result()
|
2018-03-22 10:47:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return sdk.Result{}
|
|
|
|
}
|