[rpc mempool] Add support for unbroadcastcount to RPC getmempoolinfo.

This commit is contained in:
Jonathan Moody 2022-07-14 17:58:24 -04:00 committed by Roy Lee
parent 13e31d033a
commit abb1b8b388
5 changed files with 26 additions and 0 deletions

View file

@ -343,6 +343,7 @@ type GetMempoolInfoResult struct {
TotalFee float64 `json:"total_fee"` // Total fees for the mempool in BTC, ignoring modified fees through prioritizetransaction TotalFee float64 `json:"total_fee"` // Total fees for the mempool in BTC, ignoring modified fees through prioritizetransaction
MemPoolMinFee float64 `json:"mempoolminfee"` // Minimum fee rate in BTC/kvB for tx to be accepted. Is the maximum of minrelaytxfee and minimum mempool fee MemPoolMinFee float64 `json:"mempoolminfee"` // Minimum fee rate in BTC/kvB for tx to be accepted. Is the maximum of minrelaytxfee and minimum mempool fee
MinRelayTxFee float64 `json:"minrelaytxfee"` // Current minimum relay fee for transactions MinRelayTxFee float64 `json:"minrelaytxfee"` // Current minimum relay fee for transactions
UnbroadcastCount int64 `json:"unbroadcastcount"` // Current number of transactions that haven't passed initial broadcast yet
} }
// NetworksResult models the networks data from the getnetworkinfo command. // NetworksResult models the networks data from the getnetworkinfo command.

View file

@ -235,6 +235,9 @@ type TxPool struct {
// stats are aggregated over pool, orphans, etc. // stats are aggregated over pool, orphans, etc.
stats aggregateInfo stats aggregateInfo
// unbroadcast is a set of transactions yet to be broadcast.
unbroadcast map[chainhash.Hash]bool
} }
// Ensure the TxPool type implements the mining.TxSource interface. // Ensure the TxPool type implements the mining.TxSource interface.
@ -1555,10 +1558,23 @@ func (mp *TxPool) MiningDescs() []*mining.TxDesc {
return descs return descs
} }
func (mp *TxPool) AddUnbroadcastTx(hash *chainhash.Hash) {
mp.mtx.Lock()
mp.unbroadcast[*hash] = true
mp.mtx.Unlock()
}
func (mp *TxPool) RemoveUnbroadcastTx(hash *chainhash.Hash) {
mp.mtx.Lock()
delete(mp.unbroadcast, *hash)
mp.mtx.Unlock()
}
func (mp *TxPool) MempoolInfo() *btcjson.GetMempoolInfoResult { func (mp *TxPool) MempoolInfo() *btcjson.GetMempoolInfoResult {
mp.mtx.RLock() mp.mtx.RLock()
policy := mp.cfg.Policy policy := mp.cfg.Policy
stats := mp.stats stats := mp.stats
unbroadcastCount := int64(len(mp.unbroadcast))
mp.mtx.RUnlock() mp.mtx.RUnlock()
ret := &btcjson.GetMempoolInfoResult{ ret := &btcjson.GetMempoolInfoResult{
@ -1568,6 +1584,7 @@ func (mp *TxPool) MempoolInfo() *btcjson.GetMempoolInfoResult {
TotalFee: btcutil.Amount(stats.totalFee).ToBTC(), TotalFee: btcutil.Amount(stats.totalFee).ToBTC(),
MemPoolMinFee: btcutil.Amount(calcMinRequiredTxRelayFee(1000, policy.MinRelayTxFee)).ToBTC(), MemPoolMinFee: btcutil.Amount(calcMinRequiredTxRelayFee(1000, policy.MinRelayTxFee)).ToBTC(),
MinRelayTxFee: policy.MinRelayTxFee.ToBTC(), MinRelayTxFee: policy.MinRelayTxFee.ToBTC(),
UnbroadcastCount: unbroadcastCount,
} }
return ret return ret
@ -1646,5 +1663,6 @@ func New(cfg *Config) *TxPool {
orphansByPrev: make(map[wire.OutPoint]map[chainhash.Hash]*btcutil.Tx), orphansByPrev: make(map[wire.OutPoint]map[chainhash.Hash]*btcutil.Tx),
nextExpireScan: time.Now().Add(orphanExpireScanInterval), nextExpireScan: time.Now().Add(orphanExpireScanInterval),
outpoints: make(map[wire.OutPoint]*btcutil.Tx), outpoints: make(map[wire.OutPoint]*btcutil.Tx),
unbroadcast: make(map[chainhash.Hash]bool),
} }
} }

View file

@ -3746,6 +3746,7 @@ func handleSendRawTransaction(s *rpcServer, cmd interface{}, closeChan <-chan st
// Keep track of all the sendrawtransaction request txns so that they // Keep track of all the sendrawtransaction request txns so that they
// can be rebroadcast if they don't make their way into a block. // can be rebroadcast if they don't make their way into a block.
txD := acceptedTxs[0] txD := acceptedTxs[0]
s.cfg.TxMemPool.AddUnbroadcastTx(txD.Tx.Hash())
iv := wire.NewInvVect(wire.InvTypeTx, txD.Tx.Hash()) iv := wire.NewInvVect(wire.InvTypeTx, txD.Tx.Hash())
s.cfg.ConnMgr.AddRebroadcastInventory(iv, txD) s.cfg.ConnMgr.AddRebroadcastInventory(iv, txD)

View file

@ -459,6 +459,7 @@ var helpDescsEnUS = map[string]string{
"getmempoolinforesult-total_fee": "Total fees for the mempool in LBC, ignoring modified fees through prioritizetransaction", "getmempoolinforesult-total_fee": "Total fees for the mempool in LBC, ignoring modified fees through prioritizetransaction",
"getmempoolinforesult-mempoolminfee": "Minimum fee rate in LBC/kvB for tx to be accepted. Is the maximum of minrelaytxfee and minimum mempool fee", "getmempoolinforesult-mempoolminfee": "Minimum fee rate in LBC/kvB for tx to be accepted. Is the maximum of minrelaytxfee and minimum mempool fee",
"getmempoolinforesult-minrelaytxfee": "Current minimum relay fee for transactions", "getmempoolinforesult-minrelaytxfee": "Current minimum relay fee for transactions",
"getmempoolinforesult-unbroadcastcount": "Current number of transactions that haven't passed initial broadcast yet",
// GetMiningInfoResult help. // GetMiningInfoResult help.
"getmininginforesult-blocks": "Height of the latest best block", "getmininginforesult-blocks": "Height of the latest best block",

View file

@ -699,6 +699,11 @@ func (sp *serverPeer) OnGetData(_ *peer.Peer, msg *wire.MsgGetData) {
if i == len(msg.InvList)-1 && c != nil { if i == len(msg.InvList)-1 && c != nil {
<-c <-c
} }
} else if iv.Type == wire.InvTypeWitnessTx || iv.Type == wire.InvTypeTx {
// We interpret fulfilling a GETDATA for a transaction as a
// successful initial broadcast and remove it from our
// unbroadcast set.
sp.server.txMemPool.RemoveUnbroadcastTx(&iv.Hash)
} }
numAdded++ numAdded++
waitChan = c waitChan = c