Skip to content

Commit 7953d1b

Browse files
authored
fix(API): corruption of abridged calldata (#2618)
1 parent 5b486b4 commit 7953d1b

File tree

3 files changed

+11
-25
lines changed

3 files changed

+11
-25
lines changed

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
},
8787
include_package_data=True,
8888
install_requires=[
89-
"click>=8.1.6,<9",
89+
"click>=8.1.6,<8.2",
9090
"ijson>=3.1.4,<4",
9191
"ipython>=8.18.1,<9",
9292
"lazyasd>=0.1.4",

src/ape/api/transactions.py

+8-19
Original file line numberDiff line numberDiff line change
@@ -203,25 +203,14 @@ def to_string(self, calldata_repr: Optional["CalldataRepr"] = None) -> str:
203203
# If was not specified, use the default value from the config.
204204
calldata_repr = self.local_project.config.display.calldata
205205

206-
if calldata_repr == "full" or len(data["data"]) <= 9:
207-
data["data"] = (
208-
to_hex(bytes(data["data"], encoding="utf8"))
209-
if isinstance(data["data"], str)
210-
else to_hex(bytes(data["data"]))
211-
)
212-
213-
else:
214-
# Only want to specify encoding if data["data"] is a string
215-
data["data"] = (
216-
(
217-
"0x"
218-
+ bytes(data["data"][:4], encoding="utf8").hex()
219-
+ "..."
220-
+ bytes(data["data"][-4:], encoding="utf8").hex()
221-
)
222-
if isinstance(data["data"], str)
223-
else to_hex(bytes(data["data"][:4])) + "..." + to_hex(bytes(data["data"][-4:]))
224-
)
206+
# Ellide the transaction calldata for abridged representations if the length exceeds 8
207+
# (4 bytes for function selector and trailing 4 bytes).
208+
calldata = HexBytes(data["data"])
209+
data["data"] = (
210+
calldata[:4].to_0x_hex() + "..." + calldata[-4:].hex()
211+
if calldata_repr == "abridged" and len(calldata) > 8
212+
else calldata.to_0x_hex()
213+
)
225214

226215
params = "\n ".join(f"{k}: {v}" for k, v in data.items())
227216
cls_name = getattr(type(self), "__name__", TransactionAPI.__name__)

tests/functional/test_transaction.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -315,18 +315,15 @@ def test_str_when_data_is_long_shows_first_4_bytes(vyper_contract_instance):
315315
txn = vyper_contract_instance.setNumber.as_transaction(123)
316316
actual = str(txn)
317317
assert isinstance(actual, str)
318-
assert "data: 0x30783366..." in actual
318+
assert "data: 0x3fb5c1cb..." in actual
319319

320320

321321
def test_str_when_data_is_long_and_configured_full_calldata(project, vyper_contract_instance):
322322
txn = vyper_contract_instance.setNumber.as_transaction(123)
323323
with project.temp_config(display={"calldata": "full"}):
324324
actual = str(txn)
325325

326-
expected = (
327-
"data: 0x3078336662356331636230303030303030303030303030303030303030303030303030"
328-
"303030303030303030303030303030303030303030303030303030303030303030303030303762"
329-
)
326+
expected = "data: 0x3fb5c1cb000000000000000000000000000000000000000000000000000000000000007b"
330327
assert isinstance(actual, str)
331328
assert expected in actual
332329

0 commit comments

Comments
 (0)