Skip to content

Commit 6ba3f1f

Browse files
author
MarcoFalke
committed
Merge #15397: Remove manual byte editing in wallet_tx_clone func test
6aaa0ab Remove manual byte editing in wallet_tx_clone func test (Gregory Sanders) Pull request description: Adapted from @stevenroose Tree-SHA512: 87f251579e347f870bd30fc57b0c130f00914a3dc78799826384eb049b91d49f2525d55899bf525997e23cc976ca7d10e6b56b23f7358acec307368d48a6f6f1
2 parents 29e82e4 + 6aaa0ab commit 6ba3f1f

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

test/functional/wallet_txn_clone.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test the wallet accounts properly when there are cloned transactions with malleated scriptsigs."""
66

7+
import io
78
from test_framework.test_framework import BitcoinTestFramework
89
from test_framework.util import (
910
assert_equal,
1011
connect_nodes,
1112
disconnect_nodes,
1213
sync_blocks,
1314
)
15+
from test_framework.messages import CTransaction, COIN
1416

1517
class TxnMallTest(BitcoinTestFramework):
1618
def set_test_params(self):
@@ -72,19 +74,14 @@ def run_test(self):
7274
clone_raw = self.nodes[0].createrawtransaction(clone_inputs, clone_outputs, clone_locktime)
7375

7476
# createrawtransaction randomizes the order of its outputs, so swap them if necessary.
75-
# output 0 is at version+#inputs+input+sigstub+sequence+#outputs
76-
# 40 BTC serialized is 00286bee00000000
77-
pos0 = 2 * (4 + 1 + 36 + 1 + 4 + 1)
78-
hex40 = "00286bee00000000"
79-
output_len = 16 + 2 + 2 * int("0x" + clone_raw[pos0 + 16:pos0 + 16 + 2], 0)
80-
if (rawtx1["vout"][0]["value"] == 40 and clone_raw[pos0:pos0 + 16] != hex40 or rawtx1["vout"][0]["value"] != 40 and clone_raw[pos0:pos0 + 16] == hex40):
81-
output0 = clone_raw[pos0:pos0 + output_len]
82-
output1 = clone_raw[pos0 + output_len:pos0 + 2 * output_len]
83-
clone_raw = clone_raw[:pos0] + output1 + output0 + clone_raw[pos0 + 2 * output_len:]
77+
clone_tx = CTransaction()
78+
clone_tx.deserialize(io.BytesIO(bytes.fromhex(clone_raw)))
79+
if (rawtx1["vout"][0]["value"] == 40 and clone_tx.vout[0].nValue != 40*COIN or rawtx1["vout"][0]["value"] != 40 and clone_tx.vout[0].nValue == 40*COIN):
80+
(clone_tx.vout[0], clone_tx.vout[1]) = (clone_tx.vout[1], clone_tx.vout[0])
8481

8582
# Use a different signature hash type to sign. This creates an equivalent but malleated clone.
8683
# Don't send the clone anywhere yet
87-
tx1_clone = self.nodes[0].signrawtransactionwithwallet(clone_raw, None, "ALL|ANYONECANPAY")
84+
tx1_clone = self.nodes[0].signrawtransactionwithwallet(clone_tx.serialize().hex(), None, "ALL|ANYONECANPAY")
8885
assert_equal(tx1_clone["complete"], True)
8986

9087
# Have node0 mine a block, if requested:

0 commit comments

Comments
 (0)