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.

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