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

update: issues of cross stake and recon service #304

Merged
merged 8 commits into from
Nov 15, 2022
1 change: 1 addition & 0 deletions cmd/gaia/cmd/gaiacli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func main() {
stakecmd.GetCmdQueryValidators(storeStake, cdc),
govcmd.GetCmdQueryVote(storeGov, cdc),
govcmd.GetCmdQueryVotes(storeGov, cdc),
stakecmd.GetCmdQueryCrossStakeRewardByBscAddress(storeStake, cdc),
)...)

//Add query commands
Expand Down
1 change: 1 addition & 0 deletions x/stake/client/cli/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func AddCommands(root *cobra.Command, cdc *codec.Codec) {
GetCmdQuerySideChainReDelegationsByValidator(cdc),
GetCmdQuerySideChainTopValidators(cdc),
GetCmdQuerySideAllValidatorsCount(cdc),
GetCmdQueryCrossStakeRewardByBscAddress(cdc),
)...,
)

Expand Down
2 changes: 1 addition & 1 deletion x/stake/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ func GetCmdQueryPool(storeName string, cdc *codec.Codec) *cobra.Command {
return cmd
}

// GetCmdQueryPool implements the params query command.
// GetCmdQueryParams implements the params query command.
func GetCmdQueryParams(storeName string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "parameters",
Expand Down
47 changes: 46 additions & 1 deletion x/stake/client/cli/query_sidechain.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"encoding/json"
"errors"
"fmt"
"strconv"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"strconv"

"github.com/tendermint/tendermint/libs/cli"

Expand Down Expand Up @@ -738,6 +739,50 @@ func GetCmdQuerySideAllValidatorsCount(cdc *codec.Codec) *cobra.Command {
return cmd
}

// GetCmdQueryCrossStakeRewardByBscAddress implements the cross stake reward query command.
func GetCmdQueryCrossStakeRewardByBscAddress(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "cross-stake-reward",
Short: "Query the cross stake reward balance by BSC address",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
bscAddress, err := sdk.NewSmartChainAddress(args[0])
if err != nil {
return err
}

cliCtx := context.NewCLIContext().WithCodec(cdc)
sideChainId, _, err := getSideChainConfig(cliCtx)
if err != nil {
return err
}

params := stake.QueryCrossStakeRewardParams{
BaseParams: stake.NewBaseParams(sideChainId),
BscAddress: bscAddress,
}

bz, err := json.Marshal(params)
if err != nil {
return err
}

response, err := cliCtx.QueryWithData("custom/stake/"+stake.QueryCrossStakeReward, bz)
if err != nil {
return err
}

fmt.Println("The reward balance of specified side chain address on BC is:", string(response))

return nil
},
}

cmd.Flags().AddFlagSet(fsSideChainId)

return cmd
}

func getSideChainConfig(cliCtx context.CLIContext) (sideChainId string, prefix []byte, error error) {
sideChainId, error = getSideChainId()
if error != nil {
Expand Down
2 changes: 1 addition & 1 deletion x/stake/keeper/distribute_sidechain.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (k Keeper) DistributeInBreathBlock(ctx sdk.Context, sideChainId string) sdk
if k.AddrPool != nil {
k.AddrPool.AddAddrs(changedAddrs[:])
}
rewardSum += totalReward
rewardSum += remainReward.RawInt()
}

if ctx.IsDeliverTx() && k.PbsbServer != nil {
Expand Down
28 changes: 28 additions & 0 deletions x/stake/querier/queryable.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
QueryTopValidators = "topValidators"
QueryAllValidatorsCount = "allValidatorsCount"
QueryAllUnJailValidatorsCount = "allUnJailValidatorsCount"
QueryCrossStakeRewardByBscAddress = "crossStakeRewardByBscAddress"
)

// creates a querier for staking REST endpoints
Expand Down Expand Up @@ -148,6 +149,13 @@ func NewQuerier(k keep.Keeper, cdc *codec.Codec) sdk.Querier {
return res, err
}
return queryAllUnJailValidatorsCount(ctx, cdc, k)
case QueryCrossStakeRewardByBscAddress:
p := new(QueryCrossStakeRewardParams)
ctx, err = RequestPrepare(ctx, k, req, p)
if err != nil {
return res, err
}
return queryCrossStakeRewardByBscAddress(ctx, cdc, p, k)
default:
return nil, sdk.ErrUnknownRequest("unknown stake query endpoint")
}
Expand Down Expand Up @@ -223,6 +231,12 @@ type QueryTopValidatorsParams struct {
Top int
}

// defines the params for 'custom/stake/crossStakeReward'
type QueryCrossStakeRewardParams struct {
BaseParams
BscAddress sdk.SmartChainAddress
}

func queryValidators(ctx sdk.Context, cdc *codec.Codec, k keep.Keeper) (res []byte, err sdk.Error) {
stakeParams := k.GetParams(ctx)
validators := k.GetValidators(ctx, stakeParams.MaxValidators)
Expand Down Expand Up @@ -421,6 +435,20 @@ func queryAllUnJailValidatorsCount(ctx sdk.Context, cdc *codec.Codec, k keep.Kee
return res, nil
}

func queryCrossStakeRewardByBscAddress(ctx sdk.Context, cdc *codec.Codec, params *QueryCrossStakeRewardParams, k keep.Keeper) ([]byte, sdk.Error) {
if params.BscAddress.IsEmpty() {
return []byte{}, sdk.ErrInternal("invalid side chain address")
}
delegateCAoB := types.GetStakeCAoB(params.BscAddress[:], types.DelegateCAoBSalt)
rewardCAoB := types.GetStakeCAoB(delegateCAoB.Bytes(), types.RewardCAoBSalt)
reward := k.BankKeeper.GetCoins(ctx, rewardCAoB).AmountOf(k.BondDenom(ctx))
res, errRes := codec.MarshalJSONIndent(cdc, reward)
if errRes != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
}
return res, nil
}

func prepareSideChainCtx(ctx sdk.Context, k keep.Keeper, sideChainId string) (sdk.Context, sdk.Error) {
scCtx, err := k.ScKeeper.PrepareCtxForSideChain(ctx, sideChainId)
if err != nil {
Expand Down
48 changes: 25 additions & 23 deletions x/stake/stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,30 @@ import (
)

type (
Keeper = keeper.Keeper
Validator = types.Validator
Description = types.Description
Commission = types.Commission
Delegation = types.Delegation
UnbondingDelegation = types.UnbondingDelegation
Redelegation = types.Redelegation
Params = types.Params
Pool = types.Pool
MsgCreateValidator = types.MsgCreateValidator
MsgRemoveValidator = types.MsgRemoveValidator
MsgCreateValidatorProposal = types.MsgCreateValidatorProposal
MsgEditValidator = types.MsgEditValidator
MsgDelegate = types.MsgDelegate
MsgBeginUnbonding = types.MsgBeginUnbonding
MsgBeginRedelegate = types.MsgBeginRedelegate
GenesisState = types.GenesisState
QueryDelegatorParams = querier.QueryDelegatorParams
QueryValidatorParams = querier.QueryValidatorParams
QueryBondsParams = querier.QueryBondsParams
CreateValidatorJsonMsg = types.CreateValidatorJsonMsg
QueryTopValidatorsParams = querier.QueryTopValidatorsParams
BaseParams = querier.BaseParams
Keeper = keeper.Keeper
Validator = types.Validator
Description = types.Description
Commission = types.Commission
Delegation = types.Delegation
UnbondingDelegation = types.UnbondingDelegation
Redelegation = types.Redelegation
Params = types.Params
Pool = types.Pool
MsgCreateValidator = types.MsgCreateValidator
MsgRemoveValidator = types.MsgRemoveValidator
MsgCreateValidatorProposal = types.MsgCreateValidatorProposal
MsgEditValidator = types.MsgEditValidator
MsgDelegate = types.MsgDelegate
MsgBeginUnbonding = types.MsgBeginUnbonding
MsgBeginRedelegate = types.MsgBeginRedelegate
GenesisState = types.GenesisState
QueryDelegatorParams = querier.QueryDelegatorParams
QueryValidatorParams = querier.QueryValidatorParams
QueryBondsParams = querier.QueryBondsParams
QueryCrossStakeRewardParams = querier.QueryCrossStakeRewardParams
CreateValidatorJsonMsg = types.CreateValidatorJsonMsg
QueryTopValidatorsParams = querier.QueryTopValidatorsParams
BaseParams = querier.BaseParams

MsgCreateSideChainValidator = types.MsgCreateSideChainValidator
MsgEditSideChainValidator = types.MsgEditSideChainValidator
Expand Down Expand Up @@ -146,6 +147,7 @@ const (
QueryDelegatorValidator = querier.QueryDelegatorValidator
QueryPool = querier.QueryPool
QueryParameters = querier.QueryParameters
QueryCrossStakeReward = querier.QueryCrossStakeRewardByBscAddress

Topic = types.Topic
)
Expand Down