Skip to content

Commit 33e79e3

Browse files
authored
CHIA-1485 Simplify calling pre_validate_spendbundle and avoid spend vs spend bundle confusion (#18639)
Simplify calling pre_validate_spendbundle and avoid spend vs spend bundle confusion.
1 parent 8552e35 commit 33e79e3

File tree

3 files changed

+21
-24
lines changed

3 files changed

+21
-24
lines changed

chia/_tests/core/mempool/test_mempool_manager.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ async def test_empty_spend_bundle() -> None:
458458
mempool_manager = await instantiate_mempool_manager(zero_calls_get_coin_records)
459459
sb = SpendBundle([], G2Element())
460460
with pytest.raises(ValidationError, match="INVALID_SPEND_BUNDLE"):
461-
await mempool_manager.pre_validate_spendbundle(sb, sb.name())
461+
await mempool_manager.pre_validate_spendbundle(sb)
462462

463463

464464
@pytest.mark.anyio
@@ -467,7 +467,7 @@ async def test_negative_addition_amount() -> None:
467467
conditions = [[ConditionOpcode.CREATE_COIN, IDENTITY_PUZZLE_HASH, -1]]
468468
sb = spend_bundle_from_conditions(conditions)
469469
with pytest.raises(ValidationError, match="COIN_AMOUNT_NEGATIVE"):
470-
await mempool_manager.pre_validate_spendbundle(sb, sb.name())
470+
await mempool_manager.pre_validate_spendbundle(sb)
471471

472472

473473
@pytest.mark.anyio
@@ -478,7 +478,7 @@ async def test_valid_addition_amount() -> None:
478478
coin = Coin(IDENTITY_PUZZLE_HASH, IDENTITY_PUZZLE_HASH, max_amount)
479479
sb = spend_bundle_from_conditions(conditions, coin)
480480
# ensure this does not throw
481-
_ = await mempool_manager.pre_validate_spendbundle(sb, sb.name())
481+
_ = await mempool_manager.pre_validate_spendbundle(sb)
482482

483483

484484
@pytest.mark.anyio
@@ -488,7 +488,7 @@ async def test_too_big_addition_amount() -> None:
488488
conditions = [[ConditionOpcode.CREATE_COIN, IDENTITY_PUZZLE_HASH, max_amount + 1]]
489489
sb = spend_bundle_from_conditions(conditions)
490490
with pytest.raises(ValidationError, match="COIN_AMOUNT_EXCEEDS_MAXIMUM"):
491-
await mempool_manager.pre_validate_spendbundle(sb, sb.name())
491+
await mempool_manager.pre_validate_spendbundle(sb)
492492

493493

494494
@pytest.mark.anyio
@@ -500,7 +500,7 @@ async def test_duplicate_output() -> None:
500500
]
501501
sb = spend_bundle_from_conditions(conditions)
502502
with pytest.raises(ValidationError, match="DUPLICATE_OUTPUT"):
503-
await mempool_manager.pre_validate_spendbundle(sb, sb.name())
503+
await mempool_manager.pre_validate_spendbundle(sb)
504504

505505

506506
@pytest.mark.anyio
@@ -511,41 +511,41 @@ async def test_block_cost_exceeds_max() -> None:
511511
conditions.append([ConditionOpcode.CREATE_COIN, IDENTITY_PUZZLE_HASH, i])
512512
sb = spend_bundle_from_conditions(conditions)
513513
with pytest.raises(ValidationError, match="BLOCK_COST_EXCEEDS_MAX"):
514-
await mempool_manager.pre_validate_spendbundle(sb, sb.name())
514+
await mempool_manager.pre_validate_spendbundle(sb)
515515

516516

517517
@pytest.mark.anyio
518518
async def test_double_spend_prevalidation() -> None:
519519
mempool_manager = await instantiate_mempool_manager(zero_calls_get_coin_records)
520520
conditions = [[ConditionOpcode.CREATE_COIN, IDENTITY_PUZZLE_HASH, 1]]
521521
sb = spend_bundle_from_conditions(conditions)
522-
sb_twice: SpendBundle = SpendBundle.aggregate([sb, sb])
522+
sb_twice = SpendBundle.aggregate([sb, sb])
523523
with pytest.raises(ValidationError, match="DOUBLE_SPEND"):
524-
await mempool_manager.pre_validate_spendbundle(sb_twice, sb_twice.name())
524+
await mempool_manager.pre_validate_spendbundle(sb_twice)
525525

526526

527527
@pytest.mark.anyio
528528
async def test_minting_coin() -> None:
529529
mempool_manager = await instantiate_mempool_manager(zero_calls_get_coin_records)
530530
conditions = [[ConditionOpcode.CREATE_COIN, IDENTITY_PUZZLE_HASH, TEST_COIN_AMOUNT]]
531531
sb = spend_bundle_from_conditions(conditions)
532-
_ = await mempool_manager.pre_validate_spendbundle(sb, sb.name())
532+
_ = await mempool_manager.pre_validate_spendbundle(sb)
533533
conditions = [[ConditionOpcode.CREATE_COIN, IDENTITY_PUZZLE_HASH, TEST_COIN_AMOUNT + 1]]
534534
sb = spend_bundle_from_conditions(conditions)
535535
with pytest.raises(ValidationError, match="MINTING_COIN"):
536-
await mempool_manager.pre_validate_spendbundle(sb, sb.name())
536+
await mempool_manager.pre_validate_spendbundle(sb)
537537

538538

539539
@pytest.mark.anyio
540540
async def test_reserve_fee_condition() -> None:
541541
mempool_manager = await instantiate_mempool_manager(zero_calls_get_coin_records)
542542
conditions = [[ConditionOpcode.RESERVE_FEE, TEST_COIN_AMOUNT]]
543543
sb = spend_bundle_from_conditions(conditions)
544-
_ = await mempool_manager.pre_validate_spendbundle(sb, sb.name())
544+
_ = await mempool_manager.pre_validate_spendbundle(sb)
545545
conditions = [[ConditionOpcode.RESERVE_FEE, TEST_COIN_AMOUNT + 1]]
546546
sb = spend_bundle_from_conditions(conditions)
547547
with pytest.raises(ValidationError, match="RESERVE_FEE_CONDITION_FAILED"):
548-
await mempool_manager.pre_validate_spendbundle(sb, sb.name())
548+
await mempool_manager.pre_validate_spendbundle(sb)
549549

550550

551551
@pytest.mark.anyio

chia/full_node/mempool_manager.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -264,17 +264,14 @@ def remove_seen(self, bundle_hash: bytes32) -> None:
264264
self.seen_bundle_hashes.pop(bundle_hash)
265265

266266
async def pre_validate_spendbundle(
267-
self,
268-
new_spend: SpendBundle,
269-
spend_name: bytes32,
270-
bls_cache: Optional[BLSCache] = None,
267+
self, spend_bundle: SpendBundle, spend_bundle_id: Optional[bytes32] = None, bls_cache: Optional[BLSCache] = None
271268
) -> SpendBundleConditions:
272269
"""
273270
Errors are included within the cached_result.
274271
This runs in another process so we don't block the main thread
275272
"""
276273

277-
if new_spend.coin_spends == []:
274+
if spend_bundle.coin_spends == []:
278275
raise ValidationError(Err.INVALID_SPEND_BUNDLE, "Empty SpendBundle")
279276

280277
assert self.peak is not None
@@ -284,7 +281,7 @@ async def pre_validate_spendbundle(
284281
sbc, new_cache_entries, duration = await asyncio.get_running_loop().run_in_executor(
285282
self.pool,
286283
validate_clvm_and_signature,
287-
new_spend,
284+
spend_bundle,
288285
self.max_tx_clvm_cost,
289286
self.constants,
290287
self.peak.height,
@@ -305,10 +302,13 @@ async def pre_validate_spendbundle(
305302

306303
ret = NPCResult(None, sbc)
307304

305+
if spend_bundle_id is None:
306+
spend_bundle_id = spend_bundle.name()
307+
308308
log.log(
309309
logging.DEBUG if duration < 2 else logging.WARNING,
310310
f"pre_validate_spendbundle took {duration:0.4f} seconds "
311-
f"for {spend_name} (queue-size: {self._worker_queue_size})",
311+
f"for {spend_bundle_id} (queue-size: {self._worker_queue_size})",
312312
)
313313
if ret.error is not None:
314314
raise ValidationError(Err(ret.error), "pre_validate_spendbundle failed")

chia/rpc/full_node_rpc_api.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -859,11 +859,8 @@ async def _validate_fee_estimate_cost(self, request: Dict[str, Any]) -> uint64:
859859
raise ValueError(f"Request must contain exactly one of {ns}")
860860

861861
if "spend_bundle" in request:
862-
spend_bundle: SpendBundle = SpendBundle.from_json_dict(request["spend_bundle"])
863-
spend_name = spend_bundle.name()
864-
conds: SpendBundleConditions = await self.service.mempool_manager.pre_validate_spendbundle(
865-
spend_bundle, spend_name
866-
)
862+
spend_bundle = SpendBundle.from_json_dict(request["spend_bundle"])
863+
conds: SpendBundleConditions = await self.service.mempool_manager.pre_validate_spendbundle(spend_bundle)
867864
cost = conds.cost
868865
elif "cost" in request:
869866
cost = request["cost"]

0 commit comments

Comments
 (0)