Use protobuf enums for RetCode. s/RetCode/CodeType/g

This commit is contained in:
Jae Kwon 2016-01-31 20:39:43 -08:00
parent 012abc437b
commit 69e587f57f
10 changed files with 110 additions and 90 deletions

View File

@ -219,24 +219,24 @@ func (cli *TMSPClient) FlushSync() error {
return cli.err return cli.err
} }
func (cli *TMSPClient) AppendTxSync(tx []byte) (code types.RetCode, result []byte, log string, err error) { func (cli *TMSPClient) AppendTxSync(tx []byte) (code types.CodeType, result []byte, log string, err error) {
reqres := cli.queueRequest(types.RequestAppendTx(tx)) reqres := cli.queueRequest(types.RequestAppendTx(tx))
cli.FlushSync() cli.FlushSync()
if cli.err != nil { if cli.err != nil {
return types.RetCodeInternalError, nil, "", cli.err return types.CodeType_InternalError, nil, "", cli.err
} }
res := reqres.Response res := reqres.Response
return types.RetCode(res.Code), res.Data, res.Log, nil return res.Code, res.Data, res.Log, nil
} }
func (cli *TMSPClient) CheckTxSync(tx []byte) (code types.RetCode, result []byte, log string, err error) { func (cli *TMSPClient) CheckTxSync(tx []byte) (code types.CodeType, result []byte, log string, err error) {
reqres := cli.queueRequest(types.RequestCheckTx(tx)) reqres := cli.queueRequest(types.RequestCheckTx(tx))
cli.FlushSync() cli.FlushSync()
if cli.err != nil { if cli.err != nil {
return types.RetCodeInternalError, nil, "", cli.err return types.CodeType_InternalError, nil, "", cli.err
} }
res := reqres.Response res := reqres.Response
return types.RetCode(res.Code), res.Data, res.Log, nil return res.Code, res.Data, res.Log, nil
} }
func (cli *TMSPClient) GetHashSync() (hash []byte, log string, err error) { func (cli *TMSPClient) GetHashSync() (hash []byte, log string, err error) {
@ -249,14 +249,14 @@ func (cli *TMSPClient) GetHashSync() (hash []byte, log string, err error) {
return res.Data, res.Log, nil return res.Data, res.Log, nil
} }
func (cli *TMSPClient) QuerySync(query []byte) (code types.RetCode, result []byte, log string, err error) { func (cli *TMSPClient) QuerySync(query []byte) (code types.CodeType, result []byte, log string, err error) {
reqres := cli.queueRequest(types.RequestQuery(query)) reqres := cli.queueRequest(types.RequestQuery(query))
cli.FlushSync() cli.FlushSync()
if cli.err != nil { if cli.err != nil {
return types.RetCodeInternalError, nil, "", cli.err return types.CodeType_InternalError, nil, "", cli.err
} }
res := reqres.Response res := reqres.Response
return types.RetCode(res.Code), res.Data, res.Log, nil return res.Code, res.Data, res.Log, nil
} }
//---------------------------------------- //----------------------------------------

View File

@ -29,29 +29,29 @@ func (app *CounterApplication) SetOption(key string, value string) (log string)
return "" return ""
} }
func (app *CounterApplication) AppendTx(tx []byte) (code types.RetCode, result []byte, log string) { func (app *CounterApplication) AppendTx(tx []byte) (code types.CodeType, result []byte, log string) {
if app.serial { if app.serial {
tx8 := make([]byte, 8) tx8 := make([]byte, 8)
copy(tx8[len(tx8)-len(tx):], tx) copy(tx8[len(tx8)-len(tx):], tx)
txValue := binary.BigEndian.Uint64(tx8) txValue := binary.BigEndian.Uint64(tx8)
if txValue != uint64(app.txCount) { if txValue != uint64(app.txCount) {
return types.RetCodeBadNonce, nil, fmt.Sprintf("Invalid nonce. Expected %v, got %v", app.txCount, txValue) return types.CodeType_BadNonce, nil, fmt.Sprintf("Invalid nonce. Expected %v, got %v", app.txCount, txValue)
} }
} }
app.txCount += 1 app.txCount += 1
return types.RetCodeOK, nil, "" return types.CodeType_OK, nil, ""
} }
func (app *CounterApplication) CheckTx(tx []byte) (code types.RetCode, result []byte, log string) { func (app *CounterApplication) CheckTx(tx []byte) (code types.CodeType, result []byte, log string) {
if app.serial { if app.serial {
tx8 := make([]byte, 8) tx8 := make([]byte, 8)
copy(tx8[len(tx8)-len(tx):], tx) copy(tx8[len(tx8)-len(tx):], tx)
txValue := binary.BigEndian.Uint64(tx8) txValue := binary.BigEndian.Uint64(tx8)
if txValue < uint64(app.txCount) { if txValue < uint64(app.txCount) {
return types.RetCodeBadNonce, nil, fmt.Sprintf("Invalid nonce. Expected >= %v, got %v", app.txCount, txValue) return types.CodeType_BadNonce, nil, fmt.Sprintf("Invalid nonce. Expected >= %v, got %v", app.txCount, txValue)
} }
} }
return types.RetCodeOK, nil, "" return types.CodeType_OK, nil, ""
} }
func (app *CounterApplication) GetHash() (hash []byte, log string) { func (app *CounterApplication) GetHash() (hash []byte, log string) {
@ -66,6 +66,6 @@ func (app *CounterApplication) GetHash() (hash []byte, log string) {
} }
} }
func (app *CounterApplication) Query(query []byte) (code types.RetCode, result []byte, log string) { func (app *CounterApplication) Query(query []byte) (code types.CodeType, result []byte, log string) {
return types.RetCodeOK, nil, fmt.Sprintf("Query is not supported") return types.CodeType_OK, nil, fmt.Sprintf("Query is not supported")
} }

View File

@ -26,13 +26,13 @@ func (app *DummyApplication) SetOption(key string, value string) (log string) {
return "" return ""
} }
func (app *DummyApplication) AppendTx(tx []byte) (code types.RetCode, result []byte, log string) { func (app *DummyApplication) AppendTx(tx []byte) (code types.CodeType, result []byte, log string) {
app.state.Set(tx, tx) app.state.Set(tx, tx)
return types.RetCodeOK, nil, "" return types.CodeType_OK, nil, ""
} }
func (app *DummyApplication) CheckTx(tx []byte) (code types.RetCode, result []byte, log string) { func (app *DummyApplication) CheckTx(tx []byte) (code types.CodeType, result []byte, log string) {
return types.RetCodeOK, nil, "" return types.CodeType_OK, nil, ""
} }
func (app *DummyApplication) GetHash() (hash []byte, log string) { func (app *DummyApplication) GetHash() (hash []byte, log string) {
@ -40,6 +40,6 @@ func (app *DummyApplication) GetHash() (hash []byte, log string) {
return hash, "" return hash, ""
} }
func (app *DummyApplication) Query(query []byte) (code types.RetCode, result []byte, log string) { func (app *DummyApplication) Query(query []byte) (code types.CodeType, result []byte, log string) {
return types.RetCodeOK, nil, "Query not supported" return types.CodeType_OK, nil, "Query not supported"
} }

View File

@ -41,7 +41,7 @@ func TestStream(t *testing.T) {
switch res.Type { switch res.Type {
case types.MessageType_AppendTx: case types.MessageType_AppendTx:
counter += 1 counter += 1
if types.RetCode(res.Code) != types.RetCodeOK { if res.Code != types.CodeType_OK {
t.Error("AppendTx failed with ret_code", res.Code) t.Error("AppendTx failed with ret_code", res.Code)
} }
if counter > numAppendTxs { if counter > numAppendTxs {

View File

@ -9,14 +9,14 @@ type Application interface {
SetOption(key string, value string) (log string) SetOption(key string, value string) (log string)
// Append a tx // Append a tx
AppendTx(tx []byte) (code RetCode, result []byte, log string) AppendTx(tx []byte) (code CodeType, result []byte, log string)
// Validate a tx for the mempool // Validate a tx for the mempool
CheckTx(tx []byte) (code RetCode, result []byte, log string) CheckTx(tx []byte) (code CodeType, result []byte, log string)
// Return the application Merkle root hash // Return the application Merkle root hash
GetHash() (hash []byte, log string) GetHash() (hash []byte, log string)
// Query for state // Query for state
Query(query []byte) (code RetCode, result []byte, log string) Query(query []byte) (code CodeType, result []byte, log string)
} }

View File

@ -97,19 +97,19 @@ func ResponseSetOption(log string) *Response {
} }
} }
func ResponseAppendTx(code RetCode, result []byte, log string) *Response { func ResponseAppendTx(code CodeType, result []byte, log string) *Response {
return &Response{ return &Response{
Type: MessageType_AppendTx, Type: MessageType_AppendTx,
Code: uint32(code), Code: code,
Data: result, Data: result,
Log: log, Log: log,
} }
} }
func ResponseCheckTx(code RetCode, result []byte, log string) *Response { func ResponseCheckTx(code CodeType, result []byte, log string) *Response {
return &Response{ return &Response{
Type: MessageType_CheckTx, Type: MessageType_CheckTx,
Code: uint32(code), Code: code,
Data: result, Data: result,
Log: log, Log: log,
} }
@ -123,10 +123,10 @@ func ResponseGetHash(hash []byte, log string) *Response {
} }
} }
func ResponseQuery(code RetCode, result []byte, log string) *Response { func ResponseQuery(code CodeType, result []byte, log string) *Response {
return &Response{ return &Response{
Type: MessageType_Query, Type: MessageType_Query,
Code: uint32(code), Code: code,
Data: result, Data: result,
Log: log, Log: log,
} }

View File

@ -1,20 +0,0 @@
package types
type RetCode uint
// Reserved return codes
const (
RetCodeOK RetCode = 0
RetCodeInternalError RetCode = 1
RetCodeUnauthorized RetCode = 2
RetCodeInsufficientFees RetCode = 3
RetCodeUnknownRequest RetCode = 4
RetCodeEncodingError RetCode = 5
RetCodeBadNonce RetCode = 6
)
//go:generate stringer -type=RetCode
// NOTE: The previous comment generates r.String().
// To run it, `go get golang.org/x/tools/cmd/stringer`
// and `go generate` in tmsp/types

View File

@ -1,16 +0,0 @@
// generated by stringer -type=RetCode; DO NOT EDIT
package types
import "fmt"
const _RetCode_name = "RetCodeOKRetCodeInternalErrorRetCodeUnauthorizedRetCodeInsufficientFeesRetCodeUnknownRequestRetCodeEncodingErrorRetCodeBadNonce"
var _RetCode_index = [...]uint8{0, 9, 29, 48, 71, 92, 112, 127}
func (i RetCode) String() string {
if i < 0 || i >= RetCode(len(_RetCode_index)-1) {
return fmt.Sprintf("RetCode(%d)", i)
}
return _RetCode_name[_RetCode_index[i]:_RetCode_index[i+1]]
}

View File

@ -68,6 +68,42 @@ func (x MessageType) String() string {
} }
func (MessageType) EnumDescriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } func (MessageType) EnumDescriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
type CodeType int32
const (
CodeType_OK CodeType = 0
CodeType_InternalError CodeType = 1
CodeType_Unauthorized CodeType = 2
CodeType_InsufficientFees CodeType = 3
CodeType_UnknownRequest CodeType = 4
CodeType_EncodingError CodeType = 5
CodeType_BadNonce CodeType = 6
)
var CodeType_name = map[int32]string{
0: "OK",
1: "InternalError",
2: "Unauthorized",
3: "InsufficientFees",
4: "UnknownRequest",
5: "EncodingError",
6: "BadNonce",
}
var CodeType_value = map[string]int32{
"OK": 0,
"InternalError": 1,
"Unauthorized": 2,
"InsufficientFees": 3,
"UnknownRequest": 4,
"EncodingError": 5,
"BadNonce": 6,
}
func (x CodeType) String() string {
return proto.EnumName(CodeType_name, int32(x))
}
func (CodeType) EnumDescriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
type Request struct { type Request struct {
Type MessageType `protobuf:"varint,1,opt,name=type,enum=types.MessageType" json:"type,omitempty"` Type MessageType `protobuf:"varint,1,opt,name=type,enum=types.MessageType" json:"type,omitempty"`
Data []byte `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"` Data []byte `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"`
@ -83,7 +119,7 @@ func (*Request) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0}
type Response struct { type Response struct {
Type MessageType `protobuf:"varint,1,opt,name=type,enum=types.MessageType" json:"type,omitempty"` Type MessageType `protobuf:"varint,1,opt,name=type,enum=types.MessageType" json:"type,omitempty"`
Data []byte `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"` Data []byte `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"`
Code uint32 `protobuf:"varint,3,opt,name=code" json:"code,omitempty"` Code CodeType `protobuf:"varint,3,opt,name=code,enum=types.CodeType" json:"code,omitempty"`
Error string `protobuf:"bytes,4,opt,name=error" json:"error,omitempty"` Error string `protobuf:"bytes,4,opt,name=error" json:"error,omitempty"`
Log string `protobuf:"bytes,5,opt,name=log" json:"log,omitempty"` Log string `protobuf:"bytes,5,opt,name=log" json:"log,omitempty"`
} }
@ -97,26 +133,33 @@ func init() {
proto.RegisterType((*Request)(nil), "types.Request") proto.RegisterType((*Request)(nil), "types.Request")
proto.RegisterType((*Response)(nil), "types.Response") proto.RegisterType((*Response)(nil), "types.Response")
proto.RegisterEnum("types.MessageType", MessageType_name, MessageType_value) proto.RegisterEnum("types.MessageType", MessageType_name, MessageType_value)
proto.RegisterEnum("types.CodeType", CodeType_name, CodeType_value)
} }
var fileDescriptor0 = []byte{ var fileDescriptor0 = []byte{
// 284 bytes of a gzipped FileDescriptorProto // 382 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xa4, 0x91, 0x4d, 0x4e, 0xc3, 0x30, 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xa4, 0x92, 0xdf, 0x0e, 0xd2, 0x30,
0x10, 0x85, 0x49, 0x9b, 0xd0, 0x64, 0xd2, 0x82, 0x3b, 0x74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d4, 0x40, 0xcd, 0x80,
0x6d, 0x85, 0xaa, 0x4f, 0xa7, 0x3c, 0xc9, 0x49, 0xe8, 0x0d, 0x91, 0x32, 0x80, 0x08, 0xe3, 0xa3,
0xb8, 0x08, 0xf9, 0x43, 0xdc, 0x7f, 0xb6, 0xa1, 0x34, 0x71, 0x91, 0x30, 0x63, 0xca, 0xc5, 0xb9,
0x89, 0x73, 0xa4, 0x6f, 0x79, 0xba, 0x93, 0x22, 0x21, 0xd6, 0xf9, 0xd2, 0x71, 0x0f, 0xe5, 0xf5,
0xdf, 0x00, 0x00, 0x00, 0xff, 0xff, 0xc5, 0x93, 0xf0, 0xfc, 0x3d, 0x02, 0x00, 0x00,
} }

View File

@ -20,6 +20,19 @@ enum MessageType {
Query = 0x14; Query = 0x14;
} }
//----------------------------------------
// Code types
enum CodeType {
OK = 0;
InternalError = 1;
Unauthorized = 2;
InsufficientFees = 3;
UnknownRequest = 4;
EncodingError = 5;
BadNonce = 6;
}
//---------------------------------------- //----------------------------------------
// Request types // Request types
@ -36,7 +49,7 @@ message Request {
message Response { message Response {
MessageType type = 1; MessageType type = 1;
bytes data = 2; bytes data = 2;
uint32 code = 3; CodeType code = 3;
string error = 4; string error = 4;
string log = 5; string log = 5;
} }