Skip to content

Commit 7fdd0b2

Browse files
authored
Improved mempool validation and test (#8652)
1 parent 9179353 commit 7fdd0b2

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

chia/full_node/full_node_api.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22
import dataclasses
33
import time
4+
import traceback
45
from secrets import token_bytes
56
from typing import Callable, Dict, List, Optional, Tuple, Set
67

@@ -724,6 +725,7 @@ async def declare_proof_of_space(
724725
curr_l_tb.header_hash
725726
)
726727
except Exception as e:
728+
self.log.error(f"Traceback: {traceback.format_exc()}")
727729
self.full_node.log.error(f"Error making spend bundle {e} peak: {peak}")
728730
mempool_bundle = None
729731
if mempool_bundle is not None:

chia/full_node/mempool_manager.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ async def add_spendbundle(
255255
return None, MempoolInclusionStatus.FAILED, Err(npc_result.error)
256256
# build removal list
257257
removal_names: List[bytes32] = [npc.coin_name for npc in npc_list]
258+
if set(removal_names) != set([s.name() for s in new_spend.removals()]):
259+
return None, MempoolInclusionStatus.FAILED, Err.INVALID_SPEND_BUNDLE
258260

259261
additions = additions_for_npc(npc_list)
260262

chia/util/errors.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ class Err(Enum):
152152
INVALID_FEE_TOO_CLOSE_TO_ZERO = 123
153153
COIN_AMOUNT_NEGATIVE = 124
154154
INTERNAL_PROTOCOL_ERROR = 125
155+
INVALID_SPEND_BUNDLE = 126
155156

156157

157158
class ValidationError(Exception):

tests/core/full_node/test_mempool.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from chia.simulator.simulator_protocol import FarmNewBlockProtocol
1515
from chia.types.announcement import Announcement
1616
from chia.types.blockchain_format.coin import Coin
17+
from chia.types.blockchain_format.sized_bytes import bytes32
1718
from chia.types.coin_spend import CoinSpend
1819
from chia.types.condition_opcodes import ConditionOpcode
1920
from chia.types.condition_with_args import ConditionWithArgs
@@ -30,6 +31,7 @@
3031
from chia.full_node.pending_tx_cache import PendingTxCache
3132
from blspy import G2Element
3233

34+
from chia.util.recursive_replace import recursive_replace
3335
from tests.connection_utils import connect_and_get_peer
3436
from tests.core.node_height import node_height_at_least
3537
from tests.setup_nodes import bt, setup_simulators_and_wallets
@@ -2179,3 +2181,26 @@ def test_many_create_coin(self):
21792181
assert len(npc_result.npc_list[0].conditions[0][1]) == 6094
21802182
assert run_time < 1
21812183
print(f"run time:{run_time}")
2184+
2185+
@pytest.mark.asyncio
2186+
async def test_invalid_coin_spend_coin(self, two_nodes):
2187+
reward_ph = WALLET_A.get_new_puzzlehash()
2188+
blocks = bt.get_consecutive_blocks(
2189+
5,
2190+
guarantee_transaction_block=True,
2191+
farmer_reward_puzzle_hash=reward_ph,
2192+
pool_reward_puzzle_hash=reward_ph,
2193+
)
2194+
full_node_1, full_node_2, server_1, server_2 = two_nodes
2195+
2196+
for block in blocks:
2197+
await full_node_1.full_node.respond_block(full_node_protocol.RespondBlock(block))
2198+
2199+
await time_out_assert(60, node_height_at_least, True, full_node_2, blocks[-1].height)
2200+
2201+
spend_bundle = generate_test_spend_bundle(list(blocks[-1].get_included_reward_coins())[0])
2202+
coin_spend_0 = recursive_replace(spend_bundle.coin_spends[0], "coin.puzzle_hash", bytes32([1] * 32))
2203+
new_bundle = recursive_replace(spend_bundle, "coin_spends", [coin_spend_0] + spend_bundle.coin_spends[1:])
2204+
assert spend_bundle is not None
2205+
res = await full_node_1.full_node.respond_transaction(new_bundle, new_bundle.name())
2206+
assert res == (MempoolInclusionStatus.FAILED, Err.INVALID_SPEND_BUNDLE)

0 commit comments

Comments
 (0)