Skip to content

Commit

Permalink
Limit context background (#8093)
Browse files Browse the repository at this point in the history
* limit context background

* update

* fixed minor issues

* fixed TestStatusCommand

* Fix keyring import from older versions. (#8436)

Co-authored-by: Alessio Treglia <[email protected]>
Co-authored-by: sahith-narahari <[email protected]>

* Add changelog (#8490)

* fix: tendermint subcommands should not create missing files (#8481)

If the user specifies an incorrect `--home`, then the old behaviour
would automatically populate it with fresh values, but we should
fail instead.

* limit context background

* update

* fixed minor issues

* fixed TestStatusCommand

* statik.go

* replaced static.go

Co-authored-by: Jonathan Gimeno <[email protected]>
Co-authored-by: Alessio Treglia <[email protected]>
Co-authored-by: sahith-narahari <[email protected]>
Co-authored-by: Michael FIG <[email protected]>
  • Loading branch information
5 people authored Feb 3, 2021
1 parent ce161c4 commit d37c590
Show file tree
Hide file tree
Showing 21 changed files with 96 additions and 107 deletions.
3 changes: 1 addition & 2 deletions client/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ func TestSetCmdClientContextHandler(t *testing.T) {
tc := tc

t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &client.Context{})
ctx := context.WithValue(context.Background(), client.ClientContextKey, &client.Context{})

cmd := newCmd()
_ = testutil.ApplyMockIODiscardOutErr(cmd)
Expand Down
4 changes: 2 additions & 2 deletions client/grpc/tmservice/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"github.com/cosmos/cosmos-sdk/client"
)

func getBlock(clientCtx client.Context, height *int64) (*ctypes.ResultBlock, error) {
func getBlock(ctx context.Context, clientCtx client.Context, height *int64) (*ctypes.ResultBlock, error) {
// get the node
node, err := clientCtx.GetNode()
if err != nil {
return nil, err
}

return node.Block(context.Background(), height)
return node.Block(ctx, height)
}
18 changes: 9 additions & 9 deletions client/grpc/tmservice/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func NewQueryServer(clientCtx client.Context, interfaceRegistry codectypes.Inter
}

// GetSyncing implements ServiceServer.GetSyncing
func (s queryServer) GetSyncing(_ context.Context, _ *GetSyncingRequest) (*GetSyncingResponse, error) {
status, err := getNodeStatus(s.clientCtx)
func (s queryServer) GetSyncing(ctx context.Context, _ *GetSyncingRequest) (*GetSyncingResponse, error) {
status, err := getNodeStatus(ctx, s.clientCtx)
if err != nil {
return nil, err
}
Expand All @@ -45,8 +45,8 @@ func (s queryServer) GetSyncing(_ context.Context, _ *GetSyncingRequest) (*GetSy
}

// GetLatestBlock implements ServiceServer.GetLatestBlock
func (s queryServer) GetLatestBlock(context.Context, *GetLatestBlockRequest) (*GetLatestBlockResponse, error) {
status, err := getBlock(s.clientCtx, nil)
func (s queryServer) GetLatestBlock(ctx context.Context, _ *GetLatestBlockRequest) (*GetLatestBlockResponse, error) {
status, err := getBlock(ctx, s.clientCtx, nil)
if err != nil {
return nil, err
}
Expand All @@ -64,7 +64,7 @@ func (s queryServer) GetLatestBlock(context.Context, *GetLatestBlockRequest) (*G
}

// GetBlockByHeight implements ServiceServer.GetBlockByHeight
func (s queryServer) GetBlockByHeight(_ context.Context, req *GetBlockByHeightRequest) (*GetBlockByHeightResponse, error) {
func (s queryServer) GetBlockByHeight(ctx context.Context, req *GetBlockByHeightRequest) (*GetBlockByHeightResponse, error) {
chainHeight, err := rpc.GetChainHeight(s.clientCtx)
if err != nil {
return nil, err
Expand All @@ -74,7 +74,7 @@ func (s queryServer) GetBlockByHeight(_ context.Context, req *GetBlockByHeightRe
return nil, status.Error(codes.InvalidArgument, "requested block height is bigger then the chain length")
}

res, err := getBlock(s.clientCtx, &req.Height)
res, err := getBlock(ctx, s.clientCtx, &req.Height)
if err != nil {
return nil, err
}
Expand All @@ -96,7 +96,7 @@ func (s queryServer) GetLatestValidatorSet(ctx context.Context, req *GetLatestVa
return nil, err
}

validatorsRes, err := rpc.GetValidators(s.clientCtx, nil, &page, &limit)
validatorsRes, err := rpc.GetValidators(ctx, s.clientCtx, nil, &page, &limit)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -147,7 +147,7 @@ func (s queryServer) GetValidatorSetByHeight(ctx context.Context, req *GetValida
return nil, status.Error(codes.InvalidArgument, "requested block height is bigger then the chain length")
}

validatorsRes, err := rpc.GetValidators(s.clientCtx, &req.Height, &page, &limit)
validatorsRes, err := rpc.GetValidators(ctx, s.clientCtx, &req.Height, &page, &limit)

if err != nil {
return nil, err
Expand Down Expand Up @@ -175,7 +175,7 @@ func (s queryServer) GetValidatorSetByHeight(ctx context.Context, req *GetValida

// GetNodeInfo implements ServiceServer.GetNodeInfo
func (s queryServer) GetNodeInfo(ctx context.Context, req *GetNodeInfoRequest) (*GetNodeInfoResponse, error) {
status, err := getNodeStatus(s.clientCtx)
status, err := getNodeStatus(ctx, s.clientCtx)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions client/grpc/tmservice/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"github.com/cosmos/cosmos-sdk/client"
)

func getNodeStatus(clientCtx client.Context) (*ctypes.ResultStatus, error) {
func getNodeStatus(ctx context.Context, clientCtx client.Context) (*ctypes.ResultStatus, error) {
node, err := clientCtx.GetNode()
if err != nil {
return &ctypes.ResultStatus{}, err
}
return node.Status(context.Background())
return node.Status(ctx)
}
3 changes: 1 addition & 2 deletions client/keys/parse.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package keys

import (
"context"
"encoding/hex"
"errors"
"fmt"
Expand Down Expand Up @@ -86,7 +85,7 @@ hexadecimal into bech32 cosmos prefixed format and vice versa.
}

func parseKey(cmd *cobra.Command, args []string) error {
config, _ := sdk.GetSealedConfig(context.Background())
config, _ := sdk.GetSealedConfig(cmd.Context())
return doParseKey(cmd, config, args)
}

Expand Down
10 changes: 5 additions & 5 deletions client/rpc/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func ValidatorCommand() *cobra.Command {
page, _ := cmd.Flags().GetInt(flags.FlagPage)
limit, _ := cmd.Flags().GetInt(flags.FlagLimit)

result, err := GetValidators(clientCtx, height, &page, &limit)
result, err := GetValidators(cmd.Context(), clientCtx, height, &page, &limit)
if err != nil {
return err
}
Expand Down Expand Up @@ -117,14 +117,14 @@ func validatorOutput(validator *tmtypes.Validator) (ValidatorOutput, error) {
}

// GetValidators from client
func GetValidators(clientCtx client.Context, height *int64, page, limit *int) (ResultValidatorsOutput, error) {
func GetValidators(ctx context.Context, clientCtx client.Context, height *int64, page, limit *int) (ResultValidatorsOutput, error) {
// get the node
node, err := clientCtx.GetNode()
if err != nil {
return ResultValidatorsOutput{}, err
}

validatorsRes, err := node.Validators(context.Background(), height, page, limit)
validatorsRes, err := node.Validators(ctx, height, page, limit)
if err != nil {
return ResultValidatorsOutput{}, err
}
Expand Down Expand Up @@ -172,7 +172,7 @@ func ValidatorSetRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
return
}

output, err := GetValidators(clientCtx, &height, &page, &limit)
output, err := GetValidators(r.Context(), clientCtx, &height, &page, &limit)
if rest.CheckInternalServerError(w, err) {
return
}
Expand All @@ -189,7 +189,7 @@ func LatestValidatorSetRequestHandlerFn(clientCtx client.Context) http.HandlerFu
return
}

output, err := GetValidators(clientCtx, nil, &page, &limit)
output, err := GetValidators(r.Context(), clientCtx, nil, &page, &limit)
if rest.CheckInternalServerError(w, err) {
return
}
Expand Down
5 changes: 2 additions & 3 deletions x/auth/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cli

import (
"context"
"fmt"
"strings"

Expand Down Expand Up @@ -58,7 +57,7 @@ $ <appd> query auth params
}

queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.Params(context.Background(), &types.QueryParamsRequest{})
res, err := queryClient.Params(cmd.Context(), &types.QueryParamsRequest{})
if err != nil {
return err
}
Expand Down Expand Up @@ -90,7 +89,7 @@ func GetAccountCmd() *cobra.Command {
}

queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.Account(context.Background(), &types.QueryAccountRequest{Address: key.String()})
res, err := queryClient.Account(cmd.Context(), &types.QueryAccountRequest{Address: key.String()})
if err != nil {
return err
}
Expand Down
13 changes: 6 additions & 7 deletions x/bank/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,18 @@ Example:
if err != nil {
return err
}

ctx := cmd.Context()
if denom == "" {
params := types.NewQueryAllBalancesRequest(addr, pageReq)

res, err := queryClient.AllBalances(cmd.Context(), params)
res, err := queryClient.AllBalances(ctx, params)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
}

params := types.NewQueryBalanceRequest(addr, denom)
res, err := queryClient.Balance(cmd.Context(), params)
res, err := queryClient.Balance(ctx, params)
if err != nil {
return err
}
Expand Down Expand Up @@ -183,17 +182,17 @@ To query for the total supply of a specific coin denomination use:
}

queryClient := types.NewQueryClient(clientCtx)

ctx := cmd.Context()
if denom == "" {
res, err := queryClient.TotalSupply(cmd.Context(), &types.QueryTotalSupplyRequest{})
res, err := queryClient.TotalSupply(ctx, &types.QueryTotalSupplyRequest{})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
}

res, err := queryClient.SupplyOf(cmd.Context(), &types.QuerySupplyOfRequest{Denom: denom})
res, err := queryClient.SupplyOf(ctx, &types.QuerySupplyOfRequest{Denom: denom})
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions x/bank/client/testutil/cli_helpers.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package testutil

import (
"context"
"fmt"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -59,7 +58,7 @@ ignored as it is implied from [from_key_or_address].`,
msg := types.NewMsgSend(clientCtx.GetFromAddress(), toAddr, coins)
svcMsgClientConn := &msgservice.ServiceMsgClientConn{}
bankMsgClient := types.NewMsgClient(svcMsgClientConn)
_, err = bankMsgClient.Send(context.Background(), msg)
_, err = bankMsgClient.Send(cmd.Context(), msg)
if err != nil {
return err
}
Expand Down
16 changes: 8 additions & 8 deletions x/distribution/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cli

import (
"context"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -50,7 +49,7 @@ func GetCmdQueryParams() *cobra.Command {
}
queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.Params(context.Background(), &types.QueryParamsRequest{})
res, err := queryClient.Params(cmd.Context(), &types.QueryParamsRequest{})
if err != nil {
return err
}
Expand Down Expand Up @@ -94,7 +93,7 @@ $ %s query distribution validator-outstanding-rewards %s1lwjmdnks33xwnmfayc64ycp
}

res, err := queryClient.ValidatorOutstandingRewards(
context.Background(),
cmd.Context(),
&types.QueryValidatorOutstandingRewardsRequest{ValidatorAddress: validatorAddr.String()},
)
if err != nil {
Expand Down Expand Up @@ -139,7 +138,7 @@ $ %s query distribution commission %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj
}

res, err := queryClient.ValidatorCommission(
context.Background(),
cmd.Context(),
&types.QueryValidatorCommissionRequest{ValidatorAddress: validatorAddr.String()},
)
if err != nil {
Expand Down Expand Up @@ -199,7 +198,7 @@ $ %s query distribution slashes %svaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj
}

res, err := queryClient.ValidatorSlashes(
context.Background(),
cmd.Context(),
&types.QueryValidatorSlashesRequest{
ValidatorAddress: validatorAddr.String(),
StartingHeight: startHeight,
Expand Down Expand Up @@ -252,14 +251,15 @@ $ %s query distribution rewards %s1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p %s1ggh
}

// query for rewards from a particular delegation
ctx := cmd.Context()
if len(args) == 2 {
validatorAddr, err := sdk.ValAddressFromBech32(args[1])
if err != nil {
return err
}

res, err := queryClient.DelegationRewards(
context.Background(),
ctx,
&types.QueryDelegationRewardsRequest{DelegatorAddress: delegatorAddr.String(), ValidatorAddress: validatorAddr.String()},
)
if err != nil {
Expand All @@ -270,7 +270,7 @@ $ %s query distribution rewards %s1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p %s1ggh
}

res, err := queryClient.DelegationTotalRewards(
context.Background(),
ctx,
&types.QueryDelegationTotalRewardsRequest{DelegatorAddress: delegatorAddr.String()},
)
if err != nil {
Expand Down Expand Up @@ -307,7 +307,7 @@ $ %s query distribution community-pool
}
queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.CommunityPool(context.Background(), &types.QueryCommunityPoolRequest{})
res, err := queryClient.CommunityPool(cmd.Context(), &types.QueryCommunityPoolRequest{})
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions x/distribution/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cli

import (
"context"
"fmt"
"strings"

Expand Down Expand Up @@ -155,7 +154,7 @@ $ %s tx distribution withdraw-all-rewards --from mykey
}

queryClient := types.NewQueryClient(clientCtx)
delValsRes, err := queryClient.DelegatorValidators(context.Background(), &types.QueryDelegatorValidatorsRequest{DelegatorAddress: delAddr.String()})
delValsRes, err := queryClient.DelegatorValidators(cmd.Context(), &types.QueryDelegatorValidatorsRequest{DelegatorAddress: delAddr.String()})
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit d37c590

Please sign in to comment.