Skip to content

Commit

Permalink
some pre approval tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianElvis committed Oct 7, 2024
1 parent d808667 commit 1731c0d
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 62 deletions.
4 changes: 2 additions & 2 deletions x/btcstaking/keeper/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func benchBeginBlock(b *testing.B, numFPs int, numDelsUnderFP int) {
stakingValue := int64(2 * 10e8)
delSK, _, err := datagen.GenRandomBTCKeyPair(r)
h.NoError(err)
stakingTxHash, msgCreateBTCDel, actualDel, inclusionProof, err := h.CreateDelegation(
stakingTxHash, msgCreateBTCDel, actualDel, btcHeaderInfo, inclusionProof, err := h.CreateDelegation(
r,
delSK,
fp.BtcPk.MustToBTCPK(),
Expand All @@ -74,7 +74,7 @@ func benchBeginBlock(b *testing.B, numFPs int, numDelsUnderFP int) {
h.CreateCovenantSigs(r, covenantSKs, msgCreateBTCDel, actualDel)
// activate BTC delegation
// after that, all BTC delegations will have voting power
h.AddInclusionProof(stakingTxHash, inclusionProof)
h.AddInclusionProof(stakingTxHash, btcHeaderInfo, inclusionProof)
}
}

Expand Down
41 changes: 28 additions & 13 deletions x/btcstaking/keeper/btc_delegations.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,35 @@ func (k Keeper) addCovenantSigsToBTCDelegation(

// If reaching the covenant quorum after this msg, the BTC delegation becomes
// active. Then, record and emit this event
if btcDel.HasCovenantQuorums(params.CovenantQuorum) && btcDel.HasInclusionProof() {
// notify subscriber
event := &types.EventBTCDelegationStateUpdate{
StakingTxHash: btcDel.MustGetStakingTxHash().String(),
NewState: types.BTCDelegationStatus_ACTIVE,
if btcDel.HasCovenantQuorums(params.CovenantQuorum) {
if btcDel.HasInclusionProof() {
// this BTC delegation does not go through pre-approval flow
// notify subscriber that the BTC delegation becomes active
event := &types.EventBTCDelegationStateUpdate{
StakingTxHash: btcDel.MustGetStakingTxHash().String(),
NewState: types.BTCDelegationStatus_ACTIVE,
}
if err := ctx.EventManager().EmitTypedEvent(event); err != nil {
panic(fmt.Errorf("failed to emit EventBTCDelegationStateUpdate for the new active BTC delegation: %w", err))
}

// record event that the BTC delegation becomes active at this height
activeEvent := types.NewEventPowerDistUpdateWithBTCDel(event)
btcTip := k.btclcKeeper.GetTipInfo(ctx)
k.addPowerDistUpdateEvent(ctx, btcTip.Height, activeEvent)
} else {
// this BTC delegation goes through pre-approval flow
// notify subscriber that the BTC delegation becomes approved
event := &types.EventBTCDelegationStateUpdate{
StakingTxHash: btcDel.MustGetStakingTxHash().String(),
NewState: types.BTCDelegationStatus_APPROVED,
}
if err := ctx.EventManager().EmitTypedEvent(event); err != nil {
panic(fmt.Errorf("failed to emit EventBTCDelegationStateUpdate for the new active BTC delegation: %w", err))
}
// NOTE: no need to record power dist update event for approved BTC delegations
// since it does not have voting power
}
if err := ctx.EventManager().EmitTypedEvent(event); err != nil {
panic(fmt.Errorf("failed to emit EventBTCDelegationStateUpdate for the new active BTC delegation: %w", err))
}

// record event that the BTC delegation becomes active at this height
activeEvent := types.NewEventPowerDistUpdateWithBTCDel(event)
btcTip := k.btclcKeeper.GetTipInfo(ctx)
k.addPowerDistUpdateEvent(ctx, btcTip.Height, activeEvent)
}
}

Expand Down
4 changes: 2 additions & 2 deletions x/btcstaking/keeper/incentive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func FuzzRecordVotingPowerDistCache(f *testing.F) {
for j := uint64(0); j < numBTCDels; j++ {
delSK, _, err := datagen.GenRandomBTCKeyPair(r)
h.NoError(err)
stakingTxHash, delMsg, del, inclusionProof, err := h.CreateDelegation(
stakingTxHash, delMsg, del, btcHeaderInfo, inclusionProof, err := h.CreateDelegation(
r,
delSK,
fp.BtcPk.MustToBTCPK(),
Expand All @@ -65,7 +65,7 @@ func FuzzRecordVotingPowerDistCache(f *testing.F) {
)
h.NoError(err)
h.CreateCovenantSigs(r, covenantSKs, delMsg, del)
h.AddInclusionProof(stakingTxHash, inclusionProof)
h.AddInclusionProof(stakingTxHash, btcHeaderInfo, inclusionProof)
}
}

Expand Down
14 changes: 9 additions & 5 deletions x/btcstaking/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (h *Helper) CreateDelegation(
unbondingValue int64,
unbondingTime uint16,
usePreApproval bool,
) (string, *types.MsgCreateBTCDelegation, *types.BTCDelegation, *types.InclusionProof, error) {
) (string, *types.MsgCreateBTCDelegation, *types.BTCDelegation, *btclctypes.BTCHeaderInfo, *types.InclusionProof, error) {
stakingTimeBlocks := stakingTime
bsParams := h.BTCStakingKeeper.GetParams(h.Ctx)
bcParams := h.BTCCheckpointKeeper.GetParams(h.Ctx)
Expand Down Expand Up @@ -222,8 +222,8 @@ func (h *Helper) CreateDelegation(
txInclusionProof := types.NewInclusionProof(&btcctypes.TransactionKey{Index: 1, Hash: btcHeader.Hash()}, btcHeaderWithProof.SpvProof.MerkleNodes)

// mock for testing k-deep stuff
h.BTCLightClientKeeper.EXPECT().GetHeaderByHash(gomock.Any(), gomock.Eq(btcHeader.Hash())).Return(btcHeaderInfo).AnyTimes()
h.BTCLightClientKeeper.EXPECT().GetTipInfo(gomock.Any()).Return(&btclctypes.BTCHeaderInfo{Height: btcTipHeight}).AnyTimes()
h.BTCLightClientKeeper.EXPECT().GetHeaderByHash(gomock.Eq(h.Ctx), gomock.Eq(btcHeader.Hash())).Return(btcHeaderInfo).AnyTimes()
h.BTCLightClientKeeper.EXPECT().GetTipInfo(gomock.Eq(h.Ctx)).Return(&btclctypes.BTCHeaderInfo{Height: btcTipHeight}).AnyTimes()

slashingSpendInfo, err := testStakingInfo.StakingInfo.SlashingPathSpendInfo()
h.NoError(err)
Expand Down Expand Up @@ -294,7 +294,7 @@ func (h *Helper) CreateDelegation(

_, err = h.MsgServer.CreateBTCDelegation(h.Ctx, msgCreateBTCDel)
if err != nil {
return "", nil, nil, nil, err
return "", nil, nil, nil, nil, err
}

stakingMsgTx, err := bbn.NewBTCTxFromBytes(msgCreateBTCDel.StakingTx)
Expand All @@ -313,7 +313,7 @@ func (h *Helper) CreateDelegation(
require.True(h.t, btcDel.HasInclusionProof())
}

return stakingTxHash, msgCreateBTCDel, btcDel, txInclusionProof, nil
return stakingTxHash, msgCreateBTCDel, btcDel, btcHeaderInfo, txInclusionProof, nil
}

func (h *Helper) GenerateCovenantSignaturesMessages(
Expand Down Expand Up @@ -439,6 +439,7 @@ func (h *Helper) CreateCovenantSigs(

func (h *Helper) AddInclusionProof(
stakingTxHash string,
btcHeader *btclctypes.BTCHeaderInfo,
proof *types.InclusionProof,
) {
bcParams := h.BTCCheckpointKeeper.GetParams(h.Ctx)
Expand All @@ -456,6 +457,9 @@ func (h *Helper) AddInclusionProof(
StakingTxInclusionProof: proof,
}

// mock BTC header that includes the staking tx
h.BTCLightClientKeeper.EXPECT().GetHeaderByHash(gomock.Eq(h.Ctx), gomock.Eq(btcHeader.Header.Hash())).Return(btcHeader).AnyTimes()

// Call the AddBTCDelegationInclusionProof handler
_, err = h.MsgServer.AddBTCDelegationInclusionProof(h.Ctx, msg)
h.NoError(err)
Expand Down
22 changes: 11 additions & 11 deletions x/btcstaking/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func FuzzCreateBTCDelegation(f *testing.F) {
stakingValue := int64(2 * 10e8)
delSK, _, err := datagen.GenRandomBTCKeyPair(r)
h.NoError(err)
stakingTxHash, msgCreateBTCDel, _, _, err := h.CreateDelegation(
stakingTxHash, msgCreateBTCDel, _, _, _, err := h.CreateDelegation(
r,
delSK,
fpPK,
Expand Down Expand Up @@ -213,7 +213,7 @@ func TestProperVersionInDelegation(t *testing.T) {
stakingValue := int64(2 * 10e8)
delSK, _, err := datagen.GenRandomBTCKeyPair(r)
h.NoError(err)
stakingTxHash, _, _, _, err := h.CreateDelegation(
stakingTxHash, _, _, _, _, err := h.CreateDelegation(
r,
delSK,
fpPK,
Expand Down Expand Up @@ -243,7 +243,7 @@ func TestProperVersionInDelegation(t *testing.T) {
err = h.BTCStakingKeeper.SetParams(h.Ctx, currentParams)
require.NoError(t, err)
// create new delegation
stakingTxHash1, _, _, _, err := h.CreateDelegation(
stakingTxHash1, _, _, _, _, err := h.CreateDelegation(
r,
delSK,
fpPK,
Expand Down Expand Up @@ -290,7 +290,7 @@ func FuzzAddCovenantSigs(f *testing.F) {
stakingValue := int64(2 * 10e8)
delSK, _, err := datagen.GenRandomBTCKeyPair(r)
h.NoError(err)
stakingTxHash, msgCreateBTCDel, _, _, err := h.CreateDelegation(
stakingTxHash, msgCreateBTCDel, _, _, _, err := h.CreateDelegation(
r,
delSK,
fpPK,
Expand Down Expand Up @@ -365,7 +365,7 @@ func FuzzBTCUndelegate(f *testing.F) {
stakingValue := int64(2 * 10e8)
delSK, _, err := datagen.GenRandomBTCKeyPair(r)
h.NoError(err)
stakingTxHash, msgCreateBTCDel, actualDel, inclusionProof, err := h.CreateDelegation(
stakingTxHash, msgCreateBTCDel, actualDel, btcHeaderInfo, inclusionProof, err := h.CreateDelegation(
r,
delSK,
fpPK,
Expand All @@ -381,7 +381,7 @@ func FuzzBTCUndelegate(f *testing.F) {
// add covenant signatures to this BTC delegation
h.CreateCovenantSigs(r, covenantSKs, msgCreateBTCDel, actualDel)
// activate the BTC delegation
h.AddInclusionProof(stakingTxHash, inclusionProof)
h.AddInclusionProof(stakingTxHash, btcHeaderInfo, inclusionProof)

// ensure the BTC delegation is bonded right now
actualDel, err = h.BTCStakingKeeper.GetBTCDelegation(h.Ctx, stakingTxHash)
Expand Down Expand Up @@ -446,7 +446,7 @@ func FuzzSelectiveSlashing(f *testing.F) {
stakingValue := int64(2 * 10e8)
delSK, _, err := datagen.GenRandomBTCKeyPair(r)
h.NoError(err)
stakingTxHash, msgCreateBTCDel, actualDel, inclusionProof, err := h.CreateDelegation(
stakingTxHash, msgCreateBTCDel, actualDel, btcHeaderInfo, inclusionProof, err := h.CreateDelegation(
r,
delSK,
fpPK,
Expand All @@ -463,7 +463,7 @@ func FuzzSelectiveSlashing(f *testing.F) {
// so that the BTC delegation becomes bonded
h.CreateCovenantSigs(r, covenantSKs, msgCreateBTCDel, actualDel)
// activate the BTC delegation
h.AddInclusionProof(stakingTxHash, inclusionProof)
h.AddInclusionProof(stakingTxHash, btcHeaderInfo, inclusionProof)

// now BTC delegation has all covenant signatures
actualDel, err = h.BTCStakingKeeper.GetBTCDelegation(h.Ctx, stakingTxHash)
Expand Down Expand Up @@ -523,7 +523,7 @@ func FuzzSelectiveSlashing_StakingTx(f *testing.F) {
stakingValue := int64(2 * 10e8)
delSK, _, err := datagen.GenRandomBTCKeyPair(r)
h.NoError(err)
stakingTxHash, msgCreateBTCDel, actualDel, inclusionProof, err := h.CreateDelegation(
stakingTxHash, msgCreateBTCDel, actualDel, btcHeaderInfo, inclusionProof, err := h.CreateDelegation(
r,
delSK,
fpPK,
Expand All @@ -540,7 +540,7 @@ func FuzzSelectiveSlashing_StakingTx(f *testing.F) {
// so that the BTC delegation becomes bonded
h.CreateCovenantSigs(r, covenantSKs, msgCreateBTCDel, actualDel)
// activate the BTC delegation
h.AddInclusionProof(stakingTxHash, inclusionProof)
h.AddInclusionProof(stakingTxHash, btcHeaderInfo, inclusionProof)
// now BTC delegation has all covenant signatures
actualDel, err = h.BTCStakingKeeper.GetBTCDelegation(h.Ctx, stakingTxHash)
h.NoError(err)
Expand Down Expand Up @@ -775,7 +775,7 @@ func TestCorrectUnbondingTimeInDelegation(t *testing.T) {
stakingValue := int64(2 * 10e8)
delSK, _, err := datagen.GenRandomBTCKeyPair(r)
h.NoError(err)
stakingTxHash, _, _, _, err := h.CreateDelegation(
stakingTxHash, _, _, _, _, err := h.CreateDelegation(
r,
delSK,
fpPK,
Expand Down
Loading

0 comments on commit 1731c0d

Please sign in to comment.