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.

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