From e3d3088b80acb3d7943d38c495525d013a564892 Mon Sep 17 00:00:00 2001
From: preminem <31085564+preminem@users.noreply.github.com>
Date: Thu, 26 Sep 2019 08:19:03 +0800
Subject: [PATCH] btcjson+rpc: expose a transaction's weight via RPC
---
btcjson/chainsvrresults.go | 3 +++
docs/json_rpc_api.md | 2 +-
mempool/mempool.go | 1 +
rpcserver.go | 1 +
rpcserverhelp.go | 3 +++
5 files changed, 9 insertions(+), 1 deletion(-)
diff --git a/btcjson/chainsvrresults.go b/btcjson/chainsvrresults.go
index 30c6369a..cc9c0855 100644
--- a/btcjson/chainsvrresults.go
+++ b/btcjson/chainsvrresults.go
@@ -265,6 +265,7 @@ type GetPeerInfoResult struct {
type GetRawMempoolVerboseResult struct {
Size int32 `json:"size"`
Vsize int32 `json:"vsize"`
+ Weight int32 `json:"weight"`
Fee float64 `json:"fee"`
Time int64 `json:"time"`
Height int64 `json:"height"`
@@ -505,6 +506,7 @@ type TxRawResult struct {
Hash string `json:"hash,omitempty"`
Size int32 `json:"size,omitempty"`
Vsize int32 `json:"vsize,omitempty"`
+ Weight int32 `json:"weight,omitempty"`
Version int32 `json:"version"`
LockTime uint32 `json:"locktime"`
Vin []Vin `json:"vin"`
@@ -523,6 +525,7 @@ type SearchRawTransactionsResult struct {
Hash string `json:"hash"`
Size string `json:"size"`
Vsize string `json:"vsize"`
+ Weight string `json:"weight"`
Version int32 `json:"version"`
LockTime uint32 `json:"locktime"`
Vin []VinPrevOut `json:"vin"`
diff --git a/docs/json_rpc_api.md b/docs/json_rpc_api.md
index 9b8b36b3..51d4c82c 100644
--- a/docs/json_rpc_api.md
+++ b/docs/json_rpc_api.md
@@ -484,7 +484,7 @@ Example Return|`{`
`"bytes": 310768,`
`"size":
|Description|Returns an array of hashes for all of the transactions currently in the memory pool.
The `verbose` flag specifies that each transaction is returned as a JSON object.|
|Notes|Since btcd does not perform any mining, the priority related fields `startingpriority` and `currentpriority` that are available when the `verbose` flag is set are always 0.|
|Returns (verbose=false)|`[ (json array of string)`
`"transactionhash", (string) hash of the transaction`
`...`
`]`|
-|Returns (verbose=true)|`{ (json object)`
`"transactionhash": { (json object)`
`"size": n, (numeric) transaction size in bytes`
`"vsize": n, (numeric) transaction virtual size`
`"fee" : n, (numeric) transaction fee in bitcoins`
`"time": n, (numeric) local time transaction entered pool in seconds since 1 Jan 1970 GMT`
`"height": n, (numeric) block height when transaction entered the pool`
`"startingpriority": n, (numeric) priority when transaction entered the pool`
`"currentpriority": n, (numeric) current priority`
`"depends": [ (json array) unconfirmed transactions used as inputs for this transaction`
`"transactionhash", (string) hash of the parent transaction`
`...`
`]`
`}, ...`
`}`|
+|Returns (verbose=true)|`{ (json object)`
`"transactionhash": { (json object)`
`"size": n, (numeric) transaction size in bytes`
`"vsize": n, (numeric) transaction virtual size`
`"weight": n, (numeric) The transaction's weight (between vsize*4-3 and vsize*4)`
`"fee" : n, (numeric) transaction fee in bitcoins`
`"time": n, (numeric) local time transaction entered pool in seconds since 1 Jan 1970 GMT`
`"height": n, (numeric) block height when transaction entered the pool`
`"startingpriority": n, (numeric) priority when transaction entered the pool`
`"currentpriority": n, (numeric) current priority`
`"depends": [ (json array) unconfirmed transactions used as inputs for this transaction`
`"transactionhash", (string) hash of the parent transaction`
`...`
`]`
`}, ...`
`}`|
|Example Return (verbose=false)|`[`
`"3480058a397b6ffcc60f7e3345a61370fded1ca6bef4b58156ed17987f20d4e7",`
`"cbfe7c056a358c3a1dbced5a22b06d74b8650055d5195c1c2469e6b63a41514a"`
`]`|
|Example Return (verbose=true)|`{`
`"1697a19cede08694278f19584e8dcc87945f40c6b59a942dd8906f133ad3f9cc": {`
`"size": 226,`
`"fee" : 0.0001,`
`"time": 1387992789,`
`"height": 276836,`
`"startingpriority": 0,`
`"currentpriority": 0,`
`"depends": [`
`"aa96f672fcc5a1ec6a08a94aa46d6b789799c87bd6542967da25a96b2dee0afb",`
`]`
`}`|
[Return to Overview](#MethodOverview)
diff --git a/mempool/mempool.go b/mempool/mempool.go
index 35eaf234..63a86a16 100644
--- a/mempool/mempool.go
+++ b/mempool/mempool.go
@@ -1509,6 +1509,7 @@ func (mp *TxPool) RawMempoolVerbose() map[string]*btcjson.GetRawMempoolVerboseRe
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),
diff --git a/rpcserver.go b/rpcserver.go
index a3cf1e0f..097ba2ec 100644
--- a/rpcserver.go
+++ b/rpcserver.go
@@ -756,6 +756,7 @@ func createTxRawResult(chainParams *chaincfg.Params, mtx *wire.MsgTx,
Hash: mtx.WitnessHash().String(),
Size: int32(mtx.SerializeSize()),
Vsize: int32(mempool.GetTxVirtualSize(btcutil.NewTx(mtx))),
+ Weight: int32(blockchain.GetTransactionWeight(btcutil.NewTx(mtx))),
Vin: createVinList(mtx),
Vout: createVoutList(mtx, chainParams, nil),
Version: mtx.Version,
diff --git a/rpcserverhelp.go b/rpcserverhelp.go
index c875d217..e7637db6 100644
--- a/rpcserverhelp.go
+++ b/rpcserverhelp.go
@@ -207,6 +207,7 @@ var helpDescsEnUS = map[string]string{
"txrawresult-blocktime": "Block time in seconds since the 1 Jan 1970 GMT",
"txrawresult-size": "The size of the transaction in bytes",
"txrawresult-vsize": "The virtual size of the transaction in bytes",
+ "txrawresult-weight": "The transaction's weight (between vsize*4-3 and vsize*4)",
"txrawresult-hash": "The wtxid of the transaction",
// SearchRawTransactionsResult help.
@@ -223,6 +224,7 @@ var helpDescsEnUS = map[string]string{
"searchrawtransactionsresult-blocktime": "Block time in seconds since the 1 Jan 1970 GMT",
"searchrawtransactionsresult-size": "The size of the transaction in bytes",
"searchrawtransactionsresult-vsize": "The virtual size of the transaction in bytes",
+ "searchrawtransactionsresult-weight": "The transaction's weight (between vsize*4-3 and vsize*4)",
// GetBlockVerboseResult help.
"getblockverboseresult-hash": "The hash of the block (same as provided)",
@@ -476,6 +478,7 @@ var helpDescsEnUS = map[string]string{
"getrawmempoolverboseresult-currentpriority": "Current priority",
"getrawmempoolverboseresult-depends": "Unconfirmed transactions used as inputs for this transaction",
"getrawmempoolverboseresult-vsize": "The virtual size of a transaction",
+ "getrawmempoolverboseresult-weight": "The transaction's weight (between vsize*4-3 and vsize*4)",
// GetRawMempoolCmd help.
"getrawmempool--synopsis": "Returns information about all of the transactions currently in the memory pool.",