Skip to content

Commit

Permalink
add assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
gusin13 committed Dec 16, 2024
1 parent 3db9c31 commit 63dd026
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 19 deletions.
67 changes: 62 additions & 5 deletions e2etest/e2e_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package e2etest

import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
Expand All @@ -9,6 +10,7 @@ import (
"testing"
"time"

"github.com/babylonlabs-io/babylon-staking-indexer/internal/types"
bbndatagen "github.com/babylonlabs-io/babylon/testutil/datagen"
queuecli "github.com/babylonlabs-io/staking-queue-client/client"
"github.com/babylonlabs-io/staking-queue-client/config"
Expand Down Expand Up @@ -71,13 +73,60 @@ func TestActivatingDelegation(t *testing.T) {
tm.CatchUpBTCLightClient(t)

// set up a finality provider
_, fpSK := tm.CreateFinalityProvider(t)
fpPK, fpSK := tm.CreateFinalityProvider(t)

// check if the finality provider is stored in indexer db
require.Eventually(t, func() bool {
fp, err := tm.DbClient.GetFinalityProviderByBtcPk(context.Background(), fpPK.BtcPk.MarshalHex())
if err != nil {
return false
}
return fp != nil && fp.BtcPk == fpPK.BtcPk.MarshalHex()
}, eventuallyWaitTimeOut, eventuallyPollTime)

// set up a BTC delegation
stakingMsgTx, stakingSlashingInfo, _, _ := tm.CreateBTCDelegationWithoutIncl(t, fpSK)
stakingMsgTx, stakingSlashingInfo, unbondingSlashingInfo, _ := tm.CreateBTCDelegationWithoutIncl(t, fpSK)
stakingMsgTxHash := stakingMsgTx.TxHash()

// check if BTC delegation state in indexer db is PENDING
require.Eventually(t, func() bool {
state, err := tm.DbClient.GetBTCDelegationState(context.Background(), stakingMsgTxHash.String())
if err != nil {
return false
}
return state.String() == types.StatePending.String()
}, eventuallyWaitTimeOut, eventuallyPollTime)

// generate and insert new covenant signature
slashingSpendPath, err := stakingSlashingInfo.StakingInfo.SlashingPathSpendInfo()
require.NoError(t, err)
unbondingSlashingPathSpendInfo, err := unbondingSlashingInfo.UnbondingInfo.SlashingPathSpendInfo()
require.NoError(t, err)
stakingOutIdx, err := outIdx(stakingSlashingInfo.StakingTx, stakingSlashingInfo.StakingInfo.StakingOutput)
require.NoError(t, err)
tm.addCovenantSig(
t,
tm.BabylonClient.MustGetAddr(),
stakingMsgTx,
&stakingMsgTxHash,
fpSK, slashingSpendPath,
stakingSlashingInfo,
unbondingSlashingInfo,
unbondingSlashingPathSpendInfo,
stakingOutIdx,
)

// check if BTC delegation state in indexer db is VERIFIED
require.Eventually(t, func() bool {
state, err := tm.DbClient.GetBTCDelegationState(context.Background(), stakingMsgTxHash.String())
if err != nil {
return false
}
return state.String() == types.StateVerified.String()
}, eventuallyWaitTimeOut, eventuallyPollTime)

// send staking tx to Bitcoin node's mempool
_, err := tm.WalletClient.SendRawTransaction(stakingMsgTx, true)
_, err = tm.WalletClient.SendRawTransaction(stakingMsgTx, true)
require.NoError(t, err)

require.Eventually(t, func() bool {
Expand Down Expand Up @@ -114,7 +163,6 @@ func TestActivatingDelegation(t *testing.T) {
}
tm.CatchUpBTCLightClient(t)
}()

wg.Wait()

tm.SubmitInclusionProof(t, stakingMsgTxHash.String(), stakingTxInfo)
Expand All @@ -130,8 +178,17 @@ func TestActivatingDelegation(t *testing.T) {
return resp.BtcDelegation.Active
}, eventuallyWaitTimeOut, eventuallyPollTime)

// check if BTC delegation state in indexer db is ACTIVE
require.Eventually(t, func() bool {
state, err := tm.DbClient.GetBTCDelegationState(context.Background(), stakingMsgTxHash.String())
if err != nil {
return false
}
return state.String() == types.StateActive.String()
}, eventuallyWaitTimeOut, eventuallyPollTime)

// check that the staking tx is already stored
_ = tm.WaitForStakingTxStored(t, stakingMsgTxHash.String())
// _ = tm.WaitForStakingTxStored(t, stakingMsgTxHash.String())

// check that the staking event is already stored
tm.CheckNextStakingEvent(t, stakingMsgTxHash.String())
Expand Down
26 changes: 13 additions & 13 deletions e2etest/test_manager_btcstaking.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func (tm *TestManager) CreateBTCDelegationWithoutIncl(
require.NoError(t, err)

// Generate all data necessary for unbonding
unbondingSlashingInfo, unbondingSlashingPathSpendInfo, unbondingTxBytes, slashingTxSig := tm.createUnbondingData(
unbondingSlashingInfo, _, unbondingTxBytes, slashingTxSig := tm.createUnbondingData(
t,
fpPK,
bsParams,
Expand Down Expand Up @@ -298,18 +298,18 @@ func (tm *TestManager) CreateBTCDelegationWithoutIncl(
require.NoError(t, err)
t.Logf("submitted MsgCreateBTCDelegation")

// generate and insert new covenant signature, to activate the BTC delegation
tm.addCovenantSig(
t,
signerAddr,
stakingMsgTx,
stakingMsgTxHash,
fpSK, slashingSpendPath,
stakingSlashingInfo,
unbondingSlashingInfo,
unbondingSlashingPathSpendInfo,
stakingOutIdx,
)
//// generate and insert new covenant signature, to activate the BTC delegation
//tm.addCovenantSig(
// t,
// signerAddr,
// stakingMsgTx,
// stakingMsgTxHash,
// fpSK, slashingSpendPath,
// stakingSlashingInfo,
// unbondingSlashingInfo,
// unbondingSlashingPathSpendInfo,
// stakingOutIdx,
//)

return stakingMsgTx, stakingSlashingInfo, unbondingSlashingInfo, tm.WalletPrivKey
}
Expand Down
8 changes: 7 additions & 1 deletion internal/services/finality_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,13 @@ func (s *Service) processFinalityProviderStateChangeEvent(
func (s *Service) validateFinalityProviderCreatedEvent(
fpCreated *bbntypes.EventFinalityProviderCreated,
) *types.Error {
// TODO: Implement validation logic
if fpCreated.BtcPkHex == "" {
return types.NewErrorWithMsg(
http.StatusInternalServerError,
types.InternalServiceError,
"finality provider created event missing btc public key",
)
}
return nil
}

Expand Down

0 comments on commit 63dd026

Please sign in to comment.