Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/script.py
Show First 20 Lines • Show All 103 Lines • ▼ Show 20 Lines | def __repr__(self): | ||||
else: | else: | ||||
return 'CScriptOp(0x{:x})'.format(self) | return 'CScriptOp(0x{:x})'.format(self) | ||||
def __new__(cls, n): | def __new__(cls, n): | ||||
try: | try: | ||||
return _opcode_instances[n] | return _opcode_instances[n] | ||||
except IndexError: | except IndexError: | ||||
assert len(_opcode_instances) == n | assert len(_opcode_instances) == n | ||||
_opcode_instances.append(super(CScriptOp, cls).__new__(cls, n)) | _opcode_instances.append(super().__new__(cls, n)) | ||||
return _opcode_instances[n] | return _opcode_instances[n] | ||||
# Populate opcode instance table | # Populate opcode instance table | ||||
for n in range(0xff + 1): | for n in range(0xff + 1): | ||||
CScriptOp(n) | CScriptOp(n) | ||||
▲ Show 20 Lines • Show All 275 Lines • ▼ Show 20 Lines | class CScriptInvalidError(Exception): | ||||
pass | pass | ||||
class CScriptTruncatedPushDataError(CScriptInvalidError): | class CScriptTruncatedPushDataError(CScriptInvalidError): | ||||
"""Invalid pushdata due to truncation""" | """Invalid pushdata due to truncation""" | ||||
def __init__(self, msg, data): | def __init__(self, msg, data): | ||||
self.data = data | self.data = data | ||||
super(CScriptTruncatedPushDataError, self).__init__(msg) | super().__init__(msg) | ||||
# This is used, eg, for blockchain heights in coinbase scripts (bip34) | # This is used, eg, for blockchain heights in coinbase scripts (bip34) | ||||
class CScriptNum: | class CScriptNum: | ||||
__slots__ = ("value",) | __slots__ = ("value",) | ||||
def __init__(self, d=0): | def __init__(self, d=0): | ||||
self.value = d | self.value = d | ||||
▲ Show 20 Lines • Show All 69 Lines • ▼ Show 20 Lines | def __add__(self, other): | ||||
raise NotImplementedError | raise NotImplementedError | ||||
def join(self, iterable): | def join(self, iterable): | ||||
# join makes no sense for a CScript() | # join makes no sense for a CScript() | ||||
raise NotImplementedError | raise NotImplementedError | ||||
def __new__(cls, value=b''): | def __new__(cls, value=b''): | ||||
if isinstance(value, bytes) or isinstance(value, bytearray): | if isinstance(value, bytes) or isinstance(value, bytearray): | ||||
return super(CScript, cls).__new__(cls, value) | return super().__new__(cls, value) | ||||
else: | else: | ||||
def coerce_iterable(iterable): | def coerce_iterable(iterable): | ||||
for instance in iterable: | for instance in iterable: | ||||
yield cls.__coerce_instance(instance) | yield cls.__coerce_instance(instance) | ||||
# Annoyingly on both python2 and python3 bytes.join() always | # Annoyingly on both python2 and python3 bytes.join() always | ||||
# returns a bytes instance even when subclassed. | # returns a bytes instance even when subclassed. | ||||
return super(CScript, cls).__new__( | return super().__new__( | ||||
cls, b''.join(coerce_iterable(value))) | cls, b''.join(coerce_iterable(value))) | ||||
def raw_iter(self): | def raw_iter(self): | ||||
"""Raw iteration | """Raw iteration | ||||
Yields tuples of (opcode, data, sop_idx) so that the different possible | Yields tuples of (opcode, data, sop_idx) so that the different possible | ||||
PUSHDATA encodings can be accurately distinguished, as well as | PUSHDATA encodings can be accurately distinguished, as well as | ||||
determining the exact opcode byte indexes. (sop_idx) | determining the exact opcode byte indexes. (sop_idx) | ||||
▲ Show 20 Lines • Show All 247 Lines • Show Last 20 Lines |