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