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.

160 lines
5.1 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
  1. package rpc
  2. //import (
  3. // "fmt"
  4. // "os"
  5. // "testing"
  6. // "time"
  7. // "github.com/stretchr/testify/assert"
  8. // "github.com/stretchr/testify/require"
  9. // "github.com/tendermint/tendermint/abci/example/kvstore"
  10. // "github.com/tendermint/tendermint/crypto/merkle"
  11. // nm "github.com/tendermint/tendermint/node"
  12. // "github.com/tendermint/tendermint/rpc/client"
  13. // rpctest "github.com/tendermint/tendermint/rpc/test"
  14. // "github.com/tendermint/tendermint/types"
  15. //)
  16. //var node *nm.Node
  17. //var chainID = "tendermint_test" // TODO use from config.
  18. ////nolint:unused
  19. //var waitForEventTimeout = 5 * time.Second
  20. //// TODO fix tests!!
  21. //func TestMain(m *testing.M) {
  22. // app := kvstore.NewKVStoreApplication()
  23. // node = rpctest.StartTendermint(app)
  24. // code := m.Run()
  25. // rpctest.StopTendermint(node)
  26. // os.Exit(code)
  27. //}
  28. //func kvstoreTx(k, v []byte) []byte {
  29. // return []byte(fmt.Sprintf("%s=%s", k, v))
  30. //}
  31. //// TODO: enable it after general proof format has been adapted
  32. //// in abci/examples/kvstore.go
  33. ////nolint:unused,deadcode
  34. //func _TestAppProofs(t *testing.T) {
  35. // assert, require := assert.New(t), require.New(t)
  36. // prt := defaultProofRuntime()
  37. // cl := client.NewLocal(node)
  38. // client.WaitForHeight(cl, 1, nil)
  39. // // This sets up our trust on the node based on some past point.
  40. // source := certclient.NewProvider(chainID, cl)
  41. // seed, err := source.LatestFullCommit(chainID, 1, 1)
  42. // require.NoError(err, "%#v", err)
  43. // cert := lite.NewBaseVerifier(chainID, seed.Height(), seed.Validators)
  44. // // Wait for tx confirmation.
  45. // done := make(chan int64)
  46. // go func() {
  47. // evtTyp := types.EventTx
  48. // _, err = client.WaitForOneEvent(cl, evtTyp, waitForEventTimeout)
  49. // require.Nil(err, "%#v", err)
  50. // close(done)
  51. // }()
  52. // // Submit a transaction.
  53. // k := []byte("my-key")
  54. // v := []byte("my-value")
  55. // tx := kvstoreTx(k, v)
  56. // br, err := cl.BroadcastTxCommit(tx)
  57. // require.NoError(err, "%#v", err)
  58. // require.EqualValues(0, br.CheckTx.Code, "%#v", br.CheckTx)
  59. // require.EqualValues(0, br.DeliverTx.Code)
  60. // brh := br.Height
  61. // // Fetch latest after tx commit.
  62. // <-done
  63. // latest, err := source.LatestFullCommit(chainID, 1, 1<<63-1)
  64. // require.NoError(err, "%#v", err)
  65. // rootHash := latest.SignedHeader.AppHash
  66. // if rootHash == nil {
  67. // // Fetch one block later, AppHash hasn't been committed yet.
  68. // // TODO find a way to avoid doing this.
  69. // client.WaitForHeight(cl, latest.SignedHeader.Height+1, nil)
  70. // latest, err = source.LatestFullCommit(chainID, latest.SignedHeader.Height+1, 1<<63-1)
  71. // require.NoError(err, "%#v", err)
  72. // rootHash = latest.SignedHeader.AppHash
  73. // }
  74. // require.NotNil(rootHash)
  75. // // verify a query before the tx block has no data (and valid non-exist proof)
  76. // bs, height, proof, err := GetWithProof(prt, k, brh-1, cl, cert)
  77. // require.NoError(err, "%#v", err)
  78. // require.NotNil(proof)
  79. // require.Equal(height, brh-1)
  80. // // require.NotNil(proof)
  81. // // TODO: Ensure that *some* keys will be there, ensuring that proof is nil,
  82. // // (currently there's a race condition)
  83. // // and ensure that proof proves absence of k.
  84. // require.Nil(bs)
  85. // // but given that block it is good
  86. // bs, height, proof, err = GetWithProof(prt, k, brh, cl, cert)
  87. // require.NoError(err, "%#v", err)
  88. // require.NotNil(proof)
  89. // require.Equal(height, brh)
  90. // assert.EqualValues(v, bs)
  91. // err = prt.VerifyValue(proof, rootHash, string(k), bs) // XXX key encoding
  92. // assert.NoError(err, "%#v", err)
  93. // // Test non-existing key.
  94. // missing := []byte("my-missing-key")
  95. // bs, _, proof, err = GetWithProof(prt, missing, 0, cl, cert)
  96. // require.NoError(err)
  97. // require.Nil(bs)
  98. // require.NotNil(proof)
  99. // err = prt.VerifyAbsence(proof, rootHash, string(missing)) // XXX VerifyAbsence(), keyencoding
  100. // assert.NoError(err, "%#v", err)
  101. // err = prt.VerifyAbsence(proof, rootHash, string(k)) // XXX VerifyAbsence(), keyencoding
  102. // assert.Error(err, "%#v", err)
  103. //}
  104. //func TestTxProofs(t *testing.T) {
  105. // assert, require := assert.New(t), require.New(t)
  106. // cl := client.NewLocal(node)
  107. // client.WaitForHeight(cl, 1, nil)
  108. // tx := kvstoreTx([]byte("key-a"), []byte("value-a"))
  109. // br, err := cl.BroadcastTxCommit(tx)
  110. // require.NoError(err, "%#v", err)
  111. // require.EqualValues(0, br.CheckTx.Code, "%#v", br.CheckTx)
  112. // require.EqualValues(0, br.DeliverTx.Code)
  113. // brh := br.Height
  114. // source := certclient.NewProvider(chainID, cl)
  115. // seed, err := source.LatestFullCommit(chainID, brh-2, brh-2)
  116. // require.NoError(err, "%#v", err)
  117. // cert := lite.NewBaseVerifier(chainID, seed.Height(), seed.Validators)
  118. // // First let's make sure a bogus transaction hash returns a valid non-existence proof.
  119. // key := types.Tx([]byte("bogus")).Hash()
  120. // _, err = cl.Tx(key, true)
  121. // require.NotNil(err)
  122. // require.Contains(err.Error(), "not found")
  123. // // Now let's check with the real tx root hash.
  124. // key = types.Tx(tx).Hash()
  125. // res, err := cl.Tx(key, true)
  126. // require.NoError(err, "%#v", err)
  127. // require.NotNil(res)
  128. // keyHash := merkle.SimpleHashFromByteSlices([][]byte{key})
  129. // err = res.Proof.Validate(keyHash)
  130. // assert.NoError(err, "%#v", err)
  131. // commit, err := GetCertifiedCommit(br.Height, cl, cert)
  132. // require.Nil(err, "%#v", err)
  133. // require.Equal(res.Proof.RootHash, commit.Header.DataHash)
  134. //}