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: staking config #10988

Merged
merged 31 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
6a5cab2
experiment with staking config
tac0turtle Jan 20, 2022
99ae22e
remove unneeded inits
tac0turtle Jan 20, 2022
8f8e868
minor change
tac0turtle Jan 20, 2022
4bd96f9
Merge branch 'master' into staking-config
tac0turtle Jan 20, 2022
371ccbc
modify comment
tac0turtle Jan 20, 2022
cbc5aa4
remove types/staking.go
tac0turtle Jan 21, 2022
d2b4dd7
reuse values
tac0turtle Jan 21, 2022
2708cf2
handle overflow
tac0turtle Jan 21, 2022
57e4f14
add changelog entry
tac0turtle Jan 21, 2022
2a5a324
address comments
tac0turtle Jan 21, 2022
80c0c08
Merge branch 'master' into staking-config
tac0turtle Jan 21, 2022
8c0ed03
edit changelog
tac0turtle Jan 21, 2022
cb6977f
Update x/staking/types/power_reduction.go
tac0turtle Jan 23, 2022
9bcc073
Merge branch 'master' into staking-config
tac0turtle Jan 23, 2022
a98d809
Merge branch 'master' into staking-config
tac0turtle Jan 26, 2022
d43fe2b
fix group sims
tac0turtle Jan 26, 2022
d083d67
Merge branch 'master' into staking-config
tac0turtle Feb 1, 2022
110725c
Merge branch 'master' into staking-config
tac0turtle Feb 2, 2022
522e92c
add testutil token file
tac0turtle Feb 2, 2022
0c529e3
remove changelog
tac0turtle Feb 2, 2022
ba7bbdb
Merge branch 'master' into staking-config
tac0turtle Feb 2, 2022
ade2b82
Merge branch 'master' into staking-config
tac0turtle Feb 3, 2022
2e60460
fix import cycle
tac0turtle Feb 3, 2022
58e5861
Merge branch 'master' into staking-config
tac0turtle Feb 21, 2022
ddf5330
Merge branch 'master' into staking-config
tac0turtle Feb 23, 2022
812e268
Merge branch 'master' into staking-config
tac0turtle Feb 24, 2022
0b54bb3
Merge branch 'master' into staking-config
tac0turtle Mar 8, 2022
783b283
Merge branch 'master' into staking-config
tac0turtle Mar 9, 2022
77a6739
Merge branch 'master' into staking-config
atheeshp Mar 10, 2022
eb6cf94
Merge branch 'master' into staking-config
tac0turtle Mar 11, 2022
457060c
Merge branch 'master' into staking-config
tac0turtle Mar 22, 2022
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,10 @@ Ref: https://keepachangelog.com/en/1.0.0/
* [\#11124](https://github.com/cosmos/cosmos-sdk/pull/11124) Add `GetAllVersions` to application store
* (x/authz) [\#10447](https://github.com/cosmos/cosmos-sdk/pull/10447) authz `NewGrant` takes a new argument: block time, to correctly validate expire time.
* [\#10961](https://github.com/cosmos/cosmos-sdk/pull/10961) Support third-party modules to add extension snapshots to state-sync.
* [\#10988](https://github.com/cosmos/cosmos-sdk/pull/10988) Removes sdk.PowerReduction as a global and pass it directly to the staking keeper on creation.
- Deletes `types/staking.go` and moves the functions to `x/staking/types/power_reduction.go`
* [\#11274](https://github.com/cosmos/cosmos-sdk/pull/11274) `types/errors.New` now is an alias for `types/errors.Register` and should only be used in initialization code.


### Client Breaking Changes

* [\#11089](https://github.com/cosmos/cosmos-sdk/pull/11089]) interacting with the node through `grpc.Dial` requires clients to pass a codec refer to [doc](docs/run-node/interact-node.md).
Expand Down
99 changes: 0 additions & 99 deletions doc/proto-docs.md

This file was deleted.

8 changes: 7 additions & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,14 @@ func NewSimApp(
app.BankKeeper = bankkeeper.NewBaseKeeper(
appCodec, keys[banktypes.StoreKey], app.AccountKeeper, app.GetSubspace(banktypes.ModuleName), app.ModuleAccountAddrs(),
)

stakingConfig := stakingtypes.DefaultConfig()
/*
Example of setting staking params:
stakingConfig.PowerReduction = sdk.NewIntFromUint64(10)
*/
stakingKeeper := stakingkeeper.NewKeeper(
appCodec, keys[stakingtypes.StoreKey], app.AccountKeeper, app.BankKeeper, app.GetSubspace(stakingtypes.ModuleName),
appCodec, keys[stakingtypes.StoreKey], app.AccountKeeper, app.BankKeeper, app.GetSubspace(stakingtypes.ModuleName), stakingConfig,
)
app.MintKeeper = mintkeeper.NewKeeper(
appCodec, keys[minttypes.StoreKey], app.GetSubspace(minttypes.ModuleName), &stakingKeeper,
Expand Down
6 changes: 3 additions & 3 deletions simapp/simd/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ func initTestnetFiles(
return err
}

accTokens := sdk.TokensFromConsensusPower(1000, sdk.DefaultPowerReduction)
accStakingTokens := sdk.TokensFromConsensusPower(500, sdk.DefaultPowerReduction)
accTokens := sdk.NewIntFromUint64(1000_000_000)
accStakingTokens := sdk.NewIntFromUint64(500_000_000)
coins := sdk.Coins{
sdk.NewCoin("testtoken", accTokens),
sdk.NewCoin(sdk.DefaultBondDenom, accStakingTokens),
Expand All @@ -292,7 +292,7 @@ func initTestnetFiles(
genBalances = append(genBalances, banktypes.Balance{Address: addr.String(), Coins: coins.Sort()})
genAccounts = append(genAccounts, authtypes.NewBaseAccount(addr, nil, 0, 0))

valTokens := sdk.TokensFromConsensusPower(100, sdk.DefaultPowerReduction)
valTokens := sdk.NewIntFromUint64(100_000_000)
createValMsg, err := stakingtypes.NewMsgCreateValidator(
sdk.ValAddress(addr),
valPubKeys[i],
Expand Down
2 changes: 1 addition & 1 deletion simapp/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func genesisStateWithValSet(t *testing.T,
validators := make([]stakingtypes.Validator, 0, len(valSet.Validators))
delegations := make([]stakingtypes.Delegation, 0, len(valSet.Validators))

bondAmt := sdk.DefaultPowerReduction
bondAmt := stakingtypes.DefaultConfig().PowerReduction

for _, val := range valSet.Validators {
pk, err := cryptocodec.FromTmPubKeyInterface(val.PubKey)
Expand Down
6 changes: 3 additions & 3 deletions testutil/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ func DefaultConfig() Config {
NumValidators: 4,
BondDenom: sdk.DefaultBondDenom,
MinGasPrices: fmt.Sprintf("0.000006%s", sdk.DefaultBondDenom),
AccountTokens: sdk.TokensFromConsensusPower(1000, sdk.DefaultPowerReduction),
StakingTokens: sdk.TokensFromConsensusPower(500, sdk.DefaultPowerReduction),
BondedTokens: sdk.TokensFromConsensusPower(100, sdk.DefaultPowerReduction),
AccountTokens: sdk.NewIntFromUint64(1000_000_000),
StakingTokens: sdk.NewIntFromUint64(500_000_000),
BondedTokens: sdk.NewIntFromUint64(100_000_000),
PruningStrategy: storetypes.PruningOptionNothing,
CleanupDir: true,
SigningAlgo: string(hd.Secp256k1Type),
Expand Down
28 changes: 28 additions & 0 deletions testutil/token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package testutil

import (
"math"

sdk "github.com/cosmos/cosmos-sdk/types"
)

var DefaultpowerReduction = sdk.NewInt(1_000_000)

// 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)
}
13 changes: 0 additions & 13 deletions types/staking.go → types/globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,3 @@ const (
// https://tendermint.com/docs/spec/abci/apps.html#endblock
ValidatorUpdateDelay int64 = 1
)

// DefaultPowerReduction is the default amount of staking tokens required for 1 unit of consensus-engine power
var DefaultPowerReduction = NewIntFromUint64(1000000)
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved

// TokensToConsensusPower - convert input tokens to potential consensus-engine power
func TokensToConsensusPower(tokens Int, powerReduction Int) int64 {
return (tokens.Quo(powerReduction)).Int64()
}

// TokensFromConsensusPower - convert input power to tokens
func TokensFromConsensusPower(power int64, powerReduction Int) Int {
return NewInt(power).Mul(powerReduction)
}
2 changes: 1 addition & 1 deletion types/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func (m *Manager) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, genesisData

// a chain must initialize with a non-empty validator set
if len(validatorUpdates) == 0 {
panic(fmt.Sprintf("validator set is empty after InitGenesis, please ensure at least one validator is initialized with a delegation greater than or equal to the DefaultPowerReduction (%d)", sdk.DefaultPowerReduction))
panic(fmt.Sprintf("validator set is empty after InitGenesis, please ensure at least one validator is initialized with a delegation greater than or equal to the DefaultPowerReduction, found in x/staking/types/config.go"))
}

return abci.ResponseInitChain{
Expand Down
26 changes: 0 additions & 26 deletions types/staking_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions x/auth/client/testutil/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion x/auth/migrations/v043/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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{})
Expand Down
3 changes: 2 additions & 1 deletion x/bank/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -324,7 +325,7 @@ func (suite *IntegrationTestSuite) TestGRPCDenomOwners() {

bal := sdk.NewCoins(sdk.NewCoin(
sdk.DefaultBondDenom,
sdk.TokensFromConsensusPower(initialPower/10, sdk.DefaultPowerReduction),
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
sdktestutil.TokensFromConsensusPower(initialPower/10, sdktestutil.DefaultpowerReduction),
))
suite.Require().NoError(keeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, acc.GetAddress(), bal))
}
Expand Down
4 changes: 2 additions & 2 deletions x/bank/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 3 additions & 5 deletions x/bank/types/balance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions x/evidence/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ 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"

"github.com/cosmos/cosmos-sdk/baseapp"
"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 (
Expand All @@ -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))
)

Expand Down
Loading