github.com/lino-network/lino@v0.6.11/x/validator/querier.go (about)

     1  package validator
     2  
     3  import (
     4  	wire "github.com/cosmos/cosmos-sdk/codec"
     5  	sdk "github.com/cosmos/cosmos-sdk/types"
     6  	abci "github.com/tendermint/tendermint/abci/types"
     7  
     8  	linotypes "github.com/lino-network/lino/types"
     9  	"github.com/lino-network/lino/x/validator/types"
    10  )
    11  
    12  const (
    13  	// ModuleKey is the name of the module
    14  	ModuleName = "validator"
    15  
    16  	// RouterKey is the message route for gov
    17  	RouterKey = ModuleName
    18  
    19  	// QuerierRoute is the querier route for gov
    20  	QuerierRoute = ModuleName
    21  
    22  	QueryValidator        = "validator"
    23  	QueryValidatorList    = "valList"
    24  	QueryElectionVoteList = "electionVoteList"
    25  )
    26  
    27  // creates a querier for validator REST endpoints
    28  func NewQuerier(vm ValidatorKeeper) sdk.Querier {
    29  	cdc := wire.New()
    30  	wire.RegisterCrypto(cdc)
    31  	return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err sdk.Error) {
    32  		switch path[0] {
    33  		case QueryValidator:
    34  			return queryValidator(ctx, cdc, path[1:], req, vm)
    35  		case QueryValidatorList:
    36  			return queryValidatorList(ctx, cdc, path[1:], req, vm)
    37  		case QueryElectionVoteList:
    38  			return queryElectionVoteList(ctx, cdc, path[1:], req, vm)
    39  		default:
    40  			return nil, sdk.ErrUnknownRequest("unknown validator query endpoint")
    41  		}
    42  	}
    43  }
    44  
    45  func queryValidator(ctx sdk.Context, cdc *wire.Codec, path []string, req abci.RequestQuery, vm ValidatorKeeper) ([]byte, sdk.Error) {
    46  	if err := linotypes.CheckPathContentAndMinLength(path, 1); err != nil {
    47  		return nil, err
    48  	}
    49  	validator, err := vm.GetValidator(ctx, linotypes.AccountKey(path[0]))
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  	res, marshalErr := cdc.MarshalJSON(validator)
    54  	if marshalErr != nil {
    55  		return nil, types.ErrQueryFailed()
    56  	}
    57  	return res, nil
    58  }
    59  
    60  func queryValidatorList(ctx sdk.Context, cdc *wire.Codec, path []string, req abci.RequestQuery, vm ValidatorKeeper) ([]byte, sdk.Error) {
    61  	validatorList := vm.GetValidatorList(ctx)
    62  	res, marshalErr := cdc.MarshalJSON(validatorList)
    63  	if marshalErr != nil {
    64  		return nil, types.ErrQueryFailed()
    65  	}
    66  	return res, nil
    67  }
    68  
    69  func queryElectionVoteList(ctx sdk.Context, cdc *wire.Codec, path []string, req abci.RequestQuery, vm ValidatorKeeper) ([]byte, sdk.Error) {
    70  	if err := linotypes.CheckPathContentAndMinLength(path, 1); err != nil {
    71  		return nil, err
    72  	}
    73  	lst := vm.GetElectionVoteList(ctx, linotypes.AccountKey(path[0]))
    74  	res, marshalErr := cdc.MarshalJSON(lst)
    75  	if marshalErr != nil {
    76  		return nil, types.ErrQueryFailed()
    77  	}
    78  	return res, nil
    79  }