Convert TMSP to use Protobuf

This commit is contained in:
Jae Kwon
2016-01-30 19:36:33 -08:00
parent 6132ad7d6e
commit 2936c68339
12 changed files with 392 additions and 341 deletions
+146 -121
View File
@@ -1,155 +1,180 @@
package types
import "github.com/tendermint/go-wire"
import (
"io"
"github.com/golang/protobuf/proto"
"github.com/tendermint/go-wire"
)
const (
RequestTypeEcho = byte(0x01)
RequestTypeFlush = byte(0x02)
RequestTypeInfo = byte(0x03)
RequestTypeSetOption = byte(0x04)
// reserved for GetOption = byte(0x05)
RequestTypeEcho = uint32(0x01)
RequestTypeFlush = uint32(0x02)
RequestTypeInfo = uint32(0x03)
RequestTypeSetOption = uint32(0x04)
// reserved for GetOption = uint32(0x05)
ResponseTypeException = byte(0x10)
ResponseTypeEcho = byte(0x11)
ResponseTypeFlush = byte(0x12)
ResponseTypeInfo = byte(0x13)
ResponseTypeSetOption = byte(0x14)
// reserved for GetOption = byte(0x15)
ResponseTypeException = uint32(0x10)
ResponseTypeEcho = uint32(0x11)
ResponseTypeFlush = uint32(0x12)
ResponseTypeInfo = uint32(0x13)
ResponseTypeSetOption = uint32(0x14)
// reserved for GetOption = uint32(0x15)
RequestTypeAppendTx = byte(0x21)
RequestTypeCheckTx = byte(0x22)
RequestTypeGetHash = byte(0x23)
RequestTypeQuery = byte(0x24)
RequestTypeAppendTx = uint32(0x21)
RequestTypeCheckTx = uint32(0x22)
RequestTypeGetHash = uint32(0x23)
RequestTypeQuery = uint32(0x24)
ResponseTypeAppendTx = byte(0x31)
ResponseTypeCheckTx = byte(0x32)
ResponseTypeGetHash = byte(0x33)
ResponseTypeQuery = byte(0x34)
ResponseTypeAppendTx = uint32(0x31)
ResponseTypeCheckTx = uint32(0x32)
ResponseTypeGetHash = uint32(0x33)
ResponseTypeQuery = uint32(0x34)
)
func RequestEcho(message string) *Request {
return &Request{
Type: RequestTypeEcho,
Data: []byte(message),
}
}
func RequestFlush() *Request {
return &Request{
Type: RequestTypeFlush,
}
}
func RequestInfo() *Request {
return &Request{
Type: RequestTypeInfo,
}
}
func RequestSetOption(key string, value string) *Request {
return &Request{
Type: RequestTypeSetOption,
Key: key,
Value: value,
}
}
func RequestAppendTx(txBytes []byte) *Request {
return &Request{
Type: RequestTypeAppendTx,
Data: txBytes,
}
}
func RequestCheckTx(txBytes []byte) *Request {
return &Request{
Type: RequestTypeCheckTx,
Data: txBytes,
}
}
func RequestGetHash() *Request {
return &Request{
Type: RequestTypeGetHash,
}
}
func RequestQuery(queryBytes []byte) *Request {
return &Request{
Type: RequestTypeQuery,
Data: queryBytes,
}
}
//----------------------------------------
type RequestEcho struct {
Message string
func ResponseException(errStr string) *Response {
return &Response{
Type: ResponseTypeException,
Error: errStr,
}
}
type RequestFlush struct {
func ResponseEcho(message string) *Response {
return &Response{
Type: ResponseTypeEcho,
Data: []byte(message),
}
}
type RequestInfo struct {
func ResponseFlush() *Response {
return &Response{
Type: ResponseTypeFlush,
}
}
type RequestSetOption struct {
Key string
Value string
func ResponseInfo(info string) *Response {
return &Response{
Type: ResponseTypeInfo,
Data: []byte(info),
}
}
type RequestAppendTx struct {
TxBytes []byte
func ResponseSetOption(log string) *Response {
return &Response{
Type: ResponseTypeSetOption,
Log: log,
}
}
type RequestCheckTx struct {
TxBytes []byte
func ResponseAppendTx(code RetCode, result []byte, log string) *Response {
return &Response{
Type: ResponseTypeAppendTx,
Data: result,
Log: log,
}
}
type RequestGetHash struct {
func ResponseCheckTx(code RetCode, result []byte, log string) *Response {
return &Response{
Type: ResponseTypeCheckTx,
Data: result,
Log: log,
}
}
type RequestQuery struct {
QueryBytes []byte
func ResponseGetHash(hash []byte, log string) *Response {
return &Response{
Type: ResponseTypeGetHash,
Data: hash,
Log: log,
}
}
type Request interface {
AssertRequestType()
func ResponseQuery(result []byte, log string) *Response {
return &Response{
Type: ResponseTypeQuery,
Data: result,
Log: log,
}
}
func (_ RequestEcho) AssertRequestType() {}
func (_ RequestFlush) AssertRequestType() {}
func (_ RequestInfo) AssertRequestType() {}
func (_ RequestSetOption) AssertRequestType() {}
func (_ RequestAppendTx) AssertRequestType() {}
func (_ RequestCheckTx) AssertRequestType() {}
func (_ RequestGetHash) AssertRequestType() {}
func (_ RequestQuery) AssertRequestType() {}
var _ = wire.RegisterInterface(
struct{ Request }{},
wire.ConcreteType{RequestEcho{}, RequestTypeEcho},
wire.ConcreteType{RequestFlush{}, RequestTypeFlush},
wire.ConcreteType{RequestInfo{}, RequestTypeInfo},
wire.ConcreteType{RequestSetOption{}, RequestTypeSetOption},
wire.ConcreteType{RequestAppendTx{}, RequestTypeAppendTx},
wire.ConcreteType{RequestCheckTx{}, RequestTypeCheckTx},
wire.ConcreteType{RequestGetHash{}, RequestTypeGetHash},
wire.ConcreteType{RequestQuery{}, RequestTypeQuery},
)
//----------------------------------------
type ResponseException struct {
Error string
// Write proto message, length delimited
func WriteMessage(msg proto.Message, w io.Writer) error {
bz, err := proto.Marshal(msg)
if err != nil {
return err
}
var n int
wire.WriteByteSlice(bz, w, &n, &err)
return err
}
type ResponseEcho struct {
Message string
// Read proto message, length delimited
func ReadMessage(r io.Reader, msg proto.Message) error {
var n int
var err error
bz := wire.ReadByteSlice(r, 0, &n, &err)
if err != nil {
return err
}
err = proto.Unmarshal(bz, msg)
return err
}
type ResponseFlush struct {
}
type ResponseInfo struct {
Info string
}
type ResponseSetOption struct {
Log string
}
type ResponseAppendTx struct {
Code RetCode
Result []byte
Log string
}
type ResponseCheckTx struct {
Code RetCode
Result []byte
Log string
}
type ResponseGetHash struct {
Hash []byte
Log string
}
type ResponseQuery struct {
Result []byte
Log string
}
type Response interface {
AssertResponseType()
}
func (_ ResponseEcho) AssertResponseType() {}
func (_ ResponseFlush) AssertResponseType() {}
func (_ ResponseInfo) AssertResponseType() {}
func (_ ResponseSetOption) AssertResponseType() {}
func (_ ResponseAppendTx) AssertResponseType() {}
func (_ ResponseCheckTx) AssertResponseType() {}
func (_ ResponseGetHash) AssertResponseType() {}
func (_ ResponseException) AssertResponseType() {}
func (_ ResponseQuery) AssertResponseType() {}
var _ = wire.RegisterInterface(
struct{ Response }{},
wire.ConcreteType{ResponseEcho{}, ResponseTypeEcho},
wire.ConcreteType{ResponseFlush{}, ResponseTypeFlush},
wire.ConcreteType{ResponseInfo{}, ResponseTypeInfo},
wire.ConcreteType{ResponseSetOption{}, ResponseTypeSetOption},
wire.ConcreteType{ResponseAppendTx{}, ResponseTypeAppendTx},
wire.ConcreteType{ResponseCheckTx{}, ResponseTypeCheckTx},
wire.ConcreteType{ResponseGetHash{}, ResponseTypeGetHash},
wire.ConcreteType{ResponseException{}, ResponseTypeException},
wire.ConcreteType{ResponseQuery{}, ResponseTypeQuery},
)
+1 -1
View File
@@ -1,6 +1,6 @@
package types
type RetCode int
type RetCode uint
// Reserved return codes
const (
+69
View File
@@ -0,0 +1,69 @@
// Code generated by protoc-gen-go.
// source: types/types.proto
// DO NOT EDIT!
/*
Package types is a generated protocol buffer package.
It is generated from these files:
types/types.proto
It has these top-level messages:
Request
Response
*/
package types
import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
type Request struct {
Type uint32 `protobuf:"varint,1,opt,name=type" json:"type,omitempty"`
Data []byte `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"`
Key string `protobuf:"bytes,3,opt,name=key" json:"key,omitempty"`
Value string `protobuf:"bytes,4,opt,name=value" json:"value,omitempty"`
}
func (m *Request) Reset() { *m = Request{} }
func (m *Request) String() string { return proto.CompactTextString(m) }
func (*Request) ProtoMessage() {}
func (*Request) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
type Response struct {
Type uint32 `protobuf:"varint,1,opt,name=type" json:"type,omitempty"`
Data []byte `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"`
Code uint32 `protobuf:"varint,3,opt,name=code" json:"code,omitempty"`
Error string `protobuf:"bytes,4,opt,name=error" json:"error,omitempty"`
Log string `protobuf:"bytes,5,opt,name=log" json:"log,omitempty"`
}
func (m *Response) Reset() { *m = Response{} }
func (m *Response) String() string { return proto.CompactTextString(m) }
func (*Response) ProtoMessage() {}
func (*Response) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
func init() {
proto.RegisterType((*Request)(nil), "types.Request")
proto.RegisterType((*Response)(nil), "types.Response")
}
var fileDescriptor0 = []byte{
// 165 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x8c, 0x8f, 0xb1, 0xae, 0xc2, 0x30,
0x0c, 0x45, 0xd5, 0xd7, 0xe6, 0x01, 0x16, 0x48, 0x10, 0x31, 0x64, 0x44, 0x9d, 0x98, 0x60, 0xe0,
0x4f, 0xb2, 0x31, 0x06, 0x6a, 0x31, 0x50, 0xd5, 0x21, 0x49, 0x91, 0xfa, 0xf7, 0xd8, 0xae, 0xd8,
0x59, 0xa2, 0x73, 0x8f, 0xa2, 0xab, 0x6b, 0xd8, 0x95, 0x29, 0x62, 0x3e, 0xeb, 0x7b, 0x8a, 0x89,
0x0a, 0x59, 0xa3, 0xa1, 0xbd, 0xc2, 0xc2, 0xe3, 0x6b, 0xc4, 0x5c, 0xac, 0x85, 0x46, 0x9c, 0xab,
0x0e, 0xd5, 0x71, 0xe3, 0x95, 0xc5, 0x75, 0xa1, 0x04, 0xf7, 0xc7, 0x6e, 0xed, 0x95, 0xed, 0x16,
0xea, 0x27, 0x4e, 0xae, 0x66, 0xb5, 0xf2, 0x82, 0x76, 0x0f, 0xe6, 0x1d, 0xfa, 0x11, 0x5d, 0xa3,
0x6e, 0x0e, 0xed, 0x00, 0x4b, 0x8f, 0x39, 0xd2, 0x90, 0xf1, 0xe7, 0x6e, 0x76, 0x77, 0xea, 0x50,
0xcb, 0xf9, 0x9f, 0xb0, 0xb4, 0x63, 0x4a, 0x94, 0xbe, 0xed, 0x1a, 0x64, 0x45, 0x4f, 0x0f, 0x67,
0xe6, 0x15, 0x8c, 0xb7, 0x7f, 0x3d, 0xec, 0xf2, 0x09, 0x00, 0x00, 0xff, 0xff, 0xce, 0x9d, 0x3d,
0x4f, 0xed, 0x00, 0x00, 0x00,
}
+47
View File
@@ -0,0 +1,47 @@
syntax = "proto3";
package types;
//----------------------------------------
// Message types
/*
RequestTypeEcho = 0x01;
RequestTypeFlush = 0x02;
RequestTypeInfo = 0x03;
RequestTypeSetOption = 0x04;
RequestTypeAppendTx = 0x21;
RequestTypeCheckTx = 0x22;
RequestTypeGetHash = 0x23;
RequestTypeQuery = 0x24;
ResponseTypeEcho = 0x11;
ResponseTypeFlush = 0x12;
ResponseTypeInfo = 0x13;
ResponseTypeSetOption = 0x14;
ResponseTypeAppendTx = 0x31;
ResponseTypeCheckTx = 0x32;
ResponseTypeGetHash = 0x33;
ResponseTypeQuery = 0x34;
*/
//----------------------------------------
// Request types
message Request {
uint32 type = 1;
bytes data = 2;
string key = 3;
string value = 4;
}
//----------------------------------------
// Response types
message Response {
uint32 type = 1;
bytes data = 2;
uint32 code = 3;
string error = 4;
string log = 5;
}