Skip to content

Commit

Permalink
Avoid needless balance calculations from ticket autobuyer
Browse files Browse the repository at this point in the history
When the ticketbuyer.balancetomaintainabsolute flag is unset/zero,
there is no reason that the account balance needs to be calculated.
On busier wallets, avoid this results in a significant performance
improvement.

This flag is already broken for a different reason (change can land in
other accounts, causing the account balance to drop below this
setting) but there are still some people using the flag despite this,
and compensating by setting it higher than they need.
  • Loading branch information
jrick committed Jan 30, 2023
1 parent 9894560 commit f3cb6d0
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions ticketbuyer/tb.go
Expand Up @@ -254,36 +254,42 @@ func (tb *TB) buy(ctx context.Context, passphrase []byte, tip *wire.BlockHeader,
splitAccount := cfg.TicketSplitAccount
changeAccount := cfg.ChangeAccount

// Determine how many tickets to buy
bal, err := w.AccountBalance(ctx, account, minconf)
if err != nil {
return err
}
spendable := bal.Spendable

if spendable < maintain {
log.Debugf("Skipping purchase: low available balance")
return nil
}
spendable -= maintain
sdiff, err := w.NextStakeDifficultyAfterHeader(ctx, tip)
if err != nil {
return err
}
buy := int(spendable / sdiff)
if buy == 0 {
log.Debugf("Skipping purchase: low available balance")
return nil
}
max := int(w.ChainParams().MaxFreshStakePerBlock)
if buy > max {
buy = max

// Determine how many tickets to buy
var buy int
if maintain != 0 {
bal, err := w.AccountBalance(ctx, account, minconf)
if err != nil {
return err
}
spendable := bal.Spendable
if spendable < maintain {
log.Debugf("Skipping purchase: low available balance")
return nil
}
spendable -= maintain
buy = int(spendable / sdiff)
if buy == 0 {
log.Debugf("Skipping purchase: low available balance")
return nil
}
max := int(w.ChainParams().MaxFreshStakePerBlock)
if buy > max {
buy = max
}
} else {
buy = int(w.ChainParams().MaxFreshStakePerBlock)
}
if limit == 0 && csppServer != "" {
buy = 1
} else if limit > 0 && buy > limit {
buy = limit
}

purchaseTicketReq := &wallet.PurchaseTicketsRequest{
Count: buy,
SourceAccount: account,
Expand Down

0 comments on commit f3cb6d0

Please sign in to comment.