A few nitpicks with recent getrawmempool updates.

This commit addresses a few nitpicks in the recent getrawmppol update
which populates the starting and current priority fields.

In particular:

- Move the new calcInputValueAge function before the function which
  invokes it so it is consistent with the rest of the mempool code
- Double space after periods for consistency
- Correct the comments for calcInputValueAge to indiciate that inputs
  which are in the the memory pool count as zero toward the value age
  rather than the incorrect claim that that the overal input value age is
  zero when one of them does
- Rename endingPriority to currentPriority to match the RPC field and its
  actual function
- Make the comment about using zero when input transactions can't be found
  for some reason more accurate since there can be (and frequently is)
  more than one input transaction
This commit is contained in:
Dave Collins 2014-11-17 00:18:44 -06:00
parent a49b0d05b3
commit 1973aa5fd5
2 changed files with 45 additions and 47 deletions

View file

@ -671,10 +671,45 @@ func (mp *txMemPool) addTransaction(tx *btcutil.Tx, height, fee int64) {
mp.lastUpdated = time.Now() mp.lastUpdated = time.Now()
} }
// StartingPriority calculates the priority of this tx descriptor's // calcInputValueAge is a helper function used to calculate the input age of
// underlying transaction relative to when it was first added to the mempool. // a transaction. The input age for a txin is the number of confirmations
// The result is lazily computed and then cached for subsequent function // since the referenced txout multiplied by its output value. The total input
// calls. // 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 { func (txD *TxDesc) StartingPriority(txStore btcchain.TxStore) float64 {
// Return our cached result. // Return our cached result.
if txD.startingPriority != float64(0) { if txD.startingPriority != float64(0) {
@ -697,43 +732,6 @@ func (txD *TxDesc) CurrentPriority(txStore btcchain.TxStore, nextBlockHeight int
return calcPriority(txD.Tx, txSize, inputAge) 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 // checkPoolDoubleSpend checks whether or not the passed transaction is
// attempting to spend coins already spent by other transactions in the pool. // 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 // Note it does not check for double spends against transactions already in the

View file

@ -2305,14 +2305,14 @@ func handleGetRawMempool(s *rpcServer, cmd btcjson.Cmd, closeChan <-chan struct{
mp.RLock() mp.RLock()
defer mp.RUnlock() defer mp.RUnlock()
for _, desc := range descs { for _, desc := range descs {
// Calculate the starting and ending priority from the // Calculate the starting and current priority from the
// the tx's inputs. If we can't find the input for some // the tx's inputs. Use zeros if one or more of the
// reason, then we display zero in place. // input transactions can't be found for some reason.
var startingPriority, currentPriority float64
inputTxs, err := mp.fetchInputTransactions(desc.Tx) inputTxs, err := mp.fetchInputTransactions(desc.Tx)
var startingPriority, endingPriority float64
if err == nil { if err == nil {
startingPriority = desc.StartingPriority(inputTxs) startingPriority = desc.StartingPriority(inputTxs)
endingPriority = desc.CurrentPriority(inputTxs, currentPriority = desc.CurrentPriority(inputTxs,
newestHeight+1) newestHeight+1)
} }
@ -2322,7 +2322,7 @@ func handleGetRawMempool(s *rpcServer, cmd btcjson.Cmd, closeChan <-chan struct{
Time: desc.Added.Unix(), Time: desc.Added.Unix(),
Height: desc.Height, Height: desc.Height,
StartingPriority: startingPriority, StartingPriority: startingPriority,
CurrentPriority: endingPriority, CurrentPriority: currentPriority,
Depends: make([]string, 0), Depends: make([]string, 0),
} }
for _, txIn := range desc.Tx.MsgTx().TxIn { for _, txIn := range desc.Tx.MsgTx().TxIn {