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.

342 lines
9.0 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. server.Stop()
  205. return nil, nil, err
  206. }
  207. return client, server, nil
  208. }
  209. func makeGRPCClientServer(app types.Application, name string) (abcicli.Client, service.Service, error) {
  210. // Start the listener
  211. socket := fmt.Sprintf("unix://%s.sock", name)
  212. logger := log.TestingLogger()
  213. gapp := types.NewGRPCApplication(app)
  214. server := abciserver.NewGRPCServer(socket, gapp)
  215. server.SetLogger(logger.With("module", "abci-server"))
  216. if err := server.Start(); err != nil {
  217. return nil, nil, err
  218. }
  219. client := abcicli.NewGRPCClient(socket, true)
  220. client.SetLogger(logger.With("module", "abci-client"))
  221. if err := client.Start(); err != nil {
  222. server.Stop()
  223. return nil, nil, err
  224. }
  225. return client, server, nil
  226. }
  227. func TestClientServer(t *testing.T) {
  228. // set up socket app
  229. kvstore := NewApplication()
  230. client, server, err := makeSocketClientServer(kvstore, "kvstore-socket")
  231. require.NoError(t, err)
  232. defer server.Stop()
  233. defer client.Stop()
  234. runClientTests(t, client)
  235. // set up grpc app
  236. kvstore = NewApplication()
  237. gclient, gserver, err := makeGRPCClientServer(kvstore, "kvstore-grpc")
  238. require.NoError(t, err)
  239. defer gserver.Stop()
  240. defer gclient.Stop()
  241. runClientTests(t, gclient)
  242. }
  243. func runClientTests(t *testing.T, client abcicli.Client) {
  244. // run some tests....
  245. key := testKey
  246. value := key
  247. tx := []byte(key)
  248. testClient(t, client, tx, key, value)
  249. value = testValue
  250. tx = []byte(key + "=" + value)
  251. testClient(t, client, tx, key, value)
  252. }
  253. func testClient(t *testing.T, app abcicli.Client, tx []byte, key, value string) {
  254. ar, err := app.DeliverTxSync(types.RequestDeliverTx{Tx: tx})
  255. require.NoError(t, err)
  256. require.False(t, ar.IsErr(), ar)
  257. // repeating tx doesn't raise error
  258. ar, err = app.DeliverTxSync(types.RequestDeliverTx{Tx: tx})
  259. require.NoError(t, err)
  260. require.False(t, ar.IsErr(), ar)
  261. // commit
  262. _, err = app.CommitSync()
  263. require.NoError(t, err)
  264. info, err := app.InfoSync(types.RequestInfo{})
  265. require.NoError(t, err)
  266. require.NotZero(t, info.LastBlockHeight)
  267. // make sure query is fine
  268. resQuery, err := app.QuerySync(types.RequestQuery{
  269. Path: "/store",
  270. Data: []byte(key),
  271. })
  272. require.Nil(t, err)
  273. require.Equal(t, code.CodeTypeOK, resQuery.Code)
  274. require.Equal(t, key, string(resQuery.Key))
  275. require.Equal(t, value, string(resQuery.Value))
  276. require.EqualValues(t, info.LastBlockHeight, resQuery.Height)
  277. // make sure proof is fine
  278. resQuery, err = app.QuerySync(types.RequestQuery{
  279. Path: "/store",
  280. Data: []byte(key),
  281. Prove: true,
  282. })
  283. require.Nil(t, err)
  284. require.Equal(t, code.CodeTypeOK, resQuery.Code)
  285. require.Equal(t, key, string(resQuery.Key))
  286. require.Equal(t, value, string(resQuery.Value))
  287. require.EqualValues(t, info.LastBlockHeight, resQuery.Height)
  288. }