github.com/line/ostracon@v1.0.10-0.20230328032236-7f20145f065d/abci/tests/server/client.go (about)

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