feat: staking config (#10988)

## Description

staking config to remove DefaultPowerReduction global. 

I want to see if this would be accepted as its easier to document and show users how to do this. 



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
This commit is contained in:
Marko
2022-03-22 00:24:28 +00:00
committed by GitHub
parent 0ff5f2cccd
commit 5e9656f363
47 changed files with 190 additions and 238 deletions
+2 -2
View File
@@ -648,7 +648,7 @@ func (s *IntegrationTestSuite) TestCLISendGenerateSignAndBroadcast() {
account, err := val1.ClientCtx.Keyring.Key("newAccount")
s.Require().NoError(err)
sendTokens := sdk.NewCoin(s.cfg.BondDenom, sdk.TokensFromConsensusPower(10, sdk.DefaultPowerReduction))
sendTokens := sdk.NewCoin(s.cfg.BondDenom, testutil.TokensFromConsensusPower(10, testutil.DefaultpowerReduction))
addr, err := account.GetAddress()
s.Require().NoError(err)
@@ -849,7 +849,7 @@ func (s *IntegrationTestSuite) TestCLIMultisignInsufficientCosigners() {
func (s *IntegrationTestSuite) TestCLIEncode() {
val1 := s.network.Validators[0]
sendTokens := sdk.NewCoin(s.cfg.BondDenom, sdk.TokensFromConsensusPower(10, sdk.DefaultPowerReduction))
sendTokens := sdk.NewCoin(s.cfg.BondDenom, testutil.TokensFromConsensusPower(10, testutil.DefaultpowerReduction))
normalGeneratedTx, err := s.createBankMsg(
val1, val1.Address,
+3 -1
View File
@@ -9,6 +9,7 @@ import (
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
@@ -657,7 +658,7 @@ func dirtyTrackingFields(ctx sdk.Context, vesting exported.VestingAccount, app *
}
func createValidator(t *testing.T, ctx sdk.Context, app *simapp.SimApp, powers int64) (sdk.AccAddress, sdk.ValAddress) {
valTokens := sdk.TokensFromConsensusPower(powers, sdk.DefaultPowerReduction)
valTokens := testutil.TokensFromConsensusPower(powers, testutil.DefaultpowerReduction)
addrs := simapp.AddTestAddrsIncremental(app, ctx, 1, valTokens)
valAddrs := simapp.ConvertAddrsToValAddrs(addrs)
pks := simapp.CreateTestPubKeys(1)
@@ -669,6 +670,7 @@ func createValidator(t *testing.T, ctx sdk.Context, app *simapp.SimApp, powers i
app.AccountKeeper,
app.BankKeeper,
app.GetSubspace(stakingtypes.ModuleName),
stakingtypes.DefaultConfig(),
)
val1, err := stakingtypes.NewValidator(valAddrs[0], pks[0], stakingtypes.Description{})
+2 -1
View File
@@ -4,6 +4,7 @@ import (
gocontext "context"
"fmt"
sdktestutil "github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
@@ -324,7 +325,7 @@ func (suite *IntegrationTestSuite) TestGRPCDenomOwners() {
bal := sdk.NewCoins(sdk.NewCoin(
sdk.DefaultBondDenom,
sdk.TokensFromConsensusPower(initialPower/10, sdk.DefaultPowerReduction),
sdktestutil.TokensFromConsensusPower(initialPower/10, sdktestutil.DefaultpowerReduction),
))
suite.Require().NoError(keeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, acc.GetAddress(), bal))
}
+2 -2
View File
@@ -12,6 +12,7 @@ import (
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/simapp"
sdktestutil "github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
@@ -40,8 +41,7 @@ var (
randomPermAcc = authtypes.NewEmptyModuleAccount(randomPerm, "random")
// The default power validators are initialized to have within tests
initTokens = sdk.TokensFromConsensusPower(initialPower, sdk.DefaultPowerReduction)
initCoins = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))
initCoins = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdktestutil.TokensFromConsensusPower(initialPower, sdktestutil.DefaultpowerReduction)))
)
func newFooCoin(amt int64) sdk.Coin {
+3 -5
View File
@@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
bank "github.com/cosmos/cosmos-sdk/x/bank/types"
)
@@ -141,8 +142,7 @@ func TestBalance_GetAddress(t *testing.T) {
func TestSanitizeBalances(t *testing.T) {
// 1. Generate balances
tokens := sdk.TokensFromConsensusPower(81, sdk.DefaultPowerReduction)
coin := sdk.NewCoin("benchcoin", tokens)
coin := sdk.NewCoin("benchcoin", testutil.TokensFromConsensusPower(81, testutil.DefaultpowerReduction))
coins := sdk.Coins{coin}
addrs, _ := makeRandomAddressesAndPublicKeys(20)
@@ -193,9 +193,7 @@ func BenchmarkSanitizeBalances1000(b *testing.B) {
func benchmarkSanitizeBalances(b *testing.B, nAddresses int) {
b.ReportAllocs()
tokens := sdk.TokensFromConsensusPower(81, sdk.DefaultPowerReduction)
coin := sdk.NewCoin("benchcoin", tokens)
coins := sdk.Coins{coin}
coins := sdk.NewCoins(sdk.NewCoin("benchcoin", testutil.TokensFromConsensusPower(81, testutil.DefaultpowerReduction)))
addrs, _ := makeRandomAddressesAndPublicKeys(nAddresses)
b.ResetTimer()
+3 -3
View File
@@ -5,8 +5,6 @@ import (
"fmt"
"time"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
"github.com/stretchr/testify/suite"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
@@ -14,11 +12,13 @@ import (
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/evidence/exported"
"github.com/cosmos/cosmos-sdk/x/evidence/keeper"
"github.com/cosmos/cosmos-sdk/x/evidence/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
)
var (
@@ -35,7 +35,7 @@ var (
}
// The default power validators are initialized to have within tests
initAmt = sdk.TokensFromConsensusPower(200, sdk.DefaultPowerReduction)
initAmt = testutil.TokensFromConsensusPower(200, testutil.DefaultpowerReduction)
initCoins = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initAmt))
)
+2 -1
View File
@@ -11,6 +11,7 @@ import (
"github.com/cosmos/cosmos-sdk/simapp"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
sdktestutil "github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/bank/testutil"
@@ -38,7 +39,7 @@ func (suite *SimTestSuite) SetupTest() {
func (suite *SimTestSuite) getTestingAccounts(r *rand.Rand, n int) []simtypes.Account {
accounts := simtypes.RandomAccounts(r, n)
initAmt := sdk.TokensFromConsensusPower(200, sdk.DefaultPowerReduction)
initAmt := sdktestutil.TokensFromConsensusPower(200, sdktestutil.DefaultpowerReduction)
initCoins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initAmt))
// add coins to the accounts
+2 -1
View File
@@ -9,6 +9,7 @@ import (
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/gov/keeper"
@@ -373,7 +374,7 @@ func createValidators(t *testing.T, stakingMsgSvr stakingtypes.MsgServer, ctx sd
require.True(t, len(addrs) <= len(pubkeys), "Not enough pubkeys specified at top of file.")
for i := 0; i < len(addrs); i++ {
valTokens := sdk.TokensFromConsensusPower(powerAmt[i], sdk.DefaultPowerReduction)
valTokens := testutil.TokensFromConsensusPower(powerAmt[i], testutil.DefaultpowerReduction)
valCreateMsg, err := stakingtypes.NewMsgCreateValidator(
addrs[i], pubkeys[i], sdk.NewCoin(sdk.DefaultBondDenom, valTokens),
TestDescription, TestCommissionRates, sdk.OneInt(),
+2 -1
View File
@@ -8,6 +8,7 @@ import (
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/gov/types"
@@ -18,7 +19,7 @@ import (
)
var (
valTokens = sdk.TokensFromConsensusPower(42, sdk.DefaultPowerReduction)
valTokens = testutil.TokensFromConsensusPower(42, testutil.DefaultpowerReduction)
TestProposal = v1beta1.NewTextProposal("Test", "description")
TestDescription = stakingtypes.NewDescription("T", "E", "S", "T", "Z")
TestCommissionRates = stakingtypes.NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec())
+1
View File
@@ -48,6 +48,7 @@ func createValidators(t *testing.T, ctx sdk.Context, app *simapp.SimApp, powers
app.AccountKeeper,
app.BankKeeper,
app.GetSubspace(stakingtypes.ModuleName),
stakingtypes.DefaultConfig(),
)
val1, err := stakingtypes.NewValidator(valAddrs[0], pks[0], stakingtypes.Description{})
+2 -1
View File
@@ -12,6 +12,7 @@ import (
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/simapp"
sdktestutil "github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/bank/testutil"
@@ -82,7 +83,7 @@ func (suite *SimTestSuite) TestWeightedOperations() {
func (suite *SimTestSuite) getTestingAccounts(r *rand.Rand, n int) []simtypes.Account {
accounts := simtypes.RandomAccounts(r, n)
initAmt := sdk.TokensFromConsensusPower(200, sdk.DefaultPowerReduction)
initAmt := sdktestutil.TokensFromConsensusPower(200, sdktestutil.DefaultpowerReduction)
initCoins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initAmt))
// add coins to the accounts
+2
View File
@@ -25,12 +25,14 @@ func TestBeginBlocker(t *testing.T) {
addr, pk := sdk.ValAddress(pks[0].Address()), pks[0]
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
InitTokens := sdk.NewIntFromUint64(200_000_000)
// bond the validator
power := int64(100)
amt := tstaking.CreateValidatorWithValPower(addr, pk, power, true)
staking.EndBlocker(ctx, app.StakingKeeper)
require.Equal(
t, app.BankKeeper.GetAllBalances(ctx, sdk.AccAddress(addr)),
sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.GetParams(ctx).BondDenom, InitTokens.Sub(amt))),
)
require.Equal(t, amt, app.StakingKeeper.Validator(ctx, addr).GetBondedTokens())
+3 -2
View File
@@ -11,6 +11,7 @@ import (
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
@@ -41,8 +42,8 @@ func checkValidatorSigningInfo(t *testing.T, app *simapp.SimApp, addr sdk.ConsAd
}
func TestSlashingMsgs(t *testing.T) {
genTokens := sdk.TokensFromConsensusPower(42, sdk.DefaultPowerReduction)
bondTokens := sdk.TokensFromConsensusPower(10, sdk.DefaultPowerReduction)
genTokens := testutil.TokensFromConsensusPower(42, testutil.DefaultpowerReduction)
bondTokens := testutil.TokensFromConsensusPower(10, testutil.DefaultpowerReduction)
genCoin := sdk.NewCoin(sdk.DefaultBondDenom, genTokens)
bondCoin := sdk.NewCoin(sdk.DefaultBondDenom, bondTokens)
-10
View File
@@ -1,10 +0,0 @@
package slashing_test
import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
var (
// The default power validators are initialized to have within tests
InitTokens = sdk.TokensFromConsensusPower(200, sdk.DefaultPowerReduction)
)
-8
View File
@@ -1,8 +0,0 @@
package keeper_test
import sdk "github.com/cosmos/cosmos-sdk/types"
var (
// The default power validators are initialized to have within tests
InitTokens = sdk.TokensFromConsensusPower(200, sdk.DefaultPowerReduction)
)
+2
View File
@@ -94,10 +94,12 @@ func TestHandleNewValidator(t *testing.T) {
// Validator created
amt := tstaking.CreateValidatorWithValPower(addr, val, 100, true)
InitTokens := sdk.NewIntFromUint64(200_000_000)
staking.EndBlocker(ctx, app.StakingKeeper)
require.Equal(
t, app.BankKeeper.GetAllBalances(ctx, sdk.AccAddress(addr)),
sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.GetParams(ctx).BondDenom, InitTokens.Sub(amt))),
)
require.Equal(t, amt, app.StakingKeeper.Validator(ctx, addr).GetBondedTokens())
+2 -2
View File
@@ -40,8 +40,8 @@ func checkDelegation(
}
func TestStakingMsgs(t *testing.T) {
genTokens := sdk.TokensFromConsensusPower(42, sdk.DefaultPowerReduction)
bondTokens := sdk.TokensFromConsensusPower(10, sdk.DefaultPowerReduction)
genTokens := types.TokensFromConsensusPower(42, types.DefaultConfig().PowerReduction)
bondTokens := types.TokensFromConsensusPower(10, types.DefaultConfig().PowerReduction)
genCoin := sdk.NewCoin(sdk.DefaultBondDenom, genTokens)
bondCoin := sdk.NewCoin(sdk.DefaultBondDenom, bondTokens)
+1 -1
View File
@@ -20,7 +20,7 @@ import (
// default values
var (
DefaultTokens = sdk.TokensFromConsensusPower(100, sdk.DefaultPowerReduction)
DefaultTokens = types.TokensFromConsensusPower(100, types.DefaultConfig().PowerReduction)
defaultAmount = DefaultTokens.String() + sdk.DefaultBondDenom
defaultCommissionRate = "0.1"
defaultCommissionMaxRate = "0.2"
+1 -5
View File
@@ -1,7 +1,6 @@
package staking_test
import (
"math/big"
"testing"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
@@ -15,10 +14,6 @@ import (
"github.com/cosmos/cosmos-sdk/x/staking/types"
)
func init() {
sdk.DefaultPowerReduction = sdk.NewIntFromBigInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil))
}
// nolint:deadcode,unused,varcheck
var (
priv1 = secp256k1.GenPrivKey()
@@ -48,6 +43,7 @@ func getBaseSimappWithCustomKeeper(t *testing.T) (*codec.LegacyAmino, *simapp.Si
app.AccountKeeper,
app.BankKeeper,
app.GetSubspace(types.ModuleName),
types.DefaultConfig(),
)
app.StakingKeeper.SetParams(ctx, types.DefaultParams())
+4 -4
View File
@@ -17,22 +17,22 @@ var (
PKs = simapp.CreateTestPubKeys(500)
)
func init() {
sdk.DefaultPowerReduction = sdk.NewIntFromBigInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil))
}
// createTestInput Returns a simapp with custom StakingKeeper
// to avoid messing with the hooks.
func createTestInput(t *testing.T) (*codec.LegacyAmino, *simapp.SimApp, sdk.Context) {
app := simapp.Setup(t, false)
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
stakingConfig := types.DefaultConfig()
stakingConfig.PowerReduction = sdk.NewIntFromBigInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil))
app.StakingKeeper = keeper.NewKeeper(
app.AppCodec(),
app.GetKey(types.StoreKey),
app.AccountKeeper,
app.BankKeeper,
app.GetSubspace(types.ModuleName),
stakingConfig,
)
return app.LegacyAmino(), app, ctx
}
+1
View File
@@ -780,6 +780,7 @@ func createValidators(t *testing.T, ctx sdk.Context, app *simapp.SimApp, powers
app.AccountKeeper,
app.BankKeeper,
app.GetSubspace(types.ModuleName),
types.DefaultConfig(),
)
val1 := teststaking.NewValidator(t, valAddrs[0], pks[0])
+3 -1
View File
@@ -26,12 +26,13 @@ type Keeper struct {
bankKeeper types.BankKeeper
hooks types.StakingHooks
paramstore paramtypes.Subspace
Config types.Config
}
// NewKeeper creates a new staking Keeper instance
func NewKeeper(
cdc codec.BinaryCodec, key storetypes.StoreKey, ak types.AccountKeeper, bk types.BankKeeper,
ps paramtypes.Subspace,
ps paramtypes.Subspace, cfg types.Config,
) Keeper {
// set KeyTable if it has not already been set
if !ps.HasKeyTable() {
@@ -54,6 +55,7 @@ func NewKeeper(
bankKeeper: bk,
paramstore: ps,
hooks: nil,
Config: cfg,
}
}
+2 -5
View File
@@ -40,11 +40,8 @@ func (k Keeper) BondDenom(ctx sdk.Context) (res string) {
}
// PowerReduction - is the amount of staking tokens required for 1 unit of consensus-engine power.
// Currently, this returns a global variable that the app developer can tweak.
// TODO: we might turn this into an on-chain param:
// https://github.com/cosmos/cosmos-sdk/issues/8365
func (k Keeper) PowerReduction(ctx sdk.Context) sdk.Int {
return sdk.DefaultPowerReduction
func (k Keeper) PowerReduction(_ sdk.Context) sdk.Int {
return k.Config.PowerReduction
}
// MinCommissionRate - Minimum validator commission rate
+3 -2
View File
@@ -2,14 +2,15 @@ package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)
// TokensToConsensusPower - convert input tokens to potential consensus-engine power
func (k Keeper) TokensToConsensusPower(ctx sdk.Context, tokens sdk.Int) int64 {
return sdk.TokensToConsensusPower(tokens, k.PowerReduction(ctx))
return types.TokensToConsensusPower(tokens, k.PowerReduction(ctx))
}
// TokensFromConsensusPower - convert input power to tokens
func (k Keeper) TokensFromConsensusPower(ctx sdk.Context, power int64) sdk.Int {
return sdk.TokensFromConsensusPower(power, k.PowerReduction(ctx))
return types.TokensFromConsensusPower(power, k.PowerReduction(ctx))
}
+3 -3
View File
@@ -5,11 +5,11 @@ import (
)
func (suite *KeeperTestSuite) TestTokensToConsensusPower() {
suite.Require().Equal(int64(0), suite.app.StakingKeeper.TokensToConsensusPower(suite.ctx, sdk.DefaultPowerReduction.Sub(sdk.NewInt(1))))
suite.Require().Equal(int64(1), suite.app.StakingKeeper.TokensToConsensusPower(suite.ctx, sdk.DefaultPowerReduction))
suite.Require().Equal(int64(0), suite.app.StakingKeeper.TokensToConsensusPower(suite.ctx, suite.app.StakingKeeper.Config.PowerReduction.Sub(sdk.NewInt(1))))
suite.Require().Equal(int64(1), suite.app.StakingKeeper.TokensToConsensusPower(suite.ctx, suite.app.StakingKeeper.Config.PowerReduction))
}
func (suite *KeeperTestSuite) TestTokensFromConsensusPower() {
suite.Require().Equal(sdk.NewInt(0), suite.app.StakingKeeper.TokensFromConsensusPower(suite.ctx, 0))
suite.Require().Equal(sdk.DefaultPowerReduction, suite.app.StakingKeeper.TokensFromConsensusPower(suite.ctx, 1))
suite.Require().Equal(suite.app.StakingKeeper.Config.PowerReduction, suite.app.StakingKeeper.TokensFromConsensusPower(suite.ctx, 1))
}
+1 -1
View File
@@ -79,7 +79,7 @@ func GetValidatorsByPowerIndexKey(validator types.Validator) []byte {
// NOTE the address doesn't need to be stored because counter bytes must always be different
// NOTE the larger values are of higher value
consensusPower := sdk.TokensToConsensusPower(validator.Tokens, sdk.DefaultPowerReduction)
consensusPower := types.TokensToConsensusPower(validator.Tokens, types.DefaultConfig().PowerReduction)
consensusPowerBytes := make([]byte, 8)
binary.BigEndian.PutUint64(consensusPowerBytes, uint64(consensusPower))
+1 -1
View File
@@ -62,7 +62,7 @@ func TestStoreMigration(t *testing.T) {
{
"ValidatorsByPowerIndexKey",
v040staking.GetValidatorsByPowerIndexKey(val),
types.GetValidatorsByPowerIndexKey(val, sdk.DefaultPowerReduction),
types.GetValidatorsByPowerIndexKey(val, types.DefaultConfig().PowerReduction),
},
{
"DelegationKey",
-11
View File
@@ -1,11 +0,0 @@
package simulation_test
import (
"math/big"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func init() {
sdk.DefaultPowerReduction = sdk.NewIntFromBigInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil))
}
+1 -1
View File
@@ -256,7 +256,6 @@ func TestSimulateMsgBeginRedelegate(t *testing.T) {
// returns context and an app with updated mint keeper
func createTestApp(t *testing.T, isCheckTx bool, r *rand.Rand, n int) (*simapp.SimApp, sdk.Context, []simtypes.Account) {
sdk.DefaultPowerReduction = sdk.NewIntFromBigInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil))
accounts := simtypes.RandomAccounts(r, n)
// create validator set with single validator
@@ -281,6 +280,7 @@ func createTestApp(t *testing.T, isCheckTx bool, r *rand.Rand, n int) (*simapp.S
app.MintKeeper.SetParams(ctx, minttypes.DefaultParams())
app.MintKeeper.SetMinter(ctx, minttypes.DefaultInitialMinter())
app.StakingKeeper.Config.PowerReduction = sdk.NewIntFromBigInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil))
initAmt := app.StakingKeeper.TokensFromConsensusPower(ctx, 200)
initCoins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initAmt))
+17
View File
@@ -0,0 +1,17 @@
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Config is a config struct used for intialising the staking module to avoid using globals.
type Config struct {
// PowerReduction is the amount of staking tokens required for 1 unit of consensus-engine power
PowerReduction sdk.Int
}
func DefaultConfig() Config {
return Config{
PowerReduction: sdk.NewIntFromUint64(1000000),
}
}
+2 -3
View File
@@ -10,7 +10,6 @@ import (
"github.com/stretchr/testify/require"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)
@@ -29,7 +28,7 @@ func createValidators(t *testing.T) []types.Validator {
func TestHistoricalInfo(t *testing.T) {
validators := createValidators(t)
hi := types.NewHistoricalInfo(header, validators, sdk.DefaultPowerReduction)
hi := types.NewHistoricalInfo(header, validators, types.DefaultConfig().PowerReduction)
require.True(t, sort.IsSorted(types.Validators(hi.Valset)), "Validators are not sorted")
var value []byte
@@ -70,7 +69,7 @@ func TestValidateBasic(t *testing.T) {
err = types.ValidateBasic(hi)
require.Error(t, err, "ValidateBasic passed on unsorted ValSet")
hi = types.NewHistoricalInfo(header, validators, sdk.DefaultPowerReduction)
hi = types.NewHistoricalInfo(header, validators, types.DefaultConfig().PowerReduction)
err = types.ValidateBasic(hi)
require.NoError(t, err, "ValidateBasic failed on valid HistoricalInfo")
}
+1 -1
View File
@@ -82,7 +82,7 @@ func GetValidatorsByPowerIndexKey(validator Validator, powerReduction sdk.Int) [
// NOTE the address doesn't need to be stored because counter bytes must always be different
// NOTE the larger values are of higher value
consensusPower := sdk.TokensToConsensusPower(validator.Tokens, powerReduction)
consensusPower := TokensToConsensusPower(validator.Tokens, powerReduction)
consensusPowerBytes := make([]byte, 8)
binary.BigEndian.PutUint64(consensusPowerBytes, uint64(consensusPower))
+4 -4
View File
@@ -28,10 +28,10 @@ func TestGetValidatorPowerRank(t *testing.T) {
val1 := newValidator(t, valAddr1, keysPK1)
val1.Tokens = sdk.ZeroInt()
val2, val3, val4 := val1, val1, val1
val2.Tokens = sdk.TokensFromConsensusPower(1, sdk.DefaultPowerReduction)
val3.Tokens = sdk.TokensFromConsensusPower(10, sdk.DefaultPowerReduction)
val2.Tokens = sdk.NewIntFromUint64(1_000_000)
val3.Tokens = sdk.NewIntFromUint64(10_000_000)
x := new(big.Int).Exp(big.NewInt(2), big.NewInt(40), big.NewInt(0))
val4.Tokens = sdk.TokensFromConsensusPower(x.Int64(), sdk.DefaultPowerReduction)
val4.Tokens = sdk.NewInt(x.Int64()).Mul(sdk.NewIntFromUint64(1_000_000))
tests := []struct {
validator types.Validator
@@ -43,7 +43,7 @@ func TestGetValidatorPowerRank(t *testing.T) {
{val4, "230000010000000000149c288ede7df62742fc3b7d0962045a8cef0f79f6"},
}
for i, tt := range tests {
got := hex.EncodeToString(types.GetValidatorsByPowerIndexKey(tt.validator, sdk.DefaultPowerReduction))
got := hex.EncodeToString(types.GetValidatorsByPowerIndexKey(tt.validator, types.DefaultConfig().PowerReduction))
require.Equal(t, tt.wantHex, got, "Keys did not match on test case %d", i)
}
+26
View File
@@ -0,0 +1,26 @@
package types
import (
"math"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// TokensToConsensusPower - convert input tokens to Power
func TokensToConsensusPower(tokens sdk.Int, powerReduction sdk.Int) int64 {
if tokens.IsNil() || powerReduction.IsNil() || powerReduction.IsZero() {
return 0
}
power := tokens.Quo(powerReduction)
if power.GT(sdk.NewIntFromUint64(math.MaxInt64)) {
return 0
}
return power.Int64()
}
// TokensFromConsensusPower - convert input power to tokens
func TokensFromConsensusPower(power int64, powerReduction sdk.Int) sdk.Int {
return sdk.NewInt(power).Mul(powerReduction)
}
+34
View File
@@ -0,0 +1,34 @@
package types_test
import (
"math"
"math/big"
"testing"
"github.com/stretchr/testify/suite"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)
var powerReduction = sdk.NewIntFromUint64(1000000)
type stakingTestSuite struct {
suite.Suite
}
func TestStakingTestSuite(t *testing.T) {
suite.Run(t, new(stakingTestSuite))
}
func (s *stakingTestSuite) SetupSuite() {
s.T().Parallel()
}
func (s *stakingTestSuite) TestTokensToConsensusPower() {
s.Require().Equal(int64(0), types.TokensToConsensusPower(sdk.NewInt(999_999), powerReduction))
s.Require().Equal(int64(1), types.TokensToConsensusPower(sdk.NewInt(1_000_000), powerReduction))
s.Require().Equal(int64(0), types.TokensToConsensusPower(sdk.NewIntFromBigInt(big.NewInt(math.MaxInt64).Add(big.NewInt(math.MaxInt64), big.NewInt(1))), sdk.NewIntFromUint64(1)))
s.Require().Equal(int64(0), types.TokensToConsensusPower(sdk.NewInt(1_000_000), sdk.NewIntFromUint64(0)))
}
+1 -1
View File
@@ -366,7 +366,7 @@ func (v Validator) ConsensusPower(r sdk.Int) int64 {
// PotentialConsensusPower returns the potential consensus-engine power.
func (v Validator) PotentialConsensusPower(r sdk.Int) int64 {
return sdk.TokensToConsensusPower(v.Tokens, r)
return TokensToConsensusPower(v.Tokens, r)
}
// UpdateStatus updates the location of the shares within a validator
+6 -6
View File
@@ -58,7 +58,7 @@ func TestUpdateDescription(t *testing.T) {
func TestABCIValidatorUpdate(t *testing.T) {
validator := newValidator(t, valAddr1, pk1)
abciVal := validator.ABCIValidatorUpdate(sdk.DefaultPowerReduction)
abciVal := validator.ABCIValidatorUpdate(types.DefaultConfig().PowerReduction)
pk, err := validator.TmConsPublicKey()
require.NoError(t, err)
require.Equal(t, pk, abciVal.PubKey)
@@ -290,15 +290,15 @@ func TestValidatorsSortTendermint(t *testing.T) {
valz := types.Validators(vals)
// create expected tendermint validators by converting to tendermint then sorting
expectedVals, err := teststaking.ToTmValidators(valz, sdk.DefaultPowerReduction)
expectedVals, err := teststaking.ToTmValidators(valz, types.DefaultConfig().PowerReduction)
require.NoError(t, err)
sort.Sort(tmtypes.ValidatorsByVotingPower(expectedVals))
// sort in SDK and then convert to tendermint
sort.SliceStable(valz, func(i, j int) bool {
return types.ValidatorsByVotingPower(valz).Less(i, j, sdk.DefaultPowerReduction)
return types.ValidatorsByVotingPower(valz).Less(i, j, types.DefaultConfig().PowerReduction)
})
actualVals, err := teststaking.ToTmValidators(valz, sdk.DefaultPowerReduction)
actualVals, err := teststaking.ToTmValidators(valz, types.DefaultConfig().PowerReduction)
require.NoError(t, err)
require.Equal(t, expectedVals, actualVals, "sorting in SDK is not the same as sorting in Tendermint")
@@ -316,9 +316,9 @@ func TestValidatorToTm(t *testing.T) {
vals[i] = val
tmPk, err := cryptocodec.ToTmPubKeyInterface(pk)
require.NoError(t, err)
expected[i] = tmtypes.NewValidator(tmPk, val.ConsensusPower(sdk.DefaultPowerReduction))
expected[i] = tmtypes.NewValidator(tmPk, val.ConsensusPower(types.DefaultConfig().PowerReduction))
}
vs, err := teststaking.ToTmValidators(vals, sdk.DefaultPowerReduction)
vs, err := teststaking.ToTmValidators(vals, types.DefaultConfig().PowerReduction)
require.NoError(t, err)
require.Equal(t, expected, vs)
}