Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/QB-1334: Remove unnecessary stores & code optimize #176

Merged
merged 1 commit into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions proto/stratos/sds/v1/query.proto
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
syntax = "proto3";
package stratos.sds.v1;

import "gogoproto/gogo.proto";

import "google/api/annotations.proto";

import "stratos/sds/v1/sds.proto";

option go_package = "github.com/stratosnet/stratos-chain/x/sds/types";
Expand All @@ -15,10 +12,6 @@ service Query {
rpc Fileupload(QueryFileUploadRequest) returns (QueryFileUploadResponse) {
option (google.api.http).get = "/stratos/sds/v1/file_upload/{file_hash}";
}
// Query balance of prepayment in Volume Pool
rpc Prepay(QueryPrepayRequest) returns (QueryPrepayResponse) {
option (google.api.http).get = "/stratos/sds/v1/prepay/{acct_addr}";
}

// Params queries SDS module Params info.
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
Expand All @@ -38,18 +31,6 @@ message QueryFileUploadResponse {
FileInfo file_info = 1;
}

// QueryPrepayRequest is request type for the Query/Prepay RPC method
message QueryPrepayRequest {
string acct_addr = 1;
}

// QueryPrepayResponse is response type for the Query/Prepay RPC method
message QueryPrepayResponse {
string balance = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int"
];
}

// QueryParamsRequest is request type for the Query/Params RPC method.
message QueryParamsRequest {}

Expand Down
1 change: 0 additions & 1 deletion x/pot/keeper/distribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ func (k Keeper) saveRewardInfo(ctx sdk.Context, rewardDetailList []types.Reward,
oldTotalMinedToken := k.GetTotalMinedTokens(ctx)
newTotalMinedToken := oldTotalMinedToken.Add(newMinedTotal)
k.SetTotalMinedTokens(ctx, newTotalMinedToken)
k.setMinedTokens(ctx, currentEpoch, newMinedTotal)
return nil
}

Expand Down
16 changes: 0 additions & 16 deletions x/pot/keeper/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,6 @@ func (k Keeper) GetTotalMinedTokens(ctx sdk.Context) (totalMinedToken sdk.Coin)
return
}

func (k Keeper) setMinedTokens(ctx sdk.Context, epoch sdk.Int, minedToken sdk.Coin) {
store := ctx.KVStore(k.storeKey)
b := types.ModuleCdc.MustMarshalLengthPrefixed(minedToken)
store.Set(types.GetMinedTokensKey(epoch), b)
}

func (k Keeper) GetMinedTokens(ctx sdk.Context, epoch sdk.Int) (minedToken sdk.Coin) {
store := ctx.KVStore(k.storeKey)
b := store.Get(types.GetMinedTokensKey(epoch))
if b == nil {
return sdk.NewCoin(k.RewardDenom(ctx), sdk.ZeroInt())
}
types.ModuleCdc.MustUnmarshalLengthPrefixed(b, &minedToken)
return
}

func (k Keeper) SetLastReportedEpoch(ctx sdk.Context, epoch sdk.Int) {
store := ctx.KVStore(k.storeKey)
b := types.ModuleCdc.MustMarshalLengthPrefixed(epoch)
Expand Down
25 changes: 9 additions & 16 deletions x/pot/types/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,18 @@ const (
)

var (
TotalMinedTokensKey = []byte{0x03}
MinedTokensKeyPrefix = []byte{0x04} // key: prefix_epoch
TotalMinedTokensKey = []byte{0x01}
LastReportedEpochKey = []byte{0x02}
IndividualRewardKeyPrefix = []byte{0x03} // key: prefix{address}_{epoch}, the amount that is matured at {epoch}
MatureTotalRewardKeyPrefix = []byte{0x04} // key: prefix{address}
ImmatureTotalRewardKeyPrefix = []byte{0x05} // key: prefix{address}
VolumeReportStoreKeyPrefix = []byte{0x06} // VolumeReportStoreKeyPrefix prefix for volumeReport store

LastReportedEpochKey = []byte{0x12}
IndividualRewardKeyPrefix = []byte{0x13} // key: prefix{address}_{epoch}, the amount that is matured at {epoch}
MatureTotalRewardKeyPrefix = []byte{0x14} // key: prefix{address}
ImmatureTotalRewardKeyPrefix = []byte{0x15} // key: prefix{address}

VolumeReportStoreKeyPrefix = []byte{0x41} // VolumeReportStoreKeyPrefix prefix for volumeReport store
UnhandledReportKeyPrefix = []byte{0x42} // prefix for report need to be handled, after reward distribution, clear data
UnhandledEpochKey = []byte{0x43} // prefix for epoch need to be handled, after reward distribution, clear data
IsReadyToDistributeReward = []byte{0x44}
UnhandledReportKeyPrefix = []byte{0x11} // prefix for report need to be handled, after reward distribution, clear data
UnhandledEpochKey = []byte{0x12} // prefix for epoch need to be handled, after reward distribution, clear data
IsReadyToDistributeReward = []byte{0x13}
)

func GetMinedTokensKey(epoch sdk.Int) []byte {
bEpoch := []byte(epoch.String())
return append(MinedTokensKeyPrefix, bEpoch...)
}

func VolumeReportStoreKey(epoch sdk.Int) []byte {
return append(VolumeReportStoreKeyPrefix, epoch.String()...)
}
Expand Down
55 changes: 0 additions & 55 deletions x/sds/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/version"
"github.com/spf13/cobra"
Expand All @@ -26,7 +25,6 @@ func GetQueryCmd() *cobra.Command {

sdsQueryCmd.AddCommand(
GetCmdQueryUploadedFile(),
GetCmdQueryPrepayBalance(),
GetCmdQueryParams(),
)

Expand Down Expand Up @@ -97,14 +95,6 @@ $ %s query sds upload c03661732294feb49caf6dc16c7cbb2534986d73
return sdkerrors.Wrap(types.ErrEmptyFileHash, "Missing file hash")
}

// query file by fileHash
//resp, _, err := common.QueryUploadedFile(clientCtx, queryRoute, args[0])
//if err != nil {
// return err
//}
//fi := types.MustUnmarshalFileInfo(cdc, resp)
//return cliCtx.PrintOutput(fi.String())

result, err := queryClient.Fileupload(cmd.Context(), &types.QueryFileUploadRequest{
FileHash: queryFileHash,
})
Expand All @@ -120,48 +110,3 @@ $ %s query sds upload c03661732294feb49caf6dc16c7cbb2534986d73
flags.AddPaginationFlagsToCmd(cmd, "upload")
return cmd
}

// GetCmdQueryPrepayBalance implements the query prepay balance command.
func GetCmdQueryPrepayBalance() *cobra.Command {
cmd := &cobra.Command{
Use: "prepay [acct_addr]",
Args: cobra.ExactArgs(1),
Short: "Query balance of prepayment in Volume Pool",
Long: strings.TrimSpace(
fmt.Sprintf(`Query balance of prepayment in Volumn Pool.

Example:
$ %s query sds prepay st1yx3kkx9jnqeck59j744nc5qgtv4lt4dc45jcwz
`,
version.AppName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

queryAccAddr := strings.TrimSpace(args[0][:])
if len(queryAccAddr) == 0 {
return sdkerrors.Wrap(types.ErrEmptySenderAddr, "Missing sender address")
}
_, err = sdk.AccAddressFromBech32(queryAccAddr)
if err != nil {
return err
}

result, err := queryClient.Prepay(cmd.Context(), &types.QueryPrepayRequest{
AcctAddr: queryAccAddr,
})
if err != nil {
return err
}

return clientCtx.PrintProto(result)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
23 changes: 1 addition & 22 deletions x/sds/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ func FileUploadTxCmd() *cobra.Command {
return tx.GenerateOrBroadcastTxWithFactory(clientCtx, txf, msg)
},
}
//cmd = flags.PostCommands(cmd)[0]
//cmd.Flags().String(flags.FlagFrom, "", "from address")
//cmd.Flags().String(FlagFileHash, "", "Hash of uploaded file")
//cmd.Flags().String(FlagReporter, "", "Reporter of file")
//cmd.Flags().String(FlagUploader, "", "Uploader of file")

cmd.Flags().AddFlagSet(flagSetFileHash())
cmd.Flags().AddFlagSet(flagSetReporter())
cmd.Flags().AddFlagSet(flagSetUploader())
Expand Down Expand Up @@ -100,26 +96,9 @@ func PrepayTxCmd() *cobra.Command {
}

return tx.GenerateOrBroadcastTxWithFactory(clientCtx, txf, msg)
//
//
//
//inBuf := bufio.NewReader(cmd.InOrStdin())
//txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
//cliCtx := context.NewCLIContextWithInputAndFrom(inBuf, args[0]).WithCodec(cdc)
//
//coins, err := sdk.ParseCoins(args[1])
//if err != nil {
// return err
//}
//
//// build and sign the transaction, then broadcast to Tendermint
//msg := types.NewMsgPrepay(cliCtx.GetFromAddress(), coins)
//return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
flags.AddTxFlagsToCmd(cmd)
//cmd = flags.PostCommands(cmd)[0]

return cmd
}

Expand Down
20 changes: 0 additions & 20 deletions x/sds/client/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,6 @@ import (
sdstypes "github.com/stratosnet/stratos-chain/x/sds/types"
)

// QueryUploadedFile queries the hash of an uploaded file by sender
//func QueryUploadedFile(clientCtx client.Context, queryRoute, fileHashHex string) ([]byte, int64, error) {
// fileHashByteArr, err := hex.DecodeString(fileHashHex)
// if err != nil {
// return nil, 0, fmt.Errorf("invalid file hash, please specify a hash in hex format %w", err)
// }
// route := fmt.Sprintf("custom/%s/%s", queryRoute, sds.QueryUploadedFile)
// return clientCtx.QueryWithData(route, fileHashByteArr)
//}

// QueryPrepayBalance queries the prepaid balance by sender in VolumnPool
//func QueryPrepayBalance(clientCtx client.Context, queryRoute, sender string) ([]byte, int64, error) {
// accAddr, err := sdk.AccAddressFromBech32(sender)
// if err != nil {
// return nil, 0, fmt.Errorf("invalid sender, please specify a sender in Bech32 format %w", err)
// }
// route := fmt.Sprintf("custom/%s/%s", queryRoute, sds.QueryPrepay)
// return clientCtx.QueryWithData(route, accAddr)
//}

// QuerySimulatePrepay queries the ongoing price for prepay
func QuerySimulatePrepay(clientCtx client.Context, amtToPrepay sdk.Int) ([]byte, int64, error) {
amtByteArray, err := amtToPrepay.MarshalJSON()
Expand Down
7 changes: 3 additions & 4 deletions x/sds/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import (
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/gorilla/mux"
"github.com/stratosnet/stratos-chain/x/sds/client/common"
sdskeeper "github.com/stratosnet/stratos-chain/x/sds/keeper"
sdstypes "github.com/stratosnet/stratos-chain/x/sds/types"
"github.com/stratosnet/stratos-chain/x/sds/types"
)

func sdsQueryRoutes(clientCtx client.Context, r *mux.Router) {
Expand All @@ -29,7 +28,7 @@ func sdsQueryRoutes(clientCtx client.Context, r *mux.Router) {
UozSupplyHandlerFn(clientCtx),
).Methods("GET")
r.HandleFunc("/sds/params",
sdsParamsHandlerFn(clientCtx, sdskeeper.QuerySdsParams),
sdsParamsHandlerFn(clientCtx, types.QueryParams),
).Methods("GET")

}
Expand All @@ -43,7 +42,7 @@ func sdsParamsHandlerFn(clientCtx client.Context, queryPath string) http.Handler
return
}

route := fmt.Sprintf("custom/%s/%s", sdstypes.QuerierRoute, queryPath)
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, queryPath)
res, height, err := cliCtx.Query(route)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
Expand Down
78 changes: 0 additions & 78 deletions x/sds/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ func RegisterHandlers(clientCtx client.Context, rtr *mux.Router) {
r := rest.WithHTTPDeprecationHeaders(rtr)
sdsQueryRoutes(clientCtx, r)
sdsTxHandlers(clientCtx, r)

//// RegisterRoutes registers sds-related REST handlers to a router
//func RegisterRoutes(clientCtx client.Context, r *mux.Router, queryRoute string) {
// r.HandleFunc("/sds/file/upload", FileUploadRequestHandlerFn(cliCtx)).Methods("POST")
// r.HandleFunc("/sds/prepay", PrepayRequestHandlerFn(cliCtx)).Methods("POST")
// registerSdsQueryRoutes(cliCtx, r, queryRoute)
}

// FileUploadReq defines the properties of a file upload request's body.
Expand All @@ -34,75 +28,3 @@ type PrepayReq struct {
BaseReq sdktrest.BaseReq `json:"base_req" yaml:"base_req"`
Amount sdk.Coins `json:"amount" yaml:"amount"`
}

//// FileUploadRequestHandlerFn - http request handler for file uploading.
//func FileUploadRequestHandlerFn(clientCtx client.Context, queryPath string) http.HandlerFunc {
// return func(w http.ResponseWriter, r *http.Request) {
// cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, clientCtx, r)
// if !ok {
// return
// }
//
// var req FileUploadReq
// if !rest.ReadRESTReq(w, r, clientCtx.Codec, &req) {
// return
// }
//
// req.BaseReq = req.BaseReq.Sanitize()
// if !req.BaseReq.ValidateBasic(w) {
// return
// }
//
// fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
// if err != nil {
// rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
// return
// }
//
// reporter, err := stratos.SdsAddressFromBech32(req.Reporter)
// if err != nil {
// rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
// return
// }
//
// fileHash := req.FileHash
// _, err = hex.DecodeString(fileHash)
// if err != nil {
// rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
// return
// }
//
// uploader, err := sdk.AccAddressFromBech32(req.Uploader)
// if err != nil {
// rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
// return
// }
//
// msg := types.NewMsgUpload(fileHash, fromAddr, reporter, uploader)
// utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg})
// }
//}
//
//// PrepayRequestHandlerFn - http request handler for prepay.
//func PrepayRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
// return func(w http.ResponseWriter, r *http.Request) {
// var req PrepayReq
// if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) {
// return
// }
//
// req.BaseReq = req.BaseReq.Sanitize()
// if !req.BaseReq.ValidateBasic(w) {
// return
// }
//
// fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
// if err != nil {
// rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
// return
// }
//
// msg := types.NewMsgPrepay(fromAddr, req.Amount)
// utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg})
// }
//}
Loading