Skip to content

Commit 3a8a4dc

Browse files
committed
Merge #12791: Expose a transaction's weight via RPC
9e50c33 Note new weight field in release-notes. (Matt Corallo) d0d9112 Test new weight field in p2p_segwit (Matt Corallo) 2874709 Expose a transaction's weight via RPC (Matt Corallo) Pull request description: This seems like an obvious oversight. Tree-SHA512: defd047de34fb06a31f589e1a4eef68fcae85095cc67b7c8fb434237bb40300d7f3f97e852d3e7226330e26b96943846b7baf6da0cfc79db8d56e9c1f7848ad9
2 parents 6b46288 + 9e50c33 commit 3a8a4dc

28 files changed

+36
-1
lines changed

doc/release-notes.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ RPC changes
6565
- The `fundrawtransaction` RPC will reject the previously deprecated `reserveChangeKey` option.
6666
- `sendmany` now shuffles outputs to improve privacy, so any previously expected behavior with regards to output ordering can no longer be relied upon.
6767
- The new RPC `testmempoolaccept` can be used to test acceptance of a transaction to the mempool without adding it.
68+
- JSON transaction decomposition now includes a `weight` field which provides
69+
the transaction's exact weight. This is included in REST /rest/tx/ and
70+
/rest/block/ endpoints when in json mode. This is also included in `getblock`
71+
(with verbosity=2), `listsinceblock`, `listtransactions`, and
72+
`getrawtransaction` RPC commands.
6873

6974
External wallet files
7075
---------------------

src/core_write.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry,
161161
entry.pushKV("version", tx.nVersion);
162162
entry.pushKV("size", (int)::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION));
163163
entry.pushKV("vsize", (GetTransactionWeight(tx) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR);
164+
entry.pushKV("weight", GetTransactionWeight(tx));
164165
entry.pushKV("locktime", (int64_t)tx.nLockTime);
165166

166167
UniValue vin(UniValue::VARR);

src/rpc/rawtransaction.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ UniValue getrawtransaction(const JSONRPCRequest& request)
9494
" \"hash\" : \"id\", (string) The transaction hash (differs from txid for witness transactions)\n"
9595
" \"size\" : n, (numeric) The serialized transaction size\n"
9696
" \"vsize\" : n, (numeric) The virtual transaction size (differs from size for witness transactions)\n"
97+
" \"weight\" : n, (numeric) The transaction's weight (between vsize*4-3 and vsize*4)\n"
9798
" \"version\" : n, (numeric) The version\n"
9899
" \"locktime\" : ttt, (numeric) The lock time\n"
99100
" \"vin\" : [ (array of json objects)\n"
@@ -494,6 +495,7 @@ UniValue decoderawtransaction(const JSONRPCRequest& request)
494495
" \"hash\" : \"id\", (string) The transaction hash (differs from txid for witness transactions)\n"
495496
" \"size\" : n, (numeric) The transaction size\n"
496497
" \"vsize\" : n, (numeric) The virtual transaction size (differs from size for witness transactions)\n"
498+
" \"weight\" : n, (numeric) The transaction's weight (between vsize*4 - 3 and vsize*4)\n"
497499
" \"version\" : n, (numeric) The version\n"
498500
" \"locktime\" : ttt, (numeric) The lock time\n"
499501
" \"vin\" : [ (array of json objects)\n"

test/functional/p2p_segwit.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from test_framework.script import *
1111
from test_framework.blocktools import create_block, create_coinbase, add_witness_commitment, get_witness_script, WITNESS_COMMITMENT_HEADER
1212
from test_framework.key import CECKey, CPubKey
13+
import math
1314
import time
1415
import random
1516
from binascii import hexlify
@@ -930,8 +931,10 @@ def test_tx_relay_after_segwit_activation(self):
930931
raw_tx = self.nodes[0].getrawtransaction(tx3.hash, 1)
931932
assert_equal(int(raw_tx["hash"], 16), tx3.calc_sha256(True))
932933
assert_equal(raw_tx["size"], len(tx3.serialize_with_witness()))
933-
vsize = (len(tx3.serialize_with_witness()) + 3*len(tx3.serialize_without_witness()) + 3) / 4
934+
weight = len(tx3.serialize_with_witness()) + 3*len(tx3.serialize_without_witness())
935+
vsize = math.ceil(weight / 4)
934936
assert_equal(raw_tx["vsize"], vsize)
937+
assert_equal(raw_tx["weight"], weight)
935938
assert_equal(len(raw_tx["vin"][0]["txinwitness"]), 1)
936939
assert_equal(raw_tx["vin"][0]["txinwitness"][0], hexlify(witness_program).decode('ascii'))
937940
assert(vsize != raw_tx["size"])

test/util/data/blanktxv1.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"version": 1,
55
"size": 10,
66
"vsize": 10,
7+
"weight": 40,
78
"locktime": 0,
89
"vin": [
910
],

test/util/data/blanktxv2.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"version": 2,
55
"size": 10,
66
"vsize": 10,
7+
"weight": 40,
78
"locktime": 0,
89
"vin": [
910
],

test/util/data/tt-delin1-out.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"version": 1,
55
"size": 3040,
66
"vsize": 3040,
7+
"weight": 12160,
78
"locktime": 0,
89
"vin": [
910
{

test/util/data/tt-delout1-out.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"version": 1,
55
"size": 3155,
66
"vsize": 3155,
7+
"weight": 12620,
78
"locktime": 0,
89
"vin": [
910
{

test/util/data/tt-locktime317000-out.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"version": 1,
55
"size": 3189,
66
"vsize": 3189,
7+
"weight": 12756,
78
"locktime": 317000,
89
"vin": [
910
{

test/util/data/txcreate1.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"version": 2,
55
"size": 201,
66
"vsize": 201,
7+
"weight": 804,
78
"locktime": 0,
89
"vin": [
910
{

0 commit comments

Comments
 (0)