Skip to content

Commit 7daffc6

Browse files
Gillian ChuGillian Chu
authored andcommitted
[test] CScriptNum Decode Check as Unit Tests
Migrates the CScriptNum decode tests into a unit test, and moved some changes made in #14816. Made possible by the integration of test_framework unit testing in #18576. Further extends the original test with larger ints, similar to the scriptnum_tests.cpp file. Adds test to blocktools.py testing fn create_coinbase() with CScriptNum decode.
1 parent 9bc7751 commit 7daffc6

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
@@ -731,3 +731,9 @@ def test_bn2vch(self):
731731
self.assertEqual(bn2vch(0xFFFFFFFF), bytes([0xFF, 0xFF, 0xFF, 0xFF, 0x00]))
732732
self.assertEqual(bn2vch(123456789), bytes([0x15, 0xCD, 0x5B, 0x07]))
733733
self.assertEqual(bn2vch(-54321), bytes([0x31, 0xD4, 0x80]))
734+
735+
def test_cscriptnum_encoding(self):
736+
# round-trip negative and multi-byte CScriptNums
737+
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]
738+
for value in values:
739+
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)