-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refine codes, remove unneeded data from genesis
- Loading branch information
1 parent
6e77932
commit 0affb06
Showing
20 changed files
with
352 additions
and
390 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package cli | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
"github.com/cosmos/cosmos-sdk/client/tx" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/bnb-chain/greenfield/x/challenge/types" | ||
) | ||
|
||
var _ = strconv.Itoa(0) | ||
|
||
func CmdHeartbeat() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "heartbeat [challenge-id] [vote-validator-set] [vote-agg-signature]", | ||
Short: "Broadcast message heartbeat", | ||
Args: cobra.ExactArgs(3), | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
// validate that the challenge id is an uint | ||
argChallengeId, err := strconv.ParseUint(args[0], 10, 64) | ||
if err != nil { | ||
return fmt.Errorf("challenge-id %s not a valid uint, please input a valid challenge-id", args[0]) | ||
} | ||
|
||
argVoteValidatorSet := make([]uint64, 0) | ||
splits := strings.Split(args[1], ",") | ||
for _, split := range splits { | ||
val, err := strconv.ParseUint(split, 10, 64) | ||
if err != nil { | ||
return fmt.Errorf("vote-validator-set %s not a valid comma seperated uint array, please input a valid vote-validator-set", args[1]) | ||
} | ||
argVoteValidatorSet = append(argVoteValidatorSet, val) | ||
} | ||
if len(argVoteValidatorSet) == 0 { | ||
return fmt.Errorf("vote-validator-set %s not a valid comma seperated uint array, please input a valid vote-validator-set", args[1]) | ||
} | ||
|
||
argVoteAggSignature, err := hex.DecodeString(args[2]) | ||
if err != nil { | ||
return fmt.Errorf("vote-agg-signature %s not a hex encoded bytes, please input a valid vote-agg-signature", args[2]) | ||
} | ||
|
||
clientCtx, err := client.GetClientTxContext(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
msg := types.NewMsgHeartbeat( | ||
clientCtx.GetFromAddress(), | ||
argChallengeId, | ||
argVoteValidatorSet, | ||
argVoteAggSignature, | ||
) | ||
if err := msg.ValidateBasic(); err != nil { | ||
return err | ||
} | ||
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) | ||
}, | ||
} | ||
|
||
flags.AddTxFlagsToCmd(cmd) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package keeper | ||
|
||
import ( | ||
"context" | ||
|
||
sdkmath "cosmossdk.io/math" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types" | ||
|
||
app "github.com/bnb-chain/greenfield/sdk/types" | ||
"github.com/bnb-chain/greenfield/x/challenge/types" | ||
paymentmoduletypes "github.com/bnb-chain/greenfield/x/payment/types" | ||
) | ||
|
||
func (k msgServer) Heartbeat(goCtx context.Context, msg *types.MsgHeartbeat) (*types.MsgHeartbeatResponse, error) { | ||
ctx := sdk.UnwrapSDKContext(goCtx) | ||
|
||
submitterAddress, err := sdk.AccAddressFromHexUnsafe(msg.Creator) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// check challenge id | ||
heartbeatInterval := k.HeartbeatInterval(ctx) | ||
maxChallengeId := k.GetChallengeId(ctx) | ||
heartbeatChallengeId := k.GetHeartbeatChallengeId(ctx) | ||
|
||
if msg.ChallengeId > maxChallengeId || msg.ChallengeId <= heartbeatChallengeId || | ||
msg.ChallengeId%heartbeatInterval != 0 { // be noted, we allow to skip some heartbeats | ||
return nil, types.ErrInvalidChallengeId | ||
} | ||
|
||
// check heartbeat validators and signatures | ||
_, err = k.verifySignature(ctx, msg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// calculate rewards | ||
total := k.paymentKeeper.QueryValidatorRewards(ctx) | ||
validatorReward, submitterReward := k.calculateHeartbeatRewards(ctx, total) | ||
|
||
// reward tx validator & submitter | ||
toValidator := sdk.Coins{ | ||
sdk.Coin{Denom: app.Denom, Amount: validatorReward}, | ||
} | ||
err = k.bankKeeper.SendCoinsFromModuleToModule(ctx, paymentmoduletypes.ModuleName, distributiontypes.ModuleName, toValidator) | ||
if err != nil { | ||
return nil, err | ||
} | ||
toSubmitter := sdk.Coins{ | ||
sdk.Coin{Denom: app.Denom, Amount: submitterReward}, | ||
} | ||
err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, paymentmoduletypes.ModuleName, submitterAddress, toSubmitter) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := ctx.EventManager().EmitTypedEvents(&types.EventChallengeHeartbeat{ | ||
ChallengeId: msg.ChallengeId, | ||
}); err != nil { | ||
return nil, err | ||
} | ||
|
||
k.SetHeartbeatChallengeId(ctx, msg.ChallengeId) | ||
|
||
return &types.MsgHeartbeatResponse{}, nil | ||
} | ||
|
||
func (k msgServer) calculateHeartbeatRewards(ctx sdk.Context, total sdkmath.Int) (sdkmath.Int, sdkmath.Int) { | ||
var submitterReward sdkmath.Int | ||
threshold := k.HeartbeatRewardThreshold(ctx) | ||
rated := k.HeartbeatRewardRate(ctx).Mul(sdk.NewDecFromInt(total)).TruncateInt() | ||
if rated.GT(threshold) { | ||
submitterReward = threshold | ||
} else { | ||
submitterReward = rated | ||
} | ||
|
||
return total.Sub(submitterReward), submitterReward | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package simulation | ||
|
||
import ( | ||
"math/rand" | ||
|
||
"github.com/cosmos/cosmos-sdk/baseapp" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
simtypes "github.com/cosmos/cosmos-sdk/types/simulation" | ||
|
||
"github.com/bnb-chain/greenfield/x/challenge/keeper" | ||
"github.com/bnb-chain/greenfield/x/challenge/types" | ||
) | ||
|
||
func SimulateMsgHeartbeat( | ||
ak types.AccountKeeper, | ||
bk types.BankKeeper, | ||
k keeper.Keeper, | ||
) simtypes.Operation { | ||
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, | ||
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { | ||
simAccount, _ := simtypes.RandomAcc(r, accs) | ||
msg := &types.MsgHeartbeat{ | ||
Creator: simAccount.Address.String(), | ||
} | ||
|
||
// TODO: Handling the Heartbeat simulation | ||
|
||
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "Heartbeat simulation not implemented"), nil, nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.