Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(*): handle funding response #25

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion harness/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func startHarness(cmdCtx context.Context, cfg config.Config) error {
if err != nil {
return err
}
stakers = append(stakers, NewBTCStaker(tm, stakerSender, fpMgr.randomFp().btcPk.MustToBTCPK(), tm.fundingRequests))
stakers = append(stakers, NewBTCStaker(tm, stakerSender, fpMgr.randomFp().btcPk.MustToBTCPK(), tm.fundingRequests, tm.fundingResponse))
}

// periodically check if we need to fund the staker
Expand Down
54 changes: 41 additions & 13 deletions harness/btcstaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,26 @@ import (
)

type BTCStaker struct {
tm *TestManager
client *SenderWithBabylonClient
fpPK *btcec.PublicKey
fundingRequest chan sdk.AccAddress
tm *TestManager
client *SenderWithBabylonClient
fpPK *btcec.PublicKey
fundingRequest chan sdk.AccAddress
fundingResponse chan sdk.AccAddress
}

func NewBTCStaker(
tm *TestManager,
client *SenderWithBabylonClient,
finalityProviderPublicKey *btcec.PublicKey,
fundingRequest chan sdk.AccAddress,
fundingResponse chan sdk.AccAddress,
) *BTCStaker {
return &BTCStaker{
tm: tm,
client: client,
fpPK: finalityProviderPublicKey,
fundingRequest: fundingRequest,
tm: tm,
client: client,
fpPK: finalityProviderPublicKey,
fundingRequest: fundingRequest,
fundingResponse: fundingResponse,
}
}

Expand Down Expand Up @@ -88,19 +91,44 @@ func (s *BTCStaker) runForever(ctx context.Context, stakerAddress btcutil.Addres
err = s.buildAndSendStakingTransaction(ctx, stakerAddress, stakerPk, &paramsResp.Params)
if err != nil {
fmt.Printf("🚫 Err in BTC Staker (%s), err: %v\n", s.client.BabylonAddress.String(), err)

if strings.Contains(strings.ToLower(err.Error()), "insufficient funds") {
select {
case s.fundingRequest <- s.client.BabylonAddress:
time.Sleep(5 * time.Second)
default:
fmt.Println("fundingRequest channel is full or closed")
if s.requestFunding(ctx) {
fmt.Printf("✅ Received funding for %s\n", s.client.BabylonAddress.String())
} else {
fmt.Printf("🚫 Funding timeout or context canceled for %s\n", s.client.BabylonAddress.String())
}
}
}
}
}
}

// Helper function to request funding and wait for a response
func (s *BTCStaker) requestFunding(ctx context.Context) bool {
// Attempt to send a funding request with a timeout
select {
case s.fundingRequest <- s.client.BabylonAddress:
fmt.Printf("📤 Funding requested for %s\n", s.client.BabylonAddress.String())
case <-ctx.Done():
return false
case <-time.After(5 * time.Second):
fmt.Println("⚠️ Funding request channel is full or unresponsive")
return false
}

// Wait for funding response or timeout
waitCtx, cancel := context.WithTimeout(ctx, 1*time.Minute)
defer cancel()

select {
case <-waitCtx.Done():
return false
case addr := <-s.fundingResponse:
return addr.String() == s.client.BabylonAddress.String()
}
}

func (s *BTCStaker) NewBabylonBip322Pop(
msg []byte,
w wire.TxWitness,
Expand Down
12 changes: 4 additions & 8 deletions harness/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type TestManager struct {
babylonDir string
benchConfig benchcfg.Config
fundingRequests chan sdk.AccAddress
fundingResponse chan sdk.AccAddress
fundingAddress sdk.AccAddress
}

Expand Down Expand Up @@ -221,7 +222,8 @@ func StartManager(ctx context.Context, outputsInWallet uint32, epochInterval uin
manger: manager,
babylonDir: babylonDir,
benchConfig: runCfg,
fundingRequests: make(chan sdk.AccAddress, 100),
fundingRequests: make(chan sdk.AccAddress, 250),
fundingResponse: make(chan sdk.AccAddress, 250),
fundingAddress: fundingAddress,
}, nil
}
Expand Down Expand Up @@ -385,14 +387,8 @@ func (tm *TestManager) fundBnnAddress(
return fmt.Errorf("context error before funding: %w", err)
}

fundingAccount := tm.BabylonClientNode0.MustGetAddr()
fundingAddress, err := sdk.AccAddressFromBech32(fundingAccount)
if err != nil {
return fmt.Errorf("failed to parse funding address: %w", err)
}

amount := types.NewCoins(types.NewInt64Coin("ubbn", 100_000_000))
msg := banktypes.NewMsgSend(fundingAddress, addr, amount)
msg := banktypes.NewMsgSend(tm.fundingAddress, addr, amount)

resp, err := tm.BabylonClientNode0.ReliablySendMsg(ctx, msg, nil, nil)
if err != nil {
Expand Down