Skip to content

Commit

Permalink
modify voting power dist based on timestamping
Browse files Browse the repository at this point in the history
  • Loading branch information
gitferry committed Aug 19, 2024
1 parent 9a187be commit 2357217
Show file tree
Hide file tree
Showing 20 changed files with 159 additions and 57 deletions.
4 changes: 4 additions & 0 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,9 @@ func (ak *AppKeepers) InitKeepers(
runtime.NewKVStoreService(keys[btcstakingtypes.StoreKey]),
&btclightclientKeeper,
&btcCheckpointKeeper,
// setting the finality keeper as nil for now
// need to set it after finality keeper is initiated
nil,
btcNetParams,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
Expand All @@ -539,6 +542,7 @@ func (ak *AppKeepers) InitKeepers(
)
ak.BTCStakingKeeper = *ak.BTCStakingKeeper.SetHooks(btcstakingtypes.NewMultiBtcStakingHooks(ak.FinalityKeeper.Hooks()))
ak.FinalityKeeper = *ak.FinalityKeeper.SetHooks(finalitytypes.NewMultiFinalityHooks(ak.BTCStakingKeeper.Hooks()))
ak.BTCStakingKeeper.FinalityKeeper = ak.FinalityKeeper

// create evidence keeper with router
evidenceKeeper := evidencekeeper.NewKeeper(
Expand Down
2 changes: 2 additions & 0 deletions testutil/keeper/btcstaking.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func BTCStakingKeeper(
t testing.TB,
btclcKeeper types.BTCLightClientKeeper,
btccKeeper types.BtcCheckpointKeeper,
finalityKeeper types.FinalityKeeper,
) (*keeper.Keeper, sdk.Context) {
storeKey := storetypes.NewKVStoreKey(types.StoreKey)

Expand All @@ -43,6 +44,7 @@ func BTCStakingKeeper(
runtime.NewKVStoreService(storeKey),
btclcKeeper,
btccKeeper,
finalityKeeper,
&chaincfg.SimNetParams,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
Expand Down
2 changes: 1 addition & 1 deletion x/btcstaking/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestGenesis(t *testing.T) {
Params: []*types.Params{&p},
}

k, ctx := keepertest.BTCStakingKeeper(t, nil, nil)
k, ctx := keepertest.BTCStakingKeeper(t, nil, nil, nil)
btcstaking.InitGenesis(ctx, *k, genesisState)
got := btcstaking.ExportGenesis(ctx, *k)
require.NotNil(t, got)
Expand Down
3 changes: 2 additions & 1 deletion x/btcstaking/keeper/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func benchBeginBlock(b *testing.B, numFPs int, numDelsUnderFP int) {
defer ctrl.Finish()
btclcKeeper := types.NewMockBTCLightClientKeeper(ctrl)
btccKeeper := types.NewMockBtcCheckpointKeeper(ctrl)
h := NewHelper(b, btclcKeeper, btccKeeper)
finalityKeeper := types.NewMockFinalityKeeper(ctrl)
h := NewHelper(b, btclcKeeper, btccKeeper, finalityKeeper)
// set all parameters
covenantSKs, _ := h.GenAndApplyParams(r)
changeAddress, err := datagen.GenRandomBTCAddress(r, h.Net)
Expand Down
2 changes: 1 addition & 1 deletion x/btcstaking/keeper/btc_height_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func FuzzBTCHeightIndex(f *testing.F) {

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

// randomise Babylon height and BTC height
babylonHeight := datagen.RandomInt(r, 100)
Expand Down
16 changes: 8 additions & 8 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)
keeper, ctx := testkeeper.BTCStakingKeeper(t, nil, 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)
keeper, ctx := testkeeper.BTCStakingKeeper(t, nil, 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)
keeper, ctx := testkeeper.BTCStakingKeeper(t, nil, nil, nil)
ctx = sdk.UnwrapSDKContext(ctx)

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

// covenant and slashing addr
covenantSKs, covenantPKs, covenantQuorum := datagen.GenCovenantCommittee(r)
Expand Down Expand Up @@ -274,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)
keeper, ctx := testkeeper.BTCStakingKeeper(t, nil, nil, nil)

// random finality provider
fp, err := datagen.GenRandomFinalityProvider(r)
Expand Down Expand Up @@ -323,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)
keeper, ctx := testkeeper.BTCStakingKeeper(t, nil, nil, nil)

// random finality provider
fp, err := datagen.GenRandomFinalityProvider(r)
Expand Down Expand Up @@ -375,7 +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()
keeper, ctx := testkeeper.BTCStakingKeeper(t, btclcKeeper, btccKeeper)
keeper, ctx := testkeeper.BTCStakingKeeper(t, btclcKeeper, btccKeeper, nil)

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

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

// set all parameters
covenantSKs, _ := h.GenAndApplyParams(r)
Expand Down
11 changes: 7 additions & 4 deletions x/btcstaking/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ type (
cdc codec.BinaryCodec
storeService corestoretypes.KVStoreService

btclcKeeper types.BTCLightClientKeeper
btccKeeper types.BtcCheckpointKeeper
btclcKeeper types.BTCLightClientKeeper
btccKeeper types.BtcCheckpointKeeper
FinalityKeeper types.FinalityKeeper

hooks types.BtcStakingHooks

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

btclcKeeper types.BTCLightClientKeeper,
btccKeeper types.BtcCheckpointKeeper,
finalityKeeper types.FinalityKeeper,

btcNet *chaincfg.Params,
authority string,
Expand All @@ -45,8 +47,9 @@ func NewKeeper(
cdc: cdc,
storeService: storeService,

btclcKeeper: btclcKeeper,
btccKeeper: btccKeeper,
btclcKeeper: btclcKeeper,
btccKeeper: btccKeeper,
FinalityKeeper: finalityKeeper,

hooks: nil,

Expand Down
10 changes: 8 additions & 2 deletions x/btcstaking/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,19 @@ type Helper struct {
BTCStakingKeeper *keeper.Keeper
BTCLightClientKeeper *types.MockBTCLightClientKeeper
BTCCheckpointKeeper *types.MockBtcCheckpointKeeper
FinalityKeeper *types.MockFinalityKeeper
BTCStakingHooks *types.MockBtcStakingHooks
MsgServer types.MsgServer
Net *chaincfg.Params
}

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

Expand Down
30 changes: 20 additions & 10 deletions x/btcstaking/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ func FuzzMsgCreateFinalityProvider(f *testing.F) {
// mock BTC light client and BTC checkpoint modules
btclcKeeper := types.NewMockBTCLightClientKeeper(ctrl)
btccKeeper := types.NewMockBtcCheckpointKeeper(ctrl)
h := NewHelper(t, btclcKeeper, btccKeeper)
finalityKeeper := types.NewMockFinalityKeeper(ctrl)
h := NewHelper(t, btclcKeeper, btccKeeper, finalityKeeper)

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

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

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

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

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

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

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

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

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

// set all parameters, by default minimal unbonding value is 80% of staking value
_, _ = h.GenAndApplyParams(r)
Expand Down
6 changes: 3 additions & 3 deletions x/btcstaking/keeper/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func TestGetParams(t *testing.T) {
k, ctx := testkeeper.BTCStakingKeeper(t, nil, nil)
k, ctx := testkeeper.BTCStakingKeeper(t, nil, nil, nil)
params := types.DefaultParams()

err := k.SetParams(ctx, params)
Expand All @@ -23,7 +23,7 @@ func TestGetParams(t *testing.T) {
}

func TestGetParamsVersions(t *testing.T) {
k, ctx := testkeeper.BTCStakingKeeper(t, nil, nil)
k, ctx := testkeeper.BTCStakingKeeper(t, nil, nil, nil)
params := types.DefaultParams()

pv := k.GetParamsWithVersion(ctx)
Expand Down Expand Up @@ -56,7 +56,7 @@ func FuzzParamsVersioning(f *testing.F) {
datagen.AddRandomSeedsToFuzzer(f, 10)
f.Fuzz(func(t *testing.T, seed int64) {
r := rand.New(rand.NewSource(seed))
k, ctx := testkeeper.BTCStakingKeeper(t, nil, nil)
k, ctx := testkeeper.BTCStakingKeeper(t, nil, nil, nil)
numVersionsToGenerate := r.Intn(100) + 1
params0 := k.GetParams(ctx)
var generatedParams []*types.Params
Expand Down
11 changes: 11 additions & 0 deletions x/btcstaking/keeper/power_dist_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func (k Keeper) ProcessAllPowerDistUpdateEvents(
events []*types.EventPowerDistUpdate,
maxActiveFps uint32,
) *types.VotingPowerDistCache {
height := uint64(sdk.UnwrapSDKContext(ctx).HeaderInfo().Height)
// a map where key is finality provider's BTC PK hex and value is a list
// of BTC delegations that newly become active under this provider
activeBTCDels := map[string][]*types.BTCDelegation{}
Expand Down Expand Up @@ -196,6 +197,11 @@ func (k Keeper) ProcessAllPowerDistUpdateEvents(

// add this finality provider to the new cache if it has voting power
if fp.TotalVotingPower > 0 {
// voting power is not assigned if it does not have timestamped
// public randomness for this height
if k.FinalityKeeper.HasTimestampedPubRand(ctx, fp.BtcPk, height) {
fp.TotalVotingPower = 0
}
newDc.AddFinalityProviderDistInfo(&fp)
}
}
Expand Down Expand Up @@ -232,6 +238,11 @@ func (k Keeper) ProcessAllPowerDistUpdateEvents(

// add this finality provider to the new cache if it has voting power
if fpDistInfo.TotalVotingPower > 0 {
// voting power is not assigned if it does not have timestamped
// public randomness for this height
if k.FinalityKeeper.HasTimestampedPubRand(ctx, fpBTCPK, height) {
fpDistInfo.TotalVotingPower = 0
}
newDc.AddFinalityProviderDistInfo(fpDistInfo)
}
}
Expand Down
9 changes: 6 additions & 3 deletions x/btcstaking/keeper/power_dist_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func FuzzProcessAllPowerDistUpdateEvents_Determinism(f *testing.F) {
// mock BTC light client and BTC checkpoint modules
btclcKeeper := types.NewMockBTCLightClientKeeper(ctrl)
btccKeeper := types.NewMockBtcCheckpointKeeper(ctrl)
h := NewHelper(t, btclcKeeper, btccKeeper)
finalityKeeper := types.NewMockFinalityKeeper(ctrl)
h := NewHelper(t, btclcKeeper, btccKeeper, finalityKeeper)

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

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

// set all parameters
covenantSKs, _ := h.GenAndApplyParams(r)
Expand Down
Loading

0 comments on commit 2357217

Please sign in to comment.