Skip to content

Commit

Permalink
wip: update getrawmempool and implement getmempoolentry
Browse files Browse the repository at this point in the history
TODO::
1. Populate Ancestor and decsendent related fields instead of mocking.
2. Move and refator the implementation of getmempoolentry to the mempool
   package.
  • Loading branch information
roylee17 committed Feb 18, 2022
1 parent eecc55d commit dc407a7
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 19 deletions.
1 change: 1 addition & 0 deletions btcjson/chainsvrresults.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ type GetMempoolEntryResult struct {
WTxId string `json:"wtxid"`
Fees MempoolFees `json:"fees"`
Depends []string `json:"depends"`
SpentBy []string `json:"spentby"`
}

// GetChainTipsResult models the data returns from the getchaintips command.
Expand Down
44 changes: 25 additions & 19 deletions mempool/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -1507,36 +1507,42 @@ func (mp *TxPool) MiningDescs() []*mining.TxDesc {
// populated btcjson result.
//
// This function is safe for concurrent access.
func (mp *TxPool) RawMempoolVerbose() map[string]*btcjson.GetRawMempoolVerboseResult {
func (mp *TxPool) RawMempoolVerbose() map[string]*btcjson.GetMempoolEntryResult {
mp.mtx.RLock()
defer mp.mtx.RUnlock()

result := make(map[string]*btcjson.GetRawMempoolVerboseResult,
result := make(map[string]*btcjson.GetMempoolEntryResult,
len(mp.pool))
bestHeight := mp.cfg.BestHeight()

for _, desc := range mp.pool {
// Calculate the current priority based on the inputs to
// the transaction. Use zero if one or more of the
// input transactions can't be found for some reason.
tx := desc.Tx
var currentPriority float64
utxos, err := mp.fetchInputUtxos(tx)
if err == nil {
currentPriority = mining.CalcPriority(tx.MsgTx(), utxos,
bestHeight+1)
}

mpd := &btcjson.GetRawMempoolVerboseResult{
Size: int32(tx.MsgTx().SerializeSize()),
Vsize: int32(GetTxVirtualSize(tx)),
Weight: int32(blockchain.GetTransactionWeight(tx)),
Fee: btcutil.Amount(desc.Fee).ToBTC(),
Time: desc.Added.Unix(),
Height: int64(desc.Height),
StartingPriority: desc.StartingPriority,
CurrentPriority: currentPriority,
Depends: make([]string, 0),
mpd := &btcjson.GetMempoolEntryResult{
VSize: int32(GetTxVirtualSize(tx)),
Size: int32(tx.MsgTx().SerializeSize()),
Weight: blockchain.GetTransactionWeight(tx),
Fee: btcutil.Amount(desc.Fee).ToBTC(),
ModifiedFee: btcutil.Amount(desc.Fee).ToBTC(), // TODO, Deprecated
Time: desc.Added.Unix(),
Height: int64(desc.Height),
DescendantCount: 1, // TODO
DescendantSize: GetTxVirtualSize(tx), // TODO
DescendantFees: btcutil.Amount(desc.Fee).ToBTC(), // TODO, Deprecated
AncestorCount: 1, // TODO
AncestorSize: GetTxVirtualSize(tx), // TODO
AncestorFees: btcutil.Amount(desc.Fee).ToBTC(), // TODO, Deprecated
WTxId: desc.Tx.WitnessHash().String(),
Fees: btcjson.MempoolFees{
Base: btcutil.Amount(desc.Fee).ToBTC(),
Modified: btcutil.Amount(desc.Fee).ToBTC(), // TODO
Ancestor: btcutil.Amount(desc.Fee).ToBTC(), // TODO
Descendant: btcutil.Amount(desc.Fee).ToBTC(), // TODO
},
Depends: make([]string, 0), // TODO
SpentBy: make([]string, 0), // TODO
}
for _, txIn := range tx.MsgTx().TxIn {
hash := &txIn.PreviousOutPoint.Hash
Expand Down
51 changes: 51 additions & 0 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ var rpcHandlersBeforeInit = map[string]commandHandler{
"getheaders": handleGetHeaders,
"getinfo": handleGetInfo,
"getmempoolinfo": handleGetMempoolInfo,
"getmempoolentry": handleGetMempoolEntry,
"getmininginfo": handleGetMiningInfo,
"getnettotals": handleGetNetTotals,
"getnetworkhashps": handleGetNetworkHashPS,
Expand Down Expand Up @@ -2483,6 +2484,56 @@ func handleGetMempoolInfo(s *rpcServer, cmd interface{}, closeChan <-chan struct
return ret, nil
}

// handleGetMempoolEntry implements the getmempoolentry command.
func handleGetMempoolEntry(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {

c := cmd.(*btcjson.GetMempoolEntryCmd)
txHash, err := chainhash.NewHashFromStr(c.TxID)
if err != nil {
if err != nil {
return nil, rpcDecodeHexError(c.TxID)
}
}

mp := s.cfg.TxMemPool
for _, desc := range mp.TxDescs() {
tx := desc.Tx
if tx.Hash().IsEqual(txHash) {
continue
}
desc.Tx.WitnessHash()

ret := &btcjson.GetMempoolEntryResult{
VSize: int32(mempool.GetTxVirtualSize(tx)),
Size: int32(tx.MsgTx().SerializeSize()),
Weight: blockchain.GetTransactionWeight(tx),
Fee: btcutil.Amount(desc.Fee).ToBTC(),
ModifiedFee: btcutil.Amount(desc.Fee).ToBTC(), // TODO, Deprecated
Time: desc.Added.Unix(),
Height: int64(desc.Height),
DescendantCount: 1, // TODO
DescendantSize: mempool.GetTxVirtualSize(tx), // TODO
DescendantFees: btcutil.Amount(desc.Fee).ToBTC(), // TODO, Deprecated
AncestorCount: 1, // TODO
AncestorSize: mempool.GetTxVirtualSize(tx), // TODO
AncestorFees: btcutil.Amount(desc.Fee).ToBTC(), // TODO, Deprecated
WTxId: desc.Tx.WitnessHash().String(),
Fees: btcjson.MempoolFees{
Base: btcutil.Amount(desc.Fee).ToBTC(),
Modified: btcutil.Amount(desc.Fee).ToBTC(), // TODO
Ancestor: btcutil.Amount(desc.Fee).ToBTC(), // TODO
Descendant: btcutil.Amount(desc.Fee).ToBTC(), // TODO
},
Depends: make([]string, 0), // TODO
SpentBy: make([]string, 0), // TODO
}

return ret, nil
}

return nil, rpcNoTxInfoError(txHash)
}

// handleGetMiningInfo implements the getmininginfo command. We only return the
// fields that are not related to wallet functionality.
func handleGetMiningInfo(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
Expand Down

0 comments on commit dc407a7

Please sign in to comment.