Skip to content

Commit 108ea28

Browse files
authored
Merge pull request #141 from firedancer-io/mjain/reorg-utils
chore: consolidate codec utils
2 parents 57c0dfc + 8cca12b commit 108ea28

File tree

4 files changed

+45
-38
lines changed

4 files changed

+45
-38
lines changed

src/test_suite/context/codec_utils.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,38 @@
11
import test_suite.context_pb2 as context_pb
22
import fd58
3-
from test_suite.fuzz_interface import encode_hex_compact, decode_hex_compact
3+
import re
4+
5+
6+
def decode_hex_compact(encoded):
7+
res = bytearray()
8+
parts = re.split(r"\.\.\.(\d+) zeros\.\.\.", encoded.decode("ascii"))
9+
10+
for i, part in enumerate(parts):
11+
if i % 2 == 0:
12+
# Regular hex part
13+
res.extend(bytes.fromhex(part))
14+
else:
15+
# Skipped zeros part
16+
res.extend(b"\x00" * int(part))
17+
18+
return bytes(res)
19+
20+
21+
def encode_hex_compact(buf, gap=16):
22+
res = ""
23+
skipped = 0
24+
for i in range(0, len(buf), gap):
25+
row = buf[i : i + gap]
26+
if row == bytes([0] * len(row)):
27+
skipped += len(row)
28+
else:
29+
if skipped > 0:
30+
res += f"...{skipped} zeros..."
31+
res += "".join([f"{b:0>2x}" for b in buf[i : i + gap]])
32+
skipped = 0
33+
if skipped > 0:
34+
res += f"...{skipped} zeros..."
35+
return bytes(res, "ascii")
436

537

638
def encode_acct_state(acct_state: context_pb.AcctState):

src/test_suite/fuzz_interface.py

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from typing import Callable, Type, TypeVar
22
from google.protobuf import message, descriptor, message_factory
33
from dataclasses import dataclass, InitVar, field
4-
import re
54

65
msg_factory = message_factory.MessageFactory()
76

@@ -27,38 +26,6 @@
2726
"""
2827

2928

30-
def decode_hex_compact(encoded):
31-
res = bytearray()
32-
parts = re.split(r"\.\.\.(\d+) zeros\.\.\.", encoded.decode("ascii"))
33-
34-
for i, part in enumerate(parts):
35-
if i % 2 == 0:
36-
# Regular hex part
37-
res.extend(bytes.fromhex(part))
38-
else:
39-
# Skipped zeros part
40-
res.extend(b"\x00" * int(part))
41-
42-
return bytes(res)
43-
44-
45-
def encode_hex_compact(buf, gap=16):
46-
res = ""
47-
skipped = 0
48-
for i in range(0, len(buf), gap):
49-
row = buf[i : i + gap]
50-
if row == bytes([0] * len(row)):
51-
skipped += len(row)
52-
else:
53-
if skipped > 0:
54-
res += f"...{skipped} zeros..."
55-
res += "".join([f"{b:0>2x}" for b in buf[i : i + gap]])
56-
skipped = 0
57-
if skipped > 0:
58-
res += f"...{skipped} zeros..."
59-
return bytes(res, "ascii")
60-
61-
6229
def generic_effects_prune(
6330
ctx: ContextType | None, effects: dict[str, str | None]
6431
) -> dict[str, str | None] | None:

src/test_suite/instr/codec_utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import fd58
2-
from test_suite.context.codec_utils import decode_acct_state, encode_acct_state
3-
from test_suite.fuzz_interface import decode_hex_compact, encode_hex_compact
2+
from test_suite.context.codec_utils import (
3+
decode_acct_state,
4+
decode_hex_compact,
5+
encode_acct_state,
6+
encode_hex_compact,
7+
)
48
import test_suite.invoke_pb2 as invoke_pb
59

610

src/test_suite/txn/codec_utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import fd58
2-
from test_suite.context.codec_utils import decode_acct_state, encode_acct_state
3-
from test_suite.fuzz_interface import decode_hex_compact, encode_hex_compact
2+
from test_suite.context.codec_utils import (
3+
decode_acct_state,
4+
decode_hex_compact,
5+
encode_acct_state,
6+
encode_hex_compact,
7+
)
48
import test_suite.txn_pb2 as txn_pb
59

610

0 commit comments

Comments
 (0)