From f31fc0e92efae793af840c9a46e765c20e0899b4 Mon Sep 17 00:00:00 2001 From: John Newbery Date: Wed, 30 Oct 2019 10:13:17 -0400 Subject: [tests] fix flake8 warnings in script.py and bignum.py --- test/functional/test_framework/bignum.py | 11 +- test/functional/test_framework/script.py | 274 ++++++++++++++++--------------- 2 files changed, 144 insertions(+), 141 deletions(-) (limited to 'test') diff --git a/test/functional/test_framework/bignum.py b/test/functional/test_framework/bignum.py index db5ccd62c2..0dd4ea4b42 100644 --- a/test/functional/test_framework/bignum.py +++ b/test/functional/test_framework/bignum.py @@ -4,25 +4,24 @@ # file COPYING or http://www.opensource.org/licenses/mit-license.php. """Big number routines. +Functions for converting numbers to MPI format and Bitcoin-specific little +endian format. + This file is copied from python-bitcoinlib. """ - import struct - -# generic big endian MPI format - def bn_bytes(v, have_ext=False): ext = 0 if have_ext: ext = 1 - return ((v.bit_length()+7)//8) + ext + return (v.bit_length() + 7) // 8 + ext def bn2bin(v): s = bytearray() i = bn_bytes(v) while i > 0: - s.append((v >> ((i-1) * 8)) & 0xff) + s.append((v >> ((i - 1) * 8)) & 0xff) i -= 1 return s diff --git a/test/functional/test_framework/script.py b/test/functional/test_framework/script.py index 51aa9057f7..0dbd521433 100644 --- a/test/functional/test_framework/script.py +++ b/test/functional/test_framework/script.py @@ -6,16 +6,21 @@ This file is modified from python-bitcoinlib. """ - -from .messages import CTransaction, CTxOut, sha256, hash256, uint256_from_str, ser_uint256, ser_string - import hashlib import struct from .bignum import bn2vch +from .messages import ( + CTransaction, + CTxOut, + hash256, + ser_string, + ser_uint256, + sha256, + uint256_from_str, +) MAX_SCRIPT_ELEMENT_SIZE = 520 - OPCODE_NAMES = {} def hash160(s): @@ -31,13 +36,13 @@ class CScriptOp(int): def encode_op_pushdata(d): """Encode a PUSHDATA op, returning bytes""" if len(d) < 0x4c: - return b'' + bytes([len(d)]) + d # OP_PUSHDATA + return b'' + bytes([len(d)]) + d # OP_PUSHDATA elif len(d) <= 0xff: - return b'\x4c' + bytes([len(d)]) + d # OP_PUSHDATA1 + return b'\x4c' + bytes([len(d)]) + d # OP_PUSHDATA1 elif len(d) <= 0xffff: - return b'\x4d' + struct.pack(b'= 0x80: # Mask for all but the highest result bit - num_mask = (2**(len(value)*8) - 1) >> 1 + num_mask = (2**(len(value) * 8) - 1) >> 1 result &= num_mask result *= -1 return result @@ -493,21 +498,20 @@ class CScript(bytes): pushdata_type = 'PUSHDATA2' if i + 1 >= len(self): raise CScriptInvalidError('PUSHDATA2: missing data length') - datasize = self[i] + (self[i+1] << 8) + datasize = self[i] + (self[i + 1] << 8) i += 2 elif opcode == OP_PUSHDATA4: pushdata_type = 'PUSHDATA4' if i + 3 >= len(self): raise CScriptInvalidError('PUSHDATA4: missing data length') - datasize = self[i] + (self[i+1] << 8) + (self[i+2] << 16) + (self[i+3] << 24) + datasize = self[i] + (self[i + 1] << 8) + (self[i + 2] << 16) + (self[i + 3] << 24) i += 4 else: - assert False # shouldn't happen - + assert False # shouldn't happen - data = bytes(self[i:i+datasize]) + data = bytes(self[i:i + datasize]) # Check for truncation if len(data) < datasize: -- cgit v1.2.3 From 1dc68aee66795bd806675913dc0401420383b9d1 Mon Sep 17 00:00:00 2001 From: John Newbery Date: Wed, 30 Oct 2019 10:22:36 -0400 Subject: [tests] add function comments to bignum --- test/functional/test_framework/bignum.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/functional/test_framework/bignum.py b/test/functional/test_framework/bignum.py index 0dd4ea4b42..e70ef2a7ff 100644 --- a/test/functional/test_framework/bignum.py +++ b/test/functional/test_framework/bignum.py @@ -12,12 +12,14 @@ This file is copied from python-bitcoinlib. import struct def bn_bytes(v, have_ext=False): + """Return number of bytes in integer representation of v.""" ext = 0 if have_ext: ext = 1 return (v.bit_length() + 7) // 8 + ext def bn2bin(v): + """Convert a number to a byte array.""" s = bytearray() i = bn_bytes(v) while i > 0: @@ -26,6 +28,7 @@ def bn2bin(v): return s def bn2mpi(v): + """Convert number to MPI format.""" have_ext = False if v.bit_length() > 0: have_ext = (v.bit_length() & 0x07) == 0 @@ -47,11 +50,12 @@ def bn2mpi(v): v_bin[0] |= 0x80 return s + ext + v_bin -# bitcoin-specific little endian format, with implicit size def mpi2vch(s): + """Convert MPI format to bitcoin-specific little endian format, with implicit size.""" r = s[4:] # strip size r = r[::-1] # reverse string, converting BE->LE return r def bn2vch(v): + """Convert number to bitcoin-specific little endian format.""" return bytes(mpi2vch(bn2mpi(v))) -- cgit v1.2.3 From 9a60bef50def228da763fe842bc2a7b9bf4fbcd7 Mon Sep 17 00:00:00 2001 From: John Newbery Date: Wed, 30 Oct 2019 10:25:23 -0400 Subject: [tests] don't encode the integer size in bignum We just throw it away whenever we use the result so don't add it. --- test/functional/test_framework/bignum.py | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) (limited to 'test') diff --git a/test/functional/test_framework/bignum.py b/test/functional/test_framework/bignum.py index e70ef2a7ff..f50c648c42 100644 --- a/test/functional/test_framework/bignum.py +++ b/test/functional/test_framework/bignum.py @@ -9,14 +9,9 @@ endian format. This file is copied from python-bitcoinlib. """ -import struct - -def bn_bytes(v, have_ext=False): +def bn_bytes(v): """Return number of bytes in integer representation of v.""" - ext = 0 - if have_ext: - ext = 1 - return (v.bit_length() + 7) // 8 + ext + return (v.bit_length() + 7) // 8 def bn2bin(v): """Convert a number to a byte array.""" @@ -28,7 +23,7 @@ def bn2bin(v): return s def bn2mpi(v): - """Convert number to MPI format.""" + """Convert number to MPI format, without the sign byte.""" have_ext = False if v.bit_length() > 0: have_ext = (v.bit_length() & 0x07) == 0 @@ -38,7 +33,6 @@ def bn2mpi(v): neg = True v = -v - s = struct.pack(b">I", bn_bytes(v, have_ext)) ext = bytearray() if have_ext: ext.append(0) @@ -48,13 +42,11 @@ def bn2mpi(v): ext[0] |= 0x80 else: v_bin[0] |= 0x80 - return s + ext + v_bin + return ext + v_bin def mpi2vch(s): - """Convert MPI format to bitcoin-specific little endian format, with implicit size.""" - r = s[4:] # strip size - r = r[::-1] # reverse string, converting BE->LE - return r + """Convert MPI format to bitcoin-specific little endian format.""" + return s[::-1] # reverse string, converting BE->LE def bn2vch(v): """Convert number to bitcoin-specific little endian format.""" -- cgit v1.2.3 From a760aa14a974cc18fa70a91f87a96a3db395a624 Mon Sep 17 00:00:00 2001 From: John Newbery Date: Wed, 30 Oct 2019 10:27:25 -0400 Subject: [tests] remove mpi2vch() function All it does is reverse the bytes order. --- test/functional/test_framework/bignum.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'test') diff --git a/test/functional/test_framework/bignum.py b/test/functional/test_framework/bignum.py index f50c648c42..ed440a2cd8 100644 --- a/test/functional/test_framework/bignum.py +++ b/test/functional/test_framework/bignum.py @@ -44,10 +44,6 @@ def bn2mpi(v): v_bin[0] |= 0x80 return ext + v_bin -def mpi2vch(s): - """Convert MPI format to bitcoin-specific little endian format.""" - return s[::-1] # reverse string, converting BE->LE - def bn2vch(v): """Convert number to bitcoin-specific little endian format.""" - return bytes(mpi2vch(bn2mpi(v))) + return bytes(reversed(bn2mpi(v))) -- cgit v1.2.3 From 3b9b38579c59d5b31bd75103618776eafc05c132 Mon Sep 17 00:00:00 2001 From: John Newbery Date: Wed, 30 Oct 2019 10:30:36 -0400 Subject: [tests] remove bn_bytes() function It is one line and is called in one place. --- test/functional/test_framework/bignum.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) (limited to 'test') diff --git a/test/functional/test_framework/bignum.py b/test/functional/test_framework/bignum.py index ed440a2cd8..c34704aa0c 100644 --- a/test/functional/test_framework/bignum.py +++ b/test/functional/test_framework/bignum.py @@ -9,17 +9,12 @@ endian format. This file is copied from python-bitcoinlib. """ -def bn_bytes(v): - """Return number of bytes in integer representation of v.""" - return (v.bit_length() + 7) // 8 - def bn2bin(v): """Convert a number to a byte array.""" s = bytearray() - i = bn_bytes(v) - while i > 0: + bytes_len = (v.bit_length() + 7) // 8 + for i in range(bytes_len, 0, -1): s.append((v >> ((i - 1) * 8)) & 0xff) - i -= 1 return s def bn2mpi(v): -- cgit v1.2.3 From f950ec25201e8ff7948be99ce3171f9700c4a8dc Mon Sep 17 00:00:00 2001 From: John Newbery Date: Wed, 30 Oct 2019 10:39:15 -0400 Subject: [tests] remove bn2bin() It's only called in one place. --- test/functional/test_framework/bignum.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'test') diff --git a/test/functional/test_framework/bignum.py b/test/functional/test_framework/bignum.py index c34704aa0c..529d39c420 100644 --- a/test/functional/test_framework/bignum.py +++ b/test/functional/test_framework/bignum.py @@ -9,29 +9,29 @@ endian format. This file is copied from python-bitcoinlib. """ -def bn2bin(v): - """Convert a number to a byte array.""" - s = bytearray() - bytes_len = (v.bit_length() + 7) // 8 - for i in range(bytes_len, 0, -1): - s.append((v >> ((i - 1) * 8)) & 0xff) - return s - def bn2mpi(v): """Convert number to MPI format, without the sign byte.""" + # The top bit is used to indicate the sign of the number. If there + # isn't a spare bit in the bit length, add an extension byte. have_ext = False + ext = bytearray() if v.bit_length() > 0: have_ext = (v.bit_length() & 0x07) == 0 + ext.append(0) + # Is the number negative? neg = False if v < 0: neg = True v = -v - ext = bytearray() - if have_ext: - ext.append(0) - v_bin = bn2bin(v) + # Convert the int to bytes + v_bin = bytearray() + bytes_len = (v.bit_length() + 7) // 8 + for i in range(bytes_len, 0, -1): + v_bin.append((v >> ((i - 1) * 8)) & 0xff) + + # Add the sign bit if necessary if neg: if have_ext: ext[0] |= 0x80 -- cgit v1.2.3 From 3ed772d2219e58d6afea3d12c0ebebb8487445e7 Mon Sep 17 00:00:00 2001 From: John Newbery Date: Wed, 30 Oct 2019 10:45:03 -0400 Subject: [tests] remove bignum.py It only contains one function and is only imported by one other module (script.py). Just move the function to script.py. --- test/functional/README.md | 3 --- test/functional/test_framework/bignum.py | 44 -------------------------------- test/functional/test_framework/script.py | 35 ++++++++++++++++++++++++- 3 files changed, 34 insertions(+), 48 deletions(-) delete mode 100644 test/functional/test_framework/bignum.py (limited to 'test') diff --git a/test/functional/README.md b/test/functional/README.md index a9b83076eb..92a2bf36f7 100644 --- a/test/functional/README.md +++ b/test/functional/README.md @@ -121,9 +121,6 @@ Utilities for manipulating transaction scripts (originally from python-bitcoinli #### [key.py](test_framework/key.py) Test-only secp256k1 elliptic curve implementation -#### [bignum.py](test_framework/bignum.py) -Helpers for script.py - #### [blocktools.py](test_framework/blocktools.py) Helper functions for creating blocks and transactions. diff --git a/test/functional/test_framework/bignum.py b/test/functional/test_framework/bignum.py deleted file mode 100644 index 529d39c420..0000000000 --- a/test/functional/test_framework/bignum.py +++ /dev/null @@ -1,44 +0,0 @@ -#!/usr/bin/env python3 -# -# Distributed under the MIT software license, see the accompanying -# file COPYING or http://www.opensource.org/licenses/mit-license.php. -"""Big number routines. - -Functions for converting numbers to MPI format and Bitcoin-specific little -endian format. - -This file is copied from python-bitcoinlib. -""" -def bn2mpi(v): - """Convert number to MPI format, without the sign byte.""" - # The top bit is used to indicate the sign of the number. If there - # isn't a spare bit in the bit length, add an extension byte. - have_ext = False - ext = bytearray() - if v.bit_length() > 0: - have_ext = (v.bit_length() & 0x07) == 0 - ext.append(0) - - # Is the number negative? - neg = False - if v < 0: - neg = True - v = -v - - # Convert the int to bytes - v_bin = bytearray() - bytes_len = (v.bit_length() + 7) // 8 - for i in range(bytes_len, 0, -1): - v_bin.append((v >> ((i - 1) * 8)) & 0xff) - - # Add the sign bit if necessary - if neg: - if have_ext: - ext[0] |= 0x80 - else: - v_bin[0] |= 0x80 - return ext + v_bin - -def bn2vch(v): - """Convert number to bitcoin-specific little endian format.""" - return bytes(reversed(bn2mpi(v))) diff --git a/test/functional/test_framework/script.py b/test/functional/test_framework/script.py index 0dbd521433..f14191fe51 100644 --- a/test/functional/test_framework/script.py +++ b/test/functional/test_framework/script.py @@ -9,7 +9,6 @@ This file is modified from python-bitcoinlib. import hashlib import struct -from .bignum import bn2vch from .messages import ( CTransaction, CTxOut, @@ -26,6 +25,40 @@ OPCODE_NAMES = {} def hash160(s): return hashlib.new('ripemd160', sha256(s)).digest() +def bn2vch(v): + """Convert number to bitcoin-specific little endian format.""" + # The top bit is used to indicate the sign of the number. If there + # isn't a spare bit in the bit length, add an extension byte. + have_ext = False + ext = bytearray() + if v.bit_length() > 0: + have_ext = (v.bit_length() & 0x07) == 0 + ext.append(0) + + # Is the number negative? + neg = False + if v < 0: + neg = True + v = -v + + # Convert the int to bytes + v_bin = bytearray() + bytes_len = (v.bit_length() + 7) // 8 + for i in range(bytes_len, 0, -1): + v_bin.append((v >> ((i - 1) * 8)) & 0xff) + + # Add the sign bit if necessary + if neg: + if have_ext: + ext[0] |= 0x80 + else: + v_bin[0] |= 0x80 + + v_bytes = ext + v_bin + # Reverse bytes ordering for LE + v_bytes.reverse() + + return bytes(v_bytes) _opcode_instances = [] class CScriptOp(int): -- cgit v1.2.3