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.

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