tendermint/types/messages.go

185 lines
3.2 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
func RequestEcho(message string) *Request {
return &Request{
2016-01-31 19:56:02 -08:00
Type: MessageType_Echo,
2016-01-30 19:36:33 -08:00
Data: []byte(message),
}
2015-11-08 15:18:58 -08:00
}
2016-01-30 19:36:33 -08:00
func RequestFlush() *Request {
return &Request{
2016-01-31 19:56:02 -08:00
Type: MessageType_Flush,
2016-01-30 19:36:33 -08:00
}
}
2016-01-30 19:36:33 -08:00
func RequestInfo() *Request {
return &Request{
2016-01-31 19:56:02 -08:00
Type: MessageType_Info,
2016-01-30 19:36:33 -08:00
}
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{
2016-01-31 19:56:02 -08:00
Type: MessageType_SetOption,
2016-01-30 19:36:33 -08:00
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{
2016-01-31 19:56:02 -08:00
Type: MessageType_AppendTx,
2016-01-30 19:36:33 -08:00
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{
2016-01-31 19:56:02 -08:00
Type: MessageType_CheckTx,
2016-01-30 19:36:33 -08:00
Data: txBytes,
}
2015-11-02 07:39:53 -08:00
}
2016-02-14 13:11:06 -08:00
func RequestCommit() *Request {
2016-01-30 19:36:33 -08:00
return &Request{
2016-02-14 13:11:06 -08:00
Type: MessageType_Commit,
2016-01-30 19:36:33 -08:00
}
2016-01-18 14:37:42 -08:00
}
2016-01-30 19:36:33 -08:00
func RequestQuery(queryBytes []byte) *Request {
return &Request{
2016-01-31 19:56:02 -08:00
Type: MessageType_Query,
2016-01-30 19:36:33 -08:00
Data: queryBytes,
}
2015-11-02 07:39:53 -08:00
}
2016-02-28 19:19:29 -08:00
func RequestInitValidators(validators []*Validator) *Request {
return &Request{
Type: MessageType_InitValidators,
Validators: validators,
}
}
func RequestSyncValidators() *Request {
return &Request{
Type: MessageType_SyncValidators,
}
}
2015-11-02 07:39:53 -08:00
//----------------------------------------
2016-01-30 19:36:33 -08:00
func ResponseException(errStr string) *Response {
return &Response{
2016-01-31 19:56:02 -08:00
Type: MessageType_Exception,
2016-01-30 19:36:33 -08:00
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{
2016-01-31 19:56:02 -08:00
Type: MessageType_Echo,
2016-01-30 19:36:33 -08:00
Data: []byte(message),
}
2015-11-08 15:18:58 -08:00
}
2016-01-30 19:36:33 -08:00
func ResponseFlush() *Response {
return &Response{
2016-01-31 19:56:02 -08:00
Type: MessageType_Flush,
2016-01-30 19:36:33 -08:00
}
}
2016-01-30 19:36:33 -08:00
func ResponseInfo(info string) *Response {
return &Response{
2016-01-31 19:56:02 -08:00
Type: MessageType_Info,
2016-01-30 19:36:33 -08:00
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{
2016-01-31 19:56:02 -08:00
Type: MessageType_SetOption,
2016-01-30 19:36:33 -08:00
Log: log,
}
2015-11-02 07:39:53 -08:00
}
func ResponseAppendTx(code CodeType, result []byte, log string) *Response {
2016-01-30 19:36:33 -08:00
return &Response{
2016-01-31 19:56:02 -08:00
Type: MessageType_AppendTx,
Code: code,
2016-01-30 19:36:33 -08:00
Data: result,
Log: log,
}
2015-11-02 07:39:53 -08:00
}
func ResponseCheckTx(code CodeType, result []byte, log string) *Response {
2016-01-30 19:36:33 -08:00
return &Response{
2016-01-31 19:56:02 -08:00
Type: MessageType_CheckTx,
Code: code,
2016-01-30 19:36:33 -08:00
Data: result,
Log: log,
}
2015-11-02 07:39:53 -08:00
}
2016-02-14 13:11:06 -08:00
func ResponseCommit(hash []byte, log string) *Response {
2016-01-30 19:36:33 -08:00
return &Response{
2016-02-14 13:11:06 -08:00
Type: MessageType_Commit,
2016-01-30 19:36:33 -08:00
Data: hash,
Log: log,
}
2016-01-18 14:37:42 -08:00
}
func ResponseQuery(code CodeType, result []byte, log string) *Response {
2016-01-30 19:36:33 -08:00
return &Response{
2016-01-31 19:56:02 -08:00
Type: MessageType_Query,
Code: code,
2016-01-30 19:36:33 -08:00
Data: result,
Log: log,
}
2015-11-02 07:39:53 -08:00
}
2016-02-28 19:19:29 -08:00
func ResponseInitValidators() *Response {
return &Response{
Type: MessageType_InitValidators,
}
}
func ResponseSyncValidators(validators []*Validator) *Response {
return &Response{
Type: MessageType_SyncValidators,
Validators: validators,
}
}
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
}