diff --git a/mempool.go b/mempool.go index 3b46eb13..c525aa20 100644 --- a/mempool.go +++ b/mempool.go @@ -671,10 +671,45 @@ func (mp *txMemPool) addTransaction(tx *btcutil.Tx, height, fee int64) { mp.lastUpdated = time.Now() } -// StartingPriority calculates the priority of this tx descriptor's -// underlying transaction relative to when it was first added to the mempool. -// The result is lazily computed and then cached for subsequent function -// calls. +// calcInputValueAge is a helper function used to calculate the input age of +// a transaction. The input age for a txin is the number of confirmations +// since the referenced txout multiplied by its output value. The total input +// age is the sum of this value for each txin. Any inputs to the transaction +// which are currently in the mempool and hence not mined into a block yet, +// contribute no additional input age to the transaction. +func calcInputValueAge(txDesc *TxDesc, txStore btcchain.TxStore, nextBlockHeight int64) float64 { + var totalInputAge float64 + for _, txIn := range txDesc.Tx.MsgTx().TxIn { + originHash := &txIn.PreviousOutPoint.Hash + originIndex := txIn.PreviousOutPoint.Index + + // Don't attempt to accumulate the total input age if the txIn + // in question doesn't exist. + if txData, exists := txStore[*originHash]; exists && txData.Tx != nil { + // Inputs with dependencies currently in the mempool + // have their block height set to a special constant. + // Their input age should computed as zero since their + // parent hasn't made it into a block yet. + var inputAge int64 + if txData.BlockHeight == mempoolHeight { + inputAge = 0 + } else { + inputAge = nextBlockHeight - txData.BlockHeight + } + + // Sum the input value times age. + originTxOut := txData.Tx.MsgTx().TxOut[originIndex] + inputValue := originTxOut.Value + totalInputAge += float64(inputValue * inputAge) + } + } + + return totalInputAge +} + +// StartingPriority calculates the priority of this tx descriptor's underlying +// transaction relative to when it was first added to the mempool. The result +// is lazily computed and then cached for subsequent function calls. func (txD *TxDesc) StartingPriority(txStore btcchain.TxStore) float64 { // Return our cached result. if txD.startingPriority != float64(0) { @@ -697,43 +732,6 @@ func (txD *TxDesc) CurrentPriority(txStore btcchain.TxStore, nextBlockHeight int return calcPriority(txD.Tx, txSize, inputAge) } -// calcInputValueAge is a helper function used to calculate the input age of -// a transaction. The input age for a txin is the number of confirmations -// since the referenced txout multiplied by its output value. -// The total input age is the sum of this value for each txin. If the tx -// depends on one currently in the mempool, then its input age is zero. -func calcInputValueAge(txDesc *TxDesc, txStore btcchain.TxStore, - nextBlockHeight int64) float64 { - var totalInputAge float64 - for _, txIn := range txDesc.Tx.MsgTx().TxIn { - originHash := &txIn.PreviousOutPoint.Hash - originIndex := txIn.PreviousOutPoint.Index - - // Don't attempt to accumulate the total input age if the txIn - // in question doesn't exist. - if txData, exists := txStore[*originHash]; exists && txData.Tx != nil { - originTxOut := txData.Tx.MsgTx().TxOut[originIndex] - - // Transactions with dependencies currently in the - // mempool have their block height set to a special - // constant. Their input age should computed as zero - // since their parent hasn't made it into a block yet. - var inputAge int64 - if txData.BlockHeight == mempoolHeight { - inputAge = 0 - } else { - inputAge = nextBlockHeight - txData.BlockHeight - } - - // Sum the input value times age. - inputValue := originTxOut.Value - totalInputAge += float64(inputValue * inputAge) - } - } - - return totalInputAge -} - // checkPoolDoubleSpend checks whether or not the passed transaction is // attempting to spend coins already spent by other transactions in the pool. // Note it does not check for double spends against transactions already in the diff --git a/rpcserver.go b/rpcserver.go index d57e3a8b..e0376b57 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -2305,14 +2305,14 @@ func handleGetRawMempool(s *rpcServer, cmd btcjson.Cmd, closeChan <-chan struct{ mp.RLock() defer mp.RUnlock() for _, desc := range descs { - // Calculate the starting and ending priority from the - // the tx's inputs. If we can't find the input for some - // reason, then we display zero in place. + // Calculate the starting and current priority from the + // the tx's inputs. Use zeros if one or more of the + // input transactions can't be found for some reason. + var startingPriority, currentPriority float64 inputTxs, err := mp.fetchInputTransactions(desc.Tx) - var startingPriority, endingPriority float64 if err == nil { startingPriority = desc.StartingPriority(inputTxs) - endingPriority = desc.CurrentPriority(inputTxs, + currentPriority = desc.CurrentPriority(inputTxs, newestHeight+1) } @@ -2322,7 +2322,7 @@ func handleGetRawMempool(s *rpcServer, cmd btcjson.Cmd, closeChan <-chan struct{ Time: desc.Added.Unix(), Height: desc.Height, StartingPriority: startingPriority, - CurrentPriority: endingPriority, + CurrentPriority: currentPriority, Depends: make([]string, 0), } for _, txIn := range desc.Tx.MsgTx().TxIn {