github.com/badrootd/nibiru-cometbft@v0.37.5-0.20240307173500-2a75559eee9b/abci/tests/server/client.go (about)

     1  package testsuite
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"fmt"
     7  
     8  	abcicli "github.com/badrootd/nibiru-cometbft/abci/client"
     9  	"github.com/badrootd/nibiru-cometbft/abci/types"
    10  	cmtrand "github.com/badrootd/nibiru-cometbft/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 PrepareProposal(client abcicli.Client, txBytes [][]byte, txExpected [][]byte, dataExp []byte) error {
    69  	res, _ := client.PrepareProposalSync(types.RequestPrepareProposal{Txs: txBytes})
    70  	for i, tx := range res.Txs {
    71  		if !bytes.Equal(tx, txExpected[i]) {
    72  			fmt.Println("Failed test: PrepareProposal")
    73  			fmt.Printf("PrepareProposal transaction was unexpected. Got %x expected %x.",
    74  				tx, txExpected[i])
    75  			return errors.New("PrepareProposal error")
    76  		}
    77  	}
    78  	fmt.Println("Passed test: PrepareProposal")
    79  	return nil
    80  }
    81  
    82  func ProcessProposal(client abcicli.Client, txBytes [][]byte, statusExp types.ResponseProcessProposal_ProposalStatus) error {
    83  	res, _ := client.ProcessProposalSync(types.RequestProcessProposal{Txs: txBytes})
    84  	if res.Status != statusExp {
    85  		fmt.Println("Failed test: ProcessProposal")
    86  		fmt.Printf("ProcessProposal response status was unexpected. Got %v expected %v.",
    87  			res.Status, statusExp)
    88  		return errors.New("ProcessProposal error")
    89  	}
    90  	fmt.Println("Passed test: ProcessProposal")
    91  	return nil
    92  }
    93  
    94  func CheckTx(client abcicli.Client, txBytes []byte, codeExp uint32, dataExp []byte) error {
    95  	res, _ := client.CheckTxSync(types.RequestCheckTx{Tx: txBytes})
    96  	code, data, log := res.Code, res.Data, res.Log
    97  	if code != codeExp {
    98  		fmt.Println("Failed test: CheckTx")
    99  		fmt.Printf("CheckTx response code was unexpected. Got %v expected %v. Log: %v\n",
   100  			code, codeExp, log)
   101  		return errors.New("checkTx")
   102  	}
   103  	if !bytes.Equal(data, dataExp) {
   104  		fmt.Println("Failed test: CheckTx")
   105  		fmt.Printf("CheckTx response data was unexpected. Got %X expected %X\n",
   106  			data, dataExp)
   107  		return errors.New("checkTx")
   108  	}
   109  	fmt.Println("Passed test: CheckTx")
   110  	return nil
   111  }