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.

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