You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
4.1 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package types
  2. import (
  3. "bufio"
  4. "encoding/binary"
  5. "io"
  6. "github.com/gogo/protobuf/proto"
  7. )
  8. const (
  9. maxMsgSize = 104857600 // 100MB
  10. )
  11. // WriteMessage writes a varint length-delimited protobuf message.
  12. func WriteMessage(msg proto.Message, w io.Writer) error {
  13. bz, err := proto.Marshal(msg)
  14. if err != nil {
  15. return err
  16. }
  17. return encodeByteSlice(w, bz)
  18. }
  19. // ReadMessage reads a varint length-delimited protobuf message.
  20. func ReadMessage(r io.Reader, msg proto.Message) error {
  21. return readProtoMsg(r, msg, maxMsgSize)
  22. }
  23. func readProtoMsg(r io.Reader, msg proto.Message, maxSize int) error {
  24. // binary.ReadVarint takes an io.ByteReader, eg. a bufio.Reader
  25. reader, ok := r.(*bufio.Reader)
  26. if !ok {
  27. reader = bufio.NewReader(r)
  28. }
  29. length64, err := binary.ReadVarint(reader)
  30. if err != nil {
  31. return err
  32. }
  33. length := int(length64)
  34. if length < 0 || length > maxSize {
  35. return io.ErrShortBuffer
  36. }
  37. buf := make([]byte, length)
  38. if _, err := io.ReadFull(reader, buf); err != nil {
  39. return err
  40. }
  41. return proto.Unmarshal(buf, msg)
  42. }
  43. //-----------------------------------------------------------------------
  44. // NOTE: we copied wire.EncodeByteSlice from go-wire rather than keep
  45. // go-wire as a dep
  46. func encodeByteSlice(w io.Writer, bz []byte) (err error) {
  47. err = encodeVarint(w, int64(len(bz)))
  48. if err != nil {
  49. return
  50. }
  51. _, err = w.Write(bz)
  52. return
  53. }
  54. func encodeVarint(w io.Writer, i int64) (err error) {
  55. var buf [10]byte
  56. n := binary.PutVarint(buf[:], i)
  57. _, err = w.Write(buf[0:n])
  58. return
  59. }
  60. //----------------------------------------
  61. func ToRequestEcho(message string) *Request {
  62. return &Request{
  63. Value: &Request_Echo{&RequestEcho{Message: message}},
  64. }
  65. }
  66. func ToRequestFlush() *Request {
  67. return &Request{
  68. Value: &Request_Flush{&RequestFlush{}},
  69. }
  70. }
  71. func ToRequestInfo(req RequestInfo) *Request {
  72. return &Request{
  73. Value: &Request_Info{&req},
  74. }
  75. }
  76. func ToRequestSetOption(req RequestSetOption) *Request {
  77. return &Request{
  78. Value: &Request_SetOption{&req},
  79. }
  80. }
  81. func ToRequestDeliverTx(req RequestDeliverTx) *Request {
  82. return &Request{
  83. Value: &Request_DeliverTx{&req},
  84. }
  85. }
  86. func ToRequestCheckTx(req RequestCheckTx) *Request {
  87. return &Request{
  88. Value: &Request_CheckTx{&req},
  89. }
  90. }
  91. func ToRequestCommit() *Request {
  92. return &Request{
  93. Value: &Request_Commit{&RequestCommit{}},
  94. }
  95. }
  96. func ToRequestQuery(req RequestQuery) *Request {
  97. return &Request{
  98. Value: &Request_Query{&req},
  99. }
  100. }
  101. func ToRequestInitChain(req RequestInitChain) *Request {
  102. return &Request{
  103. Value: &Request_InitChain{&req},
  104. }
  105. }
  106. func ToRequestBeginBlock(req RequestBeginBlock) *Request {
  107. return &Request{
  108. Value: &Request_BeginBlock{&req},
  109. }
  110. }
  111. func ToRequestEndBlock(req RequestEndBlock) *Request {
  112. return &Request{
  113. Value: &Request_EndBlock{&req},
  114. }
  115. }
  116. //----------------------------------------
  117. func ToResponseException(errStr string) *Response {
  118. return &Response{
  119. Value: &Response_Exception{&ResponseException{Error: errStr}},
  120. }
  121. }
  122. func ToResponseEcho(message string) *Response {
  123. return &Response{
  124. Value: &Response_Echo{&ResponseEcho{Message: message}},
  125. }
  126. }
  127. func ToResponseFlush() *Response {
  128. return &Response{
  129. Value: &Response_Flush{&ResponseFlush{}},
  130. }
  131. }
  132. func ToResponseInfo(res ResponseInfo) *Response {
  133. return &Response{
  134. Value: &Response_Info{&res},
  135. }
  136. }
  137. func ToResponseSetOption(res ResponseSetOption) *Response {
  138. return &Response{
  139. Value: &Response_SetOption{&res},
  140. }
  141. }
  142. func ToResponseDeliverTx(res ResponseDeliverTx) *Response {
  143. return &Response{
  144. Value: &Response_DeliverTx{&res},
  145. }
  146. }
  147. func ToResponseCheckTx(res ResponseCheckTx) *Response {
  148. return &Response{
  149. Value: &Response_CheckTx{&res},
  150. }
  151. }
  152. func ToResponseCommit(res ResponseCommit) *Response {
  153. return &Response{
  154. Value: &Response_Commit{&res},
  155. }
  156. }
  157. func ToResponseQuery(res ResponseQuery) *Response {
  158. return &Response{
  159. Value: &Response_Query{&res},
  160. }
  161. }
  162. func ToResponseInitChain(res ResponseInitChain) *Response {
  163. return &Response{
  164. Value: &Response_InitChain{&res},
  165. }
  166. }
  167. func ToResponseBeginBlock(res ResponseBeginBlock) *Response {
  168. return &Response{
  169. Value: &Response_BeginBlock{&res},
  170. }
  171. }
  172. func ToResponseEndBlock(res ResponseEndBlock) *Response {
  173. return &Response{
  174. Value: &Response_EndBlock{&res},
  175. }
  176. }