Skip to content

Commit 39afe5b

Browse files
committed
Merge #19082: test: Moved the CScriptNum asserts into the unit test in script.py
7daffc6 [test] CScriptNum Decode Check as Unit Tests (Gillian Chu) Pull request description: The CScriptNum test (#14816) is a roundtrip test of the test framework. Thus, it would be better suited as a unit test. This is now possible with the introduction of the unit test module for the functional tests. See #18576. This PR: 1. Refactors the CScriptNum tests into 2 unit tests, one in script.py and one in blocktools.py. 2. Extends the script.py CScriptNum test to trial larger numbers. ACKs for top commit: laanwj: ACK 7daffc6 Tree-SHA512: 17a04a4bfff1b1817bfc167824c679455d9e06e6e0164c00a7e44f8aa5041c5f5080adcc1452fd80ba1a6d8409f976c982bc481d686c434edf97a5893a32a436
2 parents 365f108 + 7daffc6 commit 39afe5b

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

test/functional/mining_basic.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
assert_raises_rpc_error,
3030
connect_nodes,
3131
)
32-
from test_framework.script import CScriptNum
33-
3432

3533
def assert_template(node, block, expect, rehash=True):
3634
if rehash:
@@ -91,12 +89,6 @@ def assert_submitblock(block, result_str_1, result_str_2=None):
9189
coinbase_tx.rehash()
9290

9391
# round-trip the encoded bip34 block height commitment
94-
assert_equal(CScriptNum.decode(coinbase_tx.vin[0].scriptSig), next_height)
95-
# round-trip negative and multi-byte CScriptNums to catch python regression
96-
assert_equal(CScriptNum.decode(CScriptNum.encode(CScriptNum(1500))), 1500)
97-
assert_equal(CScriptNum.decode(CScriptNum.encode(CScriptNum(-1500))), -1500)
98-
assert_equal(CScriptNum.decode(CScriptNum.encode(CScriptNum(-1))), -1)
99-
10092
block = CBlock()
10193
block.nVersion = tmpl["version"]
10294
block.hashPrevBlock = int(tmpl["previousblockhash"], 16)

test/functional/test_framework/blocktools.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Utilities for manipulating blocks and transactions."""
66

7+
import unittest
8+
79
from .address import (
810
key_to_p2sh_p2wpkh,
911
key_to_p2wpkh,
@@ -217,3 +219,9 @@ def send_to_witness(use_p2wsh, node, utxo, pubkey, encode_p2sh, amount, sign=Tru
217219
tx_to_witness = ToHex(tx)
218220

219221
return node.sendrawtransaction(tx_to_witness)
222+
223+
class TestFrameworkBlockTools(unittest.TestCase):
224+
def test_create_coinbase(self):
225+
height = 20
226+
coinbase_tx = create_coinbase(height=height)
227+
assert_equal(CScriptNum.decode(coinbase_tx.vin[0].scriptSig), height)

test/functional/test_framework/script.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,3 +732,9 @@ def test_bn2vch(self):
732732
self.assertEqual(bn2vch(0xFFFFFFFF), bytes([0xFF, 0xFF, 0xFF, 0xFF, 0x00]))
733733
self.assertEqual(bn2vch(123456789), bytes([0x15, 0xCD, 0x5B, 0x07]))
734734
self.assertEqual(bn2vch(-54321), bytes([0x31, 0xD4, 0x80]))
735+
736+
def test_cscriptnum_encoding(self):
737+
# round-trip negative and multi-byte CScriptNums
738+
values = [0, 1, -1, -2, 127, 128, -255, 256, (1 << 15) - 1, -(1 << 16), (1 << 24) - 1, (1 << 31), 1 - (1 << 32), 1 << 40, 1500, -1500]
739+
for value in values:
740+
self.assertEqual(CScriptNum.decode(CScriptNum.encode(CScriptNum(value))), value)

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868

6969
TEST_FRAMEWORK_MODULES = [
7070
"address",
71+
"blocktools",
7172
"script",
7273
]
7374

0 commit comments

Comments
 (0)