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.

321 lines
7.8 KiB

  1. package dummy
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. "sort"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. . "github.com/tendermint/go-common"
  10. "github.com/tendermint/go-crypto"
  11. merkle "github.com/tendermint/go-merkle"
  12. "github.com/tendermint/go-wire"
  13. <<<<<<< HEAD
  14. "github.com/tendermint/abci/types"
  15. )
  16. func testDummy(t *testing.T, dummy types.Application, tx []byte, key, value string) {
  17. if r := dummy.DeliverTx(tx); r.IsErr() {
  18. t.Fatal(r)
  19. }
  20. if r := dummy.DeliverTx(tx); r.IsErr() {
  21. t.Fatal(r)
  22. }
  23. r := dummy.Query([]byte(key))
  24. if r.IsErr() {
  25. t.Fatal(r)
  26. }
  27. =======
  28. tmspcli "github.com/tendermint/tmsp/client"
  29. "github.com/tendermint/tmsp/server"
  30. "github.com/tendermint/tmsp/types"
  31. )
  32. func testDummy(t *testing.T, app types.Application, tx []byte, key, value string) {
  33. ar := app.AppendTx(tx)
  34. require.False(t, ar.IsErr(), ar)
  35. // repeating tx doesn't raise error
  36. ar = app.AppendTx(tx)
  37. require.False(t, ar.IsErr(), ar)
  38. >>>>>>> Add tests for client-server proofs over socket and grpc
  39. // make sure query is fine
  40. r := app.Query([]byte(key))
  41. require.False(t, r.IsErr(), r)
  42. q := new(QueryResult)
  43. err := wire.ReadJSONBytes(r.Data, q)
  44. require.Nil(t, err)
  45. require.Equal(t, value, q.Value)
  46. // make sure proof is fine
  47. rp := app.Proof([]byte(key), 0)
  48. require.False(t, rp.IsErr(), rp)
  49. p, err := merkle.LoadProof(rp.Data)
  50. require.Nil(t, err)
  51. require.True(t, p.Valid())
  52. assert.Equal(t, []byte(key), p.Key())
  53. assert.Equal(t, []byte(value), p.Value())
  54. }
  55. func TestDummyKV(t *testing.T) {
  56. dummy := NewDummyApplication()
  57. key := "abc"
  58. value := key
  59. tx := []byte(key)
  60. testDummy(t, dummy, tx, key, value)
  61. value = "def"
  62. tx = []byte(key + "=" + value)
  63. testDummy(t, dummy, tx, key, value)
  64. }
  65. func TestPersistentDummyKV(t *testing.T) {
  66. dir, err := ioutil.TempDir("/tmp", "abci-dummy-test") // TODO
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. dummy := NewPersistentDummyApplication(dir)
  71. key := "abc"
  72. value := key
  73. tx := []byte(key)
  74. testDummy(t, dummy, tx, key, value)
  75. value = "def"
  76. tx = []byte(key + "=" + value)
  77. testDummy(t, dummy, tx, key, value)
  78. }
  79. func TestPersistentDummyInfo(t *testing.T) {
  80. dir, err := ioutil.TempDir("/tmp", "abci-dummy-test") // TODO
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. dummy := NewPersistentDummyApplication(dir)
  85. height := uint64(0)
  86. resInfo := dummy.Info()
  87. if resInfo.LastBlockHeight != height {
  88. t.Fatalf("expected height of %d, got %d", height, resInfo.LastBlockHeight)
  89. }
  90. // make and apply block
  91. height = uint64(1)
  92. hash := []byte("foo")
  93. header := &types.Header{
  94. Height: uint64(height),
  95. }
  96. dummy.BeginBlock(hash, header)
  97. dummy.EndBlock(height)
  98. dummy.Commit()
  99. resInfo = dummy.Info()
  100. if resInfo.LastBlockHeight != height {
  101. t.Fatalf("expected height of %d, got %d", height, resInfo.LastBlockHeight)
  102. }
  103. }
  104. // add a validator, remove a validator, update a validator
  105. func TestValSetChanges(t *testing.T) {
  106. dir, err := ioutil.TempDir("/tmp", "abci-dummy-test") // TODO
  107. if err != nil {
  108. t.Fatal(err)
  109. }
  110. dummy := NewPersistentDummyApplication(dir)
  111. // init with some validators
  112. total := 10
  113. nInit := 5
  114. vals := make([]*types.Validator, total)
  115. for i := 0; i < total; i++ {
  116. pubkey := crypto.GenPrivKeyEd25519FromSecret([]byte(Fmt("test%d", i))).PubKey().Bytes()
  117. power := RandInt()
  118. vals[i] = &types.Validator{pubkey, uint64(power)}
  119. }
  120. // iniitalize with the first nInit
  121. dummy.InitChain(vals[:nInit])
  122. vals1, vals2 := vals[:nInit], dummy.Validators()
  123. valsEqual(t, vals1, vals2)
  124. var v1, v2, v3 *types.Validator
  125. // add some validators
  126. v1, v2 = vals[nInit], vals[nInit+1]
  127. diff := []*types.Validator{v1, v2}
  128. tx1 := MakeValSetChangeTx(v1.PubKey, v1.Power)
  129. tx2 := MakeValSetChangeTx(v2.PubKey, v2.Power)
  130. makeApplyBlock(t, dummy, 1, diff, tx1, tx2)
  131. vals1, vals2 = vals[:nInit+2], dummy.Validators()
  132. valsEqual(t, vals1, vals2)
  133. // remove some validators
  134. v1, v2, v3 = vals[nInit-2], vals[nInit-1], vals[nInit]
  135. v1.Power = 0
  136. v2.Power = 0
  137. v3.Power = 0
  138. diff = []*types.Validator{v1, v2, v3}
  139. tx1 = MakeValSetChangeTx(v1.PubKey, v1.Power)
  140. tx2 = MakeValSetChangeTx(v2.PubKey, v2.Power)
  141. tx3 := MakeValSetChangeTx(v3.PubKey, v3.Power)
  142. makeApplyBlock(t, dummy, 2, diff, tx1, tx2, tx3)
  143. vals1 = append(vals[:nInit-2], vals[nInit+1])
  144. vals2 = dummy.Validators()
  145. valsEqual(t, vals1, vals2)
  146. // update some validators
  147. v1 = vals[0]
  148. if v1.Power == 5 {
  149. v1.Power = 6
  150. } else {
  151. v1.Power = 5
  152. }
  153. diff = []*types.Validator{v1}
  154. tx1 = MakeValSetChangeTx(v1.PubKey, v1.Power)
  155. makeApplyBlock(t, dummy, 3, diff, tx1)
  156. vals1 = append([]*types.Validator{v1}, vals1[1:len(vals1)]...)
  157. vals2 = dummy.Validators()
  158. valsEqual(t, vals1, vals2)
  159. }
  160. func makeApplyBlock(t *testing.T, dummy types.Application, heightInt int, diff []*types.Validator, txs ...[]byte) {
  161. // make and apply block
  162. height := uint64(heightInt)
  163. hash := []byte("foo")
  164. header := &types.Header{
  165. Height: height,
  166. }
  167. dummyChain := dummy.(types.BlockchainAware) // hmm...
  168. dummyChain.BeginBlock(hash, header)
  169. for _, tx := range txs {
  170. if r := dummy.DeliverTx(tx); r.IsErr() {
  171. t.Fatal(r)
  172. }
  173. }
  174. resEndBlock := dummyChain.EndBlock(height)
  175. dummy.Commit()
  176. valsEqual(t, diff, resEndBlock.Diffs)
  177. }
  178. // order doesn't matter
  179. func valsEqual(t *testing.T, vals1, vals2 []*types.Validator) {
  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.Validators(vals1))
  184. sort.Sort(types.Validators(vals2))
  185. for i, v1 := range vals1 {
  186. v2 := vals2[i]
  187. if !bytes.Equal(v1.PubKey, 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) (tmspcli.Client, Service, error) {
  194. // Start the listener
  195. socket := Fmt("unix://%s.sock", name)
  196. server, err := server.NewSocketServer(socket, app)
  197. if err != nil {
  198. return nil, nil, err
  199. }
  200. // Connect to the socket
  201. client, err := tmspcli.NewSocketClient(socket, false)
  202. if err != nil {
  203. server.Stop()
  204. return nil, nil, err
  205. }
  206. client.Start()
  207. return client, server, err
  208. }
  209. func makeGRPCClientServer(app types.Application, name string) (tmspcli.Client, Service, error) {
  210. // Start the listener
  211. socket := Fmt("unix://%s.sock", name)
  212. gapp := types.NewGRPCApplication(app)
  213. server, err := server.NewGRPCServer(socket, gapp)
  214. if err != nil {
  215. return nil, nil, err
  216. }
  217. client, err := tmspcli.NewGRPCClient(socket, true)
  218. if err != nil {
  219. server.Stop()
  220. return nil, nil, err
  221. }
  222. return client, server, err
  223. }
  224. func TestClientServer(t *testing.T) {
  225. // set up socket app
  226. dummy := NewDummyApplication()
  227. client, server, err := makeSocketClientServer(dummy, "dummy-socket")
  228. require.Nil(t, err)
  229. defer server.Stop()
  230. defer client.Stop()
  231. runClientTests(t, client)
  232. // set up grpc app
  233. dummy = NewDummyApplication()
  234. gclient, gserver, err := makeGRPCClientServer(dummy, "dummy-grpc")
  235. require.Nil(t, err)
  236. defer gserver.Stop()
  237. defer gclient.Stop()
  238. runClientTests(t, gclient)
  239. }
  240. func runClientTests(t *testing.T, client tmspcli.Client) {
  241. // run some tests....
  242. key := "abc"
  243. value := key
  244. tx := []byte(key)
  245. testClient(t, client, tx, key, value)
  246. value = "def"
  247. tx = []byte(key + "=" + value)
  248. testClient(t, client, tx, key, value)
  249. }
  250. func testClient(t *testing.T, app tmspcli.Client, tx []byte, key, value string) {
  251. ar := app.AppendTxSync(tx)
  252. require.False(t, ar.IsErr(), ar)
  253. // repeating tx doesn't raise error
  254. ar = app.AppendTxSync(tx)
  255. require.False(t, ar.IsErr(), ar)
  256. // make sure query is fine
  257. r := app.QuerySync([]byte(key))
  258. require.False(t, r.IsErr(), r)
  259. q := new(QueryResult)
  260. err := wire.ReadJSONBytes(r.Data, q)
  261. require.Nil(t, err)
  262. require.Equal(t, value, q.Value)
  263. // make sure proof is fine
  264. rp := app.ProofSync([]byte(key), 0)
  265. require.False(t, rp.IsErr(), rp)
  266. p, err := merkle.LoadProof(rp.Data)
  267. require.Nil(t, err)
  268. require.True(t, p.Valid())
  269. assert.Equal(t, []byte(key), p.Key())
  270. assert.Equal(t, []byte(value), p.Value())
  271. }