github.com/DFWallet/tendermint-cosmos@v0.0.2/abci/types/messages.go (about)

     1  package types
     2  
     3  import (
     4  	"bufio"
     5  	"encoding/binary"
     6  	"io"
     7  
     8  	"github.com/gogo/protobuf/proto"
     9  )
    10  
    11  const (
    12  	maxMsgSize = 104857600 // 100MB
    13  )
    14  
    15  // WriteMessage writes a varint length-delimited protobuf message.
    16  func WriteMessage(msg proto.Message, w io.Writer) error {
    17  	bz, err := proto.Marshal(msg)
    18  	if err != nil {
    19  		return err
    20  	}
    21  	return encodeByteSlice(w, bz)
    22  }
    23  
    24  // ReadMessage reads a varint length-delimited protobuf message.
    25  func ReadMessage(r io.Reader, msg proto.Message) error {
    26  	return readProtoMsg(r, msg, maxMsgSize)
    27  }
    28  
    29  func readProtoMsg(r io.Reader, msg proto.Message, maxSize int) error {
    30  	// binary.ReadVarint takes an io.ByteReader, eg. a bufio.Reader
    31  	reader, ok := r.(*bufio.Reader)
    32  	if !ok {
    33  		reader = bufio.NewReader(r)
    34  	}
    35  	length64, err := binary.ReadVarint(reader)
    36  	if err != nil {
    37  		return err
    38  	}
    39  	length := int(length64)
    40  	if length < 0 || length > maxSize {
    41  		return io.ErrShortBuffer
    42  	}
    43  	buf := make([]byte, length)
    44  	if _, err := io.ReadFull(reader, buf); err != nil {
    45  		return err
    46  	}
    47  	return proto.Unmarshal(buf, msg)
    48  }
    49  
    50  //-----------------------------------------------------------------------
    51  // NOTE: we copied wire.EncodeByteSlice from go-wire rather than keep
    52  // go-wire as a dep
    53  
    54  func encodeByteSlice(w io.Writer, bz []byte) (err error) {
    55  	err = encodeVarint(w, int64(len(bz)))
    56  	if err != nil {
    57  		return
    58  	}
    59  	_, err = w.Write(bz)
    60  	return
    61  }
    62  
    63  func encodeVarint(w io.Writer, i int64) (err error) {
    64  	var buf [10]byte
    65  	n := binary.PutVarint(buf[:], i)
    66  	_, err = w.Write(buf[0:n])
    67  	return
    68  }
    69  
    70  //----------------------------------------
    71  
    72  func ToRequestEcho(message string) *Request {
    73  	return &Request{
    74  		Value: &Request_Echo{&RequestEcho{Message: message}},
    75  	}
    76  }
    77  
    78  func ToRequestFlush() *Request {
    79  	return &Request{
    80  		Value: &Request_Flush{&RequestFlush{}},
    81  	}
    82  }
    83  
    84  func ToRequestInfo(req RequestInfo) *Request {
    85  	return &Request{
    86  		Value: &Request_Info{&req},
    87  	}
    88  }
    89  
    90  func ToRequestSetOption(req RequestSetOption) *Request {
    91  	return &Request{
    92  		Value: &Request_SetOption{&req},
    93  	}
    94  }
    95  
    96  func ToRequestDeliverTx(req RequestDeliverTx) *Request {
    97  	return &Request{
    98  		Value: &Request_DeliverTx{&req},
    99  	}
   100  }
   101  
   102  func ToRequestCheckTx(req RequestCheckTx) *Request {
   103  	return &Request{
   104  		Value: &Request_CheckTx{&req},
   105  	}
   106  }
   107  
   108  func ToRequestCommit() *Request {
   109  	return &Request{
   110  		Value: &Request_Commit{&RequestCommit{}},
   111  	}
   112  }
   113  
   114  func ToRequestQuery(req RequestQuery) *Request {
   115  	return &Request{
   116  		Value: &Request_Query{&req},
   117  	}
   118  }
   119  
   120  func ToRequestInitChain(req RequestInitChain) *Request {
   121  	return &Request{
   122  		Value: &Request_InitChain{&req},
   123  	}
   124  }
   125  
   126  func ToRequestBeginBlock(req RequestBeginBlock) *Request {
   127  	return &Request{
   128  		Value: &Request_BeginBlock{&req},
   129  	}
   130  }
   131  
   132  func ToRequestEndBlock(req RequestEndBlock) *Request {
   133  	return &Request{
   134  		Value: &Request_EndBlock{&req},
   135  	}
   136  }
   137  
   138  func ToRequestListSnapshots(req RequestListSnapshots) *Request {
   139  	return &Request{
   140  		Value: &Request_ListSnapshots{&req},
   141  	}
   142  }
   143  
   144  func ToRequestOfferSnapshot(req RequestOfferSnapshot) *Request {
   145  	return &Request{
   146  		Value: &Request_OfferSnapshot{&req},
   147  	}
   148  }
   149  
   150  func ToRequestLoadSnapshotChunk(req RequestLoadSnapshotChunk) *Request {
   151  	return &Request{
   152  		Value: &Request_LoadSnapshotChunk{&req},
   153  	}
   154  }
   155  
   156  func ToRequestApplySnapshotChunk(req RequestApplySnapshotChunk) *Request {
   157  	return &Request{
   158  		Value: &Request_ApplySnapshotChunk{&req},
   159  	}
   160  }
   161  
   162  //----------------------------------------
   163  
   164  func ToResponseException(errStr string) *Response {
   165  	return &Response{
   166  		Value: &Response_Exception{&ResponseException{Error: errStr}},
   167  	}
   168  }
   169  
   170  func ToResponseEcho(message string) *Response {
   171  	return &Response{
   172  		Value: &Response_Echo{&ResponseEcho{Message: message}},
   173  	}
   174  }
   175  
   176  func ToResponseFlush() *Response {
   177  	return &Response{
   178  		Value: &Response_Flush{&ResponseFlush{}},
   179  	}
   180  }
   181  
   182  func ToResponseInfo(res ResponseInfo) *Response {
   183  	return &Response{
   184  		Value: &Response_Info{&res},
   185  	}
   186  }
   187  
   188  func ToResponseSetOption(res ResponseSetOption) *Response {
   189  	return &Response{
   190  		Value: &Response_SetOption{&res},
   191  	}
   192  }
   193  
   194  func ToResponseDeliverTx(res ResponseDeliverTx) *Response {
   195  	return &Response{
   196  		Value: &Response_DeliverTx{&res},
   197  	}
   198  }
   199  
   200  func ToResponseCheckTx(res ResponseCheckTx) *Response {
   201  	return &Response{
   202  		Value: &Response_CheckTx{&res},
   203  	}
   204  }
   205  
   206  func ToResponseCommit(res ResponseCommit) *Response {
   207  	return &Response{
   208  		Value: &Response_Commit{&res},
   209  	}
   210  }
   211  
   212  func ToResponseQuery(res ResponseQuery) *Response {
   213  	return &Response{
   214  		Value: &Response_Query{&res},
   215  	}
   216  }
   217  
   218  func ToResponseInitChain(res ResponseInitChain) *Response {
   219  	return &Response{
   220  		Value: &Response_InitChain{&res},
   221  	}
   222  }
   223  
   224  func ToResponseBeginBlock(res ResponseBeginBlock) *Response {
   225  	return &Response{
   226  		Value: &Response_BeginBlock{&res},
   227  	}
   228  }
   229  
   230  func ToResponseEndBlock(res ResponseEndBlock) *Response {
   231  	return &Response{
   232  		Value: &Response_EndBlock{&res},
   233  	}
   234  }
   235  
   236  func ToResponseListSnapshots(res ResponseListSnapshots) *Response {
   237  	return &Response{
   238  		Value: &Response_ListSnapshots{&res},
   239  	}
   240  }
   241  
   242  func ToResponseOfferSnapshot(res ResponseOfferSnapshot) *Response {
   243  	return &Response{
   244  		Value: &Response_OfferSnapshot{&res},
   245  	}
   246  }
   247  
   248  func ToResponseLoadSnapshotChunk(res ResponseLoadSnapshotChunk) *Response {
   249  	return &Response{
   250  		Value: &Response_LoadSnapshotChunk{&res},
   251  	}
   252  }
   253  
   254  func ToResponseApplySnapshotChunk(res ResponseApplySnapshotChunk) *Response {
   255  	return &Response{
   256  		Value: &Response_ApplySnapshotChunk{&res},
   257  	}
   258  }