Skip to content

Commit 8f5a7e0

Browse files
authored
CHIA-3799 It's not reasonable to advertise a new transaction with zero cost (#20231)
It's not reasonable to advertise a new transaction with zero cost.
1 parent 2971eb0 commit 8f5a7e0

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

chia/_tests/core/full_node/test_full_node.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3463,3 +3463,32 @@ async def send_from_node_3() -> None:
34633463
await time_out_assert(5, lambda: full_node_2_ip in server_1.banned_peers)
34643464
else:
34653465
await time_out_assert(5, lambda: full_node_2_ip not in server_1.banned_peers)
3466+
3467+
3468+
@pytest.mark.anyio
3469+
async def test_new_tx_zero_cost(
3470+
setup_two_nodes_fixture: tuple[list[FullNodeSimulator], list[tuple[WalletNode, ChiaServer]], BlockTools],
3471+
self_hostname: str,
3472+
) -> None:
3473+
"""
3474+
Tests that a peer gets banned if it sends a `NewTransaction` message with
3475+
zero cost.
3476+
"""
3477+
[full_node_1, full_node_2], _, bt = setup_two_nodes_fixture
3478+
server_1 = full_node_1.full_node.server
3479+
server_2 = full_node_2.full_node.server
3480+
await server_2.start_client(PeerInfo(self_hostname, server_1.get_port()), full_node_2.full_node.on_connect)
3481+
ws_con_1 = next(iter(server_1.all_connections.values()))
3482+
ws_con_2 = next(iter(server_2.all_connections.values()))
3483+
await full_node_1.full_node.add_block(bt.get_consecutive_blocks(1)[0])
3484+
# Send a NewTransaction with zero cost
3485+
msg = make_msg(
3486+
ProtocolMessageTypes.new_transaction, NewTransaction(bytes32.random(), cost=uint64(0), fees=uint64(42))
3487+
)
3488+
# We won't ban localhost, so let's set a different ip address for the
3489+
# second node.
3490+
full_node_2_ip = "1.3.3.7"
3491+
ws_con_1.peer_info = PeerInfo(full_node_2_ip, ws_con_1.peer_info.port)
3492+
await ws_con_2.send_message(msg)
3493+
# Make sure the first full node has banned the second.
3494+
await time_out_assert(3, lambda: full_node_2_ip in server_1.banned_peers)

chia/full_node/full_node_api.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,14 @@ async def new_transaction(
219219
if not (await self.full_node.synced()):
220220
return None
221221

222+
# It's not reasonable to advertise a transaction with zero cost
223+
if transaction.cost == 0:
224+
self.log.warning(
225+
f"Banning peer {peer.peer_node_id}. Sent us a tx {transaction.transaction_id} with zero cost."
226+
)
227+
await peer.close(CONSENSUS_ERROR_BAN_SECONDS)
228+
return None
229+
222230
# If already seen, the cost and fee must match, otherwise ban the peer
223231
mempool_item = self.full_node.mempool_manager.get_mempool_item(transaction.transaction_id, include_pending=True)
224232
if mempool_item is not None:

0 commit comments

Comments
 (0)