Skip to content

Commit

Permalink
fund staker faster
Browse files Browse the repository at this point in the history
  • Loading branch information
Lazar955 committed Nov 11, 2024
1 parent c132eb3 commit ad4df58
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
4 changes: 3 additions & 1 deletion harness/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ func startHarness(cmdCtx context.Context, cfg config.Config) error {
stakers = append(stakers, NewBTCStaker(tm, stakerSender, fpMgr.randomFp().btcPk.MustToBTCPK(), tm.fundingRequests))
}

// periodically check if we need to fund the staker
go tm.fundForever(ctx)

// fund all stakers
if err := tm.fundAllParties(ctx, senders(stakers)); err != nil {
return err
Expand Down Expand Up @@ -117,7 +120,6 @@ func startHarness(cmdCtx context.Context, cfg config.Config) error {
fpMgr.Start(ctx)

go tm.listBlocksForever(ctx)
go tm.fundForever(ctx)

select {
case <-ctx.Done():
Expand Down
9 changes: 7 additions & 2 deletions harness/btcstaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,13 @@ 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(err.Error(), "Insufficient funds") {
s.fundingRequest <- s.client.BabylonAddress
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")
}
}
}
}
Expand Down
14 changes: 10 additions & 4 deletions harness/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func StartManager(ctx context.Context, outputsInWallet uint32, epochInterval uin
manger: manager,
babylonDir: babylonDir,
benchConfig: runCfg,
fundingRequests: make(chan sdk.AccAddress),
fundingRequests: make(chan sdk.AccAddress, 100),
}, nil
}

Expand Down Expand Up @@ -366,6 +366,7 @@ func (tm *TestManager) fundAllParties(

return nil
}

func (tm *TestManager) fundBnnAddress(
ctx context.Context,
addr sdk.AccAddress,
Expand Down Expand Up @@ -425,14 +426,19 @@ func (tm *TestManager) listBlocksForever(ctx context.Context) {
}

func (tm *TestManager) fundForever(ctx context.Context) {
ticker := time.NewTicker(3 * time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
case addr := <-tm.fundingRequests:
if err := tm.fundBnnAddress(ctx, addr); err != nil {
fmt.Printf("🚫 Failed to fund addr %s, err %v\n", addr.String(), err)
}
go func() {
if err := tm.fundBnnAddress(ctx, addr); err != nil {
fmt.Printf("🚫 Failed to fund addr %s, err %v\n", addr.String(), err)
}
}()
}
}
}
Expand Down

0 comments on commit ad4df58

Please sign in to comment.