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.

161 lines
4.9 KiB

  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. 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. node.Stop()
  28. node.Wait()
  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 := client.NewLocal(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. // TODO: Ensure that *some* keys will be there, ensuring that proof is nil,
  83. // (currently there's a race condition)
  84. // and ensure that proof proves absence of k.
  85. require.Nil(bs)
  86. // but given that block it is good
  87. bs, height, proof, err = GetWithProof(prt, k, brh, cl, cert)
  88. require.NoError(err, "%#v", err)
  89. require.NotNil(proof)
  90. require.Equal(height, brh)
  91. assert.EqualValues(v, bs)
  92. err = prt.VerifyValue(proof, rootHash, string(k), bs) // XXX key encoding
  93. assert.NoError(err, "%#v", err)
  94. // Test non-existing key.
  95. missing := []byte("my-missing-key")
  96. bs, _, proof, err = GetWithProof(prt, missing, 0, cl, cert)
  97. require.NoError(err)
  98. require.Nil(bs)
  99. require.NotNil(proof)
  100. err = prt.VerifyAbsence(proof, rootHash, string(missing)) // XXX VerifyAbsence(), keyencoding
  101. assert.NoError(err, "%#v", err)
  102. err = prt.VerifyAbsence(proof, rootHash, string(k)) // XXX VerifyAbsence(), keyencoding
  103. assert.Error(err, "%#v", err)
  104. }
  105. func TestTxProofs(t *testing.T) {
  106. assert, require := assert.New(t), require.New(t)
  107. cl := client.NewLocal(node)
  108. client.WaitForHeight(cl, 1, nil)
  109. tx := kvstoreTx([]byte("key-a"), []byte("value-a"))
  110. br, err := cl.BroadcastTxCommit(tx)
  111. require.NoError(err, "%#v", err)
  112. require.EqualValues(0, br.CheckTx.Code, "%#v", br.CheckTx)
  113. require.EqualValues(0, br.DeliverTx.Code)
  114. brh := br.Height
  115. source := certclient.NewProvider(chainID, cl)
  116. seed, err := source.LatestFullCommit(chainID, brh-2, brh-2)
  117. require.NoError(err, "%#v", err)
  118. cert := lite.NewBaseVerifier(chainID, seed.Height(), seed.Validators)
  119. // First let's make sure a bogus transaction hash returns a valid non-existence proof.
  120. key := types.Tx([]byte("bogus")).Hash()
  121. res, err := cl.Tx(key, true)
  122. require.NotNil(err)
  123. require.Contains(err.Error(), "not found")
  124. // Now let's check with the real tx root hash.
  125. key = types.Tx(tx).Hash()
  126. res, err = cl.Tx(key, true)
  127. require.NoError(err, "%#v", err)
  128. require.NotNil(res)
  129. keyHash := merkle.SimpleHashFromByteSlices([][]byte{key})
  130. err = res.Proof.Validate(keyHash)
  131. assert.NoError(err, "%#v", err)
  132. commit, err := GetCertifiedCommit(br.Height, cl, cert)
  133. require.Nil(err, "%#v", err)
  134. require.Equal(res.Proof.RootHash, commit.Header.DataHash)
  135. }