Skip to content

Commit

Permalink
fix: Tier module fixes and improvements (part 2) (#8)
Browse files Browse the repository at this point in the history
* Add creation_height to UnlockingLockupRequest message

* Generate proto

* Add CreateValidator helper function

* Add CreateTestEncodingConfig helper, move CreateTestValidator to utils

* Add mock and tx dependencies

* Add mock kepers for testing

* Add DistributionKeeper to expected keepers

* Add DistributionKeeper to tier module

* Improve and fix logic in TierKeeper testutil

* Add UnlockingLockupKey and LockupKeyToAddressesAtHeight to handle unlocking lockup keys separately

* Add comments related to reward rate precision

* Add MustIterateUnlockingLockups, and related helpers, improve existing logic

* Add TestMustIterateUnlockingLockups and TestIterateLockups

* Add mock keeper tests for Lock/Unlock

* Add tests for keeper Lock, Unlock, Redelegate, and CompleteUnlocking

* Update grpc_query

* Fix grpc query tests

* Improve and fix logic in keeper methods

* Add EventTypeCancelUnlocking

* Add CancelUnlocking, fix Unlock and CompleteUnlocking logic

* Fix mock keeper tests

* Add TestCancelUnlocking, fix existing tests

* Add CancelUnlocking message, fix Unlock message

* Fix comments

* Add comments and minor fixes to keeper tests

* Fix comment

* Fix typo

* Add mock TestRedelegate, improve tests

* Refactor SetupKeeper() util

* Validate correct address and amount in MintCredit(), add Test_MintCredit

* Update codecov.yml

* fix: Tier module fixes and improvements (part 3) (#10)

* Add tier module events and attributes

* Emit corresponding events on Lock, Unlock, Redelegate, and CompleteUnlocking

* Improve CancelUnlocking to search for unbonding delegation entry by creationHeight

* Add IterateValidators and TotalBondedTokens to expected staking keeper, define corresponding mocks

* Fix calculation bug in TotalAmountByAddr caused by delAddr shadowing, add TestTotalAmountByAddr

* Fix SubtractLockup to remove lockup if subtracting the whole locked amount, add TestHasLockup and TestGetUnlockingLockup

* Refactor calculateCredit and related tests

* Add SaveLockup, refactor SetLockup, fix GetLockup and removeUnlockingLockup

* Add TestGetLockup and TestGetLockups, fix/improve existing tests

* Use SaveLockup instead of SetLockup in InitGenesis

* Update grpc query tests

* Improve CancelUnlocking to support partial unlocks and fix bug in existing logic

* Improve TestCancelUnlocking to verify subsequent and partial unlocks, fix/update existing tests

* Improve SubtractLockup and SubtractUnlockingLockup logic to handle invalid amounts

* Add TestSubtractUnlockingLockup, improve TestSubtractLockup

* Improve Lock, Unlock, and Redelegate to handle invalid amounts, fix/improve related tests

* Add ErrInvalidAmount

* Fix typo in ErrUnauthorized, add setupMsgServer and basic TestMsgServer

* Add TestMsgUpdateParams

* Add TestMsgLock

* Add TestMsgUnlock

* Add TestMsgRedelegate

* Update TestMsgLock, TestMsgUnlock, and TestMsgUpdateParams, fix error messages

* Add TestMsgCancelUnlocking, minor fixes
  • Loading branch information
iverc authored Dec 18, 2024
1 parent e5f5cfe commit 24c0f5c
Show file tree
Hide file tree
Showing 35 changed files with 3,301 additions and 414 deletions.
258 changes: 158 additions & 100 deletions api/sourcehub/tier/v1beta1/query.pulsar.go

Large diffs are not rendered by default.

20 changes: 11 additions & 9 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
coverage:
ignore:
- "api/*"
- "data/*"
- "devnet/*"
- "docs/*"
- "example/*"
- "manual/*"
- "proto/*"
- "scripts/*"
- "testutil/*"
- "api/**"
- "data/**"
- "devnet/**"
- "docs/**"
- "example/**"
- "manual/**"
- "proto/**"
- "scripts/**"
- "testutil/**"
- "**/*.pb.go"
- "**/*.pb.gw.go"
- "**/*.pulsar.go"
- "**/*_test.go"
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
cosmossdk.io/x/circuit v0.1.0
cosmossdk.io/x/evidence v0.1.0
cosmossdk.io/x/feegrant v0.1.0
cosmossdk.io/x/tx v0.13.3
cosmossdk.io/x/upgrade v0.1.1
github.com/TBD54566975/ssi-sdk v0.0.4-alpha
github.com/bufbuild/buf v1.30.0
Expand All @@ -39,6 +40,7 @@ require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0
github.com/go-jose/go-jose/v3 v3.0.1-0.20221117193127-916db76e8214
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.4
github.com/google/go-cmp v0.6.0
github.com/gorilla/mux v1.8.1
Expand Down Expand Up @@ -70,7 +72,6 @@ require (
connectrpc.com/connect v1.15.0 // indirect
connectrpc.com/otelconnect v0.7.0 // indirect
cosmossdk.io/collections v0.4.0 // indirect
cosmossdk.io/x/tx v0.13.3 // indirect
filippo.io/edwards25519 v1.0.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/99designs/keyring v1.2.2 // indirect
Expand Down Expand Up @@ -152,7 +153,6 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v1.2.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/cel-go v0.20.1 // indirect
Expand Down
1 change: 1 addition & 0 deletions proto/sourcehub/tier/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ message LockupsResponse {
message UnlockingLockupRequest {
string delegator_address = 1;
string validator_address = 2;
int64 creation_height = 3;
}

// UnlockingLockupResponse is the response type for the Query/UnlockingLockup RPC method.
Expand Down
128 changes: 122 additions & 6 deletions testutil/keeper/tier.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package keeper

import (
"testing"
"time"

cryptocdc "github.com/cosmos/cosmos-sdk/crypto/codec"
appparams "github.com/sourcenetwork/sourcehub/app/params"

"cosmossdk.io/log"
"cosmossdk.io/store"
Expand All @@ -13,38 +17,150 @@ import (
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
authcodec "github.com/cosmos/cosmos-sdk/x/auth/codec"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/stretchr/testify/require"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
epochskeeper "github.com/sourcenetwork/sourcehub/x/epochs/keeper"
epochstypes "github.com/sourcenetwork/sourcehub/x/epochs/types"

distrkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
"github.com/sourcenetwork/sourcehub/x/tier/keeper"
"github.com/sourcenetwork/sourcehub/x/tier/types"
"github.com/stretchr/testify/require"
)

func TierKeeper(t testing.TB) (keeper.Keeper, sdk.Context) {
storeKey := storetypes.NewKVStoreKey(types.StoreKey)
authStoreKey := storetypes.NewKVStoreKey(authtypes.StoreKey)
bankStoreKey := storetypes.NewKVStoreKey(banktypes.StoreKey)
stakingStoreKey := storetypes.NewKVStoreKey(stakingtypes.StoreKey)
distrStoreKey := storetypes.NewKVStoreKey(distrtypes.StoreKey)
epochsStoreKey := storetypes.NewKVStoreKey(epochstypes.StoreKey)

db := dbm.NewMemDB()
stateStore := store.NewCommitMultiStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics())
stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db)
stateStore.MountStoreWithDB(authStoreKey, storetypes.StoreTypeIAVL, db)
stateStore.MountStoreWithDB(bankStoreKey, storetypes.StoreTypeIAVL, db)
stateStore.MountStoreWithDB(stakingStoreKey, storetypes.StoreTypeIAVL, db)
stateStore.MountStoreWithDB(distrStoreKey, storetypes.StoreTypeIAVL, db)
stateStore.MountStoreWithDB(epochsStoreKey, storetypes.StoreTypeIAVL, db)
require.NoError(t, stateStore.LoadLatestVersion())

registry := codectypes.NewInterfaceRegistry()
cryptocdc.RegisterInterfaces(registry)
authtypes.RegisterInterfaces(registry)
banktypes.RegisterInterfaces(registry)
stakingtypes.RegisterInterfaces(registry)
distrtypes.RegisterInterfaces(registry)

cdc := codec.NewProtoCodec(registry)
authority := authtypes.NewModuleAddress(govtypes.ModuleName)
bech32Prefix := "source"
addressCodec := authcodec.NewBech32Codec(bech32Prefix)
valOperCodec := authcodec.NewBech32Codec(bech32Prefix + "valoper")
valConsCodec := authcodec.NewBech32Codec(bech32Prefix + "valcons")

maccPerms := map[string][]string{
authtypes.FeeCollectorName: nil,
types.ModuleName: {authtypes.Minter, authtypes.Burner, authtypes.Staking},
stakingtypes.BondedPoolName: {authtypes.Burner, authtypes.Staking},
stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking},
distrtypes.ModuleName: {authtypes.Minter, authtypes.Burner},
}

authKeeper := authkeeper.NewAccountKeeper(
cdc,
runtime.NewKVStoreService(authStoreKey),
authtypes.ProtoBaseAccount,
maccPerms,
addressCodec,
bech32Prefix,
authority.String(),
)

blockedAddrs := make(map[string]bool)
for acc := range maccPerms {
blockedAddrs[authtypes.NewModuleAddress(acc).String()] = true
}
bankKeeper := bankkeeper.NewBaseKeeper(
cdc,
runtime.NewKVStoreService(bankStoreKey),
authKeeper,
blockedAddrs,
authority.String(),
log.NewNopLogger(),
)

ctx := sdk.NewContext(stateStore, cmtproto.Header{}, false, log.NewNopLogger())
ctx = ctx.WithBlockHeight(1).WithChainID("sourcehub").WithBlockTime(time.Unix(1000000000, 0))

appparams.RegisterDenoms(ctx, bankKeeper)

bondedPool := authtypes.NewEmptyModuleAccount(stakingtypes.BondedPoolName, authtypes.Burner, authtypes.Staking)
notBondedPool := authtypes.NewEmptyModuleAccount(stakingtypes.NotBondedPoolName, authtypes.Burner, authtypes.Staking)
tierPool := authtypes.NewEmptyModuleAccount(types.ModuleName, authtypes.Minter, authtypes.Burner)
distrPool := authtypes.NewEmptyModuleAccount(distrtypes.ModuleName)

if authKeeper.GetModuleAccount(ctx, stakingtypes.BondedPoolName) == nil {
authKeeper.SetModuleAccount(ctx, bondedPool)
}
if authKeeper.GetModuleAccount(ctx, stakingtypes.NotBondedPoolName) == nil {
authKeeper.SetModuleAccount(ctx, notBondedPool)
}
if authKeeper.GetModuleAccount(ctx, types.ModuleName) == nil {
authKeeper.SetModuleAccount(ctx, tierPool)
}
if authKeeper.GetModuleAccount(ctx, distrtypes.ModuleName) == nil {
authKeeper.SetModuleAccount(ctx, distrPool)
}

stakingKeeper := stakingkeeper.NewKeeper(
cdc,
runtime.NewKVStoreService(stakingStoreKey),
authKeeper,
bankKeeper,
authority.String(),
valOperCodec,
valConsCodec,
)

stakingParams := stakingtypes.DefaultParams()
stakingParams.BondDenom = appparams.DefaultBondDenom
require.NoError(t, stakingKeeper.SetParams(ctx, stakingParams))

distributionKeeper := distrkeeper.NewKeeper(
cdc,
runtime.NewKVStoreService(distrStoreKey),
authKeeper,
bankKeeper,
stakingKeeper,
authority.String(),
authority.String(),
)

epochsKeeper := epochskeeper.NewKeeper(
runtime.NewKVStoreService(epochsStoreKey),
log.NewNopLogger(),
)

k := keeper.NewKeeper(
cdc,
runtime.NewKVStoreService(storeKey),
log.NewNopLogger(),
authority.String(),
nil,
nil,
nil,
bankKeeper,
stakingKeeper,
epochsKeeper,
distributionKeeper,
)

ctx := sdk.NewContext(stateStore, cmtproto.Header{}, false, log.NewNopLogger())

// Initialize params
if err := k.SetParams(ctx, types.DefaultParams()); err != nil {
panic(err)
Expand Down
Loading

0 comments on commit 24c0f5c

Please sign in to comment.