Skip to content

Commit 6df066f

Browse files
authored
fix incorrect assert in create_block_generator2() (#20015)
1 parent 1600703 commit 6df066f

File tree

3 files changed

+97
-4
lines changed

3 files changed

+97
-4
lines changed

chia/_tests/core/mempool/test_mempool.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3213,6 +3213,97 @@ def test_get_puzzle_and_solution_for_coin_failure() -> None:
32133213
raise ValueError(f"Failed to get puzzle and solution for coin {TEST_COIN}, error: {e}") from e
32143214

32153215

3216+
# this puzzle just creates coins, however many are requested by the solution
3217+
# (mod (A)
3218+
# (defun loop (n)
3219+
# (if (= n 1)
3220+
# (list)
3221+
# (c (list 51 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff n) (loop (- n 1))))
3222+
# )
3223+
# (loop A)
3224+
# )
3225+
create_coins_loop: str = (
3226+
"ff02ffff01ff02ff02ffff04ff02ffff04ff05ff80808080ffff04ffff01ff02"
3227+
"ffff03ffff09ff05ffff010180ff80ffff01ff04ffff04ffff0133ffff04ffff"
3228+
"01a0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
3229+
"ffffffff04ff05ff80808080ffff02ff02ffff04ff02ffff04ffff11ff05ffff"
3230+
"010180ff808080808080ff0180ff018080"
3231+
)
3232+
3233+
3234+
# this test uses artificial puzzles just to exercise the block creation. These
3235+
# spends are expected not to verify any signatures
3236+
# This is to keep the test simple.
3237+
@pytest.mark.parametrize(
3238+
"puzzle, solution",
3239+
[
3240+
# create 2000 coins
3241+
(create_coins_loop, "ff8207d180"),
3242+
# create 1000 coins
3243+
(create_coins_loop, "ff8203e980"),
3244+
# create 500 coins
3245+
(create_coins_loop, "ff8201f580"),
3246+
],
3247+
)
3248+
@pytest.mark.parametrize("old", [True, False])
3249+
def test_create_block_generator_custom_spend(puzzle: str, solution: str, old: bool) -> None:
3250+
mempool_info = MempoolInfo(
3251+
CLVMCost(uint64(11000000000 * 3)),
3252+
FeeRate(uint64(1000000)),
3253+
CLVMCost(uint64(11000000000)),
3254+
)
3255+
3256+
fee_estimator = create_bitcoin_fee_estimator(test_constants.MAX_BLOCK_COST_CLVM)
3257+
solution_str = SerializedProgram.fromhex(solution)
3258+
puzzle_reveal = SerializedProgram.fromhex(puzzle)
3259+
puzzle_hash = puzzle_reveal.get_tree_hash()
3260+
mempool = Mempool(mempool_info, fee_estimator)
3261+
coins = [Coin(bytes32.random(), puzzle_hash, uint64(amount)) for amount in range(100000000, 100000022)]
3262+
3263+
spend_bundles = [
3264+
SpendBundle(
3265+
coin_spends=[CoinSpend(coin, puzzle_reveal=puzzle_reveal, solution=solution_str)],
3266+
aggregated_signature=G2Element(),
3267+
)
3268+
for coin in coins
3269+
]
3270+
3271+
for sb in spend_bundles:
3272+
mi = mempool_item_from_spendbundle(sb)
3273+
mempool.add_to_pool(mi)
3274+
invariant_check_mempool(mempool)
3275+
3276+
create_block = mempool.create_block_generator if old else mempool.create_block_generator2
3277+
generator = create_block(test_constants, test_constants.HARD_FORK2_HEIGHT, 10.0)
3278+
assert generator is not None
3279+
3280+
assert generator.signature == G2Element()
3281+
3282+
removals = set(generator.removals)
3283+
3284+
err, conds = run_block_generator2(
3285+
bytes(generator.program),
3286+
generator.generator_refs,
3287+
test_constants.MAX_BLOCK_COST_CLVM,
3288+
0,
3289+
generator.signature,
3290+
None,
3291+
test_constants,
3292+
)
3293+
3294+
assert err is None
3295+
assert conds is not None
3296+
3297+
assert len(conds.spends) == len(removals)
3298+
3299+
for spend in conds.spends:
3300+
removal = Coin(spend.parent_id, spend.puzzle_hash, uint64(spend.coin_amount))
3301+
assert removal in coins
3302+
assert removal in removals
3303+
3304+
invariant_check_mempool(mempool)
3305+
3306+
32163307
@pytest.mark.parametrize("old", [True, False])
32173308
def test_create_block_generator(old: bool) -> None:
32183309
mempool = construct_mempool()

chia/_tests/core/mempool/test_mempool_manager.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,9 @@ def make_bundle_spends_map_and_fee(
590590

591591

592592
def mempool_item_from_spendbundle(spend_bundle: SpendBundle) -> MempoolItem:
593-
conds = get_conditions_from_spendbundle(spend_bundle, INFINITE_COST, DEFAULT_CONSTANTS, uint32(0))
593+
conds = get_conditions_from_spendbundle(
594+
spend_bundle, INFINITE_COST, DEFAULT_CONSTANTS, DEFAULT_CONSTANTS.HARD_FORK2_HEIGHT
595+
)
594596
bundle_coin_spends, fee = make_bundle_spends_map_and_fee(spend_bundle, conds)
595597
return MempoolItem(
596598
spend_bundle=spend_bundle,

chia/full_node/mempool.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,7 @@ def create_block_generator2(
750750
f"cost: {batch_cost} total cost: {block_cost}"
751751
)
752752
else:
753+
log.info(f"Skipping transaction batch cumulative cost: {block_cost} batch cost: {batch_cost}")
753754
skipped_items += 1
754755

755756
batch_cost = 0
@@ -775,12 +776,12 @@ def create_block_generator2(
775776

776777
if len(batch_transactions) > 0:
777778
added, _ = builder.add_spend_bundles(batch_transactions, uint64(batch_cost), constants)
779+
log.info(f"trying to add residual batch: {len(batch_transactions)} batch cost: {batch_cost} added: {added}")
778780

779781
if added:
780782
added_spends += batch_spends
781783
additions.extend(batch_additions)
782784
removals.extend([cs.coin for sb in batch_transactions for cs in sb.coin_spends])
783-
block_cost = builder.cost()
784785
log.info(
785786
f"adding TX batch, additions: {len(batch_additions)} removals: {batch_spends} "
786787
f"cost: {batch_cost} total cost: {block_cost}"
@@ -797,7 +798,6 @@ def create_block_generator2(
797798
f"create_block_generator2() took {duration:0.4f} seconds. "
798799
f"block cost: {cost} spends: {added_spends} additions: {len(additions)}",
799800
)
800-
assert block_cost == cost
801801

802802
return NewBlockGenerator(
803803
SerializedProgram.from_bytes(block_program),
@@ -806,5 +806,5 @@ def create_block_generator2(
806806
signature,
807807
additions,
808808
removals,
809-
uint64(block_cost),
809+
uint64(cost),
810810
)

0 commit comments

Comments
 (0)