Skip to content

Commit c2892ff

Browse files
committed
refactor(scripts): mode execute dependencies from construct
1 parent 97bbcc4 commit c2892ff

File tree

3 files changed

+54
-60
lines changed

3 files changed

+54
-60
lines changed

hathor/transaction/scripts/construct.py

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,13 @@
1717

1818
from hathor.conf.get_settings import get_settings
1919
from hathor.crypto.util import decode_address
20-
from hathor.transaction.exceptions import OutOfData, ScriptError
20+
from hathor.transaction.exceptions import ScriptError
2121
from hathor.transaction.scripts.base_script import BaseScript
2222

2323
if TYPE_CHECKING:
2424
from hathor.transaction.scripts import P2PKH, MultiSig, Opcode
2525

2626

27-
class OpcodePosition(NamedTuple):
28-
opcode: int
29-
position: int
30-
31-
3227
def re_compile(pattern: str) -> Pattern[bytes]:
3328
""" Transform a given script pattern into a regular expression.
3429
@@ -145,51 +140,6 @@ def parse_address_script(script: bytes) -> Optional[Union['P2PKH', 'MultiSig']]:
145140
return None
146141

147142

148-
def get_data_bytes(position: int, length: int, data: bytes) -> bytes:
149-
""" Extract `length` bytes from `data` starting at `position`
150-
151-
:param position: start position of bytes string to extract
152-
:type position: int
153-
154-
:param length: len of bytes str to extract
155-
:type length: int
156-
157-
:param data: script containing data to extract
158-
:type data: bytes
159-
160-
:raises OutOfData: when trying to read out of script
161-
162-
:return: bytes string of extracted data
163-
:rtype: bytes
164-
"""
165-
if not (0 < length <= len(data)):
166-
raise OutOfData("length ({}) should be from 0 up to data length".format(length))
167-
if not (0 < position < len(data)):
168-
raise OutOfData("position should be inside data")
169-
if (position+length) > len(data):
170-
raise OutOfData('trying to read {} bytes starting at {}, available {}'.format(length, position, len(data)))
171-
return data[position:position+length]
172-
173-
174-
def get_data_single_byte(position: int, data: bytes) -> int:
175-
""" Extract 1 byte from `data` at `position`
176-
177-
:param position: position of byte to extract
178-
:type position: int
179-
180-
:param data: script containing data to extract
181-
:type data: bytes
182-
183-
:raises OutOfData: when trying to read out of script
184-
185-
:return: extracted byte
186-
:rtype: int
187-
"""
188-
if not (0 <= position < len(data)):
189-
raise OutOfData("trying to read a byte at {} outside of data, available {}".format(position, len(data)))
190-
return data[position]
191-
192-
193143
class _ScriptOperation(NamedTuple):
194144
opcode: Union['Opcode', int]
195145
position: int

hathor/transaction/scripts/execute.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
from hathor.transaction import BaseTransaction, Transaction, TxInput
1919
from hathor.transaction.exceptions import DataIndexError, FinalStackInvalid, InvalidScriptError, OutOfData, ScriptError
20-
from hathor.transaction.scripts.construct import OpcodePosition, get_data_bytes, get_data_single_byte
2120

2221

2322
class ScriptExtras(NamedTuple):
@@ -35,6 +34,11 @@ class ScriptExtras(NamedTuple):
3534
Stack = list[Union[bytes, int, str]]
3635

3736

37+
class OpcodePosition(NamedTuple):
38+
opcode: int
39+
position: int
40+
41+
3842
def execute_eval(data: bytes, log: list[str], extras: ScriptExtras) -> None:
3943
""" Execute eval from data executing opcode methods
4044
@@ -257,3 +261,48 @@ def binary_to_int(binary: bytes) -> int:
257261

258262
(value,) = struct.unpack(_format, binary)
259263
return value
264+
265+
266+
def get_data_bytes(position: int, length: int, data: bytes) -> bytes:
267+
""" Extract `length` bytes from `data` starting at `position`
268+
269+
:param position: start position of bytes string to extract
270+
:type position: int
271+
272+
:param length: len of bytes str to extract
273+
:type length: int
274+
275+
:param data: script containing data to extract
276+
:type data: bytes
277+
278+
:raises OutOfData: when trying to read out of script
279+
280+
:return: bytes string of extracted data
281+
:rtype: bytes
282+
"""
283+
if not (0 < length <= len(data)):
284+
raise OutOfData("length ({}) should be from 0 up to data length".format(length))
285+
if not (0 < position < len(data)):
286+
raise OutOfData("position should be inside data")
287+
if (position+length) > len(data):
288+
raise OutOfData('trying to read {} bytes starting at {}, available {}'.format(length, position, len(data)))
289+
return data[position:position+length]
290+
291+
292+
def get_data_single_byte(position: int, data: bytes) -> int:
293+
""" Extract 1 byte from `data` at `position`
294+
295+
:param position: position of byte to extract
296+
:type position: int
297+
298+
:param data: script containing data to extract
299+
:type data: bytes
300+
301+
:raises OutOfData: when trying to read out of script
302+
303+
:return: extracted byte
304+
:rtype: int
305+
"""
306+
if not (0 <= position < len(data)):
307+
raise OutOfData("trying to read a byte at {} outside of data, available {}".format(position, len(data)))
308+
return data[position]

tests/tx/test_scripts.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,13 @@
2626
create_base_script,
2727
create_output_script,
2828
)
29-
from hathor.transaction.scripts.construct import (
30-
count_sigops,
31-
get_data_bytes,
32-
get_data_single_byte,
33-
get_pushdata,
34-
get_sigops_count,
35-
re_compile,
36-
)
29+
from hathor.transaction.scripts.construct import count_sigops, get_pushdata, get_sigops_count, re_compile
3730
from hathor.transaction.scripts.execute import (
3831
binary_to_int,
3932
decode_opn,
4033
evaluate_final_stack,
34+
get_data_bytes,
35+
get_data_single_byte,
4136
get_data_value,
4237
get_script_op,
4338
)

0 commit comments

Comments
 (0)