Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidn committed Feb 5, 2025
1 parent dc74493 commit f225f82
Showing 1 changed file with 73 additions and 11 deletions.
84 changes: 73 additions & 11 deletions chia/_tests/core/mempool/test_mempool.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Callable, Optional

import pytest
from chia_rs import G1Element, G2Element, get_flags_for_height_and_constants
from chia_rs import AugSchemeMPL, G1Element, G2Element, get_flags_for_height_and_constants, run_block_generator2
from clvm.casts import int_to_bytes
from clvm_tools import binutils
from clvm_tools.binutils import assemble
Expand Down Expand Up @@ -3093,21 +3093,22 @@ def test_get_items_by_coin_ids(items: list[MempoolItem], coin_ids: list[bytes32]
assert set(result) == set(expected)


def make_test_spendbundle(coin: Coin, *, fee: int = 0, with_higher_cost: bool = False) -> SpendBundle:
conditions = []
actual_fee = fee
if with_higher_cost:
conditions.extend([[ConditionOpcode.CREATE_COIN, IDENTITY_PUZZLE_HASH, i] for i in range(3)])
actual_fee += 3
conditions.append([ConditionOpcode.CREATE_COIN, IDENTITY_PUZZLE_HASH, coin.amount - actual_fee])
sb = spend_bundle_from_conditions(conditions, coin)
return sb


@pytest.mark.anyio
async def test_aggregating_on_a_solution_then_a_more_cost_saving_one_appears() -> None:
async def get_unspent_lineage_info_for_puzzle_hash(_: bytes32) -> Optional[UnspentLineageInfo]:
assert False # pragma: no cover

def make_test_spendbundle(coin: Coin, *, fee: int = 0, with_higher_cost: bool = False) -> SpendBundle:
conditions = []
actual_fee = fee
if with_higher_cost:
conditions.extend([[ConditionOpcode.CREATE_COIN, IDENTITY_PUZZLE_HASH, i] for i in range(3)])
actual_fee += 3
conditions.append([ConditionOpcode.CREATE_COIN, IDENTITY_PUZZLE_HASH, coin.amount - actual_fee])
sb = spend_bundle_from_conditions(conditions, coin)
return sb

def agg_and_add_sb_returning_cost_info(mempool: Mempool, spend_bundles: list[SpendBundle]) -> uint64:
sb = SpendBundle.aggregate(spend_bundles)
mi = mempool_item_from_spendbundle(sb)
Expand Down Expand Up @@ -3178,6 +3179,67 @@ def test_get_puzzle_and_solution_for_coin_failure() -> None:
get_puzzle_and_solution_for_coin(BlockGenerator(SerializedProgram.to(None), []), TEST_COIN, 0, test_constants)


@pytest.mark.anyio
async def test_create_block_generator() -> None:
async def get_unspent_lineage_info_for_puzzle_hash(_: bytes32) -> Optional[UnspentLineageInfo]:
assert False # pragma: no cover

mempool_info = MempoolInfo(
CLVMCost(uint64(11000000000 * 3)),
FeeRate(uint64(1000000)),
CLVMCost(uint64(11000000000)),
)

fee_estimator = create_bitcoin_fee_estimator(test_constants.MAX_BLOCK_COST_CLVM)
mempool = Mempool(mempool_info, fee_estimator)
coins = [
Coin(IDENTITY_PUZZLE_HASH, IDENTITY_PUZZLE_HASH, uint64(amount)) for amount in range(2000000000, 2000000020, 2)
]

spend_bundles = set(make_test_spendbundle(c) for c in coins)
expected_additions = set(Coin(c.name(), IDENTITY_PUZZLE_HASH, c.amount) for c in coins)
expected_signature = AugSchemeMPL.aggregate([sb.aggregated_signature for sb in spend_bundles])

for sb in spend_bundles:
mi = mempool_item_from_spendbundle(sb)
mempool.add_to_pool(mi)
invariant_check_mempool(mempool)

generator, signature, additions = await mempool.create_block_generator(
get_unspent_lineage_info_for_puzzle_hash, test_constants, uint32(0)
)

assert set(additions) == expected_additions

assert len(additions) == len(expected_additions)
assert signature == expected_signature

err, conds = run_block_generator2(
bytes(generator.program),
generator.generator_refs,
test_constants.MAX_BLOCK_COST_CLVM,
0,
signature,
None,
test_constants,
)

assert err is None
assert conds is not None

assert len(conds.spends) == len(coins)

num_additions = 0
for spend in conds.spends:
assert Coin(spend.parent_id, spend.puzzle_hash, uint64(spend.coin_amount)) in coins
for add2 in spend.create_coin:
assert Coin(spend.coin_id, add2[0], uint64(add2[1])) in expected_additions
num_additions += 1

assert num_additions == len(additions)
invariant_check_mempool(mempool)


# TODO: import this from chia_rs once we bump the version we depend on
ENABLE_KECCAK = 0x200
ENABLE_KECCAK_OPS_OUTSIDE_GUARD = 0x100
Expand Down

0 comments on commit f225f82

Please sign in to comment.