Skip to content

Commit e1064fa

Browse files
authored
Simplify creating a spend bundle from mempool items and move this logic to the mempool (#14934)
Simplify creating a spend bundle from mempool items and move this logic to the mempool.
1 parent a0659e9 commit e1064fa

File tree

2 files changed

+44
-38
lines changed

2 files changed

+44
-38
lines changed

chia/full_node/mempool.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
from __future__ import annotations
22

3+
import logging
34
import sqlite3
45
from dataclasses import dataclass
56
from datetime import datetime
67
from enum import Enum
7-
from typing import Dict, Iterator, List, Optional
8+
from typing import Callable, Dict, Iterator, List, Optional, Tuple
9+
10+
from chia_rs import Coin
811

912
from chia.consensus.cost_calculator import NPCResult
13+
from chia.consensus.default_constants import DEFAULT_CONSTANTS
1014
from chia.full_node.fee_estimation import FeeMempoolInfo, MempoolInfo, MempoolItemInfo
1115
from chia.full_node.fee_estimator_interface import FeeEstimatorInterface
1216
from chia.types.blockchain_format.sized_bytes import bytes32
@@ -17,6 +21,8 @@
1721
from chia.util.db_wrapper import SQLITE_MAX_VARIABLE_NUMBER
1822
from chia.util.ints import uint32, uint64
1923

24+
log = logging.getLogger(__name__)
25+
2026
# We impose a limit on the fee a single transaction can pay in order to have the
2127
# sum of all fees in the mempool be less than 2^63. That's the limit of sqlite's
2228
# integers, which we rely on for computing fee per cost as well as the fee sum
@@ -312,3 +318,39 @@ def at_full_capacity(self, cost: int) -> bool:
312318
"""
313319

314320
return self.total_mempool_cost() + cost > self.mempool_info.max_size_in_cost
321+
322+
def create_bundle_from_mempool_items(
323+
self, item_inclusion_filter: Callable[[bytes32], bool]
324+
) -> Optional[Tuple[SpendBundle, List[Coin], List[Coin]]]:
325+
cost_sum = 0 # Checks that total cost does not exceed block maximum
326+
fee_sum = 0 # Checks that total fees don't exceed 64 bits
327+
spend_bundles: List[SpendBundle] = []
328+
removals: List[Coin] = []
329+
additions: List[Coin] = []
330+
log.info(f"Starting to make block, max cost: {self.mempool_info.max_block_clvm_cost}")
331+
for item in self.spends_by_feerate():
332+
if not item_inclusion_filter(item.name):
333+
continue
334+
log.info("Cumulative cost: %d, fee per cost: %0.4f", cost_sum, item.fee_per_cost)
335+
if (
336+
item.cost + cost_sum > self.mempool_info.max_block_clvm_cost
337+
or item.fee + fee_sum > DEFAULT_CONSTANTS.MAX_COIN_AMOUNT
338+
):
339+
break
340+
spend_bundles.append(item.spend_bundle)
341+
cost_sum += item.cost
342+
fee_sum += item.fee
343+
removals.extend(item.removals)
344+
if item.npc_result.conds is not None:
345+
for spend in item.npc_result.conds.spends:
346+
for puzzle_hash, amount, _ in spend.create_coin:
347+
coin = Coin(spend.coin_id, puzzle_hash, amount)
348+
additions.append(coin)
349+
if len(spend_bundles) == 0:
350+
return None
351+
log.info(
352+
f"Cumulative cost of block (real cost should be less) {cost_sum}. Proportion "
353+
f"full: {cost_sum / self.mempool_info.max_block_clvm_cost}"
354+
)
355+
agg = SpendBundle.aggregate(spend_bundles)
356+
return agg, additions, removals

chia/full_node/mempool_manager.py

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -209,31 +209,6 @@ def __init__(
209209
def shut_down(self) -> None:
210210
self.pool.shutdown(wait=True)
211211

212-
def process_mempool_items(
213-
self, item_inclusion_filter: Callable[[bytes32], bool]
214-
) -> Tuple[List[SpendBundle], uint64, List[Coin], List[Coin]]:
215-
cost_sum = 0 # Checks that total cost does not exceed block maximum
216-
fee_sum = 0 # Checks that total fees don't exceed 64 bits
217-
spend_bundles: List[SpendBundle] = []
218-
removals: List[Coin] = []
219-
additions: List[Coin] = []
220-
for item in self.mempool.spends_by_feerate():
221-
if not item_inclusion_filter(item.name):
222-
continue
223-
log.info("Cumulative cost: %d, fee per cost: %0.4f", cost_sum, item.fee_per_cost)
224-
if item.cost + cost_sum > self.max_block_clvm_cost or item.fee + fee_sum > self.constants.MAX_COIN_AMOUNT:
225-
return (spend_bundles, uint64(cost_sum), additions, removals)
226-
spend_bundles.append(item.spend_bundle)
227-
cost_sum += item.cost
228-
fee_sum += item.fee
229-
removals.extend(item.removals)
230-
if item.npc_result.conds is not None:
231-
for spend in item.npc_result.conds.spends:
232-
for puzzle_hash, amount, _ in spend.create_coin:
233-
coin = Coin(spend.coin_id, puzzle_hash, amount)
234-
additions.append(coin)
235-
return (spend_bundles, uint64(cost_sum), additions, removals)
236-
237212
def create_bundle_from_mempool(
238213
self,
239214
last_tb_header_hash: bytes32,
@@ -245,24 +220,13 @@ def create_bundle_from_mempool(
245220
"""
246221
if self.peak is None or self.peak.header_hash != last_tb_header_hash:
247222
return None
248-
249223
if item_inclusion_filter is None:
250224

251225
def always(bundle_name: bytes32) -> bool:
252226
return True
253227

254228
item_inclusion_filter = always
255-
256-
log.info(f"Starting to make block, max cost: {self.max_block_clvm_cost}")
257-
spend_bundles, cost_sum, additions, removals = self.process_mempool_items(item_inclusion_filter)
258-
if len(spend_bundles) == 0:
259-
return None
260-
log.info(
261-
f"Cumulative cost of block (real cost should be less) {cost_sum}. Proportion "
262-
f"full: {cost_sum / self.max_block_clvm_cost}"
263-
)
264-
agg = SpendBundle.aggregate(spend_bundles)
265-
return agg, additions, removals
229+
return self.mempool.create_bundle_from_mempool_items(item_inclusion_filter)
266230

267231
def get_filter(self) -> bytes:
268232
all_transactions: Set[bytes32] = set()

0 commit comments

Comments
 (0)