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
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. 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. // 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. res, 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. }