Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert nonce implementation fix #1774

Merged
merged 2 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 6 additions & 17 deletions bittensor/axon.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,25 +914,14 @@ async def default_verify(self, synapse: bittensor.Synapse):
# Build the unique endpoint key.
endpoint_key = f"{synapse.dendrite.hotkey}:{synapse.dendrite.uuid}"

# Check the nonce from the endpoint key with 4 second delta
allowedDelta = 4000000000

# Requests must have nonces to be safe from replays
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.
if (
self.nonces.get(endpoint_key) is None
and synapse.dendrite.nonce <= time.time_ns() - allowedDelta
):
raise Exception("Nonce is too old")
# Check the nonce from the endpoint key.
if (
self.nonces.get(endpoint_key) is not None
endpoint_key in self.nonces.keys()
and self.nonces[endpoint_key] is not None
and synapse.dendrite.nonce is not None
and synapse.dendrite.nonce <= self.nonces[endpoint_key]
):
raise Exception("Nonce is too old")
raise Exception("Nonce is too small")

if not keypair.verify(message, synapse.dendrite.signature):
raise Exception(
Expand Down Expand Up @@ -1201,7 +1190,7 @@ async def preprocess(self, request: Request) -> bittensor.Synapse:
{
"version": str(bittensor.__version_as_int__),
"uuid": str(self.axon.uuid),
"nonce": f"{time.time_ns()}",
"nonce": f"{time.monotonic_ns()}",
"status_message": "Success",
"status_code": "100",
}
Expand Down
2 changes: 1 addition & 1 deletion bittensor/dendrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ def preprocess_synapse_for_request(
synapse.dendrite = bittensor.TerminalInfo(
ip=self.external_ip,
version=bittensor.__version_as_int__,
nonce=time.time_ns(),
nonce=time.monotonic_ns(),
uuid=self.uuid,
hotkey=self.keypair.ss58_address,
)
Expand Down
6 changes: 3 additions & 3 deletions bittensor/synapse.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class TerminalInfo(pydantic.BaseModel):
ip (str): IP address of the terminal, crucial for network routing and data transmission.
port (int): Network port used by the terminal, key for establishing network connections.
version (int): Bittensor version running on the terminal, ensuring compatibility between different nodes in the network.
nonce (int): Unix timestamp that linearly increases for each request, ensuring requests cannot be duplicated or repeated
nonce (int): Unique, monotonically increasing number for each terminal, aiding in identifying and ordering network interactions.
uuid (str): Unique identifier for the terminal, fundamental for network security and identification.
hotkey (str): Encoded hotkey string of the terminal wallet, important for transaction and identity verification in the network.
signature (str): Digital signature verifying the tuple of nonce, axon_hotkey, dendrite_hotkey, and uuid, critical for ensuring data authenticity and security.
Expand Down Expand Up @@ -211,10 +211,10 @@ class Config:
cast_int
)

# A Unix timestamp to associate with the terminal
# A unique monotonically increasing integer nonce associate with the terminal
nonce: Optional[int] = pydantic.Field(
title="nonce",
description="A Unix timestamp that prevents replay attacks",
description="A unique monotonically increasing integer nonce associate with the terminal generated from time.monotonic_ns()",
examples=111111,
default=None,
allow_mutation=True,
Expand Down