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.

245 lines
5.0 KiB

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
  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 ToRequestDeliverTx(req RequestDeliverTx) *Request {
  77. return &Request{
  78. Value: &Request_DeliverTx{&req},
  79. }
  80. }
  81. func ToRequestCheckTx(req RequestCheckTx) *Request {
  82. return &Request{
  83. Value: &Request_CheckTx{&req},
  84. }
  85. }
  86. func ToRequestCommit() *Request {
  87. return &Request{
  88. Value: &Request_Commit{&RequestCommit{}},
  89. }
  90. }
  91. func ToRequestQuery(req RequestQuery) *Request {
  92. return &Request{
  93. Value: &Request_Query{&req},
  94. }
  95. }
  96. func ToRequestInitChain(req RequestInitChain) *Request {
  97. return &Request{
  98. Value: &Request_InitChain{&req},
  99. }
  100. }
  101. func ToRequestBeginBlock(req RequestBeginBlock) *Request {
  102. return &Request{
  103. Value: &Request_BeginBlock{&req},
  104. }
  105. }
  106. func ToRequestEndBlock(req RequestEndBlock) *Request {
  107. return &Request{
  108. Value: &Request_EndBlock{&req},
  109. }
  110. }
  111. func ToRequestListSnapshots(req RequestListSnapshots) *Request {
  112. return &Request{
  113. Value: &Request_ListSnapshots{&req},
  114. }
  115. }
  116. func ToRequestOfferSnapshot(req RequestOfferSnapshot) *Request {
  117. return &Request{
  118. Value: &Request_OfferSnapshot{&req},
  119. }
  120. }
  121. func ToRequestLoadSnapshotChunk(req RequestLoadSnapshotChunk) *Request {
  122. return &Request{
  123. Value: &Request_LoadSnapshotChunk{&req},
  124. }
  125. }
  126. func ToRequestApplySnapshotChunk(req RequestApplySnapshotChunk) *Request {
  127. return &Request{
  128. Value: &Request_ApplySnapshotChunk{&req},
  129. }
  130. }
  131. //----------------------------------------
  132. func ToResponseException(errStr string) *Response {
  133. return &Response{
  134. Value: &Response_Exception{&ResponseException{Error: errStr}},
  135. }
  136. }
  137. func ToResponseEcho(message string) *Response {
  138. return &Response{
  139. Value: &Response_Echo{&ResponseEcho{Message: message}},
  140. }
  141. }
  142. func ToResponseFlush() *Response {
  143. return &Response{
  144. Value: &Response_Flush{&ResponseFlush{}},
  145. }
  146. }
  147. func ToResponseInfo(res ResponseInfo) *Response {
  148. return &Response{
  149. Value: &Response_Info{&res},
  150. }
  151. }
  152. func ToResponseDeliverTx(res ResponseDeliverTx) *Response {
  153. return &Response{
  154. Value: &Response_DeliverTx{&res},
  155. }
  156. }
  157. func ToResponseCheckTx(res ResponseCheckTx) *Response {
  158. return &Response{
  159. Value: &Response_CheckTx{&res},
  160. }
  161. }
  162. func ToResponseCommit(res ResponseCommit) *Response {
  163. return &Response{
  164. Value: &Response_Commit{&res},
  165. }
  166. }
  167. func ToResponseQuery(res ResponseQuery) *Response {
  168. return &Response{
  169. Value: &Response_Query{&res},
  170. }
  171. }
  172. func ToResponseInitChain(res ResponseInitChain) *Response {
  173. return &Response{
  174. Value: &Response_InitChain{&res},
  175. }
  176. }
  177. func ToResponseBeginBlock(res ResponseBeginBlock) *Response {
  178. return &Response{
  179. Value: &Response_BeginBlock{&res},
  180. }
  181. }
  182. func ToResponseEndBlock(res ResponseEndBlock) *Response {
  183. return &Response{
  184. Value: &Response_EndBlock{&res},
  185. }
  186. }
  187. func ToResponseListSnapshots(res ResponseListSnapshots) *Response {
  188. return &Response{
  189. Value: &Response_ListSnapshots{&res},
  190. }
  191. }
  192. func ToResponseOfferSnapshot(res ResponseOfferSnapshot) *Response {
  193. return &Response{
  194. Value: &Response_OfferSnapshot{&res},
  195. }
  196. }
  197. func ToResponseLoadSnapshotChunk(res ResponseLoadSnapshotChunk) *Response {
  198. return &Response{
  199. Value: &Response_LoadSnapshotChunk{&res},
  200. }
  201. }
  202. func ToResponseApplySnapshotChunk(res ResponseApplySnapshotChunk) *Response {
  203. return &Response{
  204. Value: &Response_ApplySnapshotChunk{&res},
  205. }
  206. }