tendermint/types/messages.go

184 lines
3.4 KiB
Go
Raw Normal View History

2015-11-02 07:39:53 -08:00
package types
2016-01-30 19:36:33 -08:00
import (
"io"
2015-11-02 07:39:53 -08:00
2016-01-30 19:36:33 -08:00
"github.com/golang/protobuf/proto"
"github.com/tendermint/go-wire"
2015-11-02 07:39:53 -08:00
)
2016-01-30 19:36:33 -08:00
const (
RequestTypeEcho = uint32(0x01)
RequestTypeFlush = uint32(0x02)
RequestTypeInfo = uint32(0x03)
RequestTypeSetOption = uint32(0x04)
// reserved for GetOption = uint32(0x05)
ResponseTypeException = uint32(0x10)
ResponseTypeEcho = uint32(0x11)
ResponseTypeFlush = uint32(0x12)
ResponseTypeInfo = uint32(0x13)
ResponseTypeSetOption = uint32(0x14)
// reserved for GetOption = uint32(0x15)
RequestTypeAppendTx = uint32(0x21)
RequestTypeCheckTx = uint32(0x22)
RequestTypeGetHash = uint32(0x23)
RequestTypeQuery = uint32(0x24)
ResponseTypeAppendTx = uint32(0x31)
ResponseTypeCheckTx = uint32(0x32)
ResponseTypeGetHash = uint32(0x33)
ResponseTypeQuery = uint32(0x34)
)
2015-11-02 07:39:53 -08:00
2016-01-30 19:36:33 -08:00
func RequestEcho(message string) *Request {
return &Request{
Type: RequestTypeEcho,
Data: []byte(message),
}
2015-11-08 15:18:58 -08:00
}
2016-01-30 19:36:33 -08:00
func RequestFlush() *Request {
return &Request{
Type: RequestTypeFlush,
}
}
2016-01-30 19:36:33 -08:00
func RequestInfo() *Request {
return &Request{
Type: RequestTypeInfo,
}
2015-11-27 10:14:46 -08:00
}
2016-01-30 19:36:33 -08:00
func RequestSetOption(key string, value string) *Request {
return &Request{
Type: RequestTypeSetOption,
Key: key,
Value: value,
}
2015-11-02 07:39:53 -08:00
}
2016-01-30 19:36:33 -08:00
func RequestAppendTx(txBytes []byte) *Request {
return &Request{
Type: RequestTypeAppendTx,
Data: txBytes,
}
2015-11-02 07:39:53 -08:00
}
2016-01-30 19:36:33 -08:00
func RequestCheckTx(txBytes []byte) *Request {
return &Request{
Type: RequestTypeCheckTx,
Data: txBytes,
}
2015-11-02 07:39:53 -08:00
}
2016-01-30 19:36:33 -08:00
func RequestGetHash() *Request {
return &Request{
Type: RequestTypeGetHash,
}
2016-01-18 14:37:42 -08:00
}
2016-01-30 19:36:33 -08:00
func RequestQuery(queryBytes []byte) *Request {
return &Request{
Type: RequestTypeQuery,
Data: queryBytes,
}
2015-11-02 07:39:53 -08:00
}
//----------------------------------------
2016-01-30 19:36:33 -08:00
func ResponseException(errStr string) *Response {
return &Response{
Type: ResponseTypeException,
Error: errStr,
}
2015-11-02 07:39:53 -08:00
}
2016-01-30 19:36:33 -08:00
func ResponseEcho(message string) *Response {
return &Response{
Type: ResponseTypeEcho,
Data: []byte(message),
}
2015-11-08 15:18:58 -08:00
}
2016-01-30 19:36:33 -08:00
func ResponseFlush() *Response {
return &Response{
Type: ResponseTypeFlush,
}
}
2016-01-30 19:36:33 -08:00
func ResponseInfo(info string) *Response {
return &Response{
Type: ResponseTypeInfo,
Data: []byte(info),
}
2015-11-27 10:14:46 -08:00
}
2016-01-30 19:36:33 -08:00
func ResponseSetOption(log string) *Response {
return &Response{
Type: ResponseTypeSetOption,
Log: log,
}
2015-11-02 07:39:53 -08:00
}
2016-01-30 19:36:33 -08:00
func ResponseAppendTx(code RetCode, result []byte, log string) *Response {
return &Response{
Type: ResponseTypeAppendTx,
2016-01-31 18:11:24 -08:00
Code: uint32(code),
2016-01-30 19:36:33 -08:00
Data: result,
Log: log,
}
2015-11-02 07:39:53 -08:00
}
2016-01-30 19:36:33 -08:00
func ResponseCheckTx(code RetCode, result []byte, log string) *Response {
return &Response{
Type: ResponseTypeCheckTx,
2016-01-31 18:11:24 -08:00
Code: uint32(code),
2016-01-30 19:36:33 -08:00
Data: result,
Log: log,
}
2015-11-02 07:39:53 -08:00
}
2016-01-30 19:36:33 -08:00
func ResponseGetHash(hash []byte, log string) *Response {
return &Response{
Type: ResponseTypeGetHash,
Data: hash,
Log: log,
}
2016-01-18 14:37:42 -08:00
}
2016-01-31 18:11:24 -08:00
func ResponseQuery(code RetCode, result []byte, log string) *Response {
2016-01-30 19:36:33 -08:00
return &Response{
Type: ResponseTypeQuery,
2016-01-31 18:11:24 -08:00
Code: uint32(code),
2016-01-30 19:36:33 -08:00
Data: result,
Log: log,
}
2015-11-02 07:39:53 -08:00
}
2016-01-30 19:36:33 -08:00
//----------------------------------------
2015-11-02 07:39:53 -08:00
2016-01-30 19:36:33 -08:00
// 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
}
// 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
}