Add errors to IBC module

This commit is contained in:
Adrian Brink 2018-03-18 14:51:08 +01:00
parent 95a5a7c9a0
commit 6c9b62d461
No known key found for this signature in database
GPG Key ID: F61053D3FBD06353
5 changed files with 86 additions and 46 deletions

View File

@ -271,7 +271,8 @@ func TestQuizMsg(t *testing.T) {
func TestHandler(t *testing.T) {
bapp := newBasecoinApp()
chainid := "ibcchain"
sourceChain := "source-chain"
destChain := "dest-chain"
vals := []abci.Validator{}
baseAcc := auth.BaseAccount{
@ -298,8 +299,8 @@ func TestHandler(t *testing.T) {
SrcAddr: addr1,
DestAddr: addr1,
Coins: coins,
SrcChain: chainid,
DestChain: chainid,
SrcChain: sourceChain,
DestChain: destChain,
}
transferMsg := ibc.IBCTransferMsg{

View File

@ -17,7 +17,7 @@ func (code CodeType) IsOK() bool {
}
// ABCI Response Codes
// Base SDK reserves 0 ~ 99.
// Base SDK reserves 0 - 99.
const (
CodeOK CodeType = 0
CodeInternal CodeType = 1

View File

@ -8,13 +8,13 @@ import (
type CodeType = sdk.CodeType
const (
// Coin errors reserve 100 ~ 199.
CodeInvalidInput CodeType = 101
CodeInvalidOutput CodeType = 102
CodeInvalidAddress CodeType = 103
CodeUnknownAddress CodeType = 104
CodeInsufficientCoins CodeType = 105
CodeInvalidCoins CodeType = 106
// Coin errors reserve 100 - 199.
CodeInvalidInput CodeType = 100
CodeInvalidOutput CodeType = 101
CodeInvalidAddress CodeType = 102
CodeUnknownAddress CodeType = 103
CodeInsufficientCoins CodeType = 104
CodeInvalidCoins CodeType = 105
CodeUnknownRequest CodeType = sdk.CodeUnknownRequest
)

View File

@ -5,9 +5,43 @@ import (
)
const (
CodeInvalidSequence sdk.CodeType = 201
// IBC errors reserve 200 - 299.
CodeInvalidSequence sdk.CodeType = 200
CodeIdenticalChains sdk.CodeType = 201
CodeUnknownRequest sdk.CodeType = sdk.CodeUnknownRequest
)
func ErrInvalidSequence() sdk.Error {
return sdk.NewError(CodeInvalidSequence, "")
func codeToDefaultMsg(code sdk.CodeType) string {
switch code {
case CodeInvalidSequence:
return "Invalid IBC packet sequence"
case CodeIdenticalChains:
return "Source and destination chain cannot be identical"
default:
return sdk.CodeToDefaultMsg(code)
}
}
func ErrInvalidSequence() sdk.Error {
return newError(CodeInvalidSequence, "")
}
func ErrIdenticalChains() sdk.Error {
return newError(CodeIdenticalChains, "")
}
// -------------------------
// Helpers
func newError(code sdk.CodeType, msg string) sdk.Error {
msg = msgOrDefaultMsg(msg, code)
return sdk.NewError(code, msg)
}
func msgOrDefaultMsg(msg string, code sdk.CodeType) string {
if msg != "" {
return msg
} else {
return codeToDefaultMsg(code)
}
}

View File

@ -37,36 +37,11 @@ func NewIBCPacket(srcAddr sdk.Address, destAddr sdk.Address, coins sdk.Coins,
}
}
func makeCodec() *wire.Codec { // basecoin/app.MakeCodec()
const (
msgTypeSend = 0x1
msgTypeIssue = 0x2
msgTypeQuiz = 0x3
msgTypeSetTrend = 0x4
msgTypeIBCTransfer = 0x5
msgTypeIBCReceive = 0x6
)
var _ = oldwire.RegisterInterface(
struct{ sdk.Msg }{},
oldwire.ConcreteType{bank.SendMsg{}, msgTypeSend},
oldwire.ConcreteType{bank.IssueMsg{}, msgTypeIssue},
oldwire.ConcreteType{cool.QuizMsg{}, msgTypeQuiz},
oldwire.ConcreteType{cool.SetTrendMsg{}, msgTypeSetTrend},
oldwire.ConcreteType{IBCTransferMsg{}, msgTypeIBCTransfer},
oldwire.ConcreteType{IBCReceiveMsg{}, msgTypeIBCReceive},
)
const accTypeApp = 0x1
var _ = oldwire.RegisterInterface(
struct{ sdk.Account }{},
oldwire.ConcreteType{&types.AppAccount{}, accTypeApp},
)
cdc := wire.NewCodec()
return cdc
func (ibcp IBCPacket) ValidateBasic() sdk.Error {
if ibcp.SrcChain == ibcp.DestChain {
return ErrIdenticalChains().Trace("")
}
return nil
}
// ----------------------------------
@ -95,7 +70,7 @@ func (msg IBCTransferMsg) GetSignBytes() []byte {
}
func (msg IBCTransferMsg) ValidateBasic() sdk.Error {
return nil
return msg.IBCPacket.ValidateBasic()
}
// x/bank/tx.go SendMsg.GetSigners()
@ -132,7 +107,7 @@ func (msg IBCReceiveMsg) GetSignBytes() []byte {
}
func (msg IBCReceiveMsg) ValidateBasic() sdk.Error {
return nil
return msg.IBCPacket.ValidateBasic()
}
// x/bank/tx.go SendMsg.GetSigners()
@ -146,3 +121,33 @@ func (msg IBCReceiveMsg) GetSigners() []sdk.Address {
func newCodec() *wire.Codec {
return wire.NewCodec()
}
func makeCodec() *wire.Codec { // basecoin/app.MakeCodec()
const (
msgTypeSend = 0x1
msgTypeIssue = 0x2
msgTypeQuiz = 0x3
msgTypeSetTrend = 0x4
msgTypeIBCTransfer = 0x5
msgTypeIBCReceive = 0x6
)
var _ = oldwire.RegisterInterface(
struct{ sdk.Msg }{},
oldwire.ConcreteType{bank.SendMsg{}, msgTypeSend},
oldwire.ConcreteType{bank.IssueMsg{}, msgTypeIssue},
oldwire.ConcreteType{cool.QuizMsg{}, msgTypeQuiz},
oldwire.ConcreteType{cool.SetTrendMsg{}, msgTypeSetTrend},
oldwire.ConcreteType{IBCTransferMsg{}, msgTypeIBCTransfer},
oldwire.ConcreteType{IBCReceiveMsg{}, msgTypeIBCReceive},
)
const accTypeApp = 0x1
var _ = oldwire.RegisterInterface(
struct{ sdk.Account }{},
oldwire.ConcreteType{&types.AppAccount{}, accTypeApp},
)
cdc := wire.NewCodec()
return cdc
}