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.5 KiB

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