2021-02-27 10:55:57 +03:00
|
|
|
from typing import Iterable, List, Tuple
|
|
|
|
from unittest import TestCase
|
|
|
|
|
|
|
|
from blspy import AugSchemeMPL, BasicSchemeMPL, G1Element, G2Element
|
|
|
|
|
2021-04-04 06:55:26 +03:00
|
|
|
from chia.types.blockchain_format.program import Program
|
|
|
|
from chia.types.blockchain_format.sized_bytes import bytes32
|
2021-07-13 02:31:27 +03:00
|
|
|
from chia.types.coin_spend import CoinSpend
|
2022-02-17 18:30:36 +03:00
|
|
|
from chia.types.condition_opcodes import ConditionOpcode
|
2021-04-04 06:55:26 +03:00
|
|
|
from chia.types.spend_bundle import SpendBundle
|
|
|
|
from chia.util.hash import std_hash
|
|
|
|
from chia.wallet.puzzles import (
|
2021-02-27 10:55:57 +03:00
|
|
|
p2_conditions,
|
|
|
|
p2_delegated_conditions,
|
|
|
|
p2_delegated_puzzle,
|
|
|
|
p2_delegated_puzzle_or_hidden_puzzle,
|
2021-03-10 05:27:27 +03:00
|
|
|
p2_m_of_n_delegate_direct,
|
|
|
|
p2_puzzle_hash,
|
2021-02-27 10:55:57 +03:00
|
|
|
)
|
|
|
|
from tests.util.key_tool import KeyTool
|
|
|
|
|
2021-03-04 11:54:03 +03:00
|
|
|
from ..core.make_block_generator import int_to_public_key
|
2021-03-10 05:27:27 +03:00
|
|
|
from .coin_store import CoinStore, CoinTimestamp
|
2021-02-27 10:55:57 +03:00
|
|
|
|
|
|
|
T1 = CoinTimestamp(1, 10000000)
|
|
|
|
T2 = CoinTimestamp(5, 10003000)
|
|
|
|
|
2021-04-30 20:22:11 +03:00
|
|
|
MAX_BLOCK_COST_CLVM = int(1e18)
|
2021-09-18 20:25:19 +03:00
|
|
|
COST_PER_BYTE = int(12000)
|
2021-04-30 20:22:11 +03:00
|
|
|
|
2021-02-27 10:55:57 +03:00
|
|
|
|
|
|
|
def secret_exponent_for_index(index: int) -> int:
|
|
|
|
blob = index.to_bytes(32, "big")
|
|
|
|
hashed_blob = BasicSchemeMPL.key_gen(std_hash(b"foo" + blob))
|
|
|
|
r = int.from_bytes(hashed_blob, "big")
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
|
|
|
def public_key_for_index(index: int, key_lookup: KeyTool) -> bytes:
|
|
|
|
secret_exponent = secret_exponent_for_index(index)
|
|
|
|
key_lookup.add_secret_exponents([secret_exponent])
|
2021-03-04 11:54:03 +03:00
|
|
|
return bytes(int_to_public_key(secret_exponent))
|
2021-02-27 10:55:57 +03:00
|
|
|
|
|
|
|
|
|
|
|
def throwaway_puzzle_hash(index: int, key_lookup: KeyTool) -> bytes32:
|
|
|
|
return p2_delegated_puzzle.puzzle_for_pk(public_key_for_index(index, key_lookup)).get_tree_hash()
|
|
|
|
|
|
|
|
|
|
|
|
def do_test_spend(
|
|
|
|
puzzle_reveal: Program,
|
|
|
|
solution: Program,
|
|
|
|
payments: Iterable[Tuple[bytes32, int]],
|
|
|
|
key_lookup: KeyTool,
|
|
|
|
farm_time: CoinTimestamp = T1,
|
|
|
|
spend_time: CoinTimestamp = T2,
|
|
|
|
) -> SpendBundle:
|
|
|
|
"""
|
|
|
|
This method will farm a coin paid to the hash of `puzzle_reveal`, then try to spend it
|
|
|
|
with `solution`, and verify that the created coins correspond to `payments`.
|
|
|
|
|
|
|
|
The `key_lookup` is used to create a signed version of the `SpendBundle`, although at
|
|
|
|
this time, signatures are not verified.
|
|
|
|
"""
|
|
|
|
|
|
|
|
coin_db = CoinStore()
|
|
|
|
|
|
|
|
puzzle_hash = puzzle_reveal.get_tree_hash()
|
|
|
|
|
|
|
|
# farm it
|
|
|
|
coin = coin_db.farm_coin(puzzle_hash, farm_time)
|
|
|
|
|
|
|
|
# spend it
|
2021-07-13 02:31:27 +03:00
|
|
|
coin_spend = CoinSpend(coin, puzzle_reveal, solution)
|
2021-02-27 10:55:57 +03:00
|
|
|
|
2021-07-13 02:31:27 +03:00
|
|
|
spend_bundle = SpendBundle([coin_spend], G2Element())
|
2021-09-18 20:25:19 +03:00
|
|
|
coin_db.update_coin_store_for_spend_bundle(spend_bundle, spend_time, MAX_BLOCK_COST_CLVM, COST_PER_BYTE)
|
2021-02-27 10:55:57 +03:00
|
|
|
|
|
|
|
# ensure all outputs are there
|
|
|
|
for puzzle_hash, amount in payments:
|
|
|
|
for coin in coin_db.coins_for_puzzle_hash(puzzle_hash):
|
|
|
|
if coin.amount == amount:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
assert 0
|
|
|
|
|
|
|
|
# make sure we can actually sign the solution
|
|
|
|
signatures = []
|
2021-07-13 02:31:27 +03:00
|
|
|
for coin_spend in spend_bundle.coin_spends:
|
|
|
|
signature = key_lookup.signature_for_solution(coin_spend, bytes([2] * 32))
|
2021-02-27 10:55:57 +03:00
|
|
|
signatures.append(signature)
|
2021-07-13 02:31:27 +03:00
|
|
|
return SpendBundle(spend_bundle.coin_spends, AugSchemeMPL.aggregate(signatures))
|
2021-02-27 10:55:57 +03:00
|
|
|
|
|
|
|
|
|
|
|
def default_payments_and_conditions(
|
|
|
|
initial_index: int, key_lookup: KeyTool
|
|
|
|
) -> Tuple[List[Tuple[bytes32, int]], Program]:
|
|
|
|
|
|
|
|
payments = [
|
|
|
|
(throwaway_puzzle_hash(initial_index + 1, key_lookup), initial_index * 1000),
|
|
|
|
(throwaway_puzzle_hash(initial_index + 2, key_lookup), (initial_index + 1) * 1000),
|
|
|
|
]
|
|
|
|
conditions = Program.to([make_create_coin_condition(ph, amount) for ph, amount in payments])
|
|
|
|
return payments, conditions
|
|
|
|
|
|
|
|
|
|
|
|
def make_create_coin_condition(puzzle_hash, amount):
|
|
|
|
return Program.to([ConditionOpcode.CREATE_COIN, puzzle_hash, amount])
|
|
|
|
|
|
|
|
|
|
|
|
class TestPuzzles(TestCase):
|
|
|
|
def test_p2_conditions(self):
|
|
|
|
key_lookup = KeyTool()
|
|
|
|
payments, conditions = default_payments_and_conditions(1, key_lookup)
|
|
|
|
|
|
|
|
puzzle = p2_conditions.puzzle_for_conditions(conditions)
|
|
|
|
solution = p2_conditions.solution_for_conditions(conditions)
|
|
|
|
|
|
|
|
do_test_spend(puzzle, solution, payments, key_lookup)
|
|
|
|
|
|
|
|
def test_p2_delegated_conditions(self):
|
|
|
|
key_lookup = KeyTool()
|
|
|
|
payments, conditions = default_payments_and_conditions(1, key_lookup)
|
|
|
|
|
|
|
|
pk = public_key_for_index(1, key_lookup)
|
|
|
|
|
|
|
|
puzzle = p2_delegated_conditions.puzzle_for_pk(pk)
|
|
|
|
solution = p2_delegated_conditions.solution_for_conditions(conditions)
|
|
|
|
|
|
|
|
do_test_spend(puzzle, solution, payments, key_lookup)
|
|
|
|
|
|
|
|
def test_p2_delegated_puzzle_simple(self):
|
|
|
|
key_lookup = KeyTool()
|
|
|
|
payments, conditions = default_payments_and_conditions(1, key_lookup)
|
|
|
|
|
|
|
|
pk = public_key_for_index(1, key_lookup)
|
|
|
|
|
|
|
|
puzzle = p2_delegated_puzzle.puzzle_for_pk(pk)
|
|
|
|
solution = p2_delegated_puzzle.solution_for_conditions(conditions)
|
|
|
|
|
|
|
|
do_test_spend(puzzle, solution, payments, key_lookup)
|
|
|
|
|
|
|
|
def test_p2_delegated_puzzle_graftroot(self):
|
|
|
|
key_lookup = KeyTool()
|
|
|
|
payments, conditions = default_payments_and_conditions(1, key_lookup)
|
|
|
|
|
|
|
|
delegated_puzzle = p2_delegated_conditions.puzzle_for_pk(public_key_for_index(8, key_lookup))
|
|
|
|
delegated_solution = p2_delegated_conditions.solution_for_conditions(conditions)
|
|
|
|
|
|
|
|
puzzle_program = p2_delegated_puzzle.puzzle_for_pk(public_key_for_index(1, key_lookup))
|
|
|
|
solution = p2_delegated_puzzle.solution_for_delegated_puzzle(delegated_puzzle, delegated_solution)
|
|
|
|
|
|
|
|
do_test_spend(puzzle_program, solution, payments, key_lookup)
|
|
|
|
|
|
|
|
def test_p2_puzzle_hash(self):
|
|
|
|
key_lookup = KeyTool()
|
|
|
|
payments, conditions = default_payments_and_conditions(1, key_lookup)
|
|
|
|
|
|
|
|
inner_puzzle = p2_delegated_conditions.puzzle_for_pk(public_key_for_index(4, key_lookup))
|
|
|
|
inner_solution = p2_delegated_conditions.solution_for_conditions(conditions)
|
|
|
|
inner_puzzle_hash = inner_puzzle.get_tree_hash()
|
|
|
|
|
|
|
|
puzzle_program = p2_puzzle_hash.puzzle_for_inner_puzzle_hash(inner_puzzle_hash)
|
|
|
|
assert puzzle_program == p2_puzzle_hash.puzzle_for_inner_puzzle(inner_puzzle)
|
|
|
|
solution = p2_puzzle_hash.solution_for_inner_puzzle_and_inner_solution(inner_puzzle, inner_solution)
|
|
|
|
|
|
|
|
do_test_spend(puzzle_program, solution, payments, key_lookup)
|
|
|
|
|
|
|
|
def test_p2_m_of_n_delegated_puzzle(self):
|
|
|
|
key_lookup = KeyTool()
|
|
|
|
payments, conditions = default_payments_and_conditions(1, key_lookup)
|
|
|
|
|
|
|
|
pks = [public_key_for_index(_, key_lookup) for _ in range(1, 6)]
|
|
|
|
M = 3
|
|
|
|
|
|
|
|
delegated_puzzle = p2_conditions.puzzle_for_conditions(conditions)
|
|
|
|
delegated_solution = []
|
|
|
|
|
|
|
|
puzzle_program = p2_m_of_n_delegate_direct.puzzle_for_m_of_public_key_list(M, pks)
|
|
|
|
selectors = [1, [], [], 1, 1]
|
|
|
|
solution = p2_m_of_n_delegate_direct.solution_for_delegated_puzzle(
|
|
|
|
M, selectors, delegated_puzzle, delegated_solution
|
|
|
|
)
|
|
|
|
|
|
|
|
do_test_spend(puzzle_program, solution, payments, key_lookup)
|
|
|
|
|
|
|
|
def test_p2_delegated_puzzle_or_hidden_puzzle_with_hidden_puzzle(self):
|
|
|
|
key_lookup = KeyTool()
|
|
|
|
payments, conditions = default_payments_and_conditions(1, key_lookup)
|
|
|
|
|
|
|
|
hidden_puzzle = p2_conditions.puzzle_for_conditions(conditions)
|
|
|
|
hidden_public_key = public_key_for_index(10, key_lookup)
|
|
|
|
|
|
|
|
puzzle = p2_delegated_puzzle_or_hidden_puzzle.puzzle_for_public_key_and_hidden_puzzle(
|
2022-07-22 19:05:35 +03:00
|
|
|
G1Element.from_bytes_unchecked(hidden_public_key), hidden_puzzle
|
2021-02-27 10:55:57 +03:00
|
|
|
)
|
|
|
|
solution = p2_delegated_puzzle_or_hidden_puzzle.solution_for_hidden_puzzle(
|
2022-07-22 19:05:35 +03:00
|
|
|
G1Element.from_bytes_unchecked(hidden_public_key), hidden_puzzle, Program.to(0)
|
2021-02-27 10:55:57 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
do_test_spend(puzzle, solution, payments, key_lookup)
|
|
|
|
|
|
|
|
def do_test_spend_p2_delegated_puzzle_or_hidden_puzzle_with_delegated_puzzle(self, hidden_pub_key_index):
|
|
|
|
key_lookup = KeyTool()
|
|
|
|
payments, conditions = default_payments_and_conditions(1, key_lookup)
|
|
|
|
|
|
|
|
hidden_puzzle = p2_conditions.puzzle_for_conditions(conditions)
|
|
|
|
hidden_public_key = public_key_for_index(hidden_pub_key_index, key_lookup)
|
2022-07-22 19:05:35 +03:00
|
|
|
hidden_pub_key_point = G1Element.from_bytes(hidden_public_key)
|
2021-02-27 10:55:57 +03:00
|
|
|
|
|
|
|
puzzle = p2_delegated_puzzle_or_hidden_puzzle.puzzle_for_public_key_and_hidden_puzzle(
|
2022-07-22 19:05:35 +03:00
|
|
|
hidden_pub_key_point, hidden_puzzle
|
2021-02-27 10:55:57 +03:00
|
|
|
)
|
|
|
|
payable_payments, payable_conditions = default_payments_and_conditions(5, key_lookup)
|
|
|
|
|
|
|
|
delegated_puzzle = p2_conditions.puzzle_for_conditions(payable_conditions)
|
|
|
|
delegated_solution = []
|
|
|
|
|
|
|
|
synthetic_public_key = p2_delegated_puzzle_or_hidden_puzzle.calculate_synthetic_public_key(
|
2022-07-22 19:05:35 +03:00
|
|
|
G1Element.from_bytes(hidden_public_key), hidden_puzzle.get_tree_hash()
|
2021-02-27 10:55:57 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
solution = p2_delegated_puzzle_or_hidden_puzzle.solution_for_delegated_puzzle(
|
|
|
|
delegated_puzzle, delegated_solution
|
|
|
|
)
|
|
|
|
|
|
|
|
hidden_puzzle_hash = hidden_puzzle.get_tree_hash()
|
|
|
|
synthetic_offset = p2_delegated_puzzle_or_hidden_puzzle.calculate_synthetic_offset(
|
2022-07-22 19:05:35 +03:00
|
|
|
hidden_pub_key_point, hidden_puzzle_hash
|
2021-02-27 10:55:57 +03:00
|
|
|
)
|
|
|
|
|
2021-03-04 11:54:03 +03:00
|
|
|
assert synthetic_public_key == int_to_public_key(synthetic_offset) + hidden_pub_key_point
|
2021-02-27 10:55:57 +03:00
|
|
|
|
|
|
|
secret_exponent = key_lookup.get(hidden_public_key)
|
2021-03-04 11:54:03 +03:00
|
|
|
assert int_to_public_key(secret_exponent) == hidden_pub_key_point
|
2021-02-27 10:55:57 +03:00
|
|
|
|
|
|
|
synthetic_secret_exponent = secret_exponent + synthetic_offset
|
|
|
|
key_lookup.add_secret_exponents([synthetic_secret_exponent])
|
|
|
|
|
|
|
|
do_test_spend(puzzle, solution, payable_payments, key_lookup)
|
|
|
|
|
|
|
|
def test_p2_delegated_puzzle_or_hidden_puzzle_with_delegated_puzzle(self):
|
|
|
|
for hidden_pub_key_index in range(1, 10):
|
|
|
|
self.do_test_spend_p2_delegated_puzzle_or_hidden_puzzle_with_delegated_puzzle(hidden_pub_key_index)
|