Merge pull request #642 from cosmos/sunny/errors
move some errors to sdk from bank
This commit is contained in:
commit
984aff12da
|
@ -99,13 +99,13 @@ func TestMsgs(t *testing.T) {
|
|||
|
||||
// Run a Check
|
||||
cres := bapp.CheckTx(txBytes)
|
||||
assert.Equal(t, sdk.CodeUnrecognizedAddress,
|
||||
assert.Equal(t, sdk.CodeUnknownAddress,
|
||||
sdk.CodeType(cres.Code), "i: %v, log: %v", i, cres.Log)
|
||||
|
||||
// Simulate a Block
|
||||
bapp.BeginBlock(abci.RequestBeginBlock{})
|
||||
dres := bapp.DeliverTx(txBytes)
|
||||
assert.Equal(t, sdk.CodeUnrecognizedAddress,
|
||||
assert.Equal(t, sdk.CodeUnknownAddress,
|
||||
sdk.CodeType(dres.Code), "i: %v, log: %v", i, dres.Log)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,7 +37,7 @@ func (msg SetTrendMsg) String() string {
|
|||
// Validate Basic is used to quickly disqualify obviously invalid messages quickly
|
||||
func (msg SetTrendMsg) ValidateBasic() sdk.Error {
|
||||
if len(msg.Sender) == 0 {
|
||||
return sdk.ErrUnrecognizedAddress(msg.Sender.String()).Trace("")
|
||||
return sdk.ErrUnknownAddress(msg.Sender.String()).Trace("")
|
||||
}
|
||||
if strings.Contains(msg.Cool, "hot") {
|
||||
return sdk.ErrUnauthorized("").Trace("hot is not cool")
|
||||
|
@ -88,7 +88,7 @@ func (msg QuizMsg) String() string {
|
|||
// Validate Basic is used to quickly disqualify obviously invalid messages quickly
|
||||
func (msg QuizMsg) ValidateBasic() sdk.Error {
|
||||
if len(msg.Sender) == 0 {
|
||||
return sdk.ErrUnrecognizedAddress(msg.Sender.String()).Trace("")
|
||||
return sdk.ErrUnknownAddress(msg.Sender.String()).Trace("")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -19,15 +19,18 @@ func (code CodeType) IsOK() bool {
|
|||
// ABCI Response Codes
|
||||
// Base SDK reserves 0 ~ 99.
|
||||
const (
|
||||
CodeOK CodeType = 0
|
||||
CodeInternal CodeType = 1
|
||||
CodeTxParse CodeType = 2
|
||||
CodeInvalidSequence CodeType = 3
|
||||
CodeUnauthorized CodeType = 4
|
||||
CodeInsufficientFunds CodeType = 5
|
||||
CodeUnknownRequest CodeType = 6
|
||||
CodeUnrecognizedAddress CodeType = 7
|
||||
CodeInvalidPubKey CodeType = 8
|
||||
CodeOK CodeType = 0
|
||||
CodeInternal CodeType = 1
|
||||
CodeTxParse CodeType = 2
|
||||
CodeInvalidSequence CodeType = 3
|
||||
CodeUnauthorized CodeType = 4
|
||||
CodeInsufficientFunds CodeType = 5
|
||||
CodeUnknownRequest CodeType = 6
|
||||
CodeInvalidAddress CodeType = 7
|
||||
CodeInvalidPubKey CodeType = 8
|
||||
CodeUnknownAddress CodeType = 9
|
||||
CodeInsufficientCoins CodeType = 10
|
||||
CodeInvalidCoins CodeType = 11
|
||||
|
||||
CodeGenesisParse CodeType = 0xdead // TODO: remove ?
|
||||
)
|
||||
|
@ -49,10 +52,14 @@ func CodeToDefaultMsg(code CodeType) string {
|
|||
return "Insufficent funds"
|
||||
case CodeUnknownRequest:
|
||||
return "Unknown request"
|
||||
case CodeUnrecognizedAddress:
|
||||
return "Unrecognized address"
|
||||
case CodeInvalidAddress:
|
||||
return "Invalid address"
|
||||
case CodeInvalidPubKey:
|
||||
return "Invalid pubkey"
|
||||
case CodeInsufficientCoins:
|
||||
return "Insufficient coins"
|
||||
case CodeInvalidCoins:
|
||||
return "Invalid coins"
|
||||
default:
|
||||
return fmt.Sprintf("Unknown code %d", code)
|
||||
}
|
||||
|
@ -84,12 +91,21 @@ func ErrInsufficientFunds(msg string) Error {
|
|||
func ErrUnknownRequest(msg string) Error {
|
||||
return newError(CodeUnknownRequest, msg)
|
||||
}
|
||||
func ErrUnrecognizedAddress(msg string) Error {
|
||||
return newError(CodeUnrecognizedAddress, msg)
|
||||
func ErrInvalidAddress(msg string) Error {
|
||||
return newError(CodeInvalidAddress, msg)
|
||||
}
|
||||
func ErrUnknownAddress(msg string) Error {
|
||||
return newError(CodeUnknownAddress, msg)
|
||||
}
|
||||
func ErrInvalidPubKey(msg string) Error {
|
||||
return newError(CodeInvalidPubKey, msg)
|
||||
}
|
||||
func ErrInsufficientCoins(msg string) Error {
|
||||
return newError(CodeInsufficientCoins, msg)
|
||||
}
|
||||
func ErrInvalidCoins(msg string) Error {
|
||||
return newError(CodeInvalidCoins, msg)
|
||||
}
|
||||
|
||||
//----------------------------------------
|
||||
// Error & sdkError
|
||||
|
|
|
@ -104,7 +104,7 @@ func processSig(
|
|||
// Get the account.
|
||||
acc = am.GetAccount(ctx, addr)
|
||||
if acc == nil {
|
||||
return nil, sdk.ErrUnrecognizedAddress(addr.String()).Result()
|
||||
return nil, sdk.ErrUnknownAddress(addr.String()).Result()
|
||||
}
|
||||
|
||||
// Check and increment sequence number.
|
||||
|
|
|
@ -93,13 +93,13 @@ func TestAnteHandlerSigErrors(t *testing.T) {
|
|||
// test an unrecognized account
|
||||
privs, seqs = []crypto.PrivKey{priv1, priv2}, []int64{0, 0}
|
||||
tx = newTestTx(ctx, msg, privs, seqs, fee)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, sdk.CodeUnrecognizedAddress)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, sdk.CodeUnknownAddress)
|
||||
|
||||
// save the first account, but second is still unrecognized
|
||||
acc1 := mapper.NewAccountWithAddress(ctx, addr1)
|
||||
acc1.SetCoins(fee.Amount)
|
||||
mapper.SetAccount(ctx, acc1)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, sdk.CodeUnrecognizedAddress)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, sdk.CodeUnknownAddress)
|
||||
}
|
||||
|
||||
// Test logic around sequence checking with one signer and many signers.
|
||||
|
|
|
@ -5,36 +5,19 @@ import (
|
|||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
)
|
||||
|
||||
type CodeType = sdk.CodeType
|
||||
|
||||
// Coin errors reserve 100 ~ 199.
|
||||
const (
|
||||
// Coin errors reserve 100 ~ 199.
|
||||
CodeInvalidInput CodeType = 101
|
||||
CodeInvalidOutput CodeType = 102
|
||||
CodeInvalidAddress CodeType = 103
|
||||
CodeUnknownAddress CodeType = 104
|
||||
CodeInsufficientCoins CodeType = 105
|
||||
CodeInvalidCoins CodeType = 106
|
||||
CodeUnknownRequest CodeType = sdk.CodeUnknownRequest
|
||||
CodeInvalidInput sdk.CodeType = 101
|
||||
CodeInvalidOutput sdk.CodeType = 102
|
||||
)
|
||||
|
||||
// NOTE: Don't stringer this, we'll put better messages in later.
|
||||
func codeToDefaultMsg(code CodeType) string {
|
||||
func codeToDefaultMsg(code sdk.CodeType) string {
|
||||
switch code {
|
||||
case CodeInvalidInput:
|
||||
return "Invalid input coins"
|
||||
case CodeInvalidOutput:
|
||||
return "Invalid output coins"
|
||||
case CodeInvalidAddress:
|
||||
return "Invalid address"
|
||||
case CodeUnknownAddress:
|
||||
return "Unknown address"
|
||||
case CodeInsufficientCoins:
|
||||
return "Insufficient coins"
|
||||
case CodeInvalidCoins:
|
||||
return "Invalid coins"
|
||||
case CodeUnknownRequest:
|
||||
return "Unknown request"
|
||||
default:
|
||||
return sdk.CodeToDefaultMsg(code)
|
||||
}
|
||||
|
@ -59,41 +42,16 @@ func ErrNoOutputs() sdk.Error {
|
|||
return newError(CodeInvalidOutput, "")
|
||||
}
|
||||
|
||||
func ErrInvalidSequence(msg string) sdk.Error {
|
||||
return sdk.ErrInvalidSequence(msg)
|
||||
}
|
||||
|
||||
func ErrInvalidAddress(msg string) sdk.Error {
|
||||
return newError(CodeInvalidAddress, msg)
|
||||
}
|
||||
|
||||
func ErrUnknownAddress(msg string) sdk.Error {
|
||||
return newError(CodeUnknownAddress, msg)
|
||||
}
|
||||
|
||||
func ErrInsufficientCoins(msg string) sdk.Error {
|
||||
return newError(CodeInsufficientCoins, msg)
|
||||
}
|
||||
|
||||
func ErrInvalidCoins(msg string) sdk.Error {
|
||||
return newError(CodeInvalidCoins, msg)
|
||||
}
|
||||
|
||||
func ErrUnknownRequest(msg string) sdk.Error {
|
||||
return newError(CodeUnknownRequest, msg)
|
||||
}
|
||||
|
||||
//----------------------------------------
|
||||
|
||||
func msgOrDefaultMsg(msg string, code CodeType) string {
|
||||
func msgOrDefaultMsg(msg string, code sdk.CodeType) string {
|
||||
if msg != "" {
|
||||
return msg
|
||||
} else {
|
||||
return codeToDefaultMsg(code)
|
||||
}
|
||||
return codeToDefaultMsg(code)
|
||||
}
|
||||
|
||||
func newError(code CodeType, msg string) sdk.Error {
|
||||
func newError(code sdk.CodeType, msg string) sdk.Error {
|
||||
msg = msgOrDefaultMsg(msg, code)
|
||||
return sdk.NewError(code, msg)
|
||||
}
|
||||
|
|
|
@ -20,13 +20,13 @@ func NewCoinKeeper(am sdk.AccountMapper) CoinKeeper {
|
|||
func (ck CoinKeeper) SubtractCoins(ctx sdk.Context, addr sdk.Address, amt sdk.Coins) (sdk.Coins, sdk.Error) {
|
||||
acc := ck.am.GetAccount(ctx, addr)
|
||||
if acc == nil {
|
||||
return amt, sdk.ErrUnrecognizedAddress(addr.String())
|
||||
return amt, sdk.ErrUnknownAddress(addr.String())
|
||||
}
|
||||
|
||||
coins := acc.GetCoins()
|
||||
newCoins := coins.Minus(amt)
|
||||
if !newCoins.IsNotNegative() {
|
||||
return amt, ErrInsufficientCoins(fmt.Sprintf("%s < %s", coins, amt))
|
||||
return amt, sdk.ErrInsufficientCoins(fmt.Sprintf("%s < %s", coins, amt))
|
||||
}
|
||||
|
||||
acc.SetCoins(newCoins)
|
||||
|
|
14
x/bank/tx.go
14
x/bank/tx.go
|
@ -49,7 +49,7 @@ func (msg SendMsg) ValidateBasic() sdk.Error {
|
|||
}
|
||||
// make sure inputs and outputs match
|
||||
if !totalIn.IsEqual(totalOut) {
|
||||
return ErrInvalidCoins(totalIn.String()).Trace("inputs and outputs don't match")
|
||||
return sdk.ErrInvalidCoins(totalIn.String()).Trace("inputs and outputs don't match")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -147,13 +147,13 @@ type Input struct {
|
|||
// ValidateBasic - validate transaction input
|
||||
func (in Input) ValidateBasic() sdk.Error {
|
||||
if len(in.Address) == 0 {
|
||||
return ErrInvalidAddress(in.Address.String())
|
||||
return sdk.ErrInvalidAddress(in.Address.String())
|
||||
}
|
||||
if !in.Coins.IsValid() {
|
||||
return ErrInvalidCoins(in.Coins.String())
|
||||
return sdk.ErrInvalidCoins(in.Coins.String())
|
||||
}
|
||||
if !in.Coins.IsPositive() {
|
||||
return ErrInvalidCoins(in.Coins.String())
|
||||
return sdk.ErrInvalidCoins(in.Coins.String())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -183,13 +183,13 @@ type Output struct {
|
|||
// ValidateBasic - validate transaction output
|
||||
func (out Output) ValidateBasic() sdk.Error {
|
||||
if len(out.Address) == 0 {
|
||||
return ErrInvalidAddress(out.Address.String())
|
||||
return sdk.ErrInvalidAddress(out.Address.String())
|
||||
}
|
||||
if !out.Coins.IsValid() {
|
||||
return ErrInvalidCoins(out.Coins.String())
|
||||
return sdk.ErrInvalidCoins(out.Coins.String())
|
||||
}
|
||||
if !out.Coins.IsPositive() {
|
||||
return ErrInvalidCoins(out.Coins.String())
|
||||
return sdk.ErrInvalidCoins(out.Coins.String())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue