Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into quex.explore_removing…
Browse files Browse the repository at this point in the history
…_pylint_ignores
  • Loading branch information
Quexington committed Nov 12, 2024
2 parents 1ae4318 + 06b2447 commit 17cfe2f
Show file tree
Hide file tree
Showing 29 changed files with 839 additions and 685 deletions.
13 changes: 5 additions & 8 deletions chia/_tests/blockchain/blockchain_test_utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from __future__ import annotations

import asyncio
from typing import Optional

from chia_rs import SpendBundleConditions

from chia.consensus.block_body_validation import ForkInfo
from chia.consensus.blockchain import AddBlockResult, Blockchain
from chia.consensus.difficulty_adjustment import get_next_sub_slot_iters_and_difficulty
from chia.consensus.multiprocess_validation import PreValidationResult, pre_validate_blocks_multiprocessing
from chia.consensus.multiprocess_validation import PreValidationResult, pre_validate_block
from chia.types.full_block import FullBlock
from chia.types.validation_state import ValidationState
from chia.util.augmented_chain import AugmentedBlockchain
Expand Down Expand Up @@ -81,17 +80,15 @@ async def _validate_and_add_block(
conds = SpendBundleConditions([], 0, 0, 0, None, None, [], 0, 0, 0, True)
results = PreValidationResult(None, uint64(1), conds, uint32(0))
else:
futures = await pre_validate_blocks_multiprocessing(
future = await pre_validate_block(
blockchain.constants,
AugmentedBlockchain(blockchain),
[block],
block,
blockchain.pool,
{},
None,
ValidationState(ssi, diff, prev_ses_block),
)
pre_validation_results: list[PreValidationResult] = list(await asyncio.gather(*futures))
assert pre_validation_results is not None
results = pre_validation_results[0]
results = await future
if results.error is not None:
if expected_result == AddBlockResult.INVALID_BLOCK and expected_error is None:
# We expected an error but didn't specify which one
Expand Down
229 changes: 118 additions & 111 deletions chia/_tests/blockchain/test_blockchain.py

Large diffs are not rendered by default.

45 changes: 24 additions & 21 deletions chia/_tests/core/full_node/test_full_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import random
import time
from collections.abc import Coroutine
from typing import Optional
from typing import Awaitable, Optional

import pytest
from chia_rs import AugSchemeMPL, G2Element, PrivateKey, SpendBundleConditions
Expand All @@ -24,7 +24,7 @@
from chia._tests.util.setup_nodes import SimulatorsAndWalletsServices
from chia._tests.util.time_out_assert import time_out_assert, time_out_assert_custom_interval, time_out_messages
from chia.consensus.block_body_validation import ForkInfo
from chia.consensus.multiprocess_validation import PreValidationResult, pre_validate_blocks_multiprocessing
from chia.consensus.multiprocess_validation import PreValidationResult, pre_validate_block
from chia.consensus.pot_iterations import is_overflow_block
from chia.full_node.full_node import WalletUpdate
from chia.full_node.full_node_api import FullNodeAPI
Expand Down Expand Up @@ -440,16 +440,21 @@ async def check_transaction_confirmed(transaction) -> bool:
for reorg_block in reog_blocks[:r]:
await _validate_and_add_block_no_error(blockchain, reorg_block, fork_info=fork_info)
for i in range(1, height):
futures = await pre_validate_blocks_multiprocessing(
blockchain.constants,
AugmentedBlockchain(blockchain),
all_blocks[:i],
blockchain.pool,
{},
ValidationState(ssi, diff, None),
)
vs = ValidationState(ssi, diff, None)
chain = AugmentedBlockchain(blockchain)
futures: list[Awaitable[PreValidationResult]] = []
for block in all_blocks[:i]:
futures.append(
await pre_validate_block(
blockchain.constants,
chain,
block,
blockchain.pool,
None,
vs,
)
)
results: list[PreValidationResult] = list(await asyncio.gather(*futures))
assert results is not None
for result in results:
assert result.error is None

Expand All @@ -458,16 +463,14 @@ async def check_transaction_confirmed(transaction) -> bool:
for block in all_blocks[:r]:
await _validate_and_add_block_no_error(blockchain, block, fork_info=fork_info)
for i in range(1, height):
futures = await pre_validate_blocks_multiprocessing(
blockchain.constants,
AugmentedBlockchain(blockchain),
all_blocks[:i],
blockchain.pool,
{},
ValidationState(ssi, diff, None),
)
results: list[PreValidationResult] = list(await asyncio.gather(*futures))
assert results is not None
vs = ValidationState(ssi, diff, None)
chain = AugmentedBlockchain(blockchain)
futures = []
for block in all_blocks[:i]:
futures.append(
await pre_validate_block(blockchain.constants, chain, block, blockchain.pool, None, vs)
)
results = list(await asyncio.gather(*futures))
for result in results:
assert result.error is None

Expand Down
9 changes: 7 additions & 2 deletions chia/_tests/core/mempool/test_mempool.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from chia.full_node.pending_tx_cache import ConflictTxCache, PendingTxCache
from chia.protocols import full_node_protocol, wallet_protocol
from chia.protocols.wallet_protocol import TransactionAck
from chia.server.api_protocol import ApiMetadata
from chia.server.outbound_message import Message
from chia.server.server import ChiaServer
from chia.server.ws_connection import WSChiaConnection
Expand All @@ -59,7 +60,6 @@
from chia.types.mempool_item import MempoolItem
from chia.types.spend_bundle import SpendBundle, estimate_fees
from chia.types.spend_bundle_conditions import SpendBundleConditions
from chia.util.api_decorators import api_request
from chia.util.errors import Err
from chia.util.hash import std_hash
from chia.util.ints import uint32, uint64
Expand Down Expand Up @@ -326,7 +326,12 @@ async def test_basic_mempool(
assert spend_bundle is not None


@api_request(peer_required=True, bytes_required=True)
metadata = ApiMetadata()


# this (method'ish) function is not designed per normal uses so allowing the ignore
# for the different return type. normal is Optional[Message]
@metadata.request(peer_required=True, bytes_required=True) # type: ignore[type-var]
async def respond_transaction(
self: FullNodeAPI,
tx: full_node_protocol.RespondTransaction,
Expand Down
29 changes: 29 additions & 0 deletions chia/_tests/core/mempool/test_mempool_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,16 @@ def make_test_coins() -> list[Coin]:
return ret


def make_ephemeral(coins: list[Coin]) -> list[Coin]:
ret: list[Coin] = []
for i, parent in enumerate(coins):
ret.append(Coin(parent.name(), height_hash(i + 150), uint64(i * 100)))
return ret


coins = make_test_coins()
eph = make_ephemeral(coins)
eph2 = make_ephemeral(eph)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -800,6 +809,26 @@ def make_test_coins() -> list[Coin]:
([mk_item(coins[0:2])], mk_item(coins[0:2], fee=10000000), True),
# or if we spend the same coins with additional coins
([mk_item(coins[0:2])], mk_item(coins[0:3], fee=10000000), True),
# SUPERSET RULE WITH EPHEMERAL COINS
# the super set rule only takes non-ephemeral coins into account. The
# ephmeral coins depend on how we spend, and might prevent legitimate
# replace-by-fee attempts.
# replace a spend that includes an ephemeral coin with one that doesn't
([mk_item(coins[0:2] + eph[0:1])], mk_item(coins[0:2], fee=10000000), True),
# replace a spend with two-levels of ephemeral coins, with one that
# only has 1-level
([mk_item(coins[0:2] + eph[0:1] + eph2[0:1])], mk_item(coins[0:2] + eph[0:1], fee=10000000), True),
# replace a spend with two-levels of ephemeral coins, with one that
# doesn't
([mk_item(coins[0:2] + eph[0:1] + eph2[0:1])], mk_item(coins[0:2], fee=10000000), True),
# replace a spend with two-levels of ephemeral coins, with one that
# has *different* ephemeral coins
([mk_item(coins[0:2] + eph[0:1] + eph2[0:1])], mk_item(coins[0:2] + eph[1:2] + eph2[1:2], fee=10000000), True),
# it's OK to add new ephemeral spends
([mk_item(coins[0:2])], mk_item(coins[0:2] + eph[1:2] + eph2[1:2], fee=10000000), True),
# eph2[0:1] is not an ephemeral coin here, this violates the superset
# rule. eph[0:1] is missing for that
([mk_item(coins[0:2] + eph2[0:1])], mk_item(coins[0:2] + eph[1:2] + eph2[1:2], fee=10000000), False),
# FEE- AND FEE RATE RULES
# if we're replacing two items, each paying a fee of 100, we need to
# spend (at least) the same coins and pay at least 10000000 higher fee
Expand Down
21 changes: 21 additions & 0 deletions chia/_tests/core/server/test_api_protocol.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from __future__ import annotations

import pytest

from chia.protocols.full_node_protocol import RequestTransaction
from chia.protocols.protocol_message_types import ProtocolMessageTypes
from chia.server.api_protocol import ApiMetadata


def test_api_protocol_raises_for_repeat_request_registration() -> None:
metadata = ApiMetadata()

@metadata.request(request_type=ProtocolMessageTypes.handshake)
async def f(self: object, request: RequestTransaction) -> None: ...

async def g(self: object, request: RequestTransaction) -> None: ...

decorator = metadata.request(request_type=ProtocolMessageTypes.handshake)

with pytest.raises(Exception, match="request type already registered"):
decorator(g)
7 changes: 4 additions & 3 deletions chia/_tests/core/server/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import logging
from dataclasses import dataclass
from typing import Callable, cast
from typing import Callable, ClassVar, cast

import pytest
from packaging.version import Version
Expand All @@ -16,6 +16,7 @@
from chia.protocols.protocol_message_types import ProtocolMessageTypes
from chia.protocols.shared_protocol import Error, protocol_version
from chia.protocols.wallet_protocol import RejectHeaderRequest
from chia.server.api_protocol import ApiMetadata
from chia.server.outbound_message import NodeType, make_msg
from chia.server.server import ChiaServer
from chia.server.start_full_node import create_full_node_service
Expand All @@ -24,20 +25,20 @@
from chia.simulator.block_tools import BlockTools
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.types.peer_info import PeerInfo
from chia.util.api_decorators import api_request
from chia.util.errors import ApiError, Err
from chia.util.ints import int16, uint32


@dataclass
class TestAPI:
log: logging.Logger = logging.getLogger(__name__)
metadata: ClassVar[ApiMetadata] = ApiMetadata()

def ready(self) -> bool:
return True

# API call from FullNodeAPI
@api_request()
@metadata.request()
async def request_transaction(self, request: RequestTransaction) -> None:
raise ApiError(Err.NO_TRANSACTIONS_WHILE_SYNCING, f"Some error message: {request.transaction_id}", b"ab")

Expand Down
Loading

0 comments on commit 17cfe2f

Please sign in to comment.