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.

366 lines
9.4 KiB

lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
7 years ago
7 years ago
7 years ago
8 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
  1. package kvstore
  2. import (
  3. "context"
  4. "fmt"
  5. "io/ioutil"
  6. "sort"
  7. "testing"
  8. "github.com/stretchr/testify/require"
  9. "github.com/tendermint/tendermint/libs/log"
  10. "github.com/tendermint/tendermint/libs/service"
  11. abciclient "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. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  16. )
  17. const (
  18. testKey = "abc"
  19. testValue = "def"
  20. )
  21. var ctx = context.Background()
  22. func testKVStore(t *testing.T, app types.Application, tx []byte, key, value string) {
  23. req := types.RequestDeliverTx{Tx: tx}
  24. ar := app.DeliverTx(req)
  25. require.False(t, ar.IsErr(), ar)
  26. // repeating tx doesn't raise error
  27. ar = app.DeliverTx(req)
  28. require.False(t, ar.IsErr(), ar)
  29. // commit
  30. app.Commit()
  31. info := app.Info(types.RequestInfo{})
  32. require.NotZero(t, info.LastBlockHeight)
  33. // make sure query is fine
  34. resQuery := app.Query(types.RequestQuery{
  35. Path: "/store",
  36. Data: []byte(key),
  37. })
  38. require.Equal(t, code.CodeTypeOK, resQuery.Code)
  39. require.Equal(t, key, string(resQuery.Key))
  40. require.Equal(t, value, string(resQuery.Value))
  41. require.EqualValues(t, info.LastBlockHeight, resQuery.Height)
  42. // make sure proof is fine
  43. resQuery = app.Query(types.RequestQuery{
  44. Path: "/store",
  45. Data: []byte(key),
  46. Prove: true,
  47. })
  48. require.EqualValues(t, code.CodeTypeOK, resQuery.Code)
  49. require.Equal(t, key, string(resQuery.Key))
  50. require.Equal(t, value, string(resQuery.Value))
  51. require.EqualValues(t, info.LastBlockHeight, resQuery.Height)
  52. }
  53. func TestKVStoreKV(t *testing.T) {
  54. kvstore := NewApplication()
  55. key := testKey
  56. value := key
  57. tx := []byte(key)
  58. testKVStore(t, kvstore, tx, key, value)
  59. value = testValue
  60. tx = []byte(key + "=" + value)
  61. testKVStore(t, kvstore, tx, key, value)
  62. }
  63. func TestPersistentKVStoreKV(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. key := testKey
  70. value := key
  71. tx := []byte(key)
  72. testKVStore(t, kvstore, tx, key, value)
  73. value = testValue
  74. tx = []byte(key + "=" + value)
  75. testKVStore(t, kvstore, tx, key, value)
  76. }
  77. func TestPersistentKVStoreInfo(t *testing.T) {
  78. dir, err := ioutil.TempDir("/tmp", "abci-kvstore-test") // TODO
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. kvstore := NewPersistentKVStoreApplication(dir)
  83. InitKVStore(kvstore)
  84. height := int64(0)
  85. resInfo := kvstore.Info(types.RequestInfo{})
  86. if resInfo.LastBlockHeight != height {
  87. t.Fatalf("expected height of %d, got %d", height, resInfo.LastBlockHeight)
  88. }
  89. // make and apply block
  90. height = int64(1)
  91. hash := []byte("foo")
  92. header := tmproto.Header{
  93. Height: height,
  94. }
  95. kvstore.BeginBlock(types.RequestBeginBlock{Hash: hash, Header: header})
  96. kvstore.EndBlock(types.RequestEndBlock{Height: header.Height})
  97. kvstore.Commit()
  98. resInfo = kvstore.Info(types.RequestInfo{})
  99. if resInfo.LastBlockHeight != height {
  100. t.Fatalf("expected height of %d, got %d", height, resInfo.LastBlockHeight)
  101. }
  102. }
  103. // add a validator, remove a validator, update a validator
  104. func TestValUpdates(t *testing.T) {
  105. dir, err := ioutil.TempDir("/tmp", "abci-kvstore-test") // TODO
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. kvstore := NewPersistentKVStoreApplication(dir)
  110. // init with some validators
  111. total := 10
  112. nInit := 5
  113. vals := RandVals(total)
  114. // initialize with the first nInit
  115. kvstore.InitChain(types.RequestInitChain{
  116. Validators: vals[:nInit],
  117. })
  118. vals1, vals2 := vals[:nInit], kvstore.Validators()
  119. valsEqual(t, vals1, vals2)
  120. var v1, v2, v3 types.ValidatorUpdate
  121. // add some validators
  122. v1, v2 = vals[nInit], vals[nInit+1]
  123. diff := []types.ValidatorUpdate{v1, v2}
  124. tx1 := MakeValSetChangeTx(v1.PubKey, v1.Power)
  125. tx2 := MakeValSetChangeTx(v2.PubKey, v2.Power)
  126. makeApplyBlock(t, kvstore, 1, diff, tx1, tx2)
  127. vals1, vals2 = vals[:nInit+2], kvstore.Validators()
  128. valsEqual(t, vals1, vals2)
  129. // remove some validators
  130. v1, v2, v3 = vals[nInit-2], vals[nInit-1], vals[nInit]
  131. v1.Power = 0
  132. v2.Power = 0
  133. v3.Power = 0
  134. diff = []types.ValidatorUpdate{v1, v2, v3}
  135. tx1 = MakeValSetChangeTx(v1.PubKey, v1.Power)
  136. tx2 = MakeValSetChangeTx(v2.PubKey, v2.Power)
  137. tx3 := MakeValSetChangeTx(v3.PubKey, v3.Power)
  138. makeApplyBlock(t, kvstore, 2, diff, tx1, tx2, tx3)
  139. vals1 = append(vals[:nInit-2], vals[nInit+1])
  140. vals2 = kvstore.Validators()
  141. valsEqual(t, vals1, vals2)
  142. // update some validators
  143. v1 = vals[0]
  144. if v1.Power == 5 {
  145. v1.Power = 6
  146. } else {
  147. v1.Power = 5
  148. }
  149. diff = []types.ValidatorUpdate{v1}
  150. tx1 = MakeValSetChangeTx(v1.PubKey, v1.Power)
  151. makeApplyBlock(t, kvstore, 3, diff, tx1)
  152. vals1 = append([]types.ValidatorUpdate{v1}, vals1[1:]...)
  153. vals2 = kvstore.Validators()
  154. valsEqual(t, vals1, vals2)
  155. }
  156. func makeApplyBlock(
  157. t *testing.T,
  158. kvstore types.Application,
  159. heightInt int,
  160. diff []types.ValidatorUpdate,
  161. txs ...[]byte) {
  162. // make and apply block
  163. height := int64(heightInt)
  164. hash := []byte("foo")
  165. header := tmproto.Header{
  166. Height: height,
  167. }
  168. kvstore.BeginBlock(types.RequestBeginBlock{Hash: hash, Header: header})
  169. for _, tx := range txs {
  170. if r := kvstore.DeliverTx(types.RequestDeliverTx{Tx: tx}); r.IsErr() {
  171. t.Fatal(r)
  172. }
  173. }
  174. resEndBlock := kvstore.EndBlock(types.RequestEndBlock{Height: header.Height})
  175. kvstore.Commit()
  176. valsEqual(t, diff, resEndBlock.ValidatorUpdates)
  177. }
  178. // order doesn't matter
  179. func valsEqual(t *testing.T, vals1, vals2 []types.ValidatorUpdate) {
  180. if len(vals1) != len(vals2) {
  181. t.Fatalf("vals dont match in len. got %d, expected %d", len(vals2), len(vals1))
  182. }
  183. sort.Sort(types.ValidatorUpdates(vals1))
  184. sort.Sort(types.ValidatorUpdates(vals2))
  185. for i, v1 := range vals1 {
  186. v2 := vals2[i]
  187. if !v1.PubKey.Equal(v2.PubKey) ||
  188. v1.Power != v2.Power {
  189. t.Fatalf("vals dont match at index %d. got %X/%d , expected %X/%d", i, v2.PubKey, v2.Power, v1.PubKey, v1.Power)
  190. }
  191. }
  192. }
  193. func makeSocketClientServer(app types.Application, name string) (abciclient.Client, service.Service, error) {
  194. // Start the listener
  195. socket := fmt.Sprintf("unix://%s.sock", name)
  196. logger := log.TestingLogger()
  197. server := abciserver.NewSocketServer(socket, app)
  198. server.SetLogger(logger.With("module", "abci-server"))
  199. if err := server.Start(); err != nil {
  200. return nil, nil, err
  201. }
  202. // Connect to the socket
  203. client := abciclient.NewSocketClient(socket, false)
  204. client.SetLogger(logger.With("module", "abci-client"))
  205. if err := client.Start(); err != nil {
  206. if err = server.Stop(); err != nil {
  207. return nil, nil, err
  208. }
  209. return nil, nil, err
  210. }
  211. return client, server, nil
  212. }
  213. func makeGRPCClientServer(app types.Application, name string) (abciclient.Client, service.Service, error) {
  214. // Start the listener
  215. socket := fmt.Sprintf("unix://%s.sock", name)
  216. logger := log.TestingLogger()
  217. gapp := types.NewGRPCApplication(app)
  218. server := abciserver.NewGRPCServer(socket, gapp)
  219. server.SetLogger(logger.With("module", "abci-server"))
  220. if err := server.Start(); err != nil {
  221. return nil, nil, err
  222. }
  223. client := abciclient.NewGRPCClient(socket, true)
  224. client.SetLogger(logger.With("module", "abci-client"))
  225. if err := client.Start(); err != nil {
  226. if err := server.Stop(); err != nil {
  227. return nil, nil, err
  228. }
  229. return nil, nil, err
  230. }
  231. return client, server, nil
  232. }
  233. func TestClientServer(t *testing.T) {
  234. // set up socket app
  235. kvstore := NewApplication()
  236. client, server, err := makeSocketClientServer(kvstore, "kvstore-socket")
  237. require.NoError(t, err)
  238. t.Cleanup(func() {
  239. if err := server.Stop(); err != nil {
  240. t.Error(err)
  241. }
  242. })
  243. t.Cleanup(func() {
  244. if err := client.Stop(); err != nil {
  245. t.Error(err)
  246. }
  247. })
  248. runClientTests(t, client)
  249. // set up grpc app
  250. kvstore = NewApplication()
  251. gclient, gserver, err := makeGRPCClientServer(kvstore, "/tmp/kvstore-grpc")
  252. require.NoError(t, err)
  253. t.Cleanup(func() {
  254. if err := gserver.Stop(); err != nil {
  255. t.Error(err)
  256. }
  257. })
  258. t.Cleanup(func() {
  259. if err := gclient.Stop(); err != nil {
  260. t.Error(err)
  261. }
  262. })
  263. runClientTests(t, gclient)
  264. }
  265. func runClientTests(t *testing.T, client abciclient.Client) {
  266. // run some tests....
  267. key := testKey
  268. value := key
  269. tx := []byte(key)
  270. testClient(t, client, tx, key, value)
  271. value = testValue
  272. tx = []byte(key + "=" + value)
  273. testClient(t, client, tx, key, value)
  274. }
  275. func testClient(t *testing.T, app abciclient.Client, tx []byte, key, value string) {
  276. ar, err := app.DeliverTxSync(ctx, types.RequestDeliverTx{Tx: tx})
  277. require.NoError(t, err)
  278. require.False(t, ar.IsErr(), ar)
  279. // repeating tx doesn't raise error
  280. ar, err = app.DeliverTxSync(ctx, types.RequestDeliverTx{Tx: tx})
  281. require.NoError(t, err)
  282. require.False(t, ar.IsErr(), ar)
  283. // commit
  284. _, err = app.CommitSync(ctx)
  285. require.NoError(t, err)
  286. info, err := app.InfoSync(ctx, types.RequestInfo{})
  287. require.NoError(t, err)
  288. require.NotZero(t, info.LastBlockHeight)
  289. // make sure query is fine
  290. resQuery, err := app.QuerySync(ctx, types.RequestQuery{
  291. Path: "/store",
  292. Data: []byte(key),
  293. })
  294. require.Nil(t, err)
  295. require.Equal(t, code.CodeTypeOK, resQuery.Code)
  296. require.Equal(t, key, string(resQuery.Key))
  297. require.Equal(t, value, string(resQuery.Value))
  298. require.EqualValues(t, info.LastBlockHeight, resQuery.Height)
  299. // make sure proof is fine
  300. resQuery, err = app.QuerySync(ctx, types.RequestQuery{
  301. Path: "/store",
  302. Data: []byte(key),
  303. Prove: true,
  304. })
  305. require.Nil(t, err)
  306. require.Equal(t, code.CodeTypeOK, resQuery.Code)
  307. require.Equal(t, key, string(resQuery.Key))
  308. require.Equal(t, value, string(resQuery.Value))
  309. require.EqualValues(t, info.LastBlockHeight, resQuery.Height)
  310. }