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.

185 lines
3.9 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 ToRequestEcho(message string) *Request {
  8. return &Request{
  9. Value: &Request_Echo{&RequestEcho{message}},
  10. }
  11. }
  12. func ToRequestFlush() *Request {
  13. return &Request{
  14. Value: &Request_Flush{&RequestFlush{}},
  15. }
  16. }
  17. func ToRequestInfo() *Request {
  18. return &Request{
  19. Value: &Request_Info{&RequestInfo{}},
  20. }
  21. }
  22. func ToRequestSetOption(key string, value string) *Request {
  23. return &Request{
  24. Value: &Request_SetOption{&RequestSetOption{key, value}},
  25. }
  26. }
  27. func ToRequestDeliverTx(txBytes []byte) *Request {
  28. return &Request{
  29. Value: &Request_DeliverTx{&RequestDeliverTx{txBytes}},
  30. }
  31. }
  32. func ToRequestCheckTx(txBytes []byte) *Request {
  33. return &Request{
  34. Value: &Request_CheckTx{&RequestCheckTx{txBytes}},
  35. }
  36. }
  37. func ToRequestCommit() *Request {
  38. return &Request{
  39. Value: &Request_Commit{&RequestCommit{}},
  40. }
  41. }
  42. func ToRequestQuery(queryBytes []byte) *Request {
  43. return &Request{
  44. Value: &Request_Query{&RequestQuery{queryBytes}},
  45. }
  46. }
  47. func ToRequestProof(key []byte, blockHeight int64) *Request {
  48. return &Request{
  49. Value: &Request_Proof{&RequestProof{key, blockHeight}},
  50. }
  51. }
  52. func ToRequestInitChain(validators []*Validator) *Request {
  53. return &Request{
  54. Value: &Request_InitChain{&RequestInitChain{validators}},
  55. }
  56. }
  57. func ToRequestBeginBlock(hash []byte, header *Header) *Request {
  58. return &Request{
  59. Value: &Request_BeginBlock{&RequestBeginBlock{hash, header}},
  60. }
  61. }
  62. func ToRequestEndBlock(height uint64) *Request {
  63. return &Request{
  64. Value: &Request_EndBlock{&RequestEndBlock{height}},
  65. }
  66. }
  67. //----------------------------------------
  68. func ToResponseException(errStr string) *Response {
  69. return &Response{
  70. Value: &Response_Exception{&ResponseException{errStr}},
  71. }
  72. }
  73. func ToResponseEcho(message string) *Response {
  74. return &Response{
  75. Value: &Response_Echo{&ResponseEcho{message}},
  76. }
  77. }
  78. func ToResponseFlush() *Response {
  79. return &Response{
  80. Value: &Response_Flush{&ResponseFlush{}},
  81. }
  82. }
  83. func ToResponseInfo(resInfo ResponseInfo) *Response {
  84. return &Response{
  85. Value: &Response_Info{&resInfo},
  86. }
  87. }
  88. func ToResponseSetOption(log string) *Response {
  89. return &Response{
  90. Value: &Response_SetOption{&ResponseSetOption{log}},
  91. }
  92. }
  93. func ToResponseDeliverTx(code CodeType, data []byte, log string) *Response {
  94. return &Response{
  95. Value: &Response_DeliverTx{&ResponseDeliverTx{code, data, log}},
  96. }
  97. }
  98. func ToResponseCheckTx(code CodeType, data []byte, log string) *Response {
  99. return &Response{
  100. Value: &Response_CheckTx{&ResponseCheckTx{code, data, log}},
  101. }
  102. }
  103. func ToResponseCommit(code CodeType, data []byte, log string) *Response {
  104. return &Response{
  105. Value: &Response_Commit{&ResponseCommit{code, data, log}},
  106. }
  107. }
  108. func ToResponseQuery(code CodeType, data []byte, log string) *Response {
  109. return &Response{
  110. Value: &Response_Query{&ResponseQuery{code, data, log}},
  111. }
  112. }
  113. func ToResponseProof(code CodeType, data []byte, log string) *Response {
  114. return &Response{
  115. Value: &Response_Proof{&ResponseProof{code, data, log}},
  116. }
  117. }
  118. func ToResponseInitChain() *Response {
  119. return &Response{
  120. Value: &Response_InitChain{&ResponseInitChain{}},
  121. }
  122. }
  123. func ToResponseBeginBlock() *Response {
  124. return &Response{
  125. Value: &Response_BeginBlock{&ResponseBeginBlock{}},
  126. }
  127. }
  128. func ToResponseEndBlock(resEndBlock ResponseEndBlock) *Response {
  129. return &Response{
  130. Value: &Response_EndBlock{&resEndBlock},
  131. }
  132. }
  133. //----------------------------------------
  134. // Write proto message, length delimited
  135. func WriteMessage(msg proto.Message, w io.Writer) error {
  136. bz, err := proto.Marshal(msg)
  137. if err != nil {
  138. return err
  139. }
  140. var n int
  141. wire.WriteByteSlice(bz, w, &n, &err)
  142. return err
  143. }
  144. // Read proto message, length delimited
  145. func ReadMessage(r io.Reader, msg proto.Message) error {
  146. var n int
  147. var err error
  148. bz := wire.ReadByteSlice(r, 0, &n, &err)
  149. if err != nil {
  150. return err
  151. }
  152. err = proto.Unmarshal(bz, msg)
  153. return err
  154. }