Skip to content

Commit 67de1ee

Browse files
committed
Merge #18378: Bugfix & simplify bn2vch using int.to_bytes
a733ad5 Add bn2vch test to functional tests (Pieter Wuille) a3ad645 Simplify bn2vch using int.to_bytes (Pieter Wuille) Pull request description: Alternative to #18374, fixing the incorrect padding added sometimes in `bn2vch`. Since we're using Python 3.2+, a much simpler implementation of `bn2vch` is possible using `int.to_bytes`. This also adds a "functional" test for bn2vch, in a new "framework_test_script.py", where the "framework_test_" prefix is intended for tests of the framework itself. ACKs for top commit: laanwj: nice, ACK a733ad5 jnewbery: Tested ACK a733ad5. Tree-SHA512: aeacc4e7fd84279023d38e8b4a5175fb16d7b3a7f93c61b9dcb59cd9927547732983c76f28564b62e37088399fc0121b38a514d73b0ea38b3983836539e9ca90
2 parents a421e0a + a733ad5 commit 67de1ee

File tree

3 files changed

+54
-33
lines changed

3 files changed

+54
-33
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2020 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
"""Tests for test_framework.script."""
6+
7+
from test_framework.test_framework import BitcoinTestFramework
8+
from test_framework.script import bn2vch
9+
from test_framework.util import assert_equal
10+
11+
def test_bn2vch():
12+
assert_equal(bn2vch(0), bytes([]))
13+
assert_equal(bn2vch(1), bytes([0x01]))
14+
assert_equal(bn2vch(-1), bytes([0x81]))
15+
assert_equal(bn2vch(0x7F), bytes([0x7F]))
16+
assert_equal(bn2vch(-0x7F), bytes([0xFF]))
17+
assert_equal(bn2vch(0x80), bytes([0x80, 0x00]))
18+
assert_equal(bn2vch(-0x80), bytes([0x80, 0x80]))
19+
assert_equal(bn2vch(0xFF), bytes([0xFF, 0x00]))
20+
assert_equal(bn2vch(-0xFF), bytes([0xFF, 0x80]))
21+
assert_equal(bn2vch(0x100), bytes([0x00, 0x01]))
22+
assert_equal(bn2vch(-0x100), bytes([0x00, 0x81]))
23+
assert_equal(bn2vch(0x7FFF), bytes([0xFF, 0x7F]))
24+
assert_equal(bn2vch(-0x8000), bytes([0x00, 0x80, 0x80]))
25+
assert_equal(bn2vch(-0x7FFFFF), bytes([0xFF, 0xFF, 0xFF]))
26+
assert_equal(bn2vch(0x80000000), bytes([0x00, 0x00, 0x00, 0x80, 0x00]))
27+
assert_equal(bn2vch(-0x80000000), bytes([0x00, 0x00, 0x00, 0x80, 0x80]))
28+
assert_equal(bn2vch(0xFFFFFFFF), bytes([0xFF, 0xFF, 0xFF, 0xFF, 0x00]))
29+
30+
assert_equal(bn2vch(123456789), bytes([0x15, 0xCD, 0x5B, 0x07]))
31+
assert_equal(bn2vch(-54321), bytes([0x31, 0xD4, 0x80]))
32+
33+
class FrameworkTestScript(BitcoinTestFramework):
34+
def setup_network(self):
35+
pass
36+
37+
def set_test_params(self):
38+
self.num_nodes = 0
39+
40+
def run_test(self):
41+
test_bn2vch()
42+
43+
if __name__ == '__main__':
44+
FrameworkTestScript().main()

test/functional/test_framework/script.py

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,38 +27,14 @@ def hash160(s):
2727

2828
def bn2vch(v):
2929
"""Convert number to bitcoin-specific little endian format."""
30-
# The top bit is used to indicate the sign of the number. If there
31-
# isn't a spare bit in the bit length, add an extension byte.
32-
have_ext = False
33-
ext = bytearray()
34-
if v.bit_length() > 0:
35-
have_ext = (v.bit_length() & 0x07) == 0
36-
ext.append(0)
37-
38-
# Is the number negative?
39-
neg = False
40-
if v < 0:
41-
neg = True
42-
v = -v
43-
44-
# Convert the int to bytes
45-
v_bin = bytearray()
46-
bytes_len = (v.bit_length() + 7) // 8
47-
for i in range(bytes_len, 0, -1):
48-
v_bin.append((v >> ((i - 1) * 8)) & 0xff)
49-
50-
# Add the sign bit if necessary
51-
if neg:
52-
if have_ext:
53-
ext[0] |= 0x80
54-
else:
55-
v_bin[0] |= 0x80
56-
57-
v_bytes = ext + v_bin
58-
# Reverse bytes ordering for LE
59-
v_bytes.reverse()
60-
61-
return bytes(v_bytes)
30+
# We need v.bit_length() bits, plus a sign bit for every nonzero number.
31+
n_bits = v.bit_length() + (v != 0)
32+
# The number of bytes for that is:
33+
n_bytes = (n_bits + 7) // 8
34+
# Convert number to absolute value + sign in top bit.
35+
encoded_v = 0 if v == 0 else abs(v) | ((v < 0) << (n_bytes * 8 - 1))
36+
# Serialize to bytes
37+
return encoded_v.to_bytes(n_bytes, 'little')
6238

6339
_opcode_instances = []
6440
class CScriptOp(int):

test/functional/test_runner.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@
222222
'rpc_help.py',
223223
'feature_help.py',
224224
'feature_shutdown.py',
225+
'framework_test_script.py',
225226
# Don't append tests at the end to avoid merge conflicts
226227
# Put them in a random line within the section that fits their approximate run-time
227228
]
@@ -614,7 +615,7 @@ def was_successful(self):
614615
def check_script_prefixes():
615616
"""Check that test scripts start with one of the allowed name prefixes."""
616617

617-
good_prefixes_re = re.compile("(example|feature|interface|mempool|mining|p2p|rpc|wallet|tool)_")
618+
good_prefixes_re = re.compile("^(example|feature|interface|mempool|mining|p2p|rpc|wallet|tool|framework_test)_")
618619
bad_script_names = [script for script in ALL_SCRIPTS if good_prefixes_re.match(script) is None]
619620

620621
if bad_script_names:

0 commit comments

Comments
 (0)