Skip to content

Commit

Permalink
remove remaining load_clvms that can be factored out
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-o-how committed Jan 3, 2025
1 parent 195a768 commit 46032cf
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 102 deletions.
6 changes: 2 additions & 4 deletions chia/_tests/wallet/db_wallet/test_db_graftroot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@
from chia.types.coin_spend import make_spend
from chia.types.mempool_inclusion_status import MempoolInclusionStatus
from chia.util.errors import Err
from chia.wallet.puzzles.load_clvm import load_clvm
from chia.wallet.db_wallet.db_wallet_puzzles import GRAFTROOT_DL_OFFERS
from chia.wallet.util.merkle_utils import build_merkle_tree, build_merkle_tree_from_binary_tree, simplify_merkle_proof
from chia.wallet.wallet_spend_bundle import WalletSpendBundle

GRAFTROOT_MOD = load_clvm("graftroot_dl_offers.clsp", package_or_requirement="chia.data_layer.puzzles")

# Always returns the last value
# (mod solution
#
Expand Down Expand Up @@ -46,7 +44,7 @@ async def test_graftroot(cost_logger: CostLogger) -> None:
desired_key_values = ((bytes32.zeros, bytes32([1] * 32)), (bytes32([7] * 32), bytes32([8] * 32)))
desired_row_hashes: list[bytes32] = [build_merkle_tree_from_binary_tree(kv)[0] for kv in desired_key_values]
fake_struct: Program = Program.to((ACS_PH, NIL_PH))
graftroot_puzzle: Program = GRAFTROOT_MOD.curry(
graftroot_puzzle: Program = GRAFTROOT_DL_OFFERS.curry(
# Do everything twice to test depending on multiple singleton updates
p2_conditions,
[fake_struct, fake_struct],
Expand Down
20 changes: 10 additions & 10 deletions chia/_tests/wallet/vc_wallet/test_vc_lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
construct_exigent_metadata_layer,
create_covenant_layer,
create_did_tp,
create_revocation_layer,
create_std_parent_morpher,
create_viral_backdoor,
match_covenant_layer,
match_did_tp,
match_viral_backdoor,
match_revocation_layer,
solve_covenant_layer,
solve_did_tp,
solve_viral_backdoor,
solve_revocation_layer,
)
from chia.wallet.wallet_spend_bundle import WalletSpendBundle

Expand Down Expand Up @@ -311,13 +311,13 @@ async def test_did_tp(cost_logger: CostLogger) -> None:


@pytest.mark.anyio
async def test_viral_backdoor(cost_logger: CostLogger) -> None:
async def test_revocation_layer(cost_logger: CostLogger) -> None:
async with sim_and_client() as (sim, client):
# Setup and farm the puzzle
hidden_puzzle: Program = Program.to((1, [[61, 1]])) # assert a coin announcement that the solution tells us
hidden_puzzle_hash: bytes32 = hidden_puzzle.get_tree_hash()
p2_either_puzzle: Program = create_viral_backdoor(hidden_puzzle_hash, ACS_PH)
assert match_viral_backdoor(uncurry_puzzle(p2_either_puzzle)) == (hidden_puzzle_hash, ACS_PH)
p2_either_puzzle: Program = create_revocation_layer(hidden_puzzle_hash, ACS_PH)
assert match_revocation_layer(uncurry_puzzle(p2_either_puzzle)) == (hidden_puzzle_hash, ACS_PH)

await sim.farm_block(p2_either_puzzle.get_tree_hash())
p2_either_coin: Coin = (
Expand All @@ -333,7 +333,7 @@ async def test_viral_backdoor(cost_logger: CostLogger) -> None:
make_spend(
p2_either_coin,
p2_either_puzzle,
solve_viral_backdoor(
solve_revocation_layer(
ACS,
Program.to(None),
hidden=True,
Expand All @@ -352,7 +352,7 @@ async def test_viral_backdoor(cost_logger: CostLogger) -> None:
make_spend(
p2_either_coin,
p2_either_puzzle,
solve_viral_backdoor(
solve_revocation_layer(
hidden_puzzle,
Program.to(bytes32.zeros),
hidden=True,
Expand All @@ -366,7 +366,7 @@ async def test_viral_backdoor(cost_logger: CostLogger) -> None:

# Spend the inner puzzle
brick_hash: bytes32 = bytes32.zeros
wrapped_brick_hash: bytes32 = create_viral_backdoor(
wrapped_brick_hash: bytes32 = create_revocation_layer(
hidden_puzzle_hash,
brick_hash,
).get_tree_hash()
Expand All @@ -378,7 +378,7 @@ async def test_viral_backdoor(cost_logger: CostLogger) -> None:
make_spend(
p2_either_coin,
p2_either_puzzle,
solve_viral_backdoor(
solve_revocation_layer(
ACS,
Program.to([[51, brick_hash, 0]]),
),
Expand Down
10 changes: 4 additions & 6 deletions chia/wallet/nft_wallet/metadata_outer_puzzle.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@

from chia.types.blockchain_format.program import Program
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.wallet.nft_wallet.nft_puzzles import (
NFT_STATE_LAYER_MOD,
NFT_STATE_LAYER_MOD_HASH,
)
from chia.wallet.puzzle_drivers import PuzzleInfo, Solver
from chia.wallet.puzzles.load_clvm import load_clvm_maybe_recompile
from chia.wallet.uncurried_puzzle import UncurriedPuzzle, uncurry_puzzle

NFT_STATE_LAYER_MOD = load_clvm_maybe_recompile(
"nft_state_layer.clsp", package_or_requirement="chia.wallet.nft_wallet.puzzles"
)
NFT_STATE_LAYER_MOD_HASH = NFT_STATE_LAYER_MOD.get_tree_hash()


def match_metadata_layer_puzzle(puzzle: UncurriedPuzzle) -> tuple[bool, list[Program]]:
if puzzle.mod == NFT_STATE_LAYER_MOD:
Expand Down
10 changes: 3 additions & 7 deletions chia/wallet/nft_wallet/ownership_outer_puzzle.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,21 @@

from chia.types.blockchain_format.program import Program
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.wallet.nft_wallet.nft_puzzles import NFT_OWNERSHIP_LAYER
from chia.wallet.puzzle_drivers import PuzzleInfo, Solver
from chia.wallet.puzzles.load_clvm import load_clvm_maybe_recompile
from chia.wallet.uncurried_puzzle import UncurriedPuzzle, uncurry_puzzle

OWNERSHIP_LAYER_MOD = load_clvm_maybe_recompile(
"nft_ownership_layer.clsp", package_or_requirement="chia.wallet.nft_wallet.puzzles"
)


def match_ownership_layer_puzzle(puzzle: UncurriedPuzzle) -> tuple[bool, list[Program]]:
if puzzle.mod == OWNERSHIP_LAYER_MOD:
if puzzle.mod == NFT_OWNERSHIP_LAYER:
return True, list(puzzle.args.as_iter())
return False, []


def puzzle_for_ownership_layer(
current_owner: Union[Program, bytes], transfer_program: Program, inner_puzzle: Program
) -> Program:
return OWNERSHIP_LAYER_MOD.curry(OWNERSHIP_LAYER_MOD.get_tree_hash(), current_owner, transfer_program, inner_puzzle)
return NFT_OWNERSHIP_LAYER.curry(NFT_OWNERSHIP_LAYER.get_tree_hash(), current_owner, transfer_program, inner_puzzle)


def solution_for_ownership_layer(inner_solution: Program) -> Program:
Expand Down
11 changes: 3 additions & 8 deletions chia/wallet/nft_wallet/transfer_program_puzzle.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,21 @@
from chia.types.blockchain_format.program import Program
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.util.ints import uint16
from chia.wallet.nft_wallet.nft_puzzles import NFT_TRANSFER_PROGRAM_DEFAULT
from chia.wallet.puzzle_drivers import PuzzleInfo, Solver
from chia.wallet.puzzles.load_clvm import load_clvm_maybe_recompile
from chia.wallet.puzzles.singleton_top_layer_v1_1 import SINGLETON_LAUNCHER_HASH, SINGLETON_MOD_HASH
from chia.wallet.uncurried_puzzle import UncurriedPuzzle

TRANSFER_PROGRAM_MOD = load_clvm_maybe_recompile(
"nft_ownership_transfer_program_one_way_claim_with_royalties.clsp",
package_or_requirement="chia.wallet.nft_wallet.puzzles",
)


def match_transfer_program_puzzle(puzzle: UncurriedPuzzle) -> tuple[bool, list[Program]]:
if puzzle.mod == TRANSFER_PROGRAM_MOD:
if puzzle.mod == NFT_TRANSFER_PROGRAM_DEFAULT:
return True, list(puzzle.args.as_iter())
return False, []


def puzzle_for_transfer_program(launcher_id: bytes32, royalty_puzzle_hash: bytes32, percentage: uint16) -> Program:
singleton_struct = Program.to((SINGLETON_MOD_HASH, (launcher_id, SINGLETON_LAUNCHER_HASH)))
return TRANSFER_PROGRAM_MOD.curry(
return NFT_TRANSFER_PROGRAM_DEFAULT.curry(
singleton_struct,
royalty_puzzle_hash,
percentage,
Expand Down
31 changes: 14 additions & 17 deletions chia/wallet/vc_wallet/cr_cat_drivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
from enum import IntEnum
from typing import Optional, TypeVar

from chia_puzzles_py.programs import (
CONDITIONS_W_FEE_ANNOUNCE,
FLAG_PROOFS_CHECKER,
)
from chia_puzzles_py.programs import (
CREDENTIAL_RESTRICTION as CREDENTIAL_RESTRICTION_BYTES,
)
from chia_puzzles_py.programs import (
CREDENTIAL_RESTRICTION_HASH as CREDENTIAL_RESTRICTION_HASH_BYTES,
)
from clvm.casts import int_to_bytes

from chia.types.blockchain_format.coin import Coin, coin_as_list
Expand All @@ -19,7 +29,6 @@
from chia.wallet.conditions import AssertCoinAnnouncement
from chia.wallet.lineage_proof import LineageProof, LineageProofField
from chia.wallet.payment import Payment
from chia.wallet.puzzles.load_clvm import load_clvm_maybe_recompile
from chia.wallet.puzzles.singleton_top_layer_v1_1 import SINGLETON_LAUNCHER_HASH, SINGLETON_MOD_HASH
from chia.wallet.uncurried_puzzle import UncurriedPuzzle, uncurry_puzzle
from chia.wallet.util.curry_and_treehash import curry_and_treehash
Expand All @@ -35,22 +44,10 @@

# Mods

CREDENTIAL_RESTRICTION: Program = load_clvm_maybe_recompile(
"credential_restriction.clsp",
package_or_requirement="chia.wallet.vc_wallet.cr_puzzles",
include_standard_libraries=True,
)
CREDENTIAL_RESTRICTION_HASH: bytes32 = CREDENTIAL_RESTRICTION.get_tree_hash()
PROOF_FLAGS_CHECKER: Program = load_clvm_maybe_recompile(
"flag_proofs_checker.clsp",
package_or_requirement="chia.wallet.vc_wallet.cr_puzzles",
include_standard_libraries=True,
)
PENDING_VC_ANNOUNCEMENT: Program = load_clvm_maybe_recompile(
"conditions_w_fee_announce.clsp",
package_or_requirement="chia.wallet.vc_wallet.cr_puzzles",
include_standard_libraries=True,
)
CREDENTIAL_RESTRICTION: Program = Program.from_bytes(CREDENTIAL_RESTRICTION_BYTES)
CREDENTIAL_RESTRICTION_HASH: bytes32 = bytes32(CREDENTIAL_RESTRICTION_HASH_BYTES)
PROOF_FLAGS_CHECKER: Program = Program.from_bytes(FLAG_PROOFS_CHECKER)
PENDING_VC_ANNOUNCEMENT: Program = Program.from_bytes(CONDITIONS_W_FEE_ANNOUNCE)
CREDENTIAL_STRUCT: Program = Program.to(
(
(
Expand Down
82 changes: 32 additions & 50 deletions chia/wallet/vc_wallet/vc_drivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
from dataclasses import dataclass, replace
from typing import Optional, TypeVar

from chia_puzzles_py.programs import COVENANT_LAYER as COVENANT_LAYER_BYTES
from chia_puzzles_py.programs import EML_COVENANT_MORPHER as EML_COVENANT_MORPHER_BYTES
from chia_puzzles_py.programs import EML_TRANSFER_PROGRAM_COVENANT_ADAPTER as EML_TP_COVENANT_ADAPTER_BYTES
from chia_puzzles_py.programs import EML_UPDATE_METADATA_WITH_DID as EML_DID_TP_BYTES
from chia_puzzles_py.programs import EXIGENT_METADATA_LAYER as EXIGENT_METADATA_LAYER_BYTES
from chia_puzzles_py.programs import P2_ANNOUNCED_DELEGATED_PUZZLE as P2_ANNOUNCED_DELEGATED_PUZZLE_BYTES
from chia_puzzles_py.programs import REVOCATION_LAYER as REVOCATION_LAYER_BYTES
from chia_puzzles_py.programs import STD_PARENT_MORPHER as STD_PARENT_MORPHER_BYTES

from chia.types.blockchain_format.coin import Coin
from chia.types.blockchain_format.program import Program
from chia.types.blockchain_format.sized_bytes import bytes32
Expand All @@ -25,42 +34,15 @@
from chia.wallet.uncurried_puzzle import UncurriedPuzzle, uncurry_puzzle

# Mods
EXTIGENT_METADATA_LAYER = load_clvm_maybe_recompile(
"exigent_metadata_layer.clsp",
package_or_requirement="chia.wallet.vc_wallet.vc_puzzles",
include_standard_libraries=True,
)
P2_ANNOUNCED_DELEGATED_PUZZLE: Program = load_clvm_maybe_recompile(
"p2_announced_delegated_puzzle.clsp",
package_or_requirement="chia.wallet.vc_wallet.vc_puzzles",
include_standard_libraries=True,
)
COVENANT_LAYER: Program = load_clvm_maybe_recompile(
"covenant_layer.clsp", package_or_requirement="chia.wallet.vc_wallet.vc_puzzles", include_standard_libraries=True
)
STD_COVENANT_PARENT_MORPHER: Program = load_clvm_maybe_recompile(
"std_parent_morpher.clsp",
package_or_requirement="chia.wallet.vc_wallet.vc_puzzles",
include_standard_libraries=True,
)
EML_TP_COVENANT_ADAPTER: Program = load_clvm_maybe_recompile(
"eml_transfer_program_covenant_adapter.clsp",
package_or_requirement="chia.wallet.vc_wallet.vc_puzzles",
include_standard_libraries=True,
)
EML_DID_TP: Program = load_clvm_maybe_recompile(
"eml_update_metadata_with_DID.clsp",
package_or_requirement="chia.wallet.vc_wallet.vc_puzzles",
include_standard_libraries=True,
)
EXTIGENT_METADATA_LAYER_COVENANT_MORPHER: Program = load_clvm_maybe_recompile(
"eml_covenant_morpher.clsp",
package_or_requirement="chia.wallet.vc_wallet.vc_puzzles",
include_standard_libraries=True,
)
VIRAL_BACKDOOR: Program = load_clvm_maybe_recompile(
"viral_backdoor.clsp", package_or_requirement="chia.wallet.vc_wallet.vc_puzzles", include_standard_libraries=True
)
EXTIGENT_METADATA_LAYER = Program.from_bytes(EXIGENT_METADATA_LAYER_BYTES)
P2_ANNOUNCED_DELEGATED_PUZZLE: Program = Program.from_bytes(P2_ANNOUNCED_DELEGATED_PUZZLE_BYTES)
COVENANT_LAYER: Program = Program.from_bytes(COVENANT_LAYER_BYTES)
STD_COVENANT_PARENT_MORPHER: Program = Program.from_bytes(STD_PARENT_MORPHER_BYTES)
EML_TP_COVENANT_ADAPTER: Program = Program.from_bytes(EML_TP_COVENANT_ADAPTER_BYTES)
EML_DID_TP: Program = Program.from_bytes(EML_DID_TP_BYTES)
EXTIGENT_METADATA_LAYER_COVENANT_MORPHER: Program = Program.from_bytes(EML_COVENANT_MORPHER_BYTES)
REVOCATION_LAYER: Program = Program.from_bytes(REVOCATION_LAYER_BYTES)

# (mod (METADATA conditions . solution) (if solution solution (list METADATA () ())))
# (a (i 7 (q . 7) (q 4 2 (q () ()))) 1)
ACS_TRANSFER_PROGRAM: Program = Program.to([2, [3, 7, (1, 7), [1, 4, 2, [1, None, None]]], 1])
Expand All @@ -73,7 +55,7 @@
STD_COVENANT_PARENT_MORPHER_HASH: bytes32 = STD_COVENANT_PARENT_MORPHER.get_tree_hash()
EML_TP_COVENANT_ADAPTER_HASH: bytes32 = EML_TP_COVENANT_ADAPTER.get_tree_hash()
EXTIGENT_METADATA_LAYER_COVENANT_MORPHER_HASH: bytes32 = EXTIGENT_METADATA_LAYER_COVENANT_MORPHER.get_tree_hash()
VIRAL_BACKDOOR_HASH: bytes32 = VIRAL_BACKDOOR.get_tree_hash()
REVOCATION_LAYER_HASH: bytes32 = REVOCATION_LAYER.get_tree_hash()


# Standard brick puzzle uses the mods above
Expand All @@ -85,7 +67,7 @@
SINGLETON_MOD_HASH,
Program.to(SINGLETON_LAUNCHER_HASH).get_tree_hash(),
EXTIGENT_METADATA_LAYER_HASH,
VIRAL_BACKDOOR_HASH,
REVOCATION_LAYER_HASH,
ACS_TRANSFER_PROGRAM.get_tree_hash(),
)
STANDARD_BRICK_PUZZLE_HASH: bytes32 = STANDARD_BRICK_PUZZLE.get_tree_hash()
Expand Down Expand Up @@ -190,22 +172,22 @@ def solve_did_tp(
##############################
# P2 Puzzle or Hidden Puzzle #
##############################
def create_viral_backdoor(hidden_puzzle_hash: bytes32, inner_puzzle_hash: bytes32) -> Program:
return VIRAL_BACKDOOR.curry(
VIRAL_BACKDOOR_HASH,
def create_revocation_layer(hidden_puzzle_hash: bytes32, inner_puzzle_hash: bytes32) -> Program:
return REVOCATION_LAYER.curry(
REVOCATION_LAYER_HASH,
hidden_puzzle_hash,
inner_puzzle_hash,
)


def match_viral_backdoor(uncurried_puzzle: UncurriedPuzzle) -> Optional[tuple[bytes32, bytes32]]:
if uncurried_puzzle.mod == VIRAL_BACKDOOR:
def match_revocation_layer(uncurried_puzzle: UncurriedPuzzle) -> Optional[tuple[bytes32, bytes32]]:
if uncurried_puzzle.mod == REVOCATION_LAYER:
return bytes32(uncurried_puzzle.args.at("rf").as_atom()), bytes32(uncurried_puzzle.args.at("rrf").as_atom())
else:
return None # pragma: no cover


def solve_viral_backdoor(puzzle_reveal: Program, inner_solution: Program, hidden: bool = False) -> Program:
def solve_revocation_layer(puzzle_reveal: Program, inner_solution: Program, hidden: bool = False) -> Program:
solution: Program = Program.to(
[
hidden,
Expand Down Expand Up @@ -371,7 +353,7 @@ def launch(
inner_transfer_program,
)
)
wrapped_inner_puzzle_hash: bytes32 = create_viral_backdoor(
wrapped_inner_puzzle_hash: bytes32 = create_revocation_layer(
STANDARD_BRICK_PUZZLE_HASH,
new_inner_puzzle_hash,
).get_tree_hash()
Expand Down Expand Up @@ -485,7 +467,7 @@ def construct_transfer_program(self) -> Program:
)

def wrap_inner_with_backdoor(self) -> Program:
return create_viral_backdoor(
return create_revocation_layer(
self.hidden_puzzle().get_tree_hash(),
self.inner_puzzle_hash,
)
Expand Down Expand Up @@ -543,7 +525,7 @@ def is_vc(puzzle_reveal: UncurriedPuzzle) -> tuple[bool, str]:

# ...and layer below EML
layer_below_eml: UncurriedPuzzle = uncurry_puzzle(layer_below_singleton.args.at("rrrrf"))
if layer_below_eml.mod != VIRAL_BACKDOOR:
if layer_below_eml.mod != REVOCATION_LAYER:
return False, "VC did not have a provider backdoor" # pragma: no cover
hidden_puzzle_hash = bytes32(layer_below_eml.args.at("rf").as_atom())
if hidden_puzzle_hash != STANDARD_BRICK_PUZZLE_HASH:
Expand Down Expand Up @@ -616,7 +598,7 @@ def get_next_from_coin_spend(cls: type[_T_VerifiedCredential], parent_spend: Coi
parent_proof_hash: bytes32 = metadata_layer.args.at("rf").get_tree_hash()
eml_lineage_proof = VCLineageProof(
parent_name=parent_coin.parent_coin_info,
inner_puzzle_hash=create_viral_backdoor(
inner_puzzle_hash=create_revocation_layer(
STANDARD_BRICK_PUZZLE_HASH,
bytes32(uncurry_puzzle(metadata_layer.args.at("rrrrf")).args.at("rrf").as_atom()),
).get_tree_hash(),
Expand Down Expand Up @@ -720,7 +702,7 @@ def do_spend(
uint64(self.coin.amount),
Program.to(
[ # solve EML
solve_viral_backdoor(
solve_revocation_layer(
inner_puzzle,
inner_solution,
),
Expand Down Expand Up @@ -773,7 +755,7 @@ def activate_backdoor(
uint64(self.coin.amount),
Program.to(
[ # solve EML
solve_viral_backdoor(
solve_revocation_layer(
self.hidden_puzzle(),
solve_std_vc_backdoor(
self.launcher_id,
Expand Down

0 comments on commit 46032cf

Please sign in to comment.