Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
KonradStaniec committed Oct 24, 2024
1 parent f071e13 commit 936c0c8
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 34 deletions.
4 changes: 2 additions & 2 deletions testutil/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ func (h *Helper) CheckDelegator(delegator sdk.AccAddress, val sdk.ValAddress, fo
require.Equal(h.t, ok, found)
}

func (h *Helper) AddDelegation(del *btcstakingtypes.BTCDelegation) {
err := h.App.BTCStakingKeeper.AddBTCDelegation(h.Ctx, del)
func (h *Helper) AddDelegation(del *btcstakingtypes.BTCDelegation, minUnbondingTime uint32) {
err := h.App.BTCStakingKeeper.AddBTCDelegation(h.Ctx, del, minUnbondingTime)
h.NoError(err)
}

Expand Down
11 changes: 5 additions & 6 deletions x/btcstaking/keeper/btc_delegations.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import (
// - indexing the given BTC delegation in the BTC delegator store,
// - saving it under BTC delegation store, and
// - emit events about this BTC delegation.
func (k Keeper) AddBTCDelegation(ctx sdk.Context, btcDel *types.BTCDelegation) error {
func (k Keeper) AddBTCDelegation(
ctx sdk.Context,
btcDel *types.BTCDelegation,
minUnbondingTime uint32,
) error {
if err := btcDel.ValidateBasic(); err != nil {
return err
}
Expand Down Expand Up @@ -76,11 +80,6 @@ func (k Keeper) AddBTCDelegation(ctx sdk.Context, btcDel *types.BTCDelegation) e
NewState: types.BTCDelegationStatus_UNBONDED,
})

btccheckpointParams := k.btccKeeper.GetParams(ctx)
stakingParams := k.GetParamsWithVersion(ctx).Params

minUnbondingTime := types.MinimumUnbondingTime(&stakingParams, &btccheckpointParams)

// NOTE: we should have verified that EndHeight > btcTip.Height + max(w, min_unbonding_time)
k.addPowerDistUpdateEvent(ctx, btcDel.EndHeight-minUnbondingTime, unbondedEvent)
}
Expand Down
2 changes: 1 addition & 1 deletion x/btcstaking/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestExportGenesis(t *testing.T) {
totalDelegations++

// sets delegations
h.AddDelegation(del)
h.AddDelegation(del, minUnbondingTime)
btcDelegations = append(btcDelegations, del)

// BTC delegators idx
Expand Down
6 changes: 3 additions & 3 deletions x/btcstaking/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func FuzzPendingBTCDelegations(f *testing.F) {
btcDel.CovenantSigs = nil
pendingBtcDelsMap[btcDel.BtcPk.MarshalHex()] = btcDel
}
err = keeper.AddBTCDelegation(ctx, btcDel)
err = keeper.AddBTCDelegation(ctx, btcDel, btcDel.UnbondingTime-1)
require.NoError(t, err)

txHash := btcDel.MustGetStakingTxHash().String()
Expand Down Expand Up @@ -443,7 +443,7 @@ func FuzzActiveFinalityProvidersAtHeight(f *testing.F) {
slashingChangeLockTime,
)
require.NoError(t, err)
err = keeper.AddBTCDelegation(ctx, btcDel)
err = keeper.AddBTCDelegation(ctx, btcDel, btcDel.UnbondingTime-1)
require.NoError(t, err)
totalVotingPower += btcDel.TotalSat
}
Expand Down Expand Up @@ -556,7 +556,7 @@ func FuzzFinalityProviderDelegations(f *testing.F) {
)
require.NoError(t, err)
expectedBtcDelsMap[btcDel.BtcPk.MarshalHex()] = btcDel
err = keeper.AddBTCDelegation(ctx, btcDel)
err = keeper.AddBTCDelegation(ctx, btcDel, btcDel.UnbondingTime-1)
require.NoError(t, err)
}

Expand Down
6 changes: 2 additions & 4 deletions x/btcstaking/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,6 @@ func (ms msgServer) CreateBTCDelegation(goCtx context.Context, req *types.MsgCre
return nil, err
}

minUnbondingTime := types.MinimumUnbondingTime(&vp.Params, &btccParams)

// 6. If the delegation contains the inclusion proof, we need to verify the proof
// and set start height and end height
var startHeight, endHeight uint32
Expand All @@ -194,7 +192,7 @@ func (ms msgServer) CreateBTCDelegation(goCtx context.Context, req *types.MsgCre
btcutil.NewTx(parsedMsg.StakingTx.Transaction),
btccParams.BtcConfirmationDepth,
uint32(parsedMsg.StakingTime),
minUnbondingTime,
paramsValidationResult.MinUnbondingTime,
parsedMsg.StakingTxProofOfInclusion)
if err != nil {
return nil, fmt.Errorf("invalid inclusion proof: %w", err)
Expand Down Expand Up @@ -238,7 +236,7 @@ func (ms msgServer) CreateBTCDelegation(goCtx context.Context, req *types.MsgCre
}

// add this BTC delegation, and emit corresponding events
if err := ms.AddBTCDelegation(ctx, newBTCDel); err != nil {
if err := ms.AddBTCDelegation(ctx, newBTCDel, paramsValidationResult.MinUnbondingTime); err != nil {
panic(fmt.Errorf("failed to add BTC delegation that has passed verification: %w", err))
}

Expand Down
11 changes: 7 additions & 4 deletions x/btcstaking/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -976,9 +976,12 @@ func FuzzDeterminismBtcstakingBeginBlocker(f *testing.F) {
require.Equal(t, appHash1, appHash2)

// Default params are the same in both apps
covQuorum := h.App.BTCStakingKeeper.GetParams(h.Ctx).CovenantQuorum
maxFinalityProviders := int32(h.App.BTCStakingKeeper.GetParams(h.Ctx).MaxActiveFinalityProviders)
stakingParams := h.App.BTCStakingKeeper.GetParams(h.Ctx)
covQuorum := stakingParams.CovenantQuorum
maxFinalityProviders := int32(stakingParams.MaxActiveFinalityProviders)
btcckptParams := h.App.BtcCheckpointKeeper.GetParams(h.Ctx)

minUnbondingTime := types.MinimumUnbondingTime(&stakingParams, &btcckptParams)
// Number of finality providers from 10 to maxFinalityProviders + 10
numFinalityProviders := int(r.Int31n(maxFinalityProviders) + 10)

Expand Down Expand Up @@ -1012,8 +1015,8 @@ func FuzzDeterminismBtcstakingBeginBlocker(f *testing.F) {
)

for _, del := range delegations {
h.AddDelegation(del)
h1.AddDelegation(del)
h.AddDelegation(del, minUnbondingTime)
h1.AddDelegation(del, minUnbondingTime)
}
}

Expand Down
2 changes: 2 additions & 0 deletions x/btcstaking/types/validate_parsed_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type ParamsValidationResult struct {
StakingOutputIdx uint32
UnbondingOutputIdx uint32
MinUnbondingTime uint32
}

// ValidateParsedMessageAgainstTheParams validates parsed message against parameters
Expand Down Expand Up @@ -196,5 +197,6 @@ func ValidateParsedMessageAgainstTheParams(
return &ParamsValidationResult{
StakingOutputIdx: stakingOutputIdx,
UnbondingOutputIdx: unbondingOutputIdx,
MinUnbondingTime: minUnbondingTime,
}, nil
}
17 changes: 3 additions & 14 deletions x/btcstaking/types/validate_parsed_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -759,20 +759,6 @@ func TestValidateParsedMessageAgainstTheParams(t *testing.T) {
},
err: types.ErrInvalidUnbondingTx,
},
{
name: "Msg.UnbondingTime larger than Msg.StakingTime - CheckpointFinalizationTimeout",
fn: func(r *rand.Rand, t *testing.T) (*types.MsgCreateBTCDelegation, *types.Params, *btcckpttypes.Params) {
params := testStakingParams(r, t)
checkpointParams := testCheckpointParams()
msg, _ := createMsgDelegationForParams(r, t, params, checkpointParams)

maxUnbondingTime := msg.StakingTime - checkpointParams.CheckpointFinalizationTimeout
msg.UnbondingTime = maxUnbondingTime + 1

return msg, params, checkpointParams
},
err: types.ErrInvalidUnbondingTx,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -796,6 +782,9 @@ func TestValidateParsedMessageAgainstTheParams(t *testing.T) {
} else {
require.NoError(t, err)
require.NotNil(t, got)

minUnbondingTime := types.MinimumUnbondingTime(params, checkpointParams)
require.Equal(t, minUnbondingTime, got.MinUnbondingTime)
}

})
Expand Down

0 comments on commit 936c0c8

Please sign in to comment.