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.

312 lines
8.1 KiB

7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
  1. package kvstore
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "sort"
  7. "testing"
  8. "github.com/stretchr/testify/require"
  9. cmn "github.com/tendermint/tendermint/libs/common"
  10. "github.com/tendermint/tendermint/libs/log"
  11. abcicli "github.com/tendermint/tendermint/abci/client"
  12. "github.com/tendermint/tendermint/abci/example/code"
  13. abciserver "github.com/tendermint/tendermint/abci/server"
  14. "github.com/tendermint/tendermint/abci/types"
  15. )
  16. func testKVStore(t *testing.T, app types.Application, tx []byte, key, value string) {
  17. req := types.RequestDeliverTx{Tx: tx}
  18. ar := app.DeliverTx(req)
  19. require.False(t, ar.IsErr(), ar)
  20. // repeating tx doesn't raise error
  21. ar = app.DeliverTx(req)
  22. require.False(t, ar.IsErr(), ar)
  23. // make sure query is fine
  24. resQuery := app.Query(types.RequestQuery{
  25. Path: "/store",
  26. Data: []byte(key),
  27. })
  28. require.Equal(t, code.CodeTypeOK, resQuery.Code)
  29. require.Equal(t, value, string(resQuery.Value))
  30. // make sure proof is fine
  31. resQuery = app.Query(types.RequestQuery{
  32. Path: "/store",
  33. Data: []byte(key),
  34. Prove: true,
  35. })
  36. require.EqualValues(t, code.CodeTypeOK, resQuery.Code)
  37. require.Equal(t, value, string(resQuery.Value))
  38. }
  39. func TestKVStoreKV(t *testing.T) {
  40. kvstore := NewKVStoreApplication()
  41. key := "abc"
  42. value := key
  43. tx := []byte(key)
  44. testKVStore(t, kvstore, tx, key, value)
  45. value = "def"
  46. tx = []byte(key + "=" + value)
  47. testKVStore(t, kvstore, tx, key, value)
  48. }
  49. func TestPersistentKVStoreKV(t *testing.T) {
  50. dir, err := ioutil.TempDir("/tmp", "abci-kvstore-test") // TODO
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. kvstore := NewPersistentKVStoreApplication(dir)
  55. key := "abc"
  56. value := key
  57. tx := []byte(key)
  58. testKVStore(t, kvstore, tx, key, value)
  59. value = "def"
  60. tx = []byte(key + "=" + value)
  61. testKVStore(t, kvstore, tx, key, value)
  62. }
  63. func TestPersistentKVStoreInfo(t *testing.T) {
  64. dir, err := ioutil.TempDir("/tmp", "abci-kvstore-test") // TODO
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. kvstore := NewPersistentKVStoreApplication(dir)
  69. InitKVStore(kvstore)
  70. height := int64(0)
  71. resInfo := kvstore.Info(types.RequestInfo{})
  72. if resInfo.LastBlockHeight != height {
  73. t.Fatalf("expected height of %d, got %d", height, resInfo.LastBlockHeight)
  74. }
  75. // make and apply block
  76. height = int64(1)
  77. hash := []byte("foo")
  78. header := types.Header{
  79. Height: int64(height),
  80. }
  81. kvstore.BeginBlock(types.RequestBeginBlock{Hash: hash, Header: header})
  82. kvstore.EndBlock(types.RequestEndBlock{Height: header.Height})
  83. kvstore.Commit()
  84. resInfo = kvstore.Info(types.RequestInfo{})
  85. if resInfo.LastBlockHeight != height {
  86. t.Fatalf("expected height of %d, got %d", height, resInfo.LastBlockHeight)
  87. }
  88. }
  89. // add a validator, remove a validator, update a validator
  90. func TestValUpdates(t *testing.T) {
  91. dir, err := ioutil.TempDir("/tmp", "abci-kvstore-test") // TODO
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. kvstore := NewPersistentKVStoreApplication(dir)
  96. // init with some validators
  97. total := 10
  98. nInit := 5
  99. vals := RandVals(total)
  100. // iniitalize with the first nInit
  101. kvstore.InitChain(types.RequestInitChain{
  102. Validators: vals[:nInit],
  103. })
  104. vals1, vals2 := vals[:nInit], kvstore.Validators()
  105. valsEqual(t, vals1, vals2)
  106. var v1, v2, v3 types.ValidatorUpdate
  107. // add some validators
  108. v1, v2 = vals[nInit], vals[nInit+1]
  109. diff := []types.ValidatorUpdate{v1, v2}
  110. tx1 := MakeValSetChangeTx(v1.PubKey, v1.Power)
  111. tx2 := MakeValSetChangeTx(v2.PubKey, v2.Power)
  112. makeApplyBlock(t, kvstore, 1, diff, tx1, tx2)
  113. vals1, vals2 = vals[:nInit+2], kvstore.Validators()
  114. valsEqual(t, vals1, vals2)
  115. // remove some validators
  116. v1, v2, v3 = vals[nInit-2], vals[nInit-1], vals[nInit]
  117. v1.Power = 0
  118. v2.Power = 0
  119. v3.Power = 0
  120. diff = []types.ValidatorUpdate{v1, v2, v3}
  121. tx1 = MakeValSetChangeTx(v1.PubKey, v1.Power)
  122. tx2 = MakeValSetChangeTx(v2.PubKey, v2.Power)
  123. tx3 := MakeValSetChangeTx(v3.PubKey, v3.Power)
  124. makeApplyBlock(t, kvstore, 2, diff, tx1, tx2, tx3)
  125. vals1 = append(vals[:nInit-2], vals[nInit+1])
  126. vals2 = kvstore.Validators()
  127. valsEqual(t, vals1, vals2)
  128. // update some validators
  129. v1 = vals[0]
  130. if v1.Power == 5 {
  131. v1.Power = 6
  132. } else {
  133. v1.Power = 5
  134. }
  135. diff = []types.ValidatorUpdate{v1}
  136. tx1 = MakeValSetChangeTx(v1.PubKey, v1.Power)
  137. makeApplyBlock(t, kvstore, 3, diff, tx1)
  138. vals1 = append([]types.ValidatorUpdate{v1}, vals1[1:]...)
  139. vals2 = kvstore.Validators()
  140. valsEqual(t, vals1, vals2)
  141. }
  142. func makeApplyBlock(t *testing.T, kvstore types.Application, heightInt int, diff []types.ValidatorUpdate, txs ...[]byte) {
  143. // make and apply block
  144. height := int64(heightInt)
  145. hash := []byte("foo")
  146. header := types.Header{
  147. Height: height,
  148. }
  149. kvstore.BeginBlock(types.RequestBeginBlock{Hash: hash, Header: header})
  150. for _, tx := range txs {
  151. if r := kvstore.DeliverTx(types.RequestDeliverTx{Tx: tx}); r.IsErr() {
  152. t.Fatal(r)
  153. }
  154. }
  155. resEndBlock := kvstore.EndBlock(types.RequestEndBlock{Height: header.Height})
  156. kvstore.Commit()
  157. valsEqual(t, diff, resEndBlock.ValidatorUpdates)
  158. }
  159. // order doesn't matter
  160. func valsEqual(t *testing.T, vals1, vals2 []types.ValidatorUpdate) {
  161. if len(vals1) != len(vals2) {
  162. t.Fatalf("vals dont match in len. got %d, expected %d", len(vals2), len(vals1))
  163. }
  164. sort.Sort(types.ValidatorUpdates(vals1))
  165. sort.Sort(types.ValidatorUpdates(vals2))
  166. for i, v1 := range vals1 {
  167. v2 := vals2[i]
  168. if !bytes.Equal(v1.PubKey.Data, v2.PubKey.Data) ||
  169. v1.Power != v2.Power {
  170. t.Fatalf("vals dont match at index %d. got %X/%d , expected %X/%d", i, v2.PubKey, v2.Power, v1.PubKey, v1.Power)
  171. }
  172. }
  173. }
  174. func makeSocketClientServer(app types.Application, name string) (abcicli.Client, cmn.Service, error) {
  175. // Start the listener
  176. socket := fmt.Sprintf("unix://%s.sock", name)
  177. logger := log.TestingLogger()
  178. server := abciserver.NewSocketServer(socket, app)
  179. server.SetLogger(logger.With("module", "abci-server"))
  180. if err := server.Start(); err != nil {
  181. return nil, nil, err
  182. }
  183. // Connect to the socket
  184. client := abcicli.NewSocketClient(socket, false)
  185. client.SetLogger(logger.With("module", "abci-client"))
  186. if err := client.Start(); err != nil {
  187. server.Stop()
  188. return nil, nil, err
  189. }
  190. return client, server, nil
  191. }
  192. func makeGRPCClientServer(app types.Application, name string) (abcicli.Client, cmn.Service, error) {
  193. // Start the listener
  194. socket := fmt.Sprintf("unix://%s.sock", name)
  195. logger := log.TestingLogger()
  196. gapp := types.NewGRPCApplication(app)
  197. server := abciserver.NewGRPCServer(socket, gapp)
  198. server.SetLogger(logger.With("module", "abci-server"))
  199. if err := server.Start(); err != nil {
  200. return nil, nil, err
  201. }
  202. client := abcicli.NewGRPCClient(socket, true)
  203. client.SetLogger(logger.With("module", "abci-client"))
  204. if err := client.Start(); err != nil {
  205. server.Stop()
  206. return nil, nil, err
  207. }
  208. return client, server, nil
  209. }
  210. func TestClientServer(t *testing.T) {
  211. // set up socket app
  212. kvstore := NewKVStoreApplication()
  213. client, server, err := makeSocketClientServer(kvstore, "kvstore-socket")
  214. require.Nil(t, err)
  215. defer server.Stop()
  216. defer client.Stop()
  217. runClientTests(t, client)
  218. // set up grpc app
  219. kvstore = NewKVStoreApplication()
  220. gclient, gserver, err := makeGRPCClientServer(kvstore, "kvstore-grpc")
  221. require.Nil(t, err)
  222. defer gserver.Stop()
  223. defer gclient.Stop()
  224. runClientTests(t, gclient)
  225. }
  226. func runClientTests(t *testing.T, client abcicli.Client) {
  227. // run some tests....
  228. key := "abc"
  229. value := key
  230. tx := []byte(key)
  231. testClient(t, client, tx, key, value)
  232. value = "def"
  233. tx = []byte(key + "=" + value)
  234. testClient(t, client, tx, key, value)
  235. }
  236. func testClient(t *testing.T, app abcicli.Client, tx []byte, key, value string) {
  237. ar, err := app.DeliverTxSync(tx)
  238. require.NoError(t, err)
  239. require.False(t, ar.IsErr(), ar)
  240. // repeating tx doesn't raise error
  241. ar, err = app.DeliverTxSync(tx)
  242. require.NoError(t, err)
  243. require.False(t, ar.IsErr(), ar)
  244. // make sure query is fine
  245. resQuery, err := app.QuerySync(types.RequestQuery{
  246. Path: "/store",
  247. Data: []byte(key),
  248. })
  249. require.Nil(t, err)
  250. require.Equal(t, code.CodeTypeOK, resQuery.Code)
  251. require.Equal(t, value, string(resQuery.Value))
  252. // make sure proof is fine
  253. resQuery, err = app.QuerySync(types.RequestQuery{
  254. Path: "/store",
  255. Data: []byte(key),
  256. Prove: true,
  257. })
  258. require.Nil(t, err)
  259. require.Equal(t, code.CodeTypeOK, resQuery.Code)
  260. require.Equal(t, value, string(resQuery.Value))
  261. }