Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
sontrinh16 committed Jun 3, 2024
2 parents f761eb1 + d1aab15 commit 13cd0df
Show file tree
Hide file tree
Showing 34 changed files with 351 additions and 62 deletions.
8 changes: 0 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -454,14 +454,6 @@ jobs:
run: |
cd simapp
go test -mod=readonly -timeout 30m -tags='app_v1 norace ledger test_ledger_mock' ./...
- name: sonarcloud
if: ${{ env.GIT_DIFF && !github.event.pull_request.draft && env.SONAR_TOKEN != null }}
uses: SonarSource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
with:
projectBaseDir: simapp/
test-collections:
runs-on: ubuntu-latest
Expand Down
16 changes: 0 additions & 16 deletions simapp/sonar-project.properties

This file was deleted.

7 changes: 7 additions & 0 deletions tests/integration/bank/keeper/deterministic_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
"context"
"testing"

"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -84,6 +85,12 @@ func initDeterministicFixture(t *testing.T) *deterministicFixture {
// gomock initializations
ctrl := gomock.NewController(t)
acctsModKeeper := authtestutil.NewMockAccountsModKeeper(ctrl)
accNum := uint64(0)
acctsModKeeper.EXPECT().NextAccountNumber(gomock.Any()).AnyTimes().DoAndReturn(func(ctx context.Context) (uint64, error) {
currentNum := accNum
accNum++
return currentNum, nil
})

accountKeeper := authkeeper.NewAccountKeeper(
runtime.NewEnvironment(runtime.NewKVStoreService(keys[authtypes.StoreKey]), log.NewNopLogger()),
Expand Down
7 changes: 7 additions & 0 deletions tests/integration/distribution/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
"context"
"fmt"
"testing"

Expand Down Expand Up @@ -95,6 +96,12 @@ func initFixture(t *testing.T) *fixture {
// gomock initializations
ctrl := gomock.NewController(t)
acctsModKeeper := authtestutil.NewMockAccountsModKeeper(ctrl)
accNum := uint64(0)
acctsModKeeper.EXPECT().NextAccountNumber(gomock.Any()).AnyTimes().DoAndReturn(func(ctx context.Context) (uint64, error) {
currentNum := accNum
accNum++
return currentNum, nil
})

accountKeeper := authkeeper.NewAccountKeeper(
runtime.NewEnvironment(runtime.NewKVStoreService(keys[authtypes.StoreKey]), log.NewNopLogger()),
Expand Down
6 changes: 6 additions & 0 deletions tests/integration/evidence/keeper/infraction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ func initFixture(tb testing.TB) *fixture {
// gomock initializations
ctrl := gomock.NewController(tb)
acctsModKeeper := authtestutil.NewMockAccountsModKeeper(ctrl)
accNum := uint64(0)
acctsModKeeper.EXPECT().NextAccountNumber(gomock.Any()).AnyTimes().DoAndReturn(func(ctx context.Context) (uint64, error) {
currentNum := accNum
accNum++
return currentNum, nil
})

maccPerms := map[string][]string{
pooltypes.ModuleName: {},
Expand Down
13 changes: 13 additions & 0 deletions tests/integration/example/example_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package integration_test

import (
"context"
"fmt"
"io"
"testing"
Expand Down Expand Up @@ -49,6 +50,12 @@ func Example() {
// gomock initializations
ctrl := gomock.NewController(&testing.T{})
acctsModKeeper := authtestutil.NewMockAccountsModKeeper(ctrl)
accNum := uint64(0)
acctsModKeeper.EXPECT().NextAccountNumber(gomock.Any()).AnyTimes().DoAndReturn(func(ctx context.Context) (uint64, error) {
currentNum := accNum
accNum++
return currentNum, nil
})

accountKeeper := authkeeper.NewAccountKeeper(
runtime.NewEnvironment(runtime.NewKVStoreService(keys[authtypes.StoreKey]), log.NewNopLogger()),
Expand Down Expand Up @@ -147,6 +154,12 @@ func Example_oneModule() {
// gomock initializations
ctrl := gomock.NewController(&testing.T{})
acctsModKeeper := authtestutil.NewMockAccountsModKeeper(ctrl)
accNum := uint64(0)
acctsModKeeper.EXPECT().NextAccountNumber(gomock.Any()).AnyTimes().DoAndReturn(func(ctx context.Context) (uint64, error) {
currentNum := accNum
accNum++
return currentNum, nil
})

accountKeeper := authkeeper.NewAccountKeeper(
runtime.NewEnvironment(runtime.NewKVStoreService(keys[authtypes.StoreKey]), log.NewNopLogger()),
Expand Down
7 changes: 7 additions & 0 deletions tests/integration/gov/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
"context"
"testing"

"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -77,6 +78,12 @@ func initFixture(tb testing.TB) *fixture {
// gomock initializations
ctrl := gomock.NewController(tb)
acctsModKeeper := authtestutil.NewMockAccountsModKeeper(ctrl)
accNum := uint64(0)
acctsModKeeper.EXPECT().NextAccountNumber(gomock.Any()).AnyTimes().DoAndReturn(func(ctx context.Context) (uint64, error) {
currentNum := accNum
accNum++
return currentNum, nil
})

accountKeeper := authkeeper.NewAccountKeeper(
runtime.NewEnvironment(runtime.NewKVStoreService(keys[authtypes.StoreKey]), log.NewNopLogger()),
Expand Down
9 changes: 8 additions & 1 deletion tests/integration/slashing/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
"context"
"testing"
"time"

Expand Down Expand Up @@ -81,8 +82,14 @@ func initFixture(tb testing.TB) *fixture {
queryRouter := baseapp.NewGRPCQueryRouter()

// gomock initializations
ctrl := gomock.NewController(&testing.T{})
ctrl := gomock.NewController(tb)
acctsModKeeper := authtestutil.NewMockAccountsModKeeper(ctrl)
accNum := uint64(0)
acctsModKeeper.EXPECT().NextAccountNumber(gomock.Any()).AnyTimes().DoAndReturn(func(ctx context.Context) (uint64, error) {
currentNum := accNum
accNum++
return currentNum, nil
})

accountKeeper := authkeeper.NewAccountKeeper(
runtime.NewEnvironment(runtime.NewKVStoreService(keys[authtypes.StoreKey]), log.NewNopLogger(), runtime.EnvWithQueryRouterService(queryRouter), runtime.EnvWithMsgRouterService(msgRouter)),
Expand Down
9 changes: 8 additions & 1 deletion tests/integration/staking/keeper/common_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
"context"
"math/big"
"testing"

Expand Down Expand Up @@ -134,7 +135,7 @@ func initFixture(tb testing.TB) *fixture {
}

// gomock initializations
ctrl := gomock.NewController(&testing.T{})
ctrl := gomock.NewController(tb)
acctsModKeeper := authtestutil.NewMockAccountsModKeeper(ctrl)

accountKeeper := authkeeper.NewAccountKeeper(
Expand Down Expand Up @@ -187,6 +188,12 @@ func initFixture(tb testing.TB) *fixture {

// set default staking params
assert.NilError(tb, stakingKeeper.Params.Set(sdkCtx, types.DefaultParams()))
accNum := uint64(0)
acctsModKeeper.EXPECT().NextAccountNumber(gomock.Any()).AnyTimes().DoAndReturn(func(ctx context.Context) (uint64, error) {
currentNum := accNum
accNum++
return currentNum, nil
})

f := fixture{
app: integrationApp,
Expand Down
7 changes: 7 additions & 0 deletions tests/integration/staking/keeper/deterministic_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
"context"
"testing"
"time"

Expand Down Expand Up @@ -92,6 +93,12 @@ func initDeterministicFixture(t *testing.T) *deterministicFixture {
// gomock initializations
ctrl := gomock.NewController(t)
acctsModKeeper := authtestutil.NewMockAccountsModKeeper(ctrl)
accNum := uint64(0)
acctsModKeeper.EXPECT().NextAccountNumber(gomock.Any()).AnyTimes().DoAndReturn(func(ctx context.Context) (uint64, error) {
currentNum := accNum
accNum++
return currentNum, nil
})

accountKeeper := authkeeper.NewAccountKeeper(
runtime.NewEnvironment(runtime.NewKVStoreService(keys[authtypes.StoreKey]), log.NewNopLogger()),
Expand Down
24 changes: 24 additions & 0 deletions x/accounts/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,30 @@ func (k Keeper) IsAccountsModuleAccount(
return hasAcc
}

func (k Keeper) NextAccountNumber(
ctx context.Context,
) (accNum uint64, err error) {
accNum, err = k.AccountNumber.Next(ctx)
if err != nil {
return 0, err
}

return accNum, nil
}

// InitAccountNumberSeqUnsafe use to set accounts account number tracking.
// Only use for account number migration.
func (k Keeper) InitAccountNumberSeqUnsafe(ctx context.Context, accNum uint64) error {
currentNum, err := k.AccountNumber.Peek(ctx)
if err != nil {
return err
}
if currentNum > accNum {
return fmt.Errorf("cannot set number lower than current account number got %v while current account number is %v", accNum, currentNum)
}
return k.AccountNumber.Set(ctx, accNum)
}

// Init creates a new account of the given type.
func (k Keeper) Init(
ctx context.Context,
Expand Down
7 changes: 7 additions & 0 deletions x/auth/ante/testutil_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ante_test

import (
"context"
"testing"

abci "github.com/cometbft/cometbft/abci/types"
Expand Down Expand Up @@ -79,6 +80,12 @@ func SetupTestSuite(t *testing.T, isCheckTx bool) *AnteTestSuite {
suite.ctx = testCtx.Ctx.WithIsCheckTx(isCheckTx).WithBlockHeight(1)
suite.encCfg = moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}, auth.AppModule{})

accNum := uint64(0)
suite.acctsModKeeper.EXPECT().NextAccountNumber(gomock.Any()).AnyTimes().DoAndReturn(func(ctx context.Context) (uint64, error) {
currNum := accNum
accNum++
return currNum, nil
})
maccPerms := map[string][]string{
"fee_collector": nil,
"mint": {"minter"},
Expand Down
7 changes: 6 additions & 1 deletion x/auth/keeper/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ func (ak AccountKeeper) NewAccountWithAddress(ctx context.Context, addr sdk.AccA

// NewAccount sets the next account number to a given account interface
func (ak AccountKeeper) NewAccount(ctx context.Context, acc sdk.AccountI) sdk.AccountI {
if err := acc.SetAccountNumber(ak.NextAccountNumber(ctx)); err != nil {
accNum, err := ak.AccountsModKeeper.NextAccountNumber(ctx)
if err != nil {
panic(err)
}

if err := acc.SetAccountNumber(accNum); err != nil {
panic(err)
}

Expand Down
7 changes: 7 additions & 0 deletions x/auth/keeper/deterministic_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
"context"
"encoding/hex"
"sort"
"sync/atomic"
Expand Down Expand Up @@ -69,6 +70,12 @@ func (suite *DeterministicTestSuite) SetupTest() {
ctrl := gomock.NewController(suite.T())
acctsModKeeper := authtestutil.NewMockAccountsModKeeper(ctrl)
suite.acctsModKeeper = acctsModKeeper
accNum := uint64(0)
suite.acctsModKeeper.EXPECT().NextAccountNumber(gomock.Any()).AnyTimes().DoAndReturn(func(ctx context.Context) (uint64, error) {
currNum := accNum
accNum++
return currNum, nil
})

maccPerms := map[string][]string{
"fee_collector": nil,
Expand Down
5 changes: 4 additions & 1 deletion x/auth/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ func (ak AccountKeeper) InitGenesis(ctx context.Context, data types.GenesisState
for _, acc := range accounts {
accNum := acc.GetAccountNumber()
for lastAccNum == nil || *lastAccNum < accNum {
n := ak.NextAccountNumber(ctx)
n, err := ak.AccountsModKeeper.NextAccountNumber(ctx)
if err != nil {
return err
}
lastAccNum = &n
}
ak.SetAccount(ctx, acc)
Expand Down
13 changes: 8 additions & 5 deletions x/auth/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type AccountKeeperI interface {
GetSequence(context.Context, sdk.AccAddress) (uint64, error)

// Fetch the next account number, and increment the internal counter.
//
// Deprecated: keep this to avoid breaking api
NextAccountNumber(context.Context) uint64

// GetModulePermissions fetches per-module account permissions
Expand Down Expand Up @@ -97,9 +99,9 @@ type AccountKeeper struct {
authority string

// State
Schema collections.Schema
Params collections.Item[types.Params]
AccountNumber collections.Sequence
Schema collections.Schema
Params collections.Item[types.Params]

// Accounts key: AccAddr | value: AccountI | index: AccountsIndex
Accounts *collections.IndexedMap[sdk.AccAddress, sdk.AccountI, AccountsIndexes]
}
Expand Down Expand Up @@ -133,7 +135,6 @@ func NewAccountKeeper(
permAddrs: permAddrs,
authority: authority,
Params: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[types.Params](cdc)),
AccountNumber: collections.NewSequence(sb, types.GlobalAccountNumberKey, "account_number"),
Accounts: collections.NewIndexedMap(sb, types.AddressStoreKeyPrefix, "accounts", sdk.AccAddressKey, codec.CollInterfaceValue[sdk.AccountI](cdc), NewAccountIndexes(sb)),
}
schema, err := sb.Build()
Expand Down Expand Up @@ -181,8 +182,10 @@ func (ak AccountKeeper) GetSequence(ctx context.Context, addr sdk.AccAddress) (u

// NextAccountNumber returns and increments the global account number counter.
// If the global account number is not set, it initializes it with value 0.
//
// Deprecated: NextAccountNumber is deprecated
func (ak AccountKeeper) NextAccountNumber(ctx context.Context) uint64 {
n, err := ak.AccountNumber.Next(ctx)
n, err := ak.AccountsModKeeper.NextAccountNumber(ctx)
if err != nil {
panic(err)
}
Expand Down
13 changes: 11 additions & 2 deletions x/auth/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
"context"
"testing"

"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -62,6 +63,12 @@ func (suite *KeeperTestSuite) SetupTest() {
ctrl := gomock.NewController(suite.T())
acctsModKeeper := authtestutil.NewMockAccountsModKeeper(ctrl)
suite.acctsModKeeper = acctsModKeeper
accNum := uint64(0)
suite.acctsModKeeper.EXPECT().NextAccountNumber(gomock.Any()).AnyTimes().DoAndReturn(func(ctx context.Context) (uint64, error) {
currNum := accNum
accNum++
return currNum, nil
})

maccPerms := map[string][]string{
"fee_collector": nil,
Expand Down Expand Up @@ -202,7 +209,8 @@ func (suite *KeeperTestSuite) TestInitGenesis() {
suite.Require().Equal(6, int(feeCollector.GetAccountNumber()))

// The 3rd account has account number 5, but because the FeeCollector account gets initialized last, the next should be 7.
nextNum := suite.accountKeeper.NextAccountNumber(ctx)
nextNum, err := suite.accountKeeper.AccountsModKeeper.NextAccountNumber(ctx)
suite.Require().NoError(err)
suite.Require().Equal(7, int(nextNum))

suite.SetupTest() // reset
Expand Down Expand Up @@ -237,7 +245,8 @@ func (suite *KeeperTestSuite) TestInitGenesis() {
feeCollector = suite.accountKeeper.GetModuleAccount(ctx, "fee_collector")
suite.Require().Equal(1, int(feeCollector.GetAccountNumber()))

nextNum = suite.accountKeeper.NextAccountNumber(ctx)
nextNum, err = suite.accountKeeper.AccountsModKeeper.NextAccountNumber(ctx)
suite.Require().NoError(err)
// we expect nextNum to be 2 because we initialize fee_collector as account number 1
suite.Require().Equal(2, int(nextNum))
}
Loading

0 comments on commit 13cd0df

Please sign in to comment.