github.com/number571/tendermint@v0.34.11-gost/abci/tests/server/client.go (about)

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