@@ -1716,10 +1716,16 @@ func (w *Wallet) CalculateAccountBalances(account uint32, confirms int32) (Balan
17161716 }
17171717
17181718 bals .Total += output .Amount
1719- if output .FromCoinBase && ! confirmed (int32 (w .chainParams .CoinbaseMaturity ),
1720- output .Height , syncBlock .Height ) {
1719+ if output .FromCoinBase && ! hasMinConfs (
1720+ int32 (w .chainParams .CoinbaseMaturity ),
1721+ output .Height , syncBlock .Height ,
1722+ ) {
1723+
17211724 bals .ImmatureReward += output .Amount
1722- } else if confirmed (confirms , output .Height , syncBlock .Height ) {
1725+ } else if hasMinConfs (
1726+ confirms , output .Height , syncBlock .Height ,
1727+ ) {
1728+
17231729 bals .Spendable += output .Amount
17241730 }
17251731 }
@@ -2120,8 +2126,11 @@ func (c CreditCategory) String() string {
21202126// this package at a later time.
21212127func RecvCategory (details * wtxmgr.TxDetails , syncHeight int32 , net * chaincfg.Params ) CreditCategory {
21222128 if blockchain .IsCoinBaseTx (& details .MsgTx ) {
2123- if confirmed (int32 (net .CoinbaseMaturity ), details .Block .Height ,
2124- syncHeight ) {
2129+ if hasMinConfs (
2130+ int32 (net .CoinbaseMaturity ), details .Block .Height ,
2131+ syncHeight ,
2132+ ) {
2133+
21252134 return CreditGenerate
21262135 }
21272136 return CreditImmature
@@ -2146,7 +2155,9 @@ func listTransactions(tx walletdb.ReadTx, details *wtxmgr.TxDetails, addrMgr *wa
21462155 if details .Block .Height != - 1 {
21472156 blockHashStr = details .Block .Hash .String ()
21482157 blockTime = details .Block .Time .Unix ()
2149- confirmations = int64 (confirms (details .Block .Height , syncHeight ))
2158+ confirmations = int64 (
2159+ calcConf (details .Block .Height , syncHeight ),
2160+ )
21502161 }
21512162
21522163 results := []btcjson.ListTransactionsResult {}
@@ -2596,15 +2607,24 @@ func (w *Wallet) GetTransaction(txHash chainhash.Hash) (*GetTransactionResult,
25962607
25972608 res = GetTransactionResult {
25982609 Summary : makeTxSummary (dbtx , w , txDetail ),
2599- Timestamp : txDetail .Block .Time .Unix (),
2600- Confirmations : txDetail .Block .Height ,
2610+ BlockHash : nil ,
2611+ Height : - 1 ,
2612+ Confirmations : 0 ,
2613+ Timestamp : 0 ,
26012614 }
26022615
26032616 // If it is a confirmed transaction we set the corresponding
2604- // block height and hash.
2617+ // block height, timestamp, hash, and confirmations .
26052618 if txDetail .Block .Height != - 1 {
26062619 res .Height = txDetail .Block .Height
2620+ res .Timestamp = txDetail .Block .Time .Unix ()
26072621 res .BlockHash = & txDetail .Block .Hash
2622+
2623+ bestBlock := w .SyncedTo ()
2624+ blockHeight := txDetail .Block .Height
2625+ res .Confirmations = calcConf (
2626+ blockHeight , bestBlock .Height ,
2627+ )
26082628 }
26092629
26102630 return nil
@@ -2750,11 +2770,18 @@ func (w *Wallet) AccountBalances(scope waddrmgr.KeyScope,
27502770 }
27512771 for i := range unspentOutputs {
27522772 output := & unspentOutputs [i ]
2753- if ! confirmed (requiredConfs , output .Height , syncBlock .Height ) {
2773+ if ! hasMinConfs (
2774+ requiredConfs , output .Height , syncBlock .Height ,
2775+ ) {
2776+
27542777 continue
27552778 }
2756- if output .FromCoinBase && ! confirmed (int32 (w .ChainParams ().CoinbaseMaturity ),
2757- output .Height , syncBlock .Height ) {
2779+
2780+ if output .FromCoinBase && ! hasMinConfs (
2781+ int32 (w .ChainParams ().CoinbaseMaturity ),
2782+ output .Height , syncBlock .Height ,
2783+ ) {
2784+
27582785 continue
27592786 }
27602787 _ , addrs , _ , err := txscript .ExtractPkScriptAddrs (output .PkScript , w .chainParams )
@@ -2845,17 +2872,20 @@ func (w *Wallet) ListUnspent(minconf, maxconf int32,
28452872 for i := range unspent {
28462873 output := unspent [i ]
28472874
2848- // Outputs with fewer confirmations than the minimum or more
2849- // confs than the maximum are excluded.
2850- confs := confirms (output .Height , syncBlock .Height )
2875+ // Outputs with fewer confirmations than the minimum or
2876+ // more confs than the maximum are excluded.
2877+ confs := calcConf (output .Height , syncBlock .Height )
28512878 if confs < minconf || confs > maxconf {
28522879 continue
28532880 }
28542881
28552882 // Only mature coinbase outputs are included.
28562883 if output .FromCoinBase {
28572884 target := int32 (w .ChainParams ().CoinbaseMaturity )
2858- if ! confirmed (target , output .Height , syncBlock .Height ) {
2885+ if ! hasMinConfs (
2886+ target , output .Height , syncBlock .Height ,
2887+ ) {
2888+
28592889 continue
28602890 }
28612891 }
@@ -3330,19 +3360,27 @@ func (w *Wallet) newChangeAddress(addrmgrNs walletdb.ReadWriteBucket,
33303360 return addrs [0 ].Address (), nil
33313361}
33323362
3333- // confirmed checks whether a transaction at height txHeight has met minconf
3334- // confirmations for a blockchain at height curHeight .
3335- func confirmed ( minconf , txHeight , curHeight int32 ) bool {
3336- return confirms (txHeight , curHeight ) >= minconf
3363+ // hasMinConfs returns whether a transaction has met at least minConf
3364+ // confirmations at the current block height.
3365+ func hasMinConfs ( minConf , txHeight , curHeight int32 ) bool {
3366+ return calcConf (txHeight , curHeight ) >= minConf
33373367}
33383368
3339- // confirms returns the number of confirmations for a transaction in a block at
3340- // height txHeight (or -1 for an unconfirmed tx) given the chain height
3341- // curHeight .
3342- func confirms (txHeight , curHeight int32 ) int32 {
3369+ // calcConf returns the number of confirmations for a transaction given its
3370+ // containing block height and the current best block height. Unconfirmed
3371+ // transactions have a height of -1 and are considered to have 0 confirmations .
3372+ func calcConf (txHeight , curHeight int32 ) int32 {
33433373 switch {
3344- case txHeight == - 1 , txHeight > curHeight :
3374+ // Unconfirmed transactions have 0 confirmations.
3375+ case txHeight == - 1 :
33453376 return 0
3377+
3378+ // A transaction in a block after the current best block is considered
3379+ // unconfirmed. This can happen during a chain reorg.
3380+ case txHeight > curHeight :
3381+ return 0
3382+
3383+ // Confirmed transactions have at least one confirmation.
33463384 default :
33473385 return curHeight - txHeight + 1
33483386 }
@@ -3414,8 +3452,12 @@ func (w *Wallet) TotalReceivedForAccounts(scope waddrmgr.KeyScope,
34143452 }
34153453 res := & results [acctIndex ]
34163454 res .TotalReceived += cred .Amount
3417- res .LastConfirmation = confirms (
3418- detail .Block .Height , syncBlock .Height )
3455+
3456+ confs := calcConf (
3457+ detail .Block .Height ,
3458+ syncBlock .Height ,
3459+ )
3460+ res .LastConfirmation = confs
34193461 }
34203462 }
34213463 }
0 commit comments