Skip to content

Commit 9c2c8ec

Browse files
committed
Simplify MempoolItem by constructing SpendBundle on demand from its components.
1 parent 2b6ff08 commit 9c2c8ec

11 files changed

+66
-27
lines changed

chia/_tests/core/mempool/test_mempool.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def make_item(
117117
) -> MempoolItem:
118118
spend_bundle_name = bytes32([idx] * 32)
119119
return MempoolItem(
120-
SpendBundle([], G2Element()),
120+
G2Element(),
121121
fee,
122122
SpendBundleConditions([], 0, 0, 0, None, None, [], cost, 0, 0, False, 0, 0, 0, 0, 0),
123123
spend_bundle_name,
@@ -2922,8 +2922,8 @@ def test_items_by_feerate(items: list[MempoolItem], expected: list[Coin]) -> Non
29222922

29232923
last_fpc: Optional[float] = None
29242924
for mi, expected_coin in zip(ordered_items, expected):
2925-
assert len(mi.spend_bundle.coin_spends) == 1
2926-
assert mi.spend_bundle.coin_spends[0].coin == expected_coin
2925+
assert len(mi.bundle_coin_spends) == 1
2926+
assert next(iter(mi.bundle_coin_spends.values())).coin_spend.coin == expected_coin
29272927
assert last_fpc is None or last_fpc >= mi.fee_per_cost
29282928
last_fpc = mi.fee_per_cost
29292929

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from __future__ import annotations
2+
3+
import random
4+
5+
import pytest
6+
from chia_rs import Coin, CoinSpend, G2Element, Program, SpendBundle
7+
from chia_rs.sized_bytes import bytes32
8+
from chia_rs.sized_ints import uint64
9+
10+
from chia._tests.core.mempool.test_mempool_manager import (
11+
IDENTITY_PUZZLE,
12+
IDENTITY_PUZZLE_HASH,
13+
TestCoins,
14+
add_spendbundle,
15+
setup_mempool,
16+
)
17+
18+
19+
@pytest.mark.anyio
20+
async def test_to_spend_bundle() -> None:
21+
"""
22+
Tests that we can properly go back to a `SpendBundle` from a `MempoolItem`.
23+
"""
24+
coins = [Coin(bytes32.random(), IDENTITY_PUZZLE_HASH, uint64(i + 1)) for i in range(random.randint(42, 1337))]
25+
mempool_manager = await setup_mempool(TestCoins(coins, {}))
26+
random_sample = random.sample(coins, 42)
27+
sb = SpendBundle([CoinSpend(coin, IDENTITY_PUZZLE, Program.to(None)) for coin in random_sample], G2Element())
28+
sb_name = sb.name()
29+
await add_spendbundle(mempool_manager, sb, sb_name)
30+
mi = mempool_manager.get_mempool_item(sb_name)
31+
assert mi is not None
32+
result = mi.to_spend_bundle()
33+
assert result == sb
34+
assert result.name() == sb.name()

chia/_tests/core/mempool/test_mempool_item_queries.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def make_item(coin_spends: list[CoinSpend]) -> MempoolItem:
4848
assert npc_result.conds is not None
4949
bundle_coin_spends, fee = make_bundle_spends_map_and_fee(spend_bundle, npc_result.conds)
5050
return MempoolItem(
51-
spend_bundle=spend_bundle,
51+
aggregated_signature=spend_bundle.aggregated_signature,
5252
fee=fee,
5353
conds=npc_result.conds,
5454
spend_bundle_name=spend_bundle.name(),

chia/_tests/core/mempool/test_mempool_manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ def mempool_item_from_spendbundle(spend_bundle: SpendBundle) -> MempoolItem:
597597
)
598598
bundle_coin_spends, fee = make_bundle_spends_map_and_fee(spend_bundle, conds)
599599
return MempoolItem(
600-
spend_bundle=spend_bundle,
600+
aggregated_signature=spend_bundle.aggregated_signature,
601601
fee=fee,
602602
conds=conds,
603603
spend_bundle_name=spend_bundle.name(),
@@ -927,7 +927,7 @@ def mk_item(
927927
spend_bundle = SpendBundle(coin_spends, G2Element())
928928
conds = make_test_conds(cost=cost, spend_ids=spend_ids)
929929
return MempoolItem(
930-
spend_bundle=spend_bundle,
930+
aggregated_signature=spend_bundle.aggregated_signature,
931931
fee=uint64(fee),
932932
conds=conds,
933933
spend_bundle_name=spend_bundle.name(),

chia/_tests/core/mempool/test_singleton_fast_forward.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ def test_process_fast_forward_spends_nothing_to_do() -> None:
5353
item = mempool_item_from_spendbundle(sb)
5454
# This coin is not eligible for fast forward
5555
assert not item.bundle_coin_spends[TEST_COIN_ID].supports_fast_forward
56-
internal_mempool_item = InternalMempoolItem(sb, item.conds, item.height_added_to_mempool, item.bundle_coin_spends)
56+
internal_mempool_item = InternalMempoolItem(
57+
sb.aggregated_signature, item.conds, item.height_added_to_mempool, item.bundle_coin_spends
58+
)
5759
original_version = dataclasses.replace(internal_mempool_item)
5860
singleton_ff = SingletonFastForward()
5961
bundle_coin_spends = singleton_ff.process_fast_forward_spends(
@@ -83,7 +85,9 @@ def test_process_fast_forward_spends_latest_unspent() -> None:
8385
item = mempool_item_from_spendbundle(sb)
8486
assert item.bundle_coin_spends[test_coin.name()].supports_fast_forward
8587
item.bundle_coin_spends[test_coin.name()].latest_singleton_lineage = test_unspent_lineage_info
86-
internal_mempool_item = InternalMempoolItem(sb, item.conds, item.height_added_to_mempool, item.bundle_coin_spends)
88+
internal_mempool_item = InternalMempoolItem(
89+
sb.aggregated_signature, item.conds, item.height_added_to_mempool, item.bundle_coin_spends
90+
)
8791
original_version = dataclasses.replace(internal_mempool_item)
8892
singleton_ff = SingletonFastForward()
8993
bundle_coin_spends = singleton_ff.process_fast_forward_spends(

chia/_tests/fee_estimation/test_fee_estimation_integration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def make_mempoolitem() -> MempoolItem:
4242
spends: list[SpendConditions] = []
4343
conds = SpendBundleConditions(spends, 0, 0, 0, None, None, [], cost, 0, 0, False, 0, 0, 0, 0, 0)
4444
mempool_item = MempoolItem(
45-
spend_bundle,
45+
spend_bundle.aggregated_signature,
4646
fee,
4747
conds,
4848
spend_bundle.name(),

chia/full_node/eligible_coin_spends.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,7 @@ def process_fast_forward_spends(
268268
# This item doesn't have any fast forward coins, nothing to do here
269269
return new_bundle_coin_spends
270270
# Update the mempool item after validating the new spend bundle
271-
new_sb = SpendBundle(
272-
coin_spends=new_coin_spends, aggregated_signature=mempool_item.spend_bundle.aggregated_signature
273-
)
271+
new_sb = SpendBundle(coin_spends=new_coin_spends, aggregated_signature=mempool_item.aggregated_signature)
274272
assert mempool_item.conds is not None
275273
try:
276274
# Run the new spend bundle to make sure it remains valid. What we

chia/full_node/mempool.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def _row_to_item(self, row: sqlite3.Row) -> MempoolItem:
158158
item = self._items[name]
159159

160160
return MempoolItem(
161-
item.spend_bundle,
161+
item.aggregated_signature,
162162
uint64(fee),
163163
item.conds,
164164
name,
@@ -481,7 +481,7 @@ def add_to_pool(self, item: MempoolItem) -> MempoolAddInfo:
481481
conn.executemany("INSERT OR IGNORE INTO spends VALUES(?, ?)", all_coin_spends)
482482

483483
self._items[item_name] = InternalMempoolItem(
484-
item.spend_bundle, item.conds, item.height_added_to_mempool, item.bundle_coin_spends
484+
item.aggregated_signature, item.conds, item.height_added_to_mempool, item.bundle_coin_spends
485485
)
486486
self._total_cost += item.cost
487487
self._total_fee += item.fee
@@ -653,7 +653,7 @@ def create_bundle_from_mempool_items(
653653
break
654654
coin_spends.extend(unique_coin_spends)
655655
additions.extend(unique_additions)
656-
sigs.append(item.spend_bundle.aggregated_signature)
656+
sigs.append(item.aggregated_signature)
657657
cost_sum = new_cost_sum
658658
fee_sum = new_fee_sum
659659
processed_spend_bundles += 1
@@ -761,7 +761,7 @@ def create_block_generator2(
761761
break
762762

763763
batch_cost += cost - cost_saving
764-
batch_transactions.append(SpendBundle(unique_coin_spends, item.spend_bundle.aggregated_signature))
764+
batch_transactions.append(SpendBundle(unique_coin_spends, item.aggregated_signature))
765765
batch_spends += len(unique_coin_spends)
766766
batch_additions.extend(unique_additions)
767767
fee_sum = new_fee_sum

chia/full_node/mempool_manager.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ async def validate_spend_bundle(
741741
return Err.IMPOSSIBLE_SECONDS_ABSOLUTE_CONSTRAINTS, None, [] # MempoolInclusionStatus.FAILED
742742

743743
potential = MempoolItem(
744-
new_spend,
744+
new_spend.aggregated_signature,
745745
uint64(fees),
746746
conds,
747747
spend_name,
@@ -781,7 +781,7 @@ def get_spendbundle(self, bundle_hash: bytes32) -> Optional[SpendBundle]:
781781
"""Returns a full SpendBundle if it's inside one the mempools"""
782782
item: Optional[MempoolItem] = self.mempool.get_item_by_id(bundle_hash)
783783
if item is not None:
784-
return item.spend_bundle
784+
return item.to_spend_bundle()
785785
return None
786786

787787
def get_mempool_item(self, bundle_hash: bytes32, include_pending: bool = False) -> Optional[MempoolItem]:
@@ -950,7 +950,7 @@ async def local_get_coin_records(names: Collection[bytes32]) -> list[CoinRecord]
950950

951951
for item in old_pool.all_items():
952952
info = await self.add_spend_bundle(
953-
item.spend_bundle,
953+
item.to_spend_bundle(),
954954
item.conds,
955955
item.spend_bundle_name,
956956
item.height_added_to_mempool,
@@ -972,7 +972,7 @@ async def local_get_coin_records(names: Collection[bytes32]) -> list[CoinRecord]
972972
txs_added = []
973973
for item in potential_txs.values():
974974
info = await self.add_spend_bundle(
975-
item.spend_bundle,
975+
item.to_spend_bundle(),
976976
item.conds,
977977
item.spend_bundle_name,
978978
item.height_added_to_mempool,
@@ -1003,7 +1003,7 @@ def get_items_not_in_filter(self, mempool_filter: PyBIP158, limit: int = 100) ->
10031003
return items
10041004
if mempool_filter.Match(bytearray(item.spend_bundle_name)):
10051005
continue
1006-
items.append(item.spend_bundle)
1006+
items.append(item.to_spend_bundle())
10071007

10081008
return items
10091009

chia/types/internal_mempool_item.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from dataclasses import dataclass
44

5-
from chia_rs import SpendBundle, SpendBundleConditions
5+
from chia_rs import G2Element, SpendBundleConditions
66
from chia_rs.sized_bytes import bytes32
77
from chia_rs.sized_ints import uint32
88

@@ -11,7 +11,7 @@
1111

1212
@dataclass(frozen=True)
1313
class InternalMempoolItem:
14-
spend_bundle: SpendBundle
14+
aggregated_signature: G2Element
1515
conds: SpendBundleConditions
1616
height_added_to_mempool: uint32
1717
# Map of coin ID to coin spend data between the bundle and its SpendBundleConditions

0 commit comments

Comments
 (0)