github.com/ari-anchor/sei-tendermint@v0.0.0-20230519144642-dc826b7b56bb/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  	abciclient "github.com/ari-anchor/sei-tendermint/abci/client"
    11  	"github.com/ari-anchor/sei-tendermint/abci/types"
    12  	tmrand "github.com/ari-anchor/sei-tendermint/libs/rand"
    13  )
    14  
    15  func InitChain(ctx context.Context, client abciclient.Client) error {
    16  	total := 10
    17  	vals := make([]types.ValidatorUpdate, total)
    18  	for i := 0; i < total; i++ {
    19  		pubkey := tmrand.Bytes(33)
    20  		// nolint:gosec // G404: Use of weak random number generator
    21  		power := mrand.Int()
    22  		vals[i] = types.UpdateValidator(pubkey, int64(power), "")
    23  	}
    24  	_, err := client.InitChain(ctx, &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 Commit(ctx context.Context, client abciclient.Client) error {
    36  	_, err := client.Commit(ctx)
    37  	if err != nil {
    38  		fmt.Println("Failed test: Commit")
    39  		fmt.Printf("error while committing: %v\n", err)
    40  		return err
    41  	}
    42  	fmt.Println("Passed test: Commit")
    43  	return nil
    44  }
    45  
    46  func FinalizeBlock(ctx context.Context, client abciclient.Client, txBytes [][]byte, codeExp []uint32, dataExp []byte, hashExp []byte) error {
    47  	res, _ := client.FinalizeBlock(ctx, &types.RequestFinalizeBlock{Txs: txBytes})
    48  	appHash := res.AppHash
    49  	for i, tx := range res.TxResults {
    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  	if !bytes.Equal(appHash, hashExp) {
    65  		fmt.Println("Failed test: FinalizeBlock")
    66  		fmt.Printf("Application hash was unexpected. Got %X expected %X\n", appHash, hashExp)
    67  		return errors.New("FinalizeBlock  error")
    68  	}
    69  	fmt.Println("Passed test: FinalizeBlock")
    70  	return nil
    71  }
    72  
    73  func CheckTx(ctx context.Context, client abciclient.Client, txBytes []byte, codeExp uint32, dataExp []byte) error {
    74  	res, _ := client.CheckTx(ctx, &types.RequestCheckTx{Tx: txBytes})
    75  	code, data := res.Code, res.Data
    76  	if code != codeExp {
    77  		fmt.Println("Failed test: CheckTx")
    78  		fmt.Printf("CheckTx response code was unexpected. Got %v expected %v.,",
    79  			code, codeExp)
    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  }