Skip to content

Commit efea5db

Browse files
authored
refactor(distribution)!: add cometinfo (#20588)
1 parent 9f68fc5 commit efea5db

File tree

10 files changed

+67
-14
lines changed

10 files changed

+67
-14
lines changed

simapp/app.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ func NewSimApp(
357357

358358
app.PoolKeeper = poolkeeper.NewKeeper(appCodec, runtime.NewEnvironment(runtime.NewKVStoreService(keys[pooltypes.StoreKey]), logger.With(log.ModuleKey, "x/protocolpool")), app.AuthKeeper, app.BankKeeper, app.StakingKeeper, authtypes.NewModuleAddress(govtypes.ModuleName).String())
359359

360-
app.DistrKeeper = distrkeeper.NewKeeper(appCodec, runtime.NewEnvironment(runtime.NewKVStoreService(keys[distrtypes.StoreKey]), logger.With(log.ModuleKey, "x/distribution")), app.AuthKeeper, app.BankKeeper, app.StakingKeeper, app.PoolKeeper, authtypes.FeeCollectorName, authtypes.NewModuleAddress(govtypes.ModuleName).String())
360+
app.DistrKeeper = distrkeeper.NewKeeper(appCodec, runtime.NewEnvironment(runtime.NewKVStoreService(keys[distrtypes.StoreKey]), logger.With(log.ModuleKey, "x/distribution")), app.AuthKeeper, app.BankKeeper, app.StakingKeeper, app.PoolKeeper, cometService, authtypes.FeeCollectorName, authtypes.NewModuleAddress(govtypes.ModuleName).String())
361361

362362
app.SlashingKeeper = slashingkeeper.NewKeeper(runtime.NewEnvironment(runtime.NewKVStoreService(keys[slashingtypes.StoreKey]), logger.With(log.ModuleKey, "x/slashing")),
363363
appCodec, legacyAmino, app.StakingKeeper, authtypes.NewModuleAddress(govtypes.ModuleName).String(),

tests/integration/distribution/keeper/msg_server_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func initFixture(t *testing.T) *fixture {
137137
poolKeeper := poolkeeper.NewKeeper(cdc, runtime.NewEnvironment(runtime.NewKVStoreService(keys[pooltypes.StoreKey]), log.NewNopLogger()), accountKeeper, bankKeeper, stakingKeeper, authority.String())
138138

139139
distrKeeper := distrkeeper.NewKeeper(
140-
cdc, runtime.NewEnvironment(runtime.NewKVStoreService(keys[distrtypes.StoreKey]), logger), accountKeeper, bankKeeper, stakingKeeper, poolKeeper, distrtypes.ModuleName, authority.String(),
140+
cdc, runtime.NewEnvironment(runtime.NewKVStoreService(keys[distrtypes.StoreKey]), logger), accountKeeper, bankKeeper, stakingKeeper, poolKeeper, cometService, distrtypes.ModuleName, authority.String(),
141141
)
142142

143143
authModule := auth.NewAppModule(cdc, accountKeeper, acctsModKeeper, authsims.RandomGenesisAccounts)
@@ -163,6 +163,7 @@ func initFixture(t *testing.T) *fixture {
163163
},
164164
},
165165
},
166+
ProposerAddress: valConsAddr,
166167
})
167168

168169
integrationApp := integration.NewIntegrationApp(ctx, logger, keys, cdc,

x/distribution/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Ref: https://keepachangelog.com/en/1.0.0/
3131

3232
### API Breaking Changes
3333

34+
35+
* [#20588](https://github.com/cosmos/cosmos-sdk/pull/20588) `x/distribution` now takes cometService in order to get consensus related information.
3436
* [#19868](https://github.com/cosmos/cosmos-sdk/pull/19868) Removes Accounts String method
3537
* `NewMsgSetWithdrawAddress` now takes strings as argument instead of `sdk.AccAddress`.
3638
* `NewGenesisState` now takes a string as argument instead of `sdk.ConsAddress`.
@@ -74,4 +76,4 @@ Ref: https://keepachangelog.com/en/1.0.0/
7476

7577
### Bug Fixes
7678

77-
* [#19301](https://github.com/cosmos/cosmos-sdk/pull/19301) Fix vulnerability in `incrementReferenceCount` in distribution.
79+
* [#19301](https://github.com/cosmos/cosmos-sdk/pull/19301) Fix vulnerability in `incrementReferenceCount` in distribution.

x/distribution/depinject.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package distribution
33
import (
44
modulev1 "cosmossdk.io/api/cosmos/distribution/module/v1"
55
"cosmossdk.io/core/appmodule"
6+
"cosmossdk.io/core/comet"
67
"cosmossdk.io/depinject"
78
"cosmossdk.io/depinject/appconfig"
89
authtypes "cosmossdk.io/x/auth/types"
@@ -27,9 +28,10 @@ func init() {
2728
type ModuleInputs struct {
2829
depinject.In
2930

30-
Config *modulev1.Module
31-
Environment appmodule.Environment
32-
Cdc codec.Codec
31+
Config *modulev1.Module
32+
Environment appmodule.Environment
33+
Cdc codec.Codec
34+
CometService comet.Service
3335

3436
AccountKeeper types.AccountKeeper
3537
BankKeeper types.BankKeeper
@@ -69,6 +71,7 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
6971
in.BankKeeper,
7072
in.StakingKeeper,
7173
in.PoolKeeper,
74+
in.CometService,
7275
feeCollectorName,
7376
authorityAddr,
7477
)

x/distribution/keeper/abci.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package keeper
22

33
import (
4+
"context"
5+
46
"cosmossdk.io/x/distribution/types"
57

68
"github.com/cosmos/cosmos-sdk/telemetry"
@@ -10,31 +12,33 @@ import (
1012
// BeginBlocker sets the proposer for determining distribution during endblock
1113
// and distribute rewards for the previous block.
1214
// TODO: use context.Context after including the comet service
13-
func (k Keeper) BeginBlocker(ctx sdk.Context) error {
15+
func (k Keeper) BeginBlocker(ctx context.Context) error {
1416
defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyBeginBlocker)
1517

1618
// determine the total power signing the block
1719
var previousTotalPower int64
18-
for _, vote := range ctx.CometInfo().LastCommit.Votes {
20+
header := k.HeaderService.HeaderInfo(ctx)
21+
ci := k.cometService.CometInfo(ctx)
22+
for _, vote := range ci.LastCommit.Votes {
1923
previousTotalPower += vote.Validator.Power
2024
}
2125

2226
// TODO this is Tendermint-dependent
2327
// ref https://github.com/cosmos/cosmos-sdk/issues/3095
24-
if ctx.BlockHeight() > 1 {
25-
if err := k.AllocateTokens(ctx, previousTotalPower, ctx.CometInfo().LastCommit.Votes); err != nil {
28+
if header.Height > 1 {
29+
if err := k.AllocateTokens(ctx, previousTotalPower, ci.LastCommit.Votes); err != nil {
2630
return err
2731
}
2832

2933
// every 1000 blocks send whole coins from decimal pool to community pool
30-
if ctx.BlockHeight()%1000 == 0 {
34+
if header.Height%1000 == 0 {
3135
if err := k.sendDecimalPoolToCommunityPool(ctx); err != nil {
3236
return err
3337
}
3438
}
3539
}
3640

3741
// record the proposer for when we payout on the next block
38-
consAddr := sdk.ConsAddress(ctx.BlockHeader().ProposerAddress)
42+
consAddr := sdk.ConsAddress(ci.ProposerAddress)
3943
return k.PreviousProposer.Set(ctx, consAddr)
4044
}

x/distribution/keeper/allocation_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package keeper_test
22

33
import (
4+
"context"
45
"testing"
56
"time"
67

@@ -28,6 +29,17 @@ import (
2829
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
2930
)
3031

32+
var _ comet.Service = (*emptyCometService)(nil)
33+
34+
type emptyCometService struct{}
35+
36+
// CometInfo implements comet.Service.
37+
func (e *emptyCometService) CometInfo(context.Context) comet.Info {
38+
return comet.Info{}
39+
}
40+
41+
var testCometService = &emptyCometService{}
42+
3143
func TestAllocateTokensToValidatorWithCommission(t *testing.T) {
3244
ctrl := gomock.NewController(t)
3345
key := storetypes.NewKVStoreKey(disttypes.StoreKey)
@@ -58,6 +70,7 @@ func TestAllocateTokensToValidatorWithCommission(t *testing.T) {
5870
bankKeeper,
5971
stakingKeeper,
6072
poolKeeper,
73+
testCometService,
6174
"fee_collector",
6275
authorityAddr,
6376
)
@@ -124,6 +137,7 @@ func TestAllocateTokensToManyValidators(t *testing.T) {
124137
bankKeeper,
125138
stakingKeeper,
126139
poolKeeper,
140+
testCometService,
127141
"fee_collector",
128142
authorityAddr,
129143
)
@@ -264,6 +278,7 @@ func TestAllocateTokensTruncation(t *testing.T) {
264278
bankKeeper,
265279
stakingKeeper,
266280
poolKeeper,
281+
testCometService,
267282
"fee_collector",
268283
authorityAddr,
269284
)

x/distribution/keeper/delegation_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func TestCalculateRewardsBasic(t *testing.T) {
5454
bankKeeper,
5555
stakingKeeper,
5656
poolKeeper,
57+
testCometService,
5758
"fee_collector",
5859
authorityAddr,
5960
)
@@ -167,6 +168,7 @@ func TestCalculateRewardsAfterSlash(t *testing.T) {
167168
bankKeeper,
168169
stakingKeeper,
169170
poolKeeper,
171+
testCometService,
170172
"fee_collector",
171173
authorityAddr,
172174
)
@@ -283,6 +285,7 @@ func TestCalculateRewardsAfterManySlashes(t *testing.T) {
283285
bankKeeper,
284286
stakingKeeper,
285287
poolKeeper,
288+
testCometService,
286289
"fee_collector",
287290
authorityAddr,
288291
)
@@ -420,6 +423,7 @@ func TestCalculateRewardsMultiDelegator(t *testing.T) {
420423
bankKeeper,
421424
stakingKeeper,
422425
poolKeeper,
426+
testCometService,
423427
"fee_collector",
424428
authorityAddr,
425429
)
@@ -530,6 +534,7 @@ func TestWithdrawDelegationRewardsBasic(t *testing.T) {
530534
bankKeeper,
531535
stakingKeeper,
532536
poolKeeper,
537+
testCometService,
533538
"fee_collector",
534539
authorityAddr,
535540
)
@@ -618,6 +623,7 @@ func TestCalculateRewardsAfterManySlashesInSameBlock(t *testing.T) {
618623
bankKeeper,
619624
stakingKeeper,
620625
poolKeeper,
626+
testCometService,
621627
"fee_collector",
622628
authorityAddr,
623629
)
@@ -747,6 +753,7 @@ func TestCalculateRewardsMultiDelegatorMultiSlash(t *testing.T) {
747753
bankKeeper,
748754
stakingKeeper,
749755
poolKeeper,
756+
testCometService,
750757
"fee_collector",
751758
authorityAddr,
752759
)
@@ -900,6 +907,7 @@ func TestCalculateRewardsMultiDelegatorMultWithdraw(t *testing.T) {
900907
bankKeeper,
901908
stakingKeeper,
902909
poolKeeper,
910+
testCometService,
903911
"fee_collector",
904912
authorityAddr,
905913
)
@@ -1114,6 +1122,7 @@ func Test100PercentCommissionReward(t *testing.T) {
11141122
bankKeeper,
11151123
stakingKeeper,
11161124
poolKeeper,
1125+
testCometService,
11171126
"fee_collector",
11181127
authorityAddr,
11191128
)

x/distribution/keeper/keeper.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"cosmossdk.io/collections"
99
collcodec "cosmossdk.io/collections/codec"
1010
"cosmossdk.io/core/appmodule"
11+
"cosmossdk.io/core/comet"
1112
"cosmossdk.io/core/event"
1213
errorsmod "cosmossdk.io/errors"
1314
"cosmossdk.io/x/distribution/types"
@@ -21,6 +22,8 @@ import (
2122
type Keeper struct {
2223
appmodule.Environment
2324

25+
cometService comet.Service
26+
2427
cdc codec.BinaryCodec
2528
authKeeper types.AccountKeeper
2629
bankKeeper types.BankKeeper
@@ -57,8 +60,13 @@ type Keeper struct {
5760

5861
// NewKeeper creates a new distribution Keeper instance
5962
func NewKeeper(
60-
cdc codec.BinaryCodec, env appmodule.Environment,
61-
ak types.AccountKeeper, bk types.BankKeeper, sk types.StakingKeeper, pk types.PoolKeeper,
63+
cdc codec.BinaryCodec,
64+
env appmodule.Environment,
65+
ak types.AccountKeeper,
66+
bk types.BankKeeper,
67+
sk types.StakingKeeper,
68+
pk types.PoolKeeper,
69+
cometService comet.Service,
6270
feeCollectorName, authority string,
6371
) Keeper {
6472
// ensure distribution module account is set
@@ -69,6 +77,7 @@ func NewKeeper(
6977
sb := collections.NewSchemaBuilder(env.KVStoreService)
7078
k := Keeper{
7179
Environment: env,
80+
cometService: cometService,
7281
cdc: cdc,
7382
authKeeper: ak,
7483
bankKeeper: bk,

x/distribution/keeper/keeper_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ func initFixture(t *testing.T) (sdk.Context, []sdk.AccAddress, keeper.Keeper, de
7070
bankKeeper,
7171
stakingKeeper,
7272
poolKeeper,
73+
testCometService,
7374
"fee_collector",
7475
authorityAddr,
7576
)

x/distribution/migrations/v4/migrate_funds_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/golang/mock/gomock"
88
"github.com/stretchr/testify/require"
99

10+
"cosmossdk.io/core/comet"
1011
"cosmossdk.io/log"
1112
storetypes "cosmossdk.io/store/types"
1213
"cosmossdk.io/x/auth"
@@ -31,6 +32,13 @@ import (
3132
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
3233
)
3334

35+
type emptyCometService struct{}
36+
37+
// CometInfo implements comet.Service.
38+
func (e *emptyCometService) CometInfo(context.Context) comet.Info {
39+
return comet.Info{}
40+
}
41+
3442
func TestFundsMigration(t *testing.T) {
3543
keys := storetypes.NewKVStoreKeys(
3644
authtypes.StoreKey, banktypes.StoreKey, disttypes.StoreKey,
@@ -90,6 +98,7 @@ func TestFundsMigration(t *testing.T) {
9098
bankKeeper,
9199
stakingKeeper,
92100
poolKeeper,
101+
&emptyCometService{},
93102
disttypes.ModuleName,
94103
authority,
95104
)

0 commit comments

Comments
 (0)