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.

157 lines
4.7 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/lite"
  11. certclient "github.com/tendermint/tendermint/lite/client"
  12. nm "github.com/tendermint/tendermint/node"
  13. "github.com/tendermint/tendermint/rpc/client"
  14. rpctest "github.com/tendermint/tendermint/rpc/test"
  15. "github.com/tendermint/tendermint/types"
  16. )
  17. var node *nm.Node
  18. var chainID = "tendermint_test" // TODO use from config.
  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. node.Stop()
  26. node.Wait()
  27. os.Exit(code)
  28. }
  29. func kvstoreTx(k, v []byte) []byte {
  30. return []byte(fmt.Sprintf("%s=%s", k, v))
  31. }
  32. // TODO: enable it after general proof format has been adapted
  33. // in abci/examples/kvstore.go
  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. // TODO: Ensure that *some* keys will be there, ensuring that proof is nil,
  80. // (currently there's a race condition)
  81. // and ensure that proof proves absence of k.
  82. require.Nil(bs)
  83. // but given that block it is good
  84. bs, height, proof, err = GetWithProof(prt, k, brh, cl, cert)
  85. require.NoError(err, "%#v", err)
  86. require.NotNil(proof)
  87. require.Equal(height, brh)
  88. assert.EqualValues(v, bs)
  89. err = prt.VerifyValue(proof, rootHash, string(k), bs) // XXX key encoding
  90. assert.NoError(err, "%#v", err)
  91. // Test non-existing key.
  92. missing := []byte("my-missing-key")
  93. bs, _, proof, err = GetWithProof(prt, missing, 0, cl, cert)
  94. require.NoError(err)
  95. require.Nil(bs)
  96. require.NotNil(proof)
  97. err = prt.VerifyAbsence(proof, rootHash, string(missing)) // XXX VerifyAbsence(), keyencoding
  98. assert.NoError(err, "%#v", err)
  99. err = prt.VerifyAbsence(proof, rootHash, string(k)) // XXX VerifyAbsence(), keyencoding
  100. assert.Error(err, "%#v", err)
  101. }
  102. func TestTxProofs(t *testing.T) {
  103. assert, require := assert.New(t), require.New(t)
  104. cl := client.NewLocal(node)
  105. client.WaitForHeight(cl, 1, nil)
  106. tx := kvstoreTx([]byte("key-a"), []byte("value-a"))
  107. br, err := cl.BroadcastTxCommit(tx)
  108. require.NoError(err, "%#v", err)
  109. require.EqualValues(0, br.CheckTx.Code, "%#v", br.CheckTx)
  110. require.EqualValues(0, br.DeliverTx.Code)
  111. brh := br.Height
  112. source := certclient.NewProvider(chainID, cl)
  113. seed, err := source.LatestFullCommit(chainID, brh-2, brh-2)
  114. require.NoError(err, "%#v", err)
  115. cert := lite.NewBaseVerifier(chainID, seed.Height(), seed.Validators)
  116. // First let's make sure a bogus transaction hash returns a valid non-existence proof.
  117. key := types.Tx([]byte("bogus")).Hash()
  118. res, err := cl.Tx(key, true)
  119. require.NotNil(err)
  120. require.Contains(err.Error(), "not found")
  121. // Now let's check with the real tx hash.
  122. key = types.Tx(tx).Hash()
  123. res, err = cl.Tx(key, true)
  124. require.NoError(err, "%#v", err)
  125. require.NotNil(res)
  126. err = res.Proof.Validate(key)
  127. assert.NoError(err, "%#v", err)
  128. commit, err := GetCertifiedCommit(br.Height, cl, cert)
  129. require.Nil(err, "%#v", err)
  130. require.Equal(res.Proof.RootHash, commit.Header.DataHash)
  131. }