chia-blockchain/tests/clvm/test_puzzle_compression.py

109 lines
4.5 KiB
Python
Raw Normal View History

from blspy import G1Element, G2Element
from typing import Dict
from chia.types.blockchain_format.program import Program
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.types.blockchain_format.coin import Coin
from chia.types.spend_bundle import SpendBundle
from chia.types.coin_spend import CoinSpend
from chia.util.ints import uint64
from chia.wallet.trading.offer import OFFER_MOD
from chia.wallet.util.puzzle_compression import (
LATEST_VERSION,
lowest_best_version,
compress_object_with_puzzles,
decompress_object_with_puzzles,
)
from chia.wallet.cat_wallet.cat_utils import CAT_MOD, construct_cat_puzzle
from chia.wallet.puzzles.p2_delegated_puzzle_or_hidden_puzzle import puzzle_for_pk
ZERO_32 = bytes32([0] * 32)
ONE_32 = bytes32([17] * 32)
COIN = Coin(ZERO_32, ZERO_32, uint64(0))
SOLUTION = Program.to([])
class TestPuzzleCompression:
compression_factors: Dict[str, float] = {}
def test_standard_puzzle(self):
coin_spend = CoinSpend(
COIN,
puzzle_for_pk(G1Element()),
SOLUTION,
)
compressed = compress_object_with_puzzles(bytes(coin_spend), LATEST_VERSION)
assert len(bytes(coin_spend)) > len(compressed)
assert coin_spend == CoinSpend.from_bytes(decompress_object_with_puzzles(compressed))
self.compression_factors["standard_puzzle"] = len(bytes(compressed)) / len(bytes(coin_spend))
def test_cat_puzzle(self):
coin_spend = CoinSpend(
COIN,
construct_cat_puzzle(CAT_MOD, Program.to([]).get_tree_hash(), Program.to(1)),
SOLUTION,
)
compressed = compress_object_with_puzzles(bytes(coin_spend), LATEST_VERSION)
assert len(bytes(coin_spend)) > len(compressed)
assert coin_spend == CoinSpend.from_bytes(decompress_object_with_puzzles(compressed))
self.compression_factors["cat_puzzle"] = len(bytes(compressed)) / len(bytes(coin_spend))
def test_offer_puzzle(self):
coin_spend = CoinSpend(
COIN,
OFFER_MOD,
SOLUTION,
)
compressed = compress_object_with_puzzles(bytes(coin_spend), LATEST_VERSION)
assert len(bytes(coin_spend)) > len(compressed)
assert coin_spend == CoinSpend.from_bytes(decompress_object_with_puzzles(compressed))
self.compression_factors["offer_puzzle"] = len(bytes(compressed)) / len(bytes(coin_spend))
def test_nesting_puzzles(self):
coin_spend = CoinSpend(
COIN,
construct_cat_puzzle(CAT_MOD, Program.to([]).get_tree_hash(), puzzle_for_pk(G1Element())),
SOLUTION,
)
compressed = compress_object_with_puzzles(bytes(coin_spend), LATEST_VERSION)
assert len(bytes(coin_spend)) > len(compressed)
assert coin_spend == CoinSpend.from_bytes(decompress_object_with_puzzles(compressed))
self.compression_factors["cat_w_standard_puzzle"] = len(bytes(compressed)) / len(bytes(coin_spend))
def test_unknown_wrapper(self):
unknown = Program.to([2, 2, []]) # (a 2 ())
coin_spend = CoinSpend(
COIN,
unknown.curry(puzzle_for_pk(G1Element())),
SOLUTION,
)
compressed = compress_object_with_puzzles(bytes(coin_spend), LATEST_VERSION)
assert len(bytes(coin_spend)) > len(compressed)
assert coin_spend == CoinSpend.from_bytes(decompress_object_with_puzzles(compressed))
self.compression_factors["unknown_and_standard"] = len(bytes(compressed)) / len(bytes(coin_spend))
def test_lowest_best_version(self):
catchup: into main from long_lived/post_1.5.0 @ 916ccee549b1594349c19fcbb0d0d210698c899b (#12629) * updated wallet name * deprecated series * swap to cat2 * updating .gitmodule to point to defender-gui * Remove break (its preventing other sockets from getting data when earlier ones have an error) (#12241) * Convert DID Wallet to use the new coin selection algorithm that the normal wallet and the CAT wallet already use (#12063) * small type change * use coin_selection.py with DID Wallet use more efficient coin selection methods. * Add special DID edgecase + fix int type * Ms.fix coin selection (#12261) * Fix coin selection bug * Fix properly * Fallback in cases of too many coins selected * Also check for num coins * Lint issues. * Add another test * No sorting, and faster knapsack * Lint fix * Remove comment and useless check * Lint line * Tx submission idempotance, and prioritize wallet (#12282) * Tx submission idempotance, and prioritize wallet * TODO comment * Updating gui modules * extend min_coin to rpc calls & cli for coin selection (#12274) * add tests to test if min_coin is working they are passing, but no harm in being safe * expand min coin amount across wallet.py * extend to trade_manager * add new options to rpc's almost done lol. * add min_coin_amount to wallet send * make param non optional alleviate None errors * add cat wallet changes, rpc and all + fix a bug i accidentally made oops * Fix offer compression backwards compatibility * bumping gui pin to head of release/1.5.0 * Calculate NFT royalty amount * Create NFT wallet after the DID created (#12175) * Bumping gui * Show total amount to be paid for NFT offers * Fix for NFT0 and NFT+Royalty detection suggested by quex * Linter fix and formatting change * Add RPCs for getting/extending the current derivation path index (#12472) * Sleep to allow neworking layer to execute (#12463) * Sleep to allow neworking layer to execute * Add comment * Added param to indicate how many additional phs create_more_puzzle_hashes should create. (#12493) Account for range() not including last_index when `up_to_index` is provided. * Fixed the wallet db rename from v2/v1 to v2_r1. Removed vestigial code for dealing with the lite wallet db now that we're syncing v2_r1 from scratch. * When extending the derivation index, make sure we don't mark previously (#12513) unused indices as used. This helps minimize gaps in the address space. * Updating SBX asset ID * Adding 1.5.0 changelog (#56) * Adding 1.5.0 changelog * Adding CVE fix * Updating gitmodules (#57) * Updating gitmodules * Pinning gui * black fixes * mypy fixes * xfail some run_block tests that need CAT2 update * fix test * Fix test based on series<->edition changes * Drop EOL impish and hirsute (#12559) (cherry picked from commit 189790ced27c5dfc71df5f0035252c653f085152) * Expand select_coins rpc (#12360) * change type to uint64, 128 is too big anyway * add new options to select_coins endpoint * oops * add tests and finalize * oops (cherry picked from commit d5bf4d8b59e3f0757f046ff6c9b7b2fd878920b4) * .resolve() for wallet db path tests in windows * followup to actually fix the wallet db path tests * Update .gitmodules * Update wallet_node.py * Update trade_manager.py Co-authored-by: Sebastjan <trepca@gmail.com> Co-authored-by: matt <matt@chia.net> Co-authored-by: William Allen <wallentx@users.noreply.github.com> Co-authored-by: wallentx <william.allentx@gmail.com> Co-authored-by: Chris Marslender <chrismarslender@gmail.com> Co-authored-by: Jack Nelson <jack@jacknelson.xyz> Co-authored-by: Mariano Sorgente <3069354+mariano54@users.noreply.github.com> Co-authored-by: Matt Hauff <quexington@gmail.com> Co-authored-by: Jeff Cruikshank <jeff@chia.net> Co-authored-by: Kronus91 <t.yu@chia.net> Co-authored-by: Justin England <justin@chia.net> Co-authored-by: Earle Lowe <e.lowe@chia.net> Co-authored-by: Earle Lowe <30607889+emlowe@users.noreply.github.com>
2022-07-30 00:58:21 +03:00
assert lowest_best_version([bytes(CAT_MOD)]) == 4
assert lowest_best_version([bytes(OFFER_MOD)]) == 2
def test_version_override(self):
coin_spend = CoinSpend(
COIN,
OFFER_MOD,
SOLUTION,
)
spend_bundle = SpendBundle([coin_spend], G2Element())
compressed = compress_object_with_puzzles(bytes(spend_bundle), LATEST_VERSION)
compressed_earlier = compress_object_with_puzzles(bytes(spend_bundle), 1)
assert len(bytes(spend_bundle)) > len(bytes(compressed))
assert spend_bundle == SpendBundle.from_bytes(decompress_object_with_puzzles(compressed))
assert spend_bundle == SpendBundle.from_bytes(decompress_object_with_puzzles(compressed_earlier))
assert len(bytes(compressed_earlier)) > len(bytes(compressed))
def test_compression_factors(self):
import json
import logging
log = logging.getLogger(__name__)
log.warning(json.dumps(self.compression_factors))