Skip to content

Commit

Permalink
Merge pull request #29 from iamdefinitelyahuman/v1.2.4
Browse files Browse the repository at this point in the history
v1.2.4
  • Loading branch information
iamdefinitelyahuman authored Jan 31, 2024
2 parents 1482c8e + 27f8b69 commit cfc9450
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 77 deletions.
50 changes: 25 additions & 25 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,96 +11,96 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v4

- name: Setup Python 3.8
uses: actions/setup-python@v1
- name: Setup Python 3.10
uses: actions/setup-python@v5
with:
python-version: 3.8
python-version: '3.10'

- name: Install Tox
run: pip install tox

- name: Run Tox
run: tox -e lint

py38core:
py310core:
runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [windows-latest, ubuntu-latest]

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v4

- name: Setup Python 3.8
uses: actions/setup-python@v1
- name: Setup Python 3.10
uses: actions/setup-python@v5
with:
python-version: 3.8
python-version: '3.10'

- name: Install Tox
run: pip install tox

- name: Run Tox
run: tox -e py38
run: tox -e py310

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
with:
file: ./coverage.xml
fail_ci_if_error: true
fail_ci_if_error: false

py37core:
py311core:
runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [windows-latest, ubuntu-latest]

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v4

- name: Setup Python 3.7
uses: actions/setup-python@v1
- name: Setup Python 3.11
uses: actions/setup-python@v5
with:
python-version: 3.7
python-version: '3.11'

- name: Install Tox
run: pip install tox

- name: Run Tox
run: tox -e py37
run: tox -e py311

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
with:
file: ./coverage.xml
fail_ci_if_error: true
fail_ci_if_error: false

py36core:
py312core:
runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [windows-latest, ubuntu-latest]

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v4

- name: Setup Python 3.6
uses: actions/setup-python@v1
- name: Setup Python 3.12
uses: actions/setup-python@v5
with:
python-version: 3.6
python-version: '3.12'

- name: Install Tox
run: pip install tox

- name: Run Tox
run: tox -e py36
run: tox -e py312

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
with:
file: ./coverage.xml
fail_ci_if_error: true
fail_ci_if_error: false
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ This changelog format is based on [Keep a Changelog](https://keepachangelog.com/
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased](https://github.com/iamdefinitelyahuman/eth-event)\

## [1.2.4](https://github.com/iamdefinitelyahuman/eth-event/releases/tag/v1.2.4) - 2024-01-31
### Added
- Include `logIndex`, `blockNumber`, `transactionIndex` in decoded event logs

### Fixed
- Bump dependencies
- Updates to handle downstream breaking changes

## [1.2.3](https://github.com/iamdefinitelyahuman/eth-event/releases/tag/v1.2.3) - 2021-04-16
### Fixed
- Do not require `anonymous` field in ABI
Expand Down
34 changes: 19 additions & 15 deletions eth_event/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import re
from typing import Dict, List

from eth_abi import decode_abi, decode_single
import eth_abi
from eth_abi.exceptions import InsufficientDataBytes, NoEntriesFound, NonEmptyPaddingBytes
from eth_hash.auto import keccak
from eth_utils import to_checksum_address
Expand All @@ -26,7 +26,7 @@ class UnknownEvent(Exception):
pass


ADD_LOG_ENTRIES = ['logIndex', 'blockNumber', 'transactionIndex']
ADD_LOG_ENTRIES = ["logIndex", "blockNumber", "transactionIndex"]


def get_log_topic(event_abi: Dict) -> str:
Expand All @@ -51,7 +51,7 @@ def get_log_topic(event_abi: Dict) -> str:
types = _params(event_abi["inputs"])
key = f"{event_abi['name']}({','.join(types)})".encode()

return "0x" + keccak(key).hex()
return _0xstring(keccak(key))


def get_topic_map(abi: List) -> Dict:
Expand Down Expand Up @@ -128,7 +128,7 @@ def decode_log(log: Dict, topic_map: Dict) -> Dict:
if not log["topics"]:
raise EventError("Cannot decode an anonymous event")

key = HexBytes(log["topics"][0]).hex()
key = _0xstring(log["topics"][0])
if key not in topic_map:
raise UnknownEvent("Event topic is not present in given ABI")
abi = topic_map[key]
Expand Down Expand Up @@ -183,14 +183,14 @@ def decode_logs(logs: List, topic_map: Dict, allow_undecoded: bool = False) -> L
events = []

for item in logs:
topics = [HexBytes(i).hex() for i in item["topics"]]
topics = [_0xstring(i) for i in item["topics"]]
if not topics or topics[0] not in topic_map:
if not allow_undecoded:
raise UnknownEvent("Log contains undecodable event")
event = {
"name": None,
"topics": topics,
"data": HexBytes(item["data"]).hex(),
"data": _0xstring(item["data"]),
"decoded": False,
"address": to_checksum_address(item["address"]),
}
Expand All @@ -211,7 +211,7 @@ def append_additional_log_data(log: Dict, event: Dict, log_entries: List[str]):


def decode_traceTransaction(
struct_logs: List, topic_map: Dict, allow_undecoded: bool = False, initial_address: str = None
struct_logs: List, topic_map: Dict, allow_undecoded: bool = False, initial_address: str = None
) -> List:
"""
Extract and decode a list of event logs from a transaction traceback.
Expand Down Expand Up @@ -265,14 +265,14 @@ def decode_traceTransaction(
offset = int(step["stack"][-1], 16)
length = int(step["stack"][-2], 16)
topic_len = int(step["op"][-1])
topics = [HexBytes(i).hex() for i in step["stack"][-3 : -3 - topic_len : -1]]
topics = [_0xstring(i) for i in step["stack"][-3 : -3 - topic_len : -1]]
except KeyError:
raise StructLogError("StructLog has no stack")
except (IndexError, TypeError):
raise StructLogError("Malformed stack")

try:
data = HexBytes("".join(step["memory"]))[offset : offset + length].hex()
data = _0xstring(HexBytes("".join(step["memory"]))[offset : offset + length].hex())
except (KeyError, TypeError):
raise StructLogError("Malformed memory")

Expand All @@ -298,6 +298,11 @@ def decode_traceTransaction(
return events


def _0xstring(value: bytes) -> str:
# prepend bytes with 0x to avoid a breaking change from HexBytes v1
return f"0x{HexBytes(value).hex()}"


def _params(abi_params: List) -> List:
types = []
# regex with 2 capturing groups
Expand Down Expand Up @@ -345,10 +350,10 @@ def _decode(inputs: List, topics: List, data: str) -> List:

if unindexed_types and data == "0x":
length = len(unindexed_types) * 32
data = f"0x{bytes(length).hex()}"
data = _0xstring(length)

try:
decoded = list(decode_abi(unindexed_types, HexBytes(data)))[::-1]
decoded = list(eth_abi.decode(unindexed_types, HexBytes(data)))[::-1]
except InsufficientDataBytes:
raise EventError("Event data has insufficient length")
except NonEmptyPaddingBytes:
Expand All @@ -368,17 +373,16 @@ def _decode(inputs: List, topics: List, data: str) -> List:
if topics and i["indexed"]:
encoded = HexBytes(topics.pop())
try:
value = decode_single(i["type"], encoded)
value = eth_abi.decode([i["type"]], encoded)[0]
except (InsufficientDataBytes, NoEntriesFound, OverflowError):
# an array or other data type that uses multiple slots
result[-1].update({"value": encoded.hex(), "decoded": False})
result[-1].update({"value": _0xstring(encoded), "decoded": False})
continue
else:
value = decoded.pop()

if isinstance(value, bytes):
# converting to `HexBytes` first ensures the leading `0x`
value = HexBytes(value).hex()
value = _0xstring(value)
result[-1].update({"value": value, "decoded": True})

return result
11 changes: 11 additions & 0 deletions requirements-dev.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
black
bumpversion
flake8
isort
mypy
pip-tools
pytest
pytest-cov
tox
twine
wheel
Loading

0 comments on commit cfc9450

Please sign in to comment.