Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
77 changes: 35 additions & 42 deletions cdv/examples/tests/test_piggybank.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,64 +68,57 @@ async def make_and_spend_piggybank(self, network, alice, bob, CONTRIBUTION_AMOUN
@pytest.mark.asyncio
async def test_piggybank_contribution(self, setup):
network, alice, bob = setup
try:
result: dict[str, list[Coin]] = await self.make_and_spend_piggybank(network, alice, bob, 500)
result: dict[str, list[Coin]] = await self.make_and_spend_piggybank(network, alice, bob, 500)

assert "error" not in result
assert "error" not in result

# Make sure there is exactly one piggybank with the new amount
filtered_result: list[Coin] = list(
filter(
lambda addition: (addition.amount == 501)
# Make sure there is exactly one piggybank with the new amount
filtered_result: list[Coin] = list(
filter(
lambda addition: (
(addition.amount == 501)
and (
addition.puzzle_hash == create_piggybank_puzzle(1000000000000, bob.puzzle_hash).get_tree_hash()
),
result["additions"],
)
)
),
result["additions"],
)
assert len(filtered_result) == 1
finally:
pass
)
assert len(filtered_result) == 1

@pytest.mark.asyncio
async def test_piggybank_completion(self, setup):
network, alice, bob = setup
try:
result: dict[str, list[Coin]] = await self.make_and_spend_piggybank(network, alice, bob, 1000000000000)
result: dict[str, list[Coin]] = await self.make_and_spend_piggybank(network, alice, bob, 1000000000000)

assert "error" not in result
assert "error" not in result

# Make sure there is exactly one piggybank with value 0
filtered_result: list[Coin] = list(
filter(
lambda addition: (addition.amount == 0)
# Make sure there is exactly one piggybank with value 0
filtered_result: list[Coin] = list(
filter(
lambda addition: (
(addition.amount == 0)
and (
addition.puzzle_hash == create_piggybank_puzzle(1000000000000, bob.puzzle_hash).get_tree_hash()
),
result["additions"],
)
)
),
result["additions"],
)
assert len(filtered_result) == 1

# Make sure there is exactly one coin that has been cashed out to bob
filtered_result: list[Coin] = list(
filter(
lambda addition: (addition.amount == 1000000000001) and (addition.puzzle_hash == bob.puzzle_hash),
result["additions"],
)
)
assert len(filtered_result) == 1

# Make sure there is exactly one coin that has been cashed out to bob
filtered_result: list[Coin] = list(
filter(
lambda addition: (addition.amount == 1000000000001) and (addition.puzzle_hash == bob.puzzle_hash),
result["additions"],
)
assert len(filtered_result) == 1
finally:
pass
)
assert len(filtered_result) == 1

@pytest.mark.asyncio
async def test_piggybank_stealing(self, setup):
network, alice, bob = setup
try:
result: dict[str, list[Coin]] = await self.make_and_spend_piggybank(network, alice, bob, -100)
assert "error" in result
assert (
"GENERATOR_RUNTIME_ERROR" in result["error"]
) # This fails during puzzle execution, not in driver code
finally:
pass
result: dict[str, list[Coin]] = await self.make_and_spend_piggybank(network, alice, bob, -100)
assert "error" in result
assert "GENERATOR_RUNTIME_ERROR" in result["error"] # This fails during puzzle execution, not in driver code
6 changes: 3 additions & 3 deletions cdv/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import contextlib
import datetime
import struct
from collections.abc import AsyncIterator
from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager

import pytimeparse
Expand Down Expand Up @@ -568,7 +568,7 @@ class Network:

@classmethod
@contextlib.asynccontextmanager
async def managed(cls) -> AsyncIterator[Self]:
async def managed(cls) -> AsyncGenerator[Self, None]:
self = cls()
self.time = datetime.timedelta(days=18750, seconds=61201) # Past the initial transaction freeze
async with SpendSim.managed() as sim:
Expand Down Expand Up @@ -709,7 +709,7 @@ async def push_tx(self, bundle: SpendBundle) -> dict[str, str | list[Coin]]:


@asynccontextmanager
async def setup() -> AsyncIterator[tuple[Network, Wallet, Wallet]]:
async def setup() -> AsyncGenerator[tuple[Network, Wallet, Wallet], None]:
async with Network.managed() as network:
alice = network.make_wallet("alice")
bob = network.make_wallet("bob")
Expand Down
5 changes: 1 addition & 4 deletions cdv/test/test_skeleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,4 @@ async def setup(self):
@pytest.mark.asyncio
async def test_something(self, setup):
_network, _alice, _bob = setup
try:
pass
finally:
pass
pass
71 changes: 34 additions & 37 deletions cdv/util/load_clvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,45 @@

# Handle optional use of clvm_tools_rs if available and requested
if "CLVM_TOOLS_RS" in os.environ:
try:

def sha256file(f):
import hashlib
def sha256file(f):
import hashlib

m = hashlib.sha256()
m.update(open(f).read().encode("utf8"))
return m.hexdigest()
m = hashlib.sha256()
m.update(open(f).read().encode("utf8"))
return m.hexdigest()

from clvm_tools_rs import compile_clvm as compile_clvm_rs # type: ignore[import-untyped]
from clvm_tools_rs import compile_clvm as compile_clvm_rs # type: ignore[import-untyped]

def translate_path(p_):
p = str(p_)
if os.path.isdir(p):
def translate_path(p_):
p = str(p_)
if os.path.isdir(p):
return p
else:
try:
module_object = importlib.import_module(p)
return os.path.dirname(inspect.getfile(module_object))
except Exception:
return p
else:
try:
module_object = importlib.import_module(p)
return os.path.dirname(inspect.getfile(module_object))
except Exception:
return p

def rust_compile_clvm(full_path, output, search_paths=[]):
treated_include_paths = list(map(translate_path, search_paths))
print("compile_clvm_rs", full_path, output, treated_include_paths)
compile_clvm_rs(str(full_path), str(output), treated_include_paths)

if os.environ["CLVM_TOOLS_RS"] == "check":
assert False
orig = str(output) + ".orig"
compile_clvm_py(full_path, orig, search_paths=search_paths)
orig256 = sha256file(orig)
rs256 = sha256file(output)

if orig256 != rs256:
print(f"Compiled {full_path}: {orig256} vs {rs256}\n")
print("Aborting compilation due to mismatch with rust")
assert orig256 == rs256

compile_clvm = rust_compile_clvm
finally:
pass

def rust_compile_clvm(full_path, output, search_paths=[]):
treated_include_paths = list(map(translate_path, search_paths))
print("compile_clvm_rs", full_path, output, treated_include_paths)
compile_clvm_rs(str(full_path), str(output), treated_include_paths)

if os.environ["CLVM_TOOLS_RS"] == "check":
assert False
orig = str(output) + ".orig"
compile_clvm_py(full_path, orig, search_paths=search_paths)
orig256 = sha256file(orig)
rs256 = sha256file(output)

if orig256 != rs256:
print(f"Compiled {full_path}: {orig256} vs {rs256}\n")
print("Aborting compilation due to mismatch with rust")
assert orig256 == rs256

compile_clvm = rust_compile_clvm


def load_serialized_clvm(clvm_filename, package_or_requirement=__name__, search_paths=[]) -> SerializedProgram:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ dependencies = [
dev = [
"anyio",
"mypy",
"ruff==0.14.6",
"ruff==0.17.1",
"types-aiofiles",
"types-click",
"types-cryptography",
Expand Down
54 changes: 28 additions & 26 deletions ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,52 +18,54 @@ select = [
explicit-preview-rules = false
ignore = [
# Pylint convention
"PLC0415", # import-outside-top-level
"PLC1901", # compare-to-empty-string
"import-outside-top-level",
"compare-to-empty-string",
# Should probably fix these
"PLC2801", # unnecessary-dunder-call
"PLC2701", # import-private-name
"unnecessary-dunder-call",
"import-private-name",

# Pylint refactor
"PLR0915", # too-many-statements
"PLR0914", # too-many-locals
"PLR0913", # too-many-arguments
"PLR0912", # too-many-branches
"PLR1702", # too-many-nested-blocks
"PLR0904", # too-many-public-methods
"PLR0917", # too-many-positional-arguments
"PLR0916", # too-many-boolean-expressions
"PLR0911", # too-many-return-statements
"too-many-statements",
"too-many-locals",
"too-many-arguments",
"too-many-branches",
"too-many-nested-blocks",
"too-many-public-methods",
"too-many-positional-arguments",
"too-many-boolean-expressions",
"too-many-return-statements",
# Should probably fix these
"PLR6301", # no-self-use
"PLR2004", # magic-value-comparison
"PLR1704", # redefined-argument-from-local
"PLR5501", # collapsible-else-if
"no-self-use",
"magic-value-comparison",
"redefined-argument-from-local",
"collapsible-else-if",

# Pylint warning
"PLW1641", # eq-without-hash
"eq-without-hash",
# Should probably fix these
"PLW2901", # redefined-loop-name
"PLW1514", # unspecified-encoding
"PLW0603", # global-statement
"redefined-loop-name",
"unspecified-encoding",
"global-statement",

# Flake8 async
# Should probably fix these
"ASYNC230", # blocking-open-call-in-async-function
"blocking-open-call-in-async-function",
# Should probably fix these after dealing with shielding for anyio
"ASYNC109", # async-function-with-timeout
"async-function-with-timeout",

# flake8-implicit-str-concat
"ISC003", # explicit-string-concatenation
"explicit-string-concatenation",

# Ruff Specific
# Module-level code is intentional in __init__.py files.
"non-empty-init-module",

# This code is problematic because using instantiated types as defaults is so common across the codebase.
# Its purpose is to prevent accidenatally assigning mutable defaults. However, this is a bit overkill for that.
# That being said, it would be nice to add some way to actually guard against that specific mutable default behavior.
"RUF009", # function-call-in-dataclass-default-argument
"function-call-in-dataclass-default-argument",
# Should probably fix this
"RUF029",
"unused-async",
]


Expand Down
5 changes: 1 addition & 4 deletions tests/test_skeleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ async def setup(self):
@pytest.mark.asyncio
async def test_something(self, setup):
_network, _alice, _bob = setup
try:
pass
finally:
pass
pass

@pytest.mark.asyncio
async def test_skip_time_invalid_duration(self, setup):
Expand Down
Loading