diff --git a/test/functional/mining_basic.py b/test/functional/mining_basic.py --- a/test/functional/mining_basic.py +++ b/test/functional/mining_basic.py @@ -29,7 +29,6 @@ assert_raises_rpc_error, connect_nodes, ) -from test_framework.script import CScriptNum def assert_template(node, block, expect, rehash=True): @@ -99,22 +98,6 @@ coinbase_tx.vin[0].nSequence = 2 ** 32 - 2 coinbase_tx.rehash() - # round-trip the encoded bip34 block height commitment - assert_equal( - CScriptNum.decode( - coinbase_tx.vin[0].scriptSig), - next_height) - # round-trip negative and multi-byte CScriptNums to catch python - # regression - assert_equal( - CScriptNum.decode( - CScriptNum.encode( - CScriptNum(1500))), - 1500) - assert_equal(CScriptNum.decode( - CScriptNum.encode(CScriptNum(-1500))), -1500) - assert_equal(CScriptNum.decode(CScriptNum.encode(CScriptNum(-1))), -1) - block = CBlock() block.nVersion = tmpl["version"] block.hashPrevBlock = int(tmpl["previousblockhash"], 16) diff --git a/test/functional/test_framework/blocktools.py b/test/functional/test_framework/blocktools.py --- a/test/functional/test_framework/blocktools.py +++ b/test/functional/test_framework/blocktools.py @@ -4,6 +4,8 @@ # file COPYING or http://www.opensource.org/licenses/mit-license.php. """Utilities for manipulating blocks and transactions.""" +import unittest + from .script import ( CScript, CScriptNum, @@ -226,3 +228,10 @@ txid = node.sendrawtransaction(signresult["hex"], 0) txids.append(txid) return txids + + +class TestFrameworkBlockTools(unittest.TestCase): + def test_create_coinbase(self): + height = 20 + coinbase_tx = create_coinbase(height=height) + assert_equal(CScriptNum.decode(coinbase_tx.vin[0].scriptSig), height) diff --git a/test/functional/test_framework/script.py b/test/functional/test_framework/script.py --- a/test/functional/test_framework/script.py +++ b/test/functional/test_framework/script.py @@ -780,3 +780,13 @@ bytes([0x15, 0xCD, 0x5B, 0x07])) self.assertEqual(bn2vch(-54321), bytes([0x31, 0xD4, 0x80])) + + def test_cscriptnum_encoding(self): + # round-trip negative and multi-byte CScriptNums + 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] + for value in values: + self.assertEqual( + CScriptNum.decode(CScriptNum.encode(CScriptNum(value))), + value) diff --git a/test/functional/test_runner.py b/test/functional/test_runner.py --- a/test/functional/test_runner.py +++ b/test/functional/test_runner.py @@ -76,6 +76,7 @@ TEST_FRAMEWORK_MODULES = [ "address", + "blocktools", "script", ] @@ -179,6 +180,9 @@ build_dir = config["environment"]["BUILDDIR"] tests_dir = os.path.join(src_dir, 'test', 'functional') + # SRCDIR must be set for cdefs.py to find and parse consensus.h + os.environ["SRCDIR"] = src_dir + # Parse arguments and pass through unrecognised args parser = argparse.ArgumentParser(add_help=False, usage='%(prog)s [test_runner.py options] [script options] [scripts]',