2018-03-22 10:47:13 -07:00
|
|
|
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-03-22 10:47:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
func handleMsgMine(ctx sdk.Context, pk Keeper, msg MsgMine) 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-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{}
|
|
|
|
}
|