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. "sort"
  6. "testing"
  7. "github.com/fortytw2/leaktest"
  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. func testKVStore(t *testing.T, app types.Application, tx []byte, key, value string) {
  22. req := types.RequestFinalizeBlock{Txs: [][]byte{tx}}
  23. ar := app.FinalizeBlock(req)
  24. require.Equal(t, 1, len(ar.Txs))
  25. require.False(t, ar.Txs[0].IsErr())
  26. // repeating tx doesn't raise error
  27. ar = app.FinalizeBlock(req)
  28. require.Equal(t, 1, len(ar.Txs))
  29. require.False(t, ar.Txs[0].IsErr())
  30. // commit
  31. app.Commit()
  32. info := app.Info(types.RequestInfo{})
  33. require.NotZero(t, info.LastBlockHeight)
  34. // make sure query is fine
  35. resQuery := app.Query(types.RequestQuery{
  36. Path: "/store",
  37. Data: []byte(key),
  38. })
  39. require.Equal(t, code.CodeTypeOK, resQuery.Code)
  40. require.Equal(t, key, string(resQuery.Key))
  41. require.Equal(t, value, string(resQuery.Value))
  42. require.EqualValues(t, info.LastBlockHeight, resQuery.Height)
  43. // make sure proof is fine
  44. resQuery = app.Query(types.RequestQuery{
  45. Path: "/store",
  46. Data: []byte(key),
  47. Prove: true,
  48. })
  49. require.EqualValues(t, code.CodeTypeOK, resQuery.Code)
  50. require.Equal(t, key, string(resQuery.Key))
  51. require.Equal(t, value, string(resQuery.Value))
  52. require.EqualValues(t, info.LastBlockHeight, resQuery.Height)
  53. }
  54. func TestKVStoreKV(t *testing.T) {
  55. kvstore := NewApplication()
  56. key := testKey
  57. value := key
  58. tx := []byte(key)
  59. testKVStore(t, kvstore, tx, key, value)
  60. value = testValue
  61. tx = []byte(key + "=" + value)
  62. testKVStore(t, kvstore, tx, key, value)
  63. }
  64. func TestPersistentKVStoreKV(t *testing.T) {
  65. dir := t.TempDir()
  66. logger := log.NewNopLogger()
  67. kvstore := NewPersistentKVStoreApplication(logger, dir)
  68. key := testKey
  69. value := key
  70. tx := []byte(key)
  71. testKVStore(t, kvstore, tx, key, value)
  72. value = testValue
  73. tx = []byte(key + "=" + value)
  74. testKVStore(t, kvstore, tx, key, value)
  75. }
  76. func TestPersistentKVStoreInfo(t *testing.T) {
  77. dir := t.TempDir()
  78. logger := log.NewNopLogger()
  79. kvstore := NewPersistentKVStoreApplication(logger, dir)
  80. InitKVStore(kvstore)
  81. height := int64(0)
  82. resInfo := kvstore.Info(types.RequestInfo{})
  83. if resInfo.LastBlockHeight != height {
  84. t.Fatalf("expected height of %d, got %d", height, resInfo.LastBlockHeight)
  85. }
  86. // make and apply block
  87. height = int64(1)
  88. hash := []byte("foo")
  89. header := tmproto.Header{
  90. Height: height,
  91. }
  92. kvstore.FinalizeBlock(types.RequestFinalizeBlock{Hash: hash, Header: header, Height: height})
  93. kvstore.Commit()
  94. resInfo = kvstore.Info(types.RequestInfo{})
  95. if resInfo.LastBlockHeight != height {
  96. t.Fatalf("expected height of %d, got %d", height, resInfo.LastBlockHeight)
  97. }
  98. }
  99. // add a validator, remove a validator, update a validator
  100. func TestValUpdates(t *testing.T) {
  101. kvstore := NewApplication()
  102. // init with some validators
  103. total := 10
  104. nInit := 5
  105. vals := RandVals(total)
  106. // initialize with the first nInit
  107. kvstore.InitChain(types.RequestInitChain{
  108. Validators: vals[:nInit],
  109. })
  110. vals1, vals2 := vals[:nInit], kvstore.Validators()
  111. valsEqual(t, vals1, vals2)
  112. var v1, v2, v3 types.ValidatorUpdate
  113. // add some validators
  114. v1, v2 = vals[nInit], vals[nInit+1]
  115. diff := []types.ValidatorUpdate{v1, v2}
  116. tx1 := MakeValSetChangeTx(v1.PubKey, v1.Power)
  117. tx2 := MakeValSetChangeTx(v2.PubKey, v2.Power)
  118. makeApplyBlock(t, kvstore, 1, diff, tx1, tx2)
  119. vals1, vals2 = vals[:nInit+2], kvstore.Validators()
  120. valsEqual(t, vals1, vals2)
  121. // remove some validators
  122. v1, v2, v3 = vals[nInit-2], vals[nInit-1], vals[nInit]
  123. v1.Power = 0
  124. v2.Power = 0
  125. v3.Power = 0
  126. diff = []types.ValidatorUpdate{v1, v2, v3}
  127. tx1 = MakeValSetChangeTx(v1.PubKey, v1.Power)
  128. tx2 = MakeValSetChangeTx(v2.PubKey, v2.Power)
  129. tx3 := MakeValSetChangeTx(v3.PubKey, v3.Power)
  130. makeApplyBlock(t, kvstore, 2, diff, tx1, tx2, tx3)
  131. vals1 = append(vals[:nInit-2], vals[nInit+1]) // nolint: gocritic
  132. vals2 = kvstore.Validators()
  133. valsEqual(t, vals1, vals2)
  134. // update some validators
  135. v1 = vals[0]
  136. if v1.Power == 5 {
  137. v1.Power = 6
  138. } else {
  139. v1.Power = 5
  140. }
  141. diff = []types.ValidatorUpdate{v1}
  142. tx1 = MakeValSetChangeTx(v1.PubKey, v1.Power)
  143. makeApplyBlock(t, kvstore, 3, diff, tx1)
  144. vals1 = append([]types.ValidatorUpdate{v1}, vals1[1:]...)
  145. vals2 = kvstore.Validators()
  146. valsEqual(t, vals1, vals2)
  147. }
  148. func makeApplyBlock(
  149. t *testing.T,
  150. kvstore types.Application,
  151. heightInt int,
  152. diff []types.ValidatorUpdate,
  153. txs ...[]byte) {
  154. // make and apply block
  155. height := int64(heightInt)
  156. hash := []byte("foo")
  157. header := tmproto.Header{
  158. Height: height,
  159. }
  160. resFinalizeBlock := kvstore.FinalizeBlock(types.RequestFinalizeBlock{
  161. Hash: hash,
  162. Header: header,
  163. Height: height,
  164. Txs: txs,
  165. })
  166. kvstore.Commit()
  167. valsEqual(t, diff, resFinalizeBlock.ValidatorUpdates)
  168. }
  169. // order doesn't matter
  170. func valsEqual(t *testing.T, vals1, vals2 []types.ValidatorUpdate) {
  171. t.Helper()
  172. if len(vals1) != len(vals2) {
  173. t.Fatalf("vals dont match in len. got %d, expected %d", len(vals2), len(vals1))
  174. }
  175. sort.Sort(types.ValidatorUpdates(vals1))
  176. sort.Sort(types.ValidatorUpdates(vals2))
  177. for i, v1 := range vals1 {
  178. v2 := vals2[i]
  179. if !v1.PubKey.Equal(v2.PubKey) ||
  180. v1.Power != v2.Power {
  181. t.Fatalf("vals dont match at index %d. got %X/%d , expected %X/%d", i, v2.PubKey, v2.Power, v1.PubKey, v1.Power)
  182. }
  183. }
  184. }
  185. func makeSocketClientServer(
  186. ctx context.Context,
  187. t *testing.T,
  188. logger log.Logger,
  189. app types.Application,
  190. name string,
  191. ) (abciclient.Client, service.Service, error) {
  192. t.Helper()
  193. ctx, cancel := context.WithCancel(ctx)
  194. t.Cleanup(cancel)
  195. t.Cleanup(leaktest.Check(t))
  196. // Start the listener
  197. socket := fmt.Sprintf("unix://%s.sock", name)
  198. server := abciserver.NewSocketServer(logger.With("module", "abci-server"), socket, app)
  199. if err := server.Start(ctx); err != nil {
  200. cancel()
  201. return nil, nil, err
  202. }
  203. // Connect to the socket
  204. client := abciclient.NewSocketClient(logger.With("module", "abci-client"), socket, false)
  205. if err := client.Start(ctx); err != nil {
  206. cancel()
  207. return nil, nil, err
  208. }
  209. return client, server, nil
  210. }
  211. func makeGRPCClientServer(
  212. ctx context.Context,
  213. t *testing.T,
  214. logger log.Logger,
  215. app types.Application,
  216. name string,
  217. ) (abciclient.Client, service.Service, error) {
  218. ctx, cancel := context.WithCancel(ctx)
  219. t.Cleanup(cancel)
  220. t.Cleanup(leaktest.Check(t))
  221. // Start the listener
  222. socket := fmt.Sprintf("unix://%s.sock", name)
  223. gapp := types.NewGRPCApplication(app)
  224. server := abciserver.NewGRPCServer(logger.With("module", "abci-server"), socket, gapp)
  225. if err := server.Start(ctx); err != nil {
  226. cancel()
  227. return nil, nil, err
  228. }
  229. client := abciclient.NewGRPCClient(logger.With("module", "abci-client"), socket, true)
  230. if err := client.Start(ctx); err != nil {
  231. cancel()
  232. return nil, nil, err
  233. }
  234. return client, server, nil
  235. }
  236. func TestClientServer(t *testing.T) {
  237. ctx, cancel := context.WithCancel(context.Background())
  238. defer cancel()
  239. logger := log.NewNopLogger()
  240. // set up socket app
  241. kvstore := NewApplication()
  242. client, server, err := makeSocketClientServer(ctx, t, logger, kvstore, "kvstore-socket")
  243. require.NoError(t, err)
  244. t.Cleanup(func() { cancel(); server.Wait() })
  245. t.Cleanup(func() { cancel(); client.Wait() })
  246. runClientTests(ctx, t, client)
  247. // set up grpc app
  248. kvstore = NewApplication()
  249. gclient, gserver, err := makeGRPCClientServer(ctx, t, logger, kvstore, "/tmp/kvstore-grpc")
  250. require.NoError(t, err)
  251. t.Cleanup(func() { cancel(); gserver.Wait() })
  252. t.Cleanup(func() { cancel(); gclient.Wait() })
  253. runClientTests(ctx, t, gclient)
  254. }
  255. func runClientTests(ctx context.Context, t *testing.T, client abciclient.Client) {
  256. // run some tests....
  257. key := testKey
  258. value := key
  259. tx := []byte(key)
  260. testClient(ctx, t, client, tx, key, value)
  261. value = testValue
  262. tx = []byte(key + "=" + value)
  263. testClient(ctx, t, client, tx, key, value)
  264. }
  265. func testClient(ctx context.Context, t *testing.T, app abciclient.Client, tx []byte, key, value string) {
  266. ar, err := app.FinalizeBlock(ctx, types.RequestFinalizeBlock{Txs: [][]byte{tx}})
  267. require.NoError(t, err)
  268. require.Equal(t, 1, len(ar.Txs))
  269. require.False(t, ar.Txs[0].IsErr())
  270. // repeating FinalizeBlock doesn't raise error
  271. ar, err = app.FinalizeBlock(ctx, types.RequestFinalizeBlock{Txs: [][]byte{tx}})
  272. require.NoError(t, err)
  273. require.Equal(t, 1, len(ar.Txs))
  274. require.False(t, ar.Txs[0].IsErr())
  275. // commit
  276. _, err = app.Commit(ctx)
  277. require.NoError(t, err)
  278. info, err := app.Info(ctx, types.RequestInfo{})
  279. require.NoError(t, err)
  280. require.NotZero(t, info.LastBlockHeight)
  281. // make sure query is fine
  282. resQuery, err := app.Query(ctx, types.RequestQuery{
  283. Path: "/store",
  284. Data: []byte(key),
  285. })
  286. require.NoError(t, err)
  287. require.Equal(t, code.CodeTypeOK, resQuery.Code)
  288. require.Equal(t, key, string(resQuery.Key))
  289. require.Equal(t, value, string(resQuery.Value))
  290. require.EqualValues(t, info.LastBlockHeight, resQuery.Height)
  291. // make sure proof is fine
  292. resQuery, err = app.Query(ctx, types.RequestQuery{
  293. Path: "/store",
  294. Data: []byte(key),
  295. Prove: true,
  296. })
  297. require.NoError(t, err)
  298. require.Equal(t, code.CodeTypeOK, resQuery.Code)
  299. require.Equal(t, key, string(resQuery.Key))
  300. require.Equal(t, value, string(resQuery.Value))
  301. require.EqualValues(t, info.LastBlockHeight, resQuery.Height)
  302. }