github.com/cosmos/cosmos-sdk@v0.50.10/client/rpc/rpc_test.go (about)

     1  package rpc_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strconv"
     7  	"testing"
     8  
     9  	abci "github.com/cometbft/cometbft/abci/types"
    10  	"github.com/stretchr/testify/suite"
    11  	"google.golang.org/grpc"
    12  	"google.golang.org/grpc/metadata"
    13  
    14  	"github.com/cosmos/cosmos-sdk/testutil/network"
    15  	"github.com/cosmos/cosmos-sdk/testutil/testdata"
    16  	"github.com/cosmos/cosmos-sdk/types/address"
    17  	grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
    18  	banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
    19  )
    20  
    21  type IntegrationTestSuite struct {
    22  	suite.Suite
    23  
    24  	network *network.Network
    25  }
    26  
    27  func (s *IntegrationTestSuite) SetupSuite() {
    28  	s.T().Log("setting up integration test suite")
    29  
    30  	cfg, err := network.DefaultConfigWithAppConfig(network.MinimumAppConfig())
    31  
    32  	s.NoError(err)
    33  
    34  	s.network, err = network.New(s.T(), s.T().TempDir(), cfg)
    35  	s.Require().NoError(err)
    36  
    37  	s.Require().NoError(s.network.WaitForNextBlock())
    38  }
    39  
    40  func (s *IntegrationTestSuite) TearDownSuite() {
    41  	s.T().Log("tearing down integration test suite")
    42  	s.network.Cleanup()
    43  }
    44  
    45  func (s *IntegrationTestSuite) TestCLIQueryConn() {
    46  	s.T().Skip("data race in comet is causing this to fail")
    47  	var header metadata.MD
    48  
    49  	testClient := testdata.NewQueryClient(s.network.Validators[0].ClientCtx)
    50  	res, err := testClient.Echo(context.Background(), &testdata.EchoRequest{Message: "hello"}, grpc.Header(&header))
    51  	s.NoError(err)
    52  
    53  	blockHeight := header.Get(grpctypes.GRPCBlockHeightHeader)
    54  	height, err := strconv.Atoi(blockHeight[0])
    55  	s.Require().NoError(err)
    56  	s.Require().GreaterOrEqual(height, 1) // at least the 1st block
    57  
    58  	s.Equal("hello", res.Message)
    59  }
    60  
    61  func (s *IntegrationTestSuite) TestQueryABCIHeight() {
    62  	testCases := []struct {
    63  		name      string
    64  		reqHeight int64
    65  		ctxHeight int64
    66  		expHeight int64
    67  	}{
    68  		{
    69  			name:      "non zero request height",
    70  			reqHeight: 3,
    71  			ctxHeight: 1, // query at height 1 or 2 would cause an error
    72  			expHeight: 3,
    73  		},
    74  		{
    75  			name:      "empty request height - use context height",
    76  			reqHeight: 0,
    77  			ctxHeight: 3,
    78  			expHeight: 3,
    79  		},
    80  		{
    81  			name:      "empty request height and context height - use latest height",
    82  			reqHeight: 0,
    83  			ctxHeight: 0,
    84  			expHeight: 4,
    85  		},
    86  	}
    87  
    88  	for _, tc := range testCases {
    89  		s.Run(tc.name, func() {
    90  			s.network.WaitForHeight(tc.expHeight)
    91  
    92  			val := s.network.Validators[0]
    93  
    94  			clientCtx := val.ClientCtx
    95  			clientCtx = clientCtx.WithHeight(tc.ctxHeight)
    96  
    97  			req := abci.RequestQuery{
    98  				Path:   fmt.Sprintf("store/%s/key", banktypes.StoreKey),
    99  				Height: tc.reqHeight,
   100  				Data:   address.MustLengthPrefix(val.Address),
   101  				Prove:  true,
   102  			}
   103  
   104  			res, err := clientCtx.QueryABCI(req)
   105  			s.Require().NoError(err)
   106  
   107  			s.Require().Equal(tc.expHeight, res.Height)
   108  		})
   109  	}
   110  }
   111  
   112  func TestIntegrationTestSuite(t *testing.T) {
   113  	suite.Run(t, new(IntegrationTestSuite))
   114  }