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.

229 lines
8.8 KiB

lite2: light client with weak subjectivity (#3989) Refs #1771 ADR: https://github.com/tendermint/tendermint/blob/master/docs/architecture/adr-044-lite-client-with-weak-subjectivity.md ## Commits: * add Verifier and VerifyCommitTrusting * add two more checks make trustLevel an option * float32 for trustLevel * check newHeader time * started writing lite Client * unify Verify methods * ensure h2.Header.bfttime < h1.Header.bfttime + tp * move trust checks into Verify function * add more comments * more docs * started writing tests * unbonding period failures * tests are green * export ErrNewHeaderTooFarIntoFuture * make golangci happy * test for non-adjusted headers * more precision * providers and stores * VerifyHeader and VerifyHeaderAtHeight funcs * fix compile errors * remove lastVerifiedHeight, persist new trusted header * sequential verification * remove TrustedStore option * started writing tests for light client * cover basic cases for linear verification * bisection tests PASS * rename BisectingVerification to SkippingVerification * refactor the code * add TrustedHeader method * consolidate sequential verification tests * consolidate skipping verification tests * rename trustedVals to trustedNextVals * start writing docs * ValidateTrustLevel func and ErrOldHeaderExpired error * AutoClient and example tests * fix errors * update doc * remove ErrNewHeaderTooFarIntoFuture This check is unnecessary given existing a) ErrOldHeaderExpired b) h2.Time > now checks. * return an error if we're at more recent height * add comments * add LastSignedHeaderHeight method to Store I think it's fine if Store tracks last height * copy over proxy from old lite package * make TrustedHeader return latest if height=0 * modify LastSignedHeaderHeight to return an error if no headers exist * copy over proxy impl * refactor proxy and start http lite client * Tx and BlockchainInfo methods * Block method * commit method * code compiles again * lite client compiles * extract updateLiteClientIfNeededTo func * move final parts * add placeholder for tests * force usage of lite http client in proxy * comment out query tests for now * explicitly mention tp: trusting period * verify nextVals in VerifyHeader * refactor bisection * move the NextValidatorsHash check into updateTrustedHeaderAndVals + update the comment * add ConsensusParams method to RPC client * add ConsensusParams to rpc/mock/client * change trustLevel type to a new cmn.Fraction type + update SkippingVerification comment * stress out trustLevel is only used for non-adjusted headers * fixes after Fede's review Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * compare newHeader with a header from an alternative provider * save pivot header Refs https://github.com/tendermint/tendermint/pull/3989#discussion_r349122824 * check header can still be trusted in TrustedHeader Refs https://github.com/tendermint/tendermint/pull/3989#discussion_r349101424 * lite: update Validators and Block endpoints - Block no longer contains BlockMeta - Validators now accept two additional params: page and perPage * make linter happy
5 years ago
lite2: light client with weak subjectivity (#3989) Refs #1771 ADR: https://github.com/tendermint/tendermint/blob/master/docs/architecture/adr-044-lite-client-with-weak-subjectivity.md ## Commits: * add Verifier and VerifyCommitTrusting * add two more checks make trustLevel an option * float32 for trustLevel * check newHeader time * started writing lite Client * unify Verify methods * ensure h2.Header.bfttime < h1.Header.bfttime + tp * move trust checks into Verify function * add more comments * more docs * started writing tests * unbonding period failures * tests are green * export ErrNewHeaderTooFarIntoFuture * make golangci happy * test for non-adjusted headers * more precision * providers and stores * VerifyHeader and VerifyHeaderAtHeight funcs * fix compile errors * remove lastVerifiedHeight, persist new trusted header * sequential verification * remove TrustedStore option * started writing tests for light client * cover basic cases for linear verification * bisection tests PASS * rename BisectingVerification to SkippingVerification * refactor the code * add TrustedHeader method * consolidate sequential verification tests * consolidate skipping verification tests * rename trustedVals to trustedNextVals * start writing docs * ValidateTrustLevel func and ErrOldHeaderExpired error * AutoClient and example tests * fix errors * update doc * remove ErrNewHeaderTooFarIntoFuture This check is unnecessary given existing a) ErrOldHeaderExpired b) h2.Time > now checks. * return an error if we're at more recent height * add comments * add LastSignedHeaderHeight method to Store I think it's fine if Store tracks last height * copy over proxy from old lite package * make TrustedHeader return latest if height=0 * modify LastSignedHeaderHeight to return an error if no headers exist * copy over proxy impl * refactor proxy and start http lite client * Tx and BlockchainInfo methods * Block method * commit method * code compiles again * lite client compiles * extract updateLiteClientIfNeededTo func * move final parts * add placeholder for tests * force usage of lite http client in proxy * comment out query tests for now * explicitly mention tp: trusting period * verify nextVals in VerifyHeader * refactor bisection * move the NextValidatorsHash check into updateTrustedHeaderAndVals + update the comment * add ConsensusParams method to RPC client * add ConsensusParams to rpc/mock/client * change trustLevel type to a new cmn.Fraction type + update SkippingVerification comment * stress out trustLevel is only used for non-adjusted headers * fixes after Fede's review Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * compare newHeader with a header from an alternative provider * save pivot header Refs https://github.com/tendermint/tendermint/pull/3989#discussion_r349122824 * check header can still be trusted in TrustedHeader Refs https://github.com/tendermint/tendermint/pull/3989#discussion_r349101424 * lite: update Validators and Block endpoints - Block no longer contains BlockMeta - Validators now accept two additional params: page and perPage * make linter happy
5 years ago
lite2: light client with weak subjectivity (#3989) Refs #1771 ADR: https://github.com/tendermint/tendermint/blob/master/docs/architecture/adr-044-lite-client-with-weak-subjectivity.md ## Commits: * add Verifier and VerifyCommitTrusting * add two more checks make trustLevel an option * float32 for trustLevel * check newHeader time * started writing lite Client * unify Verify methods * ensure h2.Header.bfttime < h1.Header.bfttime + tp * move trust checks into Verify function * add more comments * more docs * started writing tests * unbonding period failures * tests are green * export ErrNewHeaderTooFarIntoFuture * make golangci happy * test for non-adjusted headers * more precision * providers and stores * VerifyHeader and VerifyHeaderAtHeight funcs * fix compile errors * remove lastVerifiedHeight, persist new trusted header * sequential verification * remove TrustedStore option * started writing tests for light client * cover basic cases for linear verification * bisection tests PASS * rename BisectingVerification to SkippingVerification * refactor the code * add TrustedHeader method * consolidate sequential verification tests * consolidate skipping verification tests * rename trustedVals to trustedNextVals * start writing docs * ValidateTrustLevel func and ErrOldHeaderExpired error * AutoClient and example tests * fix errors * update doc * remove ErrNewHeaderTooFarIntoFuture This check is unnecessary given existing a) ErrOldHeaderExpired b) h2.Time > now checks. * return an error if we're at more recent height * add comments * add LastSignedHeaderHeight method to Store I think it's fine if Store tracks last height * copy over proxy from old lite package * make TrustedHeader return latest if height=0 * modify LastSignedHeaderHeight to return an error if no headers exist * copy over proxy impl * refactor proxy and start http lite client * Tx and BlockchainInfo methods * Block method * commit method * code compiles again * lite client compiles * extract updateLiteClientIfNeededTo func * move final parts * add placeholder for tests * force usage of lite http client in proxy * comment out query tests for now * explicitly mention tp: trusting period * verify nextVals in VerifyHeader * refactor bisection * move the NextValidatorsHash check into updateTrustedHeaderAndVals + update the comment * add ConsensusParams method to RPC client * add ConsensusParams to rpc/mock/client * change trustLevel type to a new cmn.Fraction type + update SkippingVerification comment * stress out trustLevel is only used for non-adjusted headers * fixes after Fede's review Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * compare newHeader with a header from an alternative provider * save pivot header Refs https://github.com/tendermint/tendermint/pull/3989#discussion_r349122824 * check header can still be trusted in TrustedHeader Refs https://github.com/tendermint/tendermint/pull/3989#discussion_r349101424 * lite: update Validators and Block endpoints - Block no longer contains BlockMeta - Validators now accept two additional params: page and perPage * make linter happy
5 years ago
lite2: light client with weak subjectivity (#3989) Refs #1771 ADR: https://github.com/tendermint/tendermint/blob/master/docs/architecture/adr-044-lite-client-with-weak-subjectivity.md ## Commits: * add Verifier and VerifyCommitTrusting * add two more checks make trustLevel an option * float32 for trustLevel * check newHeader time * started writing lite Client * unify Verify methods * ensure h2.Header.bfttime < h1.Header.bfttime + tp * move trust checks into Verify function * add more comments * more docs * started writing tests * unbonding period failures * tests are green * export ErrNewHeaderTooFarIntoFuture * make golangci happy * test for non-adjusted headers * more precision * providers and stores * VerifyHeader and VerifyHeaderAtHeight funcs * fix compile errors * remove lastVerifiedHeight, persist new trusted header * sequential verification * remove TrustedStore option * started writing tests for light client * cover basic cases for linear verification * bisection tests PASS * rename BisectingVerification to SkippingVerification * refactor the code * add TrustedHeader method * consolidate sequential verification tests * consolidate skipping verification tests * rename trustedVals to trustedNextVals * start writing docs * ValidateTrustLevel func and ErrOldHeaderExpired error * AutoClient and example tests * fix errors * update doc * remove ErrNewHeaderTooFarIntoFuture This check is unnecessary given existing a) ErrOldHeaderExpired b) h2.Time > now checks. * return an error if we're at more recent height * add comments * add LastSignedHeaderHeight method to Store I think it's fine if Store tracks last height * copy over proxy from old lite package * make TrustedHeader return latest if height=0 * modify LastSignedHeaderHeight to return an error if no headers exist * copy over proxy impl * refactor proxy and start http lite client * Tx and BlockchainInfo methods * Block method * commit method * code compiles again * lite client compiles * extract updateLiteClientIfNeededTo func * move final parts * add placeholder for tests * force usage of lite http client in proxy * comment out query tests for now * explicitly mention tp: trusting period * verify nextVals in VerifyHeader * refactor bisection * move the NextValidatorsHash check into updateTrustedHeaderAndVals + update the comment * add ConsensusParams method to RPC client * add ConsensusParams to rpc/mock/client * change trustLevel type to a new cmn.Fraction type + update SkippingVerification comment * stress out trustLevel is only used for non-adjusted headers * fixes after Fede's review Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * compare newHeader with a header from an alternative provider * save pivot header Refs https://github.com/tendermint/tendermint/pull/3989#discussion_r349122824 * check header can still be trusted in TrustedHeader Refs https://github.com/tendermint/tendermint/pull/3989#discussion_r349101424 * lite: update Validators and Block endpoints - Block no longer contains BlockMeta - Validators now accept two additional params: page and perPage * make linter happy
5 years ago
  1. package proxy
  2. import (
  3. "github.com/tendermint/tendermint/libs/bytes"
  4. lrpc "github.com/tendermint/tendermint/lite2/rpc"
  5. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  6. rpcserver "github.com/tendermint/tendermint/rpc/lib/server"
  7. rpctypes "github.com/tendermint/tendermint/rpc/lib/types"
  8. "github.com/tendermint/tendermint/types"
  9. )
  10. func RPCRoutes(c *lrpc.Client) map[string]*rpcserver.RPCFunc {
  11. return map[string]*rpcserver.RPCFunc{
  12. // Subscribe/unsubscribe are reserved for websocket events.
  13. "subscribe": rpcserver.NewWSRPCFunc(c.SubscribeWS, "query"),
  14. "unsubscribe": rpcserver.NewWSRPCFunc(c.UnsubscribeWS, "query"),
  15. "unsubscribe_all": rpcserver.NewWSRPCFunc(c.UnsubscribeAllWS, ""),
  16. // info API
  17. "health": rpcserver.NewRPCFunc(makeHealthFunc(c), ""),
  18. "status": rpcserver.NewRPCFunc(makeStatusFunc(c), ""),
  19. "net_info": rpcserver.NewRPCFunc(makeNetInfoFunc(c), ""),
  20. "blockchain": rpcserver.NewRPCFunc(makeBlockchainInfoFunc(c), "minHeight,maxHeight"),
  21. "genesis": rpcserver.NewRPCFunc(makeGenesisFunc(c), ""),
  22. "block": rpcserver.NewRPCFunc(makeBlockFunc(c), "height"),
  23. "block_results": rpcserver.NewRPCFunc(makeBlockResultsFunc(c), "height"),
  24. "commit": rpcserver.NewRPCFunc(makeCommitFunc(c), "height"),
  25. "tx": rpcserver.NewRPCFunc(makeTxFunc(c), "hash,prove"),
  26. "tx_search": rpcserver.NewRPCFunc(makeTxSearchFunc(c), "query,prove,page,per_page"),
  27. "validators": rpcserver.NewRPCFunc(makeValidatorsFunc(c), "height,page,per_page"),
  28. "dump_consensus_state": rpcserver.NewRPCFunc(makeDumpConsensusStateFunc(c), ""),
  29. "consensus_state": rpcserver.NewRPCFunc(makeConsensusStateFunc(c), ""),
  30. "consensus_params": rpcserver.NewRPCFunc(makeConsensusParamsFunc(c), "height"),
  31. "unconfirmed_txs": rpcserver.NewRPCFunc(makeUnconfirmedTxsFunc(c), "limit"),
  32. "num_unconfirmed_txs": rpcserver.NewRPCFunc(makeNumUnconfirmedTxsFunc(c), ""),
  33. // tx broadcast API
  34. "broadcast_tx_commit": rpcserver.NewRPCFunc(makeBroadcastTxCommitFunc(c), "tx"),
  35. "broadcast_tx_sync": rpcserver.NewRPCFunc(makeBroadcastTxSyncFunc(c), "tx"),
  36. "broadcast_tx_async": rpcserver.NewRPCFunc(makeBroadcastTxAsyncFunc(c), "tx"),
  37. // abci API
  38. "abci_query": rpcserver.NewRPCFunc(makeABCIQueryFunc(c), "path,data,height,prove"),
  39. "abci_info": rpcserver.NewRPCFunc(makeABCIInfoFunc(c), ""),
  40. // evidence API
  41. "broadcast_evidence": rpcserver.NewRPCFunc(makeBroadcastEvidenceFunc(c), "evidence"),
  42. }
  43. }
  44. type rpcHealthFunc func(ctx *rpctypes.Context) (*ctypes.ResultHealth, error)
  45. func makeHealthFunc(c *lrpc.Client) rpcHealthFunc {
  46. return func(ctx *rpctypes.Context) (*ctypes.ResultHealth, error) {
  47. return c.Health()
  48. }
  49. }
  50. type rpcStatusFunc func(ctx *rpctypes.Context) (*ctypes.ResultStatus, error)
  51. // nolint: interfacer
  52. func makeStatusFunc(c *lrpc.Client) rpcStatusFunc {
  53. return func(ctx *rpctypes.Context) (*ctypes.ResultStatus, error) {
  54. return c.Status()
  55. }
  56. }
  57. type rpcNetInfoFunc func(ctx *rpctypes.Context, minHeight, maxHeight int64) (*ctypes.ResultNetInfo, error)
  58. func makeNetInfoFunc(c *lrpc.Client) rpcNetInfoFunc {
  59. return func(ctx *rpctypes.Context, minHeight, maxHeight int64) (*ctypes.ResultNetInfo, error) {
  60. return c.NetInfo()
  61. }
  62. }
  63. type rpcBlockchainInfoFunc func(ctx *rpctypes.Context, minHeight, maxHeight int64) (*ctypes.ResultBlockchainInfo, error)
  64. func makeBlockchainInfoFunc(c *lrpc.Client) rpcBlockchainInfoFunc {
  65. return func(ctx *rpctypes.Context, minHeight, maxHeight int64) (*ctypes.ResultBlockchainInfo, error) {
  66. return c.BlockchainInfo(minHeight, maxHeight)
  67. }
  68. }
  69. type rpcGenesisFunc func(ctx *rpctypes.Context) (*ctypes.ResultGenesis, error)
  70. func makeGenesisFunc(c *lrpc.Client) rpcGenesisFunc {
  71. return func(ctx *rpctypes.Context) (*ctypes.ResultGenesis, error) {
  72. return c.Genesis()
  73. }
  74. }
  75. type rpcBlockFunc func(ctx *rpctypes.Context, height *int64) (*ctypes.ResultBlock, error)
  76. func makeBlockFunc(c *lrpc.Client) rpcBlockFunc {
  77. return func(ctx *rpctypes.Context, height *int64) (*ctypes.ResultBlock, error) {
  78. return c.Block(height)
  79. }
  80. }
  81. type rpcBlockResultsFunc func(ctx *rpctypes.Context, height *int64) (*ctypes.ResultBlockResults, error)
  82. func makeBlockResultsFunc(c *lrpc.Client) rpcBlockResultsFunc {
  83. return func(ctx *rpctypes.Context, height *int64) (*ctypes.ResultBlockResults, error) {
  84. return c.BlockResults(height)
  85. }
  86. }
  87. type rpcCommitFunc func(ctx *rpctypes.Context, height *int64) (*ctypes.ResultCommit, error)
  88. func makeCommitFunc(c *lrpc.Client) rpcCommitFunc {
  89. return func(ctx *rpctypes.Context, height *int64) (*ctypes.ResultCommit, error) {
  90. return c.Commit(height)
  91. }
  92. }
  93. type rpcTxFunc func(ctx *rpctypes.Context, hash []byte, prove bool) (*ctypes.ResultTx, error)
  94. func makeTxFunc(c *lrpc.Client) rpcTxFunc {
  95. return func(ctx *rpctypes.Context, hash []byte, prove bool) (*ctypes.ResultTx, error) {
  96. return c.Tx(hash, prove)
  97. }
  98. }
  99. type rpcTxSearchFunc func(ctx *rpctypes.Context, query string, prove bool,
  100. page, perPage int) (*ctypes.ResultTxSearch, error)
  101. func makeTxSearchFunc(c *lrpc.Client) rpcTxSearchFunc {
  102. return func(ctx *rpctypes.Context, query string, prove bool, page, perPage int) (*ctypes.ResultTxSearch, error) {
  103. return c.TxSearch(query, prove, page, perPage)
  104. }
  105. }
  106. type rpcValidatorsFunc func(ctx *rpctypes.Context, height *int64,
  107. page, perPage int) (*ctypes.ResultValidators, error)
  108. func makeValidatorsFunc(c *lrpc.Client) rpcValidatorsFunc {
  109. return func(ctx *rpctypes.Context, height *int64, page, perPage int) (*ctypes.ResultValidators, error) {
  110. return c.Validators(height, page, perPage)
  111. }
  112. }
  113. type rpcDumpConsensusStateFunc func(ctx *rpctypes.Context) (*ctypes.ResultDumpConsensusState, error)
  114. func makeDumpConsensusStateFunc(c *lrpc.Client) rpcDumpConsensusStateFunc {
  115. return func(ctx *rpctypes.Context) (*ctypes.ResultDumpConsensusState, error) {
  116. return c.DumpConsensusState()
  117. }
  118. }
  119. type rpcConsensusStateFunc func(ctx *rpctypes.Context) (*ctypes.ResultConsensusState, error)
  120. func makeConsensusStateFunc(c *lrpc.Client) rpcConsensusStateFunc {
  121. return func(ctx *rpctypes.Context) (*ctypes.ResultConsensusState, error) {
  122. return c.ConsensusState()
  123. }
  124. }
  125. type rpcConsensusParamsFunc func(ctx *rpctypes.Context, height *int64) (*ctypes.ResultConsensusParams, error)
  126. func makeConsensusParamsFunc(c *lrpc.Client) rpcConsensusParamsFunc {
  127. return func(ctx *rpctypes.Context, height *int64) (*ctypes.ResultConsensusParams, error) {
  128. return c.ConsensusParams(height)
  129. }
  130. }
  131. type rpcUnconfirmedTxsFunc func(ctx *rpctypes.Context, limit int) (*ctypes.ResultUnconfirmedTxs, error)
  132. func makeUnconfirmedTxsFunc(c *lrpc.Client) rpcUnconfirmedTxsFunc {
  133. return func(ctx *rpctypes.Context, limit int) (*ctypes.ResultUnconfirmedTxs, error) {
  134. return c.UnconfirmedTxs(limit)
  135. }
  136. }
  137. type rpcNumUnconfirmedTxsFunc func(ctx *rpctypes.Context) (*ctypes.ResultUnconfirmedTxs, error)
  138. func makeNumUnconfirmedTxsFunc(c *lrpc.Client) rpcNumUnconfirmedTxsFunc {
  139. return func(ctx *rpctypes.Context) (*ctypes.ResultUnconfirmedTxs, error) {
  140. return c.NumUnconfirmedTxs()
  141. }
  142. }
  143. type rpcBroadcastTxCommitFunc func(ctx *rpctypes.Context, tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error)
  144. func makeBroadcastTxCommitFunc(c *lrpc.Client) rpcBroadcastTxCommitFunc {
  145. return func(ctx *rpctypes.Context, tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) {
  146. return c.BroadcastTxCommit(tx)
  147. }
  148. }
  149. type rpcBroadcastTxSyncFunc func(ctx *rpctypes.Context, tx types.Tx) (*ctypes.ResultBroadcastTx, error)
  150. func makeBroadcastTxSyncFunc(c *lrpc.Client) rpcBroadcastTxSyncFunc {
  151. return func(ctx *rpctypes.Context, tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
  152. return c.BroadcastTxSync(tx)
  153. }
  154. }
  155. type rpcBroadcastTxAsyncFunc func(ctx *rpctypes.Context, tx types.Tx) (*ctypes.ResultBroadcastTx, error)
  156. func makeBroadcastTxAsyncFunc(c *lrpc.Client) rpcBroadcastTxAsyncFunc {
  157. return func(ctx *rpctypes.Context, tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
  158. return c.BroadcastTxAsync(tx)
  159. }
  160. }
  161. type rpcABCIQueryFunc func(ctx *rpctypes.Context, path string, data bytes.HexBytes) (*ctypes.ResultABCIQuery, error)
  162. func makeABCIQueryFunc(c *lrpc.Client) rpcABCIQueryFunc {
  163. return func(ctx *rpctypes.Context, path string, data bytes.HexBytes) (*ctypes.ResultABCIQuery, error) {
  164. return c.ABCIQuery(path, data)
  165. }
  166. }
  167. type rpcABCIInfoFunc func(ctx *rpctypes.Context) (*ctypes.ResultABCIInfo, error)
  168. func makeABCIInfoFunc(c *lrpc.Client) rpcABCIInfoFunc {
  169. return func(ctx *rpctypes.Context) (*ctypes.ResultABCIInfo, error) {
  170. return c.ABCIInfo()
  171. }
  172. }
  173. type rpcBroadcastEvidenceFunc func(ctx *rpctypes.Context, ev types.Evidence) (*ctypes.ResultBroadcastEvidence, error)
  174. // nolint: interfacer
  175. func makeBroadcastEvidenceFunc(c *lrpc.Client) rpcBroadcastEvidenceFunc {
  176. return func(ctx *rpctypes.Context, ev types.Evidence) (*ctypes.ResultBroadcastEvidence, error) {
  177. return c.BroadcastEvidence(ev)
  178. }
  179. }