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.

163 lines
5.0 KiB

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