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.

173 lines
3.6 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 ToRequestAppendTx(txBytes []byte) *Request {
  28. return &Request{
  29. Value: &Request_AppendTx{&RequestAppendTx{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 ToRequestInitChain(validators []*Validator) *Request {
  48. return &Request{
  49. Value: &Request_InitChain{&RequestInitChain{validators}},
  50. }
  51. }
  52. func ToRequestBeginBlock(height uint64) *Request {
  53. return &Request{
  54. Value: &Request_BeginBlock{&RequestBeginBlock{height}},
  55. }
  56. }
  57. func ToRequestEndBlock(height uint64) *Request {
  58. return &Request{
  59. Value: &Request_EndBlock{&RequestEndBlock{height}},
  60. }
  61. }
  62. //----------------------------------------
  63. func ToResponseException(errStr string) *Response {
  64. return &Response{
  65. Value: &Response_Exception{&ResponseException{errStr}},
  66. }
  67. }
  68. func ToResponseEcho(message string) *Response {
  69. return &Response{
  70. Value: &Response_Echo{&ResponseEcho{message}},
  71. }
  72. }
  73. func ToResponseFlush() *Response {
  74. return &Response{
  75. Value: &Response_Flush{&ResponseFlush{}},
  76. }
  77. }
  78. func ToResponseInfo(info string) *Response {
  79. return &Response{
  80. Value: &Response_Info{&ResponseInfo{info}},
  81. }
  82. }
  83. func ToResponseSetOption(log string) *Response {
  84. return &Response{
  85. Value: &Response_SetOption{&ResponseSetOption{log}},
  86. }
  87. }
  88. func ToResponseAppendTx(code CodeType, data []byte, log string) *Response {
  89. return &Response{
  90. Value: &Response_AppendTx{&ResponseAppendTx{code, data, log}},
  91. }
  92. }
  93. func ToResponseCheckTx(code CodeType, data []byte, log string) *Response {
  94. return &Response{
  95. Value: &Response_CheckTx{&ResponseCheckTx{code, data, log}},
  96. }
  97. }
  98. func ToResponseCommit(code CodeType, data []byte, log string) *Response {
  99. return &Response{
  100. Value: &Response_Commit{&ResponseCommit{code, data, log}},
  101. }
  102. }
  103. func ToResponseQuery(code CodeType, data []byte, log string) *Response {
  104. return &Response{
  105. Value: &Response_Query{&ResponseQuery{code, data, log}},
  106. }
  107. }
  108. func ToResponseInitChain() *Response {
  109. return &Response{
  110. Value: &Response_InitChain{&ResponseInitChain{}},
  111. }
  112. }
  113. func ToResponseBeginBlock() *Response {
  114. return &Response{
  115. Value: &Response_BeginBlock{&ResponseBeginBlock{}},
  116. }
  117. }
  118. func ToResponseEndBlock(validators []*Validator) *Response {
  119. return &Response{
  120. Value: &Response_EndBlock{&ResponseEndBlock{validators}},
  121. }
  122. }
  123. //----------------------------------------
  124. // Write proto message, length delimited
  125. func WriteMessage(msg proto.Message, w io.Writer) error {
  126. bz, err := proto.Marshal(msg)
  127. if err != nil {
  128. return err
  129. }
  130. var n int
  131. wire.WriteByteSlice(bz, w, &n, &err)
  132. return err
  133. }
  134. // Read proto message, length delimited
  135. func ReadMessage(r io.Reader, msg proto.Message) error {
  136. var n int
  137. var err error
  138. bz := wire.ReadByteSlice(r, 0, &n, &err)
  139. if err != nil {
  140. return err
  141. }
  142. err = proto.Unmarshal(bz, msg)
  143. return err
  144. }