Skip to content

Commit fa0aa87

Browse files
author
MarcoFalke
committed
rpc: Return wtxid from testmempoolaccept
1 parent 6af0137 commit fa0aa87

File tree

7 files changed

+48
-10
lines changed

7 files changed

+48
-10
lines changed

src/rpc/rawtransaction.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,7 @@ static RPCHelpMan testmempoolaccept()
894894
{RPCResult::Type::OBJ, "", "",
895895
{
896896
{RPCResult::Type::STR_HEX, "txid", "The transaction hash in hex"},
897+
{RPCResult::Type::STR_HEX, "wtxid", "The transaction witness hash in hex"},
897898
{RPCResult::Type::BOOL, "allowed", "If the mempool allows this tx to be inserted"},
898899
{RPCResult::Type::NUM, "vsize", "Virtual transaction size as defined in BIP 141. This is different from actual serialized size for witness transactions as witness data is discounted (only present when 'allowed' is true)"},
899900
{RPCResult::Type::OBJ, "fees", "Transaction fees (only present if 'allowed' is true)",
@@ -930,7 +931,6 @@ static RPCHelpMan testmempoolaccept()
930931
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed. Make sure the tx has at least one input.");
931932
}
932933
CTransactionRef tx(MakeTransactionRef(std::move(mtx)));
933-
const uint256& tx_hash = tx->GetHash();
934934

935935
const CFeeRate max_raw_tx_fee_rate = request.params[1].isNull() ?
936936
DEFAULT_MAX_RAW_TX_FEE_RATE :
@@ -942,7 +942,8 @@ static RPCHelpMan testmempoolaccept()
942942

943943
UniValue result(UniValue::VARR);
944944
UniValue result_0(UniValue::VOBJ);
945-
result_0.pushKV("txid", tx_hash.GetHex());
945+
result_0.pushKV("txid", tx->GetHash().GetHex());
946+
result_0.pushKV("wtxid", tx->GetWitnessHash().GetHex());
946947

947948
TxValidationState state;
948949
bool test_accept_res;

test/functional/feature_cltv.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,13 @@ def run_test(self):
126126
# First we show that this tx is valid except for CLTV by getting it
127127
# rejected from the mempool for exactly that reason.
128128
assert_equal(
129-
[{'txid': spendtx.hash, 'allowed': False, 'reject-reason': 'non-mandatory-script-verify-flag (Negative locktime)'}],
130-
self.nodes[0].testmempoolaccept(rawtxs=[spendtx.serialize().hex()], maxfeerate=0)
129+
[{
130+
'txid': spendtx.hash,
131+
'wtxid': spendtx.getwtxid(),
132+
'allowed': False,
133+
'reject-reason': 'non-mandatory-script-verify-flag (Negative locktime)',
134+
}],
135+
self.nodes[0].testmempoolaccept(rawtxs=[spendtx.serialize().hex()], maxfeerate=0),
131136
)
132137

133138
# Now we verify that a block with this transaction is also invalid.

test/functional/feature_dersig.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,13 @@ def run_test(self):
112112
# First we show that this tx is valid except for DERSIG by getting it
113113
# rejected from the mempool for exactly that reason.
114114
assert_equal(
115-
[{'txid': spendtx.hash, 'allowed': False, 'reject-reason': 'non-mandatory-script-verify-flag (Non-canonical DER signature)'}],
116-
self.nodes[0].testmempoolaccept(rawtxs=[spendtx.serialize().hex()], maxfeerate=0)
115+
[{
116+
'txid': spendtx.hash,
117+
'wtxid': spendtx.getwtxid(),
118+
'allowed': False,
119+
'reject-reason': 'non-mandatory-script-verify-flag (Non-canonical DER signature)',
120+
}],
121+
self.nodes[0].testmempoolaccept(rawtxs=[spendtx.serialize().hex()], maxfeerate=0),
117122
)
118123

119124
# Now we verify that a block with this transaction is also invalid.

test/functional/mempool_accept.py

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ def skip_test_if_missing_module(self):
5151
def check_mempool_result(self, result_expected, *args, **kwargs):
5252
"""Wrapper to check result of testmempoolaccept on node_0's mempool"""
5353
result_test = self.nodes[0].testmempoolaccept(*args, **kwargs)
54+
for r in result_test:
55+
r.pop('wtxid') # Skip check for now
5456
assert_equal(result_expected, result_test)
5557
assert_equal(self.nodes[0].getmempoolinfo()['size'], self.mempool_size) # Must not change mempool state
5658

test/functional/p2p_segwit.py

+24-2
Original file line numberDiff line numberDiff line change
@@ -686,13 +686,35 @@ def test_standardness_v0(self):
686686
if not self.segwit_active:
687687
# Just check mempool acceptance, but don't add the transaction to the mempool, since witness is disallowed
688688
# in blocks and the tx is impossible to mine right now.
689-
assert_equal(self.nodes[0].testmempoolaccept([tx3.serialize_with_witness().hex()]), [{'txid': tx3.hash, 'allowed': True, 'vsize': tx3.get_vsize(), 'fees': { 'base': Decimal('0.00001000')}}])
689+
assert_equal(
690+
self.nodes[0].testmempoolaccept([tx3.serialize_with_witness().hex()]),
691+
[{
692+
'txid': tx3.hash,
693+
'wtxid': tx3.getwtxid(),
694+
'allowed': True,
695+
'vsize': tx3.get_vsize(),
696+
'fees': {
697+
'base': Decimal('0.00001000'),
698+
},
699+
}],
700+
)
690701
# Create the same output as tx3, but by replacing tx
691702
tx3_out = tx3.vout[0]
692703
tx3 = tx
693704
tx3.vout = [tx3_out]
694705
tx3.rehash()
695-
assert_equal(self.nodes[0].testmempoolaccept([tx3.serialize_with_witness().hex()]), [{'txid': tx3.hash, 'allowed': True, 'vsize': tx3.get_vsize(), 'fees': { 'base': Decimal('0.00011000')}}])
706+
assert_equal(
707+
self.nodes[0].testmempoolaccept([tx3.serialize_with_witness().hex()]),
708+
[{
709+
'txid': tx3.hash,
710+
'wtxid': tx3.getwtxid(),
711+
'allowed': True,
712+
'vsize': tx3.get_vsize(),
713+
'fees': {
714+
'base': Decimal('0.00011000'),
715+
},
716+
}],
717+
)
696718
test_transaction_acceptance(self.nodes[0], self.test_node, tx3, with_witness=True, accepted=True)
697719

698720
self.nodes[0].generate(1)

test/functional/test_framework/messages.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,9 @@ def serialize_with_witness(self):
564564
def serialize(self):
565565
return self.serialize_with_witness()
566566

567+
def getwtxid(self):
568+
return hash256(self.serialize())[::-1].hex()
569+
567570
# Recalculate the txid (transaction hash without witness)
568571
def rehash(self):
569572
self.sha256 = None
@@ -579,7 +582,7 @@ def calc_sha256(self, with_witness=False):
579582

580583
if self.sha256 is None:
581584
self.sha256 = uint256_from_str(hash256(self.serialize_without_witness()))
582-
self.hash = encode(hash256(self.serialize_without_witness())[::-1], 'hex_codec').decode('ascii')
585+
self.hash = hash256(self.serialize_without_witness())[::-1].hex()
583586

584587
def is_valid(self):
585588
self.calc_sha256()

test/functional/test_framework/wallet.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,4 @@ def send_self_transfer(self, *, fee_rate=Decimal("0.003"), from_node, utxo_to_sp
7676
from_node.sendrawtransaction(tx_hex)
7777
assert_equal(tx_info['vsize'], vsize)
7878
assert_equal(tx_info['fees']['base'], fee)
79-
return {'txid': tx_info['txid'], 'wtxid': from_node.decoderawtransaction(tx_hex)['hash'], 'hex': tx_hex}
79+
return {'txid': tx_info['txid'], 'wtxid': tx_info['wtxid'], 'hex': tx_hex}

0 commit comments

Comments
 (0)