Fix some remaining issues with reported balances.

CalculateBalance now works correctly: if confirmations is 0, all UTXOs
will be used for the balance.  Otherwise, unconfirmed UTXOs will be
exclused.  1 confirmation will allow the UTXO height and current block
height to be equal.  Even though the difference is zero, the
transaction including the UTXO has been mined into one block.

This change also remove extraneous account balance notifications for
connected and disconnected blocks.
This commit is contained in:
Josh Rickmar 2013-10-28 16:58:35 -04:00
parent b34563bbd9
commit aad61db6d0
2 changed files with 14 additions and 25 deletions

21
cmd.go
View file

@ -94,6 +94,9 @@ func NewBtcWalletStore() *BtcWalletStore {
}
// Rollback rolls back each BtcWallet saved in the store.
//
// TODO(jrick): This must also roll back the UTXO and TX stores, and notify
// all wallets of new account balances.
func (s *BtcWalletStore) Rollback(height int64, hash *btcwire.ShaHash) {
s.Lock()
for _, w := range s.m {
@ -267,6 +270,12 @@ func getCurHeight() (height int64) {
// CalculateBalance sums the amounts of all unspent transaction
// outputs to addresses of a wallet and returns the balance as a
// float64.
//
// If confirmations is 0, all UTXOs, even those not present in a
// block (height -1), will be used to get the balance. Otherwise,
// a UTXO must be in a block. If confirmations is 1 or greater,
// the balance will be calculated based on how many how many blocks
// include a UTXO.
func (w *BtcWallet) CalculateBalance(confirmations int) float64 {
var bal uint64 // Measured in satoshi
@ -277,12 +286,10 @@ func (w *BtcWallet) CalculateBalance(confirmations int) float64 {
w.UtxoStore.RLock()
for _, u := range w.UtxoStore.s {
if int(height-u.Height) >= confirmations {
// Utxos not yet in blocks (height -1) should only be
// added if confirmations is 0.
if u.Height != -1 || (confirmations == 0 && u.Height == -1) {
bal += u.Amt
}
// Utxos not yet in blocks (height -1) should only be
// added if confirmations is 0.
if confirmations == 0 || (u.Height != -1 && int(height-u.Height+1) >= confirmations) {
bal += u.Amt
}
}
w.UtxoStore.RUnlock()
@ -581,7 +588,7 @@ func (w *BtcWallet) newBlockTxHandler(result interface{}, e *btcjson.Error) bool
log.Errorf("cannot sync dirty wallet: %v", err)
}
confirmed := w.CalculateBalance(6)
confirmed := w.CalculateBalance(1)
unconfirmed := w.CalculateBalance(0) - confirmed
NotifyWalletBalance(frontendNotificationMaster, w.name, confirmed)
NotifyWalletBalanceUnconfirmed(frontendNotificationMaster, w.name, unconfirmed)

View file

@ -375,15 +375,6 @@ func NtfnBlockConnected(r interface{}) {
}
frontendNotificationMaster <- msg
wallets.RLock()
for _, w := range wallets.m {
confirmed := w.CalculateBalance(6)
unconfirmed := w.CalculateBalance(0) - confirmed
NotifyWalletBalance(frontendNotificationMaster, w.name, confirmed)
NotifyWalletBalanceUnconfirmed(frontendNotificationMaster, w.name, unconfirmed)
}
wallets.RUnlock()
// Remove all mined transactions from pool.
UnminedTxs.Lock()
for _, txid := range minedTxs {
@ -457,15 +448,6 @@ func NtfnBlockDisconnected(r interface{}) {
return
}
frontendNotificationMaster <- msg
wallets.RLock()
for _, w := range wallets.m {
confirmed := w.CalculateBalance(6)
unconfirmed := w.CalculateBalance(0) - confirmed
NotifyWalletBalance(frontendNotificationMaster, w.name, confirmed)
NotifyWalletBalanceUnconfirmed(frontendNotificationMaster, w.name, unconfirmed)
}
wallets.RUnlock()
}
var duplicateOnce sync.Once