Skip to content

Commit

Permalink
Merge branch 'staging' into fix_python313
Browse files Browse the repository at this point in the history
  • Loading branch information
mjurbanski-reef authored Jun 21, 2024
2 parents e1f0dbd + b9d3b44 commit 203ea1e
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 20 deletions.
51 changes: 32 additions & 19 deletions bittensor/axon.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
import traceback
import typing
import uuid
from inspect import signature, Signature, Parameter
from typing import List, Optional, Tuple, Callable, Any, Dict, Awaitable
from inspect import Parameter, Signature, signature
from typing import Any, Awaitable, Callable, Dict, List, Optional, Tuple

import uvicorn
from fastapi import FastAPI, APIRouter, Depends
from fastapi import APIRouter, Depends, FastAPI
from fastapi.responses import JSONResponse
from fastapi.routing import serialize_response
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
Expand All @@ -44,18 +44,19 @@
from substrateinterface import Keypair

import bittensor
from bittensor.utils.axon_utils import allowed_nonce_window_ns, calculate_diff_seconds
from bittensor.constants import V_7_2_0
from bittensor.errors import (
BlacklistedException,
InvalidRequestNameError,
SynapseDendriteNoneException,
SynapseParsingError,
UnknownSynapseError,
NotVerifiedException,
BlacklistedException,
PriorityException,
PostProcessException,
PriorityException,
SynapseDendriteNoneException,
SynapseException,
SynapseParsingError,
UnknownSynapseError,
)
from bittensor.constants import ALLOWED_DELTA, V_7_2_0
from bittensor.threadpool import PriorityThreadPoolExecutor
from bittensor.utils import networking

Expand Down Expand Up @@ -847,6 +848,8 @@ async def default_verify(self, synapse: bittensor.Synapse):
The method checks for increasing nonce values, which is a vital
step in preventing replay attacks. A replay attack involves an adversary reusing or
delaying the transmission of a valid data transmission to deceive the receiver.
The first time a nonce is seen, it is checked for freshness by ensuring it is
within an acceptable delta time range.
Authenticity and Integrity Checks
By verifying that the message's digital signature matches
Expand Down Expand Up @@ -893,33 +896,43 @@ async def default_verify(self, synapse: bittensor.Synapse):
if synapse.dendrite.nonce is None:
raise Exception("Missing Nonce")

# If we don't have a nonce stored, ensure that the nonce falls within
# a reasonable delta.

# Newer nonce structure post v7.2
if (
synapse.dendrite.version is not None
and synapse.dendrite.version >= V_7_2_0
):
# If we don't have a nonce stored, ensure that the nonce falls within
# a reasonable delta.
current_time_ns = time.time_ns()
allowed_window_ns = allowed_nonce_window_ns(
current_time_ns, synapse.timeout
)

if (
self.nonces.get(endpoint_key) is None
and synapse.dendrite.nonce
<= time.time_ns() - ALLOWED_DELTA - (synapse.timeout or 0)
and synapse.dendrite.nonce <= allowed_window_ns
):
raise Exception("Nonce is too old")
diff_seconds, allowed_delta_seconds = calculate_diff_seconds(
current_time_ns, synapse.timeout, synapse.dendrite.nonce
)
raise Exception(
f"Nonce is too old: acceptable delta is {allowed_delta_seconds:.2f} seconds but request was {diff_seconds:.2f} seconds old"
)

# If a nonce is stored, ensure the new nonce
# is greater than the previous nonce
if (
self.nonces.get(endpoint_key) is not None
and synapse.dendrite.nonce <= self.nonces[endpoint_key]
):
raise Exception("Nonce is too old")
raise Exception("Nonce is too old, a newer one was last processed")
# Older nonce structure pre v7.2
else:
if (
endpoint_key in self.nonces.keys()
and self.nonces[endpoint_key] is not None
self.nonces.get(endpoint_key) is not None
and synapse.dendrite.nonce <= self.nonces[endpoint_key]
):
raise Exception("Nonce is too small")
raise Exception("Nonce is too old, a newer one was last processed")

if not keypair.verify(message, synapse.dendrite.signature):
raise Exception(
Expand Down
3 changes: 2 additions & 1 deletion bittensor/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
# DEALINGS IN THE SOFTWARE.


ALLOWED_DELTA = 4000000000 # Delta of 4 seconds for nonce validation
ALLOWED_DELTA = 4_000_000_000 # Delta of 4 seconds for nonce validation
V_7_2_0 = 7002000
NANOSECONDS_IN_SECOND = 1_000_000_000
38 changes: 38 additions & 0 deletions bittensor/utils/axon_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# The MIT License (MIT)
# Copyright © 2021 Yuma Rao
# Copyright © 2022 Opentensor Foundation
# Copyright © 2023 Opentensor Technologies Inc

# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
# documentation files (the “Software”), to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all copies or substantial portions of
# the Software.

# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
# THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.


from typing import Optional

from bittensor.constants import ALLOWED_DELTA, NANOSECONDS_IN_SECOND


def allowed_nonce_window_ns(current_time_ns: int, synapse_timeout: Optional[float]):
synapse_timeout_ns = (synapse_timeout or 0) * NANOSECONDS_IN_SECOND
allowed_window_ns = current_time_ns - ALLOWED_DELTA - synapse_timeout_ns
return allowed_window_ns


def calculate_diff_seconds(
current_time: int, synapse_timeout: Optional[float], synapse_nonce: int
):
synapse_timeout_ns = (synapse_timeout or 0) * NANOSECONDS_IN_SECOND
diff_seconds = (current_time - synapse_nonce) / NANOSECONDS_IN_SECOND
allowed_delta_seconds = (ALLOWED_DELTA + synapse_timeout_ns) / NANOSECONDS_IN_SECOND
return diff_seconds, allowed_delta_seconds
62 changes: 62 additions & 0 deletions tests/unit_tests/test_axon.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

# Standard Lib
import re
import time
from dataclasses import dataclass

from typing import Any
Expand All @@ -38,6 +39,8 @@
from bittensor import Synapse, RunException
from bittensor.axon import AxonMiddleware
from bittensor.axon import axon as Axon
from bittensor.utils.axon_utils import allowed_nonce_window_ns, calculate_diff_seconds
from bittensor.constants import ALLOWED_DELTA, NANOSECONDS_IN_SECOND


def test_attach():
Expand Down Expand Up @@ -613,3 +616,62 @@ async def forward_fn(synapse: custom_synapse_cls):
response_data = response.json()
assert sorted(response_data.keys()) == ["message"]
assert re.match(r"Internal Server Error #[\da-f\-]+", response_data["message"])


def test_allowed_nonce_window_ns():
mock_synapse = SynapseMock()
current_time = time.time_ns()
allowed_window_ns = allowed_nonce_window_ns(current_time, mock_synapse.timeout)
expected_window_ns = (
current_time - ALLOWED_DELTA - (mock_synapse.timeout * NANOSECONDS_IN_SECOND)
)
assert (
allowed_window_ns < current_time
), "Allowed window should be less than the current time"
assert (
allowed_window_ns == expected_window_ns
), f"Expected {expected_window_ns} but got {allowed_window_ns}"


@pytest.mark.parametrize("nonce_offset_seconds", [1, 3, 5, 10])
def test_nonce_diff_seconds(nonce_offset_seconds):
mock_synapse = SynapseMock()
current_time_ns = time.time_ns()
synapse_nonce = current_time_ns - (nonce_offset_seconds * NANOSECONDS_IN_SECOND)
diff_seconds, allowed_delta_seconds = calculate_diff_seconds(
current_time_ns, mock_synapse.timeout, synapse_nonce
)

expected_diff_seconds = nonce_offset_seconds # Because we subtracted nonce_offset_seconds from current_time_ns
expected_allowed_delta_seconds = (
ALLOWED_DELTA + (mock_synapse.timeout * NANOSECONDS_IN_SECOND)
) / NANOSECONDS_IN_SECOND

assert (
diff_seconds == expected_diff_seconds
), f"Expected {expected_diff_seconds} but got {diff_seconds}"
assert (
allowed_delta_seconds == expected_allowed_delta_seconds
), f"Expected {expected_allowed_delta_seconds} but got {allowed_delta_seconds}"


# Mimicking axon default_verify nonce verification
# True: Nonce is fresh, False: Nonce is old
def is_nonce_within_allowed_window(synapse_nonce, allowed_window_ns):
return not (synapse_nonce <= allowed_window_ns)


# Test assuming synapse timeout is the default 12 seconds
@pytest.mark.parametrize(
"nonce_offset_seconds, expected_result",
[(1, True), (3, True), (5, True), (15, True), (18, False), (19, False)],
)
def test_nonce_within_allowed_window(nonce_offset_seconds, expected_result):
mock_synapse = SynapseMock()
current_time_ns = time.time_ns()
synapse_nonce = current_time_ns - (nonce_offset_seconds * NANOSECONDS_IN_SECOND)
allowed_window_ns = allowed_nonce_window_ns(current_time_ns, mock_synapse.timeout)

result = is_nonce_within_allowed_window(synapse_nonce, allowed_window_ns)

assert result == expected_result, f"Expected {expected_result} but got {result}"

0 comments on commit 203ea1e

Please sign in to comment.