Skip to content

Commit

Permalink
Merge branch 'staging' into release/8.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ibraheem-opentensor committed Oct 4, 2024
2 parents 538511c + e1d6b5c commit ff7f215
Show file tree
Hide file tree
Showing 14 changed files with 144 additions and 56 deletions.
21 changes: 0 additions & 21 deletions bittensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,6 @@
from .utils.deprecated import *


# Logging helpers.
def trace(on: bool = True):
"""
Enables or disables trace logging.
Args:
on (bool): If True, enables trace logging. If False, disables trace logging.
"""
logging.set_trace(on)


def debug(on: bool = True):
"""
Enables or disables debug logging.
Args:
on (bool): If True, enables debug logging. If False, disables debug logging.
"""
logging.set_debug(on)


def __getattr__(name):
if name == "version_split":
warnings.warn(
Expand Down
5 changes: 3 additions & 2 deletions bittensor/core/axon.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@
from typing import Any, Awaitable, Callable, Optional, Tuple

import uvicorn
from bittensor_wallet import Wallet
from bittensor_wallet import Wallet, Keypair

from fastapi import APIRouter, Depends, FastAPI
from fastapi.responses import JSONResponse
from fastapi.routing import serialize_response
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
from starlette.requests import Request
from starlette.responses import Response
from substrateinterface import Keypair


from bittensor.core.chain_data import AxonInfo
from bittensor.core.config import Config
Expand Down
3 changes: 1 addition & 2 deletions bittensor/core/dendrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
from typing import Any, AsyncGenerator, Optional, Union, Type

import aiohttp
from bittensor_wallet import Wallet
from substrateinterface import Keypair
from bittensor_wallet import Keypair, Wallet

from bittensor.core.axon import Axon
from bittensor.core.chain_data import AxonInfo
Expand Down
17 changes: 10 additions & 7 deletions bittensor/core/subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def __init__(
network: Optional[str] = None,
config: Optional["Config"] = None,
_mock: bool = False,
log_verbose: bool = True,
log_verbose: bool = False,
connection_timeout: int = 600,
) -> None:
"""
Expand Down Expand Up @@ -175,17 +175,18 @@ def __init__(
logging.info(
f"You are connecting to {self.network} network with endpoint {self.chain_endpoint}."
)
logging.warning(
logging.debug(
"We strongly encourage running a local subtensor node whenever possible. "
"This increases decentralization and resilience of the network."
)
logging.warning(
logging.debug(
"In a future release, local subtensor will become the default endpoint. "
"To get ahead of this change, please run a local subtensor node and point to it."
)

self.log_verbose = log_verbose
self._connection_timeout = connection_timeout
self.substrate: "SubstrateInterface" = None
self._get_substrate()

def __str__(self) -> str:
Expand All @@ -201,7 +202,8 @@ def __repr__(self) -> str:

def close(self):
"""Cleans up resources for this subtensor instance like active websocket connection and active extensions."""
self.substrate.close()
if self.substrate:
self.substrate.close()

def _get_substrate(self):
"""Establishes a connection to the Substrate node using configured parameters."""
Expand All @@ -214,7 +216,7 @@ def _get_substrate(self):
type_registry=settings.TYPE_REGISTRY,
)
if self.log_verbose:
logging.info(
logging.debug(
f"Connected to {self.network} network and {self.chain_endpoint}."
)

Expand All @@ -223,14 +225,15 @@ def _get_substrate(self):
except (AttributeError, TypeError, socket.error, OSError) as e:
logging.warning(f"Error setting timeout: {e}")

except ConnectionRefusedError:
except ConnectionRefusedError as error:
logging.error(
f"Could not connect to {self.network} network with {self.chain_endpoint} chain endpoint.",
)
logging.info(
"You can check if you have connectivity by running this command: nc -vz localhost "
f"{self.chain_endpoint.split(':')[2]}"
f"{self.chain_endpoint}"
)
raise ConnectionRefusedError(error.args)

@staticmethod
def config() -> "Config":
Expand Down
6 changes: 3 additions & 3 deletions bittensor/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
# DEALINGS IN THE SOFTWARE.

import hashlib
from typing import List, Dict, Literal, Union, Optional, TYPE_CHECKING
from typing import Literal, Union, Optional, TYPE_CHECKING

import scalecodec
from substrateinterface import Keypair
from bittensor_wallet import Keypair
from substrateinterface.utils import ss58

from bittensor.core.settings import SS58_FORMAT
Expand Down Expand Up @@ -245,7 +245,7 @@ def _is_valid_ed25519_pubkey(public_key: Union[str, bytes]) -> bool:
else:
raise ValueError("public_key must be a string or bytes")

keypair = Keypair(public_key=public_key, ss58_format=SS58_FORMAT)
keypair = Keypair(public_key=public_key)

ss58_addr = keypair.ss58_address
return ss58_addr is not None
Expand Down
69 changes: 65 additions & 4 deletions bittensor/utils/btlogging/loggingmachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,48 @@ class LoggingMachine(StateMachine, Logger):
Debug = State()
Trace = State()
Disabled = State()
Warning = State()

enable_default = (
Debug.to(Default)
| Trace.to(Default)
| Disabled.to(Default)
| Default.to(Default)
| Warning.to(Default)
)

enable_info = enable_default

enable_trace = (
Default.to(Trace) | Debug.to(Trace) | Disabled.to(Trace) | Trace.to(Trace)
Default.to(Trace)
| Debug.to(Trace)
| Disabled.to(Trace)
| Trace.to(Trace)
| Warning.to(Trace)
)

enable_debug = (
Default.to(Debug) | Trace.to(Debug) | Disabled.to(Debug) | Debug.to(Debug)
Default.to(Debug)
| Trace.to(Debug)
| Disabled.to(Debug)
| Debug.to(Debug)
| Warning.to(Debug)
)

enable_warning = (
Default.to(Warning)
| Trace.to(Warning)
| Disabled.to(Warning)
| Debug.to(Warning)
| Warning.to(Warning)
)

disable_trace = Trace.to(Default)

disable_debug = Debug.to(Default)

disable_warning = Warning.to(Default)

disable_logging = (
Trace.to(Disabled)
| Debug.to(Disabled)
Expand Down Expand Up @@ -301,16 +323,36 @@ def after_transition(self, event, state):
# Default Logging
def before_enable_default(self):
"""Logs status before enable Default."""
self._logger.info(f"Enabling default logging.")
self._logger.info("Enabling default logging.")
self._logger.setLevel(stdlogging.WARNING)
for logger in all_loggers():
if logger.name in self._primary_loggers:
continue
logger.setLevel(stdlogging.CRITICAL)

def before_enable_info(self):
"""Logs status before enable Default."""
self._logger.info("Enabling default logging.")
self._logger.setLevel(stdlogging.INFO)
for logger in all_loggers():
if logger.name in self._primary_loggers:
continue
logger.setLevel(stdlogging.CRITICAL)

def after_enable_default(self):
pass

def before_enable_warning(self):
"""Logs status before enable Warning."""
self._logger.info("Enabling warning.")
self._stream_formatter.set_trace(True)
for logger in all_loggers():
logger.setLevel(stdlogging.WARNING)

def after_enable_warning(self):
"""Logs status after enable Warning."""
self._logger.info("Warning enabled.")

# Trace
def before_enable_trace(self):
"""Logs status before enable Trace."""
Expand All @@ -325,7 +367,7 @@ def after_enable_trace(self):

def before_disable_trace(self):
"""Logs status before disable Trace."""
self._logger.info(f"Disabling trace.")
self._logger.info("Disabling trace.")
self._stream_formatter.set_trace(False)
self.enable_default()

Expand Down Expand Up @@ -449,6 +491,25 @@ def set_trace(self, on: bool = True):
if self.current_state_value == "Trace":
self.disable_trace()

def set_warning(self, on: bool = True):
"""Sets Warning state."""
if on and not self.current_state_value == "Warning":
self.enable_warning()
elif not on:
if self.current_state_value == "Warning":
self.disable_warning()

def set_default(self):
"""Sets Default state."""
if not self.current_state_value == "Default":
self.enable_default()

# as an option to be more obvious. `bittensor.logging.set_info()` is the same `bittensor.logging.set_default()`
def set_info(self):
"""Sets Default state."""
if not self.current_state_value == "Default":
self.enable_info()

def get_level(self) -> int:
"""Returns Logging level."""
return self._logger.level
Expand Down
35 changes: 34 additions & 1 deletion bittensor/utils/deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
Keyfile,
)
from bittensor_wallet.wallet import display_mnemonic_msg, Wallet # noqa: F401
from substrateinterface import Keypair # noqa: F401
from bittensor_wallet import Keypair # noqa: F401

from bittensor.core import settings
from bittensor.core.axon import Axon
Expand Down Expand Up @@ -112,6 +112,7 @@
)
from bittensor.utils.balance import Balance as Balance # noqa: F401
from bittensor.utils.mock.subtensor_mock import MockSubtensor as MockSubtensor # noqa: F401
from bittensor.utils.btlogging import logging
from bittensor.utils.subnets import SubnetsAPI # noqa: F401

# Backwards compatibility with previous bittensor versions.
Expand Down Expand Up @@ -148,3 +149,35 @@
# Makes the `bittensor.core.extrinsics` subpackage available as `bittensor.extrinsics` for backwards compatibility.
extrinsics_subpackage = importlib.import_module("bittensor.core.extrinsics")
sys.modules["bittensor.extrinsics"] = extrinsics_subpackage


# Logging helpers.
def trace(on: bool = True):
"""
Enables or disables trace logging.
Args:
on (bool): If True, enables trace logging. If False, disables trace logging.
"""
logging.set_trace(on)


def debug(on: bool = True):
"""
Enables or disables debug logging.
Args:
on (bool): If True, enables debug logging. If False, disables debug logging.
"""
logging.set_debug(on)


def warning(on: bool = True):
"""
Enables or disables warning logging.
Args:
on (bool): If True, enables warning logging. If False, disables warning logging and sets default (INFO) level.
"""
logging.set_warning(on)


# set Warning logging level for bittensor SDK
warning()
2 changes: 1 addition & 1 deletion bittensor/utils/weight_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

from numpy.typing import NDArray
from scalecodec import U16, ScaleBytes, Vec
from substrateinterface import Keypair
from bittensor_wallet import Keypair

from bittensor.utils.btlogging import logging
from bittensor.utils.registration import legacy_torch_api_compat, torch, use_torch
Expand Down
2 changes: 1 addition & 1 deletion requirements/prod.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ python-Levenshtein
scalecodec==1.2.11
substrate-interface~=1.7.9
uvicorn
bittensor-wallet==1.0.0
bittensor-wallet==2.0.0
5 changes: 4 additions & 1 deletion tests/e2e_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ def local_chain(request):

# install neuron templates
logging.info("downloading and installing neuron templates from github")
templates_dir = clone_or_update_templates()
# commit with subnet-template-repo changes for rust wallet
templates_dir = clone_or_update_templates(
"334d3da101279218b3a4c9d72a12d517f6e39be3"
)
install_templates(templates_dir)

timestamp = int(time.time())
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e_tests/utils/e2e_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import subprocess
import sys

from substrateinterface import Keypair
from bittensor_wallet import Keypair

import bittensor

Expand Down
Loading

0 comments on commit ff7f215

Please sign in to comment.