Skip to content

Commit

Permalink
change keeper dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
gitferry committed Aug 7, 2024
1 parent f027959 commit c19e2e3
Show file tree
Hide file tree
Showing 31 changed files with 190 additions and 223 deletions.
2 changes: 1 addition & 1 deletion app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,6 @@ func (ak *AppKeepers) InitKeepers(
runtime.NewKVStoreService(keys[btcstakingtypes.StoreKey]),
&btclightclientKeeper,
&btcCheckpointKeeper,
&checkpointingKeeper,
btcNetParams,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
Expand All @@ -535,6 +534,7 @@ func (ak *AppKeepers) InitKeepers(
runtime.NewKVStoreService(keys[finalitytypes.StoreKey]),
ak.BTCStakingKeeper,
ak.IncentiveKeeper,
ak.CheckpointingKeeper,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
ak.BTCStakingKeeper = *ak.BTCStakingKeeper.SetHooks(btcstakingtypes.NewMultiBtcStakingHooks(ak.FinalityKeeper.Hooks()))
Expand Down
2 changes: 0 additions & 2 deletions testutil/keeper/btcstaking.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func BTCStakingKeeper(
t testing.TB,
btclcKeeper types.BTCLightClientKeeper,
btccKeeper types.BtcCheckpointKeeper,
ckptKeeper types.CheckpointingKeeper,
) (*keeper.Keeper, sdk.Context) {
storeKey := storetypes.NewKVStoreKey(types.StoreKey)

Expand All @@ -44,7 +43,6 @@ func BTCStakingKeeper(
runtime.NewKVStoreService(storeKey),
btclcKeeper,
btccKeeper,
ckptKeeper,
&chaincfg.SimNetParams,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
Expand Down
3 changes: 2 additions & 1 deletion testutil/keeper/finality.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/babylonlabs-io/babylon/x/finality/types"
)

func FinalityKeeper(t testing.TB, bsKeeper types.BTCStakingKeeper, iKeeper types.IncentiveKeeper) (*keeper.Keeper, sdk.Context) {
func FinalityKeeper(t testing.TB, bsKeeper types.BTCStakingKeeper, iKeeper types.IncentiveKeeper, cKeeper types.CheckpointingKeeper) (*keeper.Keeper, sdk.Context) {
storeKey := storetypes.NewKVStoreKey(types.StoreKey)

db := dbm.NewMemDB()
Expand All @@ -38,6 +38,7 @@ func FinalityKeeper(t testing.TB, bsKeeper types.BTCStakingKeeper, iKeeper types
runtime.NewKVStoreService(storeKey),
bsKeeper,
iKeeper,
cKeeper,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

Expand Down
5 changes: 3 additions & 2 deletions x/btcstaking/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package btcstaking_test
import (
"testing"

"github.com/stretchr/testify/require"

keepertest "github.com/babylonlabs-io/babylon/testutil/keeper"
"github.com/babylonlabs-io/babylon/testutil/nullify"
"github.com/babylonlabs-io/babylon/x/btcstaking"
"github.com/babylonlabs-io/babylon/x/btcstaking/types"
"github.com/stretchr/testify/require"
)

func TestGenesis(t *testing.T) {
Expand All @@ -16,7 +17,7 @@ func TestGenesis(t *testing.T) {
Params: []*types.Params{&p},
}

k, ctx := keepertest.BTCStakingKeeper(t, nil, nil, nil)
k, ctx := keepertest.BTCStakingKeeper(t, nil, nil)
btcstaking.InitGenesis(ctx, *k, genesisState)
got := btcstaking.ExportGenesis(ctx, *k)
require.NotNil(t, got)
Expand Down
6 changes: 3 additions & 3 deletions x/btcstaking/keeper/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
"testing"
"time"

"github.com/golang/mock/gomock"

"github.com/babylonlabs-io/babylon/testutil/datagen"
btclctypes "github.com/babylonlabs-io/babylon/x/btclightclient/types"
bsmodule "github.com/babylonlabs-io/babylon/x/btcstaking"
"github.com/babylonlabs-io/babylon/x/btcstaking/types"
"github.com/golang/mock/gomock"
)

func benchBeginBlock(b *testing.B, numFPs int, numDelsUnderFP int) {
Expand All @@ -23,8 +24,7 @@ func benchBeginBlock(b *testing.B, numFPs int, numDelsUnderFP int) {
defer ctrl.Finish()
btclcKeeper := types.NewMockBTCLightClientKeeper(ctrl)
btccKeeper := types.NewMockBtcCheckpointKeeper(ctrl)
ckptKeeper := types.NewMockCheckpointingKeeper(ctrl)
h := NewHelper(b, btclcKeeper, btccKeeper, ckptKeeper)
h := NewHelper(b, btclcKeeper, btccKeeper)
// set all parameters
covenantSKs, _ := h.GenAndApplyParams(r)
changeAddress, err := datagen.GenRandomBTCAddress(r, h.Net)
Expand Down
7 changes: 4 additions & 3 deletions x/btcstaking/keeper/btc_height_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"math/rand"
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"

"github.com/babylonlabs-io/babylon/testutil/datagen"
keepertest "github.com/babylonlabs-io/babylon/testutil/keeper"
btclctypes "github.com/babylonlabs-io/babylon/x/btclightclient/types"
"github.com/babylonlabs-io/babylon/x/btcstaking/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
)

func FuzzBTCHeightIndex(f *testing.F) {
Expand All @@ -22,7 +23,7 @@ func FuzzBTCHeightIndex(f *testing.F) {

// mock BTC light client
btclcKeeper := types.NewMockBTCLightClientKeeper(ctrl)
keeper, ctx := keepertest.BTCStakingKeeper(t, btclcKeeper, nil, nil)
keeper, ctx := keepertest.BTCStakingKeeper(t, btclcKeeper, nil)

// randomise Babylon height and BTC height
babylonHeight := datagen.RandomInt(r, 100)
Expand Down
19 changes: 8 additions & 11 deletions x/btcstaking/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func FuzzActivatedHeight(f *testing.F) {
r := rand.New(rand.NewSource(seed))

// Setup keeper and context
keeper, ctx := testkeeper.BTCStakingKeeper(t, nil, nil, nil)
keeper, ctx := testkeeper.BTCStakingKeeper(t, nil, nil)
ctx = sdk.UnwrapSDKContext(ctx)

// not activated yet
Expand All @@ -51,7 +51,7 @@ func FuzzFinalityProviders(f *testing.F) {
r := rand.New(rand.NewSource(seed))

// Setup keeper and context
keeper, ctx := testkeeper.BTCStakingKeeper(t, nil, nil, nil)
keeper, ctx := testkeeper.BTCStakingKeeper(t, nil, nil)
ctx = sdk.UnwrapSDKContext(ctx)

// Generate random finality providers and add them to kv store
Expand Down Expand Up @@ -116,7 +116,7 @@ func FuzzFinalityProvider(f *testing.F) {
f.Fuzz(func(t *testing.T, seed int64) {
r := rand.New(rand.NewSource(seed))
// Setup keeper and context
keeper, ctx := testkeeper.BTCStakingKeeper(t, nil, nil, nil)
keeper, ctx := testkeeper.BTCStakingKeeper(t, nil, nil)
ctx = sdk.UnwrapSDKContext(ctx)

// Generate random finality providers and add them to kv store
Expand Down Expand Up @@ -172,8 +172,7 @@ func FuzzPendingBTCDelegations(f *testing.F) {
btclcKeeper := types.NewMockBTCLightClientKeeper(ctrl)
btccKeeper := types.NewMockBtcCheckpointKeeper(ctrl)
btccKeeper.EXPECT().GetParams(gomock.Any()).Return(btcctypes.DefaultParams()).AnyTimes()
ckptKeeper := types.NewMockCheckpointingKeeper(ctrl)
keeper, ctx := testkeeper.BTCStakingKeeper(t, btclcKeeper, btccKeeper, ckptKeeper)
keeper, ctx := testkeeper.BTCStakingKeeper(t, btclcKeeper, btccKeeper)

// covenant and slashing addr
covenantSKs, covenantPKs, covenantQuorum := datagen.GenCovenantCommittee(r)
Expand Down Expand Up @@ -275,7 +274,7 @@ func FuzzFinalityProviderPowerAtHeight(f *testing.F) {
r := rand.New(rand.NewSource(seed))

// Setup keeper and context
keeper, ctx := testkeeper.BTCStakingKeeper(t, nil, nil, nil)
keeper, ctx := testkeeper.BTCStakingKeeper(t, nil, nil)

// random finality provider
fp, err := datagen.GenRandomFinalityProvider(r)
Expand Down Expand Up @@ -324,7 +323,7 @@ func FuzzFinalityProviderCurrentVotingPower(f *testing.F) {
r := rand.New(rand.NewSource(seed))

// Setup keeper and context
keeper, ctx := testkeeper.BTCStakingKeeper(t, nil, nil, nil)
keeper, ctx := testkeeper.BTCStakingKeeper(t, nil, nil)

// random finality provider
fp, err := datagen.GenRandomFinalityProvider(r)
Expand Down Expand Up @@ -376,8 +375,7 @@ func FuzzActiveFinalityProvidersAtHeight(f *testing.F) {
btclcKeeper.EXPECT().GetTipInfo(gomock.Any()).Return(&btclctypes.BTCHeaderInfo{Height: 10}).AnyTimes()
btccKeeper := types.NewMockBtcCheckpointKeeper(ctrl)
btccKeeper.EXPECT().GetParams(gomock.Any()).Return(btcctypes.DefaultParams()).AnyTimes()
ckptKeeper := types.NewMockCheckpointingKeeper(ctrl)
keeper, ctx := testkeeper.BTCStakingKeeper(t, btclcKeeper, btccKeeper, ckptKeeper)
keeper, ctx := testkeeper.BTCStakingKeeper(t, btclcKeeper, btccKeeper)

// covenant and slashing addr
covenantSKs, covenantPKs, covenantQuorum := datagen.GenCovenantCommittee(r)
Expand Down Expand Up @@ -496,8 +494,7 @@ func FuzzFinalityProviderDelegations(f *testing.F) {
btclcKeeper := types.NewMockBTCLightClientKeeper(ctrl)
btccKeeper := types.NewMockBtcCheckpointKeeper(ctrl)
btccKeeper.EXPECT().GetParams(gomock.Any()).Return(btcctypes.DefaultParams()).AnyTimes()
ckptKeeper := types.NewMockCheckpointingKeeper(ctrl)
keeper, ctx := testkeeper.BTCStakingKeeper(t, btclcKeeper, btccKeeper, ckptKeeper)
keeper, ctx := testkeeper.BTCStakingKeeper(t, btclcKeeper, btccKeeper)

// covenant and slashing addr
covenantSKs, covenantPKs, covenantQuorum := datagen.GenCovenantCommittee(r)
Expand Down
3 changes: 1 addition & 2 deletions x/btcstaking/keeper/incentive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ func FuzzRecordVotingPowerDistCache(f *testing.F) {
// mock BTC light client and BTC checkpoint modules
btclcKeeper := types.NewMockBTCLightClientKeeper(ctrl)
btccKeeper := types.NewMockBtcCheckpointKeeper(ctrl)
ckptKeeper := types.NewMockCheckpointingKeeper(ctrl)
h := NewHelper(t, btclcKeeper, btccKeeper, ckptKeeper)
h := NewHelper(t, btclcKeeper, btccKeeper)

// set all parameters
covenantSKs, _ := h.GenAndApplyParams(r)
Expand Down
16 changes: 0 additions & 16 deletions x/btcstaking/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ type (

btclcKeeper types.BTCLightClientKeeper
btccKeeper types.BtcCheckpointKeeper
ckptKeeper types.CheckpointingKeeper

hooks types.BtcStakingHooks

Expand All @@ -38,7 +37,6 @@ func NewKeeper(

btclcKeeper types.BTCLightClientKeeper,
btccKeeper types.BtcCheckpointKeeper,
ckptKeeper types.CheckpointingKeeper,

btcNet *chaincfg.Params,
authority string,
Expand All @@ -49,7 +47,6 @@ func NewKeeper(

btclcKeeper: btclcKeeper,
btccKeeper: btccKeeper,
ckptKeeper: ckptKeeper,

hooks: nil,

Expand Down Expand Up @@ -86,16 +83,3 @@ func (k Keeper) BeginBlocker(ctx context.Context) error {

return nil
}

func (k Keeper) GetLastFinalizedEpoch(ctx context.Context) uint64 {
return k.ckptKeeper.GetLastFinalizedEpoch(ctx)
}

func (k Keeper) GetEpoch(ctx context.Context) uint64 {
epoch := k.ckptKeeper.GetEpoch(ctx)
if epoch == nil {
panic(fmt.Errorf("epoch cannot be nil"))
}

return epoch.EpochNumber
}
6 changes: 2 additions & 4 deletions x/btcstaking/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,13 @@ type Helper struct {
BTCStakingKeeper *keeper.Keeper
BTCLightClientKeeper *types.MockBTCLightClientKeeper
BTCCheckpointKeeper *types.MockBtcCheckpointKeeper
CheckpointingKeeper *types.MockCheckpointingKeeper
BTCStakingHooks *types.MockBtcStakingHooks
MsgServer types.MsgServer
Net *chaincfg.Params
}

func NewHelper(t testing.TB, btclcKeeper *types.MockBTCLightClientKeeper, btccKeeper *types.MockBtcCheckpointKeeper, ckptKeeper *types.MockCheckpointingKeeper) *Helper {
k, ctx := keepertest.BTCStakingKeeper(t, btclcKeeper, btccKeeper, ckptKeeper)
func NewHelper(t testing.TB, btclcKeeper *types.MockBTCLightClientKeeper, btccKeeper *types.MockBtcCheckpointKeeper) *Helper {
k, ctx := keepertest.BTCStakingKeeper(t, btclcKeeper, btccKeeper)
ctx = ctx.WithHeaderInfo(header.Info{Height: 1})
msgSrvr := keeper.NewMsgServerImpl(*k)

Expand All @@ -55,7 +54,6 @@ func NewHelper(t testing.TB, btclcKeeper *types.MockBTCLightClientKeeper, btccKe
BTCStakingKeeper: k,
BTCLightClientKeeper: btclcKeeper,
BTCCheckpointKeeper: btccKeeper,
CheckpointingKeeper: ckptKeeper,
MsgServer: msgSrvr,
Net: &chaincfg.SimNetParams,
}
Expand Down
30 changes: 10 additions & 20 deletions x/btcstaking/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ func FuzzMsgCreateFinalityProvider(f *testing.F) {
// mock BTC light client and BTC checkpoint modules
btclcKeeper := types.NewMockBTCLightClientKeeper(ctrl)
btccKeeper := types.NewMockBtcCheckpointKeeper(ctrl)
ckptKeeper := types.NewMockCheckpointingKeeper(ctrl)
h := NewHelper(t, btclcKeeper, btccKeeper, ckptKeeper)
h := NewHelper(t, btclcKeeper, btccKeeper)

// set all parameters
h.GenAndApplyParams(r)
Expand Down Expand Up @@ -144,8 +143,7 @@ func FuzzCreateBTCDelegation(f *testing.F) {
// mock BTC light client and BTC checkpoint modules
btclcKeeper := types.NewMockBTCLightClientKeeper(ctrl)
btccKeeper := types.NewMockBtcCheckpointKeeper(ctrl)
ckptKeeper := types.NewMockCheckpointingKeeper(ctrl)
h := NewHelper(t, btclcKeeper, btccKeeper, ckptKeeper)
h := NewHelper(t, btclcKeeper, btccKeeper)

// set all parameters
h.GenAndApplyParams(r)
Expand Down Expand Up @@ -189,8 +187,7 @@ func TestProperVersionInDelegation(t *testing.T) {
// mock BTC light client and BTC checkpoint modules
btclcKeeper := types.NewMockBTCLightClientKeeper(ctrl)
btccKeeper := types.NewMockBtcCheckpointKeeper(ctrl)
ckptKeeper := types.NewMockCheckpointingKeeper(ctrl)
h := NewHelper(t, btclcKeeper, btccKeeper, ckptKeeper)
h := NewHelper(t, btclcKeeper, btccKeeper)

// set all parameters
h.GenAndApplyParams(r)
Expand Down Expand Up @@ -257,8 +254,7 @@ func FuzzAddCovenantSigs(f *testing.F) {
// mock BTC light client and BTC checkpoint modules
btclcKeeper := types.NewMockBTCLightClientKeeper(ctrl)
btccKeeper := types.NewMockBtcCheckpointKeeper(ctrl)
ckptKeeper := types.NewMockCheckpointingKeeper(ctrl)
h := NewHelper(t, btclcKeeper, btccKeeper, ckptKeeper)
h := NewHelper(t, btclcKeeper, btccKeeper)

// set all parameters
covenantSKs, _ := h.GenAndApplyParams(r)
Expand Down Expand Up @@ -322,8 +318,7 @@ func FuzzBTCUndelegate(f *testing.F) {
// mock BTC light client and BTC checkpoint modules
btclcKeeper := types.NewMockBTCLightClientKeeper(ctrl)
btccKeeper := types.NewMockBtcCheckpointKeeper(ctrl)
ckptKeeper := types.NewMockCheckpointingKeeper(ctrl)
h := NewHelper(t, btclcKeeper, btccKeeper, ckptKeeper)
h := NewHelper(t, btclcKeeper, btccKeeper)

// set all parameters
covenantSKs, _ := h.GenAndApplyParams(r)
Expand Down Expand Up @@ -395,8 +390,7 @@ func FuzzSelectiveSlashing(f *testing.F) {
// mock BTC light client and BTC checkpoint modules
btclcKeeper := types.NewMockBTCLightClientKeeper(ctrl)
btccKeeper := types.NewMockBtcCheckpointKeeper(ctrl)
ckptKeeper := types.NewMockCheckpointingKeeper(ctrl)
h := NewHelper(t, btclcKeeper, btccKeeper, ckptKeeper)
h := NewHelper(t, btclcKeeper, btccKeeper)

// set all parameters
covenantSKs, _ := h.GenAndApplyParams(r)
Expand Down Expand Up @@ -462,8 +456,7 @@ func FuzzSelectiveSlashing_StakingTx(f *testing.F) {
// mock BTC light client and BTC checkpoint modules
btclcKeeper := types.NewMockBTCLightClientKeeper(ctrl)
btccKeeper := types.NewMockBtcCheckpointKeeper(ctrl)
ckptKeeper := types.NewMockCheckpointingKeeper(ctrl)
h := NewHelper(t, btclcKeeper, btccKeeper, ckptKeeper)
h := NewHelper(t, btclcKeeper, btccKeeper)

// set all parameters
covenantSKs, _ := h.GenAndApplyParams(r)
Expand Down Expand Up @@ -539,8 +532,7 @@ func TestDoNotAllowDelegationWithoutFinalityProvider(t *testing.T) {
btclcKeeper := types.NewMockBTCLightClientKeeper(ctrl)
btccKeeper := types.NewMockBtcCheckpointKeeper(ctrl)
btccKeeper.EXPECT().GetParams(gomock.Any()).Return(btcctypes.DefaultParams()).AnyTimes()
ckptKeeper := types.NewMockCheckpointingKeeper(ctrl)
h := NewHelper(t, btclcKeeper, btccKeeper, ckptKeeper)
h := NewHelper(t, btclcKeeper, btccKeeper)

// set covenant PK to params
_, covenantPKs := h.GenAndApplyParams(r)
Expand Down Expand Up @@ -707,8 +699,7 @@ func TestCorrectUnbondingTimeInDelegation(t *testing.T) {
// mock BTC light client and BTC checkpoint modules
btclcKeeper := types.NewMockBTCLightClientKeeper(ctrl)
btccKeeper := types.NewMockBtcCheckpointKeeper(ctrl)
ckptKeeper := types.NewMockCheckpointingKeeper(ctrl)
h := NewHelper(t, btclcKeeper, btccKeeper, ckptKeeper)
h := NewHelper(t, btclcKeeper, btccKeeper)

// set all parameters
_, _ = h.GenAndApplyCustomParams(r, tt.finalizationTimeout, tt.minUnbondingTime)
Expand Down Expand Up @@ -780,8 +771,7 @@ func TestMinimalUnbondingRate(t *testing.T) {
// mock BTC light client and BTC checkpoint modules
btclcKeeper := types.NewMockBTCLightClientKeeper(ctrl)
btccKeeper := types.NewMockBtcCheckpointKeeper(ctrl)
ckptKeeper := types.NewMockCheckpointingKeeper(ctrl)
h := NewHelper(t, btclcKeeper, btccKeeper, ckptKeeper)
h := NewHelper(t, btclcKeeper, btccKeeper)

// set all parameters, by default minimal unbonding value is 80% of staking value
_, _ = h.GenAndApplyParams(r)
Expand Down
Loading

0 comments on commit c19e2e3

Please sign in to comment.