Skip to content

Commit

Permalink
Temp (will probably revert)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed May 29, 2024
1 parent 373e387 commit 93c7314
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 21 deletions.
27 changes: 21 additions & 6 deletions chia/_tests/connection_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import asyncio
import logging
from pathlib import Path
from typing import Set, Tuple
from typing import List, Set, Tuple

import aiohttp
from cryptography import x509
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes, serialization

from chia._tests.util.time_out_assert import time_out_assert
from chia.protocols.shared_protocol import capabilities
from chia.protocols.shared_protocol import default_node_capabilities, default_wallet_capabilities
from chia.server.outbound_message import NodeType
from chia.server.server import ChiaServer, ssl_context_for_client
from chia.server.ssl_context import chia_ssl_ca_paths, private_ssl_ca_paths
Expand Down Expand Up @@ -39,15 +39,27 @@ async def disconnect_all_and_reconnect(server: ChiaServer, reconnect_to: ChiaSer


async def add_dummy_connection(
server: ChiaServer, self_hostname: str, dummy_port: int, type: NodeType = NodeType.FULL_NODE
server: ChiaServer,
self_hostname: str,
dummy_port: int,
type: NodeType = NodeType.FULL_NODE,
*,
additional_capabilities: List[Tuple[uint16, str]] = [],
) -> Tuple[asyncio.Queue, bytes32]:
wsc, peer_id = await add_dummy_connection_wsc(server, self_hostname, dummy_port, type)
wsc, peer_id = await add_dummy_connection_wsc(
server, self_hostname, dummy_port, type, additional_capabilities=additional_capabilities
)

return wsc.incoming_queue, peer_id


async def add_dummy_connection_wsc(
server: ChiaServer, self_hostname: str, dummy_port: int, type: NodeType = NodeType.FULL_NODE
server: ChiaServer,
self_hostname: str,
dummy_port: int,
type: NodeType = NodeType.FULL_NODE,
*,
additional_capabilities: List[Tuple[uint16, str]] = [],
) -> Tuple[WSChiaConnection, bytes32]:
timeout = aiohttp.ClientTimeout(total=10)
session = aiohttp.ClientSession(timeout=timeout)
Expand Down Expand Up @@ -86,7 +98,10 @@ async def add_dummy_connection_wsc(
peer_id,
100,
30,
local_capabilities_for_handshake=capabilities,
local_capabilities_for_handshake=(
default_wallet_capabilities if type == NodeType.WALLET else default_node_capabilities
)
+ additional_capabilities,
)
await wsc.perform_handshake(server._network_id, dummy_port, type)
if wsc.incoming_message_task is not None:
Expand Down
2 changes: 1 addition & 1 deletion chia/_tests/core/full_node/test_full_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from chia.protocols import timelord_protocol, wallet_protocol
from chia.protocols.full_node_protocol import RespondTransaction
from chia.protocols.protocol_message_types import ProtocolMessageTypes
from chia.protocols.shared_protocol import Capability, capabilities
from chia.protocols.shared_protocol import Capability, default_node_capabilities as capabilities
from chia.protocols.wallet_protocol import SendTransaction, TransactionAck
from chia.server.address_manager import AddressManager
from chia.server.outbound_message import Message, NodeType
Expand Down
4 changes: 2 additions & 2 deletions chia/_tests/core/ssl/test_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import aiohttp
import pytest

from chia.protocols.shared_protocol import capabilities
from chia.protocols.shared_protocol import default_node_capabilities
from chia.server.outbound_message import NodeType
from chia.server.server import ChiaServer, ssl_context_for_client
from chia.server.ssl_context import chia_ssl_ca_paths, private_ssl_ca_paths
Expand Down Expand Up @@ -31,7 +31,7 @@ async def establish_connection(server: ChiaServer, self_hostname: str, ssl_conte
bytes32(b"\x00" * 32),
100,
30,
local_capabilities_for_handshake=capabilities,
local_capabilities_for_handshake=default_node_capabilities,
)
await wsc.perform_handshake(server._network_id, dummy_port, NodeType.FULL_NODE)
await wsc.close()
Expand Down
75 changes: 71 additions & 4 deletions chia/_tests/wallet/test_new_wallet_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
from random import Random
from typing import AsyncGenerator, Dict, List, Optional, OrderedDict, Set, Tuple

from anyio import sleep
import pytest
from chia_rs import Coin, CoinState
from chia_rs import AugSchemeMPL, Coin, CoinSpend, CoinState, FullBlock, G2Element, Program

from chia._tests.connection_utils import add_dummy_connection
from chia.full_node.coin_store import CoinStore
from chia.protocols import wallet_protocol
from chia.protocols.shared_protocol import Capability
from chia.server.outbound_message import Message, NodeType
from chia.server.ws_connection import WSChiaConnection
from chia.simulator import simulator_protocol
Expand All @@ -20,21 +22,33 @@
from chia.types.aliases import WalletService
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.types.coin_record import CoinRecord
from chia.types.peer_info import PeerInfo
from chia.types.spend_bundle import SpendBundle
from chia.util.hash import std_hash
from chia.util.ints import uint8, uint32, uint64
from chia.util.ints import uint8, uint16, uint32, uint64


IDENTITY_PUZZLE = Program.to(1)
IDENTITY_PUZZLE_HASH = IDENTITY_PUZZLE.get_tree_hash()

OneNode = Tuple[List[SimulatorFullNodeService], List[WalletService], BlockTools]


async def connect_to_simulator(
one_node: OneNode, self_hostname: str
one_node: OneNode, self_hostname: str, mempool_updates: bool = False
) -> Tuple[FullNodeSimulator, Queue[Message], WSChiaConnection]:
[full_node_service], _, _ = one_node

full_node_api = full_node_service._api
fn_server = full_node_api.server

incoming_queue, peer_id = await add_dummy_connection(fn_server, self_hostname, 41723, NodeType.WALLET)
incoming_queue, peer_id = await add_dummy_connection(
fn_server,
self_hostname,
41723,
NodeType.WALLET,
additional_capabilities=[(uint16(Capability.MEMPOOL_UPDATES), "1")] if mempool_updates else [],
)
peer = fn_server.all_connections[peer_id]

return full_node_api, incoming_queue, peer
Expand Down Expand Up @@ -760,3 +774,56 @@ async def run_test(include_spent: bool, include_unspent: bool, include_hinted: b
for include_hinted in [True, False]:
for min_amount in [0, 100000, 500000000]:
await run_test(include_spent, include_unspent, include_hinted, uint64(min_amount))


@pytest.mark.anyio
async def test_subscribed_mempool_items(
one_node: OneNode, self_hostname: str, default_400_blocks: List[FullBlock]
) -> None:
simulator, queue, peer = await connect_to_simulator(one_node, self_hostname, mempool_updates=True)
subs = simulator.full_node.subscriptions
coin_store = simulator.full_node.coin_store
genesis_challenge = simulator.full_node.constants.GENESIS_CHALLENGE

print("ZZZ")

await simulator.full_node.add_block(default_400_blocks[0])

print("YYY")

await simulator.full_node.add_block_batch(default_400_blocks[1:], PeerInfo("0.0.0.0", 0), None)

print("XXX")

ph1 = IDENTITY_PUZZLE_HASH
coin1 = Coin(bytes32(b"\0" * 32), ph1, uint64(1000))

await coin_store._add_coin_records([CoinRecord(coin1, uint32(1), uint32(0), False, uint64(10000))])

print("AAA")

print("BBB")

# Request coin state
resp = await simulator.request_coin_state(
wallet_protocol.RequestCoinState([coin1.name()], None, genesis_challenge, True), peer
)
assert resp is not None

print("CCC")

response = wallet_protocol.RespondCoinState.from_bytes(resp.data)
assert response.coin_ids == [coin1.name()]

# Add mempool item
solution = Program.to([])
bundle = SpendBundle([CoinSpend(coin1, IDENTITY_PUZZLE, solution)], AugSchemeMPL.aggregate([]))
result = await simulator.full_node.add_transaction(bundle, bundle.name())
print("X", result)

print("Y", simulator.auto_farm)
print("Z", simulator.full_node.mempool_manager.get_spendbundle(bundle.name()))
await sleep(5.0)

print(queue)
assert False
14 changes: 11 additions & 3 deletions chia/protocols/shared_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Capability(IntEnum):
RATE_LIMITS_V2 = 3

# a node can handle a None response and not wait the full timeout
# removed but functionality is still supported
NONE_RESPONSE = 4

# Opts in to receiving mempool updates for subscribed transactions
Expand All @@ -55,12 +56,19 @@ class Handshake(Streamable):
capabilities: List[Tuple[uint16, str]]


# "1" means capability is enabled
capabilities = [
# "1" means that the capability is enabled.

default_node_capabilities: List[Tuple[uint16, str]] = [
(uint16(Capability.BASE.value), "1"),
(uint16(Capability.BLOCK_HEADERS.value), "1"),
(uint16(Capability.RATE_LIMITS_V2.value), "1"),
(uint16(Capability.MEMPOOL_UPDATES.value), "1"),
]

default_wallet_capabilities: List[Tuple[uint16, str]] = [
(uint16(Capability.BASE.value), "1"),
(uint16(Capability.BLOCK_HEADERS.value), "1"),
(uint16(Capability.RATE_LIMITS_V2.value), "1"),
# (uint16(Capability.NONE_RESPONSE.value), "1"), # capability removed but functionality is still supported
]


Expand Down
6 changes: 4 additions & 2 deletions chia/server/start_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from chia.util.network import resolve
from chia.util.setproctitle import setproctitle

from ..protocols.shared_protocol import capabilities
from ..protocols.shared_protocol import default_node_capabilities, default_wallet_capabilities
from ..util.chia_version import chia_short_version

# this is used to detect whether we are running in the main process or not, in
Expand Down Expand Up @@ -117,7 +117,9 @@ def __init__(
if node_type == NodeType.WALLET:
inbound_rlp = self.service_config.get("inbound_rate_limit_percent", inbound_rlp)
outbound_rlp = 60
capabilities_to_use: List[Tuple[uint16, str]] = capabilities
capabilities_to_use: List[Tuple[uint16, str]] = (
default_wallet_capabilities if node_type == NodeType.WALLET else default_node_capabilities
)
if override_capabilities is not None:
capabilities_to_use = override_capabilities

Expand Down
6 changes: 3 additions & 3 deletions chia/simulator/setup_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from chia.cmds.init_funcs import init
from chia.consensus.constants import ConsensusConstants, replace_str_to_bytes
from chia.daemon.server import WebSocketServer, daemon_launch_lock_path
from chia.protocols.shared_protocol import Capability, capabilities
from chia.protocols.shared_protocol import Capability, default_node_capabilities
from chia.seeder.dns_server import DNSServer, create_dns_server_service
from chia.seeder.start_crawler import create_full_node_crawler_service
from chia.server.outbound_message import NodeType
Expand Down Expand Up @@ -71,7 +71,7 @@ def get_capabilities(disable_capabilities_values: Optional[List[Capability]]) ->
disable_capabilities_values.remove(Capability.BASE)

updated_capabilities = []
for capability in capabilities:
for capability in default_node_capabilities:
if Capability(int(capability[0])) in disable_capabilities_values:
# "0" means capability is disabled
updated_capabilities.append((capability[0], "0"))
Expand All @@ -80,7 +80,7 @@ def get_capabilities(disable_capabilities_values: Optional[List[Capability]]) ->
return updated_capabilities
except Exception:
logging.getLogger(__name__).exception("Error disabling capabilities, defaulting to all capabilities")
return capabilities.copy()
return default_node_capabilities.copy()


@asynccontextmanager
Expand Down

0 comments on commit 93c7314

Please sign in to comment.