github.com/badrootd/celestia-core@v0.0.0-20240305091328-aa4207a4b25d/abci/tests/server/client.go (about)

     1  package testsuite
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"fmt"
     7  
     8  	abcicli "github.com/badrootd/celestia-core/abci/client"
     9  	"github.com/badrootd/celestia-core/abci/types"
    10  	cmtrand "github.com/badrootd/celestia-core/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 := cmtrand.Bytes(33)
    18  		power := cmtrand.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 Commit(client abcicli.Client, hashExp []byte) error {
    33  	res, err := client.CommitSync()
    34  	data := res.Data
    35  	if err != nil {
    36  		fmt.Println("Failed test: Commit")
    37  		fmt.Printf("error while committing: %v\n", err)
    38  		return err
    39  	}
    40  	if !bytes.Equal(data, hashExp) {
    41  		fmt.Println("Failed test: Commit")
    42  		fmt.Printf("Commit hash was unexpected. Got %X expected %X\n", data, hashExp)
    43  		return errors.New("commitTx failed")
    44  	}
    45  	fmt.Println("Passed test: Commit")
    46  	return nil
    47  }
    48  
    49  func DeliverTx(client abcicli.Client, txBytes []byte, codeExp uint32, dataExp []byte) error {
    50  	res, _ := client.DeliverTxSync(types.RequestDeliverTx{Tx: txBytes})
    51  	code, data, log := res.Code, res.Data, res.Log
    52  	if code != codeExp {
    53  		fmt.Println("Failed test: DeliverTx")
    54  		fmt.Printf("DeliverTx response code was unexpected. Got %v expected %v. Log: %v\n",
    55  			code, codeExp, log)
    56  		return errors.New("deliverTx error")
    57  	}
    58  	if !bytes.Equal(data, dataExp) {
    59  		fmt.Println("Failed test: DeliverTx")
    60  		fmt.Printf("DeliverTx response data was unexpected. Got %X expected %X\n",
    61  			data, dataExp)
    62  		return errors.New("deliverTx error")
    63  	}
    64  	fmt.Println("Passed test: DeliverTx")
    65  	return nil
    66  }
    67  
    68  func CheckTx(client abcicli.Client, txBytes []byte, codeExp uint32, dataExp []byte) error {
    69  	res, _ := client.CheckTxSync(types.RequestCheckTx{Tx: txBytes})
    70  	code, data, log := res.Code, res.Data, res.Log
    71  	if code != codeExp {
    72  		fmt.Println("Failed test: CheckTx")
    73  		fmt.Printf("CheckTx response code was unexpected. Got %v expected %v. Log: %v\n",
    74  			code, codeExp, log)
    75  		return errors.New("checkTx")
    76  	}
    77  	if !bytes.Equal(data, dataExp) {
    78  		fmt.Println("Failed test: CheckTx")
    79  		fmt.Printf("CheckTx response data was unexpected. Got %X expected %X\n",
    80  			data, dataExp)
    81  		return errors.New("checkTx")
    82  	}
    83  	fmt.Println("Passed test: CheckTx")
    84  	return nil
    85  }