2018-03-22 10:47:13 -07:00
|
|
|
package pow
|
|
|
|
|
|
|
|
import (
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
// TODO remove, seems hacky
|
2018-03-22 10:47:13 -07:00
|
|
|
type CodeType = sdk.CodeType
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
// POW errors reserve 200 ~ 299
|
2018-03-22 10:47:13 -07:00
|
|
|
const (
|
2018-04-18 21:49:24 -07:00
|
|
|
DefaultCodespace sdk.CodespaceType = 5
|
|
|
|
CodeInvalidDifficulty CodeType = 201
|
|
|
|
CodeNonexistentDifficulty CodeType = 202
|
|
|
|
CodeNonexistentReward CodeType = 203
|
|
|
|
CodeNonexistentCount CodeType = 204
|
|
|
|
CodeInvalidProof CodeType = 205
|
|
|
|
CodeNotBelowTarget CodeType = 206
|
|
|
|
CodeInvalidCount CodeType = 207
|
|
|
|
CodeUnknownRequest CodeType = sdk.CodeUnknownRequest
|
2018-03-22 10:47:13 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func codeToDefaultMsg(code CodeType) string {
|
|
|
|
switch code {
|
|
|
|
case CodeInvalidDifficulty:
|
2018-06-13 12:13:22 -07:00
|
|
|
return "insuffient difficulty"
|
2018-03-22 10:47:13 -07:00
|
|
|
case CodeNonexistentDifficulty:
|
2018-06-13 12:13:22 -07:00
|
|
|
return "nonexistent difficulty"
|
2018-03-22 10:47:13 -07:00
|
|
|
case CodeNonexistentReward:
|
2018-06-13 12:13:22 -07:00
|
|
|
return "nonexistent reward"
|
2018-03-22 10:47:13 -07:00
|
|
|
case CodeNonexistentCount:
|
2018-06-13 12:13:22 -07:00
|
|
|
return "nonexistent count"
|
2018-03-22 10:47:13 -07:00
|
|
|
case CodeInvalidProof:
|
2018-06-13 12:13:22 -07:00
|
|
|
return "invalid proof"
|
2018-03-22 10:47:13 -07:00
|
|
|
case CodeNotBelowTarget:
|
2018-06-13 12:13:22 -07:00
|
|
|
return "not below target"
|
2018-03-22 10:47:13 -07:00
|
|
|
case CodeInvalidCount:
|
2018-06-13 12:13:22 -07:00
|
|
|
return "invalid count"
|
2018-03-22 10:47:13 -07:00
|
|
|
case CodeUnknownRequest:
|
2018-06-13 12:13:22 -07:00
|
|
|
return "unknown request"
|
2018-03-22 10:47:13 -07:00
|
|
|
default:
|
|
|
|
return sdk.CodeToDefaultMsg(code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
// nolint
|
2018-04-17 19:16:21 -07:00
|
|
|
func ErrInvalidDifficulty(codespace sdk.CodespaceType, msg string) sdk.Error {
|
|
|
|
return newError(codespace, CodeInvalidDifficulty, msg)
|
2018-03-22 10:47:13 -07:00
|
|
|
}
|
2018-04-17 19:16:21 -07:00
|
|
|
func ErrNonexistentDifficulty(codespace sdk.CodespaceType) sdk.Error {
|
|
|
|
return newError(codespace, CodeNonexistentDifficulty, "")
|
2018-03-22 10:47:13 -07:00
|
|
|
}
|
2018-04-17 19:16:21 -07:00
|
|
|
func ErrNonexistentReward(codespace sdk.CodespaceType) sdk.Error {
|
|
|
|
return newError(codespace, CodeNonexistentReward, "")
|
2018-03-22 10:47:13 -07:00
|
|
|
}
|
2018-04-17 19:16:21 -07:00
|
|
|
func ErrNonexistentCount(codespace sdk.CodespaceType) sdk.Error {
|
|
|
|
return newError(codespace, CodeNonexistentCount, "")
|
2018-03-22 10:47:13 -07:00
|
|
|
}
|
2018-04-17 19:16:21 -07:00
|
|
|
func ErrInvalidProof(codespace sdk.CodespaceType, msg string) sdk.Error {
|
|
|
|
return newError(codespace, CodeInvalidProof, msg)
|
2018-03-22 10:47:13 -07:00
|
|
|
}
|
2018-04-17 19:16:21 -07:00
|
|
|
func ErrNotBelowTarget(codespace sdk.CodespaceType, msg string) sdk.Error {
|
|
|
|
return newError(codespace, CodeNotBelowTarget, msg)
|
2018-03-22 10:47:13 -07:00
|
|
|
}
|
2018-04-17 19:16:21 -07:00
|
|
|
func ErrInvalidCount(codespace sdk.CodespaceType, msg string) sdk.Error {
|
|
|
|
return newError(codespace, CodeInvalidCount, msg)
|
2018-03-22 10:47:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func msgOrDefaultMsg(msg string, code CodeType) string {
|
|
|
|
if msg != "" {
|
|
|
|
return msg
|
|
|
|
}
|
2018-04-18 21:49:24 -07:00
|
|
|
return codeToDefaultMsg(code)
|
2018-03-22 10:47:13 -07:00
|
|
|
}
|
|
|
|
|
2018-04-17 19:16:21 -07:00
|
|
|
func newError(codespace sdk.CodespaceType, code CodeType, msg string) sdk.Error {
|
2018-03-22 10:47:13 -07:00
|
|
|
msg = msgOrDefaultMsg(msg, code)
|
2018-04-17 19:16:21 -07:00
|
|
|
return sdk.NewError(codespace, code, msg)
|
2018-03-22 10:47:13 -07:00
|
|
|
}
|