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.

202 lines
6.0 KiB

7 years ago
7 years ago
  1. package proxy
  2. import (
  3. cmn "github.com/tendermint/tmlibs/common"
  4. "github.com/tendermint/tendermint/lite"
  5. rpcclient "github.com/tendermint/tendermint/rpc/client"
  6. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  7. )
  8. var _ rpcclient.Client = Wrapper{}
  9. // Wrapper wraps a rpcclient with a Certifier and double-checks any input that is
  10. // provable before passing it along. Allows you to make any rpcclient fully secure.
  11. type Wrapper struct {
  12. rpcclient.Client
  13. cert *lite.InquiringCertifier
  14. }
  15. // SecureClient uses a given certifier to wrap an connection to an untrusted
  16. // host and return a cryptographically secure rpc client.
  17. //
  18. // If it is wrapping an HTTP rpcclient, it will also wrap the websocket interface
  19. func SecureClient(c rpcclient.Client, cert *lite.InquiringCertifier) Wrapper {
  20. wrap := Wrapper{c, cert}
  21. // TODO: no longer possible as no more such interface exposed....
  22. // if we wrap http client, then we can swap out the event switch to filter
  23. // if hc, ok := c.(*rpcclient.HTTP); ok {
  24. // evt := hc.WSEvents.EventSwitch
  25. // hc.WSEvents.EventSwitch = WrappedSwitch{evt, wrap}
  26. // }
  27. return wrap
  28. }
  29. // ABCIQueryWithOptions exposes all options for the ABCI query and verifies the returned proof
  30. func (w Wrapper) ABCIQueryWithOptions(path string, data cmn.HexBytes,
  31. opts rpcclient.ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) {
  32. res, _, err := GetWithProofOptions(path, data, opts, w.Client, w.cert)
  33. return res, err
  34. }
  35. // ABCIQuery uses default options for the ABCI query and verifies the returned proof
  36. func (w Wrapper) ABCIQuery(path string, data cmn.HexBytes) (*ctypes.ResultABCIQuery, error) {
  37. return w.ABCIQueryWithOptions(path, data, rpcclient.DefaultABCIQueryOptions)
  38. }
  39. // Tx queries for a given tx and verifies the proof if it was requested
  40. func (w Wrapper) Tx(hash []byte, prove bool) (*ctypes.ResultTx, error) {
  41. res, err := w.Client.Tx(hash, prove)
  42. if !prove || err != nil {
  43. return res, err
  44. }
  45. h := int64(res.Height)
  46. sh, err := GetCertifiedCommit(h, w.Client, w.cert)
  47. if err != nil {
  48. return res, err
  49. }
  50. err = res.Proof.Validate(sh.DataHash)
  51. return res, err
  52. }
  53. // BlockchainInfo requests a list of headers and verifies them all...
  54. // Rather expensive.
  55. //
  56. // TODO: optimize this if used for anything needing performance
  57. func (w Wrapper) BlockchainInfo(minHeight, maxHeight int64) (*ctypes.ResultBlockchainInfo, error) {
  58. r, err := w.Client.BlockchainInfo(minHeight, maxHeight)
  59. if err != nil {
  60. return nil, err
  61. }
  62. // go and verify every blockmeta in the result....
  63. for _, meta := range r.BlockMetas {
  64. // get a checkpoint to verify from
  65. res, err := w.Commit(&meta.Header.Height)
  66. if err != nil {
  67. return nil, err
  68. }
  69. sh := res.SignedHeader
  70. err = ValidateBlockMeta(meta, sh)
  71. if err != nil {
  72. return nil, err
  73. }
  74. }
  75. return r, nil
  76. }
  77. // Block returns an entire block and verifies all signatures
  78. func (w Wrapper) Block(height *int64) (*ctypes.ResultBlock, error) {
  79. resBlock, err := w.Client.Block(height)
  80. if err != nil {
  81. return nil, err
  82. }
  83. // get a checkpoint to verify from
  84. resCommit, err := w.Commit(height)
  85. if err != nil {
  86. return nil, err
  87. }
  88. sh := resCommit.SignedHeader
  89. // now verify
  90. err = ValidateBlockMeta(resBlock.BlockMeta, sh)
  91. if err != nil {
  92. return nil, err
  93. }
  94. err = ValidateBlock(resBlock.Block, sh)
  95. if err != nil {
  96. return nil, err
  97. }
  98. return resBlock, nil
  99. }
  100. // Commit downloads the Commit and certifies it with the lite.
  101. //
  102. // This is the foundation for all other verification in this module
  103. func (w Wrapper) Commit(height *int64) (*ctypes.ResultCommit, error) {
  104. if height == nil {
  105. resStatus, err := w.Client.Status()
  106. if err != nil {
  107. return nil, err
  108. }
  109. // NOTE: If resStatus.CatchingUp, there is a race
  110. // condition where the validator set for the next height
  111. // isn't available until some time after the blockstore
  112. // has height h on the remote node. This isn't an issue
  113. // once the node has caught up, and a syncing node likely
  114. // won't have this issue esp with the implementation we
  115. // have here, but we may have to address this at some
  116. // point.
  117. height = new(int64)
  118. *height = resStatus.SyncInfo.LatestBlockHeight
  119. }
  120. rpcclient.WaitForHeight(w.Client, *height, nil)
  121. res, err := w.Client.Commit(height)
  122. // if we got it, then certify it
  123. if err == nil {
  124. sh := res.SignedHeader
  125. err = w.cert.Certify(sh)
  126. }
  127. return res, err
  128. }
  129. // // WrappedSwitch creates a websocket connection that auto-verifies any info
  130. // // coming through before passing it along.
  131. // //
  132. // // Since the verification takes 1-2 rpc calls, this is obviously only for
  133. // // relatively low-throughput situations that can tolerate a bit extra latency
  134. // type WrappedSwitch struct {
  135. // types.EventSwitch
  136. // client rpcclient.Client
  137. // }
  138. // // FireEvent verifies any block or header returned from the eventswitch
  139. // func (s WrappedSwitch) FireEvent(event string, data events.EventData) {
  140. // tm, ok := data.(types.TMEventData)
  141. // if !ok {
  142. // fmt.Printf("bad type %#v\n", data)
  143. // return
  144. // }
  145. // // check to validate it if possible, and drop if not valid
  146. // switch t := tm.(type) {
  147. // case types.EventDataNewBlockHeader:
  148. // err := verifyHeader(s.client, t.Header)
  149. // if err != nil {
  150. // fmt.Printf("Invalid header: %#v\n", err)
  151. // return
  152. // }
  153. // case types.EventDataNewBlock:
  154. // err := verifyBlock(s.client, t.Block)
  155. // if err != nil {
  156. // fmt.Printf("Invalid block: %#v\n", err)
  157. // return
  158. // }
  159. // // TODO: can we verify tx as well? anything else
  160. // }
  161. // // looks good, we fire it
  162. // s.EventSwitch.FireEvent(event, data)
  163. // }
  164. // func verifyHeader(c rpcclient.Client, head *types.Header) error {
  165. // // get a checkpoint to verify from
  166. // commit, err := c.Commit(&head.Height)
  167. // if err != nil {
  168. // return err
  169. // }
  170. // check := certclient.CommitFromResult(commit)
  171. // return ValidateHeader(head, check)
  172. // }
  173. //
  174. // func verifyBlock(c rpcclient.Client, block *types.Block) error {
  175. // // get a checkpoint to verify from
  176. // commit, err := c.Commit(&block.Height)
  177. // if err != nil {
  178. // return err
  179. // }
  180. // check := certclient.CommitFromResult(commit)
  181. // return ValidateBlock(block, check)
  182. // }