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.

90 lines
2.6 KiB

7 years ago
  1. package testsuite
  2. import (
  3. "bytes"
  4. "context"
  5. "errors"
  6. "fmt"
  7. mrand "math/rand"
  8. abciclient "github.com/tendermint/tendermint/abci/client"
  9. "github.com/tendermint/tendermint/abci/types"
  10. tmrand "github.com/tendermint/tendermint/libs/rand"
  11. )
  12. var ctx = context.Background()
  13. func InitChain(client abciclient.Client) error {
  14. total := 10
  15. vals := make([]types.ValidatorUpdate, total)
  16. for i := 0; i < total; i++ {
  17. pubkey := tmrand.Bytes(33)
  18. // nolint:gosec // G404: Use of weak random number generator
  19. power := mrand.Int()
  20. vals[i] = types.UpdateValidator(pubkey, int64(power), "")
  21. }
  22. _, err := client.InitChainSync(ctx, types.RequestInitChain{
  23. Validators: vals,
  24. })
  25. if err != nil {
  26. fmt.Printf("Failed test: InitChain - %v\n", err)
  27. return err
  28. }
  29. fmt.Println("Passed test: InitChain")
  30. return nil
  31. }
  32. func Commit(client abciclient.Client, hashExp []byte) error {
  33. res, err := client.CommitSync(ctx)
  34. data := res.Data
  35. if err != nil {
  36. fmt.Println("Failed test: Commit")
  37. fmt.Printf("error while committing: %v\n", err)
  38. return err
  39. }
  40. if !bytes.Equal(data, hashExp) {
  41. fmt.Println("Failed test: Commit")
  42. fmt.Printf("Commit hash was unexpected. Got %X expected %X\n", data, hashExp)
  43. return errors.New("commitTx failed")
  44. }
  45. fmt.Println("Passed test: Commit")
  46. return nil
  47. }
  48. func DeliverTx(client abciclient.Client, txBytes []byte, codeExp uint32, dataExp []byte) error {
  49. res, _ := client.DeliverTxSync(ctx, types.RequestDeliverTx{Tx: txBytes})
  50. code, data, log := res.Code, res.Data, res.Log
  51. if code != codeExp {
  52. fmt.Println("Failed test: DeliverTx")
  53. fmt.Printf("DeliverTx response code was unexpected. Got %v expected %v. Log: %v\n",
  54. code, codeExp, log)
  55. return errors.New("deliverTx error")
  56. }
  57. if !bytes.Equal(data, dataExp) {
  58. fmt.Println("Failed test: DeliverTx")
  59. fmt.Printf("DeliverTx response data was unexpected. Got %X expected %X\n",
  60. data, dataExp)
  61. return errors.New("deliverTx error")
  62. }
  63. fmt.Println("Passed test: DeliverTx")
  64. return nil
  65. }
  66. func CheckTx(client abciclient.Client, txBytes []byte, codeExp uint32, dataExp []byte) error {
  67. res, _ := client.CheckTxSync(ctx, types.RequestCheckTx{Tx: txBytes})
  68. code, data, log := res.Code, res.Data, res.Log
  69. if code != codeExp {
  70. fmt.Println("Failed test: CheckTx")
  71. fmt.Printf("CheckTx response code was unexpected. Got %v expected %v. Log: %v\n",
  72. code, codeExp, log)
  73. return errors.New("checkTx")
  74. }
  75. if !bytes.Equal(data, dataExp) {
  76. fmt.Println("Failed test: CheckTx")
  77. fmt.Printf("CheckTx response data was unexpected. Got %X expected %X\n",
  78. data, dataExp)
  79. return errors.New("checkTx")
  80. }
  81. fmt.Println("Passed test: CheckTx")
  82. return nil
  83. }