Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor lib9c models #216

Merged
merged 9 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions common/lib9c/models/address.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from __future__ import annotations


class Address:
def __init__(self, addr: str):
if addr.startswith("0x"):
if len(addr) != 42:
raise ValueError("Address with 0x prefix must have exact 42 chars.")
self.raw = bytes.fromhex(addr[2:])
else:
if len(addr) != 40:
raise ValueError("Address without 0x prefix must have exact 40 chars.")
self.raw = bytes.fromhex(addr)

@property
def long_format(self):
return f"0x{self.raw.hex()}"

@property
def short_format(self):
return self.raw.hex()

def __eq__(self, other: Address):
return self.raw == other.raw
20 changes: 20 additions & 0 deletions tests/lib9c/models/test_address.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import pytest

from common.lib9c.models.address import Address


@pytest.mark.parametrize("addr",
["0xa5f7e0bd63AD2749D66380f36Eb33Fe0ba50A27D",
"0xb3cbca0e64aeb4b5b861047fe1db5a1bec1c241f",
"a5f7e0bd63AD2749D66380f36Eb33Fe0ba50A27D",
"b3cbca0e64aeb4b5b861047fe1db5a1bec1c241f",
])
def test_address(addr):
address = Address(addr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ValueError 발생 케이스를 추가하면 좋을것 같습니다.

assert len(address.raw) == 20
if addr.startswith("0x"):
assert address.raw == bytes.fromhex(addr[2:])
assert address.long_format == addr.lower()
else:
assert address.raw == bytes.fromhex(addr)
assert address.short_format == addr.lower()