github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/upgrade/client/rest/query.go (about)

     1  package rest
     2  
     3  import (
     4  	"encoding/binary"
     5  	"fmt"
     6  	"net/http"
     7  
     8  	"github.com/gorilla/mux"
     9  
    10  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
    11  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/rest"
    12  	upgrade "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/upgrade/internal/types"
    13  )
    14  
    15  // RegisterRoutes registers REST routes for the upgrade module under the path specified by routeName.
    16  func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) {
    17  	r.HandleFunc("/upgrade/current", getCurrentPlanHandler(cliCtx)).Methods("GET")
    18  	r.HandleFunc("/upgrade/applied/{name}", getDonePlanHandler(cliCtx)).Methods("GET")
    19  	registerTxRoutes(cliCtx, r)
    20  }
    21  
    22  func getCurrentPlanHandler(cliCtx context.CLIContext) func(http.ResponseWriter, *http.Request) {
    23  	return func(w http.ResponseWriter, request *http.Request) {
    24  		// ignore height for now
    25  		res, _, err := cliCtx.Query(fmt.Sprintf("custom/%s/%s", upgrade.QuerierKey, upgrade.QueryCurrent))
    26  		if err != nil {
    27  			rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
    28  			return
    29  		}
    30  		if len(res) == 0 {
    31  			http.NotFound(w, request)
    32  			return
    33  		}
    34  
    35  		var plan upgrade.Plan
    36  		err = cliCtx.Codec.UnmarshalBinaryBare(res, &plan)
    37  		if err != nil {
    38  			rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
    39  			return
    40  		}
    41  
    42  		rest.PostProcessResponse(w, cliCtx, plan)
    43  	}
    44  }
    45  
    46  func getDonePlanHandler(cliCtx context.CLIContext) func(http.ResponseWriter, *http.Request) {
    47  	return func(w http.ResponseWriter, r *http.Request) {
    48  		name := mux.Vars(r)["name"]
    49  
    50  		params := upgrade.NewQueryAppliedParams(name)
    51  		bz, err := cliCtx.Codec.MarshalJSON(params)
    52  		if err != nil {
    53  			rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
    54  			return
    55  		}
    56  
    57  		res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", upgrade.QuerierKey, upgrade.QueryApplied), bz)
    58  		if err != nil {
    59  			rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
    60  			return
    61  		}
    62  
    63  		if len(res) == 0 {
    64  			http.NotFound(w, r)
    65  			return
    66  		}
    67  		if len(res) != 8 {
    68  			rest.WriteErrorResponse(w, http.StatusInternalServerError, "unknown format for applied-upgrade")
    69  		}
    70  
    71  		applied := int64(binary.BigEndian.Uint64(res))
    72  		fmt.Println(applied)
    73  		rest.PostProcessResponse(w, cliCtx, applied)
    74  	}
    75  }