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

client/dcr: emit notification when mempool txs are seen #2554

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 9 additions & 0 deletions client/asset/dcr/dcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -5638,6 +5638,15 @@ func (dcr *ExchangeWallet) monitorBlocks(ctx context.Context) {
checkTip()

case walletTip := <-walletBlock:
if walletTip == nil {
// Mempool tx seen.
if bal, err := dcr.Balance(); err != nil {
dcr.log.Errorf("Error getting balance after mempool tx notification: %v", err)
} else {
dcr.emit.BalanceChange(bal)
}
continue
}
if queuedBlock != nil && walletTip.height >= queuedBlock.height {
if !queuedBlock.queue.Stop() && walletTip.hash == queuedBlock.hash {
continue
Expand Down
7 changes: 7 additions & 0 deletions client/asset/dcr/spv.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,13 @@ func (w *spvWallet) notesLoop(ctx context.Context, dcrw *wallet.Wallet) {
select {
case n := <-txNotes.C:
if len(n.AttachedBlocks) == 0 {
if len(n.UnminedTransactions) > 0 {
select {
case w.tipChan <- nil:
default:
w.log.Warnf("tx report channel was blocking")
}
}
continue
}
lastBlock := n.AttachedBlocks[len(n.AttachedBlocks)-1]
Expand Down
12 changes: 12 additions & 0 deletions client/asset/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -1385,6 +1385,13 @@ type TipChangeNote struct {
Data any `json:"data"`
}

// BalanceChangeNote can be sent when the wallet detects a balance change
// between tip changes.
type BalanceChangeNote struct {
AssetID uint32
Balance *Balance
}

// CustomWalletNote is any other information the wallet wishes to convey to
// the user.
type CustomWalletNote struct {
Expand Down Expand Up @@ -1430,3 +1437,8 @@ func (e *WalletEmitter) TipChange(tip uint64, datas ...any) {
}
e.emit(&TipChangeNote{AssetID: e.assetID, Tip: tip, Data: data})
}

// BalanceChange sends a BalanceChangeNote.
func (e *WalletEmitter) BalanceChange(bal *Balance) {
e.emit(&BalanceChangeNote{AssetID: e.assetID, Balance: bal})
}
29 changes: 26 additions & 3 deletions client/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -2100,16 +2100,20 @@ func (c *Core) updateWalletBalance(wallet *xcWallet) (*WalletBalance, error) {
if err != nil {
return nil, err
}
return walletBal, c.storeAndSendWalletBalance(wallet, walletBal)
}

func (c *Core) storeAndSendWalletBalance(wallet *xcWallet, walletBal *WalletBalance) error {
wallet.setBalance(walletBal)

// Store the db.Balance.
err = c.db.UpdateBalance(wallet.dbID, walletBal.Balance)
err := c.db.UpdateBalance(wallet.dbID, walletBal.Balance)
if err != nil {
return nil, fmt.Errorf("error updating %s balance in database: %w", unbip(wallet.AssetID), err)
return fmt.Errorf("error updating %s balance in database: %w", unbip(wallet.AssetID), err)
}

c.notify(newBalanceNote(wallet.AssetID, walletBal))
return walletBal, nil
return nil
}

// lockedAmounts returns the total amount locked in unredeemed and unrefunded
Expand Down Expand Up @@ -9411,6 +9415,25 @@ func (c *Core) handleWalletNotification(ni asset.WalletNotification) {
switch n := ni.(type) {
case *asset.TipChangeNote:
c.tipChange(n.AssetID)
case *asset.BalanceChangeNote:
w, ok := c.wallet(n.AssetID)
if !ok {
return
}
contractLockedAmt, orderLockedAmt, bondLockedAmt := c.lockedAmounts(n.AssetID)
bal := &WalletBalance{
Balance: &db.Balance{
Balance: *n.Balance,
Stamp: time.Now(),
},
OrderLocked: orderLockedAmt,
ContractLocked: contractLockedAmt,
BondLocked: bondLockedAmt,
}
if err := c.storeAndSendWalletBalance(w, bal); err != nil {
c.log.Errorf("Error storing and sending emitted balance: %v", err)
}
return // Notification sent already.
}
c.notify(newWalletNote(ni))
}
Expand Down
Loading