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.

98 lines
2.8 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. crypto "github.com/tendermint/go-crypto"
  9. cmn "github.com/tendermint/tmlibs/common"
  10. )
  11. func InitChain(client abcicli.Client) error {
  12. total := 10
  13. vals := make([]*types.Validator, total)
  14. for i := 0; i < total; i++ {
  15. pubkey := crypto.GenPrivKeyEd25519FromSecret([]byte(cmn.Fmt("test%d", i))).PubKey().Bytes()
  16. power := cmn.RandInt()
  17. vals[i] = &types.Validator{pubkey, int64(power)}
  18. }
  19. _, err := client.InitChainSync(types.RequestInitChain{Validators: vals})
  20. if err != nil {
  21. fmt.Println("Failed test: InitChain - %v", err)
  22. return err
  23. }
  24. fmt.Println("Passed test: InitChain")
  25. return nil
  26. }
  27. func SetOption(client abcicli.Client, key, value string) error {
  28. res, err := client.SetOptionSync(types.RequestSetOption{Key: key, Value: value})
  29. log := res.GetLog()
  30. if err != nil {
  31. fmt.Println("Failed test: SetOption")
  32. fmt.Printf("setting %v=%v: \nlog: %v", key, value, log)
  33. fmt.Println("Failed test: SetOption")
  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.Code, res.Data
  42. if err != nil {
  43. fmt.Println("Failed test: Commit")
  44. fmt.Printf("committing %v\nlog: %v", res.GetLog())
  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",
  50. data.Bytes(), hashExp)
  51. return errors.New("CommitTx failed")
  52. }
  53. fmt.Println("Passed test: Commit")
  54. return nil
  55. }
  56. func DeliverTx(client abcicli.Client, txBytes []byte, codeExp uint32, dataExp []byte) error {
  57. res, _ := client.DeliverTxSync(txBytes)
  58. code, data, log := res.Code, res.Data, res.Log
  59. if code != codeExp {
  60. fmt.Println("Failed test: DeliverTx")
  61. fmt.Printf("DeliverTx response code was unexpected. Got %v expected %v. Log: %v",
  62. code, codeExp, log)
  63. return errors.New("DeliverTx error")
  64. }
  65. if !bytes.Equal(data, dataExp) {
  66. fmt.Println("Failed test: DeliverTx")
  67. fmt.Printf("DeliverTx response data was unexpected. Got %X expected %X",
  68. data, dataExp)
  69. return errors.New("DeliverTx error")
  70. }
  71. fmt.Println("Passed test: DeliverTx")
  72. return nil
  73. }
  74. func CheckTx(client abcicli.Client, txBytes []byte, codeExp uint32, dataExp []byte) error {
  75. res, _ := client.CheckTxSync(txBytes)
  76. code, data, log := res.Code, res.Data, res.Log
  77. if code != codeExp {
  78. fmt.Println("Failed test: CheckTx")
  79. fmt.Printf("CheckTx response code was unexpected. Got %v expected %v. Log: %v",
  80. code, codeExp, log)
  81. return errors.New("CheckTx")
  82. }
  83. if !bytes.Equal(data, dataExp) {
  84. fmt.Println("Failed test: CheckTx")
  85. fmt.Printf("CheckTx response data was unexpected. Got %X expected %X",
  86. data, dataExp)
  87. return errors.New("CheckTx")
  88. }
  89. fmt.Println("Passed test: CheckTx")
  90. return nil
  91. }