Merge #12791: Expose a transaction's weight via RPC

9e50c337c Note new weight field in release-notes. (Matt Corallo)
d0d9112b7 Test new weight field in p2p_segwit (Matt Corallo)
2874709a9 Expose a transaction's weight via RPC (Matt Corallo)

Pull request description:

  This seems like an obvious oversight.

Tree-SHA512: defd047de34fb06a31f589e1a4eef68fcae85095cc67b7c8fb434237bb40300d7f3f97e852d3e7226330e26b96943846b7baf6da0cfc79db8d56e9c1f7848ad9
This commit is contained in:
Jonas Schnelli 2018-04-17 20:05:12 +02:00
commit 3a8a4dc4a1
No known key found for this signature in database
GPG key ID: 1EB776BB03C7922D
28 changed files with 36 additions and 1 deletions

View file

@ -65,6 +65,11 @@ RPC changes
- The `fundrawtransaction` RPC will reject the previously deprecated `reserveChangeKey` option. - The `fundrawtransaction` RPC will reject the previously deprecated `reserveChangeKey` option.
- `sendmany` now shuffles outputs to improve privacy, so any previously expected behavior with regards to output ordering can no longer be relied upon. - `sendmany` now shuffles outputs to improve privacy, so any previously expected behavior with regards to output ordering can no longer be relied upon.
- The new RPC `testmempoolaccept` can be used to test acceptance of a transaction to the mempool without adding it. - The new RPC `testmempoolaccept` can be used to test acceptance of a transaction to the mempool without adding it.
- JSON transaction decomposition now includes a `weight` field which provides
the transaction's exact weight. This is included in REST /rest/tx/ and
/rest/block/ endpoints when in json mode. This is also included in `getblock`
(with verbosity=2), `listsinceblock`, `listtransactions`, and
`getrawtransaction` RPC commands.
External wallet files External wallet files
--------------------- ---------------------

View file

@ -161,6 +161,7 @@ void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry,
entry.pushKV("version", tx.nVersion); entry.pushKV("version", tx.nVersion);
entry.pushKV("size", (int)::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION)); entry.pushKV("size", (int)::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION));
entry.pushKV("vsize", (GetTransactionWeight(tx) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR); entry.pushKV("vsize", (GetTransactionWeight(tx) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR);
entry.pushKV("weight", GetTransactionWeight(tx));
entry.pushKV("locktime", (int64_t)tx.nLockTime); entry.pushKV("locktime", (int64_t)tx.nLockTime);
UniValue vin(UniValue::VARR); UniValue vin(UniValue::VARR);

View file

@ -94,6 +94,7 @@ UniValue getrawtransaction(const JSONRPCRequest& request)
" \"hash\" : \"id\", (string) The transaction hash (differs from txid for witness transactions)\n" " \"hash\" : \"id\", (string) The transaction hash (differs from txid for witness transactions)\n"
" \"size\" : n, (numeric) The serialized transaction size\n" " \"size\" : n, (numeric) The serialized transaction size\n"
" \"vsize\" : n, (numeric) The virtual transaction size (differs from size for witness transactions)\n" " \"vsize\" : n, (numeric) The virtual transaction size (differs from size for witness transactions)\n"
" \"weight\" : n, (numeric) The transaction's weight (between vsize*4-3 and vsize*4)\n"
" \"version\" : n, (numeric) The version\n" " \"version\" : n, (numeric) The version\n"
" \"locktime\" : ttt, (numeric) The lock time\n" " \"locktime\" : ttt, (numeric) The lock time\n"
" \"vin\" : [ (array of json objects)\n" " \"vin\" : [ (array of json objects)\n"
@ -494,6 +495,7 @@ UniValue decoderawtransaction(const JSONRPCRequest& request)
" \"hash\" : \"id\", (string) The transaction hash (differs from txid for witness transactions)\n" " \"hash\" : \"id\", (string) The transaction hash (differs from txid for witness transactions)\n"
" \"size\" : n, (numeric) The transaction size\n" " \"size\" : n, (numeric) The transaction size\n"
" \"vsize\" : n, (numeric) The virtual transaction size (differs from size for witness transactions)\n" " \"vsize\" : n, (numeric) The virtual transaction size (differs from size for witness transactions)\n"
" \"weight\" : n, (numeric) The transaction's weight (between vsize*4 - 3 and vsize*4)\n"
" \"version\" : n, (numeric) The version\n" " \"version\" : n, (numeric) The version\n"
" \"locktime\" : ttt, (numeric) The lock time\n" " \"locktime\" : ttt, (numeric) The lock time\n"
" \"vin\" : [ (array of json objects)\n" " \"vin\" : [ (array of json objects)\n"

View file

@ -10,6 +10,7 @@ from test_framework.util import *
from test_framework.script import * from test_framework.script import *
from test_framework.blocktools import create_block, create_coinbase, add_witness_commitment, get_witness_script, WITNESS_COMMITMENT_HEADER from test_framework.blocktools import create_block, create_coinbase, add_witness_commitment, get_witness_script, WITNESS_COMMITMENT_HEADER
from test_framework.key import CECKey, CPubKey from test_framework.key import CECKey, CPubKey
import math
import time import time
import random import random
from binascii import hexlify from binascii import hexlify
@ -930,8 +931,10 @@ class SegWitTest(BitcoinTestFramework):
raw_tx = self.nodes[0].getrawtransaction(tx3.hash, 1) raw_tx = self.nodes[0].getrawtransaction(tx3.hash, 1)
assert_equal(int(raw_tx["hash"], 16), tx3.calc_sha256(True)) assert_equal(int(raw_tx["hash"], 16), tx3.calc_sha256(True))
assert_equal(raw_tx["size"], len(tx3.serialize_with_witness())) assert_equal(raw_tx["size"], len(tx3.serialize_with_witness()))
vsize = (len(tx3.serialize_with_witness()) + 3*len(tx3.serialize_without_witness()) + 3) / 4 weight = len(tx3.serialize_with_witness()) + 3*len(tx3.serialize_without_witness())
vsize = math.ceil(weight / 4)
assert_equal(raw_tx["vsize"], vsize) assert_equal(raw_tx["vsize"], vsize)
assert_equal(raw_tx["weight"], weight)
assert_equal(len(raw_tx["vin"][0]["txinwitness"]), 1) assert_equal(len(raw_tx["vin"][0]["txinwitness"]), 1)
assert_equal(raw_tx["vin"][0]["txinwitness"][0], hexlify(witness_program).decode('ascii')) assert_equal(raw_tx["vin"][0]["txinwitness"][0], hexlify(witness_program).decode('ascii'))
assert(vsize != raw_tx["size"]) assert(vsize != raw_tx["size"])

View file

@ -4,6 +4,7 @@
"version": 1, "version": 1,
"size": 10, "size": 10,
"vsize": 10, "vsize": 10,
"weight": 40,
"locktime": 0, "locktime": 0,
"vin": [ "vin": [
], ],

View file

@ -4,6 +4,7 @@
"version": 2, "version": 2,
"size": 10, "size": 10,
"vsize": 10, "vsize": 10,
"weight": 40,
"locktime": 0, "locktime": 0,
"vin": [ "vin": [
], ],

View file

@ -4,6 +4,7 @@
"version": 1, "version": 1,
"size": 3040, "size": 3040,
"vsize": 3040, "vsize": 3040,
"weight": 12160,
"locktime": 0, "locktime": 0,
"vin": [ "vin": [
{ {

View file

@ -4,6 +4,7 @@
"version": 1, "version": 1,
"size": 3155, "size": 3155,
"vsize": 3155, "vsize": 3155,
"weight": 12620,
"locktime": 0, "locktime": 0,
"vin": [ "vin": [
{ {

View file

@ -4,6 +4,7 @@
"version": 1, "version": 1,
"size": 3189, "size": 3189,
"vsize": 3189, "vsize": 3189,
"weight": 12756,
"locktime": 317000, "locktime": 317000,
"vin": [ "vin": [
{ {

View file

@ -4,6 +4,7 @@
"version": 2, "version": 2,
"size": 201, "size": 201,
"vsize": 201, "vsize": 201,
"weight": 804,
"locktime": 0, "locktime": 0,
"vin": [ "vin": [
{ {

View file

@ -4,6 +4,7 @@
"version": 2, "version": 2,
"size": 19, "size": 19,
"vsize": 19, "vsize": 19,
"weight": 76,
"locktime": 0, "locktime": 0,
"vin": [ "vin": [
], ],

View file

@ -4,6 +4,7 @@
"version": 1, "version": 1,
"size": 176, "size": 176,
"vsize": 176, "vsize": 176,
"weight": 704,
"locktime": 0, "locktime": 0,
"vin": [ "vin": [
{ {

View file

@ -4,6 +4,7 @@
"version": 2, "version": 2,
"size": 176, "size": 176,
"vsize": 176, "vsize": 176,
"weight": 704,
"locktime": 0, "locktime": 0,
"vin": [ "vin": [
{ {

View file

@ -4,6 +4,7 @@
"version": 2, "version": 2,
"size": 85, "size": 85,
"vsize": 85, "vsize": 85,
"weight": 340,
"locktime": 0, "locktime": 0,
"vin": [ "vin": [
{ {

View file

@ -4,6 +4,7 @@
"version": 1, "version": 1,
"size": 126, "size": 126,
"vsize": 126, "vsize": 126,
"weight": 504,
"locktime": 0, "locktime": 0,
"vin": [ "vin": [
{ {

View file

@ -4,6 +4,7 @@
"version": 1, "version": 1,
"size": 124, "size": 124,
"vsize": 124, "vsize": 124,
"weight": 496,
"locktime": 0, "locktime": 0,
"vin": [ "vin": [
], ],

View file

@ -4,6 +4,7 @@
"version": 1, "version": 1,
"size": 42, "size": 42,
"vsize": 42, "vsize": 42,
"weight": 168,
"locktime": 0, "locktime": 0,
"vin": [ "vin": [
], ],

View file

@ -4,6 +4,7 @@
"version": 1, "version": 1,
"size": 53, "size": 53,
"vsize": 53, "vsize": 53,
"weight": 212,
"locktime": 0, "locktime": 0,
"vin": [ "vin": [
], ],

View file

@ -4,6 +4,7 @@
"version": 1, "version": 1,
"size": 42, "size": 42,
"vsize": 42, "vsize": 42,
"weight": 168,
"locktime": 0, "locktime": 0,
"vin": [ "vin": [
], ],

View file

@ -4,6 +4,7 @@
"version": 2, "version": 2,
"size": 42, "size": 42,
"vsize": 42, "vsize": 42,
"weight": 168,
"locktime": 0, "locktime": 0,
"vin": [ "vin": [
], ],

View file

@ -4,6 +4,7 @@
"version": 1, "version": 1,
"size": 54, "size": 54,
"vsize": 54, "vsize": 54,
"weight": 216,
"locktime": 0, "locktime": 0,
"vin": [ "vin": [
], ],

View file

@ -4,6 +4,7 @@
"version": 1, "version": 1,
"size": 41, "size": 41,
"vsize": 41, "vsize": 41,
"weight": 164,
"locktime": 0, "locktime": 0,
"vin": [ "vin": [
], ],

View file

@ -4,6 +4,7 @@
"version": 1, "version": 1,
"size": 42, "size": 42,
"vsize": 42, "vsize": 42,
"weight": 168,
"locktime": 0, "locktime": 0,
"vin": [ "vin": [
], ],

View file

@ -4,6 +4,7 @@
"version": 1, "version": 1,
"size": 20, "size": 20,
"vsize": 20, "vsize": 20,
"weight": 80,
"locktime": 0, "locktime": 0,
"vin": [ "vin": [
], ],

View file

@ -4,6 +4,7 @@
"version": 1, "version": 1,
"size": 42, "size": 42,
"vsize": 42, "vsize": 42,
"weight": 168,
"locktime": 0, "locktime": 0,
"vin": [ "vin": [
], ],

View file

@ -4,6 +4,7 @@
"version": 1, "version": 1,
"size": 53, "size": 53,
"vsize": 53, "vsize": 53,
"weight": 212,
"locktime": 0, "locktime": 0,
"vin": [ "vin": [
], ],

View file

@ -4,6 +4,7 @@
"version": 1, "version": 1,
"size": 42, "size": 42,
"vsize": 42, "vsize": 42,
"weight": 168,
"locktime": 0, "locktime": 0,
"vin": [ "vin": [
], ],

View file

@ -4,6 +4,7 @@
"version": 1, "version": 1,
"size": 224, "size": 224,
"vsize": 224, "vsize": 224,
"weight": 896,
"locktime": 0, "locktime": 0,
"vin": [ "vin": [
{ {