Skip to content

Commit

Permalink
wip2
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-zaremba committed Oct 7, 2020
1 parent 98d7e9b commit 88e04f7
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 104 deletions.
14 changes: 14 additions & 0 deletions codec/types/any.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/gogo/protobuf/proto"
)

Expand Down Expand Up @@ -98,6 +99,19 @@ func UnsafePackAny(x interface{}) *Any {
return &Any{cachedValue: x}
}

// PackAny is a checked and safe version of UnsafePackAny. It assures that
// `x` implements the proto.Message interface and uses it to serialize `x`.
func PackAny(x interface{}) (*Any, error) {
if intoany, ok := x.(IntoAny); ok {
return intoany.AsAny(), nil
}
protoMsg, ok := x.(proto.Message)
if !ok {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "Expecting %T to implement proto.Message", x)
}
return NewAnyWithValue(protoMsg)
}

// GetCachedValue returns the cached value from the Any if present
func (any *Any) GetCachedValue() interface{} {
return any.cachedValue
Expand Down
19 changes: 10 additions & 9 deletions x/distribution/keeper/allocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/staking/teststaking"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

Expand All @@ -19,11 +20,11 @@ func TestAllocateTokensToValidatorWithCommission(t *testing.T) {

addrs := simapp.AddTestAddrs(app, ctx, 3, sdk.NewInt(1234))
valAddrs := simapp.ConvertAddrsToValAddrs(addrs)
sk := NewHandlerT(ctx, app.StakingKeeper)
sh := teststaking.NewService(ctx, app.StakingKeeper)

// create validator with 50% commission
commission := stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
sk.CreateValidator(t, sdk.ValAddress(addrs[0]), valConsPk1, 100, commission)
sh.CreateValidator(t, sdk.ValAddress(addrs[0]), valConsPk1, 100, commission)
val := app.StakingKeeper.Validator(ctx, valAddrs[0])

// allocate tokens
Expand All @@ -48,15 +49,15 @@ func TestAllocateTokensToManyValidators(t *testing.T) {

addrs := simapp.AddTestAddrs(app, ctx, 2, sdk.NewInt(1234))
valAddrs := simapp.ConvertAddrsToValAddrs(addrs)
sk := NewHandlerT(ctx, app.StakingKeeper)
sh := teststaking.NewService(ctx, app.StakingKeeper)

// create validator with 50% commission
commission := stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
sk.CreateValidator(t, valAddrs[0], valConsPk1, 100, commission)
sh.CreateValidator(t, valAddrs[0], valConsPk1, 100, commission)

// create second validator with 0% commission
commission = stakingtypes.NewCommissionRates(sdk.NewDec(0), sdk.NewDec(0), sdk.NewDec(0))
sk.CreateValidator(t, valAddrs[1], valConsPk2, 100, commission)
sh.CreateValidator(t, valAddrs[1], valConsPk2, 100, commission)

abciValA := abci.Validator{
Address: valConsPk1.Address(),
Expand Down Expand Up @@ -118,19 +119,19 @@ func TestAllocateTokensTruncation(t *testing.T) {

addrs := simapp.AddTestAddrs(app, ctx, 3, sdk.NewInt(1234))
valAddrs := simapp.ConvertAddrsToValAddrs(addrs)
sk := NewHandlerT(ctx, app.StakingKeeper)
sh := teststaking.NewService(ctx, app.StakingKeeper)

// create validator with 10% commission
commission := stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(1, 1), sdk.NewDecWithPrec(1, 1), sdk.NewDec(0))
sk.CreateValidator(t, valAddrs[0], valConsPk1, 110, commission)
sh.CreateValidator(t, valAddrs[0], valConsPk1, 110, commission)

// create second validator with 10% commission
commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(1, 1), sdk.NewDecWithPrec(1, 1), sdk.NewDec(0))
sk.CreateValidator(t, valAddrs[1], valConsPk2, 100, commission)
sh.CreateValidator(t, valAddrs[1], valConsPk2, 100, commission)

// create third validator with 10% commission
commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(1, 1), sdk.NewDecWithPrec(1, 1), sdk.NewDec(0))
sk.CreateValidator(t, valAddrs[2], valConsPk3, 100, commission)
sh.CreateValidator(t, valAddrs[2], valConsPk3, 100, commission)

abciValA := abci.Validator{
Address: valConsPk1.Address(),
Expand Down
39 changes: 20 additions & 19 deletions x/distribution/keeper/delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@ import (
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/cosmos/cosmos-sdk/x/staking/teststaking"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

func TestCalculateRewardsBasic(t *testing.T) {
app := simapp.Setup(false)
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
sk := NewHandlerT(ctx, app.StakingKeeper)
sh := teststaking.NewService(ctx, app.StakingKeeper)

addr := simapp.AddTestAddrs(app, ctx, 2, sdk.NewInt(1000))
valAddrs := simapp.ConvertAddrsToValAddrs(addr)

// create validator with 50% commission
commission := stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
sk.CreateValidator(t, valAddrs[0], valConsPk1, 100, commission)
sh.CreateValidator(t, valAddrs[0], valConsPk1, 100, commission)

// end block to bond validator
staking.EndBlocker(ctx, app.StakingKeeper)
Expand Down Expand Up @@ -73,12 +74,12 @@ func TestCalculateRewardsAfterSlash(t *testing.T) {

addr := simapp.AddTestAddrs(app, ctx, 2, sdk.NewInt(100000000))
valAddrs := simapp.ConvertAddrsToValAddrs(addr)
sk := NewHandlerT(ctx, app.StakingKeeper)
sh := teststaking.NewService(ctx, app.StakingKeeper)

// create validator with 50% commission
commission := stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
valPower := int64(100)
sk.CreateValidatorWithValPower(t, valAddrs[0], valConsPk1, valPower, commission)
sh.CreateValidatorWithValPower(t, valAddrs[0], valConsPk1, valPower, commission)

// end block to bond validator
staking.EndBlocker(ctx, app.StakingKeeper)
Expand Down Expand Up @@ -134,14 +135,14 @@ func TestCalculateRewardsAfterManySlashes(t *testing.T) {
app := simapp.Setup(false)
ctx := app.BaseApp.NewContext(false, tmproto.Header{})

sk := NewHandlerT(ctx, app.StakingKeeper)
sh := teststaking.NewService(ctx, app.StakingKeeper)
addr := simapp.AddTestAddrs(app, ctx, 2, sdk.NewInt(100000000))
valAddrs := simapp.ConvertAddrsToValAddrs(addr)

// create validator with 50% commission
valPower := int64(100)
commission := stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
sk.CreateValidatorWithValPower(t, valAddrs[0], valConsPk1, valPower, commission)
sh.CreateValidatorWithValPower(t, valAddrs[0], valConsPk1, valPower, commission)

// end block to bond validator
staking.EndBlocker(ctx, app.StakingKeeper)
Expand Down Expand Up @@ -209,13 +210,13 @@ func TestCalculateRewardsMultiDelegator(t *testing.T) {
app := simapp.Setup(false)
ctx := app.BaseApp.NewContext(false, tmproto.Header{})

sk := NewHandlerT(ctx, app.StakingKeeper)
sh := teststaking.NewService(ctx, app.StakingKeeper)
addr := simapp.AddTestAddrs(app, ctx, 2, sdk.NewInt(100000000))
valAddrs := simapp.ConvertAddrsToValAddrs(addr)

// create validator with 50% commission
commission := stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
sk.CreateValidator(t, valAddrs[0], valConsPk1, 100, commission)
sh.CreateValidator(t, valAddrs[0], valConsPk1, 100, commission)

// end block to bond validator
staking.EndBlocker(ctx, app.StakingKeeper)
Expand All @@ -233,7 +234,7 @@ func TestCalculateRewardsMultiDelegator(t *testing.T) {
app.DistrKeeper.AllocateTokensToValidator(ctx, val, tokens)

// second delegation
sk.Delegate(t, sdk.AccAddress(valAddrs[1]), valAddrs[0], 100)
sh.Delegate(t, sdk.AccAddress(valAddrs[1]), valAddrs[0], 100)
del2 := app.StakingKeeper.Delegation(ctx, sdk.AccAddress(valAddrs[1]), valAddrs[0])

// fetch updated validator
Expand Down Expand Up @@ -275,7 +276,7 @@ func TestWithdrawDelegationRewardsBasic(t *testing.T) {

addr := simapp.AddTestAddrs(app, ctx, 1, sdk.NewInt(1000000000))
valAddrs := simapp.ConvertAddrsToValAddrs(addr)
sk := NewHandlerT(ctx, app.StakingKeeper)
sh := teststaking.NewService(ctx, app.StakingKeeper)

// set module account coins
distrAcc := app.DistrKeeper.GetDistributionAccount(ctx)
Expand All @@ -285,7 +286,7 @@ func TestWithdrawDelegationRewardsBasic(t *testing.T) {
// create validator with 50% commission
power := int64(100)
commission := stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
valTokens := sk.CreateValidatorWithValPower(t, valAddrs[0], valConsPk1, power, commission)
valTokens := sh.CreateValidatorWithValPower(t, valAddrs[0], valConsPk1, power, commission)

// assert correct initial balance
expTokens := balanceTokens.Sub(valTokens)
Expand Down Expand Up @@ -344,12 +345,12 @@ func TestCalculateRewardsAfterManySlashesInSameBlock(t *testing.T) {

addr := simapp.AddTestAddrs(app, ctx, 1, sdk.NewInt(1000000000))
valAddrs := simapp.ConvertAddrsToValAddrs(addr)
sk := NewHandlerT(ctx, app.StakingKeeper)
sh := teststaking.NewService(ctx, app.StakingKeeper)

// create validator with 50% commission
valPower := int64(100)
commission := stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
sk.CreateValidatorWithValPower(t, valAddrs[0], valConsPk1, valPower, commission)
sh.CreateValidatorWithValPower(t, valAddrs[0], valConsPk1, valPower, commission)

// end block to bond validator
staking.EndBlocker(ctx, app.StakingKeeper)
Expand Down Expand Up @@ -410,14 +411,14 @@ func TestCalculateRewardsMultiDelegatorMultiSlash(t *testing.T) {
app := simapp.Setup(false)
ctx := app.BaseApp.NewContext(false, tmproto.Header{})

sk := NewHandlerT(ctx, app.StakingKeeper)
sh := teststaking.NewService(ctx, app.StakingKeeper)
addr := simapp.AddTestAddrs(app, ctx, 2, sdk.NewInt(1000000000))
valAddrs := simapp.ConvertAddrsToValAddrs(addr)

// create validator with 50% commission
commission := stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
valPower := int64(100)
sk.CreateValidatorWithValPower(t, valAddrs[0], valConsPk1, valPower, commission)
sh.CreateValidatorWithValPower(t, valAddrs[0], valConsPk1, valPower, commission)

// end block to bond validator
staking.EndBlocker(ctx, app.StakingKeeper)
Expand All @@ -440,7 +441,7 @@ func TestCalculateRewardsMultiDelegatorMultiSlash(t *testing.T) {
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 3)

// second delegation
sk.DelegateWithPower(t, sdk.AccAddress(valAddrs[1]), valAddrs[0], 100)
sh.DelegateWithPower(t, sdk.AccAddress(valAddrs[1]), valAddrs[0], 100)

del2 := app.StakingKeeper.Delegation(ctx, sdk.AccAddress(valAddrs[1]), valAddrs[0])

Expand Down Expand Up @@ -484,7 +485,7 @@ func TestCalculateRewardsMultiDelegatorMultWithdraw(t *testing.T) {
app := simapp.Setup(false)
ctx := app.BaseApp.NewContext(false, tmproto.Header{})

sk := NewHandlerT(ctx, app.StakingKeeper)
sh := teststaking.NewService(ctx, app.StakingKeeper)
addr := simapp.AddTestAddrs(app, ctx, 2, sdk.NewInt(1000000000))
valAddrs := simapp.ConvertAddrsToValAddrs(addr)
initial := int64(20)
Expand All @@ -499,7 +500,7 @@ func TestCalculateRewardsMultiDelegatorMultWithdraw(t *testing.T) {

// create validator with 50% commission
commission := stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
sk.CreateValidator(t, valAddrs[0], valConsPk1, 100, commission)
sh.CreateValidator(t, valAddrs[0], valConsPk1, 100, commission)

// end block to bond validator
staking.EndBlocker(ctx, app.StakingKeeper)
Expand All @@ -518,7 +519,7 @@ func TestCalculateRewardsMultiDelegatorMultWithdraw(t *testing.T) {
require.Equal(t, uint64(2), app.DistrKeeper.GetValidatorHistoricalReferenceCount(ctx))

// second delegation
sk.Delegate(t, sdk.AccAddress(valAddrs[1]), valAddrs[0], 100)
sh.Delegate(t, sdk.AccAddress(valAddrs[1]), valAddrs[0], 100)

// historical count should be 3 (second delegation init)
require.Equal(t, uint64(3), app.DistrKeeper.GetValidatorHistoricalReferenceCount(ctx))
Expand Down
5 changes: 3 additions & 2 deletions x/distribution/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/x/distribution/types"
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/cosmos/cosmos-sdk/x/staking/teststaking"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

Expand Down Expand Up @@ -340,9 +341,9 @@ func (suite *KeeperTestSuite) TestGRPCValidatorSlashes() {
func (suite *KeeperTestSuite) TestGRPCDelegationRewards() {
app, ctx, addrs, valAddrs := suite.app, suite.ctx, suite.addrs, suite.valAddrs

sk := NewHandlerT(ctx, app.StakingKeeper)
sh := teststaking.NewService(ctx, app.StakingKeeper)
comm := stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
sk.CreateValidator(suite.T(), valAddrs[0], valConsPk1, 100, comm)
sh.CreateValidator(suite.T(), valAddrs[0], valConsPk1, 100, comm)

staking.EndBlocker(ctx, app.StakingKeeper)
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
Expand Down
5 changes: 3 additions & 2 deletions x/distribution/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/distribution/keeper"
"github.com/cosmos/cosmos-sdk/x/distribution/types"
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/cosmos/cosmos-sdk/x/staking/teststaking"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

Expand Down Expand Up @@ -169,9 +170,9 @@ func TestQueries(t *testing.T) {
require.Equal(t, []types.ValidatorSlashEvent{slashOne, slashTwo}, slashes)

// test delegation rewards query
sk := NewHandlerT(ctx, app.StakingKeeper)
sh := teststaking.NewService(ctx, app.StakingKeeper)
comm := stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
sk.CreateValidator(t, valOpAddr1, valConsPk1, 100, comm)
sh.CreateValidator(t, valOpAddr1, valConsPk1, 100, comm)

staking.EndBlocker(ctx, app.StakingKeeper)

Expand Down
2 changes: 2 additions & 0 deletions x/slashing/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/slashing/keeper"
"github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/cosmos/cosmos-sdk/x/staking/teststaking"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

Expand Down Expand Up @@ -59,6 +60,7 @@ func TestCannotUnjailUnlessMeetMinSelfDelegation(t *testing.T) {
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
pks := simapp.CreateTestPubKeys(1)
simapp.AddTestAddrsFromPubKeys(app, ctx, pks, sdk.TokensFromConsensusPower(200))
ss := teststaking.NewHandlerT(ctx, app.StakingKeeper)

slh := slashing.NewHandler(app.SlashingKeeper)
amtInt := int64(100)
Expand Down
2 changes: 2 additions & 0 deletions x/staking/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
gogotypes "github.com/gogo/protobuf/types"
tmstrings "github.com/tendermint/tendermint/libs/strings"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -55,6 +56,7 @@ func handleMsgCreateValidator(ctx sdk.Context, msg *types.MsgCreateValidator, k
return nil, types.ErrValidatorOwnerExists
}

codectypes.PackAny(msg.Pubkey)
pk, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, msg.Pubkey)
if err != nil {
return nil, err
Expand Down
64 changes: 0 additions & 64 deletions x/staking/teststaking/handler.go

This file was deleted.

Loading

0 comments on commit 88e04f7

Please sign in to comment.