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.

97 lines
2.7 KiB

  1. package testsuite
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. abcicli "github.com/tendermint/abci/client"
  7. "github.com/tendermint/abci/types"
  8. cmn "github.com/tendermint/tmlibs/common"
  9. )
  10. func InitChain(client abcicli.Client) error {
  11. total := 10
  12. vals := make([]types.Validator, total)
  13. for i := 0; i < total; i++ {
  14. pubkey := cmn.RandBytes(33)
  15. power := cmn.RandInt()
  16. vals[i] = types.Validator{pubkey, int64(power)}
  17. }
  18. _, err := client.InitChainSync(types.RequestInitChain{
  19. Validators: vals,
  20. AppStateBytes: []byte("{}"),
  21. })
  22. if err != nil {
  23. fmt.Printf("Failed test: InitChain - %v\n", err)
  24. return err
  25. }
  26. fmt.Println("Passed test: InitChain")
  27. return nil
  28. }
  29. func SetOption(client abcicli.Client, key, value string) error {
  30. _, err := client.SetOptionSync(types.RequestSetOption{Key: key, Value: value})
  31. if err != nil {
  32. fmt.Println("Failed test: SetOption")
  33. fmt.Printf("error while setting %v=%v: \nerror: %v\n", key, value, err)
  34. return err
  35. }
  36. fmt.Println("Passed test: SetOption")
  37. return nil
  38. }
  39. func Commit(client abcicli.Client, hashExp []byte) error {
  40. res, err := client.CommitSync()
  41. data := res.Data
  42. if err != nil {
  43. fmt.Println("Failed test: Commit")
  44. fmt.Printf("error while committing: %v\n", err)
  45. return err
  46. }
  47. if !bytes.Equal(data, hashExp) {
  48. fmt.Println("Failed test: Commit")
  49. fmt.Printf("Commit hash was unexpected. Got %X expected %X\n", data, hashExp)
  50. return errors.New("CommitTx failed")
  51. }
  52. fmt.Println("Passed test: Commit")
  53. return nil
  54. }
  55. func DeliverTx(client abcicli.Client, txBytes []byte, codeExp uint32, dataExp []byte) error {
  56. res, _ := client.DeliverTxSync(txBytes)
  57. code, data, log := res.Code, res.Data, res.Log
  58. if code != codeExp {
  59. fmt.Println("Failed test: DeliverTx")
  60. fmt.Printf("DeliverTx response code was unexpected. Got %v expected %v. Log: %v\n",
  61. code, codeExp, log)
  62. return errors.New("DeliverTx error")
  63. }
  64. if !bytes.Equal(data, dataExp) {
  65. fmt.Println("Failed test: DeliverTx")
  66. fmt.Printf("DeliverTx response data was unexpected. Got %X expected %X\n",
  67. data, dataExp)
  68. return errors.New("DeliverTx error")
  69. }
  70. fmt.Println("Passed test: DeliverTx")
  71. return nil
  72. }
  73. func CheckTx(client abcicli.Client, txBytes []byte, codeExp uint32, dataExp []byte) error {
  74. res, _ := client.CheckTxSync(txBytes)
  75. code, data, log := res.Code, res.Data, res.Log
  76. if code != codeExp {
  77. fmt.Println("Failed test: CheckTx")
  78. fmt.Printf("CheckTx response code was unexpected. Got %v expected %v. Log: %v\n",
  79. code, codeExp, log)
  80. return errors.New("CheckTx")
  81. }
  82. if !bytes.Equal(data, dataExp) {
  83. fmt.Println("Failed test: CheckTx")
  84. fmt.Printf("CheckTx response data was unexpected. Got %X expected %X\n",
  85. data, dataExp)
  86. return errors.New("CheckTx")
  87. }
  88. fmt.Println("Passed test: CheckTx")
  89. return nil
  90. }