Skip to content

Commit

Permalink
Merge branch 'osmo-v22/v0.47.5' into mattverse/bpt-1
Browse files Browse the repository at this point in the history
  • Loading branch information
mattverse authored Feb 6, 2024
2 parents c6e5d08 + 54cc86a commit aaa15a7
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 48 deletions.
1 change: 1 addition & 0 deletions .github/workflows/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ jobs:
steps:
- uses: actions/labeler@v4
with:
configuration-path: .github/labeler.yml
repo-token: "${{ secrets.GITHUB_TOKEN }}"
22 changes: 0 additions & 22 deletions .github/workflows/lint-pr.yml

This file was deleted.

10 changes: 1 addition & 9 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,4 @@ jobs:
if: env.GIT_DIFF
run: |
cd simapp
go test -mod=readonly -timeout 30m -tags='app_v1 norace ledger test_ledger_mock rocksdb_build' ./...
- name: sonarcloud
if: env.GIT_DIFF
uses: SonarSource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
with:
projectBaseDir: simapp/
go test -mod=readonly -timeout 30m -tags='app_v1 norace ledger test_ledger_mock rocksdb_build' ./...
7 changes: 3 additions & 4 deletions store/cachekv/search_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package cachekv
import (
"strconv"
"testing"

"github.com/cosmos/cosmos-sdk/store/cachekv/internal"
)

func BenchmarkLargeUnsortedMisses(b *testing.B) {
Expand Down Expand Up @@ -36,9 +34,10 @@ func generateStore() *Store {
cache[key] = &cValue{}
}

return &Store{
store := &Store{
cache: cache,
unsortedCache: unsorted,
sortedCache: internal.NewBTree(),
}
store.resetSortedCache()
return store
}
16 changes: 12 additions & 4 deletions store/cachekv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Store struct {
mtx sync.Mutex
cache map[string]*cValue
unsortedCache map[string]struct{}
sortedCache internal.BTree // always ascending sorted
sortedCache *internal.BTree // always ascending sorted
parent types.KVStore
}

Expand All @@ -39,7 +39,7 @@ func NewStore(parent types.KVStore) *Store {
return &Store{
cache: make(map[string]*cValue),
unsortedCache: make(map[string]struct{}),
sortedCache: internal.NewBTree(),
sortedCache: nil,
parent: parent,
}
}
Expand Down Expand Up @@ -93,13 +93,18 @@ func (store *Store) Delete(key []byte) {
store.setCacheValue(key, nil, true)
}

func (store *Store) resetSortedCache() {
newTree := internal.NewBTree()
store.sortedCache = &newTree
}

// Implements Cachetypes.KVStore.
func (store *Store) Write() {
store.mtx.Lock()
defer store.mtx.Unlock()

if len(store.cache) == 0 && len(store.unsortedCache) == 0 {
store.sortedCache = internal.NewBTree()
store.sortedCache = nil
return
}

Expand Down Expand Up @@ -140,7 +145,7 @@ func (store *Store) Write() {
for key := range store.unsortedCache {
delete(store.unsortedCache, key)
}
store.sortedCache = internal.NewBTree()
store.sortedCache = nil
}

// CacheWrap implements CacheWrapper.
Expand Down Expand Up @@ -169,6 +174,9 @@ func (store *Store) ReverseIterator(start, end []byte) types.Iterator {
func (store *Store) iterator(start, end []byte, ascending bool) types.Iterator {
store.mtx.Lock()
defer store.mtx.Unlock()
if store.sortedCache == nil {
store.resetSortedCache()
}

store.dirtyItems(start, end)
isoSortedCache := store.sortedCache.Copy()
Expand Down
2 changes: 1 addition & 1 deletion store/cachemulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func NewStore(
}

func newCacheMultiStoreFromCMS(cms Store) Store {
stores := make(map[types.StoreKey]types.CacheWrapper)
stores := make(map[types.StoreKey]types.CacheWrapper, len(cms.stores))
for k, v := range cms.stores {
stores[k] = v
}
Expand Down
10 changes: 9 additions & 1 deletion x/feegrant/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,17 +323,25 @@ func (k Keeper) addToFeeAllowanceQueue(ctx sdk.Context, grantKey []byte, exp *ti
store.Set(feegrant.FeeAllowancePrefixQueue(exp, grantKey), []byte{})
}

// NOTE: backport from v50
// RemoveExpiredAllowances iterates grantsByExpiryQueue and deletes the expired grants.
func (k Keeper) RemoveExpiredAllowances(ctx sdk.Context) {
func (k Keeper) RemoveExpiredAllowances(ctx sdk.Context, limit int) {
exp := ctx.BlockTime()
store := ctx.KVStore(k.storeKey)
iterator := store.Iterator(feegrant.FeeAllowanceQueueKeyPrefix, sdk.InclusiveEndBytes(feegrant.AllowanceByExpTimeKey(&exp)))
defer iterator.Close()

count := 0
for ; iterator.Valid(); iterator.Next() {
store.Delete(iterator.Key())

granter, grantee := feegrant.ParseAddressesFromFeeAllowanceQueueKey(iterator.Key())
store.Delete(feegrant.FeeAllowanceKey(granter, grantee))

// limit the amount of iterations to avoid taking too much time
count++
if count == limit {
break
}
}
}
46 changes: 40 additions & 6 deletions x/feegrant/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,17 @@ func TestKeeperTestSuite(t *testing.T) {
}

func (suite *KeeperTestSuite) SetupTest() {
suite.addrs = simtestutil.CreateIncrementalAccounts(4)
suite.addrs = simtestutil.CreateIncrementalAccounts(20)
key := sdk.NewKVStoreKey(feegrant.StoreKey)
testCtx := testutil.DefaultContextWithDB(suite.T(), key, sdk.NewTransientStoreKey("transient_test"))
encCfg := moduletestutil.MakeTestEncodingConfig(module.AppModuleBasic{})

// setup gomock and initialize some globally expected executions
ctrl := gomock.NewController(suite.T())
suite.accountKeeper = feegranttestutil.NewMockAccountKeeper(ctrl)
suite.accountKeeper.EXPECT().GetAccount(gomock.Any(), suite.addrs[0]).Return(authtypes.NewBaseAccountWithAddress(suite.addrs[0])).AnyTimes()
suite.accountKeeper.EXPECT().GetAccount(gomock.Any(), suite.addrs[1]).Return(authtypes.NewBaseAccountWithAddress(suite.addrs[1])).AnyTimes()
suite.accountKeeper.EXPECT().GetAccount(gomock.Any(), suite.addrs[2]).Return(authtypes.NewBaseAccountWithAddress(suite.addrs[2])).AnyTimes()
suite.accountKeeper.EXPECT().GetAccount(gomock.Any(), suite.addrs[3]).Return(authtypes.NewBaseAccountWithAddress(suite.addrs[3])).AnyTimes()
for i := 0; i < len(suite.addrs); i++ {
suite.accountKeeper.EXPECT().GetAccount(gomock.Any(), suite.addrs[i]).Return(authtypes.NewBaseAccountWithAddress(suite.addrs[i])).AnyTimes()
}

suite.feegrantKeeper = keeper.NewKeeper(encCfg.Codec, key, suite.accountKeeper)
suite.ctx = testCtx.Ctx
Expand Down Expand Up @@ -447,7 +446,7 @@ func (suite *KeeperTestSuite) TestPruneGrants() {
}
err := suite.feegrantKeeper.GrantAllowance(suite.ctx, tc.granter, tc.grantee, tc.allowance)
suite.NoError(err)
suite.feegrantKeeper.RemoveExpiredAllowances(tc.ctx)
suite.feegrantKeeper.RemoveExpiredAllowances(tc.ctx, 5)
grant, err := suite.feegrantKeeper.GetAllowance(tc.ctx, tc.granter, tc.grantee)
if tc.expErrMsg != "" {
suite.Error(err)
Expand All @@ -461,3 +460,38 @@ func (suite *KeeperTestSuite) TestPruneGrants() {
})
}
}

func (suite *KeeperTestSuite) TestPruneGrantsEdgecases() {
eth := sdk.NewCoins(sdk.NewInt64Coin("eth", 123))
now := suite.ctx.BlockTime()
oneYearExpiry := now.AddDate(1, 0, 0)

granter := suite.addrs[1]
grantee1 := suite.addrs[2]
err := suite.feegrantKeeper.GrantAllowance(suite.ctx, granter, grantee1, &feegrant.BasicAllowance{
SpendLimit: eth,
Expiration: &oneYearExpiry,
})
suite.NoError(err)

grantee2 := suite.addrs[3]
err = suite.feegrantKeeper.GrantAllowance(suite.ctx, granter, grantee2, &feegrant.BasicAllowance{
SpendLimit: eth,
Expiration: &now,
})
suite.NoError(err)

// expect 2 active grants
grantsBeforePrune, err := suite.feegrantKeeper.AllowancesByGranter(suite.ctx, &feegrant.QueryAllowancesByGranterRequest{Granter: granter.String()})
suite.NoError(err)
suite.Len(grantsBeforePrune.Allowances, 2)

// use blocktime that would result in both grants being expired
expireCtx := suite.ctx.WithBlockTime(now.AddDate(1, 0, 1))

// expect 1 grant to be removed due to the imposed limit
suite.feegrantKeeper.RemoveExpiredAllowances(expireCtx, 1)
grantsAfterPrune, err := suite.feegrantKeeper.AllowancesByGranter(suite.ctx, &feegrant.QueryAllowancesByGranterRequest{Granter: granter.String()})
suite.NoError(err)
suite.Len(grantsAfterPrune.Allowances, 1)
}
2 changes: 1 addition & 1 deletion x/feegrant/module/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import (
)

func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
k.RemoveExpiredAllowances(ctx)
k.RemoveExpiredAllowances(ctx, 200)
}

0 comments on commit aaa15a7

Please sign in to comment.