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.

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