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.

186 lines
3.2 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package types
  2. import (
  3. "io"
  4. "github.com/golang/protobuf/proto"
  5. "github.com/tendermint/go-wire"
  6. )
  7. func RequestEcho(message string) *Request {
  8. return &Request{
  9. Type: MessageType_Echo,
  10. Data: []byte(message),
  11. }
  12. }
  13. func RequestFlush() *Request {
  14. return &Request{
  15. Type: MessageType_Flush,
  16. }
  17. }
  18. func RequestInfo() *Request {
  19. return &Request{
  20. Type: MessageType_Info,
  21. }
  22. }
  23. func RequestSetOption(key string, value string) *Request {
  24. return &Request{
  25. Type: MessageType_SetOption,
  26. Key: key,
  27. Value: value,
  28. }
  29. }
  30. func RequestAppendTx(txBytes []byte) *Request {
  31. return &Request{
  32. Type: MessageType_AppendTx,
  33. Data: txBytes,
  34. }
  35. }
  36. func RequestCheckTx(txBytes []byte) *Request {
  37. return &Request{
  38. Type: MessageType_CheckTx,
  39. Data: txBytes,
  40. }
  41. }
  42. func RequestCommit() *Request {
  43. return &Request{
  44. Type: MessageType_Commit,
  45. }
  46. }
  47. func RequestQuery(queryBytes []byte) *Request {
  48. return &Request{
  49. Type: MessageType_Query,
  50. Data: queryBytes,
  51. }
  52. }
  53. func RequestInitChain(validators []*Validator) *Request {
  54. return &Request{
  55. Type: MessageType_InitChain,
  56. Validators: validators,
  57. }
  58. }
  59. func RequestEndBlock(height uint64) *Request {
  60. return &Request{
  61. Type: MessageType_EndBlock,
  62. Height: height,
  63. }
  64. }
  65. //----------------------------------------
  66. func ResponseException(errStr string) *Response {
  67. return &Response{
  68. Type: MessageType_Exception,
  69. Error: errStr,
  70. }
  71. }
  72. func ResponseEcho(message string) *Response {
  73. return &Response{
  74. Type: MessageType_Echo,
  75. Data: []byte(message),
  76. }
  77. }
  78. func ResponseFlush() *Response {
  79. return &Response{
  80. Type: MessageType_Flush,
  81. }
  82. }
  83. func ResponseInfo(info string) *Response {
  84. return &Response{
  85. Type: MessageType_Info,
  86. Data: []byte(info),
  87. }
  88. }
  89. func ResponseSetOption(log string) *Response {
  90. return &Response{
  91. Type: MessageType_SetOption,
  92. Log: log,
  93. }
  94. }
  95. func ResponseAppendTx(code CodeType, data []byte, log string) *Response {
  96. return &Response{
  97. Type: MessageType_AppendTx,
  98. Code: code,
  99. Data: data,
  100. Log: log,
  101. }
  102. }
  103. func ResponseCheckTx(code CodeType, data []byte, log string) *Response {
  104. return &Response{
  105. Type: MessageType_CheckTx,
  106. Code: code,
  107. Data: data,
  108. Log: log,
  109. }
  110. }
  111. func ResponseCommit(code CodeType, data []byte, log string) *Response {
  112. return &Response{
  113. Type: MessageType_Commit,
  114. Code: code,
  115. Data: data,
  116. Log: log,
  117. }
  118. }
  119. func ResponseQuery(code CodeType, data []byte, log string) *Response {
  120. return &Response{
  121. Type: MessageType_Query,
  122. Code: code,
  123. Data: data,
  124. Log: log,
  125. }
  126. }
  127. func ResponseInitChain() *Response {
  128. return &Response{
  129. Type: MessageType_InitChain,
  130. }
  131. }
  132. func ResponseEndBlock(validators []*Validator) *Response {
  133. return &Response{
  134. Type: MessageType_EndBlock,
  135. Validators: validators,
  136. }
  137. }
  138. //----------------------------------------
  139. // Write proto message, length delimited
  140. func WriteMessage(msg proto.Message, w io.Writer) error {
  141. bz, err := proto.Marshal(msg)
  142. if err != nil {
  143. return err
  144. }
  145. var n int
  146. wire.WriteByteSlice(bz, w, &n, &err)
  147. return err
  148. }
  149. // Read proto message, length delimited
  150. func ReadMessage(r io.Reader, msg proto.Message) error {
  151. var n int
  152. var err error
  153. bz := wire.ReadByteSlice(r, 0, &n, &err)
  154. if err != nil {
  155. return err
  156. }
  157. err = proto.Unmarshal(bz, msg)
  158. return err
  159. }