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

6 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.InitChain(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.Commit(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 FinalizeBlock(ctx context.Context, client abciclient.Client, txBytes [][]byte, codeExp []uint32, dataExp []byte) error {
  48. res, _ := client.FinalizeBlock(ctx, types.RequestFinalizeBlock{Txs: txBytes})
  49. for i, tx := range res.Txs {
  50. code, data, log := tx.Code, tx.Data, tx.Log
  51. if code != codeExp[i] {
  52. fmt.Println("Failed test: FinalizeBlock")
  53. fmt.Printf("FinalizeBlock response code was unexpected. Got %v expected %v. Log: %v\n",
  54. code, codeExp, log)
  55. return errors.New("FinalizeBlock error")
  56. }
  57. if !bytes.Equal(data, dataExp) {
  58. fmt.Println("Failed test: FinalizeBlock")
  59. fmt.Printf("FinalizeBlock response data was unexpected. Got %X expected %X\n",
  60. data, dataExp)
  61. return errors.New("FinalizeBlock error")
  62. }
  63. }
  64. fmt.Println("Passed test: FinalizeBlock")
  65. return nil
  66. }
  67. func CheckTx(ctx context.Context, client abciclient.Client, txBytes []byte, codeExp uint32, dataExp []byte) error {
  68. res, _ := client.CheckTx(ctx, types.RequestCheckTx{Tx: txBytes})
  69. code, data, log := res.Code, res.Data, res.Log
  70. if code != codeExp {
  71. fmt.Println("Failed test: CheckTx")
  72. fmt.Printf("CheckTx response code was unexpected. Got %v expected %v. Log: %v\n",
  73. code, codeExp, log)
  74. return errors.New("checkTx")
  75. }
  76. if !bytes.Equal(data, dataExp) {
  77. fmt.Println("Failed test: CheckTx")
  78. fmt.Printf("CheckTx response data was unexpected. Got %X expected %X\n",
  79. data, dataExp)
  80. return errors.New("checkTx")
  81. }
  82. fmt.Println("Passed test: CheckTx")
  83. return nil
  84. }