github.com/vipernet-xyz/tm@v0.34.24/abci/tests/server/client.go (about)

     1  package testsuite
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"fmt"
     7  
     8  	abcicli "github.com/vipernet-xyz/tm/abci/client"
     9  	"github.com/vipernet-xyz/tm/abci/types"
    10  	tmrand "github.com/vipernet-xyz/tm/libs/rand"
    11  )
    12  
    13  func InitChain(client abcicli.Client) error {
    14  	total := 10
    15  	vals := make([]types.ValidatorUpdate, total)
    16  	for i := 0; i < total; i++ {
    17  		pubkey := tmrand.Bytes(33)
    18  		power := tmrand.Int()
    19  		vals[i] = types.UpdateValidator(pubkey, int64(power), "")
    20  	}
    21  	_, err := client.InitChainSync(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  
    32  func SetOption(client abcicli.Client, key, value string) error {
    33  	_, err := client.SetOptionSync(types.RequestSetOption{Key: key, Value: value})
    34  	if err != nil {
    35  		fmt.Println("Failed test: SetOption")
    36  		fmt.Printf("error while setting %v=%v: \nerror: %v\n", key, value, err)
    37  		return err
    38  	}
    39  	fmt.Println("Passed test: SetOption")
    40  	return nil
    41  }
    42  
    43  func Commit(client abcicli.Client, hashExp []byte) error {
    44  	res, err := client.CommitSync()
    45  	data := res.Data
    46  	if err != nil {
    47  		fmt.Println("Failed test: Commit")
    48  		fmt.Printf("error while committing: %v\n", err)
    49  		return err
    50  	}
    51  	if !bytes.Equal(data, hashExp) {
    52  		fmt.Println("Failed test: Commit")
    53  		fmt.Printf("Commit hash was unexpected. Got %X expected %X\n", data, hashExp)
    54  		return errors.New("commitTx failed")
    55  	}
    56  	fmt.Println("Passed test: Commit")
    57  	return nil
    58  }
    59  
    60  func DeliverTx(client abcicli.Client, txBytes []byte, codeExp uint32, dataExp []byte) error {
    61  	res, _ := client.DeliverTxSync(types.RequestDeliverTx{Tx: txBytes})
    62  	code, data, log := res.Code, res.Data, res.Log
    63  	if code != codeExp {
    64  		fmt.Println("Failed test: DeliverTx")
    65  		fmt.Printf("DeliverTx response code was unexpected. Got %v expected %v. Log: %v\n",
    66  			code, codeExp, log)
    67  		return errors.New("deliverTx error")
    68  	}
    69  	if !bytes.Equal(data, dataExp) {
    70  		fmt.Println("Failed test: DeliverTx")
    71  		fmt.Printf("DeliverTx response data was unexpected. Got %X expected %X\n",
    72  			data, dataExp)
    73  		return errors.New("deliverTx error")
    74  	}
    75  	fmt.Println("Passed test: DeliverTx")
    76  	return nil
    77  }
    78  
    79  func CheckTx(client abcicli.Client, txBytes []byte, codeExp uint32, dataExp []byte) error {
    80  	res, _ := client.CheckTxSync(types.RequestCheckTx{Tx: txBytes})
    81  	code, data, log := res.Code, res.Data, res.Log
    82  	if code != codeExp {
    83  		fmt.Println("Failed test: CheckTx")
    84  		fmt.Printf("CheckTx response code was unexpected. Got %v expected %v. Log: %v\n",
    85  			code, codeExp, log)
    86  		return errors.New("checkTx")
    87  	}
    88  	if !bytes.Equal(data, dataExp) {
    89  		fmt.Println("Failed test: CheckTx")
    90  		fmt.Printf("CheckTx response data was unexpected. Got %X expected %X\n",
    91  			data, dataExp)
    92  		return errors.New("checkTx")
    93  	}
    94  	fmt.Println("Passed test: CheckTx")
    95  	return nil
    96  }